Skip to content
Myr

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

7 files+55 added−40 removed

modified:lib/return/error.js+3 added-3 removed
modified:lib/stdio/sync.js+2 added-2 removed
1import {readFileSync, writeFileSync} from 'node:fs';2import {bufferToUint8Array, binaryToString} from '../utils.js';2import {bufferToUint8Array, uint8ArrayToString} from '../utils.js';3import {handleInput} from './handle.js';4import {TYPE_TO_MESSAGE} from './type.js';56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode
36 unmodified lines
43 ? inputs[0].value44 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');45};4647const serializeInput = ({type, value}) => type === 'string' ? value : binaryToString(value);47const serializeInput = ({type, value}) => type === 'string' ? value : uint8ArrayToString(value);4849// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode50export const pipeOutputSync = (stdioStreamsGroups, {output}) => {51 if (output === null) {
19 unmodified lines
modified:lib/stdio/validate.js+5 added-3 removed
1import {isBinary} from '../utils.js';1import {Buffer} from 'node:buffer';2import {isUint8Array} from '../utils.js';34export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode5 ? validateObjectTransformReturn.bind(undefined, optionName)6 : validateStringTransformReturn.bind(undefined, optionName);
5 unmodified lines
1213const validateStringTransformReturn = function * (optionName, chunk) {14 validateEmptyReturn(optionName, chunk);1515 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 }2021 yield chunk;22};
8 unmodified lines
modified:lib/script.js+3 added-3 removed
1import isPlainObject from 'is-plain-obj';2import {isBinary, binaryToString, isSubprocess} from './utils.js';2import {isUint8Array, uint8ArrayToString, isSubprocess} from './utils.js';3import {execa} from './async.js';4import {execaSync} from './sync.js';56export const create$ = options => {
147 unmodified lines
154 if (typeOfStdout === 'string') {155 return expression.stdout;156 }157158 if (isBinary(expression.stdout)) {159 return binaryToString(expression.stdout);158 if (isUint8Array(expression.stdout)) {159 return uint8ArrayToString(expression.stdout);160 }161162 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);163 }
13 unmodified lines
modified:lib/utils.js+1 added-2 removed
4 unmodified lines
56export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);78export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);9export const isBinary = value => isUint8Array(value) || Buffer.isBuffer(value);910const textDecoder = new TextDecoder();12export const binaryToString = uint8ArrayOrBuffer => textDecoder.decode(uint8ArrayOrBuffer);11export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);1213export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);14export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];15export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];
14 unmodified lines
modified:test/stdio/encoding-transform.js-7 removed
67 unmodified lines
6869test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');70test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');71test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');72test('Next generator argument is string with default encoding, with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'utf8', false, false, 'String');73test('Next generator argument is string with default encoding, with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, false, 'String');74test('Next generator argument is string with default encoding, with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, true, 'String');72test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');73test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'String');74test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'String');75test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');76test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'Uint8Array');77test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'buffer', false, false, 'Uint8Array');82test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, false, 'Uint8Array');83test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, true, 'Uint8Array');78test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');79test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');80test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');88test('Next generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'hex', false, false, 'String');82test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');83test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');84test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');85
96 unmodified lines
modified:test/stdio/validate.js+41 added-20 removed
1import {Buffer} from 'node:buffer';2import test from 'ava';3import {execa} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {getStdio} from '../helpers/stdio.js';5import {foobarUint8Array, foobarObject} from '../helpers/input.js';6import {foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';7import {serializeGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';89setFixtureDir();1011// eslint-disable-next-line max-params11const 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});12const 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};1819const getMessage = input => {20 if (input === null || input === undefined) {21 return 'not be called at all';22 }2324 if (Buffer.isBuffer(input)) {25 return 'not a buffer';26 }2728 return 'a string or an Uint8Array';29};3031const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];32const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];3320test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator(foobarObject, false), 'stdin-fd.js', false);21test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator(foobarObject, false), 'stdin-fd.js', false);22test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);23test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);24test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);25test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);26test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);27test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator(null, false), 'stdin-fd.js', true);28test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator(null, true), 'stdin-fd.js', true);29test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, false), 'noop-fd.js', true);30test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, true), 'noop-fd.js', true);31test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, false), 'stdin-fd.js', true);32test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, true), 'stdin-fd.js', true);33test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, false), 'noop-fd.js', true);34test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, true), 'noop-fd.js', true);34test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarObject, false, true);35test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarObject, false, true);36test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarObject, true, true);37test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarObject, true, true);38test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarObject, false, false);39test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarObject, false, false);40test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarObject, false, false);41test('Generators with result.stdin cannot return a Buffer if not in objectMode', testGeneratorReturn, 0, inputGenerator, foobarBuffer, false, true);42test('Generators with result.stdio[*] as input cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, inputGenerator, foobarBuffer, false, true);43test('The last generator with result.stdin cannot return a Buffer even in objectMode', testGeneratorReturn, 0, lastInputGenerator, foobarBuffer, true, true);44test('The last generator with result.stdio[*] as input cannot return a Buffer even in objectMode', testGeneratorReturn, 3, lastInputGenerator, foobarBuffer, true, true);45test('Generators with result.stdout cannot return a Buffer if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, foobarBuffer, false, false);46test('Generators with result.stderr cannot return a Buffer if not in objectMode', testGeneratorReturn, 2, getOutputGenerator, foobarBuffer, false, false);47test('Generators with result.stdio[*] as output cannot return a Buffer if not in objectMode', testGeneratorReturn, 3, getOutputGenerator, foobarBuffer, false, false);48test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator, null, false, true);49test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator, null, true, true);50test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, false, false);51test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator, null, true, false);52test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, false, true);53test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator, undefined, true, true);54test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, false, false);55test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator, undefined, true, false);5657test('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