send.js
This file
Path
lib/ipc/send.js
96 lines · 2.6 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
line 1why, one step back1import {promisify} from 'node:util';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 2why, one step back2import {copyOptions, checkBooleanOption} from '../utils/options.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 3why, one step back3import {Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 4why, one step back4 validateIpcMethod,Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
line 5why, one step back5 handleEpipeError,Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 6why, one step back6 handleSerializationError,Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 7why, one step back7 disconnect,Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 8why, one step back8} from './validation.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 9why, one step back9import {startSendMessage, endSendMessage} from './outgoing.js';Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 10why, one step back10import {handleSendStrict, waitForStrictResponse} from './strict.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 11–12why, one step back1112// Like `[sub]process.send()` but promise-based.Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 13why, one step back13// We do not `await subprocess` during `.sendMessage()` nor `.getOneMessage()` since those methods are transient.Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 14–15why, one step back14// Users would still need to `await subprocess` after the method is done.15// Also, this would prevent `unhandledRejection` event from being emitted, making it silent.Harden some thingsSindre Sorhusbefore adoption4a0a121
line 16why, one step back16export const sendMessage = ({anyProcess, channel, isSubprocess, ipc}, message, options) => {TweaksSindre Sorhusbefore adoption698a1b8
line 17why, one step back17 const {strict = false} = copyOptions(options);Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 18why, one step back18 checkBooleanOption(strict, 'strict');Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 19why, one step back19 const methodName = 'sendMessage';Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 20why, one step back20 validateIpcMethod({Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 21why, one step back21 methodName,Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 22–25why, one step back22 isSubprocess,23 ipc,24 isConnected: anyProcess.connected,25 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 26why, one step back26Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 27–29why, one step back27 return sendMessageAsync({28 anyProcess,29 channel,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 30why, one step back30 methodName,Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 31–34why, one step back31 isSubprocess,32 message,33 strict,34 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 35–36why, one step back35};36Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 37why, one step back37const sendMessageAsync = async ({anyProcess, channel, methodName, isSubprocess, message, strict}) => {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 38–44why, one step back38 const wrappedMessage = handleSendStrict({39 anyProcess,40 channel,41 isSubprocess,42 message,43 strict,44 });Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
line 45why, one step back45 const outgoingMessagesState = startSendMessage(anyProcess, wrappedMessage, strict);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 46–52why, one step back46 try {47 await sendOneMessage({48 anyProcess,49 methodName,50 isSubprocess,51 wrappedMessage,52 message,Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 53why, one step back53 strict,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 54–61why, one step back54 });55 } catch (error) {56 disconnect(anyProcess);57 throw error;58 } finally {59 endSendMessage(outgoingMessagesState);60 }61};Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 62why, one step back62Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 63why, one step back63// Used internally by `cancelSignal`Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 64why, one step back64export const sendOneMessage = async ({anyProcess, methodName, isSubprocess, wrappedMessage, message, strict = false}) => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 65–66why, one step back65 const sendMethod = getSendMethod(anyProcess);66Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 67why, one step back67 try {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 68why, one step back68 await Promise.all([Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 69–70why, one step back69 // Only messages sent with the `strict` option wait for an acknowledgment response70 strict ? waitForStrictResponse(wrappedMessage, anyProcess, isSubprocess) : undefined,Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 71–72why, one step back71 sendMethod(wrappedMessage),72 ]);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 73why, one step back73 } catch (error) {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 74–80why, one step back74 handleEpipeError({error, methodName, isSubprocess});75 handleSerializationError({76 error,77 methodName,78 isSubprocess,79 message,80 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 81–83why, one step back81 throw error;82 }83};Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
lines 84–96why, one step back8485// [sub]process.send() promisified, memoized86const getSendMethod = anyProcess => {87 if (PROCESS_SEND_METHODS.has(anyProcess)) {88 return PROCESS_SEND_METHODS.get(anyProcess);89 }9091 const sendMethod = promisify(anyProcess.send.bind(anyProcess));92 PROCESS_SEND_METHODS.set(anyProcess, sendMethod);93 return sendMethod;94};9596const PROCESS_SEND_METHODS = new WeakMap();