Validate `input` option's type (#668)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent beabd25 · Browse files · 8 files·+63 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+63 added−12 removed
- tests:+29 added−0 removed
- other:+34 added−12 removed
Provenance
- Commit
65742c6- Tree
5b23aae- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
beabd25
8 files changed+63 added−12 removed
modified:lib/stdio/async.js+2 added-1 removed
10 unmodified lines111213141515161718192032 unmodified lines10 unmodified lines input: { filePath: ({value}) => ({value: createReadStream(value)}), webStream: ({value}) => ({value: Readable.fromWeb(value)}), iterable: ({value}) => ({value: Readable.from(value)}), stringOrBuffer: ({value}) => ({value: Readable.from(ArrayBuffer.isView(value) ? Buffer.from(value) : value)}), string: ({value}) => ({value: Readable.from(value)}), uint8Array: ({value}) => ({value: Readable.from(Buffer.from(value))}), }, output: { filePath: ({value}) => ({value: createWriteStream(value)}), webStream: ({value}) => ({value: Writable.fromWeb(value)}),32 unmodified linesmodified:lib/stdio/direction.js+1 added-1 removed
24 unmodified lines2526272829293031323329 unmodified lines24 unmodified lines
// `stdin`/`stdout`/`stderr` have a known directionconst KNOWN_DIRECTIONS = ['input', 'output', 'output'];
// `stringOrBuffer` type always applies to `stdin`, i.e. does not need to be handled here// `string` and `uint8Array` types always applies to `stdin`, i.e. does not need to be handled hereconst guessStreamDirection = { filePath: () => undefined, iterable: () => 'input', webStream: stdioOption => isWritableStream(stdioOption) ? 'output' : 'input',29 unmodified linesmodified:lib/stdio/input.js+21 added-4 removed
1123456789910101112131415161718192021222324252627282930313216333435363738import {Buffer} from 'node:buffer';import {isStream as isNodeStream} from 'is-stream';import {isReadableStream} from 'is-stream';
// Append the `stdin` option with the `input` and `inputFile` optionsexport const handleInputOptions = ({input, inputFile}) => [ handleInputOption(input), handleInputFileOption(inputFile),].filter(Boolean);
const handleInputOption = input => input && { type: isNodeStream(input) ? 'nodeStream' : 'stringOrBuffer',const handleInputOption = input => input === undefined ? undefined : { type: getType(input), value: input, optionName: 'input', index: 0,};
const getType = input => { if (isReadableStream(input)) { return 'nodeStream'; }
if (typeof input === 'string') { return 'string'; }
if (Object.prototype.toString.call(input) === '[object Uint8Array]' && !Buffer.isBuffer(input)) { return 'uint8Array'; }
throw new Error('The `input` option must be a string, a Uint8Array or a Node.js Readable stream.');};
const handleInputFileOption = inputFile => inputFile && {const handleInputFileOption = inputFile => inputFile === undefined ? undefined : { type: 'filePath', value: inputFile, optionName: 'inputFile', index: 0,};modified:lib/stdio/sync.js+5 added-4 removed
1 import {readFileSync, writeFileSync} from 'node:fs';2 import {isStream as isNodeStream} from 'is-stream';3 import {handleInput} from './handle.js';4 import {TYPE_TO_MESSAGE} from './type.js';5+import {bufferToUint8Array} from './utils.js';6 7 // Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode8 export const handleInputSync = options => {9 const stdioStreams = handleInput(addPropertiesSync, options);14 unmodified lines24 };25 26 const addPropertiesSync = {27 input: {27- filePath: ({value}) => ({value: readFileSync(value, 'utf8'), type: 'stringOrBuffer'}),28+ filePath: ({value}) => ({value: bufferToUint8Array(readFileSync(value)), type: 'uint8Array'}),29 webStream: forbiddenIfSync,30 nodeStream: forbiddenIfSync,31 iterable: forbiddenIfSync,32 native: forbiddenIfStreamSync,6 unmodified lines39 },40 };41 42 const addInputOptionSync = (stdioStreams, options) => {42- const inputs = stdioStreams.filter(({type}) => type === 'stringOrBuffer');43+ const inputs = stdioStreams.filter(({type}) => type === 'string' || type === 'uint8Array');44 if (inputs.length === 0) {45 return;46 }47 48 options.input = inputs.length === 149 ? inputs[0].value49- : inputs.map(({value}) => serializeInput(value)).join('');50+ : inputs.map(stdioStream => serializeInput(stdioStream)).join('');51 };52 52-const serializeInput = value => typeof value === 'string' ? value : new TextDecoder().decode(value);53+const serializeInput = ({type, value}) => type === 'string' ? value : new TextDecoder().decode(value);54 55 // Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode56 export const pipeOutputSync = (stdioStreams, result) => {57 if (result.output === null) {17 unmodified linesmodified:lib/stdio/type.js+2 added-1 removed
47 unmodified lines48 webStream: 'a web stream',49 nodeStream: 'a Node.js stream',50 native: 'any value',51 iterable: 'an iterable',52- stringOrBuffer: 'a string or Uint8Array',52+ string: 'a string',53+ uint8Array: 'a Uint8Array',54 };added:lib/stdio/utils.js+1 added
1export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);modified:test/stdio/input.js+29 added
1234567891011121314151617181910 unmodified lines303132333435363738394041424344454647484950515253545556import {Buffer} from 'node:buffer';import {Writable} from 'node:stream';import test from 'ava';import {execa, execaSync, $} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getScriptSync, identity} from '../helpers/stdio.js';
setFixtureDir();
const textEncoder = new TextEncoder();const binaryFoobar = textEncoder.encode('foobar');const bufferFoobar = Buffer.from(binaryFoobar);const arrayBufferFoobar = binaryFoobar.buffer;const dataViewFoobar = new DataView(arrayBufferFoobar);const uint16ArrayFoobar = new Uint16Array(arrayBufferFoobar);
const testInput = async (t, input, execaMethod) => { const {stdout} = await execaMethod('stdin.js', {input}); t.is(stdout, 'foobar');10 unmodified lines};
test('input option can be used with $', testInputScript, identity);test('input option can be used with $.sync', testInputScript, getScriptSync);
const testInvalidInput = async (t, input, execaMethod) => { t.throws(() => { execaMethod('noop.js', {input}); }, {message: /a string, a Uint8Array/});};
test('input option cannot be a Buffer', testInvalidInput, bufferFoobar, execa);test('input option cannot be an ArrayBuffer', testInvalidInput, arrayBufferFoobar, execa);test('input option cannot be a DataView', testInvalidInput, dataViewFoobar, execa);test('input option cannot be a Uint16Array', testInvalidInput, uint16ArrayFoobar, execa);test('input option cannot be 0', testInvalidInput, 0, execa);test('input option cannot be false', testInvalidInput, false, execa);test('input option cannot be null', testInvalidInput, null, execa);test('input option cannot be a non-Readable stream', testInvalidInput, new Writable(), execa);test('input option cannot be a Buffer - sync', testInvalidInput, bufferFoobar, execaSync);test('input option cannot be an ArrayBuffer - sync', testInvalidInput, arrayBufferFoobar, execaSync);test('input option cannot be a DataView - sync', testInvalidInput, dataViewFoobar, execaSync);test('input option cannot be a Uint16Array - sync', testInvalidInput, uint16ArrayFoobar, execaSync);test('input option cannot be 0 - sync', testInvalidInput, 0, execaSync);test('input option cannot be false - sync', testInvalidInput, false, execaSync);test('input option cannot be null - sync', testInvalidInput, null, execaSync);test('input option cannot be a non-Readable stream - sync', testInvalidInput, new Writable(), execaSync);modified:index.js+2 added-1 removed
14 unmodified lines15 import {getSpawnedResult, makeAllStream} from './lib/stream.js';16 import {mergePromise} from './lib/promise.js';17 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';18 import {logCommand, verboseDefault} from './lib/verbose.js';19+import {bufferToUint8Array} from './lib/stdio/utils.js';20 21 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;22 23 const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {57 unmodified lines81 };82 83 const handleOutputSync = (options, value, error) => {84 if (Buffer.isBuffer(value)) {84- value = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);85+ value = bufferToUint8Array(value);86 }87 88 return handleOutput(options, value, error);89 };242 unmodified lines