Allow `stdin` to be a file URL (#610)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 85a9821 · Browse files · 5 files·+81 added−6 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+81 added−6 removed
- tests:+45 added−0 removed
- other:+36 added−6 removed
Provenance
- Commit
d37a0f6- Tree
69c7acb- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
85a9821
5 files changed+81 added−6 removed
modified:lib/stdio.js+31 added-2 removed
13 unmodified lines141516171819202122232425262723282930313233343536373839404142435 unmodified lines495051525354555657585960616263646566676869707141 unmodified lines1131141151161171181191201211221231241251267 unmodified lines1341351361371381391401411421431441458 unmodified lines15415515615712915815916016116245 unmodified lines13 unmodified linesconst getIterableStdin = stdioArray => isIterableStdin(stdioArray[0]) ? stdioArray[0] : undefined;
const isUrlInstance = stdioOption => Object.prototype.toString.call(stdioOption) === '[object URL]';const hasFileProtocol = url => url.protocol === 'file:';const isFileUrl = stdioOption => isUrlInstance(stdioOption) && hasFileProtocol(stdioOption);const isRegularUrl = stdioOption => isUrlInstance(stdioOption) && !hasFileProtocol(stdioOption);
// Check whether the `stdin` option results in `spawned.stdin` being `undefined`.// We use a deny list instead of an allow list to be forward compatible with new options.const cannotPipeStdio = stdioOption => NO_PIPE_STDIN.has(stdioOption) || isStream(stdioOption) || typeof stdioOption === 'number' || isIterableStdin(stdioOption); || isIterableStdin(stdioOption) || isFileUrl(stdioOption);
const NO_PIPE_STDIN = new Set(['ipc', 'ignore', 'inherit']);
const validateFileUrl = stdioOption => { if (isRegularUrl(stdioOption)) { throw new TypeError(`The \`stdin: URL\` option must use the \`file:\` scheme.For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`); }};
const validateInputOptions = (stdioArray, input, inputFile) => { if (input !== undefined && inputFile !== undefined) { throw new TypeError('The `input` and `inputFile` options cannot be both set.'); }5 unmodified lines
if (noPipeStdin && inputFile !== undefined) { throw new TypeError('The `inputFile` and `stdin` options cannot be both set.'); }
validateFileUrl(stdioArray[0]);};
const getStdioStreams = (stdioArray, {input, inputFile}) => { const iterableStdin = getIterableStdin(stdioArray);
if (iterableStdin !== undefined) { return {stdinStream: Readable.from(iterableStdin)}; }
if (isFileUrl(stdioArray[0])) { return {stdinStream: createReadStream(stdioArray[0])}; }
if (inputFile !== undefined) { return {stdinStream: createReadStream(inputFile)}; }
41 unmodified lines spawned.stdin.end(stdinInput); }};
const transformStdioItemSync = stdioItem => isFileUrl(stdioItem) ? 'pipe' : stdioItem;
const transformStdioSync = stdio => Array.isArray(stdio) ? stdio.map(stdioItem => transformStdioItemSync(stdioItem)) : stdio;
const validateInputOptionsSync = (stdioArray, input) => { if (getIterableStdin(stdioArray) !== undefined) { throw new TypeError('The `stdin` option cannot be an iterable in sync mode'); }7 unmodified lines const stdioArray = arrifyStdio(stdio); validateInputOptions(stdioArray, input, inputFile); validateInputOptionsSync(stdioArray, input);
if (isFileUrl(stdioArray[0])) { return readFileSync(stdioArray[0]); }
if (inputFile !== undefined) { return readFileSync(inputFile); }
8 unmodified lines if (input !== undefined) { options.input = input; }
options.stdio = stdio; options.stdio = transformStdioSync(stdio);};
const hasAlias = options => aliases.some(alias => options[alias] !== undefined);
45 unmodified linesmodified:test/stream.js+45 added
2 unmodified lines3 import process from 'node:process';4 import fs from 'node:fs';5 import Stream from 'node:stream';6 import {promisify} from 'node:util';7+import {pathToFileURL} from 'node:url';8 import test from 'ava';9 import getStream from 'get-stream';10 import {pEvent} from 'p-event';11 import tempfile from 'tempfile';146 unmodified lines158 execa('stdin.js', {stdin: ['foo', 'bar'], inputFile: 'dummy.txt'});159 }, {message: /`inputFile` and `stdin` options/});160 });161 162+test('stdin option cannot be a file URL when "input" is used', t => {163+ t.throws(() => {164+ execa('stdin.js', {stdin: pathToFileURL('unknown'), input: 'foobar'});165+ }, {message: /`input` and `stdin` options/});166+});167+168+test('stdin option cannot be a file URL when "inputFile" is used', t => {169+ t.throws(() => {170+ execa('stdin.js', {stdin: pathToFileURL('unknown'), inputFile: 'dummy.txt'});171+ }, {message: /`inputFile` and `stdin` options/});172+});173+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'});176 });177 42 unmodified lines220 const {stdout} = await $({input: 'foobar'})`stdin.js`;221 t.is(stdout, 'foobar');222 });223 224+test('stdin can be a file URL', async t => {225+ const inputFile = tempfile();226+ fs.writeFileSync(inputFile, 'howdy');227+ const stdin = pathToFileURL(inputFile);228+ const {stdout} = await execa('stdin.js', {stdin});229+ t.is(stdout, 'howdy');230+});231+232+test('stdin cannot be a non-file URL', async t => {233+ await t.throws(() => {234+ execa('stdin.js', {stdin: new URL('https://example.com')});235+ }, {message: /pathToFileURL/});236+});237+238 test('inputFile can be set', async t => {239 const inputFile = tempfile();240 fs.writeFileSync(inputFile, 'howdy');241 const {stdout} = await execa('stdin.js', {inputFile});18 unmodified lines260 execa('stdin.js', {inputFile: '', stdin: 'ignore'});261 }, {message: /`inputFile` and `stdin` options/});262 });263 264+test('stdin file URL errors should be handled', async t => {265+ await t.throwsAsync(execa('stdin.js', {stdin: pathToFileURL('unknown')}), {code: 'ENOENT'});266+});267+268 test('inputFile errors should be handled', async t => {269 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});270 });271 35 unmodified lines307 {message: /The `input` option cannot be a stream in sync mode/},308 );309 });310 311+test('stdin can be a file URL - sync', t => {312+ const inputFile = tempfile();313+ fs.writeFileSync(inputFile, 'howdy');314+ const stdin = pathToFileURL(inputFile);315+ const {stdout} = execaSync('stdin.js', {stdin});316+ t.is(stdout, 'howdy');317+});318+319+test('stdin cannot be a non-file URL - sync', async t => {320+ await t.throws(() => {321+ execaSync('stdin.js', {stdin: new URL('https://example.com')});322+ }, {message: /pathToFileURL/});323+});324+325 test('inputFile can be set - sync', t => {326 const inputFile = tempfile();327 fs.writeFileSync(inputFile, 'howdy');328 const {stdout} = execaSync('stdin.js', {inputFile});121 unmodified linesmodified:index.d.ts+2 added-2 removed
10 unmodified lines11 | Stream12 | number13 | undefined;14 15-export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>;15+export type StdinOption = StdioOption | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array> | URL;16 17 type EncodingOption =18 | 'utf8'19 // eslint-disable-next-line unicorn/text-encoding-identifier-case67 unmodified lines87 88 /**89 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).90 91- It can also be 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.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.92 93 @default `inherit` with `$`, `pipe` otherwise94 */95 readonly stdin?: StdinOption;904 unmodified linesmodified:index.test-d.ts+1 added
162 unmodified lines163 execa('unicorns', {stdin: stringGenerator()});164 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: 1});169 execa('unicorns', {stdin: undefined});170 execa('unicorns', {stdout: 'pipe'});171 execa('unicorns', {stdout: 'overlapped'});195 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 | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\562+Type: `string | number | Stream | 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 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 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.568 569 #### stdout570 571 Type: `string | number | Stream | undefined`\242 unmodified lines