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
Provenance
- Commit
a104b72- Tree
1083a3d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c388126
3 files changed+8 added−9 removed
modified:lib/kill.js+1 added-1 removed
64 unmodified lines656667686969707172737 unmodified lines64 unmodified lines }};
// `cleanup` option handlingexport const cleanupOnExit = (spawned, cleanup, detached, {signal}) => {export const cleanupOnExit = (spawned, {cleanup, detached}, {signal}) => { if (!cleanup || detached) { return; }
7 unmodified linesmodified:lib/stream.js+3 added-7 removed
1 unmodified line2 import {finished} from 'node:stream/promises';3 import {setImmediate} from 'node:timers/promises';4 import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';5 import mergeStreams from '@sindresorhus/merge-streams';6-import {throwOnTimeout, cleanupOnExit} from './kill.js';6+import {throwOnTimeout} from './kill.js';7 import {isStandardStream} from './stdio/utils.js';8 import {generatorToDuplexStream} from './stdio/generator.js';9 10 // `all` interleaves `stdout` and `stderr`131 unmodified lines142 143 // Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)144 export 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);152-151 const processSpawnPromise = pEvent(spawned, 'spawn', controller);152 const processErrorPromise = pEvent(spawned, 'error', controller);153 const processExitPromise = pEvent(spawned, 'exit', controller);154 18 unmodified lines173 ...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 lines10 import {makeError} from './lib/error.js';11 import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12 import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13 import {normalizeStdioNode} from './lib/stdio/normalize.js';14-import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay} from './lib/kill.js';14+import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit} from './lib/kill.js';15 import {pipeToProcess} from './lib/pipe.js';16 import {getSpawnedResult, makeAllStream} from './lib/stream.js';17 import {mergePromise} from './lib/promise.js';18 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';136 unmodified lines155 const controller = new AbortController();156 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);157 158 pipeOutputAsync(spawned, stdioStreamsGroups);159+ cleanupOnExit(spawned, options, controller);160 161 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 lines175 [, exitCode, signal],176 stdioResults,177 allResult,178 ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, controller});179+ controller.abort();180+181 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));182 const all = handleOutput(options, allResult);183 184 if (error || exitCode !== 0 || signal !== null) {178 unmodified lines