Skip to content
Myr

forward.js

This file

Path

lib/ipc/forward.js

58 lines · 2.1 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

Improve `buffer: false` option with IPC (#1094)ehmickybefore adoptiond8bdaf0

line 1why, one step back
1import {EventEmitter} from 'node:events';

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 2–3why, one step back
2import {onMessage, onDisconnect} from './incoming.js';
3import {undoAddedReferences} from './reference.js';

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

lines 4–6why, one step back
4
5// Forward the `message` and `disconnect` events from the process and subprocess to a proxy emitter.6// This prevents the `error` event from stopping IPC.

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 7why, one step back
7// This also allows debouncing the `message` event.

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

line 8why, one step back
8export const getIpcEmitter = (anyProcess, channel, isSubprocess) => {

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

lines 9–15why, one step back
9 if (IPC_EMITTERS.has(anyProcess)) {
10 return IPC_EMITTERS.get(anyProcess);11 }1213 // Use an `EventEmitter`, like the `process` that is being proxied14 // eslint-disable-next-line unicorn/prefer-event-target15 const ipcEmitter = new EventEmitter();

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 16–17why, one step back
16 // Each pending `getOneMessage()` or `getEachMessage()` call adds listeners, which are removed once it ends. Users cannot reach this emitter to increase its maximum number of listeners.
17 ipcEmitter.setMaxListeners(0);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 18why, one step back
18 ipcEmitter.connected = true;

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

line 19why, one step back
19 IPC_EMITTERS.set(anyProcess, ipcEmitter);

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

lines 20–25why, one step back
20 forwardEvents({
21 ipcEmitter,22 anyProcess,23 channel,24 isSubprocess,25 });

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

lines 26–33why, one step back
26 return ipcEmitter;
27};2829const IPC_EMITTERS = new WeakMap();3031// The `message` and `disconnect` events are buffered in the subprocess until the first listener is setup.32// However, unbuffering happens after one tick, so this give enough time for the caller to setup the listener on the proxy emitter first.33// See https://github.com/nodejs/node/blob/2aaeaa863c35befa2ebaa98fb7737ec84df4d8e9/lib/internal/child_process.js#L721

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

line 34why, one step back
34const forwardEvents = ({ipcEmitter, anyProcess, channel, isSubprocess}) => {

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

lines 35–40why, one step back
35 const boundOnMessage = onMessage.bind(undefined, {
36 anyProcess,37 channel,38 isSubprocess,39 ipcEmitter,40 });

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 41why, one step back
41 anyProcess.on('message', boundOnMessage);

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

lines 42–44why, one step back
42 anyProcess.once('disconnect', onDisconnect.bind(undefined, {
43 anyProcess,44 channel,

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

line 45why, one step back
45 isSubprocess,

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

lines 46–49why, one step back
46 ipcEmitter,
47 boundOnMessage,48 }));49 undoAddedReferences(channel, isSubprocess);

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

lines 50–51why, one step back
50};
51

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 52–55why, one step back
52// Check whether there might still be some `message` events to receive
53export const isConnected = anyProcess => {54 const ipcEmitter = IPC_EMITTERS.get(anyProcess);55 return ipcEmitter === undefined

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 56why, one step back
56 ? anyProcess.channel !== undefined && anyProcess.channel !== null

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 57why, one step back
57 : ipcEmitter.connected;

Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a

line 58why, one step back
58};