Skip to content
Myr

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 file

Take it

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

line 1why, one step back
1import {promisify} from 'node:util';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 2why, one step back
2import {copyOptions, checkBooleanOption} from '../utils/options.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 3why, one step back
3import {

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

line 4why, one step back
4 validateIpcMethod,

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

line 5why, one step back
5 handleEpipeError,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 6why, one step back
6 handleSerializationError,

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

line 7why, one step back
7 disconnect,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 8why, one step back
8} from './validation.js';

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 9why, one step back
9import {startSendMessage, endSendMessage} from './outgoing.js';

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

line 10why, one step back
10import {handleSendStrict, waitForStrictResponse} from './strict.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 11–12why, one step back
11
12// Like `[sub]process.send()` but promise-based.

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 13why, one step back
13// 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 back
14// 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 back
16export const sendMessage = ({anyProcess, channel, isSubprocess, ipc}, message, options) => {

TweaksSindre Sorhusbefore adoption698a1b8

line 17why, one step back
17 const {strict = false} = copyOptions(options);

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 18why, one step back
18 checkBooleanOption(strict, 'strict');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 19why, one step back
19 const methodName = 'sendMessage';

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

line 20why, one step back
20 validateIpcMethod({

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 21why, one step back
21 methodName,

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

lines 22–25why, one step back
22 isSubprocess,
23 ipc,24 isConnected: anyProcess.connected,25 });

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 26why, one step back
26

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

lines 27–29why, one step back
27 return sendMessageAsync({
28 anyProcess,29 channel,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 30why, one step back
30 methodName,

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

lines 31–34why, one step back
31 isSubprocess,
32 message,33 strict,34 });

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 35–36why, one step back
35};
36

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 37why, one step back
37const sendMessageAsync = async ({anyProcess, channel, methodName, isSubprocess, message, strict}) => {

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

lines 38–44why, one step back
38 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 back
45 const outgoingMessagesState = startSendMessage(anyProcess, wrappedMessage, strict);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 46–52why, one step back
46 try {
47 await sendOneMessage({48 anyProcess,49 methodName,50 isSubprocess,51 wrappedMessage,52 message,

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 53why, one step back
53 strict,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 54–61why, one step back
54 });
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 back
62

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 63why, one step back
63// Used internally by `cancelSignal`

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 64why, one step back
64export const sendOneMessage = async ({anyProcess, methodName, isSubprocess, wrappedMessage, message, strict = false}) => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 65–66why, one step back
65 const sendMethod = getSendMethod(anyProcess);
66

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 67why, one step back
67 try {

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

line 68why, one step back
68 await Promise.all([

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 69–70why, one step back
69 // Only messages sent with the `strict` option wait for an acknowledgment response
70 strict ? waitForStrictResponse(wrappedMessage, anyProcess, isSubprocess) : undefined,

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

lines 71–72why, one step back
71 sendMethod(wrappedMessage),
72 ]);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 73why, one step back
73 } catch (error) {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 74–80why, one step back
74 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 back
81 throw error;
82 }83};

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 84–96why, one step back
84
85// [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();