Skip to content

Commit a6d50a1

Browse files
ronagtargos
authored andcommitted
stream: duplexify
PR-URL: #39519 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Backport-PR-URL: #39820
1 parent af7047a commit a6d50a1

File tree

9 files changed

+655
-17
lines changed

9 files changed

+655
-17
lines changed

doc/api/stream.md

+28
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,34 @@ added: REPLACEME
19661966

19671967
Returns whether the stream has been read from or cancelled.
19681968

1969+
### `stream.Duplex.from(src)`
1970+
<!-- YAML
1971+
added: REPLACEME
1972+
-->
1973+
1974+
* `src` {Stream|Blob|ArrayBuffer|string|Iterable|AsyncIterable|
1975+
AsyncGeneratorFunction|AsyncFunction|Promise|Object}
1976+
1977+
A utility method for creating duplex streams.
1978+
1979+
* `Stream` converts writable stream into writable `Duplex` and readable stream
1980+
to `Duplex`.
1981+
* `Blob` converts into readable `Duplex`.
1982+
* `string` converts into readable `Duplex`.
1983+
* `ArrayBuffer` converts into readable `Duplex`.
1984+
* `AsyncIterable` converts into a readable `Duplex`. Cannot yield
1985+
`null`.
1986+
* `AsyncGeneratorFunction` converts into a readable/writable transform
1987+
`Duplex`. Must take a source `AsyncIterable` as first parameter. Cannot yield
1988+
`null`.
1989+
* `AsyncFunction` converts into a writable `Duplex`. Must return
1990+
either `null` or `undefined`
1991+
* `Object ({ writable, readable })` converts `readable` and
1992+
`writable` into `Stream` and then combines them into `Duplex` where the
1993+
`Duplex` will write to the `writable` and read from the `readable`.
1994+
* `Promise` converts into readable `Duplex`. Value `null` is ignored.
1995+
* Returns: {stream.Duplex}
1996+
19691997
### `stream.addAbortSignal(signal, stream)`
19701998
<!-- YAML
19711999
added: v15.4.0

lib/internal/streams/destroy.js

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ function isRequest(stream) {
307307

308308
// Normalize destroy for legacy.
309309
function destroyer(stream, err) {
310+
if (!stream) return;
310311
if (isRequest(stream)) return stream.abort();
311312
if (isRequest(stream.req)) return stream.req.abort();
312313
if (typeof stream.destroy === 'function') return stream.destroy(err);

lib/internal/streams/duplex.js

+9
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,12 @@ ObjectDefineProperties(Duplex.prototype, {
108108
}
109109
}
110110
});
111+
112+
let duplexify;
113+
114+
Duplex.from = function(body) {
115+
if (!duplexify) {
116+
duplexify = require('internal/streams/duplexify');
117+
}
118+
return duplexify(body, 'body');
119+
};

0 commit comments

Comments
 (0)