graceful.js
This file
Path
lib/ipc/graceful.js
74 lines · 2.1 KiB · JavaScript
Last changed by
Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
History of this fileTake it
Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 1–31why, one step back1import {scheduler} from 'node:timers/promises';2import {sendOneMessage} from './send.js';3import {getIpcEmitter} from './forward.js';4import {validateConnection, getAbortDisconnectError, throwOnMissingParent} from './validation.js';56// Send an IPC message so the subprocess performs a graceful termination7export const sendAbort = (subprocess, message) => {8 const methodName = 'cancelSignal';9 validateConnection(methodName, false, subprocess.connected);10 return sendOneMessage({11 anyProcess: subprocess,12 methodName,13 isSubprocess: false,14 wrappedMessage: {type: GRACEFUL_CANCEL_TYPE, message},15 message,16 });17};1819// When the signal is being used, start listening for incoming messages.20// Unbuffering messages takes one microtask to complete, so this must be async.21export const getCancelSignal = async ({anyProcess, channel, isSubprocess, ipc}) => {22 await startIpc({23 anyProcess,24 channel,25 isSubprocess,26 ipc,27 });28 return cancelController.signal;29};3031const startIpc = async ({anyProcess, channel, isSubprocess, ipc}) => {Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 32why, one step back32 if (isCancelListening) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 33–35why, one step back33 return;34 }35Harden some thingsSindre Sorhusbefore adoption4a0a121
line 36why, one step back36 // This is validated before the flag is set, so that a call which throws keeps failing on subsequent callsAdd `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 37–40why, one step back37 if (!ipc) {38 throwOnMissingParent();39 return;40 }Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 41–42why, one step back4142 isCancelListening = true;Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 43why, one step back43Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 44–45why, one step back44 // `channel` is retrieved when Execa is imported, so the current process might have disconnected since then45 if (!anyProcess.connected) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 46–53why, one step back46 abortOnDisconnect();47 return;48 }4950 getIpcEmitter(anyProcess, channel, isSubprocess);51 await scheduler.yield();52};53Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 54why, one step back54let isCancelListening = false;Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 55–74why, one step back5556// Reception of IPC message to perform a graceful termination57export const handleAbort = wrappedMessage => {58 if (wrappedMessage?.type !== GRACEFUL_CANCEL_TYPE) {59 return false;60 }6162 cancelController.abort(wrappedMessage.message);63 return true;64};6566const GRACEFUL_CANCEL_TYPE = 'execa:ipc:cancel';6768// When the current process disconnects early, the subprocess `cancelSignal` is aborted.69// Otherwise, the signal would never be able to be aborted later on.70export const abortOnDisconnect = () => {71 cancelController.abort(getAbortDisconnectError());72};7374const cancelController = new AbortController();