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
Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 1why, one step back1import {once, on} from 'node:events';TweaksSindre Sorhusbefore adoption698a1b8
line 2why, one step back2import {copyOptions} from '../utils/options.js';Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 3–7why, one step back3import {4 validateIpcMethod,5 disconnect,6 getStrictResponseError,7} from './validation.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 8–9why, one step back8import {getIpcEmitter, isConnected} from './forward.js';9import {addReference, removeReference} from './reference.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 10why, one step back10Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 11–12why, one step back11export const internalGetEachMessageOptions = Symbol('internalGetEachMessageOptions');12Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 13why, one step back13// Like `[sub]process.on('message')` but promise-basedHarden some thingsSindre Sorhusbefore adoption4a0a121
line 14why, one step back14export const getEachMessage = (subprocessInfo, rawOptions) => {TweaksSindre Sorhusbefore adoption698a1b8
line 15why, one step back15 const options = copyOptions(rawOptions);Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 16–18why, one step back16 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 back19Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 20–27why, one step back20 return loopOnMessages({21 ...subprocessInfo,22 shouldAwait,23 reference,24 signal,25 });26};27Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 28why, one step back28// Same but used internallyAdd `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 29why, one step back29export const loopOnMessages = ({anyProcess, waitProcess = anyProcess, channel, isSubprocess, ipc, shouldAwait, reference, signal}) => {Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 30–33why, one step back30 validateIpcMethod({31 methodName: 'getEachMessage',32 isSubprocess,33 ipc,Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 34why, one step back34 isConnected: isConnected(anyProcess),Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 35why, one step back35 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 36why, one step back36Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 37why, one step back37 addReference(channel, reference);Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 38why, one step back38 const ipcEmitter = getIpcEmitter(anyProcess, channel, isSubprocess);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 39why, one step back39 const controller = new AbortController();Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 40why, one step back40 const state = {};Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 41why, one step back41 stopOnAbort(signal, controller);Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 42why, one step back42 stopOnDisconnect(anyProcess, ipcEmitter, controller);Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 43–48why, one step back43 abortOnStrictError({44 ipcEmitter,45 isSubprocess,46 controller,47 state,48 });Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 49–50why, one step back49 return iterateOnMessages({50 anyProcess,Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 51why, one step back51 waitProcess,Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 52why, one step back52 channel,Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 53why, one step back53 ipcEmitter,Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 54–56why, one step back54 isSubprocess,55 shouldAwait,56 controller,Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 57why, one step back57 state,Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 58why, one step back58 reference,Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 59why, one step back59 signal,Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
line 60why, one step back60 });Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 61–76why, one step back61};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 back77};78Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 79why, one step back79const stopOnDisconnect = async (anyProcess, ipcEmitter, controller) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 80why, one step back80 try {Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 81why, one step back81 await once(ipcEmitter, 'disconnect', {signal: controller.signal});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 82why, one step back82 controller.abort();Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 83why, one step back83 } catch {}Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 84–85why, one step back84};85Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 86why, one step back86const abortOnStrictError = async ({ipcEmitter, isSubprocess, controller, state}) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 87why, one step back87 try {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 88–93why, one step back88 const [error] = await once(ipcEmitter, 'strict:error', {signal: controller.signal});89 state.error = getStrictResponseError(error, isSubprocess);90 controller.abort();91 } catch {}92};93Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 94why, one step back94const 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 back95 try {Do not disconnect IPC on `error` event on process (#1086)ehmickybefore adoptiond6f5d9a
line 96why, one step back96 for await (const [message] of on(ipcEmitter, 'message', {signal: controller.signal})) {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 97why, one step back97 throwIfStrictError(state);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 98–99why, one step back98 yield message;99 }Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 100–102why, one step back100 } catch {101 throwIfStrictError(state);102 } finally {Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 103why, one step back103 controller.abort();Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
line 104why, one step back104 removeReference(channel, reference);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
line 105why, one step back105Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 106–107why, one step back106 // When the internal `signal` stops the iteration, e.g. when a `.pipe()` is canceled, the subprocess keeps running and can still use IPC107 if (!isSubprocess && !signal?.aborted) {Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 108–109why, one step back108 disconnect(anyProcess);109 }Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
lines 110–111why, one step back110111 if (shouldAwait) {Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 112why, one step back112 await waitProcess;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 113why, one step back113 }Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 114–119why, one step back114 }115};116117const throwIfStrictError = ({error}) => {118 if (error) {119 throw error;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 120–121why, one step back120 }121};