Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v16.x backport] stream: duplexify #39820

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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