Skip to content
Myr

Add support for `ReadableStream` in the `stdin` option (#615)

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

authored by ehmicky · committed by GitHub · parent bea94d6 · Browse files · 5 files+64 added−26 removed
This commit

State

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

Diff stats

5 files+64 added−26 removed

  • tests:+33 added−9 removed
  • other:+31 added−17 removed

5 files+64 added−26 removed

modified:lib/stdio.js+17 added-5 removed
1import {createReadStream, readFileSync} from 'node:fs';2import {isAbsolute} from 'node:path';3import {Readable} from 'node:stream';4import {isStream} from 'is-stream';4import {isStream as isNodeStream} from 'is-stream';56const aliases = ['stdin', 'stdout', 'stderr'];78const arrifyStdio = (stdio = []) => Array.isArray(stdio) ? stdio : [stdio, stdio, stdio];910const isIterableStdin = stdinOption => typeof stdinOption === 'object'11 && stdinOption !== null12 && !isStream(stdinOption)12 && !isNodeStream(stdinOption)13 && !isReadableStream(stdinOption)14 && (typeof stdinOption[Symbol.asyncIterator] === 'function' || typeof stdinOption[Symbol.iterator] === 'function');1516const getIterableStdin = stdioArray => isIterableStdin(stdioArray[0])17 ? stdioArray[0]
7 unmodified lines
25const stringIsFilePath = stdioOption => stdioOption.startsWith('.') || isAbsolute(stdioOption);26const isFilePath = stdioOption => typeof stdioOption === 'string' && stringIsFilePath(stdioOption);27const isUnknownStdioString = stdioOption => typeof stdioOption === 'string' && !stringIsFilePath(stdioOption) && !KNOWN_STDIO.has(stdioOption);2829const isReadableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object ReadableStream]';3031// Check whether the `stdin` option results in `spawned.stdin` being `undefined`.32// We use a deny list instead of an allow list to be forward compatible with new options.33const cannotPipeStdio = stdioOption => NO_PIPE_STDIO.has(stdioOption)31 || isStream(stdioOption)34 || isNodeStream(stdioOption)35 || isReadableStream(stdioOption)36 || typeof stdioOption === 'number'37 || isIterableStdin(stdioOption)38 || isFileUrl(stdioOption)39 || isFilePath(stdioOption);
35 unmodified lines
75 if (iterableStdin !== undefined) {76 return {stdinStream: Readable.from(iterableStdin)};77 }7879 if (isReadableStream(stdioArray[0])) {80 return {stdinStream: Readable.fromWeb(stdioArray[0])};81 }8283 if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {84 return {stdinStream: createReadStream(stdioArray[0])};85 }86
4 unmodified lines
91 if (input === undefined) {92 return {};93 }9487 if (isStream(input)) {95 if (isNodeStream(input)) {96 return {stdinStream: input};97 }9899 return {stdinInput: input};
43 unmodified lines
143 if (getIterableStdin(stdioArray) !== undefined) {144 throw new TypeError('The `stdin` option cannot be an iterable in sync mode');145 }146147 if (isReadableStream(stdioArray[0])) {148 throw new TypeError('The `stdin` option cannot be a stream in sync mode');149 }150139 if (isStream(input)) {151 if (isNodeStream(input)) {152 throw new TypeError('The `input` option cannot be a stream in sync mode');153 }154};155
75 unmodified lines
modified:test/stream.js+33 added-9 removed
210 unmodified lines
211 const {stdout} = await execa('stdin.js', {input: 'testing12'});212 t.is(stdout, 'testing12');213});214215test('input can be a Stream', async t => {215test('input can be a Node.js Readable', async t => {216 const stream = new Stream.PassThrough();217 stream.write('howdy');218 stream.end();219 const {stdout} = await execa('stdin.js', {input: stream});220 t.is(stdout, 'howdy');221});222223test('input option cannot be a Stream when stdin is set', t => {223test('input option cannot be a Node.js Readable when stdin is set', t => {224 t.throws(() => {225 execa('stdin.js', {input: new Stream.PassThrough(), stdin: 'ignore'});226 }, {message: /`input` and `stdin` options/});227});
2 unmodified lines
230 const {stdout} = await $({input: 'foobar'})`stdin.js`;231 t.is(stdout, 'foobar');232});233234test('stdin can be a ReadableStream', async t => {235 const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));236 const {stdout} = await execa('stdin.js', {stdin});237 t.is(stdout, 'howdy');238});239240test('stdin cannot be a ReadableStream when input is used', t => {241 const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));242 t.throws(() => {243 execa('stdin.js', {stdin, input: 'foobar'});244 }, {message: /`input` and `stdin` options/});245});246247test('stdin cannot be a ReadableStream when inputFile is used', t => {248 const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));249 t.throws(() => {250 execa('stdin.js', {stdin, inputFile: 'dummy.txt'});251 }, {message: /`inputFile` and `stdin` options/});252});253254test('stdin can be a file URL', async t => {255 const inputFile = tempfile();256 fs.writeFileSync(inputFile, 'howdy');257 const stdin = pathToFileURL(inputFile);
95 unmodified lines
353 });354 t.is(stdout, undefined);355});356337test('helpful error trying to provide an input stream in sync mode', t => {338 t.throws(339 () => {340 execaSync('stdin.js', {input: new Stream.PassThrough()});341 },342 {message: /The `input` option cannot be a stream in sync mode/},343 );357test('input cannot be a stream in sync mode', t => {358 t.throws(() => {359 execaSync('stdin.js', {input: new Stream.PassThrough()});360 }, {message: /The `input` option cannot be a stream in sync mode/});361});362363test('stdin cannot be a ReadableStream in sync mode', t => {364 const stdin = Stream.Readable.toWeb(Stream.Readable.from('howdy'));365 t.throws(() => {366 execaSync('stdin.js', {stdin});367 }, {message: /The `stdin` option cannot be a stream in sync mode/});368});369370test('stdin can be a file URL - sync', t => {371 const inputFile = tempfile();372 fs.writeFileSync(inputFile, 'howdy');373 const stdin = pathToFileURL(inputFile);
181 unmodified lines
modified:index.d.ts+9 added-8 removed
1import {type Buffer} from 'node:buffer';2import {type ChildProcess} from 'node:child_process';3import {type Stream, type Readable as ReadableStream, type Writable as WritableStream} from 'node:stream';3import {type Stream, type Readable, type Writable} from 'node:stream';45export type StdioOption =6 | 'pipe'7 | 'overlapped'
8 unmodified lines
16 | StdioOption17 | Iterable<string | Uint8Array>18 | AsyncIterable<string | Uint8Array>19 | URL20 | string;20 | string21 | ReadableStream;2223type EncodingOption =24 | 'utf8'25 // eslint-disable-next-line unicorn/text-encoding-identifier-case
67 unmodified lines
9394 /**95 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).9696 It can also be a file path, a file URL, an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used. If the file path is relative, it must start with `.`.97 It can also be a file path, a file URL, a web stream ([`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)) an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used. If the file path is relative, it must start with `.`.9899 @default `inherit` with `$`, `pipe` otherwise100 */101 readonly stdin?: StdinOption;
189 unmodified lines
291 Write some input to the `stdin` of your binary.292293 If the input is a file, use the `inputFile` option instead.294 */294 readonly input?: string | Uint8Array | ReadableStream;295 readonly input?: string | Uint8Array | Readable;296297 /**298 Use a file as input to the the `stdin` of your binary.299
188 unmodified lines
488 This is `undefined` if either:489 - the `all` option is `false` (the default value)490 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)491 */491 all?: ReadableStream;492 all?: Readable;493494 catch<ResultType = never>(495 onRejected?: (reason: ExecaError<StdoutStderrType>) => ResultType | PromiseLike<ResultType>496 ): Promise<ExecaReturnValue<StdoutStderrType> | ResultType>;
18 unmodified lines
515516 The `stdout` option] must be kept as `pipe`, its default value.517 */518 pipeStdout?<Target extends ExecaChildPromise<StdoutStderrAll>>(target: Target): Target;518 pipeStdout?(target: WritableStream | string): ExecaChildProcess<StdoutStderrType>;519 pipeStdout?(target: Writable | string): ExecaChildProcess<StdoutStderrType>;520521 /**522 Like `pipeStdout()` but piping the child process's `stderr` instead.523524 The `stderr` option must be kept as `pipe`, its default value.525 */526 pipeStderr?<Target extends ExecaChildPromise<StdoutStderrAll>>(target: Target): Target;526 pipeStderr?(target: WritableStream | string): ExecaChildProcess<StdoutStderrType>;527 pipeStderr?(target: Writable | string): ExecaChildProcess<StdoutStderrType>;528529 /**530 Combines both `pipeStdout()` and `pipeStderr()`.531532 Either the `stdout` option or the `stderr` option must be kept as `pipe`, their default value. Also, the `all` option must be set to `true`.533 */534 pipeAll?<Target extends ExecaChildPromise<StdoutStderrAll>>(target: Target): Target;534 pipeAll?(target: WritableStream | string): ExecaChildProcess<StdoutStderrType>;535 pipeAll?(target: Writable | string): ExecaChildProcess<StdoutStderrType>;536};537538export type ExecaChildProcess<StdoutStderrType extends StdoutStderrAll = string> = ChildProcess &539ExecaChildPromise<StdoutStderrType> &
466 unmodified lines
modified:index.test-d.ts+3 added-2 removed
1 unmodified line
2// For some reason a default import of `process` causes3// `process.stdin`, `process.stderr`, and `process.stdout`4// to get treated as `any` by `@typescript-eslint/no-unsafe-assignment`.5import * as process from 'node:process';6import {type Readable as ReadableStream} from 'node:stream';6import {type Readable} from 'node:stream';7import {createWriteStream} from 'node:fs';8import {expectType, expectError, expectAssignable} from 'tsd';9import {10 $,
11 unmodified lines
2223try {24 const execaPromise = execa('unicorns');25 execaPromise.cancel();26 expectType<ReadableStream | undefined>(execaPromise.all);26 expectType<Readable | undefined>(execaPromise.all);2728 const execaBufferPromise = execa('unicorns', {encoding: 'buffer'});29 const writeStream = createWriteStream('output.txt');30
126 unmodified lines
157execa('unicorns', {stdin: 'ipc'});158execa('unicorns', {stdin: 'ignore'});159execa('unicorns', {stdin: 'inherit'});160execa('unicorns', {stdin: process.stdin});161execa('unicorns', {stdin: new ReadableStream()});162execa('unicorns', {stdin: ['']});163execa('unicorns', {stdin: [new Uint8Array(0)]});164execa('unicorns', {stdin: stringGenerator()});165execa('unicorns', {stdin: binaryGenerator()});
203 unmodified lines
modified:readme.md+2 added-2 removed
557 unmodified lines
558If the input is not a file, use the [`input` option](#input) instead.559560#### stdin561562Type: `string | number | Stream | undefined | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\562Type: `string | number | stream.Readable | ReadableStream | undefined | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\563Default: `inherit` with [`$`](#command), `pipe` otherwise564565Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).566567It can also be a file path, a file URL, an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used. If the file path is relative, it must start with `.`.567It can also be a file path, a file URL, a web stream ([`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)) an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), providing neither [`execaSync()`](#execasyncfile-arguments-options), the [`input` option](#input) nor the [`inputFile` option](#inputfile) is used. If the file path is relative, it must start with `.`.568569#### stdout570571Type: `string | number | Stream | undefined`\
242 unmodified lines