Skip to content
Myr

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 file

Take it

Refactor tests (#934)ehmickybefore adoption52c4bde

line 1why, one step back
1import isPlainObj from 'is-plain-obj';

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 2why, one step back
2import {BINARY_ENCODINGS} from '../arguments/encoding.js';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

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

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 4why, one step back
4import {getEncodingTransformGenerator} from './encoding-transform.js';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 5why, one step back
5import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';

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

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

Refactor tests (#934)ehmickybefore adoption52c4bde

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

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

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

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

line 9why, one step back
9

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 10–11why, one step back
10export const getObjectMode = (stdioItems, optionName, direction, options) => {
11 const transforms = getTransforms(stdioItems, optionName, direction, options);

Refactor tests (#934)ehmickybefore adoption52c4bde

line 12why, one step back
12 if (transforms.length === 0) {

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 13–14why, one step back
13 return false;
14 }

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 15why, one step back
15

Refactor tests (#934)ehmickybefore adoption52c4bde

line 16why, one step back
16 const {value: {readableObjectMode, writableObjectMode}} = transforms.at(-1);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 17–19why, one step back
17 return direction === 'input' ? writableObjectMode : readableObjectMode;
18};19

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 20–22why, one step back
20export 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 back
23];
24

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 25–26why, one step back
25const getTransforms = (stdioItems, optionName, direction, {encoding}) => {
26 const transforms = stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type));

Refactor tests (#934)ehmickybefore adoption52c4bde

line 27why, one step back
27 const newTransforms = Array.from({length: transforms.length});

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 28why, one step back
28

Refactor tests (#934)ehmickybefore adoption52c4bde

line 29why, one step back
29 for (const [index, stdioItem] of Object.entries(transforms)) {

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 30why, one step back
30 newTransforms[index] = normalizeTransform({stdioItem, index: Number(index), newTransforms, optionName, direction, encoding});

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 31–32why, one step back
31 }
32

Refactor tests (#934)ehmickybefore adoption52c4bde

line 33why, one step back
33 return sortTransforms(newTransforms, direction);

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 34–35why, one step back
34};
35

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 36why, one step back
36export const TRANSFORM_TYPES = new Set(['generator', 'asyncGenerator', 'duplex', 'webTransform']);

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 37why, one step back
37

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 38–41why, one step back
38const 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 back
42

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 43–49why, one step back
43 if (type === 'webTransform') {
44 return normalizeTransformStream({stdioItem, index, newTransforms, direction});45 }4647 return normalizeGenerator({stdioItem, index, newTransforms, direction, encoding});48};49

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 50–72why, one step back
50const 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 back
73};
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 back
82};
8384const normalizeGenerator = ({stdioItem, stdioItem: {value}, index, newTransforms, direction, encoding}) => {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 85–90why, one step back
85 const {
86 transform,87 final,88 binary: binaryOption = false,89 preserveNewlines = false,90 objectMode,

Refactor tests (#934)ehmickybefore adoption52c4bde

line 91why, one step back
91 } = isPlainObj(value) ? value : {transform: value};

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 92why, one step back
92 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 93–94why, one step back
93 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 back
95};
96

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

line 97why, one step back
97/*

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 98–99why, one step back
98`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 back
100The last input's generator is read by `subprocess.stdin` which:

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 101–105why, one step back
101- 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 back
106const 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 back
112 const readableObjectMode = objectMode ?? writableObjectMode;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 113–115why, one step back
113 return {writableObjectMode, readableObjectMode};
114};115

Refactor tests (#934)ehmickybefore adoption52c4bde

line 116why, one step back
116const getInputObjectModes = (objectMode, index, newTransforms) => {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 117why, one step back
117 const writableObjectMode = index === 0

Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b

line 118why, one step back
118 ? objectMode === true

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 119–120why, one step back
119 : 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 back
121 return {writableObjectMode, readableObjectMode};
122};123

Refactor tests (#934)ehmickybefore adoption52c4bde

line 124why, one step back
124const sortTransforms = (newTransforms, direction) => direction === 'input' ? newTransforms.reverse() : newTransforms;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 125–126why, one step back
125
126/*

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

lines 127–129why, one step back
127Generators 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 back
130- Sharing `state` between chunks
131- Flushing logic, by using a `final` function

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

lines 132–137why, one step back
132- Asynchronous logic
133- 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.137

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 138why, one step back
138The `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 back
139
140Chunks 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 back
142export const generatorToDuplexStream = ({

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 143–146why, one step back
143 value,
144 value: {transform, final, writableObjectMode, readableObjectMode},145 optionName,146}) => {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 147why, one step back
147 const generators = addInternalGenerators(value, optionName);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 148–153why, one step back
148 const transformAsync = isAsyncGenerator(transform);
149 const finalAsync = isAsyncGenerator(final);150 const stream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});151 return {stream};152};153

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 154why, one step back
154export const getGenerators = stdioItems => stdioItems.filter(({type}) => type === 'generator');

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 155–157why, one step back
155
156export const runGeneratorsSync = (chunks, generators) => {157 for (const {value, optionName} of generators) {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 158why, one step back
158 const generators = addInternalGenerators(value, optionName);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 159–164why, one step back
159 chunks = runTransformSync(generators, chunks);
160 }161162 return chunks;163};164

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 165–166why, one step back
165const addInternalGenerators = (
166 {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 167why, one step back
167 optionName,

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 168why, one step back
168) => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 169why, one step back
169 const state = {};

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 170–171why, one step back
170 return [
171 {transform: getValidateTransformInput(writableObjectMode, optionName)},

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 172–173why, one step back
172 getEncodingTransformGenerator(binary, writableObjectMode),
173 getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode, state),

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

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

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 176why, one step back
176 getAppendNewlineGenerator({binary, preserveNewlines, readableObjectMode, state}),

Line-wise transforms (#699)ehmickybefore adoption6dd804f

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

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

lines 178–179why, one step back
178};
179

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 180why, one step back
180// `subprocess.stdin|stdout|stderr|stdio` is directly mutated.

Refactor tests (#934)ehmickybefore adoption52c4bde

line 181why, one step back
181export const pipeTransform = (subprocess, stream, direction, fdNumber) => {

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 183why, one step back
183 pipeStreams(subprocess.stdio[fdNumber], stream);

Fix error handling of transforms (#739)ehmickybefore adoption17993a8

line 184why, one step back
184 } else {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 185why, one step back
185 pipeStreams(stream, subprocess.stdio[fdNumber]);

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

lines 186–187why, one step back
186 }
187

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 188why, one step back
188 const streamProperty = SUBPROCESS_STREAM_PROPERTIES[fdNumber];

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

line 189why, one step back
189 if (streamProperty !== undefined) {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 190why, one step back
190 subprocess[streamProperty] = stream;

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

lines 191–192why, one step back
191 }
192

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 193why, one step back
193 subprocess.stdio[fdNumber] = stream;

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

lines 194–195why, one step back
194};
195

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 196why, one step back
196const SUBPROCESS_STREAM_PROPERTIES = ['stdin', 'stdout', 'stderr'];