Skip to content

Commit

Permalink
fix: Improve BodyReadable type definition (nodejs#1259)
Browse files Browse the repository at this point in the history
* fix: Improve BodyReadable type definition

* fix: Add tests for BodyReadable type
  • Loading branch information
umarov authored and crysmags committed Feb 27, 2024
1 parent 96b7c61 commit 8742379
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
34 changes: 34 additions & 0 deletions test/types/readable.test-d.ts
@@ -0,0 +1,34 @@
import { expectAssignable } from 'tsd'
import BodyReadable from '../../types/readable'
import { Blob } from 'buffer'

expectAssignable<BodyReadable>(new BodyReadable())

{
const readable = new BodyReadable()

// dump
expectAssignable<Promise<void>>(readable.dump())
expectAssignable<Promise<void>>(readable.dump({ limit: 123 }))

// text
expectAssignable<Promise<string>>(readable.text())

// json
expectAssignable<Promise<any>>(readable.json())

// blob
expectAssignable<Promise<Blob>>(readable.blob())

// arrayBuffer
expectAssignable<Promise<ArrayBuffer>>(readable.arrayBuffer())

// formData
expectAssignable<Promise<never>>(readable.formData())

// bodyUsed
expectAssignable<boolean>(readable.bodyUsed)

// body
expectAssignable<never | undefined>(readable.body)
}
7 changes: 4 additions & 3 deletions types/dispatcher.d.ts
Expand Up @@ -3,6 +3,7 @@ import { Duplex, Readable, Writable } from 'stream'
import { EventEmitter } from 'events'
import { IncomingHttpHeaders } from 'http'
import { Blob } from 'buffer'
import BodyReadable from './readable'

type AbortSignal = unknown;

Expand Down Expand Up @@ -75,7 +76,7 @@ declare namespace Dispatcher {
/** Default: 0 */
maxRedirections?: number;
/** Default: `null` */
onInfo?: (info: {statusCode: number, headers: Record<string, string | string[]>}) => void;
onInfo?: (info: { statusCode: number, headers: Record<string, string | string[]> }) => void;
/** Default: `null` */
responseHeader?: 'raw' | null;
}
Expand Down Expand Up @@ -107,7 +108,7 @@ declare namespace Dispatcher {
export interface ResponseData {
statusCode: number;
headers: IncomingHttpHeaders;
body: Readable & BodyMixin;
body: BodyReadable & BodyMixin;
trailers: Record<string, string>;
opaque: unknown;
context: object;
Expand All @@ -116,7 +117,7 @@ declare namespace Dispatcher {
statusCode: number;
headers: IncomingHttpHeaders;
opaque: unknown;
body: Readable;
body: BodyReadable;
context: object;
}
export interface StreamData {
Expand Down
61 changes: 61 additions & 0 deletions types/readable.d.ts
@@ -0,0 +1,61 @@
import { Readable } from "stream";
import { Blob } from 'buffer'

export = BodyReadable

declare class BodyReadable extends Readable {
constructor(
resume?: (this: Readable, size: number) => void | null,
abort?: () => void | null,
contentType?: string
)

/** Consumes and returns the body as a string
* https://fetch.spec.whatwg.org/#dom-body-text
*/
text(): Promise<string>

/** Consumes and returns the body as a JavaScript Object
* https://fetch.spec.whatwg.org/#dom-body-json
*/
json(): Promise<any>

/** Consumes and returns the body as a Blob
* https://fetch.spec.whatwg.org/#dom-body-blob
*/
blob(): Promise<Blob>

/** Consumes and returns the body as an ArrayBuffer
* https://fetch.spec.whatwg.org/#dom-body-arraybuffer
*/
arrayBuffer(): Promise<ArrayBuffer>

/** Not implemented
*
* https://fetch.spec.whatwg.org/#dom-body-formdata
*/
formData(): Promise<never>

/** Returns true if the body is not null and the body has been consumed
*
* Otherwise, returns false
*
* https://fetch.spec.whatwg.org/#dom-body-bodyused
*/
readonly bodyUsed: boolean

/** Throws on node 16.6.0
*
* If body is null, it should return null as the body
*
* If body is not null, should return the body as a ReadableStream
*
* https://fetch.spec.whatwg.org/#dom-body-body
*/
readonly body: never | undefined

/** Dumps the response body by reading `limit` number of bytes.
* @param opts.limit Number of bytes to read (optional) - Default: 262144
*/
dump(opts?: { limit: number }): Promise<void>
}

0 comments on commit 8742379

Please sign in to comment.