generator.js
This file
Path
lib/stdio/generator.js
196 lines · 7.5 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Refactor tests (#934)ehmickybefore adoption52c4bde
line 1why, one step back1import isPlainObj from 'is-plain-obj';Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4
line 2why, one step back2import {BINARY_ENCODINGS} from '../arguments/encoding.js';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 3why, one step back3import {generatorsToTransform, runTransformSync} from './transform.js';Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 4why, one step back4import {getEncodingTransformGenerator} from './encoding-transform.js';Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 5why, one step back5import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd
line 6why, one step back6import {pipeStreams} from './pipeline.js';Refactor tests (#934)ehmickybefore adoption52c4bde
line 7why, one step back7import {isAsyncGenerator} from './type.js';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 8why, one step back8import {getValidateTransformInput, getValidateTransformReturn} from './validate.js';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 9why, one step back9Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
lines 10–11why, one step back10export const getObjectMode = (stdioItems, optionName, direction, options) => {11 const transforms = getTransforms(stdioItems, optionName, direction, options);Refactor tests (#934)ehmickybefore adoption52c4bde
line 12why, one step back12 if (transforms.length === 0) {Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 13–14why, one step back13 return false;14 }Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 15why, one step back15Refactor tests (#934)ehmickybefore adoption52c4bde
line 16why, one step back16 const {value: {readableObjectMode, writableObjectMode}} = transforms.at(-1);Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 17–19why, one step back17 return direction === 'input' ? writableObjectMode : readableObjectMode;18};19Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
lines 20–22why, one step back20export const normalizeTransforms = (stdioItems, optionName, direction, options) => [21 ...stdioItems.filter(({type}) => !TRANSFORM_TYPES.has(type)),22 ...getTransforms(stdioItems, optionName, direction, options),Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 23–24why, one step back23];24Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
lines 25–26why, one step back25const getTransforms = (stdioItems, optionName, direction, {encoding}) => {26 const transforms = stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type));Refactor tests (#934)ehmickybefore adoption52c4bde
line 27why, one step back27 const newTransforms = Array.from({length: transforms.length});Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 28why, one step back28Refactor tests (#934)ehmickybefore adoption52c4bde
line 29why, one step back29 for (const [index, stdioItem] of Object.entries(transforms)) {Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 30why, one step back30 newTransforms[index] = normalizeTransform({stdioItem, index: Number(index), newTransforms, optionName, direction, encoding});Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 31–32why, one step back31 }32Refactor tests (#934)ehmickybefore adoption52c4bde
line 33why, one step back33 return sortTransforms(newTransforms, direction);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 34–35why, one step back34};35Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 36why, one step back36export const TRANSFORM_TYPES = new Set(['generator', 'asyncGenerator', 'duplex', 'webTransform']);Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 37why, one step back37Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
lines 38–41why, one step back38const normalizeTransform = ({stdioItem, stdioItem: {type}, index, newTransforms, optionName, direction, encoding}) => {39 if (type === 'duplex') {40 return normalizeDuplex({stdioItem, optionName});41 }Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 42why, one step back42Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
lines 43–49why, one step back43 if (type === 'webTransform') {44 return normalizeTransformStream({stdioItem, index, newTransforms, direction});45 }4647 return normalizeGenerator({stdioItem, index, newTransforms, direction, encoding});48};49Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
lines 50–72why, one step back50const normalizeDuplex = ({51 stdioItem,52 stdioItem: {53 value: {54 transform,55 transform: {writableObjectMode, readableObjectMode},56 objectMode = readableObjectMode,57 },58 },59 optionName,60}) => {61 if (objectMode && !readableObjectMode) {62 throw new TypeError(`The \`${optionName}.objectMode\` option can only be \`true\` if \`new Duplex({objectMode: true})\` is used.`);63 }6465 if (!objectMode && readableObjectMode) {66 throw new TypeError(`The \`${optionName}.objectMode\` option cannot be \`false\` if \`new Duplex({objectMode: true})\` is used.`);67 }6869 return {70 ...stdioItem,71 value: {transform, writableObjectMode, readableObjectMode},72 };Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
lines 73–81why, one step back73};7475const normalizeTransformStream = ({stdioItem, stdioItem: {value}, index, newTransforms, direction}) => {76 const {transform, objectMode} = isPlainObj(value) ? value : {transform: value};77 const {writableObjectMode, readableObjectMode} = getObjectModes(objectMode, index, newTransforms, direction);78 return ({79 ...stdioItem,80 value: {transform, writableObjectMode, readableObjectMode},81 });Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
lines 82–84why, one step back82};8384const normalizeGenerator = ({stdioItem, stdioItem: {value}, index, newTransforms, direction, encoding}) => {Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
lines 85–90why, one step back85 const {86 transform,87 final,88 binary: binaryOption = false,89 preserveNewlines = false,90 objectMode,Refactor tests (#934)ehmickybefore adoption52c4bde
line 91why, one step back91 } = isPlainObj(value) ? value : {transform: value};Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4
line 92why, one step back92 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);Refactor tests (#934)ehmickybefore adoption52c4bde
lines 93–94why, one step back93 const {writableObjectMode, readableObjectMode} = getObjectModes(objectMode, index, newTransforms, direction);94 return {...stdioItem, value: {transform, final, binary, preserveNewlines, writableObjectMode, readableObjectMode}};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 95–96why, one step back95};96Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 97why, one step back97/*Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 98–99why, one step back98`objectMode` determines the return value's type, i.e. the `readableObjectMode`.99The chunk argument's type is based on the previous generator's return value, i.e. the `writableObjectMode` is based on the previous `readableObjectMode`.Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 100why, one step back100The last input's generator is read by `subprocess.stdin` which:Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 101–105why, one step back101- should not be in `objectMode` for performance reasons.102- can only be strings, Buffers and Uint8Arrays.103Therefore its `readableObjectMode` must be `false`.104The same applies to the first output's generator's `writableObjectMode`.105*/Refactor tests (#934)ehmickybefore adoption52c4bde
lines 106–111why, one step back106const getObjectModes = (objectMode, index, newTransforms, direction) => direction === 'output'107 ? getOutputObjectModes(objectMode, index, newTransforms)108 : getInputObjectModes(objectMode, index, newTransforms);109110const getOutputObjectModes = (objectMode, index, newTransforms) => {111 const writableObjectMode = index !== 0 && newTransforms[index - 1].value.readableObjectMode;Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
line 112why, one step back112 const readableObjectMode = objectMode ?? writableObjectMode;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 113–115why, one step back113 return {writableObjectMode, readableObjectMode};114};115Refactor tests (#934)ehmickybefore adoption52c4bde
line 116why, one step back116const getInputObjectModes = (objectMode, index, newTransforms) => {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 117why, one step back117 const writableObjectMode = index === 0Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
line 118why, one step back118 ? objectMode === trueRefactor tests (#934)ehmickybefore adoption52c4bde
lines 119–120why, one step back119 : newTransforms[index - 1].value.readableObjectMode;120 const readableObjectMode = index !== newTransforms.length - 1 && (objectMode ?? writableObjectMode);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 121–123why, one step back121 return {writableObjectMode, readableObjectMode};122};123Refactor tests (#934)ehmickybefore adoption52c4bde
line 124why, one step back124const sortTransforms = (newTransforms, direction) => direction === 'input' ? newTransforms.reverse() : newTransforms;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 125–126why, one step back125126/*Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 127–129why, one step back127Generators can be used to transform/filter standard streams.128129Generators have a simple syntax, yet allows all of the following:Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 130–131why, one step back130- Sharing `state` between chunks131- Flushing logic, by using a `final` functionAllow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 132–137why, one step back132- Asynchronous logic133- Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`134- Filtering, by using no `yield`135136Therefore, there is no need to allow Node.js or web transform streams.137Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 138why, one step back138The `highWaterMark` is kept as the default value, since this is what `subprocess.std*` uses.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 139–141why, one step back139140Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.141*/Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 142why, one step back142export const generatorToDuplexStream = ({Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 143–146why, one step back143 value,144 value: {transform, final, writableObjectMode, readableObjectMode},145 optionName,146}) => {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 147why, one step back147 const generators = addInternalGenerators(value, optionName);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 148–153why, one step back148 const transformAsync = isAsyncGenerator(transform);149 const finalAsync = isAsyncGenerator(final);150 const stream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});151 return {stream};152};153Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 154why, one step back154export const getGenerators = stdioItems => stdioItems.filter(({type}) => type === 'generator');Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 155–157why, one step back155156export const runGeneratorsSync = (chunks, generators) => {157 for (const {value, optionName} of generators) {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 158why, one step back158 const generators = addInternalGenerators(value, optionName);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 159–164why, one step back159 chunks = runTransformSync(generators, chunks);160 }161162 return chunks;163};164Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 165–166why, one step back165const addInternalGenerators = (166 {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 167why, one step back167 optionName,Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 168why, one step back168) => {Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 169why, one step back169 const state = {};Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 170–171why, one step back170 return [171 {transform: getValidateTransformInput(writableObjectMode, optionName)},Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 172–173why, one step back172 getEncodingTransformGenerator(binary, writableObjectMode),173 getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode, state),Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 174–175why, one step back174 {transform, final},175 {transform: getValidateTransformReturn(readableObjectMode, optionName)},Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 176why, one step back176 getAppendNewlineGenerator({binary, preserveNewlines, readableObjectMode, state}),Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 177why, one step back177 ].filter(Boolean);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 178–179why, one step back178};179Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 180why, one step back180// `subprocess.stdin|stdout|stderr|stdio` is directly mutated.Refactor tests (#934)ehmickybefore adoption52c4bde
line 181why, one step back181export const pipeTransform = (subprocess, stream, direction, fdNumber) => {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 182why, one step back182 if (direction === 'output') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 183why, one step back183 pipeStreams(subprocess.stdio[fdNumber], stream);Fix error handling of transforms (#739)ehmickybefore adoption17993a8
line 184why, one step back184 } else {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 185why, one step back185 pipeStreams(stream, subprocess.stdio[fdNumber]);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 186–187why, one step back186 }187Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 188why, one step back188 const streamProperty = SUBPROCESS_STREAM_PROPERTIES[fdNumber];Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 189why, one step back189 if (streamProperty !== undefined) {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 190why, one step back190 subprocess[streamProperty] = stream;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 191–192why, one step back191 }192Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 193why, one step back193 subprocess.stdio[fdNumber] = stream;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 194–195why, one step back194};195Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 196why, one step back196const SUBPROCESS_STREAM_PROPERTIES = ['stdin', 'stdout', 'stderr'];