Skip to content
Myr

async.js

This file

Path

lib/async.js

96 lines · 3.5 KiB · JavaScript

Last changed by

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

line 1why, one step back
1import {setMaxListeners} from 'node:events';

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 2why, one step back
2import {spawn} from 'node:child_process';

Improve input validation (#838)ehmickybefore adoption8ee137c

line 3why, one step back
3import {normalizeArguments, handleArguments} from './arguments/options.js';

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 4why, one step back
4import {makeError, makeSuccessResult} from './return/error.js';

Split files (#837)ehmickybefore adoption672c2d7

line 5why, one step back
5import {handleOutput} from './return/output.js';

Improve spawning errors (#847)ehmickybefore adoption381dee2

lines 6–7why, one step back
6import {handleEarlyError} from './return/early-error.js';
7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 8–10why, one step back
8import {spawnedKill} from './exit/kill.js';
9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 11why, one step back
11import {PROCESS_OPTIONS} from './pipe/validate.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 12–16why, one step back
12import {makeAllStream} from './stream/all.js';
13import {getSpawnedResult} from './stream/resolve.js';14import {mergePromise} from './promise.js';1516export const execa = (rawFile, rawArgs, rawOptions) => {

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 17why, one step back
17 const {spawned, promise, options, stdioStreamsGroups} = runExeca(rawFile, rawArgs, rawOptions);

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

line 18why, one step back
18 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}});

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

lines 19–24why, one step back
19 mergePromise(spawned, promise);
20 PROCESS_OPTIONS.set(spawned, options);21 return spawned;22};2324const runExeca = (rawFile, rawArgs, rawOptions) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 25–29why, one step back
25 const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);
26 const stdioStreamsGroups = handleInputAsync(options);2728 let spawned;29 try {

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 30why, one step back
30 spawned = spawn(file, args, options);

Split files (#837)ehmickybefore adoption672c2d7

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

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 32why, one step back
32 return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});

Split files (#837)ehmickybefore adoption672c2d7

lines 33–45why, one step back
33 }
3435 const controller = new AbortController();36 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);3738 const originalStreams = [...spawned.stdio];39 pipeOutputAsync(spawned, stdioStreamsGroups, controller);40 cleanupOnExit(spawned, options, controller);4142 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});43 spawned.all = makeAllStream(spawned, options);4445 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 46why, one step back
46 return {spawned, promise, options, stdioStreamsGroups};

Split files (#837)ehmickybefore adoption672c2d7

lines 47–49why, one step back
47};
4849const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {

Improve input validation (#838)ehmickybefore adoption8ee137c

line 50why, one step back
50 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);

Split files (#837)ehmickybefore adoption672c2d7

lines 51–96why, one step back
51 const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);
52 const options = handleAsyncOptions(normalizedOptions);53 return {file, args, command, escapedCommand, options};54};5556// Prevent passing the `timeout` option directly to `child_process.spawn()`57const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});5859const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {60 const context = {timedOut: false};6162 const [63 errorInfo,64 [exitCode, signal],65 stdioResults,66 allResult,67 ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, originalStreams, controller});68 controller.abort();6970 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));71 const all = handleOutput(options, allResult);7273 if ('error' in errorInfo) {74 const isCanceled = options.signal?.aborted === true;75 const returnedError = makeError({76 error: errorInfo.error,77 command,78 escapedCommand,79 timedOut: context.timedOut,80 isCanceled,81 exitCode,82 signal,83 stdio,84 all,85 options,86 });8788 if (!options.reject) {89 return returnedError;90 }9192 throw returnedError;93 }9495 return makeSuccessResult({command, escapedCommand, stdio, all, options});96};