async.js
This file
Path
lib/async.js
96 lines · 3.5 KiB · JavaScript
Last changed by
Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import {setMaxListeners} from 'node:events';import {spawn} from 'node:child_process';import {normalizeArguments, handleArguments} from './arguments/options.js';import {makeError, makeSuccessResult} from './return/error.js';import {handleOutput} from './return/output.js';import {handleEarlyError} from './return/early-error.js';import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';import {spawnedKill} from './exit/kill.js';import {cleanupOnExit} from './exit/cleanup.js';import {pipeToProcess} from './pipe/setup.js';import {PROCESS_OPTIONS} from './pipe/validate.js';import {makeAllStream} from './stream/all.js';import {getSpawnedResult} from './stream/resolve.js';import {mergePromise} from './promise.js';
export const execa = (rawFile, rawArgs, rawOptions) => { const {spawned, promise, options, stdioStreamsGroups} = runExeca(rawFile, rawArgs, rawOptions); spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}}); mergePromise(spawned, promise); PROCESS_OPTIONS.set(spawned, options); return spawned;};
const runExeca = (rawFile, rawArgs, rawOptions) => { const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions); const stdioStreamsGroups = handleInputAsync(options);
let spawned; try { spawned = spawn(file, args, options); } catch (error) { return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options}); }
const controller = new AbortController(); setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);
const originalStreams = [...spawned.stdio]; pipeOutputAsync(spawned, stdioStreamsGroups, controller); cleanupOnExit(spawned, options, controller);
spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller}); spawned.all = makeAllStream(spawned, options);
const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}); return {spawned, promise, options, stdioStreamsGroups};};
const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => { [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions); const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions); const options = handleAsyncOptions(normalizedOptions); return {file, args, command, escapedCommand, options};};
// Prevent passing the `timeout` option directly to `child_process.spawn()`const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});
const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => { const context = {timedOut: false};
const [ errorInfo, [exitCode, signal], stdioResults, allResult, ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, originalStreams, controller}); controller.abort();
const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult)); const all = handleOutput(options, allResult);
if ('error' in errorInfo) { const isCanceled = options.signal?.aborted === true; const returnedError = makeError({ error: errorInfo.error, command, escapedCommand, timedOut: context.timedOut, isCanceled, exitCode, signal, stdio, all, options, });
if (!options.reject) { return returnedError; }
throw returnedError; }
return makeSuccessResult({command, escapedCommand, stdio, all, options});};