transform.js
This file
Path
lib/stdio/transform.js
104 lines · 3.2 KiB · JavaScript
Last changed by
Improve transform speed (#780)ehmickybefore adoption14f040e
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
lines 6–12why, one step back6export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => {7 const transformMethod = transformAsync8 ? pushChunks.bind(undefined, transformChunk)9 : pushChunksSync.bind(undefined, transformChunkSync);10 const finalMethod = transformAsync || finalAsync11 ? pushChunks.bind(undefined, finalChunks)12 : pushChunksSync.bind(undefined, finalChunksSync);Simplify transforms implementation (#755)ehmickybefore adoption14fca86
line 13why, one step back13Improve transform speed (#780)ehmickybefore adoption14f040e
lines 14–29why, one step back14 return new Transform({15 writableObjectMode,16 writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),17 readableObjectMode,18 readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),19 transform(chunk, encoding, done) {20 transformMethod([chunk, generators, 0], this, done);21 },22 flush(done) {23 finalMethod([generators], this, done);24 },25 });26};2728const pushChunks = callbackify(async (getChunks, args, transformStream) => {29 for await (const chunk of getChunks(...args)) {Simplify transforms implementation (#755)ehmickybefore adoption14fca86
lines 30–60why, one step back30 transformStream.push(chunk);31 }32});3334// For each new chunk, apply each `transform()` method35const transformChunk = async function * (chunk, generators, index) {36 if (index === generators.length) {37 yield chunk;38 return;39 }4041 const {transform} = generators[index];42 for await (const transformedChunk of transform(chunk)) {43 yield * transformChunk(transformedChunk, generators, index + 1);44 }45};4647// At the end, apply each `final()` method, followed by the `transform()` method of the next transforms48const finalChunks = async function * (generators) {49 for (const [index, {final}] of Object.entries(generators)) {50 yield * generatorFinalChunks(final, Number(index), generators);51 }52};5354const generatorFinalChunks = async function * (final, index, generators) {55 if (final === undefined) {56 return;57 }5859 for await (const finalChunk of final()) {60 yield * transformChunk(finalChunk, generators, index + 1);Improve transform speed (#780)ehmickybefore adoption14f040e
lines 61–102why, one step back61 }62};6364// Duplicate the code above but as synchronous functions.65// This is a performance optimization when the `transform`/`flush` function is synchronous, which is the common case.66const pushChunksSync = (getChunksSync, args, transformStream, done) => {67 try {68 for (const chunk of getChunksSync(...args)) {69 transformStream.push(chunk);70 }7172 done();73 } catch (error) {74 done(error);75 }76};7778const transformChunkSync = function * (chunk, generators, index) {79 if (index === generators.length) {80 yield chunk;81 return;82 }8384 const {transform} = generators[index];85 for (const transformedChunk of transform(chunk)) {86 yield * transformChunkSync(transformedChunk, generators, index + 1);87 }88};8990const finalChunksSync = function * (generators) {91 for (const [index, {final}] of Object.entries(generators)) {92 yield * generatorFinalChunksSync(final, Number(index), generators);93 }94};9596const generatorFinalChunksSync = function * (final, index, generators) {97 if (final === undefined) {98 return;99 }100101 for (const finalChunk of final()) {102 yield * transformChunkSync(finalChunk, generators, index + 1);Simplify transforms implementation (#755)ehmickybefore adoption14fca86
lines 103–104why, one step back103 }104};