Skip to content
Myr

Allow Node.js streams with synchronous methods (#708)

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

authored by ehmicky · committed by GitHub · parent 67b476b · Browse files · 6 files+84 added−75 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+84 added−75 removed

  • tests:+29 added−19 removed
  • other:+55 added−56 removed

6 files+84 added−75 removed

modified:lib/stdio/sync.js+1 added-12 removed
modified:test/helpers/stdio.js+1 added-1 removed
modified:test/stdio/node-stream.js+28 added-18 removed
15 unmodified lines
16 stream.end();17 return stream;18};1920const testNodeStreamSync = (t, StreamClass, index, optionName) => {20const testNoFileStreamSync = async (t, index, StreamClass) => {21 t.throws(() => {22 execaSync('empty.js', getStdio(index, new StreamClass()));23 }, {message: `The \`${optionName}\` option cannot be a Node.js stream in sync mode.`});23 }, {code: 'ERR_INVALID_ARG_VALUE'});24};2526test('input cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 'input', 'input');27test('stdin cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 0, 'stdin');28test('stdio[*] cannot be a Node.js Readable - sync', testNodeStreamSync, Readable, 3, 'stdio[3]');29test('stdout cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 1, 'stdout');30test('stderr cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 2, 'stderr');31test('stdio[*] cannot be a Node.js Writable - sync', testNodeStreamSync, Writable, 3, 'stdio[3]');26test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, Readable);27test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, Writable);28test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, Writable);29test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, Readable);30test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, Writable);3132test('input can be a Node.js Readable without a file descriptor', async t => {33 const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});34 t.is(stdout, 'foobar');35});3637test('input cannot be a Node.js Readable without a file descriptor - sync', t => {38 t.throws(() => {39 execaSync('empty.js', {input: createNoFileReadable('foobar')});40 }, {message: 'The `input` option cannot be a Node.js stream in sync mode.'});41});4243const testNoFileStream = async (t, index, StreamClass) => {44 await t.throwsAsync(execa('empty.js', getStdio(index, new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});45};46
2 unmodified lines
49test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, Writable);50test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, Readable);51test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, Writable);5248const testFileReadable = async (t, index) => {53const testFileReadable = async (t, index, execaMethod) => {54 const filePath = tempfile();55 await writeFile(filePath, 'foobar');56 const stream = createReadStream(filePath);57 await once(stream, 'open');5859 const indexString = index === 'input' ? '0' : `${index}`;55 const {stdout} = await execa('stdin-fd.js', [indexString], getStdio(index, stream));60 const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream));61 t.is(stdout, 'foobar');6263 await rm(filePath);64};6561test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');62test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0);63test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3);66test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input', execa);67test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0, execa);68test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);69test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);70test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);7165const testFileWritable = async (t, index) => {72const testFileWritable = async (t, index, execaMethod) => {73 const filePath = tempfile();74 const stream = createWriteStream(filePath);75 await once(stream, 'open');7670 await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));77 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));78 t.is(await readFile(filePath, 'utf8'), 'foobar');7980 await rm(filePath);81};8276test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1);77test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2);78test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3);83test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1, execa);84test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2, execa);85test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3, execa);86test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);87test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);88test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);8990const testLazyFileReadable = async (t, index) => {91 const filePath = tempfile();92 await writeFile(filePath, 'foobar');
24 unmodified lines
modified:index.d.ts+25 added-15 removed
36 unmodified lines
37 }>;3839type InputStdioOption<IsSync extends boolean = boolean> =40 | Uint8Array41 | Readable42 | IfAsync<IsSync,43 | Iterable<string | Uint8Array>44 | AsyncIterable<string | Uint8Array>44 | Readable45 | ReadableStream>;4647type OutputStdioOption<IsSync extends boolean = boolean> = IfAsync<IsSync,48| Writable49| WritableStream>;47type OutputStdioOption<IsSync extends boolean = boolean> =48 | Writable49 | IfAsync<IsSync, WritableStream>;5051type StdinSingleOption<IsSync extends boolean = boolean> =52 | CommonStdioOption<IsSync>53 | InputStdioOption<IsSync>;54export type StdinOption<IsSync extends boolean = boolean> =52 CommonStdioOption<IsSync> | InputStdioOption<IsSync>53 | Array<CommonStdioOption<IsSync> | InputStdioOption<IsSync>>;55 | StdinSingleOption<IsSync>56 | Array<StdinSingleOption<IsSync>>;57type StdoutStderrSingleOption<IsSync extends boolean = boolean> =58 | CommonStdioOption<IsSync>59 | OutputStdioOption<IsSync>;60export type StdoutStderrOption<IsSync extends boolean = boolean> =55 CommonStdioOption<IsSync> | OutputStdioOption<IsSync>56 | Array<CommonStdioOption<IsSync> | OutputStdioOption<IsSync>>;61 | StdoutStderrSingleOption<IsSync>62 | Array<StdoutStderrSingleOption<IsSync>>;63type StdioSingleOption<IsSync extends boolean = boolean> =64 | CommonStdioOption<IsSync>65 | InputStdioOption<IsSync>66 | OutputStdioOption<IsSync>;67export type StdioOption<IsSync extends boolean = boolean> =58 CommonStdioOption<IsSync> | InputStdioOption | OutputStdioOption<IsSync>59 | Array<CommonStdioOption<IsSync> | InputStdioOption | OutputStdioOption<IsSync>>;68 | StdioSingleOption<IsSync>69 | Array<StdioSingleOption<IsSync>>;7071type StdioOptionsArray<IsSync extends boolean = boolean> = readonly [72 StdinOption<IsSync>,73 StdoutStderrOption<IsSync>,
160 unmodified lines
234 Write some input to the child process' `stdin`.235236 See also the `inputFile` and `stdin` options.237 */228 readonly input?: string | Uint8Array | IfAsync<IsSync, Readable>;238 readonly input?: string | Uint8Array | Readable;239240 /**241 Use a file as input to the child process' `stdin`.242
645 unmodified lines
888889/**890Same as `execa()` but synchronous.891882Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be a stream nor an iterable.892Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.893894@param file - The program/script to execute, as a string or file URL895@param arguments - Arguments to pass to `file` on execution.896@returns A `childProcessResult` object
90 unmodified lines
987988/**989Same as `execaCommand()` but synchronous.990981Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be a stream nor an iterable.991Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.992993@param command - The program/script to execute and its arguments.994@returns A `childProcessResult` object995@throws A `childProcessResult` error
48 unmodified lines
10441045 /**1046 Same as $\`command\` but synchronous.10471038 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be a stream nor an iterable.1048 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.10491050 @returns A `childProcessResult` object1051 @throws A `childProcessResult` error1052
43 unmodified lines
10961097 /**1098 Same as $\`command\` but synchronous.10991090 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be a stream nor an iterable.1100 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization` and `signal`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.11011102 @returns A `childProcessResult` object1103 @throws A `childProcessResult` error1104
135 unmodified lines
modified:index.test-d.ts+23 added-23 removed
627 unmodified lines
628execaSync('unicorns', {input: ''});629execa('unicorns', {input: new Uint8Array()});630execaSync('unicorns', {input: new Uint8Array()});631execa('unicorns', {input: process.stdin});632expectError(execaSync('unicorns', {input: process.stdin}));632execaSync('unicorns', {input: process.stdin});633execa('unicorns', {inputFile: ''});634execaSync('unicorns', {inputFile: ''});635execa('unicorns', {inputFile: fileUrl});636execaSync('unicorns', {inputFile: fileUrl});
17 unmodified lines
654execaSync('unicorns', {stdin: 'inherit'});655execa('unicorns', {stdin: ['inherit']});656execaSync('unicorns', {stdin: ['inherit']});657execa('unicorns', {stdin: process.stdin});658expectError(execaSync('unicorns', {stdin: process.stdin}));658execaSync('unicorns', {stdin: process.stdin});659execa('unicorns', {stdin: [process.stdin]});660expectError(execaSync('unicorns', {stdin: [process.stdin]}));660execaSync('unicorns', {stdin: [process.stdin]});661execa('unicorns', {stdin: new Readable()});662expectError(execaSync('unicorns', {stdin: new Readable()}));662execaSync('unicorns', {stdin: new Readable()});663execa('unicorns', {stdin: [new Readable()]});664expectError(execaSync('unicorns', {stdin: [new Readable()]}));664execaSync('unicorns', {stdin: [new Readable()]});665expectError(execa('unicorns', {stdin: new Writable()}));666expectError(execaSync('unicorns', {stdin: new Writable()}));667expectError(execa('unicorns', {stdin: [new Writable()]}));668expectError(execaSync('unicorns', {stdin: [new Writable()]}));
81 unmodified lines
750execaSync('unicorns', {stdout: 'inherit'});751execa('unicorns', {stdout: ['inherit']});752execaSync('unicorns', {stdout: ['inherit']});753execa('unicorns', {stdout: process.stdout});754expectError(execaSync('unicorns', {stdout: process.stdout}));754execaSync('unicorns', {stdout: process.stdout});755execa('unicorns', {stdout: [process.stdout]});756expectError(execaSync('unicorns', {stdout: [process.stdout]}));756execaSync('unicorns', {stdout: [process.stdout]});757execa('unicorns', {stdout: new Writable()});758expectError(execaSync('unicorns', {stdout: new Writable()}));758execaSync('unicorns', {stdout: new Writable()});759execa('unicorns', {stdout: [new Writable()]});760expectError(execaSync('unicorns', {stdout: [new Writable()]}));760execaSync('unicorns', {stdout: [new Writable()]});761expectError(execa('unicorns', {stdout: new Readable()}));762expectError(execaSync('unicorns', {stdout: new Readable()}));763expectError(execa('unicorn', {stdout: [new Readable()]}));764expectError(execaSync('unicorn', {stdout: [new Readable()]}));
63 unmodified lines
828execaSync('unicorns', {stderr: 'inherit'});829execa('unicorns', {stderr: ['inherit']});830execaSync('unicorns', {stderr: ['inherit']});831execa('unicorns', {stderr: process.stderr});832expectError(execaSync('unicorns', {stderr: process.stderr}));832execaSync('unicorns', {stderr: process.stderr});833execa('unicorns', {stderr: [process.stderr]});834expectError(execaSync('unicorns', {stderr: [process.stderr]}));834execaSync('unicorns', {stderr: [process.stderr]});835execa('unicorns', {stderr: new Writable()});836expectError(execaSync('unicorns', {stderr: new Writable()}));836execaSync('unicorns', {stderr: new Writable()});837execa('unicorns', {stderr: [new Writable()]});838expectError(execaSync('unicorns', {stderr: [new Writable()]}));838execaSync('unicorns', {stderr: [new Writable()]});839expectError(execa('unicorns', {stderr: new Readable()}));840expectError(execaSync('unicorns', {stderr: new Readable()}));841expectError(execa('unicorns', {stderr: [new Readable()]}));842expectError(execaSync('unicorns', {stderr: [new Readable()]}));
95 unmodified lines
938expectError(execaSync('unicorns', {stdio: asyncStringGenerator()}));939expectError(execa('unicorns', {stdio: ['pipe', 'pipe']}));940expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe']}));941execa('unicorns', {stdio: [new Readable(), 'pipe', 'pipe']});942expectError(execaSync('unicorns', {stdio: [new Readable(), 'pipe', 'pipe']}));942execaSync('unicorns', {stdio: [new Readable(), 'pipe', 'pipe']});943execa('unicorns', {stdio: [[new Readable()], ['pipe'], ['pipe']]});944expectError(execaSync('unicorns', {stdio: [[new Readable()], ['pipe'], ['pipe']]}));944execaSync('unicorns', {stdio: [[new Readable()], ['pipe'], ['pipe']]});945execa('unicorns', {stdio: ['pipe', new Writable(), 'pipe']});946expectError(execaSync('unicorns', {stdio: ['pipe', new Writable(), 'pipe']}));946execaSync('unicorns', {stdio: ['pipe', new Writable(), 'pipe']});947execa('unicorns', {stdio: [['pipe'], [new Writable()], ['pipe']]});948expectError(execaSync('unicorns', {stdio: [['pipe'], [new Writable()], ['pipe']]}));948execaSync('unicorns', {stdio: [['pipe'], [new Writable()], ['pipe']]});949execa('unicorns', {stdio: ['pipe', 'pipe', new Writable()]});950expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', new Writable()]}));950execaSync('unicorns', {stdio: ['pipe', 'pipe', new Writable()]});951execa('unicorns', {stdio: [['pipe'], ['pipe'], [new Writable()]]});952expectError(execaSync('unicorns', {stdio: [['pipe'], ['pipe'], [new Writable()]]}));952execaSync('unicorns', {stdio: [['pipe'], ['pipe'], [new Writable()]]});953expectError(execa('unicorns', {stdio: [new Writable(), 'pipe', 'pipe']}));954expectError(execaSync('unicorns', {stdio: [new Writable(), 'pipe', 'pipe']}));955expectError(execa('unicorns', {stdio: [[new Writable()], ['pipe'], ['pipe']]}));956expectError(execaSync('unicorns', {stdio: [[new Writable()], ['pipe'], ['pipe']]}));
40 unmodified lines
997 1,998 undefined,999 fileUrl,1000 {file: './test'},1001 new Writable(),1002 new Readable(),1003 new Uint8Array(),1004 ],1005});1006expectError(execaSync('unicorns', {stdio: [stringOrUint8ArrayGenerator]}));1007expectError(execaSync('unicorns', {stdio: [{transform: stringOrUint8ArrayGenerator}]}));1006expectError(execaSync('unicorns', {stdio: [new Writable()]}));1007expectError(execaSync('unicorns', {stdio: [new Readable()]}));1008expectError(execaSync('unicorns', {stdio: [new WritableStream()]}));1009expectError(execaSync('unicorns', {stdio: [new ReadableStream()]}));1010expectError(execaSync('unicorns', {stdio: [emptyStringGenerator()]}));1011expectError(execaSync('unicorns', {stdio: [asyncStringGenerator()]}));
34 unmodified lines
1046 [1],1047 [undefined],1048 [fileUrl],1049 [{file: './test'}],1050 [new Writable()],1051 [new Readable()],1052 [new Uint8Array()],1053 ],1054});1055expectError(execaSync('unicorns', {stdio: [[stringOrUint8ArrayGenerator]]}));1056expectError(execaSync('unicorns', {stdio: [[{transform: stringOrUint8ArrayGenerator}]]}));1055expectError(execaSync('unicorns', {stdio: [[new Writable()]]}));1056expectError(execaSync('unicorns', {stdio: [[new Readable()]]}));1057expectError(execaSync('unicorns', {stdio: [[new WritableStream()]]}));1058expectError(execaSync('unicorns', {stdio: [[new ReadableStream()]]}));1059expectError(execaSync('unicorns', {stdio: [[emptyStringGenerator()]]}));1060expectError(execaSync('unicorns', {stdio: [[asyncStringGenerator()]]}));
141 unmodified lines
modified:readme.md+6 added-6 removed
290 unmodified lines
291### execaSync(file, arguments?, options?)292293Same as [`execa()`](#execacommandcommand-options) but synchronous.294295Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be a stream nor an iterable.295Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.296297Returns or throws a [`childProcessResult`](#childProcessResult).298299### $.sync\`command\`300### $.s\`command\`301302Same as [$\`command\`](#command) but synchronous.303304Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be a stream nor an iterable.304Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.305306Returns or throws a [`childProcessResult`](#childProcessResult).307308### execaCommandSync(command, options?)309310Same as [`execaCommand()`](#execacommand-command-options) but synchronous.311312Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be a stream nor an iterable.312Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization) and [`signal`](#signal). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.313314Returns or throws a [`childProcessResult`](#childProcessResult).315316### Shell syntax
515 unmodified lines
832The [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options can be an array of values.833The following example redirects `stdout` to both the terminal and an `output.txt` file, while also retrieving its value programmatically.834835```js836const {stdout} = await execa('npm', ['install'], {stdout: ['inherit', './output.txt', 'pipe']})836const {stdout} = await execa('npm', ['install'], {stdout: ['inherit', './output.txt', 'pipe']});837console.log(stdout);838```839840When combining `inherit` with other values, please note that the child process will not be an interactive TTY, even if the parent process is one.
10 unmodified lines
851 - a web stream ([`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) or [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream))852 - `[nodeStream, 'pipe']` instead of `nodeStream`853854```diff855- await execa(..., { stdout: nodeStream })856+ await execa(..., { stdout: [nodeStream, 'pipe'] })855- await execa(..., {stdout: nodeStream});856+ await execa(..., {stdout: [nodeStream, 'pipe']});857```858859### Retry on error860
86 unmodified lines