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
Provenance
- Commit
fcf648e- Tree
152ecec- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
bea94d6
5 files changed+64 added−26 removed
modified:lib/stdio.js+17 added-5 removed
1 import {createReadStream, readFileSync} from 'node:fs';2 import {isAbsolute} from 'node:path';3 import {Readable} from 'node:stream';4-import {isStream} from 'is-stream';4+import {isStream as isNodeStream} from 'is-stream';5 6 const aliases = ['stdin', 'stdout', 'stderr'];7 8 const arrifyStdio = (stdio = []) => Array.isArray(stdio) ? stdio : [stdio, stdio, stdio];9 10 const 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');15 16 const getIterableStdin = stdioArray => isIterableStdin(stdioArray[0])17 ? stdioArray[0]7 unmodified lines25 const stringIsFilePath = stdioOption => stdioOption.startsWith('.') || isAbsolute(stdioOption);26 const isFilePath = stdioOption => typeof stdioOption === 'string' && stringIsFilePath(stdioOption);27 const isUnknownStdioString = stdioOption => typeof stdioOption === 'string' && !stringIsFilePath(stdioOption) && !KNOWN_STDIO.has(stdioOption);28 29+const isReadableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object ReadableStream]';30+31 // 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.33 const 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 lines75 if (iterableStdin !== undefined) {76 return {stdinStream: Readable.from(iterableStdin)};77 }78 79+ if (isReadableStream(stdioArray[0])) {80+ return {stdinStream: Readable.fromWeb(stdioArray[0])};81+ }82+83 if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {84 return {stdinStream: createReadStream(stdioArray[0])};85 }86 4 unmodified lines91 if (input === undefined) {92 return {};93 }94 87- if (isStream(input)) {95+ if (isNodeStream(input)) {96 return {stdinStream: input};97 }98 99 return {stdinInput: input};43 unmodified lines143 if (getIterableStdin(stdioArray) !== undefined) {144 throw new TypeError('The `stdin` option cannot be an iterable in sync mode');145 }146 147+ if (isReadableStream(stdioArray[0])) {148+ throw new TypeError('The `stdin` option cannot be a stream in sync mode');149+ }150+139- 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 linesmodified:test/stream.js+33 added-9 removed
210 unmodified lines211 const {stdout} = await execa('stdin.js', {input: 'testing12'});212 t.is(stdout, 'testing12');213 });214 215-test('input can be a Stream', async t => {215+test('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 });222 223-test('input option cannot be a Stream when stdin is set', t => {223+test('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 lines230 const {stdout} = await $({input: 'foobar'})`stdin.js`;231 t.is(stdout, 'foobar');232 });233 234+test('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+});239+240+test('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+});246+247+test('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+});253+254 test('stdin can be a file URL', async t => {255 const inputFile = tempfile();256 fs.writeFileSync(inputFile, 'howdy');257 const stdin = pathToFileURL(inputFile);95 unmodified lines353 });354 t.is(stdout, undefined);355 });356 337-test('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- );357+test('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 });362 363+test('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+});369+370 test('stdin can be a file URL - sync', t => {371 const inputFile = tempfile();372 fs.writeFileSync(inputFile, 'howdy');373 const stdin = pathToFileURL(inputFile);181 unmodified linesmodified:index.d.ts+9 added-8 removed
1 import {type Buffer} from 'node:buffer';2 import {type ChildProcess} from 'node:child_process';3-import {type Stream, type Readable as ReadableStream, type Writable as WritableStream} from 'node:stream';3+import {type Stream, type Readable, type Writable} from 'node:stream';4 5 export type StdioOption =6 | 'pipe'7 | 'overlapped'8 unmodified lines16 | StdioOption17 | Iterable<string | Uint8Array>18 | AsyncIterable<string | Uint8Array>19 | URL20- | string;20+ | string21+ | ReadableStream;22 23 type EncodingOption =24 | 'utf8'25 // eslint-disable-next-line unicorn/text-encoding-identifier-case67 unmodified lines93 94 /**95 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).96 96- 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 `.`.98 99 @default `inherit` with `$`, `pipe` otherwise100 */101 readonly stdin?: StdinOption;189 unmodified lines291 Write some input to the `stdin` of your binary.292 293 If the input is a file, use the `inputFile` option instead.294 */294- readonly input?: string | Uint8Array | ReadableStream;295+ readonly input?: string | Uint8Array | Readable;296 297 /**298 Use a file as input to the the `stdin` of your binary.299 188 unmodified lines488 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;493 494 catch<ResultType = never>(495 onRejected?: (reason: ExecaError<StdoutStderrType>) => ResultType | PromiseLike<ResultType>496 ): Promise<ExecaReturnValue<StdoutStderrType> | ResultType>;18 unmodified lines515 516 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>;520 521 /**522 Like `pipeStdout()` but piping the child process's `stderr` instead.523 524 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>;528 529 /**530 Combines both `pipeStdout()` and `pipeStderr()`.531 532 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 };537 538 export type ExecaChildProcess<StdoutStderrType extends StdoutStderrAll = string> = ChildProcess &539 ExecaChildPromise<StdoutStderrType> &466 unmodified linesmodified:index.test-d.ts+3 added-2 removed
1 unmodified line2 // 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`.5 import * as process from 'node:process';6-import {type Readable as ReadableStream} from 'node:stream';6+import {type Readable} from 'node:stream';7 import {createWriteStream} from 'node:fs';8 import {expectType, expectError, expectAssignable} from 'tsd';9 import {10 $,11 unmodified lines22 23 try {24 const execaPromise = execa('unicorns');25 execaPromise.cancel();26- expectType<ReadableStream | undefined>(execaPromise.all);26+ expectType<Readable | undefined>(execaPromise.all);27 28 const execaBufferPromise = execa('unicorns', {encoding: 'buffer'});29 const writeStream = createWriteStream('output.txt');30 126 unmodified lines157 execa('unicorns', {stdin: 'ipc'});158 execa('unicorns', {stdin: 'ignore'});159 execa('unicorns', {stdin: 'inherit'});160 execa('unicorns', {stdin: process.stdin});161+execa('unicorns', {stdin: new ReadableStream()});162 execa('unicorns', {stdin: ['']});163 execa('unicorns', {stdin: [new Uint8Array(0)]});164 execa('unicorns', {stdin: stringGenerator()});165 execa('unicorns', {stdin: binaryGenerator()});203 unmodified linesmodified:readme.md+2 added-2 removed
557 unmodified lines558 If the input is not a file, use the [`input` option](#input) instead.559 560 #### stdin561 562-Type: `string | number | Stream | undefined | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\562+Type: `string | number | stream.Readable | ReadableStream | undefined | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\563 Default: `inherit` with [`$`](#command), `pipe` otherwise564 565 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).566 567-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 `.`.567+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 `.`.568 569 #### stdout570 571 Type: `string | number | Stream | undefined`\242 unmodified lines