Skip to content
Myr

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

9 files+25 added−32 removed

modified:lib/pipe/validate.js+1 added-2 removed
modified:lib/stdio/async.js+1 added-1 removed
55 unmodified lines
56export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {57 stdioState.subprocess = subprocess;58 const inputStreamsGroups = {};5960 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 lines
modified:lib/stdio/generator.js+1 added-1 removed
150 unmodified lines
151 const stream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode});152 return {stream};153};154155export const getGenerators = allStdioItems => allStdioItems.filter(({type}) => type === 'generator');155export const getGenerators = stdioItems => stdioItems.filter(({type}) => type === 'generator');156157export const runGeneratorsSync = (chunks, generators) => {158 for (const {value, optionName} of generators) {159 const generators = addInternalGenerators({value, forceEncoding: false, optionName});
39 unmodified lines
modified:lib/stdio/handle.js+1 added-1 removed
29 unmodified lines
30 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};3637const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;38const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
97 unmodified lines
modified:lib/stdio/input-sync.js+8 added-10 removed
7 unmodified lines
8 addInputOptionSync(fileDescriptors, fdNumber, options);9 }10};1112const getInputFdNumbers = fileDescriptors => new Set(fileDescriptors13 .filter(({direction}) => direction === 'input')14 .map(({fdNumber}) => fdNumber));12const getInputFdNumbers = fileDescriptors => new Set(Object.entries(fileDescriptors)13 .filter(([, {direction}]) => direction === 'input')14 .map(([fdNumber]) => Number(fdNumber)));1516const 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 lines
25 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be ${TYPE_TO_MESSAGE[type]} with synchronous methods.`);26 }2728 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};3235const applySingleInputGeneratorsSync = (contents, selectedStdioItems) => {36 const generators = getGenerators(selectedStdioItems).reverse();33const 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 lines
modified:lib/stdio/output-sync.js+9 added-11 removed
18 unmodified lines
19 if (result === null) {20 return;21 }2223 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;2930 try {31 if (state.error === undefined) {34 writeToFiles(serializedResult, allStdioItems);32 writeToFiles(serializedResult, stdioItems);33 }3435 return returnedResult;36 } catch (error) {
10 unmodified lines
47 return chunks;48 }49};5053const serializeChunks = ({chunks, generators, allOutputLines, encoding, lines}) => {54 if (generators.at(-1)?.value?.readableObjectMode) {51const serializeChunks = ({chunks, objectMode, outputLines, encoding, lines}) => {52 if (objectMode) {53 return {finalResult: chunks};54 }5556 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};6063const writeToFiles = (serializedResult, allStdioItems) => {64 for (const {type, path} of allStdioItems) {61const writeToFiles = (serializedResult, stdioItems) => {62 for (const {type, path} of stdioItems) {63 if (FILE_TYPES.has(type)) {64 writeFileSync(path, serializedResult);65 }66 }
29 unmodified lines
modified:lib/stream/resolve.js+1 added-1 removed
65 unmodified lines
6667// 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.70const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems, fdNumber}) => stdioItems70const 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 lines
modified:lib/stream/subprocess.js+2 added-2 removed
1import {setImmediate} from 'node:timers/promises';2import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';3import {waitForStream, handleStreamError, isInputFileDescriptor, getFileDescriptor} from './wait.js';3import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';45export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, streamInfo}) => {6 if (!stream) {7 return;
49 unmodified lines
57};5859const getOutputLines = async (stream, fdNumber, streamInfo) => {60 await waitForStream(stream, fdNumber, streamInfo);61 return getFileDescriptor(streamInfo, fdNumber).outputLines;61 return streamInfo.fileDescriptors[fdNumber].outputLines;62};6364// On failure, `result.stdout|stderr|all` should contain the currently buffered stream65// They are automatically closed and flushed by Node.js when the subprocess exits
14 unmodified lines
modified:lib/stream/wait.js+1 added-3 removed
79 unmodified lines
80// 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.84export const isInputFileDescriptor = (streamInfo, fdNumber) => getFileDescriptor(streamInfo, fdNumber).direction === 'input';84export const isInputFileDescriptor = ({fileDescriptors}, fdNumber) => fileDescriptors[fdNumber].direction === 'input';8586export const getFileDescriptor = ({fileDescriptors}, fdNumber) => fileDescriptors.find(fileDescriptor => fileDescriptor.fdNumber === fdNumber);8786// 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.89export const isStreamAbort = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';
7 unmodified lines