Avoid code duplication with logic related to `maxListeners` (#831)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 100f04f · Browse files · 5 files·+37 added−32 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+37 added−32 removed
- tests:+20 added−17 removed
- other:+17 added−15 removed
Provenance
- Commit
cd2a6c0- Tree
fb2e5d9- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
100f04f
5 files changed+37 added−32 removed
modified:lib/stdio/async.js+4 added-15 removed
1123456798910111257 unmodified lines7071727375767475767879808182838485868777787980819382838485868 unmodified linesimport {addAbortListener} from 'node:events';import {createReadStream, createWriteStream} from 'node:fs';import {Buffer} from 'node:buffer';import {Readable, Writable} from 'node:stream';import {handleInput} from './handle.js';import {pipeStreams} from './pipeline.js';import {TYPE_TO_MESSAGE} from './type.js';import {generatorToDuplexStream, pipeGenerator} from './generator.js';import {isStandardStream} from './utils.js';import {isStandardStream, incrementMaxListeners} from './utils.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleInputAsync = options => handleInput(addPropertiesAsync, options, false);
57 unmodified lines
// Multiple processes might be piping from/to `process.std*` at the same time.// This is not necessarily an error and should not print a `maxListeners` warning.const setStandardStreamMaxListeners = (stream, {signal}) => { if (!isStandardStream(stream)) { return; if (isStandardStream(stream)) { incrementMaxListeners(stream, MAX_LISTENERS_INCREMENT, signal); }
const maxListeners = stream.getMaxListeners(); if (maxListeners === 0 || maxListeners === Number.POSITIVE_INFINITY) { return; }
stream.setMaxListeners(maxListeners + maxListenersIncrement); addAbortListener(signal, () => { stream.setMaxListeners(stream.getMaxListeners() - maxListenersIncrement); });};
// `source.pipe(destination)` adds at most 1 listener for each event.// If `stdin` option is an array, the values might be combined with `merge-streams`.// That library also listens for `source` end, which adds 1 more listener.const maxListenersIncrement = 2;const MAX_LISTENERS_INCREMENT = 2;
// The stream error handling is performed by the piping logic above, which cannot be performed before process spawning.// If the process spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.// We need to create those streams before process spawning, in case their creation fails, e.g. when passing an invalid generator as argument.8 unmodified linesmodified:lib/stdio/utils.js+13 added
1234565 unmodified lines12131415161718192021222324252627import {Buffer} from 'node:buffer';import {addAbortListener} from 'node:events';import process from 'node:process';
export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
5 unmodified lines
export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];
export const incrementMaxListeners = (eventEmitter, maxListenersIncrement, signal) => { const maxListeners = eventEmitter.getMaxListeners(); if (maxListeners === 0 || maxListeners === Number.POSITIVE_INFINITY) { return; }
eventEmitter.setMaxListeners(maxListeners + maxListenersIncrement); addAbortListener(signal, () => { eventEmitter.setMaxListeners(eventEmitter.getMaxListeners() - maxListenersIncrement); });};added:test/helpers/listeners.js+14 added
1234567891011121314import process from 'node:process';
export const assertMaxListeners = t => { let warning; const captureWarning = warningArgument => { warning = warningArgument; };
process.once('warning', captureWarning); return () => { t.is(warning, undefined); process.removeListener('warning', captureWarning); };};modified:test/stdio/async.js+3 added-9 removed
4 unmodified lines5 import {execa} from '../../index.js';6 import {STANDARD_STREAMS} from '../helpers/stdio.js';7 import {foobarString} from '../helpers/input.js';8 import {setFixtureDir} from '../helpers/fixtures-dir.js';9+import {assertMaxListeners} from '../helpers/listeners.js';10 11 setFixtureDir();12 13 const getStandardStreamListeners = stream => Object.fromEntries(stream.eventNames().map(eventName => [eventName, stream.listeners(eventName)]));35 unmodified lines49 t.true(results.every(({stdout}) => stdout === foobarString));50 });51 52 const testMaxListeners = async (t, isMultiple, maxListenersCount) => {52- let warning;53- const captureWarning = warningArgument => {54- warning = warningArgument;55- };53+ const checkMaxListeners = assertMaxListeners(t);54 57- process.on('warning', captureWarning);58-55 for (const standardStream of STANDARD_STREAMS) {56 standardStream.setMaxListeners(maxListenersCount);57 }58 1 unmodified line60 const results = await Promise.all(61 Array.from({length: processesCount}, () => execa('empty.js', getComplexStdio(isMultiple))),62 );63 t.true(results.every(({exitCode}) => exitCode === 0));68- t.is(warning, undefined);64 } finally {65 await setImmediate();66 await setImmediate();67+ checkMaxListeners();68 69 for (const standardStream of STANDARD_STREAMS) {70 t.is(standardStream.getMaxListeners(), maxListenersCount);71 standardStream.setMaxListeners(defaultMaxListeners);72 }77-78- process.off('warning', captureWarning);73 }74 };75 76 test.serial('No warning with maxListeners 1 and ["pipe", "inherit"]', testMaxListeners, false, 1);9 unmodified linesmodified:test/kill.js+3 added-8 removed
5 unmodified lines6 import {pEvent} from 'p-event';7 import isRunning from 'is-running';8 import {execa, execaSync} from '../index.js';9 import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';10+import {assertMaxListeners} from './helpers/listeners.js';11 12 setFixtureDir();13 14 const isWindows = process.platform === 'win32';107 unmodified lines122 t.is(signal, 'SIGKILL');123 });124 125 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {125- let warning;126- const captureWarning = warningArgument => {127- warning = warningArgument;128- };126+ const checkMaxListeners = assertMaxListeners(t);127 130- process.once('warning', captureWarning);131-128 const subprocess = spawnNoKillableSimple();129 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {130 subprocess.kill();131 }1 unmodified line133 const {isTerminated, signal} = await t.throwsAsync(subprocess);134 t.true(isTerminated);135 t.is(signal, 'SIGKILL');136 141- t.is(warning, undefined);142- process.off('warning', captureWarning);137+ checkMaxListeners();138 });139 140 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async t => {141 const subprocess = spawnNoKillableSimple();360 unmodified lines