Skip to content
Myr

Simplify transforms implementation (#755)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent a12c8e9 · Browse files · 4 files+98 added−75 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+98 added−75 removed

  • tests:+43 added−14 removed
  • other:+55 added−61 removed

4 files+98 added−75 removed

deleted:lib/stdio/duplex.js-59 removed
modified:lib/stdio/generator.js+2 added-2 removed
added:lib/stdio/transform.js+53 added
modified:test/stdio/generator.js+43 added-14 removed
1import {Buffer} from 'node:buffer';2import {readFile, writeFile, rm} from 'node:fs/promises';3import {getDefaultHighWaterMark, PassThrough} from 'node:stream';4import {setTimeout, setImmediate} from 'node:timers/promises';4import {setTimeout, scheduler} from 'node:timers/promises';5import test from 'ava';6import getStream, {getStreamAsArray} from 'get-stream';7import tempfile from 'tempfile';8import {execa, execaSync} from '../../index.js';
282 unmodified lines
291test('Cannot use generators with sync methods and stdout', testSyncMethods, 1);292test('Cannot use generators with sync methods and stderr', testSyncMethods, 2);293test('Cannot use generators with sync methods and stdio[*]', testSyncMethods, 3);294295const repeatHighWaterMark = 10;295const repeatCount = getDefaultHighWaterMark() * 3;296297const writerGenerator = function * () {298 for (let index = 0; index < getDefaultHighWaterMark() * repeatHighWaterMark; index += 1) {298 for (let index = 0; index < repeatCount; index += 1) {299 yield '\n';300 }301};302303const getLengthGenerator = function * (chunk) {304 yield `${chunk.length}`;303const getLengthGenerator = function * (t, chunk) {304 t.is(chunk.length, 1);305 yield chunk;306};307308const testHighWaterMark = async (t, passThrough, binary, objectMode) => {309 const {stdout} = await execa('noop.js', {310 stdout: [311 ...(objectMode ? [outputObjectGenerator] : []),312 writerGenerator,313 ...(passThrough ? [noopGenerator(false, binary)] : []),313 {transform: getLengthGenerator, binary: true},314 {transform: getLengthGenerator.bind(undefined, t), binary: true, objectMode: true},315 ],316 });316 t.is(stdout, `${getDefaultHighWaterMark()}`.repeat(repeatHighWaterMark));317 t.is(stdout.length, repeatCount);318 t.true(stdout.every(chunk => chunk.toString() === '\n'));319};320319test('Stream respects highWaterMark, no passThrough', testHighWaterMark, false, false, false);320test('Stream respects highWaterMark, line-wise passThrough', testHighWaterMark, true, false, false);321test('Stream respects highWaterMark, binary passThrough', testHighWaterMark, true, true, false);322test('Stream respects highWaterMark, objectMode as input but not output', testHighWaterMark, false, false, true);321test('Synchronous yields are not buffered, no passThrough', testHighWaterMark, false, false, false);322test('Synchronous yields are not buffered, line-wise passThrough', testHighWaterMark, true, false, false);323test('Synchronous yields are not buffered, binary passThrough', testHighWaterMark, true, true, false);324test('Synchronous yields are not buffered, objectMode as input but not output', testHighWaterMark, false, false, true);325326const getTypeofGenerator = objectMode => ({327 * transform(line) {328 yield Object.prototype.toString.call(line);
152 unmodified lines
481482const testMultibyte = async (t, objectMode) => {483 const childProcess = execa('stdin.js', {stdin: noopGenerator(objectMode)});484 childProcess.stdin.write(multibyteUint8Array.slice(0, breakingLength));483 await setImmediate();485 await scheduler.yield();486 childProcess.stdin.end(multibyteUint8Array.slice(breakingLength));487 const {stdout} = await childProcess;488 t.is(stdout, multibyteString);489};
23 unmodified lines
513const suffix = ' <';514515const multipleYieldGenerator = async function * (line = foobarString) {516 yield prefix;515 await setImmediate();517 await scheduler.yield();518 yield line;517 await setImmediate();519 await scheduler.yield();520 yield suffix;521};522523const testMultipleYields = async (t, final) => {
3 unmodified lines
527528test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);529test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);530531const partsPerChunk = 4;532const chunksPerCall = 10;533const callCount = 5;534const fullString = '\n'.repeat(getDefaultHighWaterMark(false) / partsPerChunk);535536const yieldFullStrings = function * () {537 yield * Array.from({length: partsPerChunk * chunksPerCall}).fill(fullString);538};539540const manyYieldGenerator = async function * () {541 for (let index = 0; index < callCount; index += 1) {542 yield * yieldFullStrings();543 // eslint-disable-next-line no-await-in-loop544 await scheduler.yield();545 }546};547548const testManyYields = async (t, final) => {549 const childProcess = execa('noop.js', {stdout: convertTransformToFinal(manyYieldGenerator, final), buffer: false});550 const [chunks] = await Promise.all([getStreamAsArray(childProcess.stdout), childProcess]);551 const expectedChunk = Buffer.alloc(getDefaultHighWaterMark(false) * chunksPerCall).fill('\n');552 t.deepEqual(chunks, Array.from({length: callCount}).fill(expectedChunk));553};554555test('Generator "transform" yields are sent right away', testManyYields, false);556test('Generator "final" yields are sent right away', testManyYields, true);557558const testInputFile = async (t, getOptions, reversed) => {559 const filePath = tempfile();560 await writeFile(filePath, foobarString);561 const {stdin, ...options} = getOptions(filePath);
142 unmodified lines