Skip to content
Myr

generator.js

This file

Path

lib/transform/generator.js

118 lines · 4.1 KiB · JavaScript

Last changed by

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 1why, one step back
1import {Transform, getDefaultHighWaterMark} from 'node:stream';

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–5why, one step back
3import {isAsyncGenerator} from '../stdio/type.js';
4import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';5import {getValidateTransformInput, getValidateTransformReturn} from './validate.js';

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–40why, one step back
7import {
8 pushChunks,9 transformChunk,10 finalChunks,11 destroyTransform,12} from './run-async.js';13import {14 pushChunksSync,15 transformChunkSync,16 finalChunksSync,17 runTransformSync,18} from './run-sync.js';1920/*21Generators can be used to transform/filter standard streams.2223Generators have a simple syntax, yet allows all of the following:24- Sharing `state` between chunks25- Flushing logic, by using a `final` function26- Asynchronous logic27- Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`28- Filtering, by using no `yield`2930Therefore, there is no need to allow Node.js or web transform streams.3132The `highWaterMark` is kept as the default value, since this is what `subprocess.std*` uses.3334Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.3536Transform an array of generator functions into a `Transform` stream.37`Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.38*/39export const generatorToStream = ({40 value,

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 41–47why, one step back
41 value: {
42 transform,43 final,44 binary,45 writableObjectMode,46 readableObjectMode,47 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 48–69why, one step back
48 optionName,
49}, {encoding}) => {50 const state = {};51 const generators = addInternalGenerators(value, encoding, optionName);5253 const transformAsync = isAsyncGenerator(transform);54 const finalAsync = isAsyncGenerator(final);55 const transformMethod = transformAsync56 ? pushChunks.bind(undefined, transformChunk, state)57 : pushChunksSync.bind(undefined, transformChunkSync);58 const finalMethod = transformAsync || finalAsync59 ? pushChunks.bind(undefined, finalChunks, state)60 : pushChunksSync.bind(undefined, finalChunksSync);61 const destroyMethod = transformAsync || finalAsync62 ? destroyTransform.bind(undefined, state)63 : undefined;6465 const stream = new Transform({66 writableObjectMode,67 writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),68 readableObjectMode,69 readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 70–71why, one step back
70 // A string written to the stream is converted to bytes with this encoding, which `getEncodingTransformGenerator()` then decodes with the `encoding` option
71 defaultEncoding: getStringEncoding(binary, encoding),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 72–113why, one step back
72 transform(chunk, encoding, done) {
73 transformMethod([chunk, generators, 0], this, done);74 },75 flush(done) {76 finalMethod([generators], this, done);77 },78 destroy: destroyMethod,79 });80 return {stream};81};8283// Applies transform generators in sync mode84export const runGeneratorsSync = (chunks, stdioItems, encoding, isInput) => {85 const generators = stdioItems.filter(({type}) => type === 'generator');86 const reversedGenerators = isInput ? generators.reverse() : generators;8788 for (const {value, optionName} of reversedGenerators) {89 const generators = addInternalGenerators(value, encoding, optionName);90 chunks = runTransformSync(generators, chunks);91 }9293 return chunks;94};9596// Generators used internally to convert the chunk type, validate it, and split into lines97const addInternalGenerators = (98 {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},99 encoding,100 optionName,101) => {102 const state = {};103 return [104 {transform: getValidateTransformInput(writableObjectMode, optionName)},105 getEncodingTransformGenerator(binary, encoding, writableObjectMode),106 getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode, state),107 {transform, final},108 {transform: getValidateTransformReturn(readableObjectMode, optionName)},109 getAppendNewlineGenerator({110 binary,111 preserveNewlines,112 readableObjectMode,113 state,

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 114why, one step back
114 encoding,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 115why, one step back
115 }),

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 116why, one step back
116 getStringToBytesGenerator(binary, encoding, readableObjectMode),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 117–118why, one step back
117 ].filter(Boolean);
118};