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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122import {once, on} from 'node:events';import {copyOptions} from '../utils/options.js';import { validateIpcMethod, disconnect, getStrictResponseError,} from './validation.js';import {getIpcEmitter, isConnected} from './forward.js';import {addReference, removeReference} from './reference.js';
export const internalGetEachMessageOptions = Symbol('internalGetEachMessageOptions');
// Like `[sub]process.on('message')` but promise-basedexport const getEachMessage = (subprocessInfo, rawOptions) => { const options = copyOptions(rawOptions); const {reference = true} = options; // Internal callers can stop the IPC listener without exposing cancellation or pipe-awaiting options as public API. const {signal, shouldAwait = !subprocessInfo.isSubprocess} = options[internalGetEachMessageOptions] ?? {};
return loopOnMessages({ ...subprocessInfo, shouldAwait, reference, signal, });};
// Same but used internallyexport const loopOnMessages = ({anyProcess, waitProcess = anyProcess, channel, isSubprocess, ipc, shouldAwait, reference, signal}) => { validateIpcMethod({ methodName: 'getEachMessage', isSubprocess, ipc, isConnected: isConnected(anyProcess), });
addReference(channel, reference); const ipcEmitter = getIpcEmitter(anyProcess, channel, isSubprocess); const controller = new AbortController(); const state = {}; stopOnAbort(signal, controller); stopOnDisconnect(anyProcess, ipcEmitter, controller); abortOnStrictError({ ipcEmitter, isSubprocess, controller, state, }); return iterateOnMessages({ anyProcess, waitProcess, channel, ipcEmitter, isSubprocess, shouldAwait, controller, state, reference, signal, });};
const stopOnAbort = (signal, controller) => { if (signal === undefined) { return; }
if (signal.aborted) { controller.abort(); return; }
// Reuse the iterator controller for listener cleanup when iteration ends before the internal signal aborts. signal.addEventListener('abort', () => { controller.abort(); }, {once: true, signal: controller.signal});};
const stopOnDisconnect = async (anyProcess, ipcEmitter, controller) => { try { await once(ipcEmitter, 'disconnect', {signal: controller.signal}); controller.abort(); } catch {}};
const abortOnStrictError = async ({ipcEmitter, isSubprocess, controller, state}) => { try { const [error] = await once(ipcEmitter, 'strict:error', {signal: controller.signal}); state.error = getStrictResponseError(error, isSubprocess); controller.abort(); } catch {}};
const iterateOnMessages = async function * ({anyProcess, waitProcess, channel, ipcEmitter, isSubprocess, shouldAwait, controller, state, reference, signal}) { try { for await (const [message] of on(ipcEmitter, 'message', {signal: controller.signal})) { throwIfStrictError(state); yield message; } } catch { throwIfStrictError(state); } finally { controller.abort(); removeReference(channel, reference);
// When the internal `signal` stops the iteration, e.g. when a `.pipe()` is canceled, the subprocess keeps running and can still use IPC if (!isSubprocess && !signal?.aborted) { disconnect(anyProcess); }
if (shouldAwait) { await waitProcess; } }};
const throwIfStrictError = ({error}) => { if (error) { throw error; }};