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 fileTake it
Improve `buffer: false` option with IPC (#1094)ehmickybefore adoptiond8bdaf0
line 1why, one step back1import {EventEmitter} from 'node:events';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 2–3why, one step back2import {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 back45// 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 back7// This also allows debouncing the `message` event.Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 8why, one step back8export const getIpcEmitter = (anyProcess, channel, isSubprocess) => {Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
lines 9–15why, one step back9 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 back16 // 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 back18 ipcEmitter.connected = true;Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 19why, one step back19 IPC_EMITTERS.set(anyProcess, ipcEmitter);Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 20–25why, one step back20 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 back26 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#L721Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 34why, one step back34const forwardEvents = ({ipcEmitter, anyProcess, channel, isSubprocess}) => {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 35–40why, one step back35 const boundOnMessage = onMessage.bind(undefined, {36 anyProcess,37 channel,38 isSubprocess,39 ipcEmitter,40 });Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 41why, one step back41 anyProcess.on('message', boundOnMessage);Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 42–44why, one step back42 anyProcess.once('disconnect', onDisconnect.bind(undefined, {43 anyProcess,44 channel,Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 45why, one step back45 isSubprocess,Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 46–49why, one step back46 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 back50};51Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 52–55why, one step back52// Check whether there might still be some `message` events to receive53export const isConnected = anyProcess => {54 const ipcEmitter = IPC_EMITTERS.get(anyProcess);55 return ipcEmitter === undefinedAdd `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 56why, one step back56 ? anyProcess.channel !== undefined && anyProcess.channel !== nullDebounce `message` events (#1087)ehmickybefore adoptione8bab97
line 57why, one step back57 : ipcEmitter.connected;Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 58why, one step back58};