transform.js
This file
Path
lib/stdio/transform.js
137 lines · 4.1 KiB · JavaScript
Last changed by
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import {Transform, getDefaultHighWaterMark} from 'node:stream';import {callbackify} from 'node:util';
// Transform an array of generator functions into a `Transform` stream.// `Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => { const state = {}; const transformMethod = transformAsync ? pushChunks.bind(undefined, transformChunk, state) : pushChunksSync.bind(undefined, transformChunkSync); const finalMethod = transformAsync || finalAsync ? pushChunks.bind(undefined, finalChunks, state) : pushChunksSync.bind(undefined, finalChunksSync); const destroyMethod = transformAsync || finalAsync ? destroyTransform.bind(undefined, state) : undefined;
return new Transform({ writableObjectMode, writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode), readableObjectMode, readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode), transform(chunk, encoding, done) { transformMethod([chunk, generators, 0], this, done); }, flush(done) { finalMethod([generators], this, done); }, destroy: destroyMethod, });};
const pushChunks = callbackify(async (getChunks, state, args, transformStream) => { state.currentIterable = getChunks(...args);
try { for await (const chunk of state.currentIterable) { transformStream.push(chunk); } } finally { delete state.currentIterable; }});
// For each new chunk, apply each `transform()` methodconst transformChunk = async function * (chunk, generators, index) { if (index === generators.length) { yield chunk; return; }
const {transform = identityGenerator} = generators[index]; for await (const transformedChunk of transform(chunk)) { yield * transformChunk(transformedChunk, generators, index + 1); }};
// At the end, apply each `final()` method, followed by the `transform()` method of the next transformsconst finalChunks = async function * (generators) { for (const [index, {final}] of Object.entries(generators)) { yield * generatorFinalChunks(final, Number(index), generators); }};
const generatorFinalChunks = async function * (final, index, generators) { if (final === undefined) { return; }
for await (const finalChunk of final()) { yield * transformChunk(finalChunk, generators, index + 1); }};
// Cancel any ongoing async generator when the Transform is destroyed, e.g. when the subprocess errorsconst destroyTransform = callbackify(async ({currentIterable}, error) => { if (currentIterable !== undefined) { await (error ? currentIterable.throw(error) : currentIterable.return()); return; }
if (error) { throw error; }});
// Duplicate the code above but as synchronous functions.// This is a performance optimization when the `transform`/`flush` function is synchronous, which is the common case.const pushChunksSync = (getChunksSync, args, transformStream, done) => { try { for (const chunk of getChunksSync(...args)) { transformStream.push(chunk); }
done(); } catch (error) { done(error); }};
// Run synchronous generators with `execaSync()`export const runTransformSync = (generators, chunks) => [ ...chunks.flatMap(chunk => [...transformChunkSync(chunk, generators, 0)]), ...finalChunksSync(generators),];
const transformChunkSync = function * (chunk, generators, index) { if (index === generators.length) { yield chunk; return; }
const {transform = identityGenerator} = generators[index]; for (const transformedChunk of transform(chunk)) { yield * transformChunkSync(transformedChunk, generators, index + 1); }};
const finalChunksSync = function * (generators) { for (const [index, {final}] of Object.entries(generators)) { yield * generatorFinalChunksSync(final, Number(index), generators); }};
const generatorFinalChunksSync = function * (final, index, generators) { if (final === undefined) { return; }
for (const finalChunk of final()) { yield * transformChunkSync(finalChunk, generators, index + 1); }};
const identityGenerator = function * (chunk) { yield chunk;};