Fix `lines` option (#957)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 8ad9ee7 · Browse files · 20 files·+346 added−246 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
20 files+346 added−246 removed
- tests:+191 added−95 removed
- other:+155 added−151 removed
Provenance
- Commit
15ab4e6- Tree
985eaf5- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8ad9ee7
20 files changed+346 added−246 removed
modified:lib/convert/iterable.js+2 added-2 removed
12234567891011111213141510 unmodified linesimport {getReadable} from '../pipe/validate.js';import {iterateOnStdout} from './loop.js';import {iterateOnSubprocessStream} from './loop.js';
export const createIterable = (subprocess, useBinaryEncoding, { from, binary: binaryOption = false, preserveNewlines = false,} = {}) => { const binary = binaryOption || useBinaryEncoding; const subprocessStdout = getReadable(subprocess, from); const onStdoutData = iterateOnStdout({subprocessStdout, subprocess, binary, preserveNewlines, isStream: false}); const onStdoutData = iterateOnSubprocessStream({subprocessStdout, subprocess, binary, shouldEncode: true, preserveNewlines}); return iterateOnStdoutData(onStdoutData, subprocessStdout, subprocess);};
const iterateOnStdoutData = async function * (onStdoutData, subprocessStdout, subprocess) {10 unmodified linesmodified:lib/convert/loop.js+56 added-39 removed
123455678891011121314910111213141516171617181920212 unmodified lines242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071373839404142437273747576776 unmodified lines848586875859606162636465666768697071727374888990919293949596979899787980818210010110210315 unmodified linesimport {on} from 'node:events';import {getEncodingTransformGenerator} from '../stdio/encoding-transform.js';import {getSplitLinesGenerator} from '../stdio/split.js';
export const iterateOnStdout = ({subprocessStdout, subprocess, binary, preserveNewlines, isStream}) => {// Iterate over lines of `subprocess.stdout`, used by `subprocess.readable|duplex|iterable()`export const iterateOnSubprocessStream = ({subprocessStdout, subprocess, binary, shouldEncode, preserveNewlines}) => { const controller = new AbortController(); stopReadingOnExit(subprocess, controller); const onStdoutChunk = on(subprocessStdout, 'data', { signal: controller.signal, highWaterMark: HIGH_WATER_MARK, // Backward compatibility with older name for this option // See https://github.com/nodejs/node/pull/52080#discussion_r1525227861 // @todo Remove after removing support for Node 21 highWatermark: HIGH_WATER_MARK, return iterateOnStream({ stream: subprocessStdout, controller, writableObjectMode: subprocessStdout.readableObjectMode, binary, shouldEncode, shouldSplit: true, preserveNewlines, }); const onStdoutData = iterateOnData({subprocessStdout, onStdoutChunk, controller, binary, preserveNewlines, isStream}); return onStdoutData;};
const stopReadingOnExit = async (subprocess, controller) => { try {2 unmodified lines controller.abort(); }};
// Iterate over lines of `subprocess.stdout`, used by `result.stdout` + `lines: true` optionexport const iterateForResult = ({stream, onStreamEnd, lines, encoding, stripFinalNewline}) => { const controller = new AbortController(); stopReadingOnStreamEnd(controller, onStreamEnd); return iterateOnStream({ stream, controller, writableObjectMode: false, binary: encoding === 'buffer', shouldEncode: true, shouldSplit: lines, preserveNewlines: !stripFinalNewline, });};
const stopReadingOnStreamEnd = async (controller, onStreamEnd) => { try { await onStreamEnd; } catch {} finally { controller.abort(); }};
const iterateOnStream = ({stream, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) => { const onStdoutChunk = on(stream, 'data', { signal: controller.signal, highWaterMark: HIGH_WATER_MARK, // Backward compatibility with older name for this option // See https://github.com/nodejs/node/pull/52080#discussion_r1525227861 // @todo Remove after removing support for Node 21 highWatermark: HIGH_WATER_MARK, }); return iterateOnData({onStdoutChunk, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines});};
// @todo: replace with `getDefaultHighWaterMark(true)` after dropping support for Node <18.17.0export const DEFAULT_OBJECT_HIGH_WATER_MARK = 16;
// The `highWaterMark` of `events.on()` is measured in number of events, not in bytes.// Not knowing the average amount of bytes per `data` event, we use the same heuristic as streams in objectMode, since they have the same issue.// Therefore, we use the value of `getDefaultHighWaterMark(true)`.// Note: this option does not exist on Node 18, but this is ok since the logic works without it. It just consumes more memory.const HIGH_WATER_MARK = DEFAULT_OBJECT_HIGH_WATER_MARK;
const iterateOnData = async function * ({subprocessStdout, onStdoutChunk, controller, binary, preserveNewlines, isStream}) { const { encodeChunk = identityGenerator, encodeChunkFinal = noopGenerator, splitLines = identityGenerator, splitLinesFinal = noopGenerator, } = getTransforms({subprocessStdout, binary, preserveNewlines, isStream});const iterateOnData = async function * ({onStdoutChunk, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) { const {encodeChunk, encodeChunkFinal, splitLines, splitLinesFinal} = getTransforms({writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines});
try { for await (const [chunk] of onStdoutChunk) { yield * handleChunk(encodeChunk, splitLines, chunk);6 unmodified lines yield * handleFinalChunks(encodeChunkFinal, splitLines, splitLinesFinal); }};
const getTransforms = ({subprocessStdout, binary, preserveNewlines, isStream}) => { if (subprocessStdout.readableObjectMode) { return {}; }
const writableObjectMode = false;
if (!binary) { return getTextTransforms(binary, preserveNewlines, writableObjectMode); }
return isStream ? {} : getBinaryTransforms(binary, writableObjectMode);};
const getTextTransforms = (binary, preserveNewlines, writableObjectMode) => { const {transform: encodeChunk, final: encodeChunkFinal} = getEncodingTransformGenerator(binary, writableObjectMode, false); const {transform: splitLines, final: splitLinesFinal} = getSplitLinesGenerator({binary, preserveNewlines, writableObjectMode, state: {}});const getTransforms = ({writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) => { const { transform: encodeChunk = identityGenerator, final: encodeChunkFinal = noopGenerator, } = getEncodingTransformGenerator(binary, writableObjectMode || !shouldEncode) ?? {}; const { transform: splitLines = identityGenerator, final: splitLinesFinal = noopGenerator, } = getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode || !shouldSplit, {}) ?? {}; return {encodeChunk, encodeChunkFinal, splitLines, splitLinesFinal};};
const getBinaryTransforms = (binary, writableObjectMode) => { const {transform: encodeChunk} = getEncodingTransformGenerator(binary, writableObjectMode, false); return {encodeChunk};};
const identityGenerator = function * (chunk) { yield chunk;};
15 unmodified linesmodified:lib/convert/readable.js+2 added-2 removed
7 unmodified lines8 waitForSubprocessStdout,9 waitForSubprocess,10 destroyOtherStream,11 } from './shared.js';12-import {iterateOnStdout, DEFAULT_OBJECT_HIGH_WATER_MARK} from './loop.js';12+import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from './loop.js';13 14 // Create a `Readable` stream that forwards from `stdout` and awaits the subprocess15 export const createReadable = ({subprocess, concurrentStreams, useBinaryEncoding}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => {16 const binary = binaryOption || useBinaryEncoding;23 unmodified lines40 : {readableEncoding, readableObjectMode: true, readableHighWaterMark: DEFAULT_OBJECT_HIGH_WATER_MARK};41 42 export const getReadableMethods = ({subprocessStdout, subprocess, binary, preserveNewlines}) => {43 const onStdoutDataDone = createDeferred();44- const onStdoutData = iterateOnStdout({subprocessStdout, subprocess, binary, preserveNewlines, isStream: true});44+ const onStdoutData = iterateOnSubprocessStream({subprocessStdout, subprocess, binary, shouldEncode: !binary, preserveNewlines});45 46 return {47 read() {48 onRead(this, onStdoutData, onStdoutDataDone);46 unmodified linesmodified:lib/stdio/encoding-transform.js+2 added-2 removed
11 unmodified lines12131415161716171819202132 unmodified lines11 unmodified linesHowever, those are converted to Buffer:- on writes: `Duplex.writable` `decodeStrings: true` default option- on reads: `Duplex.readable` `readableEncoding: null` default option*/export const getEncodingTransformGenerator = (binary, writableObjectMode, forceEncoding) => { if (writableObjectMode && !forceEncoding) {export const getEncodingTransformGenerator = (binary, writableObjectMode) => { if (writableObjectMode) { return; }
if (binary) {32 unmodified linesmodified:lib/stdio/generator.js+7 added-9 removed
140 unmodified lines141 */142 export const generatorToDuplexStream = ({143 value,144 value: {transform, final, writableObjectMode, readableObjectMode},145- forceEncoding,145 optionName,146 }) => {148- const generators = addInternalGenerators({value, forceEncoding, optionName});147+ const generators = addInternalGenerators(value, optionName);148 const transformAsync = isAsyncGenerator(transform);149 const finalAsync = isAsyncGenerator(final);150 const stream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});151 return {stream};2 unmodified lines154 export const getGenerators = stdioItems => stdioItems.filter(({type}) => type === 'generator');155 156 export const runGeneratorsSync = (chunks, generators) => {157 for (const {value, optionName} of generators) {159- const generators = addInternalGenerators({value, forceEncoding: false, optionName});158+ const generators = addInternalGenerators(value, optionName);159 chunks = runTransformSync(generators, chunks);160 }161 162 return chunks;163 };164 166-const addInternalGenerators = ({167- value: {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},168- forceEncoding,165+const addInternalGenerators = (166+ {transform, final, binary, writableObjectMode, readableObjectMode, preserveNewlines},167 optionName,170-}) => {168+) => {169 const state = {};170 return [171 {transform: getValidateTransformInput(writableObjectMode, optionName)},174- getEncodingTransformGenerator(binary, writableObjectMode, forceEncoding),175- getSplitLinesGenerator({binary, preserveNewlines, writableObjectMode, state}),172+ getEncodingTransformGenerator(binary, writableObjectMode),173+ getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode, state),174 {transform, final},175 {transform: getValidateTransformReturn(readableObjectMode, optionName)},176 getAppendNewlineGenerator({binary, preserveNewlines, readableObjectMode, state}),177 ].filter(Boolean);19 unmodified linesmodified:lib/stdio/handle.js+5 added-8 removed
2 unmodified lines3 import {getStreamDirection} from './direction.js';4 import {normalizeStdio} from './option.js';5 import {handleNativeStream} from './native.js';6 import {handleInputOptions} from './input.js';7-import {handleStreamsLines} from './lines.js';7 import {handleStreamsEncoding} from './encoding-final.js';8 import {normalizeTransforms, getObjectMode} from './generator.js';9 import {forwardStdio, willPipeFileDescriptor} from './forward.js';10 10 unmodified lines21 // We make sure passing an array with a single item behaves the same as passing that item without an array.22 // This is what users would expect.23 // For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.24 const getFileDescriptor = ({stdioOption, fdNumber, addProperties, options, isSync, stdioState, verboseInfo}) => {26- const outputLines = [];25 const optionName = getOptionName(fdNumber);26 const {stdioItems: initialStdioItems, isStdioArray} = initializeStdioItems({stdioOption, fdNumber, options, optionName});27 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);28 const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({stdioItem, isStdioArray, fdNumber, direction, isSync}));29 const objectMode = getObjectMode(stdioItems, optionName, direction, options);30 validateFileObjectMode(stdioItems, objectMode);33- const normalizedStdioItems = normalizeStdioItems({stdioItems, fdNumber, optionName, addProperties, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode});34- return {direction, objectMode, outputLines, stdioItems: normalizedStdioItems};31+ const normalizedStdioItems = normalizeStdioItems({stdioItems, fdNumber, optionName, addProperties, options, direction, stdioState, verboseInfo, objectMode});32+ return {direction, objectMode, stdioItems: normalizedStdioItems};33 };34 35 const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;36 const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];60 unmodified lines97 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);98 }99 };100 103-const normalizeStdioItems = ({stdioItems, fdNumber, optionName, addProperties, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode}) => {104- const allStdioItems = addInternalStdioItems({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode});101+const normalizeStdioItems = ({stdioItems, fdNumber, optionName, addProperties, options, direction, stdioState, verboseInfo, objectMode}) => {102+ const allStdioItems = addInternalStdioItems({stdioItems, fdNumber, optionName, options, direction, stdioState, verboseInfo, objectMode});103 const normalizedStdioItems = normalizeTransforms(allStdioItems, optionName, direction, options);104 return normalizedStdioItems.map(stdioItem => addStreamProperties(stdioItem, addProperties, direction));105 };106 109-const addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode}) => willPipeFileDescriptor(stdioItems)107+const addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, direction, stdioState, verboseInfo, objectMode}) => willPipeFileDescriptor(stdioItems)108 ? [109 ...stdioItems,110 ...handleStreamsEncoding({options, direction, optionName, objectMode}),111 ...handleStreamsVerbose({stdioItems, options, stdioState, verboseInfo, fdNumber, optionName}),114- ...handleStreamsLines({options, isSync, direction, optionName, objectMode, outputLines}),112 ]113 : stdioItems;114 115 // Some `stdio` values require Execa to create streams.17 unmodified linesdeleted:lib/stdio/lines.js-29 removed
1-import {MaxBufferError} from 'get-stream';2-import stripFinalNewlineFunction from 'strip-final-newline';3-4-// Split chunks line-wise for streams exposed to users like `subprocess.stdout`.5-// Appending a noop transform in object mode is enough to do this, since every non-binary transform iterates line-wise.6-export const handleStreamsLines = ({options: {lines, stripFinalNewline, maxBuffer}, isSync, direction, optionName, objectMode, outputLines}) => shouldSplitLines(lines, direction, objectMode)7- ? [{8- type: 'generator',9- value: {transform: linesEndGenerator.bind(undefined, {outputLines, stripFinalNewline, maxBuffer, isSync}), preserveNewlines: true},10- optionName,11- }]12- : [];13-14-const shouldSplitLines = (lines, direction, objectMode) => direction === 'output'15- && lines16- && !objectMode;17-18-const linesEndGenerator = function * ({outputLines, stripFinalNewline, maxBuffer, isSync}, line) {19- if (!isSync && outputLines.length >= maxBuffer) {20- const error = new MaxBufferError();21- error.bufferedData = outputLines;22- throw error;23- }24-25- const strippedLine = stripFinalNewline ? stripFinalNewlineFunction(line) : line;26- outputLines.push(strippedLine);27-28- yield line;29-};modified:lib/stdio/output-sync.js+16 added-7 removed
1 import {writeFileSync} from 'node:fs';2 import {joinToString, joinToUint8Array, bufferToUint8Array, isUint8Array, concatUint8Arrays} from './uint-array.js';3 import {getGenerators, runGeneratorsSync} from './generator.js';4+import {splitLinesSync} from './split.js';5 import {FILE_TYPES} from './type.js';6 7 // Apply `stdout`/`stderr` options, after spawning, in sync mode8 export const transformOutputSync = (fileDescriptors, {output}, options) => {6 unmodified lines15 transformOutputResultSync({result, fileDescriptors, fdNumber, state}, options));16 return {output: transformedOutput, ...state};17 };18 18-const transformOutputResultSync = ({result, fileDescriptors, fdNumber, state}, {buffer, encoding, lines}) => {19+const transformOutputResultSync = ({result, fileDescriptors, fdNumber, state}, {buffer, encoding, lines, stripFinalNewline}) => {20 if (result === null) {21 return;22 }23 23- const {stdioItems, outputLines, objectMode} = fileDescriptors[fdNumber];24+ const {stdioItems, objectMode} = fileDescriptors[fdNumber];25 const uint8ArrayResult = bufferToUint8Array(result);26 const generators = getGenerators(stdioItems);27 const chunks = runOutputGeneratorsSync([uint8ArrayResult], generators, state);27- const {serializedResult, finalResult} = serializeChunks({chunks, objectMode, outputLines, encoding, lines});28+ const {serializedResult, finalResult} = serializeChunks({chunks, objectMode, encoding, lines, stripFinalNewline});29 const returnedResult = buffer ? finalResult : undefined;30 31 try {32 if (state.error === undefined) {15 unmodified lines48 return chunks;49 }50 };51 51-const serializeChunks = ({chunks, objectMode, outputLines, encoding, lines}) => {52+const serializeChunks = ({chunks, objectMode, encoding, lines, stripFinalNewline}) => {53 if (objectMode) {54 return {finalResult: chunks};55 }56 56- const serializedResult = encoding === 'buffer' ? joinToUint8Array(chunks) : joinToString(chunks, true);57- const finalResult = lines ? outputLines : serializedResult;58- return {serializedResult, finalResult};57+ if (encoding === 'buffer') {58+ const serializedResult = joinToUint8Array(chunks);59+ return {serializedResult, finalResult: serializedResult};60+ }61+62+ const serializedResult = joinToString(chunks);63+ if (!lines) {64+ return {serializedResult, finalResult: serializedResult};65+ }66+67+ return {serializedResult, finalResult: splitLinesSync(serializedResult, !stripFinalNewline)};68 };69 70 const writeToFiles = (serializedResult, stdioItems) => {71 for (const {type, path} of stdioItems) {33 unmodified linesmodified:lib/stdio/split.js+14 added-4 removed
1 // Split chunks line-wise for generators passed to the `std*` options2-export const getSplitLinesGenerator = ({binary, preserveNewlines, writableObjectMode, state}) => {3- if (binary || writableObjectMode) {4- return;5- }2+export const getSplitLinesGenerator = (binary, preserveNewlines, writableObjectMode, state) => binary || writableObjectMode3+ ? undefined4+ : initializeSplitLines(preserveNewlines, state);5 6+export const splitLinesSync = (string, preserveNewlines) => {7+ const {transform, final} = initializeSplitLines(preserveNewlines, {});8+ return [...transform(string), ...final()];9+};10+11+const initializeSplitLines = (preserveNewlines, state) => {12 state.previousChunks = '';13 return {14 transform: splitGenerator.bind(undefined, state, preserveNewlines),15 final: linesFinal.bind(undefined, state),1 unmodified line17 };18 19 // This imperative logic is much faster than using `String.split()` and uses very low memory.20 const splitGenerator = function * (state, preserveNewlines, chunk) {21+ if (typeof chunk !== 'string') {22+ yield chunk;23+ return;24+ }25+26 let {previousChunks} = state;27 let start = -1;28 29 for (let end = 0; end < chunk.length; end += 1) {76 unmodified linesmodified:lib/stdio/uint-array.js+9 added-5 removed
1 import {Buffer} from 'node:buffer';2 3-export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);3+const {toString: objectToString} = Object.prototype;4 5+export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';6+7+export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);8+9 export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);10 11 const textEncoder = new TextEncoder();12 const stringToUint8Array = string => textEncoder.encode(string);13 14 const textDecoder = new TextDecoder();15 export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);16 13-export const joinToString = (uint8ArraysOrStrings, areRelated) => {17+export const joinToString = uint8ArraysOrStrings => {18 if (uint8ArraysOrStrings.length === 1 && typeof uint8ArraysOrStrings[0] === 'string') {19 return uint8ArraysOrStrings[0];20 }21 18- const strings = uint8ArraysToStrings(uint8ArraysOrStrings, areRelated);22+ const strings = uint8ArraysToStrings(uint8ArraysOrStrings);23 return strings.join('');24 };25 22-const uint8ArraysToStrings = (uint8ArraysOrStrings, areRelated) => {26+const uint8ArraysToStrings = uint8ArraysOrStrings => {27 const decoder = new TextDecoder();28 const strings = uint8ArraysOrStrings.map(uint8ArrayOrString => isUint8Array(uint8ArrayOrString)25- ? decoder.decode(uint8ArrayOrString, {stream: areRelated})29+ ? decoder.decode(uint8ArrayOrString, {stream: true})30 : uint8ArrayOrString);31 const finalString = decoder.decode();32 return finalString === '' ? strings : [...strings, finalString];33 };33 unmodified linesmodified:lib/stream/all.js+9 added-18 removed
1 import mergeStreams from '@sindresorhus/merge-streams';2-import {generatorToDuplexStream} from '../stdio/generator.js';2 import {waitForSubprocessStream} from './subprocess.js';3 4 // `all` interleaves `stdout` and `stderr`5 export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)6 ? mergeStreams([stdout, stderr].filter(Boolean))7 : undefined;8 9 // Read the contents of `subprocess.all` and|or wait for its completion11-export const waitForAllStream = ({subprocess, encoding, buffer, maxBuffer, streamInfo}) => waitForSubprocessStream({12- stream: getAllStream(subprocess, encoding),10+export const waitForAllStream = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo}) => waitForSubprocessStream({11+ stream: subprocess.all,12 subprocess,13 fdNumber: 1,14 encoding,15 buffer,16 maxBuffer: maxBuffer * 2,17+ lines,18+ allMixed: getAllMixed(subprocess),19+ stripFinalNewline,20 streamInfo,21 });22 23 // When `subprocess.stdout` is in objectMode but not `subprocess.stderr` (or the opposite), we need to use both:24 // - `getStreamAsArray()` for the chunks in objectMode, to return as an array without changing each chunk25 // - `getStreamAsArrayBuffer()` or `getStream()` for the chunks not in objectMode, to convert them from Buffers to string or Uint8Array26 // We do this by emulating the Buffer -> string|Uint8Array conversion performed by `get-stream` with our own, which is identical.25-const getAllStream = ({all, stdout, stderr}, encoding) => all && stdout && stderr && stdout.readableObjectMode !== stderr.readableObjectMode26- ? all.pipe(generatorToDuplexStream(getAllStreamTransform(encoding)).stream)27- : all;28-29-const getAllStreamTransform = encoding => ({30- value: {31- * final() {},32- binary: encoding === 'buffer',33- writableObjectMode: true,34- readableObjectMode: true,35- preserveNewlines: true,36- },37- forceEncoding: true,38- optionName: 'all',39-});27+const getAllMixed = ({all, stdout, stderr}) => all28+ && stdout29+ && stderr30+ && stdout.readableObjectMode !== stderr.readableObjectMode;modified:lib/stream/resolve.js+7 added-7 removed
11 unmodified lines12 13 // Retrieve result of subprocess: exit code, signal, error, streams (stdout/stderr/all)14 export const getSubprocessResult = async ({15 subprocess,16- options: {encoding, buffer, maxBuffer, lines, timeoutDuration: timeout},16+ options: {encoding, buffer, maxBuffer, lines, timeoutDuration: timeout, stripFinalNewline},17 context,18 fileDescriptors,19 originalStreams,20 controller,21 }) => {22 const exitPromise = waitForExit(subprocess);23 const streamInfo = {originalStreams, fileDescriptors, subprocess, exitPromise, propagating: false};24 25- const stdioPromises = waitForSubprocessStreams({subprocess, encoding, buffer, maxBuffer, lines, streamInfo});26- const allPromise = waitForAllStream({subprocess, encoding, buffer, maxBuffer, streamInfo});25+ const stdioPromises = waitForSubprocessStreams({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo});26+ const allPromise = waitForAllStream({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo});27 const originalPromises = waitForOriginalStreams(originalStreams, subprocess, streamInfo);28 const customStreamsEndPromises = waitForCustomStreamsEnd(fileDescriptors, streamInfo);29 30 try {13 unmodified lines44 } catch (error) {45 return Promise.all([46 {error},47 exitPromise,48- Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),49- getBufferedData(allPromise, encoding),48+ Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise))),49+ getBufferedData(allPromise),50 Promise.allSettled(originalPromises),51 Promise.allSettled(customStreamsEndPromises),52 ]);53 }54 };55 56 // Read the contents of `subprocess.std*` and|or wait for its completion57-const waitForSubprocessStreams = ({subprocess, encoding, buffer, maxBuffer, lines, streamInfo}) =>58- subprocess.stdio.map((stream, fdNumber) => waitForSubprocessStream({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, streamInfo}));57+const waitForSubprocessStreams = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo}) =>58+ subprocess.stdio.map((stream, fdNumber) => waitForSubprocessStream({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, allMixed: false, stripFinalNewline, streamInfo}));59 60 // Transforms replace `subprocess.std*`, which means they are not exposed to users.61 // However, we still want to wait for their completion.62 const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) =>23 unmodified linesmodified:lib/stream/subprocess.js+26 added-19 removed
1 import {setImmediate} from 'node:timers/promises';2 import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';3+import {iterateForResult} from '../convert/loop.js';4+import {isArrayBuffer} from '../stdio/uint-array.js';5 import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';6 5-export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, streamInfo}) => {7+export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo}) => {8 if (!stream) {9 return;10 }11 10 unmodified lines22 return;23 }24 25 try {24- return await getAnyStream({stream, fdNumber, encoding, maxBuffer, lines, streamInfo});26+ return await getAnyStream({stream, fdNumber, encoding, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo});27 } catch (error) {28 if (error instanceof MaxBufferError) {29 subprocess.kill();30 }31 32 handleStreamError(error, fdNumber, streamInfo);31- return handleBufferedData(error, encoding);33+ return handleBufferedData(error);34 }35 };36 37 // When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away4 unmodified lines42 stream.resume();43 }44 };45 44-const getAnyStream = async ({stream, fdNumber, encoding, maxBuffer, lines, streamInfo}) => {46+const getAnyStream = async ({stream, fdNumber, encoding, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo}) => {47+ if (allMixed) {48+ return getStreamLines({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo});49+ }50+51 if (stream.readableObjectMode) {52 return getStreamAsArray(stream, {maxBuffer});53 }54 49- if (lines) {50- return getOutputLines(stream, fdNumber, streamInfo);55+ if (encoding === 'buffer') {56+ return new Uint8Array(await getStreamAsArrayBuffer(stream, {maxBuffer}));57 }58 53- const contents = encoding === 'buffer'54- ? await getStreamAsArrayBuffer(stream, {maxBuffer})55- : await getStream(stream, {maxBuffer});56- return applyEncoding(contents, encoding);59+ if (!lines) {60+ return getStream(stream, {maxBuffer});61+ }62+63+ return getStreamLines({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo});64 };65 59-const getOutputLines = async (stream, fdNumber, streamInfo) => {60- await waitForStream(stream, fdNumber, streamInfo);61- return streamInfo.fileDescriptors[fdNumber].outputLines;66+const getStreamLines = ({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo}) => {67+ const onStreamEnd = waitForStream(stream, fdNumber, streamInfo);68+ const iterable = iterateForResult({stream, onStreamEnd, lines, encoding, stripFinalNewline});69+ return getStreamAsArray(iterable, {maxBuffer});70 };71 72 // On failure, `result.stdout|stderr|all` should contain the currently buffered stream73 // They are automatically closed and flushed by Node.js when the subprocess exits74 // When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve67-export const getBufferedData = async (streamPromise, encoding) => {75+export const getBufferedData = async streamPromise => {76 try {77 return await streamPromise;78 } catch (error) {71- return handleBufferedData(error, encoding);79+ return handleBufferedData(error);80 }81 };82 75-const handleBufferedData = ({bufferedData}, encoding) => bufferedData === undefined || Array.isArray(bufferedData)76- ? bufferedData77- : applyEncoding(bufferedData, encoding);83+const handleBufferedData = ({bufferedData}) => isArrayBuffer(bufferedData)84+ ? new Uint8Array(bufferedData)85+ : bufferedData;86 79-const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;modified:test/fixtures/all-fail.js+2 added-2 removed
1 #!/usr/bin/env node2 import process from 'node:process';3 4-console.log('stdout');5-console.error('stderr');4+console.log('std\nout');5+console.error('std\nerr');6 process.exitCode = 1;modified:test/fixtures/all.js+2 added-2 removed
1 #!/usr/bin/env node2-console.log('stdout');3-console.error('stderr');2+console.log('std\nout');3+console.error('std\nerr');modified:test/helpers/convert.js+5 added
1 import {text} from 'node:stream/consumers';2 import {finished} from 'node:stream/promises';3+import getStream from 'get-stream';4 import isPlainObj from 'is-plain-obj';5 import {execa} from '../../index.js';6 import {foobarString} from '../helpers/input.js';7 27 unmodified lines35 export const assertStreamOutput = async (t, stream, expectedOutput = foobarString) => {36 t.is(await text(stream), expectedOutput);37 };38 39+export const assertStreamDataEvents = async (t, stream, expectedOutput = foobarString) => {40+ t.is(await getStream(stream), expectedOutput);41+};42+43 export const assertIterableChunks = async (t, asyncIterable, expectedChunks) => {44 t.deepEqual(await arrayFromAsync(asyncIterable), expectedChunks);45 };46 32 unmodified linesmodified:test/return/error.js+1 added-1 removed
69 unmodified lines70 });71 72 // eslint-disable-next-line max-params73 const testStdioMessage = async (t, encoding, all, objectMode, execaMethod) => {74- const {exitCode, message} = await execaMethod('echo-fail.js', {...getStdio(1, noopGenerator(objectMode), 4), encoding, all, reject: false});74+ const {exitCode, message} = await execaMethod('echo-fail.js', {...getStdio(1, noopGenerator(objectMode, false, true), 4), encoding, all, reject: false});75 t.is(exitCode, 1);76 const output = all ? 'stdout\nstderr' : 'stderr\n\nstdout';77 t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));78 };150 unmodified linesmodified:test/stdio/generator.js+163 added-82 removed
422 unmodified lines423 424 return encoding === 'utf8' ? uppercaseGenerator() : uppercaseBufferGenerator();425 };426 427-const getStdoutStderrOutput = (output, stdioOption, encoding, objectMode) => {427+const getStdoutStderrOutput = ({output, stdioOption, encoding, objectMode, lines}) => {428 if (objectMode && !stdioOption) {429- return [foobarObject];429+ return encoding === 'utf8' ? [foobarObject, foobarObject] : [foobarObject];430 }431 432 const stdioOutput = stdioOption ? output : output.toUpperCase();433 434 if (encoding === 'hex') {435 return Buffer.from(stdioOutput).toString('hex');436 }437 438- return encoding === 'buffer' ? textEncoder.encode(stdioOutput) : stdioOutput;438+ if (encoding === 'buffer') {439+ return textEncoder.encode(stdioOutput);440+ }441+442+ return lines ? stdioOutput.trim().split('\n').map(string => `${string}\n`) : stdioOutput;443 };444 441-const getAllOutput = (stdoutOutput, stderrOutput, encoding, objectMode) => {442- if (objectMode) {445+const getAllOutput = ({stdoutOutput, stderrOutput, encoding, objectMode, lines}) => {446+ if (objectMode || (lines && encoding === 'utf8')) {447 return [stdoutOutput, stderrOutput].flat();448 }449 450 return encoding === 'buffer'1 unmodified line452 : `${stdoutOutput}${stderrOutput}`;453 };454 455 // eslint-disable-next-line max-params452-const testGeneratorAll = async (t, reject, encoding, objectMode, stdoutOption, stderrOption, execaMethod) => {456+const testGeneratorAll = async (t, reject, encoding, objectMode, stdoutOption, stderrOption, lines, execaMethod) => {457 const fixtureName = reject ? 'all.js' : 'all-fail.js';458 const {stdout, stderr, all} = await execaMethod(fixtureName, {459 all: true,460 reject,461 stdout: getAllStdioOption(stdoutOption, encoding, objectMode),462 stderr: getAllStdioOption(stderrOption, encoding, objectMode),463 encoding,464+ lines,465 stripFinalNewline: false,466 });467 463- const stdoutOutput = getStdoutStderrOutput('stdout\n', stdoutOption, encoding, objectMode);468+ const stdoutOutput = getStdoutStderrOutput({output: 'std\nout\n', stdioOption: stdoutOption, encoding, objectMode, lines});469 t.deepEqual(stdout, stdoutOutput);465- const stderrOutput = getStdoutStderrOutput('stderr\n', stderrOption, encoding, objectMode);470+ const stderrOutput = getStdoutStderrOutput({output: 'std\nerr\n', stdioOption: stderrOption, encoding, objectMode, lines});471 t.deepEqual(stderr, stderrOutput);467- const allOutput = getAllOutput(stdoutOutput, stderrOutput, encoding, objectMode);468- t.deepEqual(all, allOutput);472+ const allOutput = getAllOutput({stdoutOutput, stderrOutput, encoding, objectMode, lines});473+ if (Array.isArray(all) && Array.isArray(allOutput)) {474+ t.deepEqual([...all].sort(), [...allOutput].sort());475+ } else {476+ t.deepEqual(all, allOutput);477+ }478 };479 471-test('Can use generators with result.all = transform + transform', testGeneratorAll, true, 'utf8', false, false, false, execa);472-test('Can use generators with error.all = transform + transform', testGeneratorAll, false, 'utf8', false, false, false, execa);473-test('Can use generators with result.all = transform + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, false, execa);474-test('Can use generators with error.all = transform + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, false, execa);475-test('Can use generators with result.all = transform + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, false, false, execa);476-test('Can use generators with error.all = transform + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, false, false, execa);477-test('Can use generators with result.all = transform + pipe', testGeneratorAll, true, 'utf8', false, false, true, execa);478-test('Can use generators with error.all = transform + pipe', testGeneratorAll, false, 'utf8', false, false, true, execa);479-test('Can use generators with result.all = transform + pipe, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, true, execa);480-test('Can use generators with error.all = transform + pipe, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, true, execa);481-test('Can use generators with result.all = transform + pipe, encoding "hex"', testGeneratorAll, true, 'hex', false, false, true, execa);482-test('Can use generators with error.all = transform + pipe, encoding "hex"', testGeneratorAll, false, 'hex', false, false, true, execa);483-test('Can use generators with result.all = pipe + transform', testGeneratorAll, true, 'utf8', false, true, false, execa);484-test('Can use generators with error.all = pipe + transform', testGeneratorAll, false, 'utf8', false, true, false, execa);485-test('Can use generators with result.all = pipe + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, true, false, execa);486-test('Can use generators with error.all = pipe + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, true, false, execa);487-test('Can use generators with result.all = pipe + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, true, false, execa);488-test('Can use generators with error.all = pipe + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, true, false, execa);489-test('Can use generators with result.all = transform + transform, objectMode', testGeneratorAll, true, 'utf8', true, false, false, execa);490-test('Can use generators with error.all = transform + transform, objectMode', testGeneratorAll, false, 'utf8', true, false, false, execa);491-test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, false, execa);492-test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, false, execa);493-test('Can use generators with result.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, false, execa);494-test('Can use generators with error.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, false, execa);495-test('Can use generators with result.all = transform + pipe, objectMode', testGeneratorAll, true, 'utf8', true, false, true, execa);496-test('Can use generators with error.all = transform + pipe, objectMode', testGeneratorAll, false, 'utf8', true, false, true, execa);497-test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, true, execa);498-test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, true, execa);499-test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, true, execa);500-test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, true, execa);501-test('Can use generators with result.all = pipe + transform, objectMode', testGeneratorAll, true, 'utf8', true, true, false, execa);502-test('Can use generators with error.all = pipe + transform, objectMode', testGeneratorAll, false, 'utf8', true, true, false, execa);503-test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false, execa);504-test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false, execa);505-test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, true, false, execa);506-test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, true, false, execa);507-test('Can use generators with result.all = transform + transform, sync', testGeneratorAll, true, 'utf8', false, false, false, execaSync);508-test('Can use generators with error.all = transform + transform, sync', testGeneratorAll, false, 'utf8', false, false, false, execaSync);509-test('Can use generators with result.all = transform + transform, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, false, false, execaSync);510-test('Can use generators with error.all = transform + transform, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, false, false, execaSync);511-test('Can use generators with result.all = transform + transform, encoding "hex", sync', testGeneratorAll, true, 'hex', false, false, false, execaSync);512-test('Can use generators with error.all = transform + transform, encoding "hex", sync', testGeneratorAll, false, 'hex', false, false, false, execaSync);513-test('Can use generators with result.all = transform + pipe, sync', testGeneratorAll, true, 'utf8', false, false, true, execaSync);514-test('Can use generators with error.all = transform + pipe, sync', testGeneratorAll, false, 'utf8', false, false, true, execaSync);515-test('Can use generators with result.all = transform + pipe, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, false, true, execaSync);516-test('Can use generators with error.all = transform + pipe, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, false, true, execaSync);517-test('Can use generators with result.all = transform + pipe, encoding "hex", sync', testGeneratorAll, true, 'hex', false, false, true, execaSync);518-test('Can use generators with error.all = transform + pipe, encoding "hex", sync', testGeneratorAll, false, 'hex', false, false, true, execaSync);519-test('Can use generators with result.all = pipe + transform, sync', testGeneratorAll, true, 'utf8', false, true, false, execaSync);520-test('Can use generators with error.all = pipe + transform, sync', testGeneratorAll, false, 'utf8', false, true, false, execaSync);521-test('Can use generators with result.all = pipe + transform, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, true, false, execaSync);522-test('Can use generators with error.all = pipe + transform, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, true, false, execaSync);523-test('Can use generators with result.all = pipe + transform, encoding "hex", sync', testGeneratorAll, true, 'hex', false, true, false, execaSync);524-test('Can use generators with error.all = pipe + transform, encoding "hex", sync', testGeneratorAll, false, 'hex', false, true, false, execaSync);525-test('Can use generators with result.all = transform + transform, objectMode, sync', testGeneratorAll, true, 'utf8', true, false, false, execaSync);526-test('Can use generators with error.all = transform + transform, objectMode, sync', testGeneratorAll, false, 'utf8', true, false, false, execaSync);527-test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, false, false, execaSync);528-test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, false, false, execaSync);529-test('Can use generators with result.all = transform + transform, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, false, false, execaSync);530-test('Can use generators with error.all = transform + transform, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, false, false, execaSync);531-test('Can use generators with result.all = transform + pipe, objectMode, sync', testGeneratorAll, true, 'utf8', true, false, true, execaSync);532-test('Can use generators with error.all = transform + pipe, objectMode, sync', testGeneratorAll, false, 'utf8', true, false, true, execaSync);533-test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, false, true, execaSync);534-test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, false, true, execaSync);535-test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, false, true, execaSync);536-test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, false, true, execaSync);537-test('Can use generators with result.all = pipe + transform, objectMode, sync', testGeneratorAll, true, 'utf8', true, true, false, execaSync);538-test('Can use generators with error.all = pipe + transform, objectMode, sync', testGeneratorAll, false, 'utf8', true, true, false, execaSync);539-test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, true, false, execaSync);540-test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, true, false, execaSync);541-test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, true, false, execaSync);542-test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, true, false, execaSync);480+test('Can use generators with result.all = transform + transform', testGeneratorAll, true, 'utf8', false, false, false, false, execa);481+test('Can use generators with error.all = transform + transform', testGeneratorAll, false, 'utf8', false, false, false, false, execa);482+test('Can use generators with result.all = transform + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, false, false, execa);483+test('Can use generators with error.all = transform + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, false, false, execa);484+test('Can use generators with result.all = transform + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, false, false, false, execa);485+test('Can use generators with error.all = transform + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, false, false, false, execa);486+test('Can use generators with result.all = transform + pipe', testGeneratorAll, true, 'utf8', false, false, true, false, execa);487+test('Can use generators with error.all = transform + pipe', testGeneratorAll, false, 'utf8', false, false, true, false, execa);488+test('Can use generators with result.all = transform + pipe, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, true, false, execa);489+test('Can use generators with error.all = transform + pipe, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, true, false, execa);490+test('Can use generators with result.all = transform + pipe, encoding "hex"', testGeneratorAll, true, 'hex', false, false, true, false, execa);491+test('Can use generators with error.all = transform + pipe, encoding "hex"', testGeneratorAll, false, 'hex', false, false, true, false, execa);492+test('Can use generators with result.all = pipe + transform', testGeneratorAll, true, 'utf8', false, true, false, false, execa);493+test('Can use generators with error.all = pipe + transform', testGeneratorAll, false, 'utf8', false, true, false, false, execa);494+test('Can use generators with result.all = pipe + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, true, false, false, execa);495+test('Can use generators with error.all = pipe + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, true, false, false, execa);496+test('Can use generators with result.all = pipe + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, true, false, false, execa);497+test('Can use generators with error.all = pipe + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, true, false, false, execa);498+test('Can use generators with result.all = transform + transform, objectMode', testGeneratorAll, true, 'utf8', true, false, false, false, execa);499+test('Can use generators with error.all = transform + transform, objectMode', testGeneratorAll, false, 'utf8', true, false, false, false, execa);500+test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, false, false, execa);501+test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, false, false, execa);502+test('Can use generators with result.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, false, false, execa);503+test('Can use generators with error.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, false, false, execa);504+test('Can use generators with result.all = transform + pipe, objectMode', testGeneratorAll, true, 'utf8', true, false, true, false, execa);505+test('Can use generators with error.all = transform + pipe, objectMode', testGeneratorAll, false, 'utf8', true, false, true, false, execa);506+test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, true, false, execa);507+test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, true, false, execa);508+test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, true, false, execa);509+test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, true, false, execa);510+test('Can use generators with result.all = pipe + transform, objectMode', testGeneratorAll, true, 'utf8', true, true, false, false, execa);511+test('Can use generators with error.all = pipe + transform, objectMode', testGeneratorAll, false, 'utf8', true, true, false, false, execa);512+test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false, false, execa);513+test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false, false, execa);514+test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, true, false, false, execa);515+test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, true, false, false, execa);516+test('Can use generators with result.all = transform + transform, sync', testGeneratorAll, true, 'utf8', false, false, false, false, execaSync);517+test('Can use generators with error.all = transform + transform, sync', testGeneratorAll, false, 'utf8', false, false, false, false, execaSync);518+test('Can use generators with result.all = transform + transform, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, false, false, false, execaSync);519+test('Can use generators with error.all = transform + transform, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, false, false, false, execaSync);520+test('Can use generators with result.all = transform + transform, encoding "hex", sync', testGeneratorAll, true, 'hex', false, false, false, false, execaSync);521+test('Can use generators with error.all = transform + transform, encoding "hex", sync', testGeneratorAll, false, 'hex', false, false, false, false, execaSync);522+test('Can use generators with result.all = transform + pipe, sync', testGeneratorAll, true, 'utf8', false, false, true, false, execaSync);523+test('Can use generators with error.all = transform + pipe, sync', testGeneratorAll, false, 'utf8', false, false, true, false, execaSync);524+test('Can use generators with result.all = transform + pipe, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, false, true, false, execaSync);525+test('Can use generators with error.all = transform + pipe, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, false, true, false, execaSync);526+test('Can use generators with result.all = transform + pipe, encoding "hex", sync', testGeneratorAll, true, 'hex', false, false, true, false, execaSync);527+test('Can use generators with error.all = transform + pipe, encoding "hex", sync', testGeneratorAll, false, 'hex', false, false, true, false, execaSync);528+test('Can use generators with result.all = pipe + transform, sync', testGeneratorAll, true, 'utf8', false, true, false, false, execaSync);529+test('Can use generators with error.all = pipe + transform, sync', testGeneratorAll, false, 'utf8', false, true, false, false, execaSync);530+test('Can use generators with result.all = pipe + transform, encoding "buffer", sync', testGeneratorAll, true, 'buffer', false, true, false, false, execaSync);531+test('Can use generators with error.all = pipe + transform, encoding "buffer", sync', testGeneratorAll, false, 'buffer', false, true, false, false, execaSync);532+test('Can use generators with result.all = pipe + transform, encoding "hex", sync', testGeneratorAll, true, 'hex', false, true, false, false, execaSync);533+test('Can use generators with error.all = pipe + transform, encoding "hex", sync', testGeneratorAll, false, 'hex', false, true, false, false, execaSync);534+test('Can use generators with result.all = transform + transform, objectMode, sync', testGeneratorAll, true, 'utf8', true, false, false, false, execaSync);535+test('Can use generators with error.all = transform + transform, objectMode, sync', testGeneratorAll, false, 'utf8', true, false, false, false, execaSync);536+test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, false, false, false, execaSync);537+test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, false, false, false, execaSync);538+test('Can use generators with result.all = transform + transform, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, false, false, false, execaSync);539+test('Can use generators with error.all = transform + transform, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, false, false, false, execaSync);540+test('Can use generators with result.all = transform + pipe, objectMode, sync', testGeneratorAll, true, 'utf8', true, false, true, false, execaSync);541+test('Can use generators with error.all = transform + pipe, objectMode, sync', testGeneratorAll, false, 'utf8', true, false, true, false, execaSync);542+test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, false, true, false, execaSync);543+test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, false, true, false, execaSync);544+test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, false, true, false, execaSync);545+test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, false, true, false, execaSync);546+test('Can use generators with result.all = pipe + transform, objectMode, sync', testGeneratorAll, true, 'utf8', true, true, false, false, execaSync);547+test('Can use generators with error.all = pipe + transform, objectMode, sync', testGeneratorAll, false, 'utf8', true, true, false, false, execaSync);548+test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer", sync', testGeneratorAll, true, 'buffer', true, true, false, false, execaSync);549+test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer", sync', testGeneratorAll, false, 'buffer', true, true, false, false, execaSync);550+test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex", sync', testGeneratorAll, true, 'hex', true, true, false, false, execaSync);551+test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex", sync', testGeneratorAll, false, 'hex', true, true, false, false, execaSync);552+test('Can use generators with result.all = transform + transform, lines', testGeneratorAll, true, 'utf8', false, false, false, true, execa);553+test('Can use generators with error.all = transform + transform, lines', testGeneratorAll, false, 'utf8', false, false, false, true, execa);554+test('Can use generators with result.all = transform + transform, encoding "buffer", lines', testGeneratorAll, true, 'buffer', false, false, false, true, execa);555+test('Can use generators with error.all = transform + transform, encoding "buffer", lines', testGeneratorAll, false, 'buffer', false, false, false, true, execa);556+test('Can use generators with result.all = transform + transform, encoding "hex", lines', testGeneratorAll, true, 'hex', false, false, false, true, execa);557+test('Can use generators with error.all = transform + transform, encoding "hex", lines', testGeneratorAll, false, 'hex', false, false, false, true, execa);558+test('Can use generators with result.all = transform + pipe, lines', testGeneratorAll, true, 'utf8', false, false, true, true, execa);559+test('Can use generators with error.all = transform + pipe, lines', testGeneratorAll, false, 'utf8', false, false, true, true, execa);560+test('Can use generators with result.all = transform + pipe, encoding "buffer", lines', testGeneratorAll, true, 'buffer', false, false, true, true, execa);561+test('Can use generators with error.all = transform + pipe, encoding "buffer", lines', testGeneratorAll, false, 'buffer', false, false, true, true, execa);562+test('Can use generators with result.all = transform + pipe, encoding "hex", lines', testGeneratorAll, true, 'hex', false, false, true, true, execa);563+test('Can use generators with error.all = transform + pipe, encoding "hex", lines', testGeneratorAll, false, 'hex', false, false, true, true, execa);564+test('Can use generators with result.all = pipe + transform, lines', testGeneratorAll, true, 'utf8', false, true, false, true, execa);565+test('Can use generators with error.all = pipe + transform, lines', testGeneratorAll, false, 'utf8', false, true, false, true, execa);566+test('Can use generators with result.all = pipe + transform, encoding "buffer", lines', testGeneratorAll, true, 'buffer', false, true, false, true, execa);567+test('Can use generators with error.all = pipe + transform, encoding "buffer", lines', testGeneratorAll, false, 'buffer', false, true, false, true, execa);568+test('Can use generators with result.all = pipe + transform, encoding "hex", lines', testGeneratorAll, true, 'hex', false, true, false, true, execa);569+test('Can use generators with error.all = pipe + transform, encoding "hex", lines', testGeneratorAll, false, 'hex', false, true, false, true, execa);570+test('Can use generators with result.all = transform + transform, objectMode, lines', testGeneratorAll, true, 'utf8', true, false, false, true, execa);571+test('Can use generators with error.all = transform + transform, objectMode, lines', testGeneratorAll, false, 'utf8', true, false, false, true, execa);572+test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer", lines', testGeneratorAll, true, 'buffer', true, false, false, true, execa);573+test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer", lines', testGeneratorAll, false, 'buffer', true, false, false, true, execa);574+test('Can use generators with result.all = transform + transform, objectMode, encoding "hex", lines', testGeneratorAll, true, 'hex', true, false, false, true, execa);575+test('Can use generators with error.all = transform + transform, objectMode, encoding "hex", lines', testGeneratorAll, false, 'hex', true, false, false, true, execa);576+test('Can use generators with result.all = transform + pipe, objectMode, lines', testGeneratorAll, true, 'utf8', true, false, true, true, execa);577+test('Can use generators with error.all = transform + pipe, objectMode, lines', testGeneratorAll, false, 'utf8', true, false, true, true, execa);578+test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer", lines', testGeneratorAll, true, 'buffer', true, false, true, true, execa);579+test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer", lines', testGeneratorAll, false, 'buffer', true, false, true, true, execa);580+test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex", lines', testGeneratorAll, true, 'hex', true, false, true, true, execa);581+test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex", lines', testGeneratorAll, false, 'hex', true, false, true, true, execa);582+test('Can use generators with result.all = pipe + transform, objectMode, lines', testGeneratorAll, true, 'utf8', true, true, false, true, execa);583+test('Can use generators with error.all = pipe + transform, objectMode, lines', testGeneratorAll, false, 'utf8', true, true, false, true, execa);584+test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer", lines', testGeneratorAll, true, 'buffer', true, true, false, true, execa);585+test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer", lines', testGeneratorAll, false, 'buffer', true, true, false, true, execa);586+test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex", lines', testGeneratorAll, true, 'hex', true, true, false, true, execa);587+test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex", lines', testGeneratorAll, false, 'hex', true, true, false, true, execa);588+test('Can use generators with result.all = transform + transform, sync, lines', testGeneratorAll, true, 'utf8', false, false, false, true, execaSync);589+test('Can use generators with error.all = transform + transform, sync, lines', testGeneratorAll, false, 'utf8', false, false, false, true, execaSync);590+test('Can use generators with result.all = transform + transform, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', false, false, false, true, execaSync);591+test('Can use generators with error.all = transform + transform, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', false, false, false, true, execaSync);592+test('Can use generators with result.all = transform + transform, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', false, false, false, true, execaSync);593+test('Can use generators with error.all = transform + transform, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', false, false, false, true, execaSync);594+test('Can use generators with result.all = transform + pipe, sync, lines', testGeneratorAll, true, 'utf8', false, false, true, true, execaSync);595+test('Can use generators with error.all = transform + pipe, sync, lines', testGeneratorAll, false, 'utf8', false, false, true, true, execaSync);596+test('Can use generators with result.all = transform + pipe, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', false, false, true, true, execaSync);597+test('Can use generators with error.all = transform + pipe, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', false, false, true, true, execaSync);598+test('Can use generators with result.all = transform + pipe, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', false, false, true, true, execaSync);599+test('Can use generators with error.all = transform + pipe, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', false, false, true, true, execaSync);600+test('Can use generators with result.all = pipe + transform, sync, lines', testGeneratorAll, true, 'utf8', false, true, false, true, execaSync);601+test('Can use generators with error.all = pipe + transform, sync, lines', testGeneratorAll, false, 'utf8', false, true, false, true, execaSync);602+test('Can use generators with result.all = pipe + transform, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', false, true, false, true, execaSync);603+test('Can use generators with error.all = pipe + transform, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', false, true, false, true, execaSync);604+test('Can use generators with result.all = pipe + transform, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', false, true, false, true, execaSync);605+test('Can use generators with error.all = pipe + transform, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', false, true, false, true, execaSync);606+test('Can use generators with result.all = transform + transform, objectMode, sync, lines', testGeneratorAll, true, 'utf8', true, false, false, true, execaSync);607+test('Can use generators with error.all = transform + transform, objectMode, sync, lines', testGeneratorAll, false, 'utf8', true, false, false, true, execaSync);608+test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', true, false, false, true, execaSync);609+test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', true, false, false, true, execaSync);610+test('Can use generators with result.all = transform + transform, objectMode, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', true, false, false, true, execaSync);611+test('Can use generators with error.all = transform + transform, objectMode, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', true, false, false, true, execaSync);612+test('Can use generators with result.all = transform + pipe, objectMode, sync, lines', testGeneratorAll, true, 'utf8', true, false, true, true, execaSync);613+test('Can use generators with error.all = transform + pipe, objectMode, sync, lines', testGeneratorAll, false, 'utf8', true, false, true, true, execaSync);614+test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', true, false, true, true, execaSync);615+test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', true, false, true, true, execaSync);616+test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', true, false, true, true, execaSync);617+test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', true, false, true, true, execaSync);618+test('Can use generators with result.all = pipe + transform, objectMode, sync, lines', testGeneratorAll, true, 'utf8', true, true, false, true, execaSync);619+test('Can use generators with error.all = pipe + transform, objectMode, sync, lines', testGeneratorAll, false, 'utf8', true, true, false, true, execaSync);620+test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer", sync, lines', testGeneratorAll, true, 'buffer', true, true, false, true, execaSync);621+test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer", sync, lines', testGeneratorAll, false, 'buffer', true, true, false, true, execaSync);622+test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex", sync, lines', testGeneratorAll, true, 'hex', true, true, false, true, execaSync);623+test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex", sync, lines', testGeneratorAll, false, 'hex', true, true, false, true, execaSync);624 625 const testInputOption = async (t, type, execaMethod) => {626 const {stdout} = await execaMethod('stdin-fd.js', ['0'], {stdin: generatorsMap[type].uppercase(), input: foobarUint8Array});627 t.is(stdout, foobarUppercase);152 unmodified linesmodified:test/stdio/lines.js+14 added-8 removed
1-import {Buffer} from 'node:buffer';2-import {once} from 'node:events';1 import {Writable} from 'node:stream';2 import test from 'ava';3 import {MaxBufferError} from 'get-stream';4 import {execa, execaSync} from '../../index.js';5 import {setFixtureDir} from '../helpers/fixtures-dir.js';6 import {fullStdio} from '../helpers/stdio.js';7 import {getOutputsGenerator} from '../helpers/generator.js';8 import {foobarString, foobarObject} from '../helpers/input.js';11-import {assertStreamOutput, assertIterableChunks} from '../helpers/convert.js';9+import {assertStreamOutput, assertStreamDataEvents, assertIterableChunks} from '../helpers/convert.js';10 import {11 simpleFull,12 simpleChunks,13 simpleFullUint8Array,148 unmodified lines162 t.is(error.cause, cause);163 t.deepEqual(error.stdout, noNewlinesChunks.slice(0, 2));164 });165 166+test('"lines: true" stops on stream error event', async t => {167+ const cause = new Error(foobarString);168+ const subprocess = getSimpleChunkSubprocessAsync();169+ subprocess.stdout.emit('error', cause);170+ const error = await t.throwsAsync(subprocess);171+ t.is(error.cause, cause);172+ t.deepEqual(error.stdout, []);173+});174+175 const testAsyncIteration = async (t, expectedLines, stripFinalNewline) => {176 const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});177 t.false(subprocess.stdout.readableObjectMode);178 await assertStreamOutput(t, subprocess.stdout, simpleFull);5 unmodified lines184 test('"lines: true" works with stream async iteration, stripFinalNewline', testAsyncIteration, noNewlinesChunks, true);185 186 const testDataEvents = async (t, expectedLines, stripFinalNewline) => {187 const subprocess = getSimpleChunkSubprocessAsync({stripFinalNewline});181- const [firstLine] = await once(subprocess.stdout, 'data');182- t.deepEqual(firstLine, Buffer.from(simpleLines[0]));188+ await assertStreamDataEvents(t, subprocess.stdout, simpleFull);189 const {stdout} = await subprocess;190 t.deepEqual(stdout, expectedLines);191 };192 193 test('"lines: true" works with stream "data" events', testDataEvents, simpleLines, false);194 test('"lines: true" works with stream "data" events, stripFinalNewline', testDataEvents, noNewlinesChunks, true);195 196 const testWritableStream = async (t, expectedLines, stripFinalNewline) => {191- const lines = [];197+ let output = '';198 const writable = new Writable({199 write(line, encoding, done) {194- lines.push(line.toString());200+ output += line.toString();201 done();202 },203 decodeStrings: false,204 });205 const {stdout} = await getSimpleChunkSubprocessAsync({stripFinalNewline, stdout: ['pipe', writable]});200- t.deepEqual(lines, simpleLines);206+ t.deepEqual(output, simpleFull);207 t.deepEqual(stdout, expectedLines);208 };209 210 test('"lines: true" works with writable streams targets', testWritableStream, simpleLines, false);11 unmodified linesmodified:test/stream/all.js+4 added
23 unmodified lines24 };25 26 test('result.all is defined', testAllBoth, doubleFoobarStringFull, 'utf8', false, false, false, execa);27 test('result.all is defined, encoding "buffer"', testAllBoth, doubleFoobarUint8ArrayFull, 'buffer', false, false, false, execa);28+test('result.all is defined, lines', testAllBoth, doubleFoobarArrayFull, 'utf8', true, false, false, execa);29 test('result.all is defined, stripFinalNewline', testAllBoth, doubleFoobarString, 'utf8', false, true, false, execa);30 test('result.all is defined, encoding "buffer", stripFinalNewline', testAllBoth, doubleFoobarUint8Array, 'buffer', false, true, false, execa);31+test('result.all is defined, lines, stripFinalNewline', testAllBoth, doubleFoobarArray, 'utf8', true, true, false, execa);32 test('result.all is defined, failure', testAllBoth, doubleFoobarStringFull, 'utf8', false, false, true, execa);33 test('result.all is defined, encoding "buffer", failure', testAllBoth, doubleFoobarUint8ArrayFull, 'buffer', false, false, true, execa);34+test('result.all is defined, lines, failure', testAllBoth, doubleFoobarArrayFull, 'utf8', true, false, true, execa);35 test('result.all is defined, stripFinalNewline, failure', testAllBoth, doubleFoobarString, 'utf8', false, true, true, execa);36 test('result.all is defined, encoding "buffer", stripFinalNewline, failure', testAllBoth, doubleFoobarUint8Array, 'buffer', false, true, true, execa);37+test('result.all is defined, lines, stripFinalNewline, failure', testAllBoth, doubleFoobarArray, 'utf8', true, true, true, execa);38 test('result.all is defined, sync', testAllBoth, doubleFoobarStringFull, 'utf8', false, false, false, execaSync);39 test('result.all is defined, encoding "buffer", sync', testAllBoth, doubleFoobarUint8ArrayFull, 'buffer', false, false, false, execaSync);40 test('result.all is defined, lines, sync', testAllBoth, doubleFoobarArrayFull, 'utf8', true, false, false, execaSync);41 test('result.all is defined, stripFinalNewline, sync', testAllBoth, doubleFoobarString, 'utf8', false, true, false, execaSync);87 unmodified lines