Skip to content

Commit 2b74f98

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Add Flow lib defs for Node.js streams fromWeb/toWeb (facebook#54615)
Summary: Pull Request resolved: facebook#54615 Since v17 (and stable since v22.17), Node.js has helper methods for converting [Node streams](https://nodejs.org/api/stream.html) to/from [Web streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) (e.g., as returned by global `fetch`). This adds the Flow typings. - Node docs: https://nodejs.org/api/stream.html#streamreadablefromwebreadablestream-options Changelog: [General][Internal] Monorepo Flow typings Reviewed By: vzaidman Differential Revision: D87543561 fbshipit-source-id: c76b9014ab3ff61f9ce57be1e01bf456b676433f
1 parent 17cba6e commit 2b74f98

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

flow-typed/environment/node.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,14 @@ declare class stream$Readable extends stream$Stream {
25392539
static from(
25402540
iterable: Iterable<any> | AsyncIterable<any>,
25412541
options?: readableStreamOptions,
2542-
): stream$Readable;
2542+
): this;
2543+
2544+
static fromWeb(
2545+
readableStream: ReadableStream,
2546+
options?: readableStreamOptions,
2547+
): this;
2548+
2549+
static toWeb(streamReadable: stream$Readable): ReadableStream;
25432550

25442551
constructor(options?: readableStreamOptions): void;
25452552
destroy(error?: Error): this;
@@ -2586,6 +2593,13 @@ type writableStreamOptions = {
25862593
...
25872594
};
25882595
declare class stream$Writable extends stream$Stream {
2596+
static fromWeb(
2597+
writableStream: WritableStream,
2598+
options?: writableStreamOptions,
2599+
): this;
2600+
2601+
static toWeb(streamWritable: stream$Writable): WritableStream;
2602+
25892603
constructor(options?: writableStreamOptions): void;
25902604
cork(): void;
25912605
destroy(error?: Error): this;
@@ -2627,10 +2641,6 @@ declare class stream$Writable extends stream$Stream {
26272641
_final(callback: (error?: Error) => void): void;
26282642
}
26292643

2630-
//According to the NodeJS docs:
2631-
//"Since JavaScript doesn't have multiple prototypal inheritance, this class
2632-
//prototypally inherits from Readable, and then parasitically from Writable."
2633-
//Source: <https://nodejs.org/api/stream.html#stream_class_stream_duplex_1
26342644
type duplexStreamOptions = writableStreamOptions &
26352645
readableStreamOptions & {
26362646
allowHalfOpen?: boolean,
@@ -2641,6 +2651,28 @@ type duplexStreamOptions = writableStreamOptions &
26412651
...
26422652
};
26432653
declare class stream$Duplex extends stream$Readable mixins stream$Writable {
2654+
// This is an unusual class at runtime, per the docs it "prototypally extends from Readable,
2655+
// and then parasitically from Writable." Its static methods have incompatible signatures
2656+
// with Readable's, which Flow doesn't like.
2657+
// See https://nodejs.org/api/stream.html#stream_class_stream_duplex_1
2658+
2659+
// $FlowFixMe[incompatible-exact] See above
2660+
// $FlowFixMe[incompatible-type] See above
2661+
static fromWeb(
2662+
pair: $ReadOnly<{
2663+
readable: ReadableStream,
2664+
writable: WritableStream,
2665+
}>,
2666+
options?: duplexStreamOptions,
2667+
): this;
2668+
2669+
// $FlowFixMe[incompatible-type] See above
2670+
static toWeb(streamDuplex: stream$Duplex): {
2671+
readable: ReadableStream,
2672+
writable: WritableStream,
2673+
...
2674+
};
2675+
26442676
constructor(options?: duplexStreamOptions): void;
26452677
}
26462678
type transformStreamOptions = duplexStreamOptions & {

0 commit comments

Comments
 (0)