incoming.js
This file
Path
lib/ipc/incoming.js
86 lines · 4.1 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 1–3why, one step back1import {once} from 'node:events';2import {scheduler} from 'node:timers/promises';3import {waitForOutgoingMessages} from './outgoing.js';Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 4why, one step back4import {redoAddedReferences} from './reference.js';Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 5why, one step back5import {handleStrictRequest, handleStrictResponse} from './strict.js';Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 6why, one step back6import {handleAbort, abortOnDisconnect} from './graceful.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 7why, one step back7Improve `buffer: false` option with IPC (#1094)ehmickybefore adoptiond8bdaf0
lines 8–25why, one step back8// By default, Node.js buffers `message` events.9// - Buffering happens when there is a `message` event is emitted but there is no handler.10// - As soon as a `message` event handler is set, all buffered `message` events are emitted, emptying the buffer.11// - This happens both in the current process and the subprocess.12// - See https://github.com/nodejs/node/blob/501546e8f37059cd577041e23941b640d0d4d406/lib/internal/child_process.js#L71913// This is helpful. Notably, this allows sending messages to a subprocess that's still initializing.14// However, it has several problems.15// - This works with `events.on()` but not `events.once()` since all buffered messages are emitted at once.16// For example, users cannot call `await getOneMessage()`/`getEachMessage()` multiple times in a row.17// - When a user intentionally starts listening to `message` at a specific point in time, past `message` events are replayed, which might be unexpected.18// - Buffering is unlimited, which might lead to an out-of-memory crash.19// - This does not work well with multiple consumers.20// For example, Execa consumes events with both `result.ipcOutput` and manual IPC calls like `getOneMessage()`.21// Since `result.ipcOutput` reads all incoming messages, no buffering happens for manual IPC calls.22// - Forgetting to setup a `message` listener, or setting it up too late, is a programming mistake.23// The default behavior does not allow users to realize they made that mistake.24// To solve those problems, instead of buffering messages, we debounce them.25// The `message` event so it is emitted at most once per macrotask.Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 26why, one step back26export const onMessage = async ({anyProcess, channel, isSubprocess, ipcEmitter}, wrappedMessage) => {Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 27–28why, one step back27 // `gracefulCancel` messages are only ever sent by the current process to its subprocess.28 // Only the subprocess must interpret them, otherwise a subprocess could abort its parent's own `cancelSignal`, or make it silently drop a message.TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29 if (handleStrictResponse(wrappedMessage, anyProcess) || (isSubprocess && handleAbort(wrappedMessage))) {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 30–32why, one step back30 return;31 }32Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 33–37why, one step back33 if (!INCOMING_MESSAGES.has(anyProcess)) {34 INCOMING_MESSAGES.set(anyProcess, []);35 }3637 const incomingMessages = INCOMING_MESSAGES.get(anyProcess);Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 38why, one step back38 incomingMessages.push(wrappedMessage);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 39–44why, one step back3940 if (incomingMessages.length > 1) {41 return;42 }4344 while (incomingMessages.length > 0) {Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a
line 45why, one step back45 // The message being processed is the oldest one, which is not `wrappedMessage` after the first iterationDebounce `message` events (#1087)ehmickybefore adoptione8bab97
line 46why, one step back46 // eslint-disable-next-line no-await-in-loopFix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a
line 47why, one step back47 await waitForOutgoingMessages(anyProcess, ipcEmitter, incomingMessages[0]);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 48–49why, one step back48 // eslint-disable-next-line no-await-in-loop49 await scheduler.yield();Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 50–62why, one step back5051 // eslint-disable-next-line no-await-in-loop52 const message = await handleStrictRequest({53 wrappedMessage: incomingMessages[0],54 anyProcess,55 channel,56 isSubprocess,57 ipcEmitter,58 });5960 incomingMessages.shift();61 ipcEmitter.emit('message', message);62 ipcEmitter.emit('message:done');Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 63–66why, one step back63 }64};6566// If the `message` event is currently debounced, the `disconnect` event must wait for itAdd `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 67why, one step back67export const onDisconnect = async ({anyProcess, channel, isSubprocess, ipcEmitter, boundOnMessage}) => {Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 68–72why, one step back68 // Like `gracefulCancel` messages, only the disconnection from the parent process aborts `getCancelSignal()`.69 // Otherwise, disconnecting from one of the current process' own subprocesses would abort it.70 if (isSubprocess) {71 abortOnDisconnect();72 }Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 73why, one step back73Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 74–76why, one step back74 const incomingMessages = INCOMING_MESSAGES.get(anyProcess);75 while (incomingMessages?.length > 0) {76 // eslint-disable-next-line no-await-in-loopAdd `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 77why, one step back77 await once(ipcEmitter, 'message:done');Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 78–80why, one step back78 }7980 anyProcess.removeListener('message', boundOnMessage);Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 81why, one step back81 redoAddedReferences(channel, isSubprocess);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 82–84why, one step back82 ipcEmitter.connected = false;83 ipcEmitter.emit('disconnect');84};Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 85–86why, one step back8586const INCOMING_MESSAGES = new WeakMap();