Skip to content
Myr

main-async.js

This file

Path

lib/methods/main-async.js

181 lines · 5.4 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–4why, one step back
1import {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 back
5import {normalizeOptions} from '../arguments/options.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 6–21why, one step back
6import {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';20import {mergePromise} from './promise.js';21

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 23–40why, one step back
23export const execaCoreAsync = (rawFile, rawArguments, rawOptions, createNested) => {
24 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleAsyncArguments(rawFile, rawArguments, rawOptions);25 const {subprocess, promise} = spawnSubprocessAsync({26 file,27 commandArguments,28 options,29 startTime,30 verboseInfo,31 command,32 escapedCommand,33 fileDescriptors,34 });35 subprocess.pipe = pipeToSubprocess.bind(undefined, {36 source: subprocess,37 sourcePromise: promise,38 boundOptions: {},39 createNested,40 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 41–45why, one step back
41 mergePromise(subprocess, promise);
42 SUBPROCESS_OPTIONS.set(subprocess, {options, fileDescriptors});43 return subprocess;44};45

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 46why, one step back
46// Compute arguments to pass to `child_process.spawn()`

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 47–48why, one step back
47const handleAsyncArguments = (rawFile, rawArguments, rawOptions) => {
48 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);

Rename files (#977)ehmickybefore adoption4b2e316

lines 49–50why, one step back
49
50 try {

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 51why, one step back
51 const {file, commandArguments, options: normalizedOptions} = normalizeOptions(rawFile, rawArguments, rawOptions);

Rename files (#977)ehmickybefore adoption4b2e316

lines 52–53why, one step back
52 const options = handleAsyncOptions(normalizedOptions);
53 const fileDescriptors = handleStdioAsync(options, verboseInfo);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 54–63why, one step back
54 return {
55 file,56 commandArguments,57 command,58 escapedCommand,59 startTime,60 verboseInfo,61 options,62 fileDescriptors,63 };

Rename files (#977)ehmickybefore adoption4b2e316

lines 64–69why, one step back
64 } catch (error) {
65 logEarlyResult(error, startTime, verboseInfo);66 throw error;67 }68};69

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 70–71why, one step back
70// Options normalization logic specific to async methods.
71// Prevent passing the `timeout` option directly to `child_process.spawn()`.

Rename files (#977)ehmickybefore adoption4b2e316

lines 72–79why, one step back
72const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => {
73 if (signal !== undefined) {74 throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');75 }7677 return {...options, timeoutDuration: timeout, signal: cancelSignal};78};79

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 80why, one step back
80const spawnSubprocessAsync = ({file, commandArguments, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors}) => {

Rename files (#977)ehmickybefore adoption4b2e316

lines 81–82why, one step back
81 let subprocess;
82 try {

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 83why, one step back
83 subprocess = spawn(file, commandArguments, options);

Rename files (#977)ehmickybefore adoption4b2e316

line 84why, one step back
84 } catch (error) {

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 85–93why, one step back
85 return handleEarlyError({
86 error,87 command,88 escapedCommand,89 fileDescriptors,90 options,91 startTime,92 verboseInfo,93 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 94–102why, one step back
94 }
9596 const controller = new AbortController();97 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);9899 const originalStreams = [...subprocess.stdio];100 pipeOutputAsync(subprocess, fileDescriptors, controller);101 cleanupOnExit(subprocess, options, controller);102

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 103–108why, one step back
103 subprocess.kill = subprocessKill.bind(undefined, {
104 kill: subprocess.kill.bind(subprocess),105 subprocess,106 options,107 controller,108 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 109–111why, one step back
109 subprocess.all = makeAllStream(subprocess, options);
110 addConvertedStreams(subprocess, options);111

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 112–122why, one step back
112 const promise = handlePromise({
113 subprocess,114 options,115 startTime,116 verboseInfo,117 fileDescriptors,118 originalStreams,119 command,120 escapedCommand,121 controller,122 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 123–125why, one step back
123 return {subprocess, promise};
124};125

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 127–129why, one step back
127const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller}) => {
128 const context = {timedOut: false};129

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 130–138why, one step back
130 const [errorInfo, [exitCode, signal], stdioResults, allResult] = await waitForSubprocessResult({
131 subprocess,132 options,133 context,134 verboseInfo,135 fileDescriptors,136 originalStreams,137 controller,138 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 139–142why, one step back
139 controller.abort();
140141 const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));142 const all = stripNewline(allResult, options, 'all');

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 143–154why, one step back
143 const result = getAsyncResult({
144 errorInfo,145 exitCode,146 signal,147 stdio,148 all,149 context,150 options,151 command,152 escapedCommand,153 startTime,154 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 155–173why, one step back
155 return handleResult(result, verboseInfo, options);
156};157158const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime}) => 'error' in errorInfo159 ? makeError({160 error: errorInfo.error,161 command,162 escapedCommand,163 timedOut: context.timedOut,164 isCanceled: options.signal?.aborted === true,165 isMaxBuffer: errorInfo.error instanceof MaxBufferError,166 exitCode,167 signal,168 stdio,169 all,170 options,171 startTime,172 isSync: false,173 })

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 174–181why, one step back
174 : makeSuccessResult({
175 command,176 escapedCommand,177 stdio,178 all,179 options,180 startTime,181 });