Skip to content
Myr

duplex.js

This file

Path

lib/stdio/duplex.js

49 lines · 2.0 KiB · JavaScript

Last changed by

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

History of this file

Take it

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 1–13why, one step back
1import {Duplex, Readable, PassThrough, getDefaultHighWaterMark} from 'node:stream';
23/*4Transform an array of generator functions into a `Duplex`.56The `Duplex` is created by `Duplex.from()` made of a writable stream and a readable stream, piped to each other.7- The writable stream is a simple `PassThrough`, so it only forwards data to the readable part.8- The `PassThrough` is read as an iterable using `passThrough.iterator()`.9- This iterable is transformed to another iterable, by applying the encoding generators.10 Those convert the chunk type from `Buffer` to `string | Uint8Array` depending on the encoding option.11- This new iterable is transformed again to another one, this time by applying the user-supplied generator.12- Finally, `Readable.from()` is used to convert this final iterable to a `Readable` stream.13*/

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 14–19why, one step back
14export const generatorsToDuplex = (generators, {writableObjectMode, readableObjectMode}) => {
15 const passThrough = new PassThrough({16 objectMode: writableObjectMode,17 highWaterMark: getDefaultHighWaterMark(writableObjectMode),18 destroy: destroyPassThrough,19 });

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 20–25why, one step back
20 let iterable = passThrough.iterator();
2122 for (const generator of generators) {23 iterable = generator(iterable);24 }25

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 26–29why, one step back
26 const readableStream = Readable.from(iterable, {
27 objectMode: readableObjectMode,28 highWaterMark: getDefaultHighWaterMark(readableObjectMode),29 });

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 30–45why, one step back
30 const duplexStream = Duplex.from({writable: passThrough, readable: readableStream});
31 return duplexStream;32};3334/*35When an error is thrown in a generator, the PassThrough is aborted.3637This creates a race condition for which error is propagated, due to the Duplex throwing twice:38- The writable side is aborted (PassThrough)39- The readable side propagate the generator's error4041In order for the later to win that race, we need to wait one microtask.42- However we wait one macrotask instead to be on the safe side43- See https://github.com/sindresorhus/execa/pull/693#discussion_r145380945044*/45const destroyPassThrough = (error, done) => {

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 46why, one step back
46 setImmediate(() => {

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 47why, one step back
47 done(error);

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 48why, one step back
48 });

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 49why, one step back
49};