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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import {scheduler} from 'node:timers/promises';import {sendOneMessage} from './send.js';import {getIpcEmitter} from './forward.js';import {validateConnection, getAbortDisconnectError, throwOnMissingParent} from './validation.js';
// Send an IPC message so the subprocess performs a graceful terminationexport const sendAbort = (subprocess, message) => { const methodName = 'cancelSignal'; validateConnection(methodName, false, subprocess.connected); return sendOneMessage({ anyProcess: subprocess, methodName, isSubprocess: false, wrappedMessage: {type: GRACEFUL_CANCEL_TYPE, message}, message, });};
// When the signal is being used, start listening for incoming messages.// Unbuffering messages takes one microtask to complete, so this must be async.export const getCancelSignal = async ({anyProcess, channel, isSubprocess, ipc}) => { await startIpc({ anyProcess, channel, isSubprocess, ipc, }); return cancelController.signal;};
const startIpc = async ({anyProcess, channel, isSubprocess, ipc}) => { if (isCancelListening) { return; }
// This is validated before the flag is set, so that a call which throws keeps failing on subsequent calls if (!ipc) { throwOnMissingParent(); return; }
isCancelListening = true;
// `channel` is retrieved when Execa is imported, so the current process might have disconnected since then if (!anyProcess.connected) { abortOnDisconnect(); return; }
getIpcEmitter(anyProcess, channel, isSubprocess); await scheduler.yield();};
let isCancelListening = false;
// Reception of IPC message to perform a graceful terminationexport const handleAbort = wrappedMessage => { if (wrappedMessage?.type !== GRACEFUL_CANCEL_TYPE) { return false; }
cancelController.abort(wrappedMessage.message); return true;};
const GRACEFUL_CANCEL_TYPE = 'execa:ipc:cancel';
// When the current process disconnects early, the subprocess `cancelSignal` is aborted.// Otherwise, the signal would never be able to be aborted later on.export const abortOnDisconnect = () => { cancelController.abort(getAbortDisconnectError());};
const cancelController = new AbortController();