Skip to content
Myr

generator.js

This file

Path

lib/transform/generator.js

107 lines · 3.7 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–5why, one step back
1import {Transform, getDefaultHighWaterMark} from 'node:stream';
2import {isAsyncGenerator} from '../stdio/type.js';3import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';4import {getValidateTransformInput, getValidateTransformReturn} from './validate.js';5import {getEncodingTransformGenerator} from './encoding-transform.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 6–17why, one step back
6import {
7 pushChunks,8 transformChunk,9 finalChunks,10 destroyTransform,11} from './run-async.js';12import {13 pushChunksSync,14 transformChunkSync,15 finalChunksSync,16 runTransformSync,17} from './run-sync.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 18–73why, one step back
18
19/*20Generators can be used to transform/filter standard streams.2122Generators have a simple syntax, yet allows all of the following:23- Sharing `state` between chunks24- Flushing logic, by using a `final` function25- Asynchronous logic26- Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`27- Filtering, by using no `yield`2829Therefore, there is no need to allow Node.js or web transform streams.3031The `highWaterMark` is kept as the default value, since this is what `subprocess.std*` uses.3233Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.3435Transform an array of generator functions into a `Transform` stream.36`Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.37*/38export const generatorToStream = ({39 value,40 value: {transform, final, writableObjectMode, readableObjectMode},41 optionName,42}, {encoding}) => {43 const state = {};44 const generators = addInternalGenerators(value, encoding, optionName);4546 const transformAsync = isAsyncGenerator(transform);47 const finalAsync = isAsyncGenerator(final);48 const transformMethod = transformAsync49 ? pushChunks.bind(undefined, transformChunk, state)50 : pushChunksSync.bind(undefined, transformChunkSync);51 const finalMethod = transformAsync || finalAsync52 ? pushChunks.bind(undefined, finalChunks, state)53 : pushChunksSync.bind(undefined, finalChunksSync);54 const destroyMethod = transformAsync || finalAsync55 ? destroyTransform.bind(undefined, state)56 : undefined;5758 const stream = new Transform({59 writableObjectMode,60 writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),61 readableObjectMode,62 readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),63 transform(chunk, encoding, done) {64 transformMethod([chunk, generators, 0], this, done);65 },66 flush(done) {67 finalMethod([generators], this, done);68 },69 destroy: destroyMethod,70 });71 return {stream};72};73

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 74why, one step back
74// Applies transform generators in sync mode

Rename files (#977)ehmickybefore adoption4b2e316

lines 75–86why, one step back
75export const runGeneratorsSync = (chunks, stdioItems, encoding, isInput) => {
76 const generators = stdioItems.filter(({type}) => type === 'generator');77 const reversedGenerators = isInput ? generators.reverse() : generators;7879 for (const {value, optionName} of reversedGenerators) {80 const generators = addInternalGenerators(value, encoding, optionName);81 chunks = runTransformSync(generators, chunks);82 }8384 return chunks;85};86

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 87why, one step back
87// Generators used internally to convert the chunk type, validate it, and split into lines

Rename files (#977)ehmickybefore adoption4b2e316

lines 88–99why, one step back
88const addInternalGenerators = (
89 {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},90 encoding,91 optionName,92) => {93 const state = {};94 return [95 {transform: getValidateTransformInput(writableObjectMode, optionName)},96 getEncodingTransformGenerator(binary, encoding, writableObjectMode),97 getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode, state),98 {transform, final},99 {transform: getValidateTransformReturn(readableObjectMode, optionName)},

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 100–105why, one step back
100 getAppendNewlineGenerator({
101 binary,102 preserveNewlines,103 readableObjectMode,104 state,105 }),

Rename files (#977)ehmickybefore adoption4b2e316

lines 106–107why, one step back
106 ].filter(Boolean);
107};