Allow passing `Buffer` to the `input` option (#1029)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 5182195 · Browse files · 7 files·+38 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+38 added−47 removed
- tests:+34 added−40 removed
- other:+4 added−7 removed
Provenance
- Commit
40fdc75- Tree
8484ccc- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5182195
7 files changed+38 added−47 removed
modified:lib/transform/encoding-transform.js+1 added-3 removed
38 unmodified lines3940414243444543444546474 unmodified lines38 unmodified lines }};
const encodingStringGenerator = function * (stringDecoder, chunk) { yield Buffer.isBuffer(chunk) || isUint8Array(chunk) ? stringDecoder.write(chunk) : chunk; yield isUint8Array(chunk) ? stringDecoder.write(chunk) : chunk;};
const encodingStringFinal = function * (stringDecoder) { const lastChunk = stringDecoder.end();4 unmodified linesmodified:lib/transform/validate.js+1 added-2 removed
26 unmodified lines27282930313231323334358 unmodified lines26 unmodified linesconst validateStringTransformReturn = function * (optionName, chunk) { validateEmptyReturn(optionName, chunk);
if (typeof chunk !== 'string' && !isUint8Array(chunk)) { const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk; throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`); throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`); }
yield chunk;};8 unmodified linesmodified:lib/utils/uint-array.js+2 added-2 removed
1123456788910111257 unmodified linesimport {Buffer} from 'node:buffer';import {StringDecoder} from 'node:string_decoder';
const {toString: objectToString} = Object.prototype;
export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';
// Is either Uint8Array or Bufferexport const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]';
export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
const textEncoder = new TextEncoder();57 unmodified linesmodified:test/io/input-option.js+2 added-2 removed
23 unmodified lines24 };25 26 test('input option can be a String', testInput, 'foobar', runExeca);27 test('input option can be a Uint8Array', testInput, foobarUint8Array, runExeca);28+test('input option can be a Buffer', testInput, foobarBuffer, runExeca);29 test('input option can be a String - sync', testInput, 'foobar', runExecaSync);30 test('input option can be a Uint8Array - sync', testInput, foobarUint8Array, runExecaSync);31+test('input option can be a Buffer - sync', testInput, foobarBuffer, runExecaSync);32 test('input option can be used with $', testInput, 'foobar', runScript);33 test('input option can be used with $.sync', testInput, 'foobar', runScriptSync);34 35 const testInvalidInput = async (t, input, execaMethod) => {1 unmodified line37 execaMethod('empty.js', {input});38 }, {message: /a string, a Uint8Array/});39 };40 39-test('input option cannot be a Buffer', testInvalidInput, foobarBuffer, execa);41 test('input option cannot be an ArrayBuffer', testInvalidInput, foobarArrayBuffer, execa);42 test('input option cannot be a DataView', testInvalidInput, foobarDataView, execa);43 test('input option cannot be a Uint16Array', testInvalidInput, foobarUint16Array, execa);44 test('input option cannot be 0', testInvalidInput, 0, execa);45 test('input option cannot be false', testInvalidInput, false, execa);46 test('input option cannot be null', testInvalidInput, null, execa);47 test('input option cannot be a non-Readable stream', testInvalidInput, new Writable(), execa);47-test('input option cannot be a Buffer - sync', testInvalidInput, foobarBuffer, execaSync);48 test('input option cannot be an ArrayBuffer - sync', testInvalidInput, foobarArrayBuffer, execaSync);49 test('input option cannot be a DataView - sync', testInvalidInput, foobarDataView, execaSync);50 test('input option cannot be a Uint16Array - sync', testInvalidInput, foobarUint16Array, execaSync);51 test('input option cannot be 0 - sync', testInvalidInput, 0, execaSync);3 unmodified linesmodified:test/stdio/typed-array.js+19 added-12 removed
123455678910910111213141516141516171819201821222023242526242526272728293031323334import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import {foobarUint8Array, foobarString} from '../helpers/input.js';import {foobarUint8Array, foobarBuffer, foobarString} from '../helpers/input.js';
setFixtureDirectory();
const testUint8Array = async (t, fdNumber, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, foobarUint8Array));const testUint8Array = async (t, fdNumber, stdioOption, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption)); t.is(stdout, foobarString);};
test('stdin option can be a Uint8Array', testUint8Array, 0, execa);test('stdio[*] option can be a Uint8Array', testUint8Array, 3, execa);test('stdin option can be a Uint8Array - sync', testUint8Array, 0, execaSync);test('stdin option can be a Uint8Array', testUint8Array, 0, foobarUint8Array, execa);test('stdio[*] option can be a Uint8Array', testUint8Array, 3, foobarUint8Array, execa);test('stdin option can be a Uint8Array - sync', testUint8Array, 0, foobarUint8Array, execaSync);test('stdin option can be a Buffer', testUint8Array, 0, foobarBuffer, execa);test('stdio[*] option can be a Buffer', testUint8Array, 3, foobarBuffer, execa);test('stdin option can be a Buffer - sync', testUint8Array, 0, foobarBuffer, execaSync);
const testNoUint8ArrayOutput = (t, fdNumber, execaMethod) => {const testNoUint8ArrayOutput = (t, fdNumber, stdioOption, execaMethod) => { t.throws(() => { execaMethod('empty.js', getStdio(fdNumber, foobarUint8Array)); execaMethod('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /cannot be a Uint8Array/});};
test('stdout option cannot be a Uint8Array', testNoUint8ArrayOutput, 1, execa);test('stderr option cannot be a Uint8Array', testNoUint8ArrayOutput, 2, execa);test('stdout option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, 1, execaSync);test('stderr option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, 2, execaSync);test('stdout option cannot be a Uint8Array', testNoUint8ArrayOutput, 1, foobarUint8Array, execa);test('stderr option cannot be a Uint8Array', testNoUint8ArrayOutput, 2, foobarUint8Array, execa);test('stdout option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, 1, foobarUint8Array, execaSync);test('stderr option cannot be a Uint8Array - sync', testNoUint8ArrayOutput, 2, foobarUint8Array, execaSync);test('stdout option cannot be a Buffer', testNoUint8ArrayOutput, 1, foobarBuffer, execa);test('stderr option cannot be a Buffer', testNoUint8ArrayOutput, 2, foobarBuffer, execa);test('stdout option cannot be a Buffer - sync', testNoUint8ArrayOutput, 1, foobarBuffer, execaSync);test('stderr option cannot be a Buffer - sync', testNoUint8ArrayOutput, 2, foobarBuffer, execaSync);modified:test/transform/generator-return.js+9 added-1 removed
1 import {Buffer} from 'node:buffer';2 import test from 'ava';3 import {execa, execaSync} from '../../index.js';4 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5-import {foobarString, foobarUint8Array} from '../helpers/input.js';5+import {foobarString, foobarUint8Array, foobarBuffer} from '../helpers/input.js';6 import {getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';7 8 setFixtureDirectory();9 11 unmodified lines21 };22 23 test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false, execa);24 test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false, execa);25+test('Generator can return Buffer with default encoding', testGeneratorReturnType, foobarBuffer, 'utf8', true, false, false, execa);26 test('Generator can return string with encoding "utf16le"', testGeneratorReturnType, foobarString, 'utf16le', true, false, false, execa);27 test('Generator can return Uint8Array with encoding "utf16le"', testGeneratorReturnType, foobarUint8Array, 'utf16le', true, false, false, execa);28+test('Generator can return Buffer with encoding "utf16le"', testGeneratorReturnType, foobarBuffer, 'utf16le', true, false, false, execa);29 test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false, execa);30 test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false, execa);31+test('Generator can return Buffer with encoding "buffer"', testGeneratorReturnType, foobarBuffer, 'buffer', true, false, false, execa);32 test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false, execa);33 test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false, execa);34+test('Generator can return Buffer with encoding "hex"', testGeneratorReturnType, foobarBuffer, 'hex', true, false, false, execa);35 test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false, execa);36 test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false, execa);37 test('Generator can return string with encoding "utf16le", failure', testGeneratorReturnType, foobarString, 'utf16le', false, false, false, execa);38 test('Generator can return Uint8Array with encoding "utf16le", failure', testGeneratorReturnType, foobarUint8Array, 'utf16le', false, false, false, execa);50 unmodified lines89 test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true, execa);90 test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true, execa);91 test('Generator can return string with default encoding, sync', testGeneratorReturnType, foobarString, 'utf8', true, false, false, execaSync);92 test('Generator can return Uint8Array with default encoding, sync', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false, execaSync);93+test('Generator can return Buffer with default encoding, sync', testGeneratorReturnType, foobarBuffer, 'utf8', true, false, false, execaSync);94 test('Generator can return string with encoding "utf16le", sync', testGeneratorReturnType, foobarString, 'utf16le', true, false, false, execaSync);95 test('Generator can return Uint8Array with encoding "utf16le", sync', testGeneratorReturnType, foobarUint8Array, 'utf16le', true, false, false, execaSync);96+test('Generator can return Buffer with encoding "utf16le", sync', testGeneratorReturnType, foobarBuffer, 'utf16le', true, false, false, execaSync);97 test('Generator can return string with encoding "buffer", sync', testGeneratorReturnType, foobarString, 'buffer', true, false, false, execaSync);98 test('Generator can return Uint8Array with encoding "buffer", sync', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false, execaSync);99+test('Generator can return Buffer with encoding "buffer", sync', testGeneratorReturnType, foobarBuffer, 'buffer', true, false, false, execaSync);100 test('Generator can return string with encoding "hex", sync', testGeneratorReturnType, foobarString, 'hex', true, false, false, execaSync);101 test('Generator can return Uint8Array with encoding "hex", sync', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false, execaSync);102+test('Generator can return Buffer with encoding "hex", sync', testGeneratorReturnType, foobarBuffer, 'hex', true, false, false, execaSync);103 test('Generator can return string with default encoding, failure, sync', testGeneratorReturnType, foobarString, 'utf8', false, false, false, execaSync);104 test('Generator can return Uint8Array with default encoding, failure, sync', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false, execaSync);105 test('Generator can return string with encoding "utf16le", failure, sync', testGeneratorReturnType, foobarString, 'utf16le', false, false, false, execaSync);106 test('Generator can return Uint8Array with encoding "utf16le", failure, sync', testGeneratorReturnType, foobarUint8Array, 'utf16le', false, false, false, execaSync);52 unmodified linesmodified:test/transform/validate.js+4 added-25 removed
1-import {Buffer} from 'node:buffer';1 import test from 'ava';2 import {execa, execaSync} from '../../index.js';3 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';4 import {getStdio} from '../helpers/stdio.js';6-import {foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';5+import {foobarUint8Array, foobarObject} from '../helpers/input.js';6 import {serializeGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';7 8 setFixtureDirectory();9 11-const getMessage = input => {12- if (input === null || input === undefined) {13- return 'not be called at all';14- }10+const getMessage = input => input === null || input === undefined11+ ? 'not be called at all'12+ : 'a string or an Uint8Array';13 16- if (Buffer.isBuffer(input)) {17- return 'not a buffer';18- }19-20- return 'a string or an Uint8Array';21-};22-14 const lastInputGenerator = input => objectMode => [foobarUint8Array, getOutputGenerator(input)(objectMode)];15 const inputGenerator = input => objectMode => [...lastInputGenerator(input)(objectMode), serializeGenerator(true)];16 17 // eslint-disable-next-line max-params9 unmodified lines27 test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarObject, true, true);28 test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarObject, false, false);29 test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarObject, false, false);30 test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarObject, false, false);40-test('Generators with result.stdin cannot return a Buffer if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarBuffer, false, true);41-test('Generators with result.stdio[*] as input cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarBuffer, false, true);42-test('The last generator with result.stdin cannot return a Buffer even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarBuffer, true, true);43-test('The last generator with result.stdio[*] as input cannot return a Buffer even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarBuffer, true, true);44-test('Generators with result.stdout cannot return a Buffer if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarBuffer, false, false);45-test('Generators with result.stderr cannot return a Buffer if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarBuffer, false, false);46-test('Generators with result.stdio[*] as output cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarBuffer, false, false);31 test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator, null, false, true);32 test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator, null, true, true);33 test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, false, false);34 test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, true, false);15 unmodified lines50 test('The last generator with result.stdin cannot return an object even in objectMode, sync', testGeneratorReturnSync, 0, lastInputGenerator, foobarObject, true, true);51 test('Generators with result.stdout cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, foobarObject, false, false);52 test('Generators with result.stderr cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 2, getOutputGenerator, foobarObject, false, false);53 test('Generators with result.stdio[*] as output cannot return an object if not in objectMode, sync', testGeneratorReturnSync, 3, getOutputGenerator, foobarObject, false, false);70-test('Generators with result.stdin cannot return a Buffer if not in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, foobarBuffer, false, true);71-test('The last generator with result.stdin cannot return a Buffer even in objectMode, sync', testGeneratorReturnSync, 0, lastInputGenerator, foobarBuffer, true, true);72-test('Generators with result.stdout cannot return a Buffer if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, foobarBuffer, false, false);73-test('Generators with result.stderr cannot return a Buffer if not in objectMode, sync', testGeneratorReturnSync, 2, getOutputGenerator, foobarBuffer, false, false);74-test('Generators with result.stdio[*] as output cannot return a Buffer if not in objectMode, sync', testGeneratorReturnSync, 3, getOutputGenerator, foobarBuffer, false, false);54 test('Generators with result.stdin cannot return null if not in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, null, false, true);55 test('Generators with result.stdin cannot return null if in objectMode, sync', testGeneratorReturnSync, 0, inputGenerator, null, true, true);56 test('Generators with result.stdout cannot return null if not in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, null, false, false);57 test('Generators with result.stdout cannot return null if in objectMode, sync', testGeneratorReturnSync, 1, getOutputGenerator, null, true, false);17 unmodified lines