Skip to content
Myr

Refactor `stdio` option logic (#639)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 2432e72 · Browse files · 6 files+77 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+77 added−49 removed

  • other:+77 added−49 removed

6 files+77 added−49 removed

modified:lib/stdio/async.js+2 added-2 removed
added:lib/stdio/direction.js+42 added
modified:lib/stdio/handle.js+23 added-24 removed
modified:lib/stdio/input.js+4 added-4 removed
modified:lib/stdio/sync.js+4 added-2 removed
12 unmodified lines
13const forbiddenIfStreamSync = ({value, optionName}) => {14 if (isNodeStream(value)) {15 forbiddenIfSync({type: 'nodeStream', optionName});16 }1718 return {};19};2021const forbiddenIfSync = ({type, optionName}) => {22 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);
27 unmodified lines
50 if (result.output === null) {51 return;52 }5352 for (const [index, stdioStream] of stdioStreams.entries()) {53 pipeStdioOptionSync(result.output[index], stdioStream);54 for (const stdioStream of stdioStreams) {55 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);56 }57};5859const pipeStdioOptionSync = (result, {type, value, direction}) => {
8 unmodified lines
modified:lib/stdio/type.js+2 added-17 removed
1import {isAbsolute} from 'node:path';2import {3 isStream as isNodeStream,4 isReadableStream as isNodeReadableStream,5 isWritableStream as isNodeWritableStream,6} from 'is-stream';2import {isStream as isNodeStream} from 'is-stream';34// The `stdin`/`stdout`/`stderr` option can be of many types. This detects it.5export const getStdioOptionType = stdioOption => {6 if (isFileUrl(stdioOption) || isFilePath(stdioOption)) {
26 unmodified lines
33export const isUnknownStdioString = (type, stdioOption) => type === 'native' && typeof stdioOption === 'string' && !KNOWN_STDIO_STRINGS.has(stdioOption);34const KNOWN_STDIO_STRINGS = new Set(['ipc', 'ignore', 'inherit', 'overlapped', 'pipe']);3536const isReadableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object ReadableStream]';41const isWritableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object WritableStream]';37export const isWritableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object WritableStream]';38const isWebStream = stdioOption => isReadableStream(stdioOption) || isWritableStream(stdioOption);3940const isIterableObject = stdinOption => typeof stdinOption === 'object'41 && stdinOption !== null42 && (typeof stdinOption[Symbol.asyncIterator] === 'function' || typeof stdinOption[Symbol.iterator] === 'function');4348// For `stdio[index]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.49// When ambiguous, we default to `output` since it is the most common use case for additional file descriptors.50// For the same reason, Duplex streams and TransformStreams are considered as outputs.51// `nodeStream` and `stringOrBuffer` types always apply to `stdin`, i.e. missing here.52export const isInputDirection = {53 filePath: () => false,54 webStream: stdioOption => !isWritableStream(stdioOption),55 native: stdioOption => (isNodeReadableStream(stdioOption) && !isNodeWritableStream(stdioOption)) || stdioOption === 0,56 iterable: () => true,57};5844// Convert types to human-friendly strings for error messages45export const TYPE_TO_MESSAGE = {46 filePath: 'a file path',47 webStream: 'a web stream',
5 unmodified lines