Skip to content
Myr

validation.js

This file

Path

lib/ipc/validation.js

122 lines · 5.8 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 1–2why, one step back
1import {inspect} from 'node:util';
2

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

lines 3–8why, one step back
3// Validate the IPC channel is connected before receiving/sending messages
4export const validateIpcMethod = ({methodName, isSubprocess, ipc, isConnected}) => {5 validateIpcOption(methodName, isSubprocess, ipc);6 validateConnection(methodName, isSubprocess, isConnected);7};8

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 9why, one step back
9// Better error message when forgetting to set `ipc: true` and using the IPC methods

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 10why, one step back
10const validateIpcOption = (methodName, isSubprocess, ipc) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 11why, one step back
11 if (!ipc) {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 12why, one step back
12 throw new Error(`${getMethodName(methodName, isSubprocess)} can only be used if the \`ipc\` option is \`true\`.`);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 13–15why, one step back
13 }
14};15

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

lines 16–17why, one step back
16// Better error message when one process does not send/receive messages once the other process has disconnected.
17// This also makes it clear that any buffered messages are lost once either process has disconnected.

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 18–19why, one step back
18// Also when aborting `cancelSignal` after disconnecting the IPC.
19export const validateConnection = (methodName, isSubprocess, isConnected) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 20why, one step back
20 if (!isConnected) {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 21why, one step back
21 throw new Error(`${getMethodName(methodName, isSubprocess)} cannot be used: the ${getOtherProcessName(isSubprocess)} has already exited or disconnected.`);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 22–24why, one step back
22 }
23};24

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 25why, one step back
25// When `getOneMessage()` could not complete due to an early disconnection

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 26why, one step back
26export const throwOnEarlyDisconnect = isSubprocess => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 27why, one step back
27 throw new Error(`${getMethodName('getOneMessage', isSubprocess)} could not complete: the ${getOtherProcessName(isSubprocess)} exited or disconnected.`);

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 28–29why, one step back
28};
29

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 30–31why, one step back
30// When both processes use `sendMessage()` with `strict` at the same time
31export const throwOnStrictDeadlockError = isSubprocess => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 32why, one step back
32 throw new Error(`${getMethodName('sendMessage', isSubprocess)} failed: the ${getOtherProcessName(isSubprocess)} is sending a message too, instead of listening to incoming messages.

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 33–35why, one step back
33This can be fixed by both sending a message and listening to incoming messages at the same time:
3435const [receivedMessage] = await Promise.all([

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 36–37why, one step back
36 ${getMethodName('getOneMessage', isSubprocess)},
37 ${getMethodName('sendMessage', isSubprocess, 'message, {strict: true}')},

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 38–40why, one step back
38]);`);
39};40

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 41why, one step back
41// When the other process used `strict` but the current process had I/O error calling `sendMessage()` for the response

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 42why, one step back
42export const getStrictResponseError = (error, isSubprocess) => new Error(`${getMethodName('sendMessage', isSubprocess)} failed when sending an acknowledgment response to the ${getOtherProcessName(isSubprocess)}.`, {cause: error});

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 43–45why, one step back
43
44// When using `strict` but the other process was not listening for messages45export const throwOnMissingStrict = isSubprocess => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 46why, one step back
46 throw new Error(`${getMethodName('sendMessage', isSubprocess)} failed: the ${getOtherProcessName(isSubprocess)} is not listening to incoming messages.`);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 47–50why, one step back
47};
4849// When using `strict` but the other process disconnected before receiving the message50export const throwOnStrictDisconnect = isSubprocess => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 51why, one step back
51 throw new Error(`${getMethodName('sendMessage', isSubprocess)} failed: the ${getOtherProcessName(isSubprocess)} exited without listening to incoming messages.`);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 52–53why, one step back
52};
53

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 54–55why, one step back
54// When the current process disconnects while the subprocess is listening to `cancelSignal`
55export const getAbortDisconnectError = () => new Error(`\`cancelSignal\` aborted: the ${getOtherProcessName(true)} disconnected.`);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 56why, one step back
56

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 57–58why, one step back
57// When the subprocess uses `cancelSignal` but not the current process
58export const throwOnMissingParent = () => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 59why, one step back
59 throw new Error('`getCancelSignal()` cannot be used without setting the `gracefulCancel` option to `true`.');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 60why, one step back
60};

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 61why, one step back
61

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

line 62why, one step back
62// EPIPE can happen when sending a message to a subprocess that is closing but has not disconnected yet

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 63why, one step back
63export const handleEpipeError = ({error, methodName, isSubprocess}) => {

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

line 64why, one step back
64 if (error.code === 'EPIPE') {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 65why, one step back
65 throw new Error(`${getMethodName(methodName, isSubprocess)} cannot be used: the ${getOtherProcessName(isSubprocess)} is disconnecting.`, {cause: error});

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

lines 66–68why, one step back
66 }
67};68

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 69–70why, one step back
69// Better error message when sending messages which cannot be serialized.
70// Works with both `serialization: 'advanced'` and `serialization: 'json'`.

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 71why, one step back
71export const handleSerializationError = ({error, methodName, isSubprocess, message}) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 72why, one step back
72 if (isSerializationError(error)) {

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 73–82why, one step back
73 throw new Error(`${getMethodName(methodName, isSubprocess)}'s argument type is invalid: the message cannot be serialized: ${serializeInvalidMessage(message)}.`, {cause: error});
74 }75};7677// `String()` throws with objects that have a `null` prototype or a throwing `toString()` method78const serializeInvalidMessage = message => {79 try {80 return String(message);81 } catch {82 return inspect(message);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 83–105why, one step back
83 }
84};8586const isSerializationError = ({code, message}) => SERIALIZATION_ERROR_CODES.has(code)87 || SERIALIZATION_ERROR_MESSAGES.some(serializationErrorMessage => message.includes(serializationErrorMessage));8889// `error.code` set by Node.js when it failed to serialize the message90const SERIALIZATION_ERROR_CODES = new Set([91 // Message is `undefined`92 'ERR_MISSING_ARGS',93 // Message is a function, a bigint, a symbol94 'ERR_INVALID_ARG_TYPE',95]);9697// `error.message` set by Node.js when it failed to serialize the message98const SERIALIZATION_ERROR_MESSAGES = [99 // Message is a promise or a proxy, with `serialization: 'advanced'`100 'could not be cloned',101 // Message has cycles, with `serialization: 'json'`102 'circular structure',103 // Message has cycles inside toJSON(), with `serialization: 'json'`104 'call stack size exceeded',105];

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 106–113why, one step back
106
107const getMethodName = (methodName, isSubprocess, parameters = '') => methodName === 'cancelSignal'108 ? '`cancelSignal`\'s `controller.abort()`'109 : `${getNamespaceName(isSubprocess)}${methodName}(${parameters})`;110111const getNamespaceName = isSubprocess => isSubprocess ? '' : 'subprocess.';112113const getOtherProcessName = isSubprocess => isSubprocess ? 'parent process' : 'subprocess';

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 114–121why, one step back
114
115// When any error arises, we disconnect the IPC.116// Otherwise, it is likely that one of the processes will stop sending/receiving messages.117// This would leave the other process hanging.118export const disconnect = anyProcess => {119 if (anyProcess.connected) {120 anyProcess.disconnect();121 }

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 122why, one step back
122};