diff --git a/test/types/readable.test-d.ts b/test/types/readable.test-d.ts new file mode 100644 index 00000000000..671f6f3c6ef --- /dev/null +++ b/test/types/readable.test-d.ts @@ -0,0 +1,34 @@ +import { expectAssignable } from 'tsd' +import BodyReadable from '../../types/readable' +import { Blob } from 'buffer' + +expectAssignable(new BodyReadable()) + +{ + const readable = new BodyReadable() + + // dump + expectAssignable>(readable.dump()) + expectAssignable>(readable.dump({ limit: 123 })) + + // text + expectAssignable>(readable.text()) + + // json + expectAssignable>(readable.json()) + + // blob + expectAssignable>(readable.blob()) + + // arrayBuffer + expectAssignable>(readable.arrayBuffer()) + + // formData + expectAssignable>(readable.formData()) + + // bodyUsed + expectAssignable(readable.bodyUsed) + + // body + expectAssignable(readable.body) +} diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index fefe6d150e6..0fe6cbfb9d4 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -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; @@ -75,7 +76,7 @@ declare namespace Dispatcher { /** Default: 0 */ maxRedirections?: number; /** Default: `null` */ - onInfo?: (info: {statusCode: number, headers: Record}) => void; + onInfo?: (info: { statusCode: number, headers: Record }) => void; /** Default: `null` */ responseHeader?: 'raw' | null; } @@ -107,7 +108,7 @@ declare namespace Dispatcher { export interface ResponseData { statusCode: number; headers: IncomingHttpHeaders; - body: Readable & BodyMixin; + body: BodyReadable & BodyMixin; trailers: Record; opaque: unknown; context: object; @@ -116,7 +117,7 @@ declare namespace Dispatcher { statusCode: number; headers: IncomingHttpHeaders; opaque: unknown; - body: Readable; + body: BodyReadable; context: object; } export interface StreamData { diff --git a/types/readable.d.ts b/types/readable.d.ts new file mode 100644 index 00000000000..0d82ba86776 --- /dev/null +++ b/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 + + /** Consumes and returns the body as a JavaScript Object + * https://fetch.spec.whatwg.org/#dom-body-json + */ + json(): Promise + + /** Consumes and returns the body as a Blob + * https://fetch.spec.whatwg.org/#dom-body-blob + */ + blob(): Promise + + /** Consumes and returns the body as an ArrayBuffer + * https://fetch.spec.whatwg.org/#dom-body-arraybuffer + */ + arrayBuffer(): Promise + + /** Not implemented + * + * https://fetch.spec.whatwg.org/#dom-body-formdata + */ + formData(): Promise + + /** 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 +}