Skip to content
Myr

Allow `stdin` to be a file path (#614)

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

authored by ehmicky · committed by GitHub · parent 43c30ac · Browse files · 5 files+79 added−13 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+79 added−13 removed

  • tests:+51 added−2 removed
  • other:+28 added−11 removed

5 files+79 added−13 removed

modified:lib/stdio.js+19 added-8 removed
1import {createReadStream, readFileSync} from 'node:fs';2import {isAbsolute} from 'node:path';3import {Readable} from 'node:stream';4import {isStream} from 'is-stream';56const aliases = ['stdin', 'stdout', 'stderr'];
13 unmodified lines
20const hasFileProtocol = url => url.protocol === 'file:';21const isFileUrl = stdioOption => isUrlInstance(stdioOption) && hasFileProtocol(stdioOption);22const isRegularUrl = stdioOption => isUrlInstance(stdioOption) && !hasFileProtocol(stdioOption);2324const stringIsFilePath = stdioOption => stdioOption.startsWith('.') || isAbsolute(stdioOption);25const isFilePath = stdioOption => typeof stdioOption === 'string' && stringIsFilePath(stdioOption);26const isUnknownStdioString = stdioOption => typeof stdioOption === 'string' && !stringIsFilePath(stdioOption) && !KNOWN_STDIO.has(stdioOption);2728// Check whether the `stdin` option results in `spawned.stdin` being `undefined`.29// We use a deny list instead of an allow list to be forward compatible with new options.25const cannotPipeStdio = stdioOption => NO_PIPE_STDIN.has(stdioOption)30const cannotPipeStdio = stdioOption => NO_PIPE_STDIO.has(stdioOption)31 || isStream(stdioOption)32 || typeof stdioOption === 'number'33 || isIterableStdin(stdioOption)29 || isFileUrl(stdioOption);34 || isFileUrl(stdioOption)35 || isFilePath(stdioOption);3631const NO_PIPE_STDIN = new Set(['ipc', 'ignore', 'inherit']);37const NO_PIPE_STDIO = new Set(['ipc', 'ignore', 'inherit']);38const KNOWN_STDIO = new Set([...NO_PIPE_STDIO, 'overlapped', 'pipe']);3933const validateFileUrl = stdioOption => {40const validateFileSdio = stdioOption => {41 if (isRegularUrl(stdioOption)) {42 throw new TypeError(`The \`stdin: URL\` option must use the \`file:\` scheme.43For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);44 }4546 if (isUnknownStdioString(stdioOption)) {47 throw new TypeError('The `stdin: filePath` option must either be an absolute file path or start with `.`.');48 }49};5051const validateInputOptions = (stdioArray, input, inputFile) => {52 if (input !== undefined && inputFile !== undefined) {
8 unmodified lines
61 if (noPipeStdin && inputFile !== undefined) {62 throw new TypeError('The `inputFile` and `stdin` options cannot be both set.');63 }6454 validateFileUrl(stdioArray[0]);65 validateFileSdio(stdioArray[0]);66};6768const getStdioStreams = (stdioArray, {input, inputFile}) => {69 const iterableStdin = getIterableStdin(stdioArray);
1 unmodified line
71 if (iterableStdin !== undefined) {72 return {stdinStream: Readable.from(iterableStdin)};73 }7464 if (isFileUrl(stdioArray[0])) {75 if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {76 return {stdinStream: createReadStream(stdioArray[0])};77 }7879 if (inputFile !== undefined) {
44 unmodified lines
124 spawned.stdin.end(stdinInput);125 }126};127117const transformStdioItemSync = stdioItem => isFileUrl(stdioItem) ? 'pipe' : stdioItem;128const transformStdioItemSync = stdioItem => isFileUrl(stdioItem) || isFilePath(stdioItem) ? 'pipe' : stdioItem;129130const transformStdioSync = stdio => Array.isArray(stdio)131 ? stdio.map(stdioItem => transformStdioItemSync(stdioItem))132 : stdio;
12 unmodified lines
145 const stdioArray = arrifyStdio(stdio);146 validateInputOptions(stdioArray, input, inputFile);147 validateInputOptionsSync(stdioArray, input);148138 if (isFileUrl(stdioArray[0])) {149 if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {150 return readFileSync(stdioArray[0]);151 }152153 if (inputFile !== undefined) {
65 unmodified lines
modified:test/stream.js+51 added-2 removed
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import process from 'node:process';4import fs from 'node:fs';5import {relative} from 'node:path';6import Stream from 'node:stream';7import {promisify} from 'node:util';8import {pathToFileURL} from 'node:url';9import test from 'ava';
161 unmodified lines
171 execa('stdin.js', {stdin: pathToFileURL('unknown'), inputFile: 'dummy.txt'});172 }, {message: /`inputFile` and `stdin` options/});173});174174test('stdin option cannot be a generic iterable string', async t => {175 await t.throwsAsync(() => execa('stdin.js', {stdin: 'foobar'}), {code: 'ERR_INVALID_SYNC_FORK_INPUT'});175test('stdin option cannot be a file path when "input" is used', t => {176 t.throws(() => {177 execa('stdin.js', {stdin: './unknown', input: 'foobar'});178 }, {message: /`input` and `stdin` options/});179});180181test('stdin option cannot be a file path when "inputFile" is used', t => {182 t.throws(() => {183 execa('stdin.js', {stdin: './unknown', inputFile: 'dummy.txt'});184 }, {message: /`inputFile` and `stdin` options/});185});186187test('stdin option handles errors in iterables', async t => {188 const {originalMessage} = await t.throwsAsync(() => execa('stdin.js', {stdin: throwingGenerator()}));189 t.is(originalMessage, 'generator error');190});
52 unmodified lines
243 execa('stdin.js', {stdin: new URL('https://example.com')});244 }, {message: /pathToFileURL/});245});246247test('stdin can be an absolute file path', async t => {248 const inputFile = tempfile();249 fs.writeFileSync(inputFile, 'howdy');250 const {stdout} = await execa('stdin.js', {stdin: inputFile});251 t.is(stdout, 'howdy');252});253254test('stdin can be a relative file path', async t => {255 const inputFile = tempfile();256 fs.writeFileSync(inputFile, 'howdy');257 const stdin = relative('.', inputFile);258 const {stdout} = await execa('stdin.js', {stdin});259 t.is(stdout, 'howdy');260});261262test('stdin option must start with . when being a relative file path', t => {263 t.throws(() => {264 execa('stdin.js', {stdin: 'foobar'});265 }, {message: /absolute file path/});266});267268test('inputFile can be set', async t => {269 const inputFile = tempfile();270 fs.writeFileSync(inputFile, 'howdy');271 const {stdout} = await execa('stdin.js', {inputFile});
22 unmodified lines
294test('stdin file URL errors should be handled', async t => {295 await t.throwsAsync(execa('stdin.js', {stdin: pathToFileURL('unknown')}), {code: 'ENOENT'});296});297298test('stdin file path errors should be handled', async t => {299 await t.throwsAsync(execa('stdin.js', {stdin: './unknown'}), {code: 'ENOENT'});300});301302test('inputFile errors should be handled', async t => {303 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});304});305
43 unmodified lines
349 const {stdout} = execaSync('stdin.js', {stdin});350 t.is(stdout, 'howdy');351});352353test('stdin can be an absolute file path - sync', t => {354 const inputFile = tempfile();355 fs.writeFileSync(inputFile, 'howdy');356 const {stdout} = execaSync('stdin.js', {stdin: inputFile});357 t.is(stdout, 'howdy');358});359360test('stdin can be a relative file path - sync', t => {361 const inputFile = tempfile();362 fs.writeFileSync(inputFile, 'howdy');363 const stdin = relative('.', inputFile);364 const {stdout} = execaSync('stdin.js', {stdin});365 t.is(stdout, 'howdy');366});367368test('stdin cannot be a non-file URL - sync', async t => {369 await t.throws(() => {370 execaSync('stdin.js', {stdin: new URL('https://example.com')});371 }, {message: /pathToFileURL/});
127 unmodified lines
modified:index.d.ts+7 added-2 removed
10 unmodified lines
11 | Stream12 | number13 | undefined;1415export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array> | URL;15export type StdinOption =16 | StdioOption17 | Iterable<string | Uint8Array>18 | AsyncIterable<string | Uint8Array>19 | URL20 | string;2122type EncodingOption =23 | 'utf8'24 // eslint-disable-next-line unicorn/text-encoding-identifier-case
67 unmodified lines
9293 /**94 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).9591 It can also be 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.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 `.`.9798 @default `inherit` with `$`, `pipe` otherwise99 */100 readonly stdin?: StdinOption;
904 unmodified lines
modified:index.test-d.ts+1 added
163 unmodified lines
164execa('unicorns', {stdin: binaryGenerator()});165expectError(execa('unicorns', {stdin: [0]}));166expectError(execa('unicorns', {stdin: numberGenerator()}));167execa('unicorns', {stdin: new URL('file:///test')});168execa('unicorns', {stdin: './test'});169execa('unicorns', {stdin: 1});170execa('unicorns', {stdin: undefined});171execa('unicorns', {stdout: 'pipe'});172execa('unicorns', {stdout: 'overlapped'});
195 unmodified lines
modified:readme.md+1 added-1 removed
562 unmodified lines
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 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.567It 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 `.`.568569#### stdout570571Type: `string | number | Stream | undefined`\
242 unmodified lines