Skip to content
Myr

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

8 files+63 added−12 removed

modified:lib/stdio/async.js+2 added-1 removed
modified:lib/stdio/direction.js+1 added-1 removed
modified:lib/stdio/input.js+21 added-4 removed
modified:lib/stdio/sync.js+5 added-4 removed
1import {readFileSync, writeFileSync} from 'node:fs';2import {isStream as isNodeStream} from 'is-stream';3import {handleInput} from './handle.js';4import {TYPE_TO_MESSAGE} from './type.js';5import {bufferToUint8Array} from './utils.js';67// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode8export const handleInputSync = options => {9 const stdioStreams = handleInput(addPropertiesSync, options);
14 unmodified lines
24};2526const 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 lines
39 },40};4142const 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 }4748 options.input = inputs.length === 149 ? inputs[0].value49 : inputs.map(({value}) => serializeInput(value)).join('');50 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');51};5252const serializeInput = value => typeof value === 'string' ? value : new TextDecoder().decode(value);53const serializeInput = ({type, value}) => type === 'string' ? value : new TextDecoder().decode(value);5455// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode56export const pipeOutputSync = (stdioStreams, result) => {57 if (result.output === null) {
17 unmodified lines
modified:lib/stdio/type.js+2 added-1 removed
47 unmodified lines
48 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
modified:test/stdio/input.js+29 added
modified:index.js+2 added-1 removed
14 unmodified lines
15import {getSpawnedResult, makeAllStream} from './lib/stream.js';16import {mergePromise} from './lib/promise.js';17import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';18import {logCommand, verboseDefault} from './lib/verbose.js';19import {bufferToUint8Array} from './lib/stdio/utils.js';2021const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;2223const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {
57 unmodified lines
81};8283const handleOutputSync = (options, value, error) => {84 if (Buffer.isBuffer(value)) {84 value = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);85 value = bufferToUint8Array(value);86 }8788 return handleOutput(options, value, error);89};
242 unmodified lines