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 fileTake it
Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 1–2why, one step back1import {inspect} from 'node:util';2Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 3–8why, one step back3// Validate the IPC channel is connected before receiving/sending messages4export const validateIpcMethod = ({methodName, isSubprocess, ipc, isConnected}) => {5 validateIpcOption(methodName, isSubprocess, ipc);6 validateConnection(methodName, isSubprocess, isConnected);7};8Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 9why, one step back9// Better error message when forgetting to set `ipc: true` and using the IPC methodsAdd `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 10why, one step back10const validateIpcOption = (methodName, isSubprocess, ipc) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 11why, one step back11 if (!ipc) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 12why, one step back12 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 back13 }14};15Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
lines 16–17why, one step back16// 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 back18// 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 back20 if (!isConnected) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 21why, one step back21 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 back22 }23};24Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 25why, one step back25// When `getOneMessage()` could not complete due to an early disconnectionDebounce `message` events (#1087)ehmickybefore adoptione8bab97
line 26why, one step back26export const throwOnEarlyDisconnect = isSubprocess => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 27why, one step back27 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 back28};29Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
lines 30–31why, one step back30// When both processes use `sendMessage()` with `strict` at the same time31export const throwOnStrictDeadlockError = isSubprocess => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 32why, one step back32 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 back33This 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 back36 ${getMethodName('getOneMessage', isSubprocess)},37 ${getMethodName('sendMessage', isSubprocess, 'message, {strict: true}')},Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
lines 38–40why, one step back38]);`);39};40Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 41why, one step back41// When the other process used `strict` but the current process had I/O error calling `sendMessage()` for the responseAdd `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 42why, one step back42export 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 back4344// 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 back46 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 back47};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 back51 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 back52};53Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 54–55why, one step back54// 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 back56Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 57–58why, one step back57// When the subprocess uses `cancelSignal` but not the current process58export const throwOnMissingParent = () => {Harden some thingsSindre Sorhusbefore adoption4a0a121
line 59why, one step back59 throw new Error('`getCancelSignal()` cannot be used without setting the `gracefulCancel` option to `true`.');Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 60why, one step back60};Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 61why, one step back61Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
line 62why, one step back62// EPIPE can happen when sending a message to a subprocess that is closing but has not disconnected yetAdd `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 63why, one step back63export const handleEpipeError = ({error, methodName, isSubprocess}) => {Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
line 64why, one step back64 if (error.code === 'EPIPE') {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 65why, one step back65 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 back66 }67};68Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 69–70why, one step back69// 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 back71export const handleSerializationError = ({error, methodName, isSubprocess, message}) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 72why, one step back72 if (isSerializationError(error)) {Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 73–82why, one step back73 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 back83 }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 back106107const 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 back114115// 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 back122};