Skip to content
Myr

transform.js

This file

Path

lib/stdio/transform.js

131 lines · 3.9 KiB · JavaScript

Last changed by

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

History of this file

Take it

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

lines 1–5why, one step back
1import {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 back
6export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => {

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

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

Improve transform speed (#780)ehmickybefore adoption14f040e

line 8why, one step back
8 const transformMethod = transformAsync

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

line 9why, one step back
9 ? pushChunks.bind(undefined, transformChunk, state)

Improve transform speed (#780)ehmickybefore adoption14f040e

lines 10–11why, one step back
10 : pushChunksSync.bind(undefined, transformChunkSync);
11 const finalMethod = transformAsync || finalAsync

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

line 12why, one step back
12 ? pushChunks.bind(undefined, finalChunks, state)

Improve transform speed (#780)ehmickybefore adoption14f040e

line 13why, one step back
13 : pushChunksSync.bind(undefined, finalChunksSync);

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

lines 14–16why, one step back
14 const destroyMethod = transformAsync || finalAsync
15 ? destroyTransform.bind(undefined, state)16 : undefined;

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

line 17why, one step back
17

Improve transform speed (#780)ehmickybefore adoption14f040e

lines 18–28why, one step back
18 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 back
29 destroy: destroyMethod,

Improve transform speed (#780)ehmickybefore adoption14f040e

lines 30–32why, one step back
30 });
31};32

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

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

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

line 52why, one step back
52 const {transform = identityGenerator} = generators[index];

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

lines 53–71why, one step back
53 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 back
72 }
73};

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

line 74why, one step back
74

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

line 75why, one step back
75// Cancel any ongoing async generator when the Transform is destroyed, e.g. when the subprocess errors

Cancel generators when the process errors (#783)ehmickybefore adoption4b487ce

lines 76–85why, one step back
76const 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–106why, one step back
86
87// 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};100101const transformChunkSync = function * (chunk, generators, index) {102 if (index === generators.length) {103 yield chunk;104 return;105 }106

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

line 107why, one step back
107 const {transform = identityGenerator} = generators[index];

Improve transform speed (#780)ehmickybefore adoption14f040e

lines 108–125why, one step back
108 for (const transformedChunk of transform(chunk)) {
109 yield * transformChunkSync(transformedChunk, generators, index + 1);110 }111};112113const finalChunksSync = function * (generators) {114 for (const [index, {final}] of Object.entries(generators)) {115 yield * generatorFinalChunksSync(final, Number(index), generators);116 }117};118119const generatorFinalChunksSync = function * (final, index, generators) {120 if (final === undefined) {121 return;122 }123124 for (const finalChunk of final()) {125 yield * transformChunkSync(finalChunk, generators, index + 1);

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

line 126why, one step back
126 }

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

lines 127–130why, one step back
127};
128129const identityGenerator = function * (chunk) {130 yield chunk;

Simplify transforms implementation (#755)ehmickybefore adoption14fca86

line 131why, one step back
131};