Skip to content

Commit

Permalink
stream: duplexify
Browse files Browse the repository at this point in the history
PR-URL: #39519
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <ckbenjamingr@gmail.com>
Backport-PR-URL: #39820
  • Loading branch information
ronag authored and targos committed Aug 23, 2021
1 parent af7047a commit a6d50a1
Show file tree
Hide file tree
Showing 9 changed files with 655 additions and 17 deletions.
28 changes: 28 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,34 @@ added: REPLACEME

Returns whether the stream has been read from or cancelled.

### `stream.Duplex.from(src)`
<!-- YAML
added: REPLACEME
-->

* `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)`
<!-- YAML
added: v15.4.0
Expand Down
1 change: 1 addition & 0 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ function isRequest(stream) {

// Normalize destroy for legacy.
function destroyer(stream, err) {
if (!stream) return;
if (isRequest(stream)) return stream.abort();
if (isRequest(stream.req)) return stream.req.abort();
if (typeof stream.destroy === 'function') return stream.destroy(err);
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/streams/duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ ObjectDefineProperties(Duplex.prototype, {
}
}
});

let duplexify;

Duplex.from = function(body) {
if (!duplexify) {
duplexify = require('internal/streams/duplexify');
}
return duplexify(body, 'body');
};
Loading

0 comments on commit a6d50a1

Please sign in to comment.