Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent dcf611c · Browse files · 23 files·+332 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
23 files+332 added−23 removed
- tests:+86 added−2 removed
- other:+246 added−21 removed
Provenance
- Commit
487a3a4- Tree
c73fd4f- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
dcf611c
23 files changed+332 added−23 removed
modified:.github/workflows/main.yml+1 added-1 removed
28 unmodified lines293031323333343536378 unmodified lines28 unmodified lines node-version: ${{ matrix.node-version }} - run: npm install - uses: lycheeverse/lychee-action@v2 with: args: --cache --verbose --no-progress --include-fragments --exclude packagephobia --exclude /pull/ --exclude linkedin --exclude stackoverflow --exclude stackexchange --exclude github.com/nodejs/node --exclude github.com/denoland/deno --exclude file:///test --exclude invalid.com '*.md' 'docs/*.md' '.github/**/*.md' '*.json' '*.js' 'lib/**/*.js' 'test/**/*.js' '*.ts' 'test-d/**/*.ts' args: --cache --verbose --no-progress --include-fragments --exclude packagephobia --exclude /pull/ --exclude depot.dev --exclude linkedin --exclude stackoverflow --exclude stackexchange --exclude github.com/nodejs/node --exclude github.com/denoland/deno --exclude file:///test --exclude invalid.com '*.md' 'docs/*.md' '.github/**/*.md' '*.json' '*.js' 'lib/**/*.js' 'test/**/*.js' '*.ts' 'test-d/**/*.ts' fail: true if: ${{ matrix.os == 'ubuntu' && matrix.node-version == 26 }} - run: npm run lint if: ${{ matrix.os == 'ubuntu' && matrix.node-version == 26 }}8 unmodified linesmodified:docs/api.md+2 added-2 removed
914 unmodified lines915 916 ### options.stdio917 918 _TypeScript:_ [`Options['stdio']`](typescript.md) or [`SyncOptions['stdio']`](typescript.md)\919-_Type:_ `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | TransformStream | URL | {file: string} | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string | Uint8Array | unknown> | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream}>` (or a tuple of those types)\919+_Type:_ `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | TransformStream | URL | {file: string} | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string | Uint8Array | unknown> | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream} | {value: 'pipe'; input?: boolean}>` (or a tuple of those types)\920 _Default:_ `pipe`921 922 Like the [`stdin`](#optionsstdin), [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options but for all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.923 924 A single string can be used [as a shortcut](output.md#shortcut).925 926-The array can have more than 3 items, to create [additional file descriptors](output.md#additional-file-descriptors) beyond [`stdin`](#optionsstdin)/[`stdout`](#optionsstdout)/[`stderr`](#optionsstderr).926+The array can have more than 3 items, to create [additional file descriptors](output.md#additional-file-descriptors) beyond [`stdin`](#optionsstdin)/[`stdout`](#optionsstdout)/[`stderr`](#optionsstderr). Passing `'pipe'` to an additional file descriptor defaults it to output. To use it for [input](input.md#additional-file-descriptors) instead, pass `{value: 'pipe', input: true}`.927 928 More info on [available values](output.md), [streaming](streams.md) and [transforms](transform.md).929 930 ### options.all377 unmodified linesmodified:docs/input.md+13 added
133 unmodified linesExpand all134135136137138139140141142143144145146147148149150151152153154155133 unmodified linesExpand all stdio: ['pipe', 'pipe', 'pipe', new Uint8Array([/* ... */])],})`npm run build`;```
When passing `'pipe'` to an additional file descriptor, Execa cannot tell whether it is meant for input or output, so it defaults to output. To use it for input instead, such as when [streaming](streams.md#different-file-descriptor) to it, pass `{value: 'pipe', input: true}`.
```jsimport {pipeline} from 'node:stream/promises';
// Stream input to the file descriptor number 3const subprocess = execa({ stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}],})`npm run build`;
await pipeline(inputStream, subprocess.writable({to: 'fd3'}));```
<hr>
[**Next**: 📢 Output](output.md)\[**Previous**: 🏁 Termination](termination.md)\[**Top**: Table of contents](../readme.md#documentation)modified:docs/streams.md+8 added
102 unmodified lines103 104 const duplex = execa`npm run scaffold`.duplex({from: 'stderr', to: 'fd3'});105 ```106 107+Writing to an additional file descriptor like `'fd3'` requires marking it as [input](input.md#additional-file-descriptors), since it otherwise defaults to output.108+109+```js110+const subprocess = execa({stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}]})`npm run scaffold`;111+112+const writable = subprocess.writable({to: 'fd3'});113+```114+115 ### Error handling116 117 When using [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions) or [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions), the stream waits for the subprocess to end, and emits an [`error`](https://nodejs.org/api/stream.html#event-error) event if the subprocess [fails](errors.md). This differs from [`subprocess.stdin`](api.md#subprocessstdin), [`subprocess.stdout`](api.md#subprocessstdout) and [`subprocess.stderr`](api.md#subprocessstderr)'s behavior.118 7 unmodified linesmodified:lib/arguments/fd-options.js+2 added-1 removed
61 unmodified lines62 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);63 }64 65 if (fileDescriptor.direction !== 'input' && isWritable) {66- throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.`);66+ throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.67+If you meant to use it as input, please set its "stdio" option to \`{value: 'pipe', input: true}\`.`);68 }69 };70 71 const getInvalidStdioOptionMessage = (fdNumber, fdName, options, isWritable) => {38 unmodified linesmodified:lib/stdio/direction.js+3 added-1 removed
18 unmodified lines19 20 return directions.find(Boolean) ?? DEFAULT_DIRECTION;21 };22 23+// `direction` is set when the user explicitly requests it, e.g. `{value: 'pipe', input: true}`.24+// It takes precedence over guessing, but not over the fixed direction of `stdin`/`stdout`/`stderr`.23-const getStdioItemDirection = ({type, value}, fdNumber) => KNOWN_DIRECTIONS[fdNumber] ?? guessStreamDirection[type](value);25+const getStdioItemDirection = ({type, value, direction}, fdNumber) => KNOWN_DIRECTIONS[fdNumber] ?? direction ?? guessStreamDirection[type](value);26 27 // `stdin`/`stdout`/`stderr` have a known direction28 const KNOWN_DIRECTIONS = ['input', 'output', 'output'];29 49 unmodified linesmodified:lib/stdio/duplicate.js+4 added-1 removed
6 unmodified lines7 8 // Duplicates in the same file descriptor is most likely an error.9 // However, this can be useful with generators.10 export const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>11- stdioItems.every((stdioItemTwo, indexTwo) => stdioItemOne.value !== stdioItemTwo.value11+ stdioItems.every((stdioItemTwo, indexTwo) => !hasSameValueAndDirection(stdioItemOne, stdioItemTwo)12 || indexOne >= indexTwo13 || stdioItemOne.type === 'generator'14 || stdioItemOne.type === 'asyncGenerator'));15 16+const hasSameValueAndDirection = (stdioItemOne, stdioItemTwo) => stdioItemOne.value === stdioItemTwo.value17+ && stdioItemOne.direction === stdioItemTwo.direction;18+19 // Check if two file descriptors are sharing the same target.20 // For example `{stdout: {file: './output.txt'}, stderr: {file: './output.txt'}}`.21 export const getDuplicateStream = ({stdioItem: {type, value, optionName}, direction, fileDescriptors, isSync}) => {22 const otherStdioItems = getOtherStdioItems(fileDescriptors, type);97 unmodified linesmodified:lib/stdio/handle-sync.js+11 added
16 unmodified lines17 18 return {};19 };20 21+// A native input pipe on an additional file descriptor cannot be fed in sync mode, since there is no way to write to it after spawning, so it would hang.22+const forbiddenNativeInputIfSync = stdioItem => {23+ const {optionName, value} = stdioItem;24+ if (value === 'pipe' && optionName !== 'stdin') {25+ throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be an input pipe with synchronous methods.`);26+ }27+28+ return forbiddenNativeIfSync(stdioItem);29+};30+31 const throwInvalidSyncValue = (optionName, value) => {32 throw new TypeError(`The \`${optionName}\` option cannot be ${value} with synchronous methods.`);33 };34 12 unmodified lines47 48 const addPropertiesSync = {49 input: {50 ...addProperties,51+ native: forbiddenNativeInputIfSync,52 fileUrl: ({value}) => ({contents: [bufferToUint8Array(readFileSync(value))]}),53 filePath: ({value: {file}}) => ({contents: [bufferToUint8Array(readFileSync(file))]}),54 fileNumber: forbiddenIfSync,55 iterable: ({value}) => ({contents: [...value]}),13 unmodified linesmodified:lib/stdio/handle.js+25 added-5 removed
3 unmodified lines4 import {5 getStdioItemType,6 isRegularUrl,7 isUnknownStdioString,8+ isPipeInputObject,9+ checkBooleanOption,10 FILE_TYPES,11 } from './type.js';12 import {getStreamDirection} from './direction.js';13 import {normalizeStdioOption} from './stdio-option.js';69 unmodified lines83 const isInheritedStdinOnly = stdioItems => stdioItems.length === 184 && stdioItems[0].type === 'native'85 && stdioItems[0].value === 'inherit';86 85-const initializeStdioItem = (value, optionName) => ({86- type: getStdioItemType(value, optionName),87- value,88- optionName,89-});87+const initializeStdioItem = (value, optionName) => {88+ if (isPipeInputObject(value)) {89+ return initializePipeInputItem(value, optionName);90+ }91 92+ return {93+ type: getStdioItemType(value, optionName),94+ value,95+ optionName,96+ };97+};98+99+// The `{value: 'pipe', input: true}` object form is a native pipe, plus an explicit input direction.100+const initializePipeInputItem = ({value, input}, optionName) => {101+ checkBooleanOption(input, `${optionName}.input`);102+103+ return {104+ type: 'native',105+ value,106+ direction: input ? 'input' : undefined,107+ optionName,108+ };109+};110+111 const validateStdioArray = (stdioItems, isStdioArray, optionName) => {112 if (stdioItems.length === 0) {113 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);114 }129 unmodified linesmodified:lib/stdio/stdio-option.js+14 added-3 removed
1 import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';2 import {normalizeIpcStdioArray} from '../ipc/array.js';3 import {isFullVerbose} from '../verbose/values.js';4+import {isPipeInputObject} from './type.js';5 6 // Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`.7 // Also normalize the `stdio` option.8 export const normalizeStdioOption = ({stdio, ipc, buffer, ...options}, verboseInfo, isSync) => {51 unmodified lines60 const normalizeStdioSync = (stdioArray, buffer, verboseInfo) => stdioArray.map((stdioOption, fdNumber) =>61 !buffer[fdNumber]62 && fdNumber !== 063 && !isFullVerbose(verboseInfo, fdNumber)63- && isOutputPipeOnly(stdioOption)64+ && isOutputPipeOnly(stdioOption, fdNumber)65 ? 'ignore'66 : stdioOption);67 67-const isOutputPipeOnly = stdioOption => stdioOption === 'pipe'68- || (Array.isArray(stdioOption) && stdioOption.every(item => item === 'pipe'));68+const isOutputPipeOnly = (stdioOption, fdNumber) => isOutputPipe(stdioOption, fdNumber)69+ || (Array.isArray(stdioOption) && stdioOption.every(item => isOutputPipe(item, fdNumber)));70+71+const isOutputPipe = (stdioOption, fdNumber) => stdioOption === 'pipe'72+ || isOutputPipeObject(stdioOption, fdNumber);73+74+const isOutputPipeObject = (stdioOption, fdNumber) => isPipeInputObject(stdioOption)75+ && stdioOption.value === 'pipe'76+ && (stdioOption.input === undefined || stdioOption.input === false || isFixedOutputPipe(fdNumber, stdioOption.input));77+78+// On `stdout`/`stderr`, the direction is always output, so `input: true` is ignored and the pipe stays output.79+const isFixedOutputPipe = (fdNumber, input) => input === true && (fdNumber === 1 || fdNumber === 2);modified:lib/stdio/type.js+10 added-1 removed
106 unmodified lines107 108 return isAsyncGenerator(transform) || isAsyncGenerator(final) ? 'asyncGenerator' : 'generator';109 };110 111-const checkBooleanOption = (value, optionName) => {111+export const checkBooleanOption = (value, optionName) => {112 if (value !== undefined && typeof value !== 'boolean') {113 throw new TypeError(`The \`${optionName}\` option must use a boolean.`);114 }115 };13 unmodified lines129 && isFilePathString(value.file);130 const FILE_PATH_KEYS = new Set(['file', 'append']);131 export const isFilePathString = file => typeof file === 'string';132 133+// The `{value: 'pipe', input: true}` object form marks an additional file descriptor as input instead of output.134+// This is only needed for `'pipe'`/`'overlapped'` on fd3+, whose direction is otherwise ambiguous and defaults to output.135+export const isPipeInputObject = value => isPlainObj(value)136+ && Object.keys(value).length > 0137+ && Object.keys(value).every(key => PIPE_INPUT_KEYS.has(key))138+ && NATIVE_PIPE_VALUES.has(value.value);139+const PIPE_INPUT_KEYS = new Set(['value', 'input']);140+const NATIVE_PIPE_VALUES = new Set(['pipe', 'overlapped']);141+142 export const isUnknownStdioString = (type, value) => type === 'native'143 && typeof value === 'string'144 && !KNOWN_STDIO_STRINGS.has(value);145 const KNOWN_STDIO_STRINGS = new Set(['ipc', 'ignore', 'inherit', 'overlapped', 'pipe']);37 unmodified linesmodified:test/arguments/fd-options.js+5 added
267 unmodified lines268 sourceOptions: fullStdio,269 to: 'fd3',270 message: 'must be a writable stream',271 });272+test('.duplex() "to" option on an output file descriptor suggests the input-pipe syntax', testNodeStream, {273+ sourceOptions: fullStdio,274+ to: 'fd3',275+ message: '{value: \'pipe\', input: true}',276+});277 test('.pipe() "to" option cannot be "all"', testPipeError, {278 destinationOptions: fullStdio,279 to: 'all',280 message: 'must be a writable stream',443 unmodified linesmodified:test/convert/duplex.js+7 added-1 removed
22 unmodified lines23 getReadWriteSubprocess,24 } from '../helpers/convert.js';25 import {foobarString} from '../helpers/input.js';26 import {majorNodeVersion} from '../helpers/node-version.js';27-import {prematureClose, fullStdio, fullReadableStdio} from '../helpers/stdio.js';27+import {28+ getStdio,29+ prematureClose,30+ fullStdio,31+ fullReadableStdio,32+} from '../helpers/stdio.js';33 import {defaultHighWaterMark} from '../helpers/stream.js';34 35 setFixtureDirectory();36 44 unmodified lines81 };82 83 test('.duplex() can use stdin', testWritableDuplexDefault, 0, 'stdin', {});84 test('.duplex() can use input stdio[*]', testWritableDuplexDefault, 3, 'fd3', fullReadableStdio());85+test('.duplex() can use input stdio[*] with { value: "pipe", input: true }', testWritableDuplexDefault, 3, 'fd3', getStdio(3, {value: 'pipe', input: true}));86 test('.duplex() uses stdin by default', testWritableDuplexDefault, 0, undefined, {});87 88 test('.duplex() abort -> subprocess fail', async t => {89 const subprocess = getReadWriteSubprocess();121 unmodified linesmodified:test/convert/writable.js+3 added-1 removed
24 unmodified lines25 foobarBuffer,26 foobarObject,27 foobarObjectString,28 } from '../helpers/input.js';29-import {prematureClose, fullReadableStdio} from '../helpers/stdio.js';29+import {getStdio, prematureClose, fullReadableStdio} from '../helpers/stdio.js';30 import {31 throwingGenerator,32 serializeGenerator,33 noopAsyncGenerator,28 unmodified lines62 };63 64 test('.writable() can use stdin', testWritableDefault, 0, 'stdin', {});65 test('.writable() can use stdio[*]', testWritableDefault, 3, 'fd3', fullReadableStdio());66+test('.writable() can use stdio[*] with { value: "pipe", input: true }', testWritableDefault, 3, 'fd3', getStdio(3, {value: 'pipe', input: true}));67+test('.writable() can use stdio[*] with [{ value: "pipe", input: true }, "pipe"]', testWritableDefault, 3, 'fd3', getStdio(3, [{value: 'pipe', input: true}, 'pipe']));68 test('.writable() uses stdin by default', testWritableDefault, 0, undefined, {});69 70 test('.writable() hangs until ended', async t => {71 const subprocess = getReadWriteSubprocess();326 unmodified linesmodified:test/io/max-buffer.js+15 added
265 unmodified lines266 test('do not buffer stdout when `buffer` set to `false`, fd-specific, sync', testNoMaxBufferSync, 1, {stdout: false});267 test('do not buffer stderr when `buffer` set to `false`, sync', testNoMaxBufferSync, 2, false);268 test('do not buffer stderr when `buffer` set to `false`, fd-specific, sync', testNoMaxBufferSync, 2, {stderr: false});269 270+const testNoMaxBufferSyncObjectPipe = (t, fdNumber, fdName, stdioOption) => {271+ const {isMaxBuffer, stdio} = execaSync('max-buffer.js', [`${fdNumber}`, `${maxBuffer + 1}`], {272+ [fdName]: stdioOption,273+ buffer: false,274+ maxBuffer,275+ });276+ t.false(isMaxBuffer);277+ t.is(stdio[fdNumber], undefined);278+};279+280+test('do not buffer stdout object pipe when `buffer` set to `false`, sync', testNoMaxBufferSyncObjectPipe, 1, 'stdout', {value: 'pipe'});281+test('do not buffer stdout object pipe with input false when `buffer` set to `false`, sync', testNoMaxBufferSyncObjectPipe, 1, 'stdout', {value: 'pipe', input: false});282+test('do not buffer stdout object pipe with input true when `buffer` set to `false`, sync', testNoMaxBufferSyncObjectPipe, 1, 'stdout', {value: 'pipe', input: true});283+test('do not buffer stderr object pipe with input true when `buffer` set to `false`, sync', testNoMaxBufferSyncObjectPipe, 2, 'stderr', {value: 'pipe', input: true});284+285 const testMaxBufferAbort = async (t, fdNumber) => {286 const subprocess = getMaxBufferSubprocess(execa, fdNumber);287 const [{isMaxBuffer, shortMessage}] = await Promise.all([288 t.throwsAsync(subprocess, maxBufferMessage),35 unmodified linesmodified:test/stdio/direction.js+56 added
47 unmodified lines48 };49 50 test('stdin ambiguous direction is influenced by other values', testAmbiguousMultiple, 0);51 test('stdio[*] ambiguous direction is influenced by other values', testAmbiguousMultiple, 3);52+53+const testDirectionInputPipe = async (t, stdioOption) => {54+ const subprocess = execa('stdin-fd.js', ['3'], getStdio(3, stdioOption));55+ subprocess.stdio[3].end(foobarString);56+ const {stdout} = await subprocess;57+ t.is(stdout, foobarString);58+};59+60+test('stdio[*] { value: "pipe", input: true } sets the direction to input', testDirectionInputPipe, {value: 'pipe', input: true});61+test('stdio[*] { value: "overlapped", input: true } sets the direction to input', testDirectionInputPipe, {value: 'overlapped', input: true});62+63+test('stdio[*] { value: "pipe", input: true } cannot be used in sync mode', t => {64+ t.throws(() => {65+ execaSync('empty.js', getStdio(3, {value: 'pipe', input: true}));66+ }, {message: /can be an input pipe with synchronous methods/});67+});68+69+test('stdio[*] { value: "pipe", input: true } cannot be used in sync mode with buffer false', t => {70+ t.throws(() => {71+ execaSync('empty.js', {...getStdio(3, {value: 'pipe', input: true}), buffer: false});72+ }, {message: /can be an input pipe with synchronous methods/});73+});74+75+const testDirectionOutputPipe = async (t, stdioOption, execaMethod) => {76+ const {stdio} = await execaMethod('noop-fd.js', ['3', foobarString], getStdio(3, stdioOption));77+ t.is(stdio[3], foobarString);78+};79+80+test('stdio[*] { value: "pipe" } keeps the default output direction', testDirectionOutputPipe, {value: 'pipe'}, execa);81+test('stdio[*] { value: "pipe" } keeps the default output direction - sync', testDirectionOutputPipe, {value: 'pipe'}, execaSync);82+test('stdio[*] { value: "pipe", input: false } keeps the default output direction', testDirectionOutputPipe, {value: 'pipe', input: false}, execa);83+test('stdio[*] { value: "pipe", input: false } keeps the default output direction - sync', testDirectionOutputPipe, {value: 'pipe', input: false}, execaSync);84+85+const testDirectionConflict = (t, execaMethod) => {86+ t.throws(() => {87+ execaMethod('empty.js', getStdio(3, [{value: 'pipe', input: true}, 1]));88+ }, {message: /readable and writable/});89+};90+91+test('Cannot pass { value: "pipe", input: true } with a writable value to stdio[*]', testDirectionConflict, execa);92+test('Cannot pass { value: "pipe", input: true } with a writable value to stdio[*] - sync', testDirectionConflict, execaSync);93+94+const testInputBoolean = (t, execaMethod) => {95+ t.throws(() => {96+ execaMethod('empty.js', getStdio(3, {value: 'pipe', input: 'yes'}));97+ }, {message: /`stdio\[3\]\.input` option must use a boolean/});98+};99+100+test('stdio[*].input must be a boolean', testInputBoolean, execa);101+test('stdio[*].input must be a boolean - sync', testInputBoolean, execaSync);102+103+test('stdout.input must be a boolean with buffer false - sync', t => {104+ t.throws(() => {105+ execaSync('empty.js', {stdout: {value: 'pipe', input: 'yes'}, buffer: false});106+ }, {message: /`stdout\.input` option must use a boolean/});107+});modified:test-d/convert/writable.test-d.ts+3 added
18 unmodified lines19 expectError(subprocess.writable({preserveNewlines: false}));20 21 expectError(subprocess.writable('stdin'));22 expectError(subprocess.writable({other: 'stdin'}));23+24+const inputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}]});25+inputPipeSubprocess.writable({to: 'fd3'});modified:test-d/return/result-stdio.test-d.ts+26 added
109 unmodified lines110 111 const inputFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', ['pipe', new Readable()]]});112 expectType<undefined>(inputFd3Result.stdio[3]);113 114+const inputPipeFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}]});115+expectType<undefined>(inputPipeFd3Result.stdio[3]);116+117+const outputPipeFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe'}]});118+expectType<string>(outputPipeFd3Result.stdio[3]);119+120+const input = true as boolean;121+const booleanInputPipeFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input}]});122+expectType<string | undefined>(booleanInputPipeFd3Result.stdio[3]);123+124+const optionalInputPipe: {readonly value: 'pipe'; readonly input?: boolean} = {value: 'pipe', input};125+const optionalInputPipeFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', optionalInputPipe]});126+expectType<string | undefined>(optionalInputPipeFd3Result.stdio[3]);127+128+const optionalTrueInputPipe: {readonly value: 'pipe'; readonly input?: true} = {value: 'pipe', input: true};129+const optionalTrueInputPipeFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', optionalTrueInputPipe]});130+expectType<string | undefined>(optionalTrueInputPipeFd3Result.stdio[3]);131+132+const stdoutInputPipeResult = await execa('unicorns', {stdout: {value: 'pipe', input: true}});133+expectType<string>(stdoutInputPipeResult.stdout);134+expectType<string>(stdoutInputPipeResult.stdio[1]);135+136+const stderrInputPipeResult = await execa('unicorns', {stderr: {value: 'pipe', input: true}});137+expectType<string>(stderrInputPipeResult.stderr);138+expectType<string>(stderrInputPipeResult.stdio[2]);139+140 const outputFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', ['pipe', new Writable()]]});141 expectType<string>(outputFd3Result.stdio[3]);142 143 const bufferFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'pipe'], encoding: 'buffer'});7 unmodified linesadded:test-d/stdio/option/input-pipe.test-d.ts+49 added
1+import {expectError, expectAssignable} from 'tsd';2+import {3+ execa,4+ execaSync,5+ type StdinOption,6+ type StdinSyncOption,7+ type StdoutStderrOption,8+ type StdoutStderrSyncOption,9+} from '../../../index.js';10+11+const inputPipe = {value: 'pipe', input: true} as const;12+const input = true as boolean;13+const booleanInputPipe = {value: 'pipe', input} as const;14+15+await execa('unicorns', {stdin: inputPipe});16+execaSync('unicorns', {stdin: inputPipe});17+await execa('unicorns', {stdout: inputPipe});18+execaSync('unicorns', {stdout: inputPipe});19+await execa('unicorns', {stderr: inputPipe});20+execaSync('unicorns', {stderr: inputPipe});21+22+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', inputPipe]});23+expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', inputPipe]}));24+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', booleanInputPipe]});25+expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', booleanInputPipe]}));26+27+await execa('unicorns', {stdin: {value: 'pipe'}});28+await execa('unicorns', {stdin: {value: 'overlapped', input: true}});29+expectError(execaSync('unicorns', {stdin: {value: 'overlapped', input: true}}));30+31+expectError(await execa('unicorns', {stdin: {value: 'other', input: true}}));32+expectError(await execa('unicorns', {stdin: {value: 'pipe', input: 'true'}}));33+34+expectAssignable<StdinOption>(inputPipe);35+expectAssignable<StdinOption>(booleanInputPipe);36+expectAssignable<StdinSyncOption>(inputPipe);37+expectAssignable<StdinSyncOption>(booleanInputPipe);38+expectAssignable<StdoutStderrOption>(inputPipe);39+expectAssignable<StdoutStderrOption>(booleanInputPipe);40+expectAssignable<StdoutStderrSyncOption>(inputPipe);41+expectAssignable<StdoutStderrSyncOption>(booleanInputPipe);42+expectAssignable<StdinOption>([inputPipe]);43+expectAssignable<StdinOption>([booleanInputPipe]);44+expectAssignable<StdinSyncOption>([inputPipe]);45+expectAssignable<StdinSyncOption>([booleanInputPipe]);46+expectAssignable<StdoutStderrOption>([inputPipe]);47+expectAssignable<StdoutStderrOption>([booleanInputPipe]);48+expectAssignable<StdoutStderrSyncOption>([inputPipe]);49+expectAssignable<StdoutStderrSyncOption>([booleanInputPipe]);modified:test-d/subprocess/stdio.test-d.ts+26 added
37 unmodified lines38 expectType<Readable>(multipleStdoutSubprocess.stderr);39 expectType<Readable>(multipleStdoutSubprocess.stdio[2]);40 expectType<Readable>(multipleStdoutSubprocess.all);41 expectError(multipleStdoutSubprocess.stdio[3].destroy());42+43+const inputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}]});44+expectType<Writable>(inputPipeSubprocess.stdio[3]);45+46+const outputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe'}]});47+expectType<Readable>(outputPipeSubprocess.stdio[3]);48+49+const input = true as boolean;50+const booleanInputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input}]});51+expectType<Readable | Writable>(booleanInputPipeSubprocess.stdio[3]);52+53+const optionalInputPipe: {readonly value: 'pipe'; readonly input?: boolean} = {value: 'pipe', input};54+const optionalInputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', optionalInputPipe]});55+expectType<Readable | Writable>(optionalInputPipeSubprocess.stdio[3]);56+57+const optionalTrueInputPipe: {readonly value: 'pipe'; readonly input?: true} = {value: 'pipe', input: true};58+const optionalTrueInputPipeSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', optionalTrueInputPipe]});59+expectType<Readable | Writable>(optionalTrueInputPipeSubprocess.stdio[3]);60+61+const stdoutInputPipeSubprocess = execa('unicorns', {stdout: {value: 'pipe', input: true}});62+expectType<Readable>(stdoutInputPipeSubprocess.stdout);63+expectType<Readable>(stdoutInputPipeSubprocess.stdio[1]);64+65+const stderrInputPipeSubprocess = execa('unicorns', {stderr: {value: 'pipe', input: true}});66+expectType<Readable>(stderrInputPipeSubprocess.stderr);67+expectType<Readable>(stderrInputPipeSubprocess.stdio[2]);modified:types/return/ignore.d.ts+7 added-3 removed
8 unmodified lines9 FdNumber extends string,10 OptionsType extends CommonOptions,11 > = FdSpecificOption<OptionsType['buffer'], FdNumber> extends false12 ? true13- : IsInputFd<FdNumber, OptionsType> extends true14- ? true15- : IgnoresSubprocessOutput<FdNumber, OptionsType>;13+ : IgnoresResultOutputDirection<IsInputFd<FdNumber, OptionsType>, FdNumber, OptionsType>;14 15+type IgnoresResultOutputDirection<16+ IsInput extends boolean,17+ FdNumber extends string,18+ OptionsType extends CommonOptions,19+> = IsInput extends true ? true : IgnoresSubprocessOutput<FdNumber, OptionsType>;20+21 // Whether `subprocess.stdout|stderr|all` is `undefined|null`22 export type IgnoresSubprocessOutput<23 FdNumber extends string,24 OptionsType extends CommonOptions,8 unmodified linesmodified:types/stdio/direction.d.ts+33 added-2 removed
1 import type {CommonOptions} from '../arguments/options.js';2 import type {Intersects} from '../utils.js';3-import type {StdioSingleOptionItems, InputStdioOption} from './type.js';3+import type {StdioSingleOptionItems, InputStdioOption, NativePipeStdioOption} from './type.js';4 import type {FdStdioArrayOption} from './option.js';5 6+type AnyNativePipeStdioOption = NativePipeStdioOption<boolean, false>;7+8+type InputNativePipeStdioOption = AnyNativePipeStdioOption & {9+ readonly input: true;10+};11+12+type NativePipeInputValue<StdioOptionType> = StdioOptionType extends AnyNativePipeStdioOption13+ ? NativePipeInputProperty<StdioOptionType>14+ : never;15+16+type NativePipeInputProperty<StdioOptionType> = 'input' extends keyof StdioOptionType17+ ? StdioOptionType extends {readonly input?: infer Input} ? Input : never18+ : never;19+20 // Whether `result.stdio[FdNumber]` is an input stream21 export type IsInputFd<22 FdNumber extends string,23 OptionsType extends CommonOptions,24 > = FdNumber extends '0'25 ? true12- : Intersects<StdioSingleOptionItems<FdStdioArrayOption<FdNumber, OptionsType>>, InputStdioOption>;26+ : IsInputFdOption<27+ FdNumber,28+ StdioSingleOptionItems<FdStdioArrayOption<FdNumber, OptionsType>>29+ >;30 31+type IsInputFdOption<32+ FdNumber extends string,33+ StdioOptionType,34+> = Intersects<35+ StdioOptionType,36+ FdNumber extends '1' | '2' ? InputStdioOption : InputStdioOption | InputNativePipeStdioOption37+> extends true38+ ? true39+ : FdNumber extends '1' | '2'40+ ? false41+ : true extends NativePipeInputValue<StdioOptionType>42+ ? boolean43+ : false;44+45 export {};modified:types/stdio/type.d.ts+9 added-1 removed
37 unmodified lines38 | Unless<And<And<Not<IsSync>, IsArray>, IsExtra>, 'inherit'>39 | Unless<IsArray, 'ignore'>40 | Unless<IsSync, 'overlapped'>;41 42+export type NativePipeStdioOption<43+ IsSync extends boolean,44+ IsExtra extends boolean,45+> = {46+ readonly value: 'pipe' | Unless<IsSync, 'overlapped'>;47+ readonly input?: Unless<And<IsSync, IsExtra>, boolean> | false;48+};49+50 // Values available in both `options.stdin|stdio` and `options.stdout|stderr|stdio`51 type CommonStdioOption<52 IsSync extends boolean,53 IsExtra extends boolean,54 IsArray extends boolean,55 > =48- SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync> | GeneratorTransformFull<IsSync> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | {readonly file: string; readonly append?: boolean};56+ SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync> | GeneratorTransformFull<IsSync> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | NativePipeStdioOption<IsSync, IsExtra> | {readonly file: string; readonly append?: boolean};57 58 // Synchronous iterables excluding strings, Uint8Arrays and Arrays59 type IterableObject<IsArray extends boolean> = Iterable<unknown>60 & object111 unmodified lines