From ea09812160e5fcead3e6617adb2a9d2b6b86db42 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 25 Jul 2021 20:01:43 +0200 Subject: [PATCH] stream: duplexify PR-URL: https://github.com/nodejs/node/pull/39519 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Backport-PR-URL: https://github.com/nodejs/node/pull/39820 --- doc/api/stream.md | 28 ++ lib/internal/streams/destroy.js | 1 + lib/internal/streams/duplex.js | 9 + lib/internal/streams/duplexify.js | 372 +++++++++++++++++++++++ lib/internal/streams/pipeline.js | 10 +- lib/internal/streams/readable.js | 21 +- lib/internal/streams/utils.js | 53 ++++ test/parallel/test-stream-duplex-from.js | 120 ++++++++ tools/doc/type-parser.mjs | 4 + 9 files changed, 602 insertions(+), 16 deletions(-) create mode 100644 lib/internal/streams/duplexify.js create mode 100644 test/parallel/test-stream-duplex-from.js diff --git a/doc/api/stream.md b/doc/api/stream.md index 79f524c152f3ce..26734fad48b40e 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1966,6 +1966,34 @@ added: REPLACEME Returns whether the stream has been read from or cancelled. +### `stream.Duplex.from(src)` + + +* `src` {Stream|Blob|ArrayBuffer|string|Iterable|AsyncIterable| + AsyncGeneratorFunction|AsyncFunction|Promise|Object} + +A utility method for creating duplex streams. + +* `Stream` converts writable stream into writable `Duplex` and readable stream + to `Duplex`. +* `Blob` converts into readable `Duplex`. +* `string` converts into readable `Duplex`. +* `ArrayBuffer` converts into readable `Duplex`. +* `AsyncIterable` converts into a readable `Duplex`. Cannot yield + `null`. +* `AsyncGeneratorFunction` converts into a readable/writable transform + `Duplex`. Must take a source `AsyncIterable` as first parameter. Cannot yield + `null`. +* `AsyncFunction` converts into a writable `Duplex`. Must return + either `null` or `undefined` +* `Object ({ writable, readable })` converts `readable` and + `writable` into `Stream` and then combines them into `Duplex` where the + `Duplex` will write to the `writable` and read from the `readable`. +* `Promise` converts into readable `Duplex`. Value `null` is ignored. +* Returns: {stream.Duplex} + ### `stream.addAbortSignal(signal, stream)`