Skip to content
Myr

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 file

Take 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 back
1import {setMaxListeners, once} from 'node:events';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–6why, one step back
2import {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 back
7import {concatenateShell} from '../arguments/shell.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 8why, one step back
8import {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 back
9import {setIpcSubprocessOptions} from '../ipc/outgoing.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 10–16why, one step back
10import {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 back
17import {getSpawnOptions, getKillFunction} from '../terminate/kill-descendants.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 18–19why, one step back
18import {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 back
20import {makeAllStream, getAllFds} from '../resolve/all-async.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–22why, one step back
21import {waitForSubprocessResult} from '../resolve/wait-subprocess.js';
22import {addConvertedStreams} from '../convert/add.js';

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 23why, one step back
23import {createDeferred} from '../utils/deferred.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 24–25why, one step back
24import {mergePromise} from './promise.js';
25

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 26why, one step back
26// Main shared logic for all async methods: `execa()`, `$`, `execaNode()`

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–28why, one step back
27export 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 back
29 const {subprocess: nodeChildProcess, promise, kill, all, convertedStreams} = spawnSubprocessAsync({

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 30–38why, one step back
30 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 back
39 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 back
47 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 back
60 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 back
73};
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 back
77// 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 back
78// 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 back
79const handleAsyncOptions = ({timeout, signal, ...options}) => ({__proto__: null, ...options, timeoutDuration: timeout});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 80–83why, one step back
80
81const 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 back
84 subprocess = spawn(...concatenateShell(file, commandArguments, getSpawnOptions(options)));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 85–87why, one step back
85 } 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 back
88 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 back
102 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 back
112 setMaxListeners(Infinity, controller.signal);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 113–115why, one step back
113
114 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 back
116 setIpcSubprocessOptions(subprocess, options);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 117why, one step back
117

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 118why, one step back
118 const context = {};

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 119why, one step back
119 const onInternalError = createDeferred();

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 120why, one step back
120 const kill = subprocessKill.bind(undefined, {

TweaksSindre Sorhusbefore adoption698a1b8

line 121why, one step back
121 kill: getKillFunction(subprocess, options),

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 122why, one step back
122 options,

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 123why, one step back
123 onInternalError,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 124why, one step back
124 context,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 125–126why, one step back
125 controller,
126 });

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 127–128why, one step back
127 cleanupOnExit(kill, options, controller);
128 const all = makeAllStream(subprocess, options);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 129–131why, one step back
129
130 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 back
132 kill,
133 all,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 134–140why, one step back
134 options,
135 startTime,136 verboseInfo,137 fileDescriptors,138 originalStreams,139 command,140 escapedCommand,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 141why, one step back
141 context,

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 142why, one step back
142 onInternalError,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–144why, one step back
143 controller,
144 });

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 145–150why, one step back
145 return {
146 subprocess,147 promise,148 kill,149 all,150 };

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 151–152why, one step back
151};
152

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 153–176why, one step back
153const 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});176

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 177why, one step back
177// Asynchronous logic, as opposed to the previous logic which can be run synchronously, i.e. can be returned to user right away

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 178why, one step back
178const 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 back
179 const [
180 errorInfo,181 [exitCode, signal],182 stdioResults,183 allResult,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 184why, one step back
184 ipcOutput,

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 185why, one step back
185 ] = await waitForSubprocessResult({

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 186why, one step back
186 subprocess,

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 187–188why, one step back
187 kill,
188 all: allStream,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 189–193why, one step back
189 options,
190 context,191 verboseInfo,192 fileDescriptors,193 originalStreams,

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 194why, one step back
194 onInternalError,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 195–197why, one step back
195 controller,
196 });197 controller.abort();

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

line 198why, one step back
198 onInternalError.resolve();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 199–200why, one step back
199
200 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 back
201 const all = stripNewline(allResult, options, 'all', getAllFds(subprocess, options.buffer));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 202–207why, one step back
202 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 back
208 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 209–217why, one step back
209 context,
210 options,211 command,212 escapedCommand,213 startTime,214 });215 return handleResult(result, verboseInfo, options);216};217

TweaksSindre Sorhusbefore adoption698a1b8

line 218why, one step back
218const 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 back
219 ? makeError({
220 error: errorInfo.error,221 command,222 escapedCommand,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 223–225why, one step back
223 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 back
226 isMaxBuffer: errorInfo.error instanceof MaxBufferError,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 227why, one step back
227 isForcefullyTerminated: context.isForcefullyTerminated,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 228–231why, one step back
228 exitCode,
229 signal,230 stdio,231 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 232why, one step back
232 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 233–241why, one step back
233 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 back
242 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 243–245why, one step back
243 options,
244 startTime,245 });