@@ -1923,7 +1923,7 @@ failure, this can cause event listener leaks and swallowed errors.
19231923added: REPLACEME
19241924-->
19251925
1926- * ` streams ` {Stream[ ] }
1926+ * ` streams ` {Stream[ ] |Iterable [ ] |AsyncIterable [ ] |Function [ ] }
19271927* Returns: {stream.Duplex}
19281928
19291929Combines two or more streams into a ` Duplex ` stream that writes to the
@@ -1937,6 +1937,9 @@ when passing streams to `stream.pipeline`, typically the first stream is
19371937a readable stream and the last a writable stream, forming a closed
19381938circuit.
19391939
1940+ If passed a ` Function ` it must be a factory method taking a ` source `
1941+ ` Iterable ` .
1942+
19401943``` mjs
19411944import { compose , Transform } from ' stream' ;
19421945
@@ -1946,11 +1949,11 @@ const removeSpaces = new Transform({
19461949 }
19471950});
19481951
1949- const toUpper = new Transform ( {
1950- transform ( chunk , encoding , callback ) {
1951- callback ( null , String (chunk).toUpperCase () );
1952+ async function * toUpper ( source ) {
1953+ for await ( const chunk of source ) {
1954+ yield String (chunk).toUpperCase ();
19521955 }
1953- });
1956+ }
19541957
19551958let res = ' ' ;
19561959for await (const buf of compose (removeSpaces, toUpper).end (' hello world' )) {
@@ -1960,6 +1963,48 @@ for await (const buf of compose(removeSpaces, toUpper).end('hello world')) {
19601963console .log (res); // prints 'HELLOWORLD'
19611964```
19621965
1966+ ` stream.compose ` can be used to convert async iterables, generators and
1967+ functions into streams.
1968+
1969+ * ` AsyncIterable ` converts into a readable ` Duplex ` . Cannot yield
1970+ ` null ` .
1971+ * ` AsyncGeneratorFunction ` converts into a readable/writable transform ` Duplex ` .
1972+ Must take a source ` AsyncIterable ` as first parameter. Cannot yield
1973+ ` null ` .
1974+ * ` AsyncFunction ` converts into a writable ` Duplex ` . Must return
1975+ either ` null ` or ` undefined ` .
1976+
1977+ ``` mjs
1978+ import { compose } from ' stream' ;
1979+ import { finished } from ' stream/promises' ;
1980+
1981+ // Convert AsyncIterable into readable Duplex.
1982+ const s1 = compose (async function * () {
1983+ yield ' Hello' ;
1984+ yield ' World' ;
1985+ }());
1986+
1987+ // Convert AsyncGenerator into transform Duplex.
1988+ const s2 = compose (async function * (source ) {
1989+ for await (const chunk of source ) {
1990+ yield String (chunk).toUpperCase ();
1991+ }
1992+ });
1993+
1994+ let res = ' ' ;
1995+
1996+ // Convert AsyncFunction into writable Duplex.
1997+ const s3 = compose (async function (source ) {
1998+ for await (const chunk of source ) {
1999+ res += chunk;
2000+ }
2001+ });
2002+
2003+ await finished (compose (s1, s2, s3));
2004+
2005+ console .log (res); // prints 'HELLOWORLD'
2006+ ```
2007+
19632008### ` stream.Readable.from(iterable, [options]) `
19642009<!-- YAML
19652010added:
0 commit comments