Skip to content
Myr

Simplify `std*` options logic (#691)

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

authored by ehmicky · committed by GitHub · parent 0298730 · Browse files · 6 files+45 added−40 removed
This commit

State

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

Diff stats

6 files+45 added−40 removed

  • other:+45 added−40 removed

6 files+45 added−40 removed

modified:lib/stdio/async.js+7 added-5 removed
modified:lib/stdio/direction.js+7 added-4 removed
modified:lib/stdio/handle.js+1 added-1 removed
11 unmodified lines
12 .map(stdioStreams => validateStreams(stdioStreams))13 .map(stdioStreams => addStreamDirection(stdioStreams))14 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));15 options.stdio = transformStdio(stdioStreamsGroups);16 return {stdioStreams: stdioStreamsGroups.flat(), stdioLength: stdioStreamsGroups.length};16 return stdioStreamsGroups;17};1819// We make sure passing an array with a single item behaves the same as passing that item without an array.20// This is what users would expect.
78 unmodified lines
modified:lib/stdio/sync.js+10 added-8 removed
4 unmodified lines
5import {bufferToUint8Array} from './utils.js';67// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode8export const handleInputSync = options => {9 const {stdioStreams, stdioLength} = handleInput(addPropertiesSync, options);10 addInputOptionSync(stdioStreams, options);11 return {stdioStreams, stdioLength};9 const stdioStreamsGroups = handleInput(addPropertiesSync, options);10 addInputOptionSync(stdioStreamsGroups, options);11 return stdioStreamsGroups;12};1314const forbiddenIfStreamSync = ({value, optionName}) => {15 if (isNodeStream(value)) {
25 unmodified lines
41 native: forbiddenIfStreamSync,42 },43};4445const addInputOptionSync = (stdioStreams, options) => {46 const inputs = stdioStreams.filter(({type}) => type === 'string' || type === 'uint8Array');45const addInputOptionSync = (stdioStreamsGroups, options) => {46 const inputs = stdioStreamsGroups.flat().filter(({type}) => type === 'string' || type === 'uint8Array');47 if (inputs.length === 0) {48 return;49 }50
4 unmodified lines
5556const serializeInput = ({type, value}) => type === 'string' ? value : new TextDecoder().decode(value);5758// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode59export const pipeOutputSync = (stdioStreams, result) => {59export const pipeOutputSync = (stdioStreamsGroups, result) => {60 if (result.output === null) {61 return;62 }6364 for (const stdioStreams of stdioStreamsGroups) {64 for (const stdioStream of stdioStreams) {65 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);65 for (const stdioStream of stdioStreams) {66 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);67 }68 }69};7071const pipeStdioOptionSync = (result, {type, value, direction}) => {
8 unmodified lines
modified:lib/stream.js+10 added-11 removed
33 unmodified lines
34 return error.bufferedData === undefined ? undefined : applyEncoding(error.bufferedData, encoding);35 }36};3738const getStdioPromise = ({stream, index, stdioStreams, encoding, buffer, maxBuffer}) => {39 const stdioStream = stdioStreams.find(stdioStream => stdioStream.index === index);40 return stdioStream?.direction === 'output' ? getStreamPromise(stream, {encoding, buffer, maxBuffer}) : undefined;41};38const getStdioPromise = ({stream, index, stdioStreamsGroups, encoding, buffer, maxBuffer}) => stdioStreamsGroups[index]?.[0]?.direction === 'output'39 ? getStreamPromise(stream, {encoding, buffer, maxBuffer})40 : undefined;4142const getStreamPromise = async (stream, {encoding, buffer, maxBuffer}) => {43 if (!stream || !buffer) {44 return;
20 unmodified lines
65// eslint-disable-next-line unicorn/text-encoding-identifier-case66const isUtf8Encoding = encoding => encoding === 'utf8' || encoding === 'utf-8';6768// Retrieve streams created by the `std*` options70const getCustomStreams = stdioStreams => stdioStreams.filter(({type}) => type !== 'native');69const getCustomStreams = stdioStreamsGroups => stdioStreamsGroups.flat().filter(({type}) => type !== 'native');7071// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.72// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.73// This makes sure we want for the completion of those streams, in order to catch any error.
23 unmodified lines
97// The streams created by the `std*` options are automatically ended by `.pipe()`.98// However `.pipe()` only does so when the source stream ended, not when it errored.99// Therefore, when `childProcess.stdin|stdout|stderr` errors, those streams must be manually destroyed.100const cleanupStdioStreams = (customStreams, error) => {102 for (const customStream of customStreams) {103 if (!STANDARD_STREAMS.includes(customStream.value)) {104 customStream.value.destroy(error);101 for (const {value} of customStreams) {102 if (!STANDARD_STREAMS.includes(value)) {103 value.destroy(error);104 }105 }106};107
1 unmodified line
109export const getSpawnedResult = async (110 spawned,111 {encoding, buffer, maxBuffer, timeout, killSignal, cleanup, detached},112 context,114 stdioStreams,113 stdioStreamsGroups,114) => {115 const finalizers = [];116 cleanupOnExit(spawned, cleanup, detached, finalizers);118 const customStreams = getCustomStreams(stdioStreams);117 const customStreams = getCustomStreams(stdioStreamsGroups);118120 const stdioPromises = spawned.stdio.map((stream, index) => getStdioPromise({stream, index, stdioStreams, encoding, buffer, maxBuffer}));119 const stdioPromises = spawned.stdio.map((stream, index) => getStdioPromise({stream, index, stdioStreamsGroups, encoding, buffer, maxBuffer}));120 const allPromise = getStreamPromise(spawned.all, {encoding, buffer, maxBuffer: maxBuffer * 2});121122 try {123 return await Promise.race([
25 unmodified lines
modified:index.js+10 added-11 removed
94 unmodified lines
95export function execa(rawFile, rawArgs, rawOptions) {96 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);97 validateTimeout(options);9899 const {stdioStreams, stdioLength} = handleInputAsync(options);99 const stdioStreamsGroups = handleInputAsync(options);100101 let spawned;102 try {103 spawned = childProcess.spawn(file, args, options);
1 unmodified line
105 // Ensure the returned error is always both a promise and a child process106 const dummySpawned = new childProcess.ChildProcess();107 const errorPromise = Promise.reject(makeError({108 error,109 stdio: Array.from({length: stdioLength}),109 stdio: Array.from({length: stdioStreamsGroups.length}),110 command,111 escapedCommand,112 options,113 timedOut: false,
2 unmodified lines
116 mergePromise(dummySpawned, errorPromise);117 return dummySpawned;118 }119120 pipeOutputAsync(spawned, stdioStreamsGroups);121122 const context = {isCanceled: false, timedOut: false};123124 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));125 spawned.cancel = spawnedCancel.bind(null, spawned, context);124125 pipeOutputAsync(spawned, stdioStreams);126126 spawned.all = makeAllStream(spawned, options);127128 addPipeMethods(spawned);129131 const promise = handlePromise({spawned, options, context, stdioStreams, command, escapedCommand});130 const promise = handlePromise({spawned, options, context, stdioStreamsGroups, command, escapedCommand});131 mergePromise(spawned, promise);132 return spawned;133}134136const handlePromise = async ({spawned, options, context, stdioStreams, command, escapedCommand}) => {135const handlePromise = async ({spawned, options, context, stdioStreamsGroups, command, escapedCommand}) => {136 const [137 [exitCode, signal, error],138 stdioResults,139 allResult,141 ] = await getSpawnedResult(spawned, options, context, stdioStreams);140 ] = await getSpawnedResult(spawned, options, context, stdioStreamsGroups);141 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));142 const all = handleOutput(options, allResult);143144 if (error || exitCode !== 0 || signal !== null) {
35 unmodified lines
180181export function execaSync(rawFile, rawArgs, rawOptions) {182 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);183185 const {stdioStreams, stdioLength} = handleInputSync(options);184 const stdioStreamsGroups = handleInputSync(options);185186 let result;187 try {188 result = childProcess.spawnSync(file, args, options);189 } catch (error) {190 throw makeError({191 error,193 stdio: Array.from({length: stdioLength}),192 stdio: Array.from({length: stdioStreamsGroups.stdioLength}),193 command,194 escapedCommand,195 options,196 timedOut: false,197 isCanceled: false,198 });199 }200202 pipeOutputSync(stdioStreams, result);201 pipeOutputSync(stdioStreamsGroups, result);202203 const output = result.output || Array.from({length: 3});204 const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));205
111 unmodified lines