Skip to content

Commit

Permalink
馃 Merge PR #55311 feat(@types/node): add stream/consumers by @favna
Browse files Browse the repository at this point in the history
This is a set of new functions added in Node 16.7. As the types are now 16.7.1, we can add them.

NodeJS docs: https://nodejs.org/dist/latest-v16.x/docs/api/webstreams.html#webstreams_utility_consumers
  • Loading branch information
favna committed Aug 27, 2021
1 parent 302bd69 commit 07e9b3d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions types/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
/// <reference path="repl.d.ts" />
/// <reference path="stream.d.ts" />
/// <reference path="stream/promises.d.ts" />
/// <reference path="stream/consumers.d.ts" />
/// <reference path="stream/web.d.ts" />
/// <reference path="string_decoder.d.ts" />
/// <reference path="timers.d.ts" />
Expand Down
2 changes: 2 additions & 0 deletions types/node/stream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
declare module 'stream' {
import { EventEmitter, Abortable } from 'node:events';
import * as streamPromises from 'node:stream/promises';
import * as streamConsumers from 'node:stream/consumers';
class internal extends EventEmitter {
pipe<T extends NodeJS.WritableStream>(
destination: T,
Expand Down Expand Up @@ -1169,6 +1170,7 @@ declare module 'stream' {
unref(): void;
}
const promises: typeof streamPromises;
const consumers: typeof streamConsumers;
}
export = internal;
}
Expand Down
25 changes: 25 additions & 0 deletions types/node/stream/consumers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Duplicates of interface in lib.dom.ts.
// Duplicated here rather than referencing lib.dom.ts because doing so causes lib.dom.ts to be loaded for "test-all"
// Which in turn causes tests to pass that shouldn't pass.
//
// This interface is not, and should not be, exported.
interface Blob {
readonly size: number;
readonly type: string;
arrayBuffer(): Promise<ArrayBuffer>;
slice(start?: number, end?: number, contentType?: string): Blob;
stream(): NodeJS.ReadableStream;
text(): Promise<string>;
}

declare module 'stream/consumers' {
import { Readable } from 'node:stream';
function buffer(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<Buffer>;
function text(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<string>;
function arrayBuffer(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<ArrayBuffer>;
function blob(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<Blob>;
function json(stream: NodeJS.ReadableStream | Readable | AsyncIterator<any>): Promise<unknown>;
}
declare module 'node:stream/consumers' {
export * from 'stream/consumers';
}
22 changes: 22 additions & 0 deletions types/node/test/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip, constants } from 'node:zlib';
import assert = require('node:assert');
import { Http2ServerResponse } from 'node:http2';
import { text, json, buffer } from 'node:stream/consumers';
import { pipeline as pipelinePromise } from 'node:stream/promises';
import { stdout } from 'node:process';
import 'node:stream/web';
Expand Down Expand Up @@ -455,6 +456,27 @@ async function streamPipelineAsyncPromiseAbortTransform() {
});
}

async function readableToString() {
const r = createReadStream('file.txt');

// $ExpectType string
await text(r);
}

async function readableToJson() {
const r = createReadStream('file.txt');

// $ExpectType unknown
await json(r);
}

async function readableToBuffer() {
const r = createReadStream('file.txt');

// $ExpectType Buffer
await buffer(r);
}

// http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
function stream_readable_pipe_test() {
const rs = createReadStream(Buffer.from('file.txt'));
Expand Down

0 comments on commit 07e9b3d

Please sign in to comment.