Refactor `reject` option (#868)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 8f51c6b · Browse files · 5 files·+80 added−94 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+80 added−94 removed
- other:+80 added−94 removed
Provenance
- Commit
1f8a55c- Tree
736e8c3- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8f51c6b
5 files changed+80 added−94 removed
modified:lib/return/early-error.js+5 added-10 removed
1234567891011121314141516151617181920219 unmodified lines313233343435363738394035import {ChildProcess} from 'node:child_process';import {PassThrough} from 'node:stream';import {cleanupStdioStreams} from '../stdio/async.js';import {makeEarlyError} from './error.js';import {handleResult} from './output.js';
// When the child process fails to spawn.// We ensure the returned error is always both a promise and a child process.export const handleEarlyError = ({error, command, escapedCommand, stdioStreamsGroups, options}) => { cleanupStdioStreams(stdioStreamsGroups);
const spawned = new ChildProcess(); createDummyStreams(spawned);
const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options}); const promise = handleDummyPromise(errorInstance, options); return {spawned, promise, options, stdioStreamsGroups}; const earlyError = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options}); const promise = handleDummyPromise(earlyError, options); return {spawned, promise};};
const createDummyStreams = spawned => { const stdin = createDummyStream();9 unmodified lines stream.end(); return stream;};
const handleDummyPromise = async (error, {reject}) => { if (reject) { throw error; }
return error;};const handleDummyPromise = async (error, options) => handleResult(error, options);modified:lib/return/output.js+8 added
15 unmodified lines16171819202122232425262715 unmodified lines }
return options.stripFinalNewline ? stripFinalNewline(value) : value;};
export const handleResult = (result, {reject}) => { if (result.failed && reject) { throw result; }
return result;};modified:lib/stdio/sync.js+3 added-3 removed
45 unmodified lines464748495051505152535455565757585960619 unmodified lines45 unmodified lines
const serializeInput = ({type, value}) => type === 'string' ? value : binaryToString(value);
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync modeexport const pipeOutputSync = (stdioStreamsGroups, result) => { if (result.output === null) {export const pipeOutputSync = (stdioStreamsGroups, {output}) => { if (output === null) { return; }
for (const stdioStreams of stdioStreamsGroups) { for (const stdioStream of stdioStreams) { pipeStdioOptionSync(result.output[stdioStream.fdNumber], stdioStream); pipeStdioOptionSync(output[stdioStream.fdNumber], stdioStream); } }};
9 unmodified linesmodified:lib/async.js+32 added-40 removed
1 import {setMaxListeners} from 'node:events';2 import {spawn} from 'node:child_process';3 import {normalizeArguments, handleArguments} from './arguments/options.js';4 import {makeError, makeSuccessResult} from './return/error.js';5-import {handleOutput} from './return/output.js';5+import {handleOutput, handleResult} from './return/output.js';6 import {handleEarlyError} from './return/early-error.js';7 import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';8 import {spawnedKill} from './exit/kill.js';9 import {cleanupOnExit} from './exit/cleanup.js';3 unmodified lines13 import {getSpawnedResult} from './stream/resolve.js';14 import {mergePromise} from './promise.js';15 16 export const execa = (rawFile, rawArgs, rawOptions) => {17- const {spawned, promise, options, stdioStreamsGroups} = runExeca(rawFile, rawArgs, rawOptions);17+ const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);18+ const stdioStreamsGroups = handleInputAsync(options);19+ const {spawned, promise} = runExeca({file, args, options, command, escapedCommand, stdioStreamsGroups});20 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}});21 mergePromise(spawned, promise);22 PROCESS_OPTIONS.set(spawned, options);23 return spawned;24 };25 24-const runExeca = (rawFile, rawArgs, rawOptions) => {25- const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);26- const stdioStreamsGroups = handleInputAsync(options);26+const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {27+ [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);28+ const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);29+ const options = handleAsyncOptions(normalizedOptions);30+ return {file, args, command, escapedCommand, options};31+};32 33+// Prevent passing the `timeout` option directly to `child_process.spawn()`34+const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});35+36+const runExeca = ({file, args, options, command, escapedCommand, stdioStreamsGroups}) => {37 let spawned;38 try {39 spawned = spawn(file, args, options);40 } catch (error) {10 unmodified lines51 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});52 spawned.all = makeAllStream(spawned, options);53 54 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});46- return {spawned, promise, options, stdioStreamsGroups};55+ return {spawned, promise};56 };57 49-const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {50- [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);51- const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);52- const options = handleAsyncOptions(normalizedOptions);53- return {file, args, command, escapedCommand, options};54-};55-56-// Prevent passing the `timeout` option directly to `child_process.spawn()`57-const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});58-58 const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {59 const context = {timedOut: false};60 61 const [5 unmodified lines67 controller.abort();68 69 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));70 const all = handleOutput(options, allResult);72-73- if ('error' in errorInfo) {74- const isCanceled = options.signal?.aborted === true;75- const returnedError = makeError({76- error: errorInfo.error,77- command,78- escapedCommand,79- timedOut: context.timedOut,80- isCanceled,81- exitCode,82- signal,83- stdio,84- all,85- options,86- });87-88- if (!options.reject) {89- return returnedError;90- }91-92- throw returnedError;93- }94-95- return makeSuccessResult({command, escapedCommand, stdio, all, options});71+ const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand});72+ return handleResult(result, options);73 };74+75+const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand}) => 'error' in errorInfo76+ ? makeError({77+ error: errorInfo.error,78+ command,79+ escapedCommand,80+ timedOut: context.timedOut,81+ isCanceled: options.signal?.aborted === true,82+ exitCode,83+ signal,84+ stdio,85+ all,86+ options,87+ })88+ : makeSuccessResult({command, escapedCommand, stdio, all, options});modified:lib/sync.js+32 added-41 removed
123445678910111213141516171819202122232425262728293031323334353637383940414243444546474849501112131415169 unmodified lines262728293031323334353637383940414243444546474849505152535455565758import {spawnSync} from 'node:child_process';import {normalizeArguments, handleArguments} from './arguments/options.js';import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';import {handleOutput} from './return/output.js';import {handleOutput, handleResult} from './return/output.js';import {handleInputSync, pipeOutputSync} from './stdio/sync.js';import {isFailedExit} from './exit/code.js';
export const execaSync = (rawFile, rawArgs, rawOptions) => { const {file, args, command, escapedCommand, options} = handleSyncArguments(rawFile, rawArgs, rawOptions); const stdioStreamsGroups = handleInputSync(options);
let result; try { result = spawnSync(file, args, options); } catch (error) { const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});
if (!options.reject) { return errorInstance; }
throw errorInstance; }
pipeOutputSync(stdioStreamsGroups, result);
const output = result.output || Array.from({length: 3}); const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));
if (result.error !== undefined || isFailedExit(result.status, result.signal)) { const error = makeError({ error: result.error, command, escapedCommand, timedOut: result.error && result.error.code === 'ETIMEDOUT', isCanceled: false, exitCode: result.status, signal: result.signal, stdio, options, });
if (!options.reject) { return error; }
throw error; }
return makeSuccessResult({command, escapedCommand, stdio, options}); const result = runExecaSync({file, args, options, command, escapedCommand, stdioStreamsGroups}); return handleResult(result, options);};
const handleSyncArguments = (rawFile, rawArgs, rawOptions) => { [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);9 unmodified lines if (ipc) { throw new TypeError('The "ipc: true" option cannot be used with synchronous methods.'); }};
const runExecaSync = ({file, args, options, command, escapedCommand, stdioStreamsGroups}) => { let syncResult; try { syncResult = spawnSync(file, args, options); } catch (error) { return makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options}); }
pipeOutputSync(stdioStreamsGroups, syncResult);
const output = syncResult.output || Array.from({length: 3}); const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput)); return getSyncResult(syncResult, {stdio, options, command, escapedCommand});};
const getSyncResult = ({error, status, signal}, {stdio, options, command, escapedCommand}) => error !== undefined || isFailedExit(status, signal) ? makeError({ error, command, escapedCommand, timedOut: error && error.code === 'ETIMEDOUT', isCanceled: false, exitCode: status, signal, stdio, options, }) : makeSuccessResult({command, escapedCommand, stdio, options});