main-async.js
This file
Path
lib/methods/main-async.js
186 lines · 5.6 KiB · JavaScript
Last changed by
Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–4why, one step back1import {setMaxListeners} from 'node:events';2import {spawn} from 'node:child_process';3import {MaxBufferError} from 'get-stream';4import {handleCommand} from '../arguments/command.js';Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 5why, one step back5import {normalizeOptions} from '../arguments/options.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 6–19why, one step back6import {SUBPROCESS_OPTIONS} from '../arguments/fd-options.js';7import {makeError, makeSuccessResult} from '../return/result.js';8import {handleResult} from '../return/reject.js';9import {handleEarlyError} from '../return/early-error.js';10import {handleStdioAsync} from '../stdio/handle-async.js';11import {stripNewline} from '../io/strip-newline.js';12import {pipeOutputAsync} from '../io/output-async.js';13import {subprocessKill} from '../terminate/kill.js';14import {cleanupOnExit} from '../terminate/cleanup.js';15import {pipeToSubprocess} from '../pipe/setup.js';16import {logEarlyResult} from '../verbose/complete.js';17import {makeAllStream} from '../resolve/all-async.js';18import {waitForSubprocessResult} from '../resolve/wait-subprocess.js';19import {addConvertedStreams} from '../convert/add.js';Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 20why, one step back20import {createDeferred} from '../utils/deferred.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 21–22why, one step back21import {mergePromise} from './promise.js';22Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 23why, one step back23// Main shared logic for all async methods: `execa()`, `execaCommand()`, `$`, `execaNode()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 24–41why, one step back24export const execaCoreAsync = (rawFile, rawArguments, rawOptions, createNested) => {25 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleAsyncArguments(rawFile, rawArguments, rawOptions);26 const {subprocess, promise} = spawnSubprocessAsync({27 file,28 commandArguments,29 options,30 startTime,31 verboseInfo,32 command,33 escapedCommand,34 fileDescriptors,35 });36 subprocess.pipe = pipeToSubprocess.bind(undefined, {37 source: subprocess,38 sourcePromise: promise,39 boundOptions: {},40 createNested,41 });Rename files (#977)ehmickybefore adoption4b2e316
lines 42–46why, one step back42 mergePromise(subprocess, promise);43 SUBPROCESS_OPTIONS.set(subprocess, {options, fileDescriptors});44 return subprocess;45};46Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 47why, one step back47// Compute arguments to pass to `child_process.spawn()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 48–49why, one step back48const handleAsyncArguments = (rawFile, rawArguments, rawOptions) => {49 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);Rename files (#977)ehmickybefore adoption4b2e316
lines 50–51why, one step back5051 try {Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 52why, one step back52 const {file, commandArguments, options: normalizedOptions} = normalizeOptions(rawFile, rawArguments, rawOptions);Rename files (#977)ehmickybefore adoption4b2e316
lines 53–54why, one step back53 const options = handleAsyncOptions(normalizedOptions);54 const fileDescriptors = handleStdioAsync(options, verboseInfo);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 55–64why, one step back55 return {56 file,57 commandArguments,58 command,59 escapedCommand,60 startTime,61 verboseInfo,62 options,63 fileDescriptors,64 };Rename files (#977)ehmickybefore adoption4b2e316
lines 65–70why, one step back65 } catch (error) {66 logEarlyResult(error, startTime, verboseInfo);67 throw error;68 }69};70Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 71–72why, one step back71// Options normalization logic specific to async methods.72// Prevent passing the `timeout` option directly to `child_process.spawn()`.Rename files (#977)ehmickybefore adoption4b2e316
lines 73–80why, one step back73const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => {74 if (signal !== undefined) {75 throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');76 }7778 return {...options, timeoutDuration: timeout, signal: cancelSignal};79};80Upgrade xo (#980)ehmickybefore adoptionccfa412
line 81why, one step back81const spawnSubprocessAsync = ({file, commandArguments, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors}) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 82–83why, one step back82 let subprocess;83 try {Upgrade xo (#980)ehmickybefore adoptionccfa412
line 84why, one step back84 subprocess = spawn(file, commandArguments, options);Rename files (#977)ehmickybefore adoption4b2e316
line 85why, one step back85 } catch (error) {Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 86–94why, one step back86 return handleEarlyError({87 error,88 command,89 escapedCommand,90 fileDescriptors,91 options,92 startTime,93 verboseInfo,94 });Rename files (#977)ehmickybefore adoption4b2e316
lines 95–103why, one step back95 }9697 const controller = new AbortController();98 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);99100 const originalStreams = [...subprocess.stdio];101 pipeOutputAsync(subprocess, fileDescriptors, controller);102 cleanupOnExit(subprocess, options, controller);103Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 104why, one step back104 const onInternalError = createDeferred();Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 105–107why, one step back105 subprocess.kill = subprocessKill.bind(undefined, {106 kill: subprocess.kill.bind(subprocess),107 options,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 108why, one step back108 onInternalError,Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 109–110why, one step back109 controller,110 });Rename files (#977)ehmickybefore adoption4b2e316
lines 111–113why, one step back111 subprocess.all = makeAllStream(subprocess, options);112 addConvertedStreams(subprocess, options);113Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 114–122why, one step back114 const promise = handlePromise({115 subprocess,116 options,117 startTime,118 verboseInfo,119 fileDescriptors,120 originalStreams,121 command,122 escapedCommand,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 123why, one step back123 onInternalError,Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 124–125why, one step back124 controller,125 });Rename files (#977)ehmickybefore adoption4b2e316
lines 126–128why, one step back126 return {subprocess, promise};127};128Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 129why, one step back129// Asynchronous logic, as opposed to the previous logic which can be run synchronously, i.e. can be returned to user right awayRefactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 130why, one step back130const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, onInternalError, controller}) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 131–132why, one step back131 const context = {timedOut: false};132Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 133–139why, one step back133 const [errorInfo, [exitCode, signal], stdioResults, allResult] = await waitForSubprocessResult({134 subprocess,135 options,136 context,137 verboseInfo,138 fileDescriptors,139 originalStreams,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 140why, one step back140 onInternalError,Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 141–142why, one step back141 controller,142 });Rename files (#977)ehmickybefore adoption4b2e316
line 143why, one step back143 controller.abort();Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 144why, one step back144 onInternalError.resolve();Rename files (#977)ehmickybefore adoption4b2e316
lines 145–147why, one step back145146 const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));147 const all = stripNewline(allResult, options, 'all');Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 148–159why, one step back148 const result = getAsyncResult({149 errorInfo,150 exitCode,151 signal,152 stdio,153 all,154 context,155 options,156 command,157 escapedCommand,158 startTime,159 });Rename files (#977)ehmickybefore adoption4b2e316
lines 160–178why, one step back160 return handleResult(result, verboseInfo, options);161};162163const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime}) => 'error' in errorInfo164 ? makeError({165 error: errorInfo.error,166 command,167 escapedCommand,168 timedOut: context.timedOut,169 isCanceled: options.signal?.aborted === true,170 isMaxBuffer: errorInfo.error instanceof MaxBufferError,171 exitCode,172 signal,173 stdio,174 all,175 options,176 startTime,177 isSync: false,178 })Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 179–186why, one step back179 : makeSuccessResult({180 command,181 escapedCommand,182 stdio,183 all,184 options,185 startTime,186 });