Fix `break` statement with `subprocess.getEachMessage()` (#1082)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 7bf6ac2 · Browse files · 5 files·+53 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+53 added−5 removed
- tests:+42 added−1 removed
- other:+11 added−4 removed
Provenance
- Commit
7143492- Tree
dc50ef7- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7bf6ac2
5 files changed+53 added−5 removed
modified:lib/io/max-buffer.js-2 removed
12234533 unmodified lines39404142444344454641 unmodified linesimport {MaxBufferError} from 'get-stream';import {disconnect} from '../ipc/validation.js';import {getStreamName} from '../utils/standard-stream.js';
// When the `maxBuffer` option is hit, a MaxBufferError is thrown.// The stream is aborted, then specific information is kept for the error message.33 unmodified lines if (ipcOutput.length !== maxBuffer) { return; }
disconnect(subprocess); const error = new MaxBufferError(); error.maxBufferInfo = {fdNumber: 'ipc'}; throw error;};41 unmodified linesmodified:lib/ipc/get-each.js+11 added-2 removed
18 unmodified lines1920212223232425262728293031324 unmodified lines373839403641424344455 unmodified lines5152535455565758596061627 unmodified lines18 unmodified lines });
const controller = new AbortController(); stopOnExit(anyProcess, controller); return iterateOnMessages(anyProcess, shouldAwait, controller); return iterateOnMessages({ anyProcess, isSubprocess, shouldAwait, controller, });};
const stopOnExit = async (anyProcess, controller) => { try {4 unmodified lines controller.abort(); }};
const iterateOnMessages = async function * (anyProcess, shouldAwait, controller) {const iterateOnMessages = async function * ({anyProcess, isSubprocess, shouldAwait, controller}) { try { for await (const [message] of on(anyProcess, 'message', {signal: controller.signal})) { yield message; }5 unmodified lines } } finally { controller.abort();
if (!isSubprocess) { disconnect(anyProcess); }
if (shouldAwait) { await anyProcess; } }7 unmodified linesmodified:test/fixtures/ipc-echo-twice.js+1 added-1 removed
1234566#!/usr/bin/env nodeimport {sendMessage, getOneMessage, exchangeMessage} from '../../index.js';
const message = await getOneMessage();const secondMessage = await exchangeMessage(message);await sendMessage(await secondMessage);await sendMessage(secondMessage);added:test/fixtures/ipc-iterate-send.js+10 added
12345678910#!/usr/bin/env nodeimport {sendMessage, getEachMessage} from '../../index.js';import {foobarString} from '../helpers/input.js';
const iterable = getEachMessage();await sendMessage(foobarString);
for await (const message of iterable) { console.log(message);}modified:test/ipc/get-each.js+31 added
49 unmodified lines50 const {ipcOutput} = await subprocess;51 t.deepEqual(ipcOutput, foobarArray);52 });53 54+test('break in subprocess.getEachMessage() disconnects', async t => {55+ const subprocess = execa('ipc-iterate-send.js', {ipc: true});56+57+ // eslint-disable-next-line no-unreachable-loop58+ for await (const message of subprocess.getEachMessage()) {59+ t.is(message, foobarString);60+ break;61+ }62+63+ const {ipcOutput} = await subprocess;64+ t.deepEqual(ipcOutput, [foobarString]);65+});66+67+const iterateAndError = async (t, subprocess, cause) => {68+ // eslint-disable-next-line no-unreachable-loop69+ for await (const message of subprocess.getEachMessage()) {70+ t.is(message, foobarString);71+ throw cause;72+ }73+};74+75+test('Exceptions in subprocess.getEachMessage() disconnect', async t => {76+ const subprocess = execa('ipc-iterate-send.js', {ipc: true});77+78+ const cause = new Error(foobarString);79+ t.is(await t.throwsAsync(iterateAndError(t, subprocess, cause)), cause);80+81+ const {ipcOutput} = await subprocess;82+ t.deepEqual(ipcOutput, [foobarString]);83+});84+85 const HIGH_CONCURRENCY_COUNT = 10;86 87 test.serial('Can send many messages at once with exports.getEachMessage()', async t => {88 const subprocess = execa('ipc-iterate.js', {ipc: true});92 unmodified lines