Use `Uint8Array` instead of `Buffer` (#915)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent f8af153 · Browse files · 7 files·+55 added−40 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+55 added−40 removed
- tests:+41 added−27 removed
- other:+14 added−13 removed
Provenance
- Commit
410bac3- Tree
08ea88e- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f8af153
7 files changed+55 added−40 removed
modified:lib/return/error.js+3 added-3 removed
12334567179 unmodified linesExpand all187188189190191192191192193194195196import {signalsByName} from 'human-signals';import stripFinalNewline from 'strip-final-newline';import {isBinary, binaryToString} from '../utils.js';import {isUint8Array, uint8ArrayToString} from '../utils.js';import {fixCwdError} from '../arguments/cwd.js';import {escapeLines} from '../arguments/escape.js';import {getDurationMs} from './duration.js';import {getFinalError, DiscardedError, isExecaError} from './cause.js';179 unmodified linesExpand all if (typeof messageItem === 'string') { return messageItem; }
if (isBinary(messageItem)) { return binaryToString(messageItem); if (isUint8Array(messageItem)) { return uint8ArrayToString(messageItem); }
return '';};modified:lib/stdio/sync.js+2 added-2 removed
1 import {readFileSync, writeFileSync} from 'node:fs';2-import {bufferToUint8Array, binaryToString} from '../utils.js';2+import {bufferToUint8Array, uint8ArrayToString} from '../utils.js';3 import {handleInput} from './handle.js';4 import {TYPE_TO_MESSAGE} from './type.js';5 6 // Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode36 unmodified lines43 ? inputs[0].value44 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');45 };46 47-const serializeInput = ({type, value}) => type === 'string' ? value : binaryToString(value);47+const serializeInput = ({type, value}) => type === 'string' ? value : uint8ArrayToString(value);48 49 // Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode50 export const pipeOutputSync = (stdioStreamsGroups, {output}) => {51 if (output === null) {19 unmodified linesmodified:lib/stdio/validate.js+5 added-3 removed
1-import {isBinary} from '../utils.js';1+import {Buffer} from 'node:buffer';2+import {isUint8Array} from '../utils.js';3 4 export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode5 ? validateObjectTransformReturn.bind(undefined, optionName)6 : validateStringTransformReturn.bind(undefined, optionName);5 unmodified lines12 13 const validateStringTransformReturn = function * (optionName, chunk) {14 validateEmptyReturn(optionName, chunk);15 15- if (typeof chunk !== 'string' && !isBinary(chunk)) {16- throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);16+ if (typeof chunk !== 'string' && !isUint8Array(chunk)) {17+ const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk;18+ throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`);19 }20 21 yield chunk;22 };8 unmodified linesmodified:lib/script.js+3 added-3 removed
1 import isPlainObject from 'is-plain-obj';2-import {isBinary, binaryToString, isSubprocess} from './utils.js';2+import {isUint8Array, uint8ArrayToString, isSubprocess} from './utils.js';3 import {execa} from './async.js';4 import {execaSync} from './sync.js';5 6 export const create$ = options => {147 unmodified lines154 if (typeOfStdout === 'string') {155 return expression.stdout;156 }157 158- if (isBinary(expression.stdout)) {159- return binaryToString(expression.stdout);158+ if (isUint8Array(expression.stdout)) {159+ return uint8ArrayToString(expression.stdout);160 }161 162 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);163 }13 unmodified linesmodified:lib/utils.js+1 added-2 removed
4 unmodified lines5 6 export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);7 8 export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);9-export const isBinary = value => isUint8Array(value) || Buffer.isBuffer(value);9 10 const textDecoder = new TextDecoder();12-export const binaryToString = uint8ArrayOrBuffer => textDecoder.decode(uint8ArrayOrBuffer);11+export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);12 13 export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);14 export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];15 export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];14 unmodified linesmodified:test/stdio/encoding-transform.js-7 removed
67 unmodified lines68 69 test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');70 test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');71 test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');72-test('Next generator argument is string with default encoding, with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'utf8', false, false, 'String');73-test('Next generator argument is string with default encoding, with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, false, 'String');74-test('Next generator argument is string with default encoding, with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, true, 'String');72 test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');73 test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'String');74 test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'String');75 test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');76 test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'Uint8Array');77 test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'Uint8Array');81-test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'buffer', false, false, 'Uint8Array');82-test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, false, 'Uint8Array');83-test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, true, 'Uint8Array');78 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');79 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');80 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');81 test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');88-test('Next generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'hex', false, false, 'String');82 test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');83 test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');84 test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');85 96 unmodified linesmodified:test/stdio/validate.js+41 added-20 removed
1+import {Buffer} from 'node:buffer';2 import test from 'ava';3 import {execa} from '../../index.js';4 import {setFixtureDir} from '../helpers/fixtures-dir.js';5 import {getStdio} from '../helpers/stdio.js';5-import {foobarUint8Array, foobarObject} from '../helpers/input.js';6+import {foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';7 import {serializeGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';8 9 setFixtureDir();10 11 // eslint-disable-next-line max-params11-const testGeneratorReturn = async (t, fdNumber, generators, fixtureName, isNull) => {12- const subprocess = execa(fixtureName, [`${fdNumber}`], getStdio(fdNumber, generators));13- const message = isNull ? /not be called at all/ : /a string or an Uint8Array/;14- await t.throwsAsync(subprocess, {message});12+const testGeneratorReturn = async (t, fdNumber, generator, input, objectMode, isInput) => {13+ const fixtureName = isInput ? 'stdin-fd.js' : 'noop-fd.js';14+ const subprocess = execa(fixtureName, [`${fdNumber}`], getStdio(fdNumber, generator(input, objectMode)));15+ const {message} = await t.throwsAsync(subprocess);16+ t.true(message.includes(getMessage(input)));17 };18 19+const getMessage = input => {20+ if (input === null || input === undefined) {21+ return 'not be called at all';22+ }23+24+ if (Buffer.isBuffer(input)) {25+ return 'not a buffer';26+ }27+28+ return 'a string or an Uint8Array';29+};30+31 const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];32 const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];33 20-test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator(foobarObject, false), 'stdin-fd.js', false);21-test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator(foobarObject, false), 'stdin-fd.js', false);22-test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);23-test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);24-test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);25-test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);26-test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);27-test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator(null, false), 'stdin-fd.js', true);28-test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator(null, true), 'stdin-fd.js', true);29-test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, false), 'noop-fd.js', true);30-test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, true), 'noop-fd.js', true);31-test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, false), 'stdin-fd.js', true);32-test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, true), 'stdin-fd.js', true);33-test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, false), 'noop-fd.js', true);34-test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, true), 'noop-fd.js', true);34+test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarObject, false, true);35+test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarObject, false, true);36+test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarObject, true, true);37+test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarObject, true, true);38+test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarObject, false, false);39+test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarObject, false, false);40+test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarObject, false, false);41+test('Generators with result.stdin cannot return a Buffer if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarBuffer, false, true);42+test('Generators with result.stdio[*] as input cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarBuffer, false, true);43+test('The last generator with result.stdin cannot return a Buffer even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarBuffer, true, true);44+test('The last generator with result.stdio[*] as input cannot return a Buffer even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarBuffer, true, true);45+test('Generators with result.stdout cannot return a Buffer if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarBuffer, false, false);46+test('Generators with result.stderr cannot return a Buffer if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarBuffer, false, false);47+test('Generators with result.stdio[*] as output cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarBuffer, false, false);48+test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator, null, false, true);49+test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator, null, true, true);50+test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, false, false);51+test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, true, false);52+test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, false, true);53+test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, true, true);54+test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, false, false);55+test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, true, false);56 57 test('Generators "final" return value is validated', async t => {58 const subprocess = execa('noop.js', {stdout: convertTransformToFinal(getOutputGenerator(null, true), true)});59 await t.throwsAsync(subprocess, {message: /not be called at all/});1 unmodified line