async.js
This file
Path
lib/async.js
89 lines · 3.2 KiB · JavaScript
Last changed by
Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
line 1why, one step back1import {setMaxListeners} from 'node:events';Improve spawning errors (#847)ehmickybefore adoption381dee2
line 2why, one step back2import {spawn} from 'node:child_process';Improve input validation (#838)ehmickybefore adoption8ee137c
line 3why, one step back3import {normalizeArguments, handleArguments} from './arguments/options.js';Improve spawning errors (#847)ehmickybefore adoption381dee2
line 4why, one step back4import {makeError, makeSuccessResult} from './return/error.js';Split files (#837)ehmickybefore adoption672c2d7
line 5why, one step back5import {handleOutput} from './return/output.js';Improve spawning errors (#847)ehmickybefore adoption381dee2
lines 6–7why, one step back6import {handleEarlyError} from './return/early-error.js';7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';Split files (#837)ehmickybefore adoption672c2d7
lines 8–20why, one step back8import {spawnedKill} from './exit/kill.js';9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';11import {makeAllStream} from './stream/all.js';12import {getSpawnedResult} from './stream/resolve.js';13import {mergePromise} from './promise.js';1415export const execa = (rawFile, rawArgs, rawOptions) => {16 const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);17 const stdioStreamsGroups = handleInputAsync(options);1819 let spawned;20 try {Improve spawning errors (#847)ehmickybefore adoption381dee2
line 21why, one step back21 spawned = spawn(file, args, options);Split files (#837)ehmickybefore adoption672c2d7
line 22why, one step back22 } catch (error) {Improve spawning errors (#847)ehmickybefore adoption381dee2
line 23why, one step back23 return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});Split files (#837)ehmickybefore adoption672c2d7
lines 24–36why, one step back24 }2526 const controller = new AbortController();27 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);2829 const originalStreams = [...spawned.stdio];30 pipeOutputAsync(spawned, stdioStreamsGroups, controller);31 cleanupOnExit(spawned, options, controller);3233 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});34 spawned.all = makeAllStream(spawned, options);3536 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 37why, one step back37 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, options});Split files (#837)ehmickybefore adoption672c2d7
lines 38–42why, one step back38 mergePromise(spawned, promise);39 return spawned;40};4142const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {Improve input validation (#838)ehmickybefore adoption8ee137c
line 43why, one step back43 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);Split files (#837)ehmickybefore adoption672c2d7
lines 44–89why, one step back44 const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);45 const options = handleAsyncOptions(normalizedOptions);46 return {file, args, command, escapedCommand, options};47};4849// Prevent passing the `timeout` option directly to `child_process.spawn()`50const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});5152const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {53 const context = {timedOut: false};5455 const [56 errorInfo,57 [exitCode, signal],58 stdioResults,59 allResult,60 ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, originalStreams, controller});61 controller.abort();6263 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));64 const all = handleOutput(options, allResult);6566 if ('error' in errorInfo) {67 const isCanceled = options.signal?.aborted === true;68 const returnedError = makeError({69 error: errorInfo.error,70 command,71 escapedCommand,72 timedOut: context.timedOut,73 isCanceled,74 exitCode,75 signal,76 stdio,77 all,78 options,79 });8081 if (!options.reject) {82 return returnedError;83 }8485 throw returnedError;86 }8788 return makeSuccessResult({command, escapedCommand, stdio, all, options});89};