main-async.js
This file
Path
lib/methods/main-async.js
245 lines · 7.5 KiB · JavaScript
Last changed by
Fix `execa()` crashing when spawning fails with `EMFILE` or `ENFILE`Sindre Sorhusbefore adoption15c46cd
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
Fix `execa()` crashing when spawning fails with `EMFILE` or `ENFILE`Sindre Sorhusbefore adoption15c46cd
line 1why, one step back1import {setMaxListeners, once} from 'node:events';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–6why, one step back2import {spawn} from 'node:child_process';3import {MaxBufferError} from 'get-stream';4import {handleCommand} from '../arguments/command.js';5import {normalizeOptions} from '../arguments/options.js';6import {SUBPROCESS_OPTIONS} from '../arguments/fd-options.js';Fix Node 24-specific deprecation warning (#1199)ehmickybefore adoption1ac5b91
line 7why, one step back7import {concatenateShell} from '../arguments/shell.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 8why, one step back8import {addIpcMethods} from '../ipc/methods.js';Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 9why, one step back9import {setIpcSubprocessOptions} from '../ipc/outgoing.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 10–16why, one step back10import {makeError, makeSuccessResult} from '../return/result.js';11import {handleResult} from '../return/reject.js';12import {handleEarlyError} from '../return/early-error.js';13import {handleStdioAsync} from '../stdio/handle-async.js';14import {stripNewline} from '../io/strip-newline.js';15import {pipeOutputAsync} from '../io/output-async.js';16import {subprocessKill} from '../terminate/kill.js';Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
line 17why, one step back17import {getSpawnOptions, getKillFunction} from '../terminate/kill-descendants.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–19why, one step back18import {cleanupOnExit} from '../terminate/cleanup.js';19import {pipeToSubprocess} from '../pipe/setup.js';Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 20why, one step back20import {makeAllStream, getAllFds} from '../resolve/all-async.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–22why, one step back21import {waitForSubprocessResult} from '../resolve/wait-subprocess.js';22import {addConvertedStreams} from '../convert/add.js';Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 23why, one step back23import {createDeferred} from '../utils/deferred.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–25why, one step back24import {mergePromise} from './promise.js';25Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 26why, one step back26// Main shared logic for all async methods: `execa()`, `$`, `execaNode()`100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–28why, one step back27export const execaCoreAsync = (rawFile, rawArguments, rawOptions, createNested) => {28 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleAsyncArguments(rawFile, rawArguments, rawOptions);Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 29why, one step back29 const {subprocess: nodeChildProcess, promise, kill, all, convertedStreams} = spawnSubprocessAsync({100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–38why, one step back30 file,31 commandArguments,32 options,33 startTime,34 verboseInfo,35 command,36 escapedCommand,37 fileDescriptors,38 });Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 39–46why, one step back39 const subprocess = getSubprocessPromise({40 promise,41 nodeChildProcess,42 kill,43 all,44 convertedStreams,45 options,46 });100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 47–59why, one step back47 subprocess.pipe = pipeToSubprocess.bind(undefined, {48 source: subprocess,49 sourcePromise: promise,50 boundOptions: {},51 createNested,52 });53 SUBPROCESS_OPTIONS.set(subprocess, {options, fileDescriptors});54 return subprocess;55};5657// Compute arguments to pass to `child_process.spawn()`58const handleAsyncArguments = (rawFile, rawArguments, rawOptions) => {59 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 60–72why, one step back60 const {file, commandArguments, options: normalizedOptions} = normalizeOptions(rawFile, rawArguments, rawOptions);61 const options = handleAsyncOptions(normalizedOptions);62 const fileDescriptors = handleStdioAsync(options, verboseInfo);63 return {64 file,65 commandArguments,66 command,67 escapedCommand,68 startTime,69 verboseInfo,70 options,71 fileDescriptors,72 };100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 73–76why, one step back73};7475// Options normalization logic specific to async methods.76// Prevent passing the `timeout` option directly to `child_process.spawn()`.Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065
line 77why, one step back77// The `signal` option is not passed either: it has been renamed and is validated in `normalizeOptions()`, for both synchronous and asynchronous methods.Harden some thingsSindre Sorhusbefore adoption4a0a121
line 78why, one step back78// The returned object keeps a null prototype, otherwise a polluted `Object.prototype` would inject option values.Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065
line 79why, one step back79const handleAsyncOptions = ({timeout, signal, ...options}) => ({__proto__: null, ...options, timeoutDuration: timeout});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 80–83why, one step back8081const spawnSubprocessAsync = ({file, commandArguments, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors}) => {82 let subprocess;83 try {Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
line 84why, one step back84 subprocess = spawn(...concatenateShell(file, commandArguments, getSpawnOptions(options)));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 85–87why, one step back85 } catch (error) {86 return handleEarlyError({87 error,Fix `execa()` crashing when spawning fails with `EMFILE` or `ENFILE`Sindre Sorhusbefore adoption15c46cd
lines 88–101why, one step back88 command,89 escapedCommand,90 fileDescriptors,91 options,92 startTime,93 verboseInfo,94 });95 }9697 // With EMFILE or ENFILE, Node.js does not create the subprocess' streams, and emits the error on the next tick instead of throwing it.98 // The listener must be added right away, or that error would crash the current process.99 if (subprocess.stdio === undefined) {100 return handleEarlyError({101 error: once(subprocess, 'error').then(([error]) => error),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 102–111why, one step back102 command,103 escapedCommand,104 fileDescriptors,105 options,106 startTime,107 verboseInfo,108 });109 }110111 const controller = new AbortController();Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 112why, one step back112 setMaxListeners(Infinity, controller.signal);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 113–115why, one step back113114 const originalStreams = [...subprocess.stdio];115 pipeOutputAsync(subprocess, fileDescriptors, controller);Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 116why, one step back116 setIpcSubprocessOptions(subprocess, options);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 117why, one step back117Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 118why, one step back118 const context = {};Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 119why, one step back119 const onInternalError = createDeferred();Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 120why, one step back120 const kill = subprocessKill.bind(undefined, {TweaksSindre Sorhusbefore adoption698a1b8
line 121why, one step back121 kill: getKillFunction(subprocess, options),100% test coverage (#1018)ehmickybefore adoption9f65b86
line 122why, one step back122 options,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 123why, one step back123 onInternalError,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 124why, one step back124 context,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 125–126why, one step back125 controller,126 });Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 127–128why, one step back127 cleanupOnExit(kill, options, controller);128 const all = makeAllStream(subprocess, options);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 129–131why, one step back129130 const promise = handlePromise({131 subprocess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 132–133why, one step back132 kill,133 all,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 134–140why, one step back134 options,135 startTime,136 verboseInfo,137 fileDescriptors,138 originalStreams,139 command,140 escapedCommand,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 141why, one step back141 context,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 142why, one step back142 onInternalError,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 143–144why, one step back143 controller,144 });Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 145–150why, one step back145 return {146 subprocess,147 promise,148 kill,149 all,150 };100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 151–152why, one step back151};152Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 153–176why, one step back153const getSubprocessPromise = ({promise, nodeChildProcess, kill, all, convertedStreams, options}) => {154 const subprocess = mergePromise(promise, getSubprocessProperties(nodeChildProcess, all));155 subprocess.kill = kill ?? subprocess.kill;156 if (convertedStreams === undefined) {157 addConvertedStreams(subprocess, options);158 } else {159 Object.assign(subprocess, convertedStreams);160 }161162 addIpcMethods(subprocess, nodeChildProcess, options);163 return subprocess;164};165166const getSubprocessProperties = (nodeChildProcess, all) => ({167 nodeChildProcess,168 pid: nodeChildProcess.pid,169 stdin: nodeChildProcess.stdin,170 stdout: nodeChildProcess.stdout,171 stderr: nodeChildProcess.stderr,172 stdio: nodeChildProcess.stdio,173 all,174 kill: nodeChildProcess.kill.bind(nodeChildProcess),175});176100% test coverage (#1018)ehmickybefore adoption9f65b86
line 177why, one step back177// Asynchronous logic, as opposed to the previous logic which can be run synchronously, i.e. can be returned to user right awayAdd `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 178why, one step back178const handlePromise = async ({subprocess, kill, all: allStream, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, context, onInternalError, controller}) => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 179–183why, one step back179 const [180 errorInfo,181 [exitCode, signal],182 stdioResults,183 allResult,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 184why, one step back184 ipcOutput,Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 185why, one step back185 ] = await waitForSubprocessResult({100% test coverage (#1018)ehmickybefore adoption9f65b86
line 186why, one step back186 subprocess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 187–188why, one step back187 kill,188 all: allStream,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 189–193why, one step back189 options,190 context,191 verboseInfo,192 fileDescriptors,193 originalStreams,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 194why, one step back194 onInternalError,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 195–197why, one step back195 controller,196 });197 controller.abort();Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 198why, one step back198 onInternalError.resolve();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 199–200why, one step back199200 const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 201why, one step back201 const all = stripNewline(allResult, options, 'all', getAllFds(subprocess, options.buffer));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 202–207why, one step back202 const result = getAsyncResult({203 errorInfo,204 exitCode,205 signal,206 stdio,207 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 208why, one step back208 ipcOutput,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 209–217why, one step back209 context,210 options,211 command,212 escapedCommand,213 startTime,214 });215 return handleResult(result, verboseInfo, options);216};217TweaksSindre Sorhusbefore adoption698a1b8
line 218why, one step back218const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, ipcOutput, context, options, command, escapedCommand, startTime}) => Object.hasOwn(errorInfo, 'error')100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 219–222why, one step back219 ? makeError({220 error: errorInfo.error,221 command,222 escapedCommand,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 223–225why, one step back223 timedOut: context.terminationReason === 'timeout',224 isCanceled: context.terminationReason === 'cancel' || context.terminationReason === 'gracefulCancel',225 isGracefullyCanceled: context.terminationReason === 'gracefulCancel',100% test coverage (#1018)ehmickybefore adoption9f65b86
line 226why, one step back226 isMaxBuffer: errorInfo.error instanceof MaxBufferError,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 227why, one step back227 isForcefullyTerminated: context.isForcefullyTerminated,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 228–231why, one step back228 exitCode,229 signal,230 stdio,231 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 232why, one step back232 ipcOutput,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 233–241why, one step back233 options,234 startTime,235 isSync: false,236 })237 : makeSuccessResult({238 command,239 escapedCommand,240 stdio,241 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 242why, one step back242 ipcOutput,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 243–245why, one step back243 options,244 startTime,245 });