Skip to content
Myr

generator.js

This file

Path

lib/stdio/generator.js

103 lines · 4.2 KiB · JavaScript

Last changed by

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

History of this file

Take it

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

line 1why, one step back
1import {generatorsToTransform} from './transform.js';

Split files (#837)ehmickybefore adoption672c2d7

line 2why, one step back
2import {getEncodingStartGenerator} from './encoding-end.js';

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 3why, one step back
3import {getLinesGenerator} from './lines.js';

Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd

line 4why, one step back
4import {pipeStreams} from './pipeline.js';

Improve transform speed (#780)ehmickybefore adoption14f040e

line 5why, one step back
5import {isGeneratorOptions, isAsyncGenerator} from './type.js';

Split files (#837)ehmickybefore adoption672c2d7

line 6why, one step back
6import {getValidateTransformReturn} from './validate.js';

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 7why, one step back
7

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 8–21why, one step back
8export const normalizeGenerators = stdioStreams => {
9 const nonGenerators = stdioStreams.filter(({type}) => type !== 'generator');10 const generators = stdioStreams.filter(({type}) => type === 'generator');1112 const newGenerators = Array.from({length: generators.length});1314 for (const [index, stdioStream] of Object.entries(generators)) {15 newGenerators[index] = normalizeGenerator(stdioStream, Number(index), newGenerators);16 }1718 return [...nonGenerators, ...sortGenerators(newGenerators)];19};2021const normalizeGenerator = ({value, ...stdioStream}, index, newGenerators) => {

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 22why, one step back
22 const {transform, final, binary = false, objectMode = false} = isGeneratorOptions(value) ? value : {transform: value};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 23–25why, one step back
23 const objectModes = stdioStream.direction === 'output'
24 ? getOutputObjectModes(objectMode, index, newGenerators)25 : getInputObjectModes(objectMode, index, newGenerators);

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 26why, one step back
26 return {...stdioStream, value: {transform, final, binary, ...objectModes}};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 27–28why, one step back
27};
28

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 29why, one step back
29/*

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 30–54why, one step back
30`objectMode` determines the return value's type, i.e. the `readableObjectMode`.
31The chunk argument's type is based on the previous generator's return value, i.e. the `writableObjectMode` is based on the previous `readableObjectMode`.32The last input's generator is read by `childProcess.stdin` which:33- should not be in `objectMode` for performance reasons.34- can only be strings, Buffers and Uint8Arrays.35Therefore its `readableObjectMode` must be `false`.36The same applies to the first output's generator's `writableObjectMode`.37*/38const getOutputObjectModes = (objectMode, index, newGenerators) => {39 const writableObjectMode = index !== 0 && newGenerators[index - 1].value.readableObjectMode;40 const readableObjectMode = objectMode;41 return {writableObjectMode, readableObjectMode};42};4344const getInputObjectModes = (objectMode, index, newGenerators) => {45 const writableObjectMode = index === 046 ? objectMode47 : newGenerators[index - 1].value.readableObjectMode;48 const readableObjectMode = index !== newGenerators.length - 1 && objectMode;49 return {writableObjectMode, readableObjectMode};50};5152const sortGenerators = newGenerators => newGenerators[0]?.direction === 'input' ? newGenerators.reverse() : newGenerators;5354/*

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 55–57why, one step back
55Generators can be used to transform/filter standard streams.
5657Generators have a simple syntax, yet allows all of the following:

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 58–59why, one step back
58- Sharing `state` between chunks
59- Flushing logic, by using a `final` function

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 60–69why, one step back
60- Asynchronous logic
61- Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`62- Filtering, by using no `yield`6364Therefore, there is no need to allow Node.js or web transform streams.6566The `highWaterMark` is kept as the default value, since this is what `childProcess.std*` uses.6768Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.69*/

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 70why, one step back
70export const generatorToDuplexStream = ({

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 71why, one step back
71 value: {transform, final, binary, writableObjectMode, readableObjectMode},

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 72–74why, one step back
72 encoding,
73 optionName,74}) => {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 75–77why, one step back
75 const generators = [
76 getEncodingStartGenerator(encoding),77 getLinesGenerator(encoding, binary),

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 78–79why, one step back
78 {transform, final},
79 {transform: getValidateTransformReturn(readableObjectMode, optionName)},

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 80why, one step back
80 ].filter(Boolean);

Improve transform speed (#780)ehmickybefore adoption14f040e

lines 81–83why, one step back
81 const transformAsync = isAsyncGenerator(transform);
82 const finalAsync = isAsyncGenerator(final);83 const duplexStream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 84–87why, one step back
84 return {value: duplexStream};
85};8687// `childProcess.stdin|stdout|stderr|stdio` is directly mutated.

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 88why, one step back
88export const pipeGenerator = (spawned, {value, direction, fdNumber}) => {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 89why, one step back
89 if (direction === 'output') {

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 90why, one step back
90 pipeStreams([spawned.stdio[fdNumber]], value);

Fix error handling of transforms (#739)ehmickybefore adoption17993a8

line 91why, one step back
91 } else {

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 92why, one step back
92 pipeStreams([value], spawned.stdio[fdNumber]);

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 93–94why, one step back
93 }
94

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 95why, one step back
95 const streamProperty = PROCESS_STREAM_PROPERTIES[fdNumber];

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 96–99why, one step back
96 if (streamProperty !== undefined) {
97 spawned[streamProperty] = value;98 }99

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 100why, one step back
100 spawned.stdio[fdNumber] = value;

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 101–103why, one step back
101};
102103const PROCESS_STREAM_PROPERTIES = ['stdin', 'stdout', 'stderr'];