Simplify implementation (#955)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 63b5ac4 · Browse files · 9 files·+25 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
9 files+25 added−32 removed
- other:+25 added−32 removed
Provenance
- Commit
8ad9ee7- Tree
c2f1700- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
63b5ac4
9 files changed+25 added−32 removed
modified:lib/pipe/validate.js+1 added-2 removed
136 unmodified linesExpand all13713813914014114214114214314414541 unmodified lines136 unmodified linesExpand all
const FD_REGEXP = /^fd(\d+)$/;
const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => { const usedDescriptor = getUsedDescriptor(fdNumber); const fileDescriptor = fileDescriptors.find(fileDescriptor => fileDescriptor.fdNumber === usedDescriptor); const fileDescriptor = fileDescriptors[getUsedDescriptor(fdNumber)]; if (fileDescriptor === undefined) { throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. That file descriptor does not exist.Please set the "stdio" option to ensure that file descriptor exists.`); }41 unmodified linesmodified:lib/stdio/async.js+1 added-1 removed
55 unmodified lines56 export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {57 stdioState.subprocess = subprocess;58 const inputStreamsGroups = {};59 60- for (const {stdioItems, direction, fdNumber} of fileDescriptors) {60+ for (const [fdNumber, {stdioItems, direction}] of Object.entries(fileDescriptors)) {61 for (const {stream} of stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type))) {62 pipeTransform(subprocess, stream, direction, fdNumber);63 }64 51 unmodified linesmodified:lib/stdio/generator.js+1 added-1 removed
150 unmodified lines151 const stream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});152 return {stream};153 };154 155-export const getGenerators = allStdioItems => allStdioItems.filter(({type}) => type === 'generator');155+export const getGenerators = stdioItems => stdioItems.filter(({type}) => type === 'generator');156 157 export const runGeneratorsSync = (chunks, generators) => {158 for (const {value, optionName} of generators) {159 const generators = addInternalGenerators({value, forceEncoding: false, optionName});39 unmodified linesmodified:lib/stdio/handle.js+1 added-1 removed
29 unmodified lines30 const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({stdioItem, isStdioArray, fdNumber, direction, isSync}));31 const objectMode = getObjectMode(stdioItems, optionName, direction, options);32 validateFileObjectMode(stdioItems, objectMode);33 const normalizedStdioItems = normalizeStdioItems({stdioItems, fdNumber, optionName, addProperties, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode});34- return {fdNumber, direction, outputLines, stdioItems: normalizedStdioItems};34+ return {direction, objectMode, outputLines, stdioItems: normalizedStdioItems};35 };36 37 const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;38 const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];97 unmodified linesmodified:lib/stdio/input-sync.js+8 added-10 removed
7 unmodified lines8 addInputOptionSync(fileDescriptors, fdNumber, options);9 }10 };11 12-const getInputFdNumbers = fileDescriptors => new Set(fileDescriptors13- .filter(({direction}) => direction === 'input')14- .map(({fdNumber}) => fdNumber));12+const getInputFdNumbers = fileDescriptors => new Set(Object.entries(fileDescriptors)13+ .filter(([, {direction}]) => direction === 'input')14+ .map(([fdNumber]) => Number(fdNumber)));15 16 const addInputOptionSync = (fileDescriptors, fdNumber, options) => {17- const selectedStdioItems = fileDescriptors18- .filter(fileDescriptor => fileDescriptor.fdNumber === fdNumber)19- .flatMap(({stdioItems}) => stdioItems);20- const allStdioItems = selectedStdioItems.filter(({contents}) => contents !== undefined);17+ const {stdioItems} = fileDescriptors[fdNumber];18+ const allStdioItems = stdioItems.filter(({contents}) => contents !== undefined);19 if (allStdioItems.length === 0) {20 return;21 }22 2 unmodified lines25 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be ${TYPE_TO_MESSAGE[type]} with synchronous methods.`);26 }27 28 const allContents = allStdioItems.map(({contents}) => contents);31- const transformedContents = allContents.map(contents => applySingleInputGeneratorsSync(contents, selectedStdioItems));29+ const transformedContents = allContents.map(contents => applySingleInputGeneratorsSync(contents, stdioItems));30 options.input = joinToUint8Array(transformedContents);31 };32 35-const applySingleInputGeneratorsSync = (contents, selectedStdioItems) => {36- const generators = getGenerators(selectedStdioItems).reverse();33+const applySingleInputGeneratorsSync = (contents, stdioItems) => {34+ const generators = getGenerators(stdioItems).reverse();35 const newContents = runGeneratorsSync(contents, generators);36 validateSerializable(newContents);37 return joinToUint8Array(newContents);38 };7 unmodified linesmodified:lib/stdio/output-sync.js+9 added-11 removed
18 unmodified lines19 if (result === null) {20 return;21 }22 23- const selectedFileDescriptors = fileDescriptors.filter(fileDescriptor => fileDescriptor.fdNumber === fdNumber && fileDescriptor.direction === 'output');24- const allStdioItems = selectedFileDescriptors.flatMap(({stdioItems}) => stdioItems);25- const allOutputLines = selectedFileDescriptors.map(({outputLines}) => outputLines);23+ const {stdioItems, outputLines, objectMode} = fileDescriptors[fdNumber];24 const uint8ArrayResult = bufferToUint8Array(result);27- const generators = getGenerators(allStdioItems);25+ const generators = getGenerators(stdioItems);26 const chunks = runOutputGeneratorsSync([uint8ArrayResult], generators, state);29- const {serializedResult, finalResult} = serializeChunks({chunks, generators, allOutputLines, encoding, lines});27+ const {serializedResult, finalResult} = serializeChunks({chunks, objectMode, outputLines, encoding, lines});28 const returnedResult = buffer ? finalResult : undefined;29 30 try {31 if (state.error === undefined) {34- writeToFiles(serializedResult, allStdioItems);32+ writeToFiles(serializedResult, stdioItems);33 }34 35 return returnedResult;36 } catch (error) {10 unmodified lines47 return chunks;48 }49 };50 53-const serializeChunks = ({chunks, generators, allOutputLines, encoding, lines}) => {54- if (generators.at(-1)?.value?.readableObjectMode) {51+const serializeChunks = ({chunks, objectMode, outputLines, encoding, lines}) => {52+ if (objectMode) {53 return {finalResult: chunks};54 }55 56 const serializedResult = encoding === 'buffer' ? joinToUint8Array(chunks) : joinToString(chunks, true);59- const finalResult = lines ? allOutputLines.flat() : serializedResult;57+ const finalResult = lines ? outputLines : serializedResult;58 return {serializedResult, finalResult};59 };60 63-const writeToFiles = (serializedResult, allStdioItems) => {64- for (const {type, path} of allStdioItems) {61+const writeToFiles = (serializedResult, stdioItems) => {62+ for (const {type, path} of stdioItems) {63 if (FILE_TYPES.has(type)) {64 writeFileSync(path, serializedResult);65 }66 }29 unmodified linesmodified:lib/stream/resolve.js+1 added-1 removed
65 unmodified lines66 67 // Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.68 // The `.pipe()` method automatically ends that stream when `subprocess` ends.69 // This makes sure we wait for the completion of those streams, in order to catch any error.70-const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems, fdNumber}) => stdioItems70+const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems71 .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream))72 .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, {73 isSameDirection: TRANSFORM_TYPES.has(type),74 stopOnExit: type === 'native',11 unmodified linesmodified:lib/stream/subprocess.js+2 added-2 removed
1 import {setImmediate} from 'node:timers/promises';2 import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';3-import {waitForStream, handleStreamError, isInputFileDescriptor, getFileDescriptor} from './wait.js';3+import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';4 5 export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, streamInfo}) => {6 if (!stream) {7 return;49 unmodified lines57 };58 59 const getOutputLines = async (stream, fdNumber, streamInfo) => {60 await waitForStream(stream, fdNumber, streamInfo);61- return getFileDescriptor(streamInfo, fdNumber).outputLines;61+ return streamInfo.fileDescriptors[fdNumber].outputLines;62 };63 64 // On failure, `result.stdout|stderr|all` should contain the currently buffered stream65 // They are automatically closed and flushed by Node.js when the subprocess exits14 unmodified linesmodified:lib/stream/wait.js+1 added-3 removed
79 unmodified lines80 // For example, `subprocess.stdin` is technically a Duplex, but can only be used as a writable.81 // Therefore, we need to use the file descriptor's direction (`stdin` is input, `stdout` is output, etc.).82 // However, while `subprocess.std*` and transforms follow that direction, any stream passed the `std*` option has the opposite direction.83 // For example, `subprocess.stdin` is a writable, but the `stdin` option is a readable.84-export const isInputFileDescriptor = (streamInfo, fdNumber) => getFileDescriptor(streamInfo, fdNumber).direction === 'input';84+export const isInputFileDescriptor = ({fileDescriptors}, fdNumber) => fileDescriptors[fdNumber].direction === 'input';85 86-export const getFileDescriptor = ({fileDescriptors}, fdNumber) => fileDescriptors.find(fileDescriptor => fileDescriptor.fdNumber === fdNumber);87-86 // When `stream.destroy()` is called without an `error` argument, stream is aborted.87 // This is the only way to abort a readable stream, which can be useful in some instances.88 // Therefore, we ignore this error on readable streams.89 export const isStreamAbort = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';7 unmodified lines