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
Provenance
- Commit
f1069b0- Tree
27b9533- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
43c30ac
5 files changed+79 added−13 removed
modified:lib/stdio.js+19 added-8 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';5 6 const aliases = ['stdin', 'stdout', 'stderr'];13 unmodified lines20 const hasFileProtocol = url => url.protocol === 'file:';21 const isFileUrl = stdioOption => isUrlInstance(stdioOption) && hasFileProtocol(stdioOption);22 const isRegularUrl = stdioOption => isUrlInstance(stdioOption) && !hasFileProtocol(stdioOption);23 24+const stringIsFilePath = stdioOption => stdioOption.startsWith('.') || isAbsolute(stdioOption);25+const isFilePath = stdioOption => typeof stdioOption === 'string' && stringIsFilePath(stdioOption);26+const isUnknownStdioString = stdioOption => typeof stdioOption === 'string' && !stringIsFilePath(stdioOption) && !KNOWN_STDIO.has(stdioOption);27+28 // 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.25-const cannotPipeStdio = stdioOption => NO_PIPE_STDIN.has(stdioOption)30+const 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);36 31-const NO_PIPE_STDIN = new Set(['ipc', 'ignore', 'inherit']);37+const NO_PIPE_STDIO = new Set(['ipc', 'ignore', 'inherit']);38+const KNOWN_STDIO = new Set([...NO_PIPE_STDIO, 'overlapped', 'pipe']);39 33-const validateFileUrl = stdioOption => {40+const validateFileSdio = stdioOption => {41 if (isRegularUrl(stdioOption)) {42 throw new TypeError(`The \`stdin: URL\` option must use the \`file:\` scheme.43 For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);44 }45+46+ if (isUnknownStdioString(stdioOption)) {47+ throw new TypeError('The `stdin: filePath` option must either be an absolute file path or start with `.`.');48+ }49 };50 51 const validateInputOptions = (stdioArray, input, inputFile) => {52 if (input !== undefined && inputFile !== undefined) {8 unmodified lines61 if (noPipeStdin && inputFile !== undefined) {62 throw new TypeError('The `inputFile` and `stdin` options cannot be both set.');63 }64 54- validateFileUrl(stdioArray[0]);65+ validateFileSdio(stdioArray[0]);66 };67 68 const getStdioStreams = (stdioArray, {input, inputFile}) => {69 const iterableStdin = getIterableStdin(stdioArray);1 unmodified line71 if (iterableStdin !== undefined) {72 return {stdinStream: Readable.from(iterableStdin)};73 }74 64- if (isFileUrl(stdioArray[0])) {75+ if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {76 return {stdinStream: createReadStream(stdioArray[0])};77 }78 79 if (inputFile !== undefined) {44 unmodified lines124 spawned.stdin.end(stdinInput);125 }126 };127 117-const transformStdioItemSync = stdioItem => isFileUrl(stdioItem) ? 'pipe' : stdioItem;128+const transformStdioItemSync = stdioItem => isFileUrl(stdioItem) || isFilePath(stdioItem) ? 'pipe' : stdioItem;129 130 const transformStdioSync = stdio => Array.isArray(stdio)131 ? stdio.map(stdioItem => transformStdioItemSync(stdioItem))132 : stdio;12 unmodified lines145 const stdioArray = arrifyStdio(stdio);146 validateInputOptions(stdioArray, input, inputFile);147 validateInputOptionsSync(stdioArray, input);148 138- if (isFileUrl(stdioArray[0])) {149+ if (isFileUrl(stdioArray[0]) || isFilePath(stdioArray[0])) {150 return readFileSync(stdioArray[0]);151 }152 153 if (inputFile !== undefined) {65 unmodified linesmodified:test/stream.js+51 added-2 removed
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3 import process from 'node:process';4 import fs from 'node:fs';5+import {relative} from 'node:path';6 import Stream from 'node:stream';7 import {promisify} from 'node:util';8 import {pathToFileURL} from 'node:url';9 import test from 'ava';161 unmodified lines171 execa('stdin.js', {stdin: pathToFileURL('unknown'), inputFile: 'dummy.txt'});172 }, {message: /`inputFile` and `stdin` options/});173 });174 174-test('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'});175+test('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 });180 181+test('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+});186+187 test('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 lines243 execa('stdin.js', {stdin: new URL('https://example.com')});244 }, {message: /pathToFileURL/});245 });246 247+test('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+});253+254+test('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+});261+262+test('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+});267+268 test('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 lines294 test('stdin file URL errors should be handled', async t => {295 await t.throwsAsync(execa('stdin.js', {stdin: pathToFileURL('unknown')}), {code: 'ENOENT'});296 });297 298+test('stdin file path errors should be handled', async t => {299+ await t.throwsAsync(execa('stdin.js', {stdin: './unknown'}), {code: 'ENOENT'});300+});301+302 test('inputFile errors should be handled', async t => {303 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});304 });305 43 unmodified lines349 const {stdout} = execaSync('stdin.js', {stdin});350 t.is(stdout, 'howdy');351 });352 353+test('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+});359+360+test('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+});367+368 test('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 linesmodified:index.d.ts+7 added-2 removed
10 unmodified lines11 | Stream12 | number13 | undefined;14 15-export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array> | URL;15+export type StdinOption =16+ | StdioOption17+ | Iterable<string | Uint8Array>18+ | AsyncIterable<string | Uint8Array>19+ | URL20+ | string;21 22 type EncodingOption =23 | 'utf8'24 // eslint-disable-next-line unicorn/text-encoding-identifier-case67 unmodified lines92 93 /**94 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).95 91- 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 `.`.97 98 @default `inherit` with `$`, `pipe` otherwise99 */100 readonly stdin?: StdinOption;904 unmodified linesmodified:index.test-d.ts+1 added
163 unmodified lines164 execa('unicorns', {stdin: binaryGenerator()});165 expectError(execa('unicorns', {stdin: [0]}));166 expectError(execa('unicorns', {stdin: numberGenerator()}));167 execa('unicorns', {stdin: new URL('file:///test')});168+execa('unicorns', {stdin: './test'});169 execa('unicorns', {stdin: 1});170 execa('unicorns', {stdin: undefined});171 execa('unicorns', {stdout: 'pipe'});172 execa('unicorns', {stdout: 'overlapped'});195 unmodified linesmodified:readme.md+1 added-1 removed
562 unmodified lines563 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 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.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 `.`.568 569 #### stdout570 571 Type: `string | number | Stream | undefined`\242 unmodified lines