Fix processes not exiting on `inputFile` validation (#588)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 3ba748d · Browse files · 4 files·+20 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−34 removed
- tests:+2 added−0 removed
- other:+18 added−34 removed
Provenance
- Commit
5b1a1c7- Tree
faf9803- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
3ba748d
4 files changed+20 added−34 removed
modified:lib/stream.js+12 added-30 removed
2 unmodified lines3456787891011121314151617181920211323241415162617181920302122233334353637383940412443442526274628293031505132333453353637383966 unmodified lines2 unmodified linesimport {isStream} from 'is-stream';import getStream, {getStreamAsBuffer} from 'get-stream';import mergeStreams from '@sindresorhus/merge-streams';
const validateInputOptions = input => { if (input !== undefined) {export const validateInputOptions = ({input, inputFile}) => { if (input !== undefined && inputFile !== undefined) { throw new TypeError('The `input` and `inputFile` options cannot be both set.'); }};
const getInputSync = ({input, inputFile}) => { if (typeof inputFile !== 'string') { return input; }
validateInputOptions(input); return readFileSync(inputFile);};
// `input` and `inputFile` option in sync modeexport const handleInputSync = options => { const input = getInputSync(options);export const handleInputSync = ({input, inputFile}) => { const inputOption = typeof inputFile === 'string' ? readFileSync(inputFile) : input;
if (isStream(input)) { if (isStream(inputOption)) { throw new TypeError('The `input` option cannot be a stream in sync mode'); }
return input; return inputOption;};
const getInput = ({input, inputFile}) => { if (typeof inputFile !== 'string') { return input; }
validateInputOptions(input); return createReadStream(inputFile);};
// `input` and `inputFile` option in async modeexport const handleInput = (spawned, options) => { const input = getInput(options);export const handleInput = (spawned, {input, inputFile}) => { const inputOption = typeof inputFile === 'string' ? createReadStream(inputFile) : input;
if (input === undefined) { if (inputOption === undefined) { return; }
if (isStream(input)) { input.pipe(spawned.stdin); if (isStream(inputOption)) { inputOption.pipe(spawned.stdin); } else { spawned.stdin.end(input); spawned.stdin.end(inputOption); }};
// `all` interleaves `stdout` and `stderr`66 unmodified linesmodified:test/node.js+2 added
98 unmodified lines99 subprocess.send('ping');100 101 const message = await pEvent(subprocess, 'message');102 t.is(message, 'pong');103+104+ subprocess.kill();105 });modified:index.js+5 added-3 removed
8 unmodified lines9 import {makeError} from './lib/error.js';10 import {normalizeStdio, normalizeStdioNode} from './lib/stdio.js';11 import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} from './lib/kill.js';12 import {addPipeMethods} from './lib/pipe.js';13-import {handleInput, getSpawnedResult, makeAllStream, handleInputSync} from './lib/stream.js';13+import {validateInputOptions, handleInput, getSpawnedResult, makeAllStream, handleInputSync} from './lib/stream.js';14 import {mergePromise, getSpawnedPromise} from './lib/promise.js';15 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';16 import {logCommand, verboseDefault} from './lib/verbose.js';17 63 unmodified lines81 const escapedCommand = getEscapedCommand(file, args);82 logCommand(escapedCommand, parsed.options);83 84 validateTimeout(parsed.options);85+ validateInputOptions(parsed.options);86 87 let spawned;88 try {89 spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);84 unmodified lines174 const command = joinCommand(file, args);175 const escapedCommand = getEscapedCommand(file, args);176 logCommand(escapedCommand, parsed.options);177 177- const input = handleInputSync(parsed.options);178+ validateInputOptions(parsed.options);179+ const inputOption = handleInputSync(parsed.options);180 181 let result;182 try {181- result = childProcess.spawnSync(parsed.file, parsed.args, {...parsed.options, input});183+ result = childProcess.spawnSync(parsed.file, parsed.args, {...parsed.options, input: inputOption});184 } catch (error) {185 throw makeError({186 error,187 stdout: '',124 unmodified linesmodified:package.json+1 added-1 removed
55 unmodified lines56 "strip-final-newline": "^4.0.0"57 },58 "devDependencies": {59 "@types/node": "^20.8.9",60- "ava": "^5.3.1",60+ "ava": "^6.0.1",61 "c8": "^8.0.1",62 "get-node": "^15.0.0",63 "is-running": "^2.1.0",64 "p-event": "^6.0.0",26 unmodified lines