Allow `stdin: uint8Array` option (#670)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent b11cbcd · Browse files · 10 files·+57 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+57 added−8 removed
- tests:+29 added−0 removed
- other:+28 added−8 removed
Provenance
- Commit
2ac81d4- Tree
928e057- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b11cbcd
10 files changed+57 added−8 removed
modified:lib/stdio/async.js+7 added-3 removed
1234567891011121314151617183 unmodified lines2223242521222326272829303125 unmodified linesimport {createReadStream, createWriteStream} from 'node:fs';import {Buffer} from 'node:buffer';import {Readable, Writable} from 'node:stream';import mergeStreams from '@sindresorhus/merge-streams';import {handleInput} from './handle.js';import {TYPE_TO_MESSAGE} from './type.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleInputAsync = options => handleInput(addPropertiesAsync, options);
const forbiddenIfAsync = ({type, optionName}) => { throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);};
const addPropertiesAsync = { input: { filePath: ({value}) => ({value: createReadStream(value)}), webStream: ({value}) => ({value: Readable.fromWeb(value)}),3 unmodified lines }, output: { filePath: ({value}) => ({value: createWriteStream(value)}), webStream: ({value}) => ({value: Writable.fromWeb(value)}), iterable({optionName}) { throw new TypeError(`The \`${optionName}\` option cannot be an iterable.`); }, iterable: forbiddenIfAsync, uint8Array: forbiddenIfAsync, },};
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode25 unmodified linesmodified:lib/stdio/direction.js+2 added-1 removed
24 unmodified lines252627282929303132333435363726 unmodified lines24 unmodified lines
// `stdin`/`stdout`/`stderr` have a known directionconst KNOWN_DIRECTIONS = ['input', 'output', 'output'];
// `string` and `uint8Array` types always applies to `stdin`, i.e. does not need to be handled here// `string` can only be added through the `input` option, i.e. does not need to be handled hereconst guessStreamDirection = { filePath: () => undefined, iterable: () => 'input', uint8Array: () => 'input', webStream: stdioOption => isWritableStream(stdioOption) ? 'output' : 'input', nodeStream(stdioOption) { if (isNodeReadableStream(stdioOption)) { return isNodeWritableStream(stdioOption) ? undefined : 'input';26 unmodified linesmodified:lib/stdio/input.js+2 added-2 removed
112345615 unmodified lines222324252626272829308 unmodified linesimport {Buffer} from 'node:buffer';import {isReadableStream} from 'is-stream';import {isUint8Array} from './utils.js';
// Append the `stdin` option with the `input` and `inputFile` optionsexport const handleInputOptions = ({input, inputFile}) => [ handleInputOption(input),15 unmodified lines if (typeof input === 'string') { return 'string'; }
if (Object.prototype.toString.call(input) === '[object Uint8Array]' && !Buffer.isBuffer(input)) { if (isUint8Array(input)) { return 'uint8Array'; }
throw new Error('The `input` option must be a string, a Uint8Array or a Node.js Readable stream.');8 unmodified linesmodified:lib/stdio/sync.js+1 added
33 unmodified lines34 output: {35 webStream: forbiddenIfSync,36 nodeStream: forbiddenIfSync,37 iterable: forbiddenIfSync,38+ uint8Array: forbiddenIfSync,39 native: forbiddenIfStreamSync,40 },41 };42 33 unmodified linesmodified:lib/stdio/type.js+5 added
1 import {isAbsolute} from 'node:path';2 import {isStream as isNodeStream} from 'is-stream';3+import {isUint8Array} from './utils.js';4 5 // The `stdin`/`stdout`/`stderr` option can be of many types. This detects it.6 export const getStdioOptionType = stdioOption => {7 if (isFileUrl(stdioOption) || isFilePath(stdioOption)) {7 unmodified lines15 if (isNodeStream(stdioOption)) {16 return 'native';17 }18 19+ if (isUint8Array(stdioOption)) {20+ return 'uint8Array';21+ }22+23 if (isIterableObject(stdioOption)) {24 return 'iterable';25 }26 33 unmodified linesmodified:lib/stdio/utils.js+4 added
12345import {Buffer} from 'node:buffer';
export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);added:test/stdio/typed-array.js+29 added
1234567891011121314151617181920212223242526272829import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';
setFixtureDir();
const uint8ArrayFoobar = new TextEncoder().encode('foobar');
const testUint8Array = async (t, fixtureName, getOptions) => { const {stdout} = await execa(fixtureName, getOptions(uint8ArrayFoobar)); t.is(stdout, 'foobar');};
test('stdin option can be a Uint8Array', testUint8Array, 'stdin.js', getStdinOption);test('stdio[*] option can be a Uint8Array', testUint8Array, 'stdin-fd3.js', getStdioOption);test('stdin option can be a Uint8Array - sync', testUint8Array, 'stdin.js', getStdinOption);test('stdio[*] option can be a Uint8Array - sync', testUint8Array, 'stdin-fd3.js', getStdioOption);
const testNoUint8ArrayOutput = (t, getOptions, execaMethod) => { t.throws(() => { execaMethod('noop.js', getOptions(uint8ArrayFoobar)); }, {message: /cannot be a Uint8Array/});};
test('stdout option cannot be a Uint8Array', testNoUint8ArrayOutput, getStdoutOption, execa);test('stderr option cannot be a Uint8Array', testNoUint8ArrayOutput, getStderrOption, execa);test('stdout option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, getStdoutOption, execaSync);test('stderr option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, getStderrOption, execaSync);modified:index.d.ts+2 added
16 unmodified lines17 18 type InputStdioOption =19 | Iterable<string | Uint8Array>20 | AsyncIterable<string | Uint8Array>21+ | Uint8Array22 | Readable23 | ReadableStream;24 25 type OutputStdioOption =96 unmodified lines122 - a file path. If relative, it must start with `.`.123 - a file URL.124 - a web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).125 - 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)126+ - an `Uint8Array`.127 128 This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.129 130 @default `inherit` with `$`, `pipe` otherwise953 unmodified linesmodified:index.test-d.ts+2 added
177 unmodified lines178 execa('unicorns', {stdin: new ReadableStream()});179 execa('unicorns', {stdin: [new ReadableStream()]});180 expectError(execa('unicorns', {stdin: new WritableStream()}));181 expectError(execa('unicorns', {stdin: [new WritableStream()]}));182+execa('unicorns', {stdin: new Uint8Array()});183 execa('unicorns', {stdin: stringGenerator()});184 execa('unicorns', {stdin: [stringGenerator()]});185 execa('unicorns', {stdin: binaryGenerator()});186 execa('unicorns', {stdin: [binaryGenerator()]});119 unmodified lines306 new Writable(),307 new Readable(),308 new WritableStream(),309 new ReadableStream(),310+ new Uint8Array(),311 stringGenerator(),312 asyncStringGenerator(),313 ],314 });171 unmodified linesmodified:readme.md+3 added-2 removed
570 unmodified lines571 See also the [`input`](#input) and [`stdin`](#stdin) options.572 573 #### stdin574 575-Type: `string | number | stream.Readable | ReadableStream | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>` (or a tuple of those types)\575+Type: `string | number | stream.Readable | ReadableStream | URL | Uint8Array | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>` (or a tuple of those types)\576 Default: `inherit` with [`$`](#command), `pipe` otherwise577 578 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard input. This can be:579 - `'pipe'`: Sets [`childProcess.stdin`](https://nodejs.org/api/child_process.html#subprocessstdin) stream.6 unmodified lines586 - a file path. If relative, it must start with `.`.587 - a file URL.588 - a web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).589 - 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)590+- an `Uint8Array`.591 592 This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.593 594 #### stdout35 unmodified lines630 This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.631 632 #### stdio633 633-Type: `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>>` (or a tuple of those types)\634+Type: `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | URL | Uint8Array | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>>` (or a tuple of those types)\635 Default: `pipe`636 637 Like the [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.638 289 unmodified lines