generator.js
This file
Path
lib/stdio/generator.js
114 lines · 4.4 KiB · JavaScript
Last changed by
Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
History of this fileTake it
Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 1–2why, one step back1import {generatorsToDuplex} from './duplex.js';2import {getEncodingStartGenerator} from './encoding.js';Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 3–4why, one step back3import {getLinesGenerator} from './lines.js';4import {isGeneratorOptions} from './type.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 5why, one step back5import {isBinary} from './utils.js';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 6why, one step back6Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 7–27why, one step back7export const normalizeGenerators = stdioStreams => {8 const nonGenerators = stdioStreams.filter(({type}) => type !== 'generator');9 const generators = stdioStreams.filter(({type}) => type === 'generator');1011 const newGenerators = Array.from({length: generators.length});1213 for (const [index, stdioStream] of Object.entries(generators)) {14 newGenerators[index] = normalizeGenerator(stdioStream, Number(index), newGenerators);15 }1617 return [...nonGenerators, ...sortGenerators(newGenerators)];18};1920const normalizeGenerator = ({value, ...stdioStream}, index, newGenerators) => {21 const {transform, binary = false, objectMode = false} = isGeneratorOptions(value) ? value : {transform: value};22 const objectModes = stdioStream.direction === 'output'23 ? getOutputObjectModes(objectMode, index, newGenerators)24 : getInputObjectModes(objectMode, index, newGenerators);25 return {...stdioStream, value: {transform, binary, ...objectModes}};26};27Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 28why, one step back28/*Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 29–53why, one step back29`objectMode` determines the return value's type, i.e. the `readableObjectMode`.30The chunk argument's type is based on the previous generator's return value, i.e. the `writableObjectMode` is based on the previous `readableObjectMode`.31The last input's generator is read by `childProcess.stdin` which:32- should not be in `objectMode` for performance reasons.33- can only be strings, Buffers and Uint8Arrays.34Therefore its `readableObjectMode` must be `false`.35The same applies to the first output's generator's `writableObjectMode`.36*/37const getOutputObjectModes = (objectMode, index, newGenerators) => {38 const writableObjectMode = index !== 0 && newGenerators[index - 1].value.readableObjectMode;39 const readableObjectMode = objectMode;40 return {writableObjectMode, readableObjectMode};41};4243const getInputObjectModes = (objectMode, index, newGenerators) => {44 const writableObjectMode = index === 045 ? objectMode46 : newGenerators[index - 1].value.readableObjectMode;47 const readableObjectMode = index !== newGenerators.length - 1 && objectMode;48 return {writableObjectMode, readableObjectMode};49};5051const sortGenerators = newGenerators => newGenerators[0]?.direction === 'input' ? newGenerators.reverse() : newGenerators;5253/*Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 54–68why, one step back54Generators can be used to transform/filter standard streams.5556Generators have a simple syntax, yet allows all of the following:57- Sharing state between chunks, by using logic before the `for` loop58- Flushing logic, by using logic after the `for` loop59- Asynchronous logic60- Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`61- Filtering, by using no `yield`6263Therefore, there is no need to allow Node.js or web transform streams.6465The `highWaterMark` is kept as the default value, since this is what `childProcess.std*` uses.6667Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.68*/Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 69–73why, one step back69export const generatorToDuplexStream = ({70 value: {transform, binary, writableObjectMode, readableObjectMode},71 encoding,72 optionName,73}) => {Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 74–77why, one step back74 const generators = [75 getEncodingStartGenerator(encoding),76 getLinesGenerator(encoding, binary),77 transform,Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 78why, one step back78 getValidateTransformReturn(readableObjectMode, optionName),Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 79why, one step back79 ].filter(Boolean);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 80why, one step back80 const duplexStream = generatorsToDuplex(generators, {writableObjectMode, readableObjectMode});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 81why, one step back81 return {value: duplexStream};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 82–95why, one step back82};8384const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode85 ? undefined86 : validateTransformReturn.bind(undefined, optionName);8788const validateTransformReturn = async function * (optionName, chunks) {89 for await (const chunk of chunks) {90 if (typeof chunk !== 'string' && !isBinary(chunk)) {91 throw new Error(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`);92 }9394 yield chunk;95 }Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 96–114why, one step back96};9798// `childProcess.stdin|stdout|stderr|stdio` is directly mutated.99export const pipeGenerator = (spawned, {value, direction, index}) => {100 if (direction === 'output') {101 spawned.stdio[index].pipe(value);102 } else {103 value.pipe(spawned.stdio[index]);104 }105106 const streamProperty = PROCESS_STREAM_PROPERTIES[index];107 if (streamProperty !== undefined) {108 spawned[streamProperty] = value;109 }110111 spawned.stdio[index] = value;112};113114const PROCESS_STREAM_PROPERTIES = ['stdin', 'stdout', 'stderr'];