Skip to content
Myr

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 file

Take it

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 1–31why, one step back
1import {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 back
32 if (isCancelListening) {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 33–35why, one step back
33 return;
34 }35

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 36why, one step back
36 // This is validated before the flag is set, so that a call which throws keeps failing on subsequent calls

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 37–40why, one step back
37 if (!ipc) {
38 throwOnMissingParent();39 return;40 }

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 41–42why, one step back
41
42 isCancelListening = true;

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 43why, one step back
43

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 44–45why, one step back
44 // `channel` is retrieved when Execa is imported, so the current process might have disconnected since then
45 if (!anyProcess.connected) {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 46–53why, one step back
46 abortOnDisconnect();
47 return;48 }4950 getIpcEmitter(anyProcess, channel, isSubprocess);51 await scheduler.yield();52};53

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 54why, one step back
54let isCancelListening = false;

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 55–74why, one step back
55
56// 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();