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
Simplify transforms implementation (#755)ehmickybefore adoption14fca86
lines 1–5why, one step back1import {Transform, getDefaultHighWaterMark} from 'node:stream';2import {callbackify} from 'node:util';34// Transform an array of generator functions into a `Transform` stream.5// `Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.Improve transform speed (#780)ehmickybefore adoption14f040e
line 6why, one step back6export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => {Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce
line 7why, one step back7 const state = {};Improve transform speed (#780)ehmickybefore adoption14f040e
line 8why, one step back8 const transformMethod = transformAsyncCancel generators when the process errors (#783)ehmickybefore adoption4b487ce
line 9why, one step back9 ? pushChunks.bind(undefined, transformChunk, state)Improve transform speed (#780)ehmickybefore adoption14f040e
lines 10–11why, one step back10 : pushChunksSync.bind(undefined, transformChunkSync);11 const finalMethod = transformAsync || finalAsyncCancel generators when the process errors (#783)ehmickybefore adoption4b487ce
line 12why, one step back12 ? pushChunks.bind(undefined, finalChunks, state)Improve transform speed (#780)ehmickybefore adoption14f040e
line 13why, one step back13 : pushChunksSync.bind(undefined, finalChunksSync);Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce
lines 14–16why, one step back14 const destroyMethod = transformAsync || finalAsync15 ? destroyTransform.bind(undefined, state)16 : undefined;Simplify transforms implementation (#755)ehmickybefore adoption14fca86
line 17why, one step back17Improve transform speed (#780)ehmickybefore adoption14f040e
lines 18–28why, one step back18 return new Transform({19 writableObjectMode,20 writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),21 readableObjectMode,22 readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),23 transform(chunk, encoding, done) {24 transformMethod([chunk, generators, 0], this, done);25 },26 flush(done) {27 finalMethod([generators], this, done);28 },Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce
line 29why, one step back29 destroy: destroyMethod,Improve transform speed (#780)ehmickybefore adoption14f040e
lines 30–32why, one step back30 });31};32Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce
lines 33–41why, one step back33const pushChunks = callbackify(async (getChunks, state, args, transformStream) => {34 state.currentIterable = getChunks(...args);3536 try {37 for await (const chunk of state.currentIterable) {38 transformStream.push(chunk);39 }40 } finally {41 delete state.currentIterable;Simplify transforms implementation (#755)ehmickybefore adoption14fca86
lines 42–51why, one step back42 }43});4445// For each new chunk, apply each `transform()` method46const transformChunk = async function * (chunk, generators, index) {47 if (index === generators.length) {48 yield chunk;49 return;50 }51Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153
line 52why, one step back52 const {transform = identityGenerator} = generators[index];Simplify transforms implementation (#755)ehmickybefore adoption14fca86
lines 53–71why, one step back53 for await (const transformedChunk of transform(chunk)) {54 yield * transformChunk(transformedChunk, generators, index + 1);55 }56};5758// At the end, apply each `final()` method, followed by the `transform()` method of the next transforms59const finalChunks = async function * (generators) {60 for (const [index, {final}] of Object.entries(generators)) {61 yield * generatorFinalChunks(final, Number(index), generators);62 }63};6465const generatorFinalChunks = async function * (final, index, generators) {66 if (final === undefined) {67 return;68 }6970 for await (const finalChunk of final()) {71 yield * transformChunk(finalChunk, generators, index + 1);Improve transform speed (#780)ehmickybefore adoption14f040e
lines 72–73why, one step back72 }73};Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce
line 74why, one step back74Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 75why, one step back75// Cancel any ongoing async generator when the Transform is destroyed, e.g. when the subprocess errorsCancel generators when the process errors (#783)ehmickybefore adoption4b487ce
lines 76–85why, one step back76const destroyTransform = callbackify(async ({currentIterable}, error) => {77 if (currentIterable !== undefined) {78 await (error ? currentIterable.throw(error) : currentIterable.return());79 return;80 }8182 if (error) {83 throw error;84 }85});Improve transform speed (#780)ehmickybefore adoption14f040e
lines 86–100why, one step back8687// Duplicate the code above but as synchronous functions.88// This is a performance optimization when the `transform`/`flush` function is synchronous, which is the common case.89const pushChunksSync = (getChunksSync, args, transformStream, done) => {90 try {91 for (const chunk of getChunksSync(...args)) {92 transformStream.push(chunk);93 }9495 done();96 } catch (error) {97 done(error);98 }99};100Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 101–106why, one step back101// Run synchronous generators with `execaSync()`102export const runTransformSync = (generators, chunks) => [103 ...chunks.flatMap(chunk => [...transformChunkSync(chunk, generators, 0)]),104 ...finalChunksSync(generators),105];106Improve transform speed (#780)ehmickybefore adoption14f040e
lines 107–112why, one step back107const transformChunkSync = function * (chunk, generators, index) {108 if (index === generators.length) {109 yield chunk;110 return;111 }112Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153
line 113why, one step back113 const {transform = identityGenerator} = generators[index];Improve transform speed (#780)ehmickybefore adoption14f040e
lines 114–131why, one step back114 for (const transformedChunk of transform(chunk)) {115 yield * transformChunkSync(transformedChunk, generators, index + 1);116 }117};118119const finalChunksSync = function * (generators) {120 for (const [index, {final}] of Object.entries(generators)) {121 yield * generatorFinalChunksSync(final, Number(index), generators);122 }123};124125const generatorFinalChunksSync = function * (final, index, generators) {126 if (final === undefined) {127 return;128 }129130 for (const finalChunk of final()) {131 yield * transformChunkSync(finalChunk, generators, index + 1);Simplify transforms implementation (#755)ehmickybefore adoption14fca86
line 132why, one step back132 }Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153
lines 133–136why, one step back133};134135const identityGenerator = function * (chunk) {136 yield chunk;Simplify transforms implementation (#755)ehmickybefore adoption14fca86
line 137why, one step back137};