Skip to content
Myr

get-one.js

This file

Path

lib/ipc/get-one.js

98 lines · 2.6 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

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';

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

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 11why, one step back
11

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

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 15why, one step back
15export const getOneMessage = ({anyProcess, channel, isSubprocess, ipc}, rawOptions) => {

TweaksSindre Sorhusbefore adoption698a1b8

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

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

lines 17–19why, one step back
17 const {reference = true, filter} = options;
18 const {signal} = options[internalGetOneMessageOptions] ?? {};19

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

line 20why, one step back
20 validateIpcMethod({

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 21why, one step back
21 methodName: 'getOneMessage',

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

lines 22–23why, one step back
22 isSubprocess,
23 ipc,

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

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

line 25why, one step back
25 });

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 26why, one step back
26

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

lines 27–31why, one step back
27 return getOneMessageAsync({
28 anyProcess,29 channel,30 isSubprocess,31 filter,

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

line 32why, one step back
32 reference,

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

line 33why, one step back
33 signal,

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

line 34why, one step back
34 });

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 35–36why, one step back
35};
36

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

line 37why, one step back
37const getOneMessageAsync = async ({anyProcess, channel, isSubprocess, filter, reference, signal}) => {

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

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

Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c

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

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

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

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

lines 41–42why, one step back
41 stopOnAbort(signal, controller);
42

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 43why, one step back
43 try {

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 44why, one step back
44 return await Promise.race([

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

line 45why, one step back
45 getMessage(ipcEmitter, filter, controller),

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 46why, one step back
46 throwOnDisconnect(ipcEmitter, isSubprocess, controller),

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

line 47why, one step back
47 throwOnStrictError(ipcEmitter, isSubprocess, controller),

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 48why, one step back
48 ]);

Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998

line 49why, one step back
49 } catch (error) {

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 50–54why, one step back
50 // When the internal `signal` stops the read, e.g. when a `.pipe()` is canceled, the subprocess keeps running and can still use IPC
51 if (!signal?.aborted) {52 disconnect(anyProcess);53 }54

Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998

line 55why, one step back
55 throw error;

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 56–57why, one step back
56 } finally {
57 controller.abort();

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

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

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 59why, one step back
59 }

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

lines 60–74why, one step back
60};
6162const stopOnAbort = (signal, controller) => {63 if (signal === undefined) {64 return;65 }6667 if (signal.aborted) {68 controller.abort();69 return;70 }7172 signal.addEventListener('abort', () => {73 controller.abort();74 }, {once: true, signal: controller.signal});

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 75–76why, one step back
75};
76

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

line 77why, one step back
77const getMessage = async (ipcEmitter, filter, {signal}) => {

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 78why, one step back
78 if (filter === undefined) {

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

line 79why, one step back
79 const [message] = await once(ipcEmitter, 'message', {signal});

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 80–82why, one step back
80 return message;
81 }82

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

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

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 84–86why, one step back
84 if (filter(message)) {
85 return message;86 }

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 87–89why, one step back
87 }
88};89

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 90why, one step back
90const throwOnDisconnect = async (ipcEmitter, isSubprocess, {signal}) => {

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

line 91why, one step back
91 await once(ipcEmitter, 'disconnect', {signal});

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 92why, one step back
92 throwOnEarlyDisconnect(isSubprocess);

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

lines 93–97why, one step back
93};
9495const throwOnStrictError = async (ipcEmitter, isSubprocess, {signal}) => {96 const [error] = await once(ipcEmitter, 'strict:error', {signal});97 throw getStrictResponseError(error, isSubprocess);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 98why, one step back
98};