Fix `stdio` option (#634)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent b495146 · Browse files · 13 files·+186 added−45 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+186 added−45 removed
- tests:+57 added−5 removed
- other:+129 added−40 removed
Provenance
- Commit
640ae39- Tree
8c06514- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b495146
13 files changed+186 added−45 removed
modified:lib/stdio/handle.js+18 added-11 removed
1123457 unmodified lines1314151617181920212217181920214 unmodified lines2627282935303132333435363738394041424344454647486 unmodified lines5556575851596061545562import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';import {getStdioOptionType, isRegularUrl, isUnknownStdioString, isInputDirection} 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 mode7 unmodified lines
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', }; let stdioStream = getInitialStdioStream(stdioOption, index);
stdioStream = handleInputOption(stdioStream, index, input); stdioStream = handleInputFileOption(stdioStream, index, inputFile, input);
4 unmodified lines ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream), };};
const OPTION_NAMES = ['stdin', 'stdout', 'stderr'];const getInitialStdioStream = (stdioOption, index) => { const type = getStdioOptionType(stdioOption); const { optionName = `stdio[${index}]`, direction = isInputDirection[type](stdioOption) ? 'input' : 'output', } = KNOWN_STREAMS[index] ?? {}; return {type, value: stdioOption, optionName, direction};};
const KNOWN_STREAMS = [ {optionName: 'stdin', direction: 'input'}, {optionName: 'stdout', direction: 'output'}, {optionName: 'stderr', direction: 'output'},];
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.`);6 unmodified lines
// 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.map((stdioItem, index) => transformStdioItem(stdioItem, stdioStreams[index])) : stdio;
const transformStdioItem = (stdioItem, index, stdioStreams) => stdioStreams[index].type !== 'native' && stdioItem !== 'overlapped' ? 'pipe' : stdioItem;const transformStdioItem = (stdioItem, {type}) => type !== 'native' && stdioItem !== 'overlapped' ? 'pipe' : stdioItem;modified:lib/stdio/sync.js+9 added
1234562 unmodified lines9101112131415161718192021222 unmodified lines25262728293031323334353637383926 unmodified linesimport {readFileSync, writeFileSync} from 'node:fs';import {isStream as isNodeStream} from 'is-stream';import {handleInput} from './handle.js';import {TYPE_TO_MESSAGE} from './type.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode2 unmodified lines addInputOptionSync(stdioStreams, options); return stdioStreams;};
const forbiddenIfStreamSync = ({value, optionName}) => { if (isNodeStream(value)) { forbiddenIfSync({type: 'nodeStream', optionName}); }};
const forbiddenIfSync = ({type, optionName}) => { throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);};
2 unmodified lines filePath: ({value}) => ({value: readFileSync(value), type: 'stringOrBuffer'}), webStream: forbiddenIfSync, nodeStream: forbiddenIfSync, iterable: forbiddenIfSync, native: forbiddenIfStreamSync, }, output: { webStream: forbiddenIfSync, nodeStream: forbiddenIfSync, iterable: forbiddenIfSync, native: forbiddenIfStreamSync, },};
const addInputOptionSync = (stdioStreams, options) => {26 unmodified linesmodified:lib/stdio/type.js+16 added-1 removed
12234567891033 unmodified lines444546474849505152535455565758596061625 unmodified linesimport {isAbsolute} from 'node:path';import {isStream as isNodeStream} from 'is-stream';import { isStream as isNodeStream, isReadableStream as isNodeReadableStream, isWritableStream as isNodeWritableStream,} from 'is-stream';
// The `stdin`/`stdout`/`stderr` option can be of many types. This detects it.export const getStdioOptionType = stdioOption => { if (isFileUrl(stdioOption) || isFilePath(stdioOption)) {33 unmodified linesconst isIterableObject = stdinOption => typeof stdinOption === 'object' && stdinOption !== null && (typeof stdinOption[Symbol.asyncIterator] === 'function' || typeof stdinOption[Symbol.iterator] === 'function');
// For `stdio[index]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.// When ambiguous, we default to `output` since it is the most common use case for additional file descriptors.// For the same reason, Duplex streams and TransformStreams are considered as outputs.// `nodeStream` and `stringOrBuffer` types always apply to `stdin`, i.e. missing here.export const isInputDirection = { filePath: () => false, webStream: stdioOption => !isWritableStream(stdioOption), native: stdioOption => (isNodeReadableStream(stdioOption) && !isNodeWritableStream(stdioOption)) || stdioOption === 0, iterable: () => true,};
// Convert types to human-friendly strings for error messagesexport const TYPE_TO_MESSAGE = { filePath: 'a file path', webStream: 'a web stream',5 unmodified linesadded:test/fixtures/noop-fd3.js+6 added
123456#!/usr/bin/env nodeimport process from 'node:process';import {writeSync} from 'node:fs';
const fileDescriptorIndex = Number(process.argv[3] || 3);writeSync(fileDescriptorIndex, `${process.argv[2]}\n`);added:test/fixtures/stdin-fd3.js+6 added
123456#!/usr/bin/env nodeimport process from 'node:process';import {readFileSync} from 'node:fs';
const fileDescriptorIndex = Number(process.argv[3] || 3);console.log(readFileSync(fileDescriptorIndex, {encoding: 'utf8'}));modified:test/helpers/stdio.js+1 added
123456783 unmodified linesexport const getStdinOption = stdioOption => ({stdin: stdioOption});export const getStdoutOption = stdioOption => ({stdout: stdioOption});export const getStderrOption = stdioOption => ({stderr: stdioOption});export const getStdioOption = stdioOption => ({stdio: ['pipe', 'pipe', 'pipe', stdioOption]});export const getPlainStdioOption = stdioOption => ({stdio: stdioOption});export const getInputOption = input => ({input});export const getInputFileOption = inputFile => ({inputFile});
3 unmodified linesmodified:test/stdio/file-descriptor.js+5 added-1 removed
1 unmodified line2 import test from 'ava';3 import tempfile from 'tempfile';4 import {execa, execaSync} from '../../index.js';5 import {setFixtureDir} from '../helpers/fixtures-dir.js';6-import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';6+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';7 8 setFixtureDir();9 10 const getStdinProp = ({stdin}) => stdin;11+const getStdioProp = ({stdio}) => stdio[3];12 13 const testFileDescriptorOption = async (t, fixtureName, getOptions, execaMethod) => {14 const filePath = tempfile();15 const fileDescriptor = await open(filePath, 'w');3 unmodified lines19 };20 21 test('pass `stdout` to a file descriptor', testFileDescriptorOption, 'noop.js', getStdoutOption, execa);22 test('pass `stderr` to a file descriptor', testFileDescriptorOption, 'noop-err.js', getStderrOption, execa);23+test('pass `stdio[*]` to a file descriptor', testFileDescriptorOption, 'noop-fd3.js', getStdioOption, execa);24 test('pass `stdout` to a file descriptor - sync', testFileDescriptorOption, 'noop.js', getStdoutOption, execaSync);25 test('pass `stderr` to a file descriptor - sync', testFileDescriptorOption, 'noop-err.js', getStderrOption, execaSync);26+test('pass `stdio[*]` to a file descriptor - sync', testFileDescriptorOption, 'noop-fd3.js', getStdioOption, execaSync);27 28 const testStdinWrite = async (t, getStreamProp, fixtureName, getOptions) => {29 const subprocess = execa(fixtureName, getOptions('pipe'));30 getStreamProp(subprocess).end('unicorns');1 unmodified line32 t.is(stdout, 'unicorns');33 };34 35 test('you can write to child.stdin', testStdinWrite, getStdinProp, 'stdin.js', getStdinOption);36+test('you can write to child.stdio[*]', testStdinWrite, getStdioProp, 'stdin-fd3.js', getStdioOption);modified:test/stdio/file-path.js+15 added-1 removed
4 unmodified lines5 import test from 'ava';6 import tempfile from 'tempfile';7 import {execa, execaSync, $} from '../../index.js';8 import {setFixtureDir} from '../helpers/fixtures-dir.js';9-import {getStdinOption, getStdoutOption, getStderrOption, getInputFileOption, getScriptSync, identity} from '../helpers/stdio.js';9+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption, getInputFileOption, getScriptSync, identity} from '../helpers/stdio.js';10 11 setFixtureDir();12 13 const nonFileUrl = new URL('https://example.com');30 unmodified lines44 };45 46 test('stdout can be a file URL', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execa);47 test('stderr can be a file URL', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execa);48+test('stdio[*] can be a file URL', testOutputFile, pathToFileURL, 'noop-fd3.js', getStdioOption, execa);49 test('stdout can be an absolute file path', testOutputFile, identity, 'noop.js', getStdoutOption, execa);50 test('stderr can be an absolute file path', testOutputFile, identity, 'noop-err.js', getStderrOption, execa);51+test('stdio[*] can be an absolute file path', testOutputFile, identity, 'noop-fd3.js', getStdioOption, execa);52 test('stdout can be a relative file path', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execa);53 test('stderr can be a relative file path', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execa);54+test('stdio[*] can be a relative file path', testOutputFile, getRelativePath, 'noop-fd3.js', getStdioOption, execa);55 test('stdout can be a file URL - sync', testOutputFile, pathToFileURL, 'noop.js', getStdoutOption, execaSync);56 test('stderr can be a file URL - sync', testOutputFile, pathToFileURL, 'noop-err.js', getStderrOption, execaSync);57+test('stdio[*] can be a file URL - sync', testOutputFile, pathToFileURL, 'noop-fd3.js', getStdioOption, execaSync);58 test('stdout can be an absolute file path - sync', testOutputFile, identity, 'noop.js', getStdoutOption, execaSync);59 test('stderr can be an absolute file path - sync', testOutputFile, identity, 'noop-err.js', getStderrOption, execaSync);60+test('stdio[*] can be an absolute file path - sync', testOutputFile, identity, 'noop-fd3.js', getStdioOption, execaSync);61 test('stdout can be a relative file path - sync', testOutputFile, getRelativePath, 'noop.js', getStdoutOption, execaSync);62 test('stderr can be a relative file path - sync', testOutputFile, getRelativePath, 'noop-err.js', getStderrOption, execaSync);63+test('stdio[*] can be a relative file path - sync', testOutputFile, getRelativePath, 'noop-fd3.js', getStdioOption, execaSync);64 65 const testStdioNonFileUrl = (t, getOptions, execaMethod) => {66 t.throws(() => {67 execaMethod('noop.js', getOptions(nonFileUrl));3 unmodified lines71 test('inputFile cannot be a non-file URL', testStdioNonFileUrl, getInputFileOption, execa);72 test('stdin cannot be a non-file URL', testStdioNonFileUrl, getStdinOption, execa);73 test('stdout cannot be a non-file URL', testStdioNonFileUrl, getStdoutOption, execa);74 test('stderr cannot be a non-file URL', testStdioNonFileUrl, getStderrOption, execa);75+test('stdio[*] cannot be a non-file URL', testStdioNonFileUrl, getStdioOption, execa);76 test('inputFile cannot be a non-file URL - sync', testStdioNonFileUrl, getInputFileOption, execaSync);77 test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, getStdinOption, execaSync);78 test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, getStdoutOption, execaSync);79 test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, getStderrOption, execaSync);80+test('stdio[*] cannot be a non-file URL - sync', testStdioNonFileUrl, getStdioOption, execaSync);81 82 const testInputFileValidUrl = async (t, execaMethod) => {83 const filePath = tempfile();84 await writeFile(filePath, 'foobar');20 unmodified lines105 106 test('stdin must start with . when being a relative file path', testStdioValidUrl, getStdinOption, execa);107 test('stdout must start with . when being a relative file path', testStdioValidUrl, getStdoutOption, execa);108 test('stderr must start with . when being a relative file path', testStdioValidUrl, getStderrOption, execa);109+test('stdio[*] must start with . when being a relative file path', testStdioValidUrl, getStdioOption, execa);110 test('stdin must start with . when being a relative file path - sync', testStdioValidUrl, getStdinOption, execaSync);111 test('stdout must start with . when being a relative file path - sync', testStdioValidUrl, getStdoutOption, execaSync);112 test('stderr must start with . when being a relative file path - sync', testStdioValidUrl, getStderrOption, execaSync);113+test('stdio[*] must start with . when being a relative file path - sync', testStdioValidUrl, getStdioOption, execaSync);114 115 const testFileError = async (t, mapFile, getOptions) => {116 await t.throwsAsync(117 execa('noop.js', getOptions(mapFile('./unknown/file'))),4 unmodified lines122 test('inputFile file URL errors should be handled', testFileError, pathToFileURL, getInputFileOption);123 test('stdin file URL errors should be handled', testFileError, pathToFileURL, getStdinOption);124 test('stdout file URL errors should be handled', testFileError, pathToFileURL, getStdoutOption);125 test('stderr file URL errors should be handled', testFileError, pathToFileURL, getStderrOption);126+test('stdio[*] file URL errors should be handled', testFileError, pathToFileURL, getStdioOption);127 test('inputFile file path errors should be handled', testFileError, identity, getInputFileOption);128 test('stdin file path errors should be handled', testFileError, identity, getStdinOption);129 test('stdout file path errors should be handled', testFileError, identity, getStdoutOption);130 test('stderr file path errors should be handled', testFileError, identity, getStderrOption);131+test('stdio[*] file path errors should be handled', testFileError, identity, getStdioOption);132 133 const testFileErrorSync = (t, mapFile, getOptions) => {134 t.throws(() => {135 execaSync('noop.js', getOptions(mapFile('./unknown/file')));3 unmodified lines139 test('inputFile file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getInputFileOption);140 test('stdin file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdinOption);141 test('stdout file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdoutOption);142 test('stderr file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStderrOption);143+test('stdio[*] file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, getStdioOption);144 test('inputFile file path errors should be handled - sync', testFileErrorSync, identity, getInputFileOption);145 test('stdin file path errors should be handled - sync', testFileErrorSync, identity, getStdinOption);146 test('stdout file path errors should be handled - sync', testFileErrorSync, identity, getStdoutOption);147 test('stderr file path errors should be handled - sync', testFileErrorSync, identity, getStderrOption);148+test('stdio[*] file path errors should be handled - sync', testFileErrorSync, identity, getStdioOption);149 150 const testInputFile = async (t, execaMethod) => {151 const inputFile = tempfile();152 await writeFile(inputFile, 'foobar');24 unmodified linesmodified:test/stdio/iterable.js+8 added-1 removed
1 import test from 'ava';2 import {execa, execaSync} from '../../index.js';3 import {setFixtureDir} from '../helpers/fixtures-dir.js';4-import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';4+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';5 6 setFixtureDir();7 8 const textEncoder = new TextEncoder();18 unmodified lines27 t.is(stdout, 'foobar');28 };29 30 test('stdin option can be a sync iterable of strings', testIterable, ['foo', 'bar'], 'stdin.js', getStdinOption);31+test('stdio[*] option can be a sync iterable of strings', testIterable, ['foo', 'bar'], 'stdin-fd3.js', getStdioOption);32 test('stdin option can be a sync iterable of Uint8Arrays', testIterable, [binaryFoo, binaryBar], 'stdin.js', getStdinOption);33+test('stdio[*] option can be a sync iterable of Uint8Arrays', testIterable, [binaryFoo, binaryBar], 'stdin-fd3.js', getStdioOption);34 test('stdin option can be an sync iterable of strings', testIterable, stringGenerator(), 'stdin.js', getStdinOption);35+test('stdio[*] option can be an sync iterable of strings', testIterable, stringGenerator(), 'stdin-fd3.js', getStdioOption);36 test('stdin option can be an sync iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin.js', getStdinOption);37+test('stdio[*] option can be an sync iterable of Uint8Arrays', testIterable, binaryGenerator(), 'stdin-fd3.js', getStdioOption);38 39 const testIterableSync = (t, stdioOption, fixtureName, getOptions) => {40 t.throws(() => {41 execaSync(fixtureName, getOptions(stdioOption));42 }, {message: /an iterable in sync mode/});43 };44 45 test('stdin option cannot be a sync iterable - sync', testIterableSync, ['foo', 'bar'], 'stdin.js', getStdinOption);46+test('stdio[*] option cannot be a sync iterable - sync', testIterableSync, ['foo', 'bar'], 'stdin-fd3.js', getStdioOption);47 test('stdin option cannot be an async iterable - sync', testIterableSync, stringGenerator(), 'stdin.js', getStdinOption);48+test('stdio[*] option cannot be an async iterable - sync', testIterableSync, stringGenerator(), 'stdin-fd3.js', getStdioOption);49 50 const testIterableError = async (t, fixtureName, getOptions) => {51 const {originalMessage} = await t.throwsAsync(execa(fixtureName, getOptions(throwingGenerator())));52 t.is(originalMessage, 'generator error');53 };54 55 test('stdin option handles errors in iterables', testIterableError, 'stdin.js', getStdinOption);56+test('stdio[*] option handles errors in iterables', testIterableError, 'stdin-fd3.js', getStdioOption);57 58 const testNoIterableOutput = (t, getOptions, execaMethod) => {59 t.throws(() => {60 execaMethod('noop.js', getOptions(['foo', 'bar']));7 unmodified linesmodified:test/stdio/node-stream.js+10 added-1 removed
4 unmodified lines5 import test from 'ava';6 import tempfile from 'tempfile';7 import {execa, execaSync} from '../../index.js';8 import {setFixtureDir} from '../helpers/fixtures-dir.js';9-import {getStdinOption, getStdoutOption, getStderrOption, getInputOption} from '../helpers/stdio.js';9+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption, getInputOption} from '../helpers/stdio.js';10 11 setFixtureDir();12 13 const createNoFileReadable = value => {9 unmodified lines23 }, {message: `The \`${optionName}\` option cannot be a Node.js stream in sync mode.`});24 };25 26 test('input cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, getInputOption, 'input');27+test('stdin cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, getStdinOption, 'stdin');28+test('stdio[*] cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, getStdioOption, 'stdio[3]');29+test('stdout cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, getStdoutOption, 'stdout');30+test('stderr cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, getStderrOption, 'stderr');31+test('stdio[*] cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, getStdioOption, 'stdio[3]');32 33 test('input can be a Node.js Readable without a file descriptor', async t => {34 const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});35 t.is(stdout, 'foobar');5 unmodified lines41 42 test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, getStdinOption, Readable);43 test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, getStdoutOption, Writable);44 test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, getStderrOption, Writable);45+test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, getStdioOption, Readable);46+test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, getStdioOption, Writable);47 48 const testFileReadable = async (t, fixtureName, getOptions) => {49 const filePath = tempfile();50 await writeFile(filePath, 'foobar');7 unmodified lines58 };59 60 test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin.js', getInputOption);61 test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin.js', getStdinOption);62+test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin-fd3.js', getStdioOption);63 64 const testFileWritable = async (t, getOptions, fixtureName) => {65 const filePath = tempfile();66 const stream = createWriteStream(filePath);6 unmodified lines73 };74 75 test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, getStdoutOption, 'noop.js');76 test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, getStderrOption, 'noop-err.js');77+test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, getStdioOption, 'noop-fd3.js');modified:test/stdio/web-stream.js+6 added-1 removed
1 import {Readable} from 'node:stream';2 import test from 'ava';3 import {execa, execaSync} from '../../index.js';4 import {setFixtureDir} from '../helpers/fixtures-dir.js';5-import {getStdinOption, getStdoutOption, getStderrOption} from '../helpers/stdio.js';5+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';6 7 setFixtureDir();8 9 const testReadableStream = async (t, fixtureName, getOptions) => {2 unmodified lines12 t.is(stdout, 'foobar');13 };14 15 test('stdin can be a ReadableStream', testReadableStream, 'stdin.js', getStdinOption);16+test('stdio[*] can be a ReadableStream', testReadableStream, 'stdin-fd3.js', getStdioOption);17 18 const testWritableStream = async (t, fixtureName, getOptions) => {19 const result = [];20 const writableStream = new WritableStream({6 unmodified lines27 };28 29 test('stdout can be a WritableStream', testWritableStream, 'noop.js', getStdoutOption);30 test('stderr can be a WritableStream', testWritableStream, 'noop-err.js', getStderrOption);31+test('stdio[*] can be a WritableStream', testWritableStream, 'noop-fd3.js', getStdioOption);32 33 const testWebStreamSync = (t, StreamClass, getOptions, optionName) => {34 t.throws(() => {35 execaSync('noop.js', getOptions(new StreamClass()));36 }, {message: `The \`${optionName}\` option cannot be a web stream in sync mode.`});37 };38 39 test('stdin cannot be a ReadableStream - sync', testWebStreamSync, ReadableStream, getStdinOption, 'stdin');40+test('stdio[*] cannot be a ReadableStream - sync', testWebStreamSync, ReadableStream, getStdioOption, 'stdio[3]');41 test('stdout cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStdoutOption, 'stdout');42 test('stderr cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStderrOption, 'stderr');43+test('stdio[*] cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStdioOption, 'stdio[3]');44 45 const testWritableStreamError = async (t, getOptions) => {46 const writableStream = new WritableStream({47 start(controller) {5 unmodified lines53 };54 55 test('stdout option handles errors in WritableStream', testWritableStreamError, getStdoutOption);56 test('stderr option handles errors in WritableStream', testWritableStreamError, getStderrOption);57+test('stdio[*] option handles errors in WritableStream', testWritableStreamError, getStdioOption);modified:index.d.ts+23 added-15 removed
1 import {type ChildProcess} from 'node:child_process';2-import {type Stream, type Readable, type Writable} from 'node:stream';2+import {type Readable, type Writable} from 'node:stream';3 4-export type StdioOption =4+type BaseStdioOption =5 | 'pipe'6 | 'overlapped'7- | 'ipc'7 | 'ignore'9- | 'inherit'10- | Stream8+ | 'inherit';9+10+type CommonStdioOption =11+ | BaseStdioOption12+ | 'ipc'13 | number12- | undefined;14+ | undefined15+ | URL16+ | string;17 14-export type StdinOption =15- | StdioOption18+type InputStdioOption =19 | Iterable<string | Uint8Array>20 | AsyncIterable<string | Uint8Array>18- | URL19- | string21+ | Readable22 | ReadableStream;23 22-export type StdoutStderrOption =23- | StdioOption24- | URL25- | string24+type OutputStdioOption =25+ | Writable26 | WritableStream;27 28+export type StdinOption = CommonStdioOption | InputStdioOption;29+export type StdoutStderrOption = CommonStdioOption | OutputStdioOption;30+export type StdioOption = CommonStdioOption | InputStdioOption | OutputStdioOption;31+32+type StdioOptions =33+ | BaseStdioOption34+ | readonly [StdinOption, StdoutStderrOption, StdoutStderrOption, ...StdioOption[]];35+36 type EncodingOption =37 | 'utf8'38 // eslint-disable-next-line unicorn/text-encoding-identifier-case39 | 'utf-8'175 unmodified lines215 - an array with more than 3 values, to create more than 3 file descriptors.216 217 @default 'pipe'218 */211- readonly stdio?: 'pipe' | 'overlapped' | 'ignore' | 'inherit' | readonly StdioOption[];219+ readonly stdio?: StdioOptions;220 221 /**222 Specify the kind of serialization used for sending messages between processes when using the `stdio: 'ipc'` option or `execaNode()`:223 - `json`: Uses `JSON.stringify()` and `JSON.parse()`.846 unmodified linesmodified:index.test-d.ts+63 added-13 removed
1 // For some reason a default import of `process` causes2 // `process.stdin`, `process.stderr`, and `process.stdout`3 // to get treated as `any` by `@typescript-eslint/no-unsafe-assignment`.4 import * as process from 'node:process';5-import {type Readable} from 'node:stream';5+import {Readable, Writable} from 'node:stream';6 import {createWriteStream} from 'node:fs';7 import {expectType, expectError, expectAssignable} from 'tsd';8 import {9 $,128 unmodified lines138 const numberGenerator = function * () {139 yield 0;140 };141 142+const asyncStringGenerator = async function * () {143+ yield '';144+};145+146+const fileUrl = new URL('file:///test');147+148 /* eslint-disable @typescript-eslint/no-floating-promises */149 execa('unicorns', {cleanup: false});150 execa('unicorns', {preferLocal: false});151 execa('unicorns', {localDir: '.'});146-execa('unicorns', {localDir: new URL('file:///test')});152+execa('unicorns', {localDir: fileUrl});153 expectError(execa('unicorns', {encoding: 'unknownEncoding'}));154 execa('unicorns', {execPath: '/path'});149-execa('unicorns', {execPath: new URL('file:///test')});155+execa('unicorns', {execPath: fileUrl});156 execa('unicorns', {buffer: false});157 execa('unicorns', {input: ''});158 execa('unicorns', {input: new Uint8Array()});159 execa('unicorns', {input: process.stdin});160 execa('unicorns', {inputFile: ''});155-execa('unicorns', {inputFile: new URL('file:///test')});161+execa('unicorns', {inputFile: fileUrl});162 execa('unicorns', {stdin: 'pipe'});163 execa('unicorns', {stdin: 'overlapped'});164 execa('unicorns', {stdin: 'ipc'});165 execa('unicorns', {stdin: 'ignore'});166 execa('unicorns', {stdin: 'inherit'});167 execa('unicorns', {stdin: process.stdin});168+execa('unicorns', {stdin: new Readable()});169+expectError(execa('unicorns', {stdin: new Writable()}));170 execa('unicorns', {stdin: new ReadableStream()});171+expectError(execa('unicorns', {stdin: new WritableStream()}));172 execa('unicorns', {stdin: ['']});173 execa('unicorns', {stdin: [new Uint8Array(0)]});174 execa('unicorns', {stdin: stringGenerator()});175 execa('unicorns', {stdin: binaryGenerator()});176+execa('unicorns', {stdin: asyncStringGenerator()});177 expectError(execa('unicorns', {stdin: [0]}));178 expectError(execa('unicorns', {stdin: numberGenerator()}));169-execa('unicorns', {stdin: new URL('file:///test')});179+execa('unicorns', {stdin: fileUrl});180 execa('unicorns', {stdin: './test'});181 execa('unicorns', {stdin: 1});182 execa('unicorns', {stdin: undefined});183 execa('unicorns', {stdout: 'pipe'});1 unmodified line185 execa('unicorns', {stdout: 'ipc'});186 execa('unicorns', {stdout: 'ignore'});187 execa('unicorns', {stdout: 'inherit'});188 execa('unicorns', {stdout: process.stdout});189+execa('unicorns', {stdout: new Writable()});190+expectError(execa('unicorns', {stdout: new Readable()}));191 execa('unicorns', {stdout: new WritableStream()});180-execa('unicorns', {stdout: new URL('file:///test')});192+expectError(execa('unicorns', {stdout: new ReadableStream()}));193+execa('unicorns', {stdout: fileUrl});194 execa('unicorns', {stdout: './test'});195 execa('unicorns', {stdout: 1});196 execa('unicorns', {stdout: undefined});197 execa('unicorns', {stderr: 'pipe'});1 unmodified line199 execa('unicorns', {stderr: 'ipc'});200 execa('unicorns', {stderr: 'ignore'});201 execa('unicorns', {stderr: 'inherit'});202 execa('unicorns', {stderr: process.stderr});203+execa('unicorns', {stderr: new Writable()});204+expectError(execa('unicorns', {stderr: new Readable()}));205 execa('unicorns', {stderr: new WritableStream()});191-execa('unicorns', {stderr: new URL('file:///test')});206+expectError(execa('unicorns', {stderr: new ReadableStream()}));207+execa('unicorns', {stderr: fileUrl});208 execa('unicorns', {stderr: './test'});209 execa('unicorns', {stderr: 1});210 execa('unicorns', {stderr: undefined});211 execa('unicorns', {all: true});212 execa('unicorns', {reject: false});213 execa('unicorns', {stripFinalNewline: false});214 execa('unicorns', {extendEnv: false});215 execa('unicorns', {cwd: '.'});200-execa('unicorns', {cwd: new URL('file:///test')});216+execa('unicorns', {cwd: fileUrl});217 // eslint-disable-next-line @typescript-eslint/naming-convention218 execa('unicorns', {env: {PATH: ''}});219 execa('unicorns', {argv0: ''});220 execa('unicorns', {stdio: 'pipe'});221 execa('unicorns', {stdio: 'overlapped'});222 execa('unicorns', {stdio: 'ignore'});223 execa('unicorns', {stdio: 'inherit'});224+expectError(execa('unicorns', {stdio: 'ipc'}));225+expectError(execa('unicorns', {stdio: 1}));226+expectError(execa('unicorns', {stdio: fileUrl}));227+expectError(execa('unicorns', {stdio: './test'}));228+expectError(execa('unicorns', {stdio: new Writable()}));229+expectError(execa('unicorns', {stdio: new Readable()}));230+expectError(execa('unicorns', {stdio: new WritableStream()}));231+expectError(execa('unicorns', {stdio: new ReadableStream()}));232+expectError(execa('unicorns', {stdio: stringGenerator()}));233+expectError(execa('unicorns', {stdio: asyncStringGenerator()}));234+expectError(execa('unicorns', {stdio: ['pipe', 'pipe']}));235+execa('unicorns', {stdio: [new Readable(), 'pipe', 'pipe']});236+execa('unicorns', {stdio: ['pipe', new Writable(), 'pipe']});237+execa('unicorns', {stdio: ['pipe', 'pipe', new Writable()]});238+expectError(execa('unicorns', {stdio: [new Writable(), 'pipe', 'pipe']}));239+expectError(execa('unicorns', {stdio: ['pipe', new Readable(), 'pipe']}));240+expectError(execa('unicorns', {stdio: ['pipe', 'pipe', new Readable()]}));241 execa('unicorns', {209- stdio: ['pipe', 'overlapped', 'ipc', 'ignore', 'inherit', process.stdin, 1, undefined],242+ stdio: [243+ 'pipe',244+ 'overlapped',245+ 'ipc',246+ 'ignore',247+ 'inherit',248+ process.stdin,249+ 1,250+ undefined,251+ fileUrl,252+ './test',253+ new Writable(),254+ new Readable(),255+ new WritableStream(),256+ new ReadableStream(),257+ stringGenerator(),258+ asyncStringGenerator(),259+ ],260 });261 execa('unicorns', {serialization: 'advanced'});262 execa('unicorns', {detached: true});263 execa('unicorns', {uid: 0});18 unmodified lines282 execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: undefined});283 284 expectError(execa(['unicorns', 'arg']));285 expectType<ExecaChildProcess>(execa('unicorns'));236-expectType<ExecaChildProcess>(execa(new URL('file:///test')));286+expectType<ExecaChildProcess>(execa(fileUrl));287 expectType<ExecaReturnValue>(await execa('unicorns'));288 expectType<ExecaReturnValue>(289 await execa('unicorns', {encoding: 'utf8'}),290 );6 unmodified lines297 );298 299 expectError(execaSync(['unicorns', 'arg']));300 expectType<ExecaSyncReturnValue>(execaSync('unicorns'));251-expectType<ExecaSyncReturnValue>(execaSync(new URL('file:///test')));301+expectType<ExecaSyncReturnValue>(execaSync(fileUrl));302 expectType<ExecaSyncReturnValue>(303 execaSync('unicorns', {encoding: 'utf8'}),304 );305 expectType<ExecaSyncReturnValue<Uint8Array>>(21 unmodified lines327 328 expectError(execaNode(['unicorns', 'arg']));329 expectType<ExecaChildProcess>(execaNode('unicorns'));330 expectType<ExecaReturnValue>(await execaNode('unicorns'));281-expectType<ExecaReturnValue>(await execaNode(new URL('file:///test')));331+expectType<ExecaReturnValue>(await execaNode(fileUrl));332 expectType<ExecaReturnValue>(333 await execaNode('unicorns', {encoding: 'utf8'}),334 );335 expectType<ExecaReturnValue<Uint8Array>>(await execaNode('unicorns', {encoding: 'buffer'}));4 unmodified lines340 await execaNode('unicorns', ['foo'], {encoding: 'buffer'}),341 );342 343 expectType<ExecaChildProcess>(execaNode('unicorns', {nodePath: './node'}));294-expectType<ExecaChildProcess>(execaNode('unicorns', {nodePath: new URL('file:///test')}));344+expectType<ExecaChildProcess>(execaNode('unicorns', {nodePath: fileUrl}));345 346 expectType<ExecaChildProcess>(execaNode('unicorns', {nodeOptions: ['--async-stack-traces']}));347 expectType<ExecaChildProcess>(execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces']}));348 expectType<ExecaChildProcess<Uint8Array>>(61 unmodified lines