Skip to content
Myr

Refactor spawning logic (#776)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent c388126 · Browse files · 3 files+8 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+8 added−9 removed

  • other:+8 added−9 removed

3 files+8 added−9 removed

modified:lib/kill.js+1 added-1 removed
modified:lib/stream.js+3 added-7 removed
1 unmodified line
2import {finished} from 'node:stream/promises';3import {setImmediate} from 'node:timers/promises';4import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';5import mergeStreams from '@sindresorhus/merge-streams';6import {throwOnTimeout, cleanupOnExit} from './kill.js';6import {throwOnTimeout} from './kill.js';7import {isStandardStream} from './stdio/utils.js';8import {generatorToDuplexStream} from './stdio/generator.js';910// `all` interleaves `stdout` and `stderr`
131 unmodified lines
142143// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)144export const getSpawnedResult = async ({145 spawned,146 options: {encoding, buffer, maxBuffer, timeoutDuration: timeout, cleanup, detached},146 options: {encoding, buffer, maxBuffer, timeoutDuration: timeout},147 context,148 stdioStreamsGroups,149 controller,150}) => {151 cleanupOnExit(spawned, cleanup, detached, controller);152151 const processSpawnPromise = pEvent(spawned, 'spawn', controller);152 const processErrorPromise = pEvent(spawned, 'error', controller);153 const processExitPromise = pEvent(spawned, 'exit', controller);154
18 unmodified lines
173 ...throwOnTimeout(timeout, context, controller),174 ]);175 } catch (error) {176 spawned.kill();179 return await Promise.all([177 return Promise.all([178 error,179 waitForFailedProcess(processSpawnPromise, processErrorPromise, processExitPromise),180 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),181 getBufferedData(allPromise, encoding),182 Promise.allSettled(customStreamsEndPromises),183 ]);186 } finally {187 controller.abort();184 }185};
modified:index.js+4 added-1 removed
9 unmodified lines
10import {makeError} from './lib/error.js';11import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13import {normalizeStdioNode} from './lib/stdio/normalize.js';14import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay} from './lib/kill.js';14import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit} from './lib/kill.js';15import {pipeToProcess} from './lib/pipe.js';16import {getSpawnedResult, makeAllStream} from './lib/stream.js';17import {mergePromise} from './lib/promise.js';18import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';
136 unmodified lines
155 const controller = new AbortController();156 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);157158 pipeOutputAsync(spawned, stdioStreamsGroups);159 cleanupOnExit(spawned, options, controller);160161 spawned.kill = spawnedKill.bind(undefined, spawned.kill.bind(spawned), options, controller);162 spawned.all = makeAllStream(spawned, options);163 spawned.pipe = pipeToProcess.bind(undefined, {spawned, stdioStreamsGroups, options});
11 unmodified lines
175 [, exitCode, signal],176 stdioResults,177 allResult,178 ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, controller});179 controller.abort();180181 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));182 const all = handleOutput(options, allResult);183184 if (error || exitCode !== 0 || signal !== null) {
178 unmodified lines