wait-subprocess.js
This file
Path
lib/resolve/wait-subprocess.js
152 lines · 4.4 KiB · JavaScript
Last changed by
Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–3why, one step back1import {once} from 'node:events';2import {isStream as isNodeStream} from 'is-stream';3import {throwOnTimeout} from '../terminate/timeout.js';Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977
line 4why, one step back4import {throwOnCancel} from '../terminate/cancel.js';Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 5why, one step back5import {throwOnGracefulCancel} from '../terminate/graceful.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–8why, one step back6import {isStandardStream} from '../utils/standard-stream.js';7import {TRANSFORM_TYPES} from '../stdio/type.js';8import {getBufferedData} from '../io/contents.js';Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
line 9why, one step back9import {waitForIpcOutput, getBufferedIpcOutput} from '../ipc/buffer-messages.js';Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 10why, one step back10import {sendIpcInput} from '../ipc/ipc-input.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 11–18why, one step back11import {waitForAllStream} from './all-async.js';12import {waitForStdioStreams} from './stdio.js';13import {waitForExit, waitForSuccessfulExit} from './exit-async.js';14import {waitForStream} from './wait-stream.js';1516// Retrieve result of subprocess: exit code, signal, error, streams (stdout/stderr/all)17export const waitForSubprocessResult = async ({18 subprocess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 19–20why, one step back19 kill,20 all,Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
lines 21–26why, one step back21 options: {22 encoding,23 buffer,24 maxBuffer,25 lines,26 timeoutDuration: timeout,Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977
line 27why, one step back27 cancelSignal,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 28–29why, one step back28 gracefulCancel,29 forceKillAfterDelay,Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
lines 30–31why, one step back30 stripFinalNewline,31 ipc,Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 32why, one step back32 ipcInput,Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 33why, one step back33 },100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 34–37why, one step back34 context,35 verboseInfo,36 fileDescriptors,37 originalStreams,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 38why, one step back38 onInternalError,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 39–40why, one step back39 controller,40}) => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 41why, one step back41 const exitPromise = waitForExit(subprocess, context);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 42–61why, one step back42 const streamInfo = {43 originalStreams,44 fileDescriptors,45 subprocess,46 exitPromise,47 propagating: false,48 };4950 const stdioPromises = waitForStdioStreams({51 subprocess,52 encoding,53 buffer,54 maxBuffer,55 lines,56 stripFinalNewline,57 verboseInfo,58 streamInfo,59 });60 const allPromise = waitForAllStream({61 subprocess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 62why, one step back62 all,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 63–70why, one step back63 encoding,64 buffer,65 maxBuffer,66 lines,67 stripFinalNewline,68 verboseInfo,69 streamInfo,70 });Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 71why, one step back71 const ipcOutput = [];Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
lines 72–79why, one step back72 const ipcOutputPromise = waitForIpcOutput({73 subprocess,74 buffer,75 maxBuffer,76 ipc,77 ipcOutput,78 verboseInfo,79 });100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 80–89why, one step back80 const originalPromises = waitForOriginalStreams(originalStreams, subprocess, streamInfo);81 const customStreamsEndPromises = waitForCustomStreamsEnd(fileDescriptors, streamInfo);8283 try {84 return await Promise.race([85 Promise.all([86 {},87 waitForSuccessfulExit(exitPromise),88 Promise.all(stdioPromises),89 allPromise,Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
line 90why, one step back90 ipcOutputPromise,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 91why, one step back91 sendIpcInput(subprocess, ipcInput, ipc),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 92–94why, one step back92 ...originalPromises,93 ...customStreamsEndPromises,94 ]),Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 95why, one step back95 onInternalError,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 96why, one step back96 throwOnSubprocessError(subprocess, controller),Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 97why, one step back97 ...throwOnTimeout(kill, timeout, context, controller),Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 98why, one step back98 ...throwOnCancel({Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 99why, one step back99 kill,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 100–106why, one step back100 cancelSignal,101 gracefulCancel,102 context,103 controller,104 }),105 ...throwOnGracefulCancel({106 subprocess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 107why, one step back107 kill,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 108–113why, one step back108 cancelSignal,109 gracefulCancel,110 forceKillAfterDelay,111 context,112 controller,113 }),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 114–115why, one step back114 ]);115 } catch (error) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 116why, one step back116 context.terminationReason ??= 'other';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 117–121why, one step back117 return Promise.all([118 {error},119 exitPromise,120 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise))),121 getBufferedData(allPromise),Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
line 122why, one step back122 getBufferedIpcOutput(ipcOutputPromise, ipcOutput),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 123–138why, one step back123 Promise.allSettled(originalPromises),124 Promise.allSettled(customStreamsEndPromises),125 ]);126 }127};128129// Transforms replace `subprocess.std*`, which means they are not exposed to users.130// However, we still want to wait for their completion.131const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) =>132 originalStreams.map((stream, fdNumber) => stream === subprocess.stdio[fdNumber]133 ? undefined134 : waitForStream(stream, fdNumber, streamInfo));135136// Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.137// The `.pipe()` method automatically ends that stream when `subprocess` ends.138// This makes sure we wait for the completion of those streams, in order to catch any error.Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
line 139why, one step back139const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems, direction}, fdNumber) => stdioItems100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 140–143why, one step back140 .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream))141 .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, {142 isSameDirection: TRANSFORM_TYPES.has(type),143 stopOnExit: type === 'native',Fix the subprocess promise hanging until the user consumes an output streamSindre Sorhusbefore adoption004f3b3
lines 144–145why, one step back144 // Output streams are destinations which are handed back to the user: waiting for their readable side would mean waiting for the user to consume them.145 readable: direction !== 'output' || TRANSFORM_TYPES.has(type),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 146–152why, one step back146 })));147148// Fails when the subprocess emits an `error` event149const throwOnSubprocessError = async (subprocess, {signal}) => {150 const [error] = await once(subprocess, 'error', {signal});151 throw error;152};