Simplify transforms syntax (#748)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent a510995 · Browse files · 15 files·+529 added−435 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
15 files+529 added−435 removed
- tests:+181 added−178 removed
- other:+348 added−257 removed
Provenance
- Commit
fd00294- Tree
96bee7f- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a510995
15 files changed+529 added−435 removed
modified:docs/transform.md+49 added-24 removed
1234556789101112131011121413141516171819202321222324252 unmodified lines2829303134353637323334383536373819 unmodified lines5859606166676869706263646566677468697071727374818283848575767778877980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181199 unmodified lines# Transforms
## Summary
Transforms map or filter the input or output of a child process. They are defined by passing an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option.Transforms map or filter the input or output of a child process. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).
```jsimport {execa} from 'execa';
const transform = async function * (lines) { for await (const line of lines) { const prefix = line.includes('error') ? 'ERROR' : 'INFO' yield `${prefix}: ${line}`const transform = function * (line) { const prefix = line.includes('error') ? 'ERROR' : 'INFO'; yield `${prefix}: ${line}`; }};
const {stdout} = await execa('echo', ['hello'], {stdout: transform});console.log(stdout); // HELLO```
## Encoding
The `lines` argument passed to the transform is an [`AsyncIterable<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `AsyncIterable<Uint8Array>` instead.The `line` argument passed to the transform is a string. If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `Uint8Array` instead.
The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `lines` argument's type.
## Filtering2 unmodified lines
```jsimport {execa} from 'execa';
const transform = async function * (lines) { for await (const line of lines) { if (!line.includes('secret')) { yield line;const transform = function * (line) { if (!line.includes('secret')) { yield line; } }};
const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});19 unmodified lines
By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.
```jsconst transform = async function * (lines) { for await (const line of lines) { yield JSON.parse(line) }}const transform = function * (line) { yield JSON.parse(line);};
const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});for (const data of stdout) { console.log(stdout) // {...} console.log(stdout); // {...}}```
`stdin` can also use `objectMode: true`.
```jsconst transform = async function * (lines) { for await (const line of lines) { yield JSON.stringify(line) }}const transform = function * (line) { yield JSON.stringify(line);};
const input = [{event: 'example'}, {event: 'otherExample'}]const input = [{event: 'example'}, {event: 'otherExample'}];await execa('./jsonlines-input.js', {stdin: [input, {transform, objectMode: true}]});```
## Sharing state
State can be shared between calls of the `transform` and [`final`](#finalizing) functions.
```jslet count = 0
// Prefix line numberconst transform = function * (line) { yield `[${count++}] ${line}`;};```
## Finalizing
To create additional lines after the last one, a `final` generator function can be used by passing a `{transform, final}` plain object.
```jslet count = 0;
const transform = function * (line) { count += 1; yield line;};
const final = function * () { yield `Number of lines: ${count}`;};
const {stdout} = await execa('./command.js', {stdout: {transform, final}});console.log(stdout); // Ends with: 'Number of lines: 54'```
## Combining
The [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) and [`stdio`](../readme.md#stdio-1) options can accept an array of values. While this is not specific to transforms, this can be useful with them too. For example, the following transform impacts the value printed by `inherit`.
9 unmodified linesmodified:lib/stdio/duplex.js+11 added-1 removed
18 unmodified lines192021222323242526272 unmodified lines30313233343536373839404142434445464712 unmodified lines18 unmodified lines }); let iterable = passThrough.iterator();
for (const generator of generators) { iterable = generator(iterable); iterable = applyGenerator(generator, iterable); }
const readableStream = Readable.from(iterable, { objectMode: readableObjectMode,2 unmodified lines const duplexStream = Duplex.from({writable: passThrough, readable: readableStream}); return duplexStream;};
const applyGenerator = async function * ({transform, final}, chunks) { for await (const chunk of chunks) { yield * transform(chunk); }
if (final !== undefined) { yield * final(); }};
/*When an error is thrown in a generator, the PassThrough is aborted.
This creates a race condition for which error is propagated, due to the Duplex throwing twice:12 unmodified linesmodified:lib/stdio/encoding.js+39 added-36 removed
10 unmodified lines11 return newStdioStreams;12 }13 14 const objectMode = newStdioStreams.findLast(({type}) => type === 'generator')?.value.objectMode === true;15- const encodingEndGenerator = objectMode ? encodingEndObjectGenerator : encodingEndStringGenerator;16- const transform = encodingEndGenerator.bind(undefined, encoding);15+ const stringDecoder = new StringDecoder(encoding);16+ const generator = objectMode17+ ? {18+ transform: encodingEndObjectGenerator.bind(undefined, stringDecoder),19+ }20+ : {21+ transform: encodingEndStringGenerator.bind(undefined, stringDecoder),22+ final: encodingEndStringFinal.bind(undefined, stringDecoder),23+ };24 return [25 ...newStdioStreams,26 {27 ...newStdioStreams[0],28 type: 'generator',22- value: {transform, binary: true, objectMode},29+ value: {...generator, binary: true, objectMode},30 encoding: 'buffer',31 },32 ];33 };5 unmodified lines39 40 // eslint-disable-next-line unicorn/text-encoding-identifier-case41 const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);42 36-const encodingEndStringGenerator = async function * (encoding, chunks) {37- const stringDecoder = new StringDecoder(encoding);43+const encodingEndStringGenerator = function * (stringDecoder, chunk) {44+ yield stringDecoder.write(chunk);45+};46 39- for await (const chunk of chunks) {40- yield stringDecoder.write(chunk);41- }42-47+const encodingEndStringFinal = function * (stringDecoder) {48 const lastChunk = stringDecoder.end();49 if (lastChunk !== '') {50 yield lastChunk;51 }52 };53 49-const encodingEndObjectGenerator = async function * (encoding, chunks) {50- const stringDecoder = new StringDecoder(encoding);51-52- for await (const chunk of chunks) {53- yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;54- }54+const encodingEndObjectGenerator = function * (stringDecoder, chunk) {55+ yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk;56 };57 58 /*59 When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.6 unmodified lines66 However, those are converted to Buffer:67 - on writes: `Duplex.writable` `decodeStrings: true` default option68 - on reads: `Duplex.readable` `readableEncoding: null` default option69 */69-export const getEncodingStartGenerator = encoding => encoding === 'buffer'70- ? encodingStartBufferGenerator71- : encodingStartStringGenerator;72-73-const encodingStartBufferGenerator = async function * (chunks) {74- const textEncoder = new TextEncoder();75-76- for await (const chunk of chunks) {77- if (Buffer.isBuffer(chunk)) {78- yield new Uint8Array(chunk);79- } else if (typeof chunk === 'string') {80- yield textEncoder.encode(chunk);81- } else {82- yield chunk;83- }70+export const getEncodingStartGenerator = encoding => {71+ if (encoding === 'buffer') {72+ return {transform: encodingStartBufferGenerator.bind(undefined, new TextEncoder())};73 }85-};74 87-const encodingStartStringGenerator = async function * (chunks) {75 const textDecoder = new TextDecoder();76+ return {77+ transform: encodingStartStringGenerator.bind(undefined, textDecoder),78+ final: encodingStartStringFinal.bind(undefined, textDecoder),79+ };80+};81 90- for await (const chunk of chunks) {91- yield Buffer.isBuffer(chunk) || isUint8Array(chunk)92- ? textDecoder.decode(chunk, {stream: true})93- : chunk;82+const encodingStartBufferGenerator = function * (textEncoder, chunk) {83+ if (Buffer.isBuffer(chunk)) {84+ yield new Uint8Array(chunk);85+ } else if (typeof chunk === 'string') {86+ yield textEncoder.encode(chunk);87+ } else {88+ yield chunk;89 }90+};91 92+const encodingStartStringGenerator = function * (textDecoder, chunk) {93+ yield Buffer.isBuffer(chunk) || isUint8Array(chunk)94+ ? textDecoder.decode(chunk, {stream: true})95+ : chunk;96+};97+98+const encodingStartStringFinal = function * (textDecoder) {99 const lastChunk = textDecoder.decode();100 if (lastChunk !== '') {101 yield lastChunk;102 }1 unmodified linemodified:lib/stdio/generator.js+17 added-21 removed
16 unmodified lines17 return [...nonGenerators, ...sortGenerators(newGenerators)];18 };19 20 const normalizeGenerator = ({value, ...stdioStream}, index, newGenerators) => {21- const {transform, binary = false, objectMode = false} = isGeneratorOptions(value) ? value : {transform: value};21+ const {transform, final, binary = false, objectMode = false} = isGeneratorOptions(value) ? value : {transform: value};22 const objectModes = stdioStream.direction === 'output'23 ? getOutputObjectModes(objectMode, index, newGenerators)24 : getInputObjectModes(objectMode, index, newGenerators);25- return {...stdioStream, value: {transform, binary, ...objectModes}};25+ return {...stdioStream, value: {transform, final, binary, ...objectModes}};26 };27 28 /*29 `objectMode` determines the return value's type, i.e. the `readableObjectMode`.23 unmodified lines53 /*54 Generators can be used to transform/filter standard streams.55 56 Generators have a simple syntax, yet allows all of the following:57-- Sharing state between chunks, by using logic before the `for` loop58-- Flushing logic, by using logic after the `for` loop57+- Sharing `state` between chunks58+- Flushing logic, by using a `final` function59 - Asynchronous logic60 - Emitting multiple chunks from a single source chunk, even if spaced in time, by using multiple `yield`61 - Filtering, by using no `yield`62 3 unmodified lines66 67 Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.68 */69 export const generatorToDuplexStream = ({70- value: {transform, binary, writableObjectMode, readableObjectMode},70+ value: {transform, final, binary, writableObjectMode, readableObjectMode},71 encoding,72 optionName,73 }) => {74 const generators = [75 getEncodingStartGenerator(encoding),76 getLinesGenerator(encoding, binary),77- transform,78- getValidateTransformReturn(readableObjectMode, optionName),77+ {transform, final},78+ {transform: getValidateTransformReturn(readableObjectMode, optionName)},79 ].filter(Boolean);80 const duplexStream = generatorsToDuplex(generators, {writableObjectMode, readableObjectMode});81 return {value: duplexStream};82 };1 unmodified line84 const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode85 ? validateObjectTransformReturn.bind(undefined, optionName)86 : validateStringTransformReturn.bind(undefined, optionName);87 88-const validateObjectTransformReturn = async function * (optionName, chunks) {89- for await (const chunk of chunks) {90- if (chunk === null) {91- throw new Error(`The \`${optionName}\` option's function must not return null.`);92- }93-94- yield chunk;88+const validateObjectTransformReturn = function * (optionName, chunk) {89+ if (chunk === null) {90+ throw new TypeError(`The \`${optionName}\` option's function must not return null.`);91 }92+93+ yield chunk;94 };95 98-const validateStringTransformReturn = async function * (optionName, chunks) {99- for await (const chunk of chunks) {100- if (typeof chunk !== 'string' && !isBinary(chunk)) {101- throw new Error(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`);102- }103-104- yield chunk;96+const validateStringTransformReturn = function * (optionName, chunk) {97+ if (typeof chunk !== 'string' && !isBinary(chunk)) {98+ throw new TypeError(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`);99 }100+101+ yield chunk;102 };103 104 // `childProcess.stdin|stdout|stderr|stdio` is directly mutated.105 export const pipeGenerator = (spawned, {value, direction, index}) => {15 unmodified linesmodified:lib/stdio/lines.js+43 added-43 removed
17 unmodified lines18 && lines19 && !isSync20 && willPipeStreams(stdioStreams);21 22-const linesEndGenerator = async function * (chunks) {23- yield * chunks;22+const linesEndGenerator = function * (chunk) {23+ yield chunk;24 };25 26 // Split chunks line-wise for generators passed to the `std*` options27 export const getLinesGenerator = (encoding, binary) => {28 if (binary) {29 return;30 }31 32- return encoding === 'buffer' ? linesUint8ArrayGenerator : linesStringGenerator;32+ const info = encoding === 'buffer' ? linesUint8ArrayInfo : linesStringInfo;33+ const state = {previousChunks: info.emptyValue};34+ return {35+ transform: linesGenerator.bind(undefined, state, info),36+ final: linesFinal.bind(undefined, state),37+ };38 };39 35-const linesUint8ArrayGenerator = async function * (chunks) {36- yield * linesGenerator({37- chunks,38- emptyValue: new Uint8Array(0),39- newline: 0x0A,40- concat: concatUint8Array,41- isValidType: isUint8Array,42- });43-};44-40 const concatUint8Array = (firstChunk, secondChunk) => {41 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);42 chunk.set(firstChunk, 0);43 chunk.set(secondChunk, firstChunk.length);44 return chunk;45 };46 52-const linesStringGenerator = async function * (chunks) {53- yield * linesGenerator({54- chunks,55- emptyValue: '',56- newline: '\n',57- concat: concatString,58- isValidType: isString,59- });47+const linesUint8ArrayInfo = {48+ emptyValue: new Uint8Array(0),49+ newline: 0x0A,50+ concat: concatUint8Array,51+ isValidType: isUint8Array,52 };53 54 const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;55 const isString = chunk => typeof chunk === 'string';56 57+const linesStringInfo = {58+ emptyValue: '',59+ newline: '\n',60+ concat: concatString,61+ isValidType: isString,62+};63+64 // This imperative logic is much faster than using `String.split()` and uses very low memory.65 // Also, it allows sharing it with `Uint8Array`.67-const linesGenerator = async function * ({chunks, emptyValue, newline, concat, isValidType}) {68- let previousChunks = emptyValue;66+const linesGenerator = function * (state, {emptyValue, newline, concat, isValidType}, chunk) {67+ if (!isValidType(chunk)) {68+ yield chunk;69+ return;70+ }71 70- for await (const chunk of chunks) {71- if (!isValidType(chunk)) {72- yield chunk;73- continue;74- }72+ let {previousChunks} = state;73+ let start = -1;74 76- let start = -1;75+ for (let end = 0; end < chunk.length; end += 1) {76+ if (chunk[end] === newline) {77+ let line = chunk.slice(start + 1, end + 1);78 78- for (let end = 0; end < chunk.length; end += 1) {79- if (chunk[end] === newline) {80- let line = chunk.slice(start + 1, end + 1);81-82- if (previousChunks.length > 0) {83- line = concat(previousChunks, line);84- previousChunks = emptyValue;79+ if (previousChunks.length > 0) {80+ line = concat(previousChunks, line);81+ previousChunks = emptyValue;85- }86-87- yield line;88- start = end;82 }90- }83 92- if (start !== chunk.length - 1) {93- previousChunks = concat(previousChunks, chunk.slice(start + 1));84+ yield line;85+ start = end;86 }87 }88 89+ if (start !== chunk.length - 1) {90+ previousChunks = concat(previousChunks, chunk.slice(start + 1));91+ }92+93+ state.previousChunks = previousChunks;94+};95+96+const linesFinal = function * ({previousChunks}) {97 if (previousChunks.length > 0) {98 yield previousChunks;99 }100 };modified:lib/stdio/type.js+10 added-10 removed
1 unmodified line2 import {isUint8Array} from './utils.js';3 4 // The `stdin`/`stdout`/`stderr` option can be of many types. This detects it.5 export const getStdioOptionType = (stdioOption, optionName) => {6- if (isAsyncGenerator(stdioOption)) {6+ if (isGenerator(stdioOption)) {7 return 'generator';8 }9 10- if (isSyncGenerator(stdioOption)) {11- throw new TypeError(`The \`${optionName}\` option must use an asynchronous generator, not a synchronous one.`);12- }13-10 if (isUrl(stdioOption)) {11 return 'fileUrl';12 }13 23 unmodified lines37 38 return 'native';39 };40 45-const getGeneratorObjectType = ({transform, binary, objectMode}, optionName) => {46- if (!isAsyncGenerator(transform)) {47- throw new TypeError(`The \`${optionName}.transform\` option must use an asynchronous generator.`);41+const getGeneratorObjectType = ({transform, final, binary, objectMode}, optionName) => {42+ if (!isGenerator(transform)) {43+ throw new TypeError(`The \`${optionName}.transform\` option must be a generator.`);44 }45 46+ if (final !== undefined && !isGenerator(final)) {47+ throw new TypeError(`The \`${optionName}.final\` option must be a generator.`);48+ }49+50 checkBooleanOption(binary, `${optionName}.binary`);51 checkBooleanOption(objectMode, `${optionName}.objectMode`);52 53 return 'generator';4 unmodified lines58 throw new TypeError(`The \`${optionName}\` option must use a boolean.`);59 }60 };61 62-const isAsyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]';63-const isSyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';62+const isGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]'63+ || Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';64 export const isGeneratorOptions = stdioOption => typeof stdioOption === 'object'65 && stdioOption !== null66 && stdioOption.transform !== undefined;67 32 unmodified linesmodified:lib/stream.js+2 added-2 removed
39 unmodified lines40 ? all.pipe(generatorToDuplexStream({value: allStreamGenerator, encoding}).value)41 : all;42 43 const allStreamGenerator = {44- async * transform(chunks) {45- yield * chunks;44+ * transform(chunk) {45+ yield chunk;46 },47 binary: true,48 writableObjectMode: true,49 readableObjectMode: true,133 unmodified linesmodified:test/fixtures/nested-inherit.js+2 added-4 removed
123456745678#!/usr/bin/env nodeimport {execa} from '../../index.js';
const uppercaseGenerator = async function * (lines) { for await (const line of lines) { yield line.toUpperCase(); }const uppercaseGenerator = function * (line) { yield line.toUpperCase();};
await execa('noop-fd.js', ['1'], {stdout: ['inherit', uppercaseGenerator]});modified:test/helpers/generator.js+31 added-25 removed
1 import {setImmediate} from 'node:timers/promises';2 import {foobarObject} from './input.js';3 4-export const noopGenerator = objectMode => ({5- async * transform(lines) {6- yield * lines;4+export const noopGenerator = (objectMode, binary) => ({5+ * transform(line) {6+ yield line;7 },8 objectMode,9+ binary,10 });11 12 export const serializeGenerator = {12- async * transform(objects) {13- for await (const object of objects) {14- yield JSON.stringify(object);15- }13+ * transform(object) {14+ yield JSON.stringify(object);15 },16 objectMode: true,17 };18 19 export const getOutputsGenerator = (inputs, objectMode) => ({21- async * transform(lines) {22- // eslint-disable-next-line no-unused-vars23- for await (const line of lines) {24- yield * inputs;25- }20+ * transform() {21+ yield * inputs;22 },23 objectMode,24 });25 26 export const getOutputGenerator = (input, objectMode) => ({31- async * transform(lines) {32- // eslint-disable-next-line no-unused-vars33- for await (const line of lines) {34- yield input;35- }27+ * transform() {28+ yield input;29 },30 objectMode,31 });32 33 export const outputObjectGenerator = getOutputGenerator(foobarObject, true);34 35 export const getChunksGenerator = (chunks, objectMode, binary) => ({43- async * transform(lines) {44- // eslint-disable-next-line no-unused-vars45- for await (const line of lines) {46- for (const chunk of chunks) {47- yield chunk;48- // eslint-disable-next-line no-await-in-loop49- await setImmediate();36+ async * transform() {37+ for (const chunk of chunks) {38+ yield chunk;39+ // eslint-disable-next-line no-await-in-loop40+ await setImmediate();50- }41 }42 },43 objectMode,44 binary,45 });46+47+const noYieldTransform = function * () {};48+49+export const noYieldGenerator = objectMode => ({50+ transform: noYieldTransform,51+ objectMode,52+});53+54+export const convertTransformToFinal = (transform, final) => {55+ if (!final) {56+ return transform;57+ }58+59+ const generatorOptions = typeof transform === 'function' ? {transform} : transform;60+ return ({...generatorOptions, transform: noYieldTransform, final: generatorOptions.transform});61+};modified:test/stdio/encoding.js+4 added-2 removed
126 unmodified lines127 test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);128 test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);129 /* eslint-enable unicorn/text-encoding-identifier-case */130 131-test('validate unknown encodings', async t => {132- await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131+test('validate unknown encodings', t => {132+ t.throws(() => {133+ execa('noop.js', {encoding: 'unknownEncoding'});134+ }, {code: 'ERR_UNKNOWN_ENCODING'});135 });136 137 const foobarArray = ['fo', 'ob', 'ar', '..'];138 58 unmodified linesmodified:test/stdio/generator.js+136 added-134 removed
7 unmodified lines8 import {execa, execaSync} from '../../index.js';9 import {setFixtureDir} from '../helpers/fixtures-dir.js';10 import {getStdio} from '../helpers/stdio.js';11 import {foobarString, foobarUint8Array, foobarBuffer, foobarObject, foobarObjectString} from '../helpers/input.js';12-import {serializeGenerator, noopGenerator, getOutputsGenerator, getOutputGenerator, outputObjectGenerator} from '../helpers/generator.js';12+import {13+ serializeGenerator,14+ noopGenerator,15+ getOutputsGenerator,16+ getOutputGenerator,17+ outputObjectGenerator,18+ noYieldGenerator,19+ convertTransformToFinal,20+} from '../helpers/generator.js';21 22 setFixtureDir();23 24 const textEncoder = new TextEncoder();1 unmodified line26 27 const foobarUppercase = foobarString.toUpperCase();28 const foobarHex = foobarBuffer.toString('hex');29 22-const uppercaseGenerator = async function * (lines) {23- for await (const line of lines) {24- yield line.toUpperCase();25- }30+const uppercaseGenerator = function * (line) {31+ yield line.toUpperCase();32 };33 28-const uppercaseBufferGenerator = async function * (lines) {29- for await (const line of lines) {30- yield textDecoder.decode(line).toUpperCase();31- }34+const uppercaseBufferGenerator = function * (line) {35+ yield textDecoder.decode(line).toUpperCase();36 };37 38 const getInputObjectMode = objectMode => objectMode39 ? {input: [foobarObject], generator: serializeGenerator, output: foobarObjectString}67 unmodified lines107 test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputNullGenerator(true), 'stdin-fd.js', true);108 test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, outputNullGenerator(false), 'noop-fd.js', false);109 test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, outputNullGenerator(true), 'noop-fd.js', true);110 111+const testGeneratorFinal = async (t, fixtureName) => {112+ const {stdout} = await execa(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString), true)});113+ t.is(stdout, foobarString);114+};115+116+test('Generators "final" can be used', testGeneratorFinal, 'noop.js');117+test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');118+119+test('Generators "final" return value is validated', async t => {120+ const childProcess = execa('noop.js', {stdout: convertTransformToFinal(outputNullGenerator(true), true)});121+ await t.throwsAsync(childProcess, {message: /not return null/});122+});123+124 // eslint-disable-next-line max-params125 const testGeneratorOutput = async (t, index, reject, useShortcutProperty, objectMode) => {126 const {generator, output} = getOutputObjectMode(objectMode);127 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';122 unmodified lines250 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: uppercaseGenerator, input: foobarUint8Array});251 t.is(stdout, foobarUppercase);252 });253 237-const syncGenerator = function * () {};238-254 const testInvalidGenerator = (t, index, stdioOption) => {255 t.throws(() => {241- execa('empty.js', getStdio(index, stdioOption));242- }, {message: /asynchronous generator/});256+ execa('empty.js', getStdio(index, {...noopGenerator(), ...stdioOption}));257+ }, {message: /must be a generator/});258 };259 245-test('Cannot use sync generators with stdin', testInvalidGenerator, 0, syncGenerator);246-test('Cannot use sync generators with stdout', testInvalidGenerator, 1, syncGenerator);247-test('Cannot use sync generators with stderr', testInvalidGenerator, 2, syncGenerator);248-test('Cannot use sync generators with stdio[*]', testInvalidGenerator, 3, syncGenerator);249-test('Cannot use sync generators with stdin, with options', testInvalidGenerator, 0, {transform: syncGenerator});250-test('Cannot use sync generators with stdout, with options', testInvalidGenerator, 1, {transform: syncGenerator});251-test('Cannot use sync generators with stderr, with options', testInvalidGenerator, 2, {transform: syncGenerator});252-test('Cannot use sync generators with stdio[*], with options', testInvalidGenerator, 3, {transform: syncGenerator});260 test('Cannot use invalid "transform" with stdin', testInvalidGenerator, 0, {transform: true});261 test('Cannot use invalid "transform" with stdout', testInvalidGenerator, 1, {transform: true});262 test('Cannot use invalid "transform" with stderr', testInvalidGenerator, 2, {transform: true});263 test('Cannot use invalid "transform" with stdio[*]', testInvalidGenerator, 3, {transform: true});264+test('Cannot use invalid "final" with stdin', testInvalidGenerator, 0, {final: true});265+test('Cannot use invalid "final" with stdout', testInvalidGenerator, 1, {final: true});266+test('Cannot use invalid "final" with stderr', testInvalidGenerator, 2, {final: true});267+test('Cannot use invalid "final" with stdio[*]', testInvalidGenerator, 3, {final: true});268 269 const testInvalidBinary = (t, index, optionName) => {270 t.throws(() => {271 execa('empty.js', getStdio(index, {transform: uppercaseGenerator, [optionName]: 'true'}));21 unmodified lines293 test('Cannot use generators with sync methods and stdio[*]', testSyncMethods, 3);294 295 const repeatHighWaterMark = 10;296 286-const writerGenerator = async function * (lines) {287- // eslint-disable-next-line no-unused-vars288- for await (const line of lines) {289- for (let index = 0; index < getDefaultHighWaterMark() * repeatHighWaterMark; index += 1) {290- yield '\n';297+const writerGenerator = function * () {298+ for (let index = 0; index < getDefaultHighWaterMark() * repeatHighWaterMark; index += 1) {299+ yield '\n';291- }300 }301 };302 295-const passThroughGenerator = async function * (chunks) {296- yield * chunks;303+const getLengthGenerator = function * (chunk) {304+ yield `${chunk.length}`;305 };306 299-const getLengthGenerator = async function * (chunks) {300- for await (const chunk of chunks) {301- yield `${chunk.length}`;302- }303-};304-307 const testHighWaterMark = async (t, passThrough, binary, objectMode) => {308 const {stdout} = await execa('noop.js', {309 stdout: [310 ...(objectMode ? [outputObjectGenerator] : []),311 writerGenerator,310- ...(passThrough ? [{transform: passThroughGenerator, binary}] : []),312+ ...(passThrough ? [noopGenerator(false, binary)] : []),313 {transform: getLengthGenerator, binary: true},314 ],315 });316 t.is(stdout, `${getDefaultHighWaterMark()}`.repeat(repeatHighWaterMark));4 unmodified lines321 test('Stream respects highWaterMark, binary passThrough', testHighWaterMark, true, true, false);322 test('Stream respects highWaterMark, objectMode as input but not output', testHighWaterMark, false, false, true);323 324 const getTypeofGenerator = objectMode => ({323- async * transform(lines) {324- for await (const line of lines) {325- yield Object.prototype.toString.call(line);326- }325+ * transform(line) {326+ yield Object.prototype.toString.call(line);327 },328 objectMode,329 });330 78 unmodified lines409 test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);410 test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);411 412 // eslint-disable-next-line max-params413-const testGeneratorReturnType = async (t, input, encoding, reject, objectMode) => {413+const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {414 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';415 const {stdout} = await execa(fixtureName, ['1', 'other'], {416- stdout: getOutputGenerator(input, objectMode),416+ stdout: convertTransformToFinal(getOutputGenerator(input, objectMode), final),417 encoding,418 reject,419 });420 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;421 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();422 t.is(output, foobarString);423 };424 425-test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false);426-test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false);427-test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false);428-test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false);429-test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false);430-test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false);431-test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false);432-test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false);433-test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false);434-test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false);435-test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false);436-test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false);437-test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true);438-test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true);439-test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true);440-test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true);441-test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true);442-test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true);443-test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true);444-test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true);445-test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true);446-test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true);447-test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true);448-test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true);425+test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);426+test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);427+test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);428+test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);429+test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);430+test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);431+test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);432+test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);433+test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);434+test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);435+test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);436+test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);437+test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);438+test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);439+test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);440+test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);441+test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);442+test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);443+test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);444+test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);445+test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);446+test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);447+test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);448+test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);449+test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);450+test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);451+test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);452+test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);453+test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);454+test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);455+test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);456+test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);457+test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);458+test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);459+test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);460+test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);461+test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);462+test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);463+test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);464+test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);465+test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);466+test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);467+test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);468+test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);469+test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);470+test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);471+test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);472+test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);473 474 const multibyteChar = '\u{1F984}';475 const multibyteString = `${multibyteChar}${multibyteChar}`;476 const multibyteUint8Array = textEncoder.encode(multibyteString);19 unmodified lines496 497 test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);498 test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);499 476-// eslint-disable-next-line require-yield477-const noYieldGenerator = async function * (lines) {478- // eslint-disable-next-line no-empty, no-unused-vars479- for await (const line of lines) {}480-};481-482-const testNoYield = async (t, objectMode, output) => {483- const {stdout} = await execa('noop.js', {stdout: {transform: noYieldGenerator, objectMode}});500+const testNoYield = async (t, objectMode, final, output) => {501+ const {stdout} = await execa('noop.js', {stdout: convertTransformToFinal(noYieldGenerator(objectMode), final)});502 t.deepEqual(stdout, output);503 };504 487-test('Generator can filter by not calling yield', testNoYield, false, '');488-test('Generator can filter by not calling yield, objectMode', testNoYield, true, []);505+test('Generator can filter "transform" by not calling yield', testNoYield, false, false, '');506+test('Generator can filter "transform" by not calling yield, objectMode', testNoYield, true, false, []);507+test('Generator can filter "final" by not calling yield', testNoYield, false, false, '');508+test('Generator can filter "final" by not calling yield, objectMode', testNoYield, true, false, []);509 510 const prefix = '> ';511 const suffix = ' <';512 493-const multipleYieldGenerator = async function * (lines) {494- for await (const line of lines) {495- yield prefix;496- await setImmediate();497- yield line;498- await setImmediate();499- yield suffix;513+const multipleYieldGenerator = async function * (line = foobarString) {514+ yield prefix;515+ await setImmediate();516+ yield line;517+ await setImmediate();518+ yield suffix;500- }519 };520 503-test('Generator can yield multiple times at different moments', async t => {504- const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: multipleYieldGenerator});521+const testMultipleYields = async (t, final) => {522+ const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(multipleYieldGenerator, final)});523 t.is(stdout, `${prefix}${foobarString}${suffix}`);506-});524+};525 526+test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);527+test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);528+529 const testInputFile = async (t, getOptions, reversed) => {530 const filePath = tempfile();531 await writeFile(filePath, foobarString);532 const {stdin, ...options} = getOptions(filePath);44 unmodified lines577 });578 579 const casedSuffix = 'k';580 560-const appendGenerator = async function * (lines) {561- for await (const line of lines) {562- yield `${line}${casedSuffix}`;563- }581+const appendGenerator = function * (line) {582+ yield `${line}${casedSuffix}`;583 };584 585 const testAppendInput = async (t, reversed) => {586 const stdin = [foobarUint8Array, uppercaseGenerator, appendGenerator];32 unmodified lines619 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));620 });621 622 test('Generators take "maxBuffer" into account, objectMode', async t => {604- const bigArray = Array.from({length: maxBuffer});623+ const bigArray = Array.from({length: maxBuffer}).fill('.');624 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputsGenerator(bigArray, true)});625 t.is(stdout.length, maxBuffer);626 627 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));628 });629 611-const timeoutGenerator = async function * (timeout, lines) {612- for await (const line of lines) {613- await setTimeout(timeout);614- yield line;615- }630+const timeoutGenerator = async function * (timeout) {631+ await setTimeout(timeout);632+ yield foobarString;633 };634 618-test('Generators are awaited on success', async t => {619- const {stdout} = await execa('noop-fd.js', ['1', foobarString], {maxBuffer, stdout: timeoutGenerator.bind(undefined, 1e3)});635+const testAsyncGenerators = async (t, final) => {636+ const {stdout} = await execa('noop.js', {637+ maxBuffer,638+ stdout: convertTransformToFinal(timeoutGenerator.bind(undefined, 1e2), final),639+ });640 t.is(stdout, foobarString);621-});641+};642 643+test('Generators "transform" is awaited on success', testAsyncGenerators, false);644+test('Generators "final" is awaited on success', testAsyncGenerators, true);645+646 // eslint-disable-next-line require-yield624-const throwingGenerator = async function * (lines) {625- // eslint-disable-next-line no-unreachable-loop626- for await (const line of lines) {627- throw new Error(`Generator error ${line}`);628- }647+const throwingGenerator = function * () {648+ throw new Error('Generator error');649 };650 631-test('Generators errors make process fail', async t => {651+const GENERATOR_ERROR_REGEXP = /Generator error/;652+653+const testThrowingGenerator = async (t, final) => {654 await t.throwsAsync(633- execa('noop-fd.js', ['1', foobarString], {stdout: throwingGenerator}),634- {message: /Generator error foobar/},655+ execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(throwingGenerator, final)}),656+ {message: GENERATOR_ERROR_REGEXP},657 );636-});658+};659 660+test('Generators "transform" errors make process fail', testThrowingGenerator, false);661+test('Generators "final" errors make process fail', testThrowingGenerator, true);662+663 test('Generators errors make process fail even when other output generators do not throw', async t => {664 await t.throwsAsync(665 execa('noop-fd.js', ['1', foobarString], {stdout: [noopGenerator(false), throwingGenerator, noopGenerator(false)]}),641- {message: /Generator error foobar/},666+ {message: GENERATOR_ERROR_REGEXP},667 );668 });669 670 test('Generators errors make process fail even when other input generators do not throw', async t => {671 const childProcess = execa('stdin-fd.js', ['0'], {stdin: [noopGenerator(false), throwingGenerator, noopGenerator(false)]});672 childProcess.stdin.write('foobar\n');648- await t.throwsAsync(childProcess, {message: /Generator error foobar/});673+ await t.throwsAsync(childProcess, {message: GENERATOR_ERROR_REGEXP});674 });650-651-// eslint-disable-next-line require-yield652-const errorHandlerGenerator = async function * (state, lines) {653- try {654- // eslint-disable-next-line no-unused-vars655- for await (const line of lines) {656- await setTimeout(1e8);657- }658- } catch (error) {659- state.error = error;660- }661-};662-663-test('Process streams failures make generators throw', async t => {664- const state = {};665- const childProcess = execa('noop-fail.js', ['1'], {stdout: errorHandlerGenerator.bind(undefined, state)});666- const error = new Error('test');667- childProcess.stdout.emit('error', error);668- const thrownError = await t.throwsAsync(childProcess);669- t.is(error, thrownError);670- await setImmediate();671- t.is(state.error.code, 'ABORT_ERR');672-});modified:test/stdio/lines.js+8 added-13 removed
27 unmodified lines28 29 const bigLine = '.'.repeat(1e5);30 const manyChunks = Array.from({length: 1e3}).fill('.');31 32-const inputGenerator = async function * (input, chunks) {33- // eslint-disable-next-line no-unused-vars34- for await (const chunk of chunks) {35- for (const inputItem of input) {36- yield inputItem;37- // eslint-disable-next-line no-await-in-loop38- await scheduler.yield();32+const inputGenerator = async function * (input) {33+ for (const inputItem of input) {34+ yield inputItem;35+ // eslint-disable-next-line no-await-in-loop36+ await scheduler.yield();39- }37 }38 };39 43-const resultGenerator = async function * (lines, chunks) {44- for await (const chunk of chunks) {45- lines.push(chunk);46- yield chunk;40+const resultGenerator = function * (lines, chunk) {41+ lines.push(chunk);42+ yield chunk;47- }43 };44 45 const textEncoder = new TextEncoder();46 const textDecoder = new TextDecoder();183 unmodified linesmodified:index.d.ts+6 added-4 removed
18 unmodified lines19 | 'inherit';20 21 // @todo Use `string`, `Uint8Array` or `unknown` for both the argument and the return type, based on whether `encoding: 'buffer'` and `objectMode: true` are used.22 // See https://github.com/sindresorhus/execa/issues/69423-type StdioTransform = ((chunks: Iterable<unknown>) => AsyncGenerator<unknown, void, void>);23+type StdioTransform = (chunk: unknown) => AsyncGenerator<unknown, void, void> | Generator<unknown, void, void>;24+type StdioFinal = () => AsyncGenerator<unknown, void, void> | Generator<unknown, void, void>;25 26 type StdioTransformFull = {27 transform: StdioTransform;28+ final?: StdioFinal;29 binary?: boolean;30 objectMode?: boolean;31 };32 287 unmodified lines320 - an `Uint8Array`.321 322 This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.323 322- This can also be an async generator function to transform the input. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)324+ This can also be a generator function to transform the input. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)325 326 @default `inherit` with `$`, `pipe` otherwise327 */328 readonly stdin?: StdinOption<IsSync>;12 unmodified lines341 - a web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).342 343 This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.344 343- This can also be an async generator function to transform the output. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)345+ This can also be a generator function to transform the output. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)346 347 @default 'pipe'348 */349 readonly stdout?: StdoutStderrOption<IsSync>;12 unmodified lines362 - a web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).363 364 This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.365 364- This can also be an async generator function to transform the output. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)366+ This can also be a generator function to transform the output. [Learn more.](https://github.com/sindresorhus/execa/tree/main/docs/transform.md)367 368 @default 'pipe'369 */370 readonly stderr?: StdoutStderrOption<IsSync>;936 unmodified linesmodified:index.test-d.ts+164 added-109 removed
32 unmodified lines33 expectType<AnySyncChunk>({} as ExecaSyncReturnValue['stdout']);34 expectType<AnySyncChunk>({} as ExecaSyncReturnValue['stderr']);35 expectType<[undefined, AnySyncChunk, AnySyncChunk]>({} as ExecaSyncReturnValue['stdio']);36 37-const objectGenerator = async function * (lines: Iterable<unknown>) {38- for await (const line of lines) {39- yield JSON.parse(line as string) as object;40- }37+const objectGenerator = function * (line: unknown) {38+ yield JSON.parse(line as string) as object;39 };40 43-const unknownArrayGenerator = async function * (lines: Iterable<unknown>) {44- for await (const line of lines) {45- yield line;46- }41+const objectFinal = function * () {42+ yield {};43 };44 49-const booleanGenerator = async function * (lines: Iterable<boolean>) {50- for await (const line of lines) {51- yield line;52- }45+const unknownGenerator = function * (line: unknown) {46+ yield line;47 };48 55-const arrayGenerator = async function * (lines: string[]) {56- for await (const line of lines) {57- yield line;58- }49+const unknownFinal = function * () {50+ yield {} as unknown;51 };52 61-const invalidReturnGenerator = async function * (lines: Iterable<string>) {62- for await (const line of lines) {63- yield line;64- }53+const booleanGenerator = function * (line: boolean) {54+ yield line;55+};56 57+const stringGenerator = function * (line: string) {58+ yield line;59+};60+61+const invalidReturnGenerator = function * (line: unknown) {62+ yield line;63 return false;64 };65 69-const syncGenerator = function * (lines: Iterable<string>) {70- for (const line of lines) {71- yield line;72- }73-66+const invalidReturnFinal = function * () {67+ yield {} as unknown;68 return false;69 };70 71+const asyncGenerator = async function * (line: unknown) {72+ yield line;73+};74+75+const asyncFinal = async function * () {76+ yield {} as unknown;77+};78+79 try {80 const execaPromise = execa('unicorns', {all: true});81 82 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});265 unmodified lines348 349 const ignoreFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'ignore']});350 expectType<undefined>(ignoreFd3Result.stdio[3]);351 350- const objectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: true}});352+ const objectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}});353 expectType<unknown[]>(objectTransformStdoutResult.stdout);354 expectType<[undefined, unknown[], string]>(objectTransformStdoutResult.stdio);355 354- const objectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, objectMode: true}});356+ const objectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}});357 expectType<unknown[]>(objectTransformStderrResult.stderr);358 expectType<[undefined, string, unknown[]]>(objectTransformStderrResult.stdio);359 358- const objectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, objectMode: true}]});360+ const objectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: true}]});361 expectType<unknown[]>(objectTransformStdioResult.stderr);362 expectType<[undefined, string, unknown[]]>(objectTransformStdioResult.stdio);363 362- const singleObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, objectMode: true}]});364+ const singleObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true}]});365 expectType<unknown[]>(singleObjectTransformStdoutResult.stdout);366 expectType<[undefined, unknown[], string]>(singleObjectTransformStdoutResult.stdio);367 366- const manyObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, objectMode: true}, {transform: objectGenerator, objectMode: true}]});368+ const manyObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true}, {transform: objectGenerator, final: objectFinal, objectMode: true}]});369 expectType<unknown[]>(manyObjectTransformStdoutResult.stdout);370 expectType<[undefined, unknown[], string]>(manyObjectTransformStdoutResult.stdio);371 370- const falseObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: false}});372+ const falseObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}});373 expectType<string>(falseObjectTransformStdoutResult.stdout);374 expectType<[undefined, string, string]>(falseObjectTransformStdoutResult.stdio);375 374- const falseObjectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, objectMode: false}});376+ const falseObjectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}});377 expectType<string>(falseObjectTransformStderrResult.stderr);378 expectType<[undefined, string, string]>(falseObjectTransformStderrResult.stdio);379 378- const falseObjectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, objectMode: false}]});380+ const falseObjectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: false}]});381 expectType<string>(falseObjectTransformStdioResult.stderr);382 expectType<[undefined, string, string]>(falseObjectTransformStdioResult.stdio);383 382- const undefinedObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator}});384+ const undefinedObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal}});385 expectType<string>(undefinedObjectTransformStdoutResult.stdout);386 expectType<[undefined, string, string]>(undefinedObjectTransformStdoutResult.stdio);387 386- const noObjectTransformStdoutResult = await execa('unicorns', {stdout: objectGenerator});388+ const noObjectTransformStdoutResult = await execa('unicorns', {stdout: objectGenerator, final: objectFinal});389 expectType<string>(noObjectTransformStdoutResult.stdout);390 expectType<[undefined, string, string]>(noObjectTransformStdoutResult.stdio);391 390- const trueTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: true}, stderr: {transform: objectGenerator, objectMode: true}, all: true});392+ const trueTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}, all: true});393 expectType<unknown[]>(trueTrueObjectTransformResult.stdout);394 expectType<unknown[]>(trueTrueObjectTransformResult.stderr);395 expectType<unknown[]>(trueTrueObjectTransformResult.all);396 expectType<[undefined, unknown[], unknown[]]>(trueTrueObjectTransformResult.stdio);397 396- const trueFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: true}, stderr: {transform: objectGenerator, objectMode: false}, all: true});398+ const trueFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}, all: true});399 expectType<unknown[]>(trueFalseObjectTransformResult.stdout);400 expectType<string>(trueFalseObjectTransformResult.stderr);401 expectType<unknown[]>(trueFalseObjectTransformResult.all);402 expectType<[undefined, unknown[], string]>(trueFalseObjectTransformResult.stdio);403 402- const falseTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: false}, stderr: {transform: objectGenerator, objectMode: true}, all: true});404+ const falseTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}, all: true});405 expectType<string>(falseTrueObjectTransformResult.stdout);406 expectType<unknown[]>(falseTrueObjectTransformResult.stderr);407 expectType<unknown[]>(falseTrueObjectTransformResult.all);408 expectType<[undefined, string, unknown[]]>(falseTrueObjectTransformResult.stdio);409 408- const falseFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, objectMode: false}, stderr: {transform: objectGenerator, objectMode: false}, all: true});410+ const falseFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}, all: true});411 expectType<string>(falseFalseObjectTransformResult.stdout);412 expectType<string>(falseFalseObjectTransformResult.stderr);413 expectType<string>(falseFalseObjectTransformResult.all);414 expectType<[undefined, string, string]>(falseFalseObjectTransformResult.stdio);103 unmodified lines518 expectType<string>(streamStderrError.stdout);519 expectType<undefined>(streamStderrError.stderr);520 expectType<string>(streamStderrError.all);521 520- const objectTransformStdoutError = error as ExecaError<{stdout: {transform: typeof objectGenerator; objectMode: true}}>;522+ const objectTransformStdoutError = error as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}}>;523 expectType<unknown[]>(objectTransformStdoutError.stdout);524 expectType<[undefined, unknown[], string]>(objectTransformStdoutError.stdio);525 524- const objectTransformStderrError = error as ExecaError<{stderr: {transform: typeof objectGenerator; objectMode: true}}>;526+ const objectTransformStderrError = error as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}}>;527 expectType<unknown[]>(objectTransformStderrError.stderr);528 expectType<[undefined, string, unknown[]]>(objectTransformStderrError.stdio);529 528- const objectTransformStdioError = error as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; objectMode: true}]}>;530+ const objectTransformStdioError = error as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}]}>;531 expectType<unknown[]>(objectTransformStdioError.stderr);532 expectType<[undefined, string, unknown[]]>(objectTransformStdioError.stdio);533 532- const falseObjectTransformStdoutError = error as ExecaError<{stdout: {transform: typeof objectGenerator; objectMode: false}}>;534+ const falseObjectTransformStdoutError = error as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}}>;535 expectType<string>(falseObjectTransformStdoutError.stdout);536 expectType<[undefined, string, string]>(falseObjectTransformStdoutError.stdio);537 536- const falseObjectTransformStderrError = error as ExecaError<{stderr: {transform: typeof objectGenerator; objectMode: false}}>;538+ const falseObjectTransformStderrError = error as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}}>;539 expectType<string>(falseObjectTransformStderrError.stderr);540 expectType<[undefined, string, string]>(falseObjectTransformStderrError.stdio);541 540- const falseObjectTransformStdioError = error as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; objectMode: false}]}>;542+ const falseObjectTransformStdioError = error as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}]}>;543 expectType<string>(falseObjectTransformStdioError.stderr);544 expectType<[undefined, string, string]>(falseObjectTransformStdioError.stdio);545 }546 285 unmodified lines832 execa('unicorns', {stdin: 1});833 execaSync('unicorns', {stdin: 1});834 execa('unicorns', {stdin: [1]});835 execaSync('unicorns', {stdin: [1]});834-execa('unicorns', {stdin: unknownArrayGenerator});835-expectError(execaSync('unicorns', {stdin: unknownArrayGenerator}));836-execa('unicorns', {stdin: [unknownArrayGenerator]});837-expectError(execaSync('unicorns', {stdin: [unknownArrayGenerator]}));836+execa('unicorns', {stdin: unknownGenerator});837+expectError(execaSync('unicorns', {stdin: unknownGenerator}));838+execa('unicorns', {stdin: [unknownGenerator]});839+expectError(execaSync('unicorns', {stdin: [unknownGenerator]}));840 expectError(execa('unicorns', {stdin: booleanGenerator}));839-expectError(execa('unicorns', {stdin: arrayGenerator}));841+expectError(execaSync('unicorns', {stdin: booleanGenerator}));842+expectError(execa('unicorns', {stdin: stringGenerator}));843+expectError(execaSync('unicorns', {stdin: stringGenerator}));844 expectError(execa('unicorns', {stdin: invalidReturnGenerator}));841-expectError(execa('unicorns', {stdin: syncGenerator}));842-execa('unicorns', {stdin: {transform: unknownArrayGenerator}});843-expectError(execaSync('unicorns', {stdin: {transform: unknownArrayGenerator}}));844-execa('unicorns', {stdin: [{transform: unknownArrayGenerator}]});845-expectError(execaSync('unicorns', {stdin: [{transform: unknownArrayGenerator}]}));845+expectError(execaSync('unicorns', {stdin: invalidReturnGenerator}));846+execa('unicorns', {stdin: asyncGenerator});847+expectError(execaSync('unicorns', {stdin: asyncGenerator}));848+execa('unicorns', {stdin: {transform: unknownGenerator}});849+expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator}}));850+execa('unicorns', {stdin: [{transform: unknownGenerator}]});851+expectError(execaSync('unicorns', {stdin: [{transform: unknownGenerator}]}));852 expectError(execa('unicorns', {stdin: {transform: booleanGenerator}}));847-expectError(execa('unicorns', {stdin: {transform: arrayGenerator}}));853+expectError(execaSync('unicorns', {stdin: {transform: booleanGenerator}}));854+expectError(execa('unicorns', {stdin: {transform: stringGenerator}}));855+expectError(execaSync('unicorns', {stdin: {transform: stringGenerator}}));856 expectError(execa('unicorns', {stdin: {transform: invalidReturnGenerator}}));849-expectError(execa('unicorns', {stdin: {transform: syncGenerator}}));857+expectError(execaSync('unicorns', {stdin: {transform: invalidReturnGenerator}}));858+execa('unicorns', {stdin: {transform: asyncGenerator}});859+expectError(execaSync('unicorns', {stdin: {transform: asyncGenerator}}));860+execa('unicorns', {stdin: {transform: unknownGenerator, final: unknownFinal}});861+expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator, final: unknownFinal}}));862+execa('unicorns', {stdin: [{transform: unknownGenerator, final: unknownFinal}]});863+expectError(execaSync('unicorns', {stdin: [{transform: unknownGenerator, final: unknownFinal}]}));864+expectError(execa('unicorns', {stdin: {transform: unknownGenerator, final: invalidReturnFinal}}));865+expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator, final: invalidReturnFinal}}));866+execa('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}});867+expectError(execaSync('unicorns', {stdin: {transform: unknownGenerator, final: asyncFinal}}));868 expectError(execa('unicorns', {stdin: {}}));869 expectError(execa('unicorns', {stdin: {binary: true}}));870 expectError(execa('unicorns', {stdin: {objectMode: true}}));871+expectError(execa('unicorns', {stdin: {final: unknownFinal}}));853-execa('unicorns', {stdin: {transform: unknownArrayGenerator, binary: true}});854-expectError(execa('unicorns', {stdin: {transform: unknownArrayGenerator, binary: 'true'}}));855-execa('unicorns', {stdin: {transform: unknownArrayGenerator, objectMode: true}});856-expectError(execa('unicorns', {stdin: {transform: unknownArrayGenerator, objectMode: 'true'}}));872+execa('unicorns', {stdin: {transform: unknownGenerator, binary: true}});873+expectError(execa('unicorns', {stdin: {transform: unknownGenerator, binary: 'true'}}));874+execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: true}});875+expectError(execa('unicorns', {stdin: {transform: unknownGenerator, objectMode: 'true'}}));876 execa('unicorns', {stdin: undefined});877 execaSync('unicorns', {stdin: undefined});878 execa('unicorns', {stdin: [undefined]});879 execaSync('unicorns', {stdin: [undefined]});50 unmodified lines930 execa('unicorns', {stdout: 1});931 execaSync('unicorns', {stdout: 1});932 execa('unicorns', {stdout: [1]});933 execaSync('unicorns', {stdout: [1]});915-execa('unicorns', {stdout: unknownArrayGenerator});916-expectError(execaSync('unicorns', {stdout: unknownArrayGenerator}));917-execa('unicorns', {stdout: [unknownArrayGenerator]});918-expectError(execaSync('unicorns', {stdout: [unknownArrayGenerator]}));934+execa('unicorns', {stdout: unknownGenerator});935+expectError(execaSync('unicorns', {stdout: unknownGenerator}));936+execa('unicorns', {stdout: [unknownGenerator]});937+expectError(execaSync('unicorns', {stdout: [unknownGenerator]}));938 expectError(execa('unicorns', {stdout: booleanGenerator}));920-expectError(execa('unicorns', {stdout: arrayGenerator}));939+expectError(execaSync('unicorns', {stdout: booleanGenerator}));940+expectError(execa('unicorns', {stdout: stringGenerator}));941+expectError(execaSync('unicorns', {stdout: stringGenerator}));942 expectError(execa('unicorns', {stdout: invalidReturnGenerator}));922-expectError(execa('unicorns', {stdout: syncGenerator}));923-execa('unicorns', {stdout: {transform: unknownArrayGenerator}});924-expectError(execaSync('unicorns', {stdout: {transform: unknownArrayGenerator}}));925-execa('unicorns', {stdout: [{transform: unknownArrayGenerator}]});926-expectError(execaSync('unicorns', {stdout: [{transform: unknownArrayGenerator}]}));943+expectError(execaSync('unicorns', {stdout: invalidReturnGenerator}));944+execa('unicorns', {stdout: asyncGenerator});945+expectError(execaSync('unicorns', {stdout: asyncGenerator}));946+execa('unicorns', {stdout: {transform: unknownGenerator}});947+expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator}}));948+execa('unicorns', {stdout: [{transform: unknownGenerator}]});949+expectError(execaSync('unicorns', {stdout: [{transform: unknownGenerator}]}));950 expectError(execa('unicorns', {stdout: {transform: booleanGenerator}}));928-expectError(execa('unicorns', {stdout: {transform: arrayGenerator}}));951+expectError(execaSync('unicorns', {stdout: {transform: booleanGenerator}}));952+expectError(execa('unicorns', {stdout: {transform: stringGenerator}}));953+expectError(execaSync('unicorns', {stdout: {transform: stringGenerator}}));954 expectError(execa('unicorns', {stdout: {transform: invalidReturnGenerator}}));930-expectError(execa('unicorns', {stdout: {transform: syncGenerator}}));955+expectError(execaSync('unicorns', {stdout: {transform: invalidReturnGenerator}}));956+execa('unicorns', {stdout: {transform: asyncGenerator}});957+expectError(execaSync('unicorns', {stdout: {transform: asyncGenerator}}));958+execa('unicorns', {stdout: {transform: unknownGenerator, final: unknownFinal}});959+expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator, final: unknownFinal}}));960+execa('unicorns', {stdout: [{transform: unknownGenerator, final: unknownFinal}]});961+expectError(execaSync('unicorns', {stdout: [{transform: unknownGenerator, final: unknownFinal}]}));962+expectError(execa('unicorns', {stdout: {transform: unknownGenerator, final: invalidReturnFinal}}));963+expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator, final: invalidReturnFinal}}));964+execa('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}});965+expectError(execaSync('unicorns', {stdout: {transform: unknownGenerator, final: asyncFinal}}));966 expectError(execa('unicorns', {stdout: {}}));967 expectError(execa('unicorns', {stdout: {binary: true}}));968 expectError(execa('unicorns', {stdout: {objectMode: true}}));969+expectError(execa('unicorns', {stdout: {final: unknownFinal}}));934-execa('unicorns', {stdout: {transform: unknownArrayGenerator, binary: true}});935-expectError(execa('unicorns', {stdout: {transform: unknownArrayGenerator, binary: 'true'}}));936-execa('unicorns', {stdout: {transform: unknownArrayGenerator, objectMode: true}});937-expectError(execa('unicorns', {stdout: {transform: unknownArrayGenerator, objectMode: 'true'}}));970+execa('unicorns', {stdout: {transform: unknownGenerator, binary: true}});971+expectError(execa('unicorns', {stdout: {transform: unknownGenerator, binary: 'true'}}));972+execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: true}});973+expectError(execa('unicorns', {stdout: {transform: unknownGenerator, objectMode: 'true'}}));974 execa('unicorns', {stdout: undefined});975 execaSync('unicorns', {stdout: undefined});976 execa('unicorns', {stdout: [undefined]});977 execaSync('unicorns', {stdout: [undefined]});50 unmodified lines1028 execa('unicorns', {stderr: 1});1029 execaSync('unicorns', {stderr: 1});1030 execa('unicorns', {stderr: [1]});1031 execaSync('unicorns', {stderr: [1]});996-execa('unicorns', {stderr: unknownArrayGenerator});997-expectError(execaSync('unicorns', {stderr: unknownArrayGenerator}));998-execa('unicorns', {stderr: [unknownArrayGenerator]});999-expectError(execaSync('unicorns', {stderr: [unknownArrayGenerator]}));1032+execa('unicorns', {stderr: unknownGenerator});1033+expectError(execaSync('unicorns', {stderr: unknownGenerator}));1034+execa('unicorns', {stderr: [unknownGenerator]});1035+expectError(execaSync('unicorns', {stderr: [unknownGenerator]}));1036 expectError(execa('unicorns', {stderr: booleanGenerator}));1001-expectError(execa('unicorns', {stderr: arrayGenerator}));1037+expectError(execaSync('unicorns', {stderr: booleanGenerator}));1038+expectError(execa('unicorns', {stderr: stringGenerator}));1039+expectError(execaSync('unicorns', {stderr: stringGenerator}));1040 expectError(execa('unicorns', {stderr: invalidReturnGenerator}));1003-expectError(execa('unicorns', {stderr: syncGenerator}));1004-execa('unicorns', {stderr: {transform: unknownArrayGenerator}});1005-expectError(execaSync('unicorns', {stderr: {transform: unknownArrayGenerator}}));1006-execa('unicorns', {stderr: [{transform: unknownArrayGenerator}]});1007-expectError(execaSync('unicorns', {stderr: [{transform: unknownArrayGenerator}]}));1041+expectError(execaSync('unicorns', {stderr: invalidReturnGenerator}));1042+execa('unicorns', {stderr: asyncGenerator});1043+expectError(execaSync('unicorns', {stderr: asyncGenerator}));1044+execa('unicorns', {stderr: {transform: unknownGenerator}});1045+expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator}}));1046+execa('unicorns', {stderr: [{transform: unknownGenerator}]});1047+expectError(execaSync('unicorns', {stderr: [{transform: unknownGenerator}]}));1048 expectError(execa('unicorns', {stderr: {transform: booleanGenerator}}));1009-expectError(execa('unicorns', {stderr: {transform: arrayGenerator}}));1049+expectError(execaSync('unicorns', {stderr: {transform: booleanGenerator}}));1050+expectError(execa('unicorns', {stderr: {transform: stringGenerator}}));1051+expectError(execaSync('unicorns', {stderr: {transform: stringGenerator}}));1052 expectError(execa('unicorns', {stderr: {transform: invalidReturnGenerator}}));1011-expectError(execa('unicorns', {stderr: {transform: syncGenerator}}));1053+expectError(execaSync('unicorns', {stderr: {transform: invalidReturnGenerator}}));1054+execa('unicorns', {stderr: {transform: asyncGenerator}});1055+expectError(execaSync('unicorns', {stderr: {transform: asyncGenerator}}));1056+execa('unicorns', {stderr: {transform: unknownGenerator, final: unknownFinal}});1057+expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator, final: unknownFinal}}));1058+execa('unicorns', {stderr: [{transform: unknownGenerator, final: unknownFinal}]});1059+expectError(execaSync('unicorns', {stderr: [{transform: unknownGenerator, final: unknownFinal}]}));1060+expectError(execa('unicorns', {stderr: {transform: unknownGenerator, final: invalidReturnFinal}}));1061+expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator, final: invalidReturnFinal}}));1062+execa('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}});1063+expectError(execaSync('unicorns', {stderr: {transform: unknownGenerator, final: asyncFinal}}));1064 expectError(execa('unicorns', {stderr: {}}));1065 expectError(execa('unicorns', {stderr: {binary: true}}));1066 expectError(execa('unicorns', {stderr: {objectMode: true}}));1067+expectError(execa('unicorns', {stderr: {final: unknownFinal}}));1015-execa('unicorns', {stderr: {transform: unknownArrayGenerator, binary: true}});1016-expectError(execa('unicorns', {stderr: {transform: unknownArrayGenerator, binary: 'true'}}));1017-execa('unicorns', {stderr: {transform: unknownArrayGenerator, objectMode: true}});1018-expectError(execa('unicorns', {stderr: {transform: unknownArrayGenerator, objectMode: 'true'}}));1068+execa('unicorns', {stderr: {transform: unknownGenerator, binary: true}});1069+expectError(execa('unicorns', {stderr: {transform: unknownGenerator, binary: 'true'}}));1070+execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: true}});1071+expectError(execa('unicorns', {stderr: {transform: unknownGenerator, objectMode: 'true'}}));1072 execa('unicorns', {stderr: undefined});1073 execaSync('unicorns', {stderr: undefined});1074 execa('unicorns', {stderr: [undefined]});1075 execaSync('unicorns', {stderr: [undefined]});28 unmodified lines1104 expectError(execa('unicorns', {stdio: 'ipc'}));1105 expectError(execaSync('unicorns', {stdio: 'ipc'}));1106 expectError(execa('unicorns', {stdio: 1}));1107 expectError(execaSync('unicorns', {stdio: 1}));1055-expectError(execa('unicorns', {stdio: unknownArrayGenerator}));1056-expectError(execaSync('unicorns', {stdio: unknownArrayGenerator}));1057-expectError(execa('unicorns', {stdio: {transform: unknownArrayGenerator}}));1058-expectError(execaSync('unicorns', {stdio: {transform: unknownArrayGenerator}}));1108+expectError(execa('unicorns', {stdio: unknownGenerator}));1109+expectError(execaSync('unicorns', {stdio: unknownGenerator}));1110+expectError(execa('unicorns', {stdio: {transform: unknownGenerator}}));1111+expectError(execaSync('unicorns', {stdio: {transform: unknownGenerator}}));1112 expectError(execa('unicorns', {stdio: fileUrl}));1113 expectError(execaSync('unicorns', {stdio: fileUrl}));1114 expectError(execa('unicorns', {stdio: {file: './test'}}));1115 expectError(execaSync('unicorns', {stdio: {file: './test'}}));42 unmodified lines1158 'ignore',1159 'inherit',1160 process.stdin,1161 1,1109- unknownArrayGenerator,1110- {transform: unknownArrayGenerator},1111- {transform: unknownArrayGenerator, binary: true},1112- {transform: unknownArrayGenerator, objectMode: true},1162+ unknownGenerator,1163+ {transform: unknownGenerator},1164+ {transform: unknownGenerator, binary: true},1165+ {transform: unknownGenerator, objectMode: true},1166+ {transform: unknownGenerator, final: unknownFinal},1167 undefined,1168 fileUrl,1169 {file: './test'},1170 new Writable(),21 unmodified lines1192 new Readable(),1193 new Uint8Array(),1194 ],1195 });1142-expectError(execaSync('unicorns', {stdio: [unknownArrayGenerator]}));1143-expectError(execaSync('unicorns', {stdio: [{transform: unknownArrayGenerator}]}));1196+expectError(execaSync('unicorns', {stdio: [unknownGenerator]}));1197+expectError(execaSync('unicorns', {stdio: [{transform: unknownGenerator}]}));1198 expectError(execaSync('unicorns', {stdio: [new WritableStream()]}));1199 expectError(execaSync('unicorns', {stdio: [new ReadableStream()]}));1200 expectError(execaSync('unicorns', {stdio: [emptyStringGenerator()]}));1201 expectError(execaSync('unicorns', {stdio: [asyncStringGenerator()]}));6 unmodified lines1208 ['ignore'],1209 ['inherit'],1210 [process.stdin],1211 [1],1158- [unknownArrayGenerator],1159- [{transform: unknownArrayGenerator}],1160- [{transform: unknownArrayGenerator, binary: true}],1161- [{transform: unknownArrayGenerator, objectMode: true}],1212+ [unknownGenerator],1213+ [{transform: unknownGenerator}],1214+ [{transform: unknownGenerator, binary: true}],1215+ [{transform: unknownGenerator, objectMode: true}],1216+ [{transform: unknownGenerator, final: unknownFinal}],1217 [undefined],1218 [fileUrl],1219 [{file: './test'}],1220 [new Writable()],25 unmodified lines1246 [new Readable()],1247 [new Uint8Array()],1248 ],1249 });1195-expectError(execaSync('unicorns', {stdio: [[unknownArrayGenerator]]}));1196-expectError(execaSync('unicorns', {stdio: [[{transform: unknownArrayGenerator}]]}));1250+expectError(execaSync('unicorns', {stdio: [[unknownGenerator]]}));1251+expectError(execaSync('unicorns', {stdio: [[{transform: unknownGenerator}]]}));1252 expectError(execaSync('unicorns', {stdio: [[new WritableStream()]]}));1253 expectError(execaSync('unicorns', {stdio: [[new ReadableStream()]]}));1254 expectError(execaSync('unicorns', {stdio: [[['foo', 'bar']]]}));1255 expectError(execaSync('unicorns', {stdio: [[[new Uint8Array(), new Uint8Array()]]]}));149 unmodified linesmodified:readme.md+7 added-7 removed
556 unmodified lines557 See also the [`input`](#input) and [`stdin`](#stdin) options.558 559 #### stdin560 561-Type: `string | number | stream.Readable | ReadableStream | URL | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string> | AsyncIterable<Uint8Array> | AsyncIterable<unknown> | AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)\561+Type: `string | number | stream.Readable | ReadableStream | URL | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string> | AsyncIterable<Uint8Array> | AsyncIterable<unknown> | GeneratorFunction<string> | GeneratorFunction<Uint8Array> | GeneratorFunction<unknown>| AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)\562 Default: `inherit` with [`$`](#command), `pipe` otherwise563 564 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard input. This can be:565 - `'pipe'`: Sets [`childProcess.stdin`](https://nodejs.org/api/child_process.html#subprocessstdin) stream.10 unmodified lines576 - an `Uint8Array`.577 578 This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.579 580-This can also be an async generator function to transform the input. [Learn more.](docs/transform.md)580+This can also be a generator function to transform the input. [Learn more.](docs/transform.md)581 582 #### stdout583 584-Type: `string | number | stream.Writable | WritableStream | URL | AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)\584+Type: `string | number | stream.Writable | WritableStream | URL | GeneratorFunction<string> | GeneratorFunction<Uint8Array> | GeneratorFunction<unknown>| AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)\585 Default: `pipe`586 587 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard output. This can be:588 - `'pipe'`: Sets [`childProcessResult.stdout`](#stdout) (as a string or `Uint8Array`) and [`childProcess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout) (as a stream).8 unmodified lines597 - a web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).598 599 This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.600 601-This can also be an async generator function to transform the output. [Learn more.](docs/transform.md)601+This can also be a generator function to transform the output. [Learn more.](docs/transform.md)602 603 #### stderr604 605-Type: `string | number | stream.Writable | WritableStream | URL | AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)`\605+Type: `string | number | stream.Writable | WritableStream | URL | GeneratorFunction<string> | GeneratorFunction<Uint8Array> | GeneratorFunction<unknown>| AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>` (or a tuple of those types)\606 Default: `pipe`607 608 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard error. This can be:609 - `'pipe'`: Sets [`childProcessResult.stderr`](#stderr) (as a string or `Uint8Array`) and [`childProcess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr) (as a stream).8 unmodified lines618 - a web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).619 620 This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.621 622-This can also be an async generator function to transform the output. [Learn more.](docs/transform.md)622+This can also be a generator function to transform the output. [Learn more.](docs/transform.md)623 624 #### stdio625 626-Type: `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | URL | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string> | AsyncIterable<Uint8Array> | AsyncIterable<unknown> | AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>>` (or a tuple of those types)\626+Type: `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | URL | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string> | AsyncIterable<Uint8Array> | AsyncIterable<unknown> | GeneratorFunction<string> | GeneratorFunction<Uint8Array> | GeneratorFunction<unknown>| AsyncGeneratorFunction<string> | AsyncGeneratorFunction<Uint8Array> | AsyncGeneratorFunction<unknown>>` (or a tuple of those types)\627 Default: `pipe`628 629 Like the [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.630 326 unmodified lines