Skip to content

Commit 558d030

Browse files
authored
🤖 Merge PR DefinitelyTyped#72028 [node] stream/consumers: cleanup, correct references to ReadableStream by @Renegade334
1 parent dbbb811 commit 558d030

File tree

6 files changed

+126
-62
lines changed

6 files changed

+126
-62
lines changed

‎types/node/stream/consumers.d.ts‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
/**
2+
* The utility consumer functions provide common options for consuming
3+
* streams.
4+
* @since v16.7.0
5+
*/
16
declare module "stream/consumers" {
27
import { Blob as NodeBlob } from "node:buffer";
3-
import { Readable } from "node:stream";
4-
function buffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<Buffer>;
5-
function text(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<string>;
6-
function arrayBuffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<ArrayBuffer>;
7-
function blob(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<NodeBlob>;
8-
function json(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<unknown>;
8+
import { ReadableStream as WebReadableStream } from "node:stream/web";
9+
/**
10+
* @since v16.7.0
11+
* @returns Fulfills with an `ArrayBuffer` containing the full contents of the stream.
12+
*/
13+
function arrayBuffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<ArrayBuffer>;
14+
/**
15+
* @since v16.7.0
16+
* @returns Fulfills with a `Blob` containing the full contents of the stream.
17+
*/
18+
function blob(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<NodeBlob>;
19+
/**
20+
* @since v16.7.0
21+
* @returns Fulfills with a `Buffer` containing the full contents of the stream.
22+
*/
23+
function buffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<Buffer>;
24+
/**
25+
* @since v16.7.0
26+
* @returns Fulfills with the contents of the stream parsed as a
27+
* UTF-8 encoded string that is then passed through `JSON.parse()`.
28+
*/
29+
function json(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<unknown>;
30+
/**
31+
* @since v16.7.0
32+
* @returns Fulfills with the contents of the stream parsed as a UTF-8 encoded string.
33+
*/
34+
function text(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<string>;
935
}
1036
declare module "node:stream/consumers" {
1137
export * from "stream/consumers";

‎types/node/test/stream.ts‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Blob } from "node:buffer";
1818
import { Http2ServerResponse } from "node:http2";
1919
import { performance } from "node:perf_hooks";
2020
import { stdout } from "node:process";
21-
import { arrayBuffer, blob, buffer, json, text } from "node:stream/consumers";
21+
import * as consumers from "node:stream/consumers";
2222
import { finished as finishedPromise, pipeline as pipelinePromise } from "node:stream/promises";
2323
import { ReadableStream, ReadableStreamBYOBReader, TransformStream, WritableStream } from "node:stream/web";
2424
import { setInterval as every, setTimeout as wait } from "node:timers/promises";
@@ -501,25 +501,18 @@ async function streamPipelineAsyncPromiseOptions() {
501501
}
502502

503503
async function testConsumers() {
504-
const r = createReadStream("file.txt");
504+
let consumable!: ReadableStream | Readable | AsyncGenerator<any>;
505505

506-
// $ExpectType string
507-
await text(r);
508-
// $ExpectType unknown
509-
await json(r);
510-
// $ExpectType Buffer || Buffer<ArrayBufferLike>
511-
await buffer(r);
512506
// $ExpectType ArrayBuffer
513-
await arrayBuffer(r);
507+
await consumers.arrayBuffer(consumable);
514508
// $ExpectType Blob
515-
await blob(r);
516-
517-
const iterable: AsyncGenerator<Buffer> = async function*() {}();
518-
await buffer(iterable);
519-
520-
const iterator: AsyncIterator<Buffer> = { next: () => iterable.next() };
521-
// @ts-expect-error
522-
await buffer(iterator);
509+
await consumers.blob(consumable);
510+
// $ExpectType Buffer || Buffer<ArrayBufferLike>
511+
await consumers.buffer(consumable);
512+
// $ExpectType unknown
513+
await consumers.json(consumable);
514+
// $ExpectType string
515+
await consumers.text(consumable);
523516
}
524517

525518
// https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

‎types/node/v18/stream/consumers.d.ts‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
/**
2+
* The utility consumer functions provide common options for consuming
3+
* streams.
4+
* @since v16.7.0
5+
*/
16
declare module "stream/consumers" {
27
import { Blob as NodeBlob } from "node:buffer";
3-
import { Readable } from "node:stream";
4-
function buffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<Buffer>;
5-
function text(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<string>;
6-
function arrayBuffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<ArrayBuffer>;
7-
function blob(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<NodeBlob>;
8-
function json(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<unknown>;
8+
import { ReadableStream as WebReadableStream } from "node:stream/web";
9+
/**
10+
* @since v16.7.0
11+
* @returns Fulfills with an `ArrayBuffer` containing the full contents of the stream.
12+
*/
13+
function arrayBuffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<ArrayBuffer>;
14+
/**
15+
* @since v16.7.0
16+
* @returns Fulfills with a `Blob` containing the full contents of the stream.
17+
*/
18+
function blob(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<NodeBlob>;
19+
/**
20+
* @since v16.7.0
21+
* @returns Fulfills with a `Buffer` containing the full contents of the stream.
22+
*/
23+
function buffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<Buffer>;
24+
/**
25+
* @since v16.7.0
26+
* @returns Fulfills with the contents of the stream parsed as a
27+
* UTF-8 encoded string that is then passed through `JSON.parse()`.
28+
*/
29+
function json(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<unknown>;
30+
/**
31+
* @since v16.7.0
32+
* @returns Fulfills with the contents of the stream parsed as a UTF-8 encoded string.
33+
*/
34+
function text(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<string>;
935
}
1036
declare module "node:stream/consumers" {
1137
export * from "stream/consumers";

‎types/node/v18/test/stream.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Blob } from "node:buffer";
1717
import { Http2ServerResponse } from "node:http2";
1818
import { performance } from "node:perf_hooks";
1919
import { stdout } from "node:process";
20-
import { arrayBuffer, blob, buffer, json, text } from "node:stream/consumers";
20+
import * as consumers from "node:stream/consumers";
2121
import { finished as finishedPromise, pipeline as pipelinePromise } from "node:stream/promises";
2222
import { ReadableStream, ReadableStreamBYOBReader, TransformStream, WritableStream } from "node:stream/web";
2323
import { setInterval as every } from "node:timers/promises";
@@ -496,18 +496,18 @@ async function streamPipelineAsyncPromiseOptions() {
496496
}
497497

498498
async function testConsumers() {
499-
const r = createReadStream("file.txt");
499+
let consumable!: ReadableStream | Readable | AsyncGenerator<any>;
500500

501-
// $ExpectType string
502-
await text(r);
503-
// $ExpectType unknown
504-
await json(r);
505-
// $ExpectType Buffer || Buffer<ArrayBufferLike>
506-
await buffer(r);
507501
// $ExpectType ArrayBuffer
508-
await arrayBuffer(r);
502+
await consumers.arrayBuffer(consumable);
509503
// $ExpectType Blob
510-
await blob(r);
504+
await consumers.blob(consumable);
505+
// $ExpectType Buffer || Buffer<ArrayBufferLike>
506+
await consumers.buffer(consumable);
507+
// $ExpectType unknown
508+
await consumers.json(consumable);
509+
// $ExpectType string
510+
await consumers.text(consumable);
511511
}
512512

513513
// https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

‎types/node/v20/stream/consumers.d.ts‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
/**
2+
* The utility consumer functions provide common options for consuming
3+
* streams.
4+
* @since v16.7.0
5+
*/
16
declare module "stream/consumers" {
27
import { Blob as NodeBlob } from "node:buffer";
3-
import { Readable } from "node:stream";
4-
function buffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<Buffer>;
5-
function text(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<string>;
6-
function arrayBuffer(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<ArrayBuffer>;
7-
function blob(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<NodeBlob>;
8-
function json(stream: NodeJS.ReadableStream | Readable | AsyncIterable<any>): Promise<unknown>;
8+
import { ReadableStream as WebReadableStream } from "node:stream/web";
9+
/**
10+
* @since v16.7.0
11+
* @returns Fulfills with an `ArrayBuffer` containing the full contents of the stream.
12+
*/
13+
function arrayBuffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<ArrayBuffer>;
14+
/**
15+
* @since v16.7.0
16+
* @returns Fulfills with a `Blob` containing the full contents of the stream.
17+
*/
18+
function blob(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<NodeBlob>;
19+
/**
20+
* @since v16.7.0
21+
* @returns Fulfills with a `Buffer` containing the full contents of the stream.
22+
*/
23+
function buffer(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<Buffer>;
24+
/**
25+
* @since v16.7.0
26+
* @returns Fulfills with the contents of the stream parsed as a
27+
* UTF-8 encoded string that is then passed through `JSON.parse()`.
28+
*/
29+
function json(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<unknown>;
30+
/**
31+
* @since v16.7.0
32+
* @returns Fulfills with the contents of the stream parsed as a UTF-8 encoded string.
33+
*/
34+
function text(stream: WebReadableStream | NodeJS.ReadableStream | AsyncIterable<any>): Promise<string>;
935
}
1036
declare module "node:stream/consumers" {
1137
export * from "stream/consumers";

‎types/node/v20/test/stream.ts‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Blob } from "node:buffer";
1818
import { Http2ServerResponse } from "node:http2";
1919
import { performance } from "node:perf_hooks";
2020
import { stdout } from "node:process";
21-
import { arrayBuffer, blob, buffer, json, text } from "node:stream/consumers";
21+
import * as consumers from "node:stream/consumers";
2222
import { finished as finishedPromise, pipeline as pipelinePromise } from "node:stream/promises";
2323
import { ReadableStream, ReadableStreamBYOBReader, TransformStream, WritableStream } from "node:stream/web";
2424
import { setInterval as every, setTimeout as wait } from "node:timers/promises";
@@ -501,25 +501,18 @@ async function streamPipelineAsyncPromiseOptions() {
501501
}
502502

503503
async function testConsumers() {
504-
const r = createReadStream("file.txt");
504+
let consumable!: ReadableStream | Readable | AsyncGenerator<any>;
505505

506-
// $ExpectType string
507-
await text(r);
508-
// $ExpectType unknown
509-
await json(r);
510-
// $ExpectType Buffer || Buffer<ArrayBufferLike>
511-
await buffer(r);
512506
// $ExpectType ArrayBuffer
513-
await arrayBuffer(r);
507+
await consumers.arrayBuffer(consumable);
514508
// $ExpectType Blob
515-
await blob(r);
516-
517-
const iterable: AsyncGenerator<Buffer> = async function*() {}();
518-
await buffer(iterable);
519-
520-
const iterator: AsyncIterator<Buffer> = { next: () => iterable.next() };
521-
// @ts-expect-error
522-
await buffer(iterator);
509+
await consumers.blob(consumable);
510+
// $ExpectType Buffer || Buffer<ArrayBufferLike>
511+
await consumers.buffer(consumable);
512+
// $ExpectType unknown
513+
await consumers.json(consumable);
514+
// $ExpectType string
515+
await consumers.text(consumable);
523516
}
524517

525518
// https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

0 commit comments

Comments
 (0)