Skip to content
Myr

get-each.js

This file

Path

lib/ipc/get-each.js

121 lines · 3.2 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 3–7why, one step back
3import {
4 validateIpcMethod,5 disconnect,6 getStrictResponseError,7} from './validation.js';

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 8–9why, one step back
8import {getIpcEmitter, isConnected} from './forward.js';
9import {addReference, removeReference} from './reference.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 10why, one step back
10

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

lines 11–12why, one step back
11export const internalGetEachMessageOptions = Symbol('internalGetEachMessageOptions');
12

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 13why, one step back
13// Like `[sub]process.on('message')` but promise-based

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 14why, one step back
14export const getEachMessage = (subprocessInfo, rawOptions) => {

TweaksSindre Sorhusbefore adoption698a1b8

line 15why, one step back
15 const options = copyOptions(rawOptions);

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

lines 16–18why, one step back
16 const {reference = true} = options;
17 // Internal callers can stop the IPC listener without exposing cancellation or pipe-awaiting options as public API.18 const {signal, shouldAwait = !subprocessInfo.isSubprocess} = options[internalGetEachMessageOptions] ?? {};

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 19why, one step back
19

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

lines 20–27why, one step back
20 return loopOnMessages({
21 ...subprocessInfo,22 shouldAwait,23 reference,24 signal,25 });26};27

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 28why, one step back
28// Same but used internally

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

line 29why, one step back
29export const loopOnMessages = ({anyProcess, waitProcess = anyProcess, channel, isSubprocess, ipc, shouldAwait, reference, signal}) => {

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

lines 30–33why, one step back
30 validateIpcMethod({
31 methodName: 'getEachMessage',32 isSubprocess,33 ipc,

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 34why, one step back
34 isConnected: isConnected(anyProcess),

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

line 35why, one step back
35 });

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 36why, one step back
36

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

line 37why, one step back
37 addReference(channel, reference);

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

line 38why, one step back
38 const ipcEmitter = getIpcEmitter(anyProcess, channel, isSubprocess);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 39why, one step back
39 const controller = new AbortController();

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

line 40why, one step back
40 const state = {};

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

line 41why, one step back
41 stopOnAbort(signal, controller);

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

line 42why, one step back
42 stopOnDisconnect(anyProcess, ipcEmitter, controller);

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

lines 43–48why, one step back
43 abortOnStrictError({
44 ipcEmitter,45 isSubprocess,46 controller,47 state,48 });

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 49–50why, one step back
49 return iterateOnMessages({
50 anyProcess,

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

line 51why, one step back
51 waitProcess,

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

line 52why, one step back
52 channel,

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

line 53why, one step back
53 ipcEmitter,

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 54–56why, one step back
54 isSubprocess,
55 shouldAwait,56 controller,

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

line 57why, one step back
57 state,

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

line 58why, one step back
58 reference,

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 59why, one step back
59 signal,

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

line 60why, one step back
60 });

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

lines 61–76why, one step back
61};
6263const stopOnAbort = (signal, controller) => {64 if (signal === undefined) {65 return;66 }6768 if (signal.aborted) {69 controller.abort();70 return;71 }7273 // Reuse the iterator controller for listener cleanup when iteration ends before the internal signal aborts.74 signal.addEventListener('abort', () => {75 controller.abort();76 }, {once: true, signal: controller.signal});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 77–78why, one step back
77};
78

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

line 79why, one step back
79const stopOnDisconnect = async (anyProcess, ipcEmitter, controller) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 80why, one step back
80 try {

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

line 81why, one step back
81 await once(ipcEmitter, 'disconnect', {signal: controller.signal});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 82why, one step back
82 controller.abort();

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

line 83why, one step back
83 } catch {}

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 84–85why, one step back
84};
85

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

line 86why, one step back
86const abortOnStrictError = async ({ipcEmitter, isSubprocess, controller, state}) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 87why, one step back
87 try {

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

lines 88–93why, one step back
88 const [error] = await once(ipcEmitter, 'strict:error', {signal: controller.signal});
89 state.error = getStrictResponseError(error, isSubprocess);90 controller.abort();91 } catch {}92};93

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 94why, one step back
94const iterateOnMessages = async function * ({anyProcess, waitProcess, channel, ipcEmitter, isSubprocess, shouldAwait, controller, state, reference, signal}) {

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

line 95why, one step back
95 try {

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

line 96why, one step back
96 for await (const [message] of on(ipcEmitter, 'message', {signal: controller.signal})) {

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

line 97why, one step back
97 throwIfStrictError(state);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 98–99why, one step back
98 yield message;
99 }

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

lines 100–102why, one step back
100 } catch {
101 throwIfStrictError(state);102 } finally {

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 103why, one step back
103 controller.abort();

Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6

line 104why, one step back
104 removeReference(channel, reference);

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

line 105why, one step back
105

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 106–107why, one step back
106 // When the internal `signal` stops the iteration, e.g. when a `.pipe()` is canceled, the subprocess keeps running and can still use IPC
107 if (!isSubprocess && !signal?.aborted) {

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 108–109why, one step back
108 disconnect(anyProcess);
109 }

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

lines 110–111why, one step back
110
111 if (shouldAwait) {

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

line 112why, one step back
112 await waitProcess;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 113why, one step back
113 }

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

lines 114–119why, one step back
114 }
115};116117const throwIfStrictError = ({error}) => {118 if (error) {119 throw error;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 120–121why, one step back
120 }
121};