Split `input`/`inputFile` options to their own file (#629)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 1afeb81 · Browse files · 5 files·+61 added−67 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+61 added−67 removed
- other:+61 added−67 removed
Provenance
- Commit
0b8e297- Tree
70d2521- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
1afeb81
5 files changed+61 added−67 removed
modified:lib/stdio/async.js+1 added-1 removed
1233456739 unmodified linesimport {createReadStream, createWriteStream} from 'node:fs';import {Readable, Writable} from 'node:stream';import {handleInput} from './input.js';import {handleInput} from './handle.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleInputAsync = options => handleInput(addPropertiesAsync, options);
39 unmodified linesadded:lib/stdio/handle.js+55 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';import {normalizeStdio} from './normalize.js';import {handleInputOption, handleInputFileOption} from './input.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options) => { const stdio = normalizeStdio(options); const stdioArray = arrifyStdio(stdio); const stdioStreams = stdioArray.map((stdioOption, index) => getStdioStream(stdioOption, index, addProperties, options)); options.stdio = transformStdio(stdio, stdioStreams); return stdioStreams;};
const arrifyStdio = (stdio = []) => Array.isArray(stdio) ? stdio : [stdio, stdio, stdio];
const getStdioStream = (stdioOption, index, addProperties, {input, inputFile}) => { let stdioStream = { type: getStdioOptionType(stdioOption), value: stdioOption, optionName: OPTION_NAMES[index], direction: index === 0 ? 'input' : 'output', };
stdioStream = handleInputOption(stdioStream, index, input); stdioStream = handleInputFileOption(stdioStream, index, inputFile, input);
validateFileStdio(stdioStream);
return { ...stdioStream, ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream), };};
const OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateFileStdio = ({type, value, optionName}) => { if (isRegularUrl(value)) { throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`); }
if (isUnknownStdioString(type, value)) { throw new TypeError(`The \`${optionName}: filePath\` option must either be an absolute file path or start with \`.\`.`); }};
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = (stdio, stdioStreams) => Array.isArray(stdio) ? stdio.map((stdioItem, index) => transformStdioItem(stdioItem, index, stdioStreams)) : stdio;
const transformStdioItem = (stdioItem, index, stdioStreams) => stdioStreams[index].type !== 'native' && stdioItem !== 'overlapped' ? 'pipe' : stdioItem;modified:lib/stdio/input.js+2 added-64 removed
1232567891011121314151617181920212223242526272829303132333435363738394041424344454647349456783 unmodified lines121314156116171819202 unmodified lines2324252672272829302 unmodified lines3334353683848586878889909192939495969798import {isStream as isNodeStream} from 'is-stream';import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';import {normalizeStdio} from './normalize.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options) => { const stdio = normalizeStdio(options); const stdioArray = arrifyStdio(stdio); const stdioStreams = stdioArray.map((stdioOption, index) => getStdioStream(stdioOption, index, addProperties, options)); options.stdio = transformStdio(stdio, stdioStreams); return stdioStreams;};
const arrifyStdio = (stdio = []) => Array.isArray(stdio) ? stdio : [stdio, stdio, stdio];
const getStdioStream = (stdioOption, index, addProperties, {input, inputFile}) => { let stdioStream = { type: getStdioOptionType(stdioOption), value: stdioOption, optionName: OPTION_NAMES[index], direction: index === 0 ? 'input' : 'output', }; validateFileStdio(stdioStream);
stdioStream = handleInputOption(stdioStream, index, input); stdioStream = handleInputFileOption(stdioStream, index, inputFile, input);
return { ...stdioStream, ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream), };};
const OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateFileStdio = ({type, value, optionName}) => { if (type !== 'native') { return; }
validateRegularUrl(value, optionName);
if (isUnknownStdioString(value)) { throw new TypeError(`The \`${optionName}: filePath\` option must either be an absolute file path or start with \`.\`.`); }};
// Override the `stdin` option with the `input` optionconst handleInputOption = (stdioStream, index, input) => {export const handleInputOption = (stdioStream, index, input) => { if (input === undefined || index !== 0) { return stdioStream; }
3 unmodified lines return {...stdioStream, value: input, type, optionName};};
// Override the `stdin` option with the `inputFile` optionconst handleInputFileOption = (stdioStream, index, inputFile, input) => {export const handleInputFileOption = (stdioStream, index, inputFile, input) => { if (inputFile === undefined || index !== 0) { return stdioStream; }
2 unmodified lines }
const optionName = 'inputFile'; validateInputOption(stdioStream.value, optionName); validateRegularUrl(inputFile, optionName); return {...stdioStream, value: inputFile, type: 'filePath', optionName};};
const validateInputOption = (value, optionName) => {2 unmodified lines }};
const CAN_USE_INPUT = new Set([undefined, null, 'overlapped', 'pipe']);
const validateRegularUrl = (value, optionName) => { if (isRegularUrl(value)) { throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`); }};
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = (stdio, stdioStreams) => Array.isArray(stdio) ? stdio.map((stdioItem, index) => transformStdioItem(stdioItem, index, stdioStreams)) : stdio;
const transformStdioItem = (stdioItem, index, stdioStreams) => stdioStreams[index].type !== 'native' && stdioItem !== 'overlapped' ? 'pipe' : stdioItem;modified:lib/stdio/sync.js+1 added-1 removed
122345650 unmodified linesimport {readFileSync, writeFileSync} from 'node:fs';import {handleInput} from './input.js';import {handleInput} from './handle.js';import {TYPE_TO_MESSAGE} from './type.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync modeexport const handleInputSync = options => {50 unmodified linesmodified:lib/stdio/type.js+2 added-1 removed
27 unmodified lines28 export const isRegularUrl = stdioOption => isUrlInstance(stdioOption) && !hasFileProtocol(stdioOption);29 30 const stringIsFilePath = stdioOption => stdioOption.startsWith('.') || isAbsolute(stdioOption);31 const isFilePath = stdioOption => typeof stdioOption === 'string' && stringIsFilePath(stdioOption);32-export const isUnknownStdioString = stdioOption => typeof stdioOption === 'string' && !stringIsFilePath(stdioOption) && !KNOWN_STDIO_STRINGS.has(stdioOption);32+33+export const isUnknownStdioString = (type, stdioOption) => type === 'native' && typeof stdioOption === 'string' && !KNOWN_STDIO_STRINGS.has(stdioOption);34 const KNOWN_STDIO_STRINGS = new Set(['ipc', 'ignore', 'inherit', 'overlapped', 'pipe']);35 36 const isReadableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object ReadableStream]';37 const isWritableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object WritableStream]';15 unmodified lines