Skip to content
Myr

forward.js

This file

Path

lib/ipc/forward.js

56 lines · 1.9 KiB · JavaScript

Last changed by

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

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();

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

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

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

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

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

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

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

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

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

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

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

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

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

line 43why, one step back
43 isSubprocess,

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

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

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

lines 48–49why, one step back
48};
49

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

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

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

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

line 56why, one step back
56};