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 fileTake it
Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 1why, one step back1import {once, on} from 'node:events';TweaksSindre Sorhusbefore adoption698a1b8
line 2why, one step back2import {copyOptions} from '../utils/options.js';Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 3–8why, one step back3import {4 validateIpcMethod,5 throwOnEarlyDisconnect,6 disconnect,7 getStrictResponseError,8} from './validation.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 9–10why, one step back9import {getIpcEmitter, isConnected} from './forward.js';10import {addReference, removeReference} from './reference.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 11why, one step back11Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 12–13why, one step back12export const internalGetOneMessageOptions = Symbol('internalGetOneMessageOptions');13Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 14why, one step back14// Like `[sub]process.once('message')` but promise-basedHarden some thingsSindre Sorhusbefore adoption4a0a121
line 15why, one step back15export const getOneMessage = ({anyProcess, channel, isSubprocess, ipc}, rawOptions) => {TweaksSindre Sorhusbefore adoption698a1b8
line 16why, one step back16 const options = copyOptions(rawOptions);Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 17–19why, one step back17 const {reference = true, filter} = options;18 const {signal} = options[internalGetOneMessageOptions] ?? {};19Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 20why, one step back20 validateIpcMethod({Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 21why, one step back21 methodName: 'getOneMessage',Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 22–23why, one step back22 isSubprocess,23 ipc,Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 24why, one step back24 isConnected: isConnected(anyProcess),Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 25why, one step back25 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 26why, one step back26Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 27–31why, one step back27 return getOneMessageAsync({28 anyProcess,29 channel,30 isSubprocess,31 filter,Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 32why, one step back32 reference,Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 33why, one step back33 signal,Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 34why, one step back34 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 35–36why, one step back35};36Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 37why, one step back37const getOneMessageAsync = async ({anyProcess, channel, isSubprocess, filter, reference, signal}) => {Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 38why, one step back38 addReference(channel, reference);Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 39why, one step back39 const ipcEmitter = getIpcEmitter(anyProcess, channel, isSubprocess);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 40why, one step back40 const controller = new AbortController();Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 41–42why, one step back41 stopOnAbort(signal, controller);42Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 43why, one step back43 try {Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 44why, one step back44 return await Promise.race([Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 45why, one step back45 getMessage(ipcEmitter, filter, controller),Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 46why, one step back46 throwOnDisconnect(ipcEmitter, isSubprocess, controller),Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 47why, one step back47 throwOnStrictError(ipcEmitter, isSubprocess, controller),Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 48why, one step back48 ]);Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
line 49why, one step back49 } catch (error) {Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 50–54why, one step back50 // When the internal `signal` stops the read, e.g. when a `.pipe()` is canceled, the subprocess keeps running and can still use IPC51 if (!signal?.aborted) {52 disconnect(anyProcess);53 }54Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
line 55why, one step back55 throw error;Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 56–57why, one step back56 } finally {57 controller.abort();Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 58why, one step back58 removeReference(channel, reference);Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 59why, one step back59 }Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 60–74why, one step back60};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 back75};76Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 77why, one step back77const getMessage = async (ipcEmitter, filter, {signal}) => {Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 78why, one step back78 if (filter === undefined) {Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 79why, one step back79 const [message] = await once(ipcEmitter, 'message', {signal});Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 80–82why, one step back80 return message;81 }82Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 83why, one step back83 for await (const [message] of on(ipcEmitter, 'message', {signal})) {Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 84–86why, one step back84 if (filter(message)) {85 return message;86 }Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 87–89why, one step back87 }88};89Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 90why, one step back90const throwOnDisconnect = async (ipcEmitter, isSubprocess, {signal}) => {Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 91why, one step back91 await once(ipcEmitter, 'disconnect', {signal});Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 92why, one step back92 throwOnEarlyDisconnect(isSubprocess);Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 93–97why, one step back93};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 back98};