Separate generator -> Duplex utility (#698)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 35ec8de · Browse files · 6 files·+238 added−224 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+238 added−224 removed
- tests:+151 added−151 removed
- other:+87 added−73 removed
Provenance
- Commit
214b6ab- Tree
b9a66ed- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
35ec8de
6 files changed+238 added−224 removed
modified:lib/stdio/async.js+3 added-3 removed
2 unmodified lines3456778910112 unmodified lines141516171818192021222324252627272829303141 unmodified lines2 unmodified linesimport {Readable, Writable} from 'node:stream';import mergeStreams from '@sindresorhus/merge-streams';import {handleInput} from './handle.js';import {TYPE_TO_MESSAGE} from './type.js';import {generatorToTransformStream, pipeGenerator} from './generator.js';import {generatorToDuplexStream, pipeGenerator} from './generator.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleInputAsync = options => handleInput(addPropertiesAsync, options, false);
2 unmodified lines};
const addPropertiesAsync = { input: { generator: generatorToTransformStream, generator: generatorToDuplexStream, fileUrl: ({value}) => ({value: createReadStream(value)}), filePath: ({value}) => ({value: createReadStream(value.file)}), webStream: ({value}) => ({value: Readable.fromWeb(value)}), iterable: ({value}) => ({value: Readable.from(value)}), string: ({value}) => ({value: Readable.from(value)}), uint8Array: ({value}) => ({value: Readable.from(Buffer.from(value))}), }, output: { generator: generatorToTransformStream, generator: generatorToDuplexStream, fileUrl: ({value}) => ({value: createWriteStream(value)}), filePath: ({value}) => ({value: createWriteStream(value.file)}), webStream: ({value}) => ({value: Writable.fromWeb(value)}), iterable: forbiddenIfAsync,41 unmodified linesadded:lib/stdio/duplex.js+43 added
12345678910111213141516171819202122232425262728293031323334353637383940414243import {Duplex, Readable, PassThrough, getDefaultHighWaterMark} from 'node:stream';
/*Transform an array of generator functions into a `Duplex`.
The `Duplex` is created by `Duplex.from()` made of a writable stream and a readable stream, piped to each other.- The writable stream is a simple `PassThrough`, so it only forwards data to the readable part.- The `PassThrough` is read as an iterable using `passThrough.iterator()`.- This iterable is transformed to another iterable, by applying the encoding generators. Those convert the chunk type from `Buffer` to `string | Uint8Array` depending on the encoding option.- This new iterable is transformed again to another one, this time by applying the user-supplied generator.- Finally, `Readable.from()` is used to convert this final iterable to a `Readable` stream.*/export const generatorsToDuplex = (generators, {objectMode}) => { const highWaterMark = getDefaultHighWaterMark(objectMode); const passThrough = new PassThrough({objectMode, highWaterMark, destroy: destroyPassThrough}); let iterable = passThrough.iterator();
for (const generator of generators) { iterable = generator(iterable); }
const readableStream = Readable.from(iterable, {objectMode, highWaterMark}); const duplexStream = Duplex.from({writable: passThrough, readable: readableStream}); return duplexStream;};
/*When an error is thrown in a generator, the PassThrough is aborted.
This creates a race condition for which error is propagated, due to the Duplex throwing twice:- The writable side is aborted (PassThrough)- The readable side propagate the generator's error
In order for the later to win that race, we need to wait one microtask.- However we wait one macrotask instead to be on the safe side- See https://github.com/sindresorhus/execa/pull/693#discussion_r1453809450*/const destroyPassThrough = (error, done) => { setTimeout(() => { done(error); }, 0);};modified:lib/stdio/encoding.js+36 added
1234567816 unmodified lines252627282930313233343536373839404142434445464748495051525354555657585960616263import {StringDecoder} from 'node:string_decoder';
// Apply the `encoding` option using an implicit generator.// This encodes the final output of `stdout`/`stderr`.export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => { if (stdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) { return stdioStreams.map(stdioStream => ({...stdioStream, encoding})); }16 unmodified lines if (lastChunk !== '') { yield lastChunk; }};
/*When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.This allows generator functions to operate with those types instead.Chunks might be Buffer, Uint8Array or strings since:- `childProcess.stdout|stderr` emits Buffers- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string- Previous generators might return Uint8Array or string
However, those are converted to Buffer:- on writes: `Duplex.writable` `decodeStrings: true` default option- on reads: `Duplex.readable` `readableEncoding: null` default option*/export const getEncodingStartGenerator = encoding => encoding === 'buffer' ? encodingStartBufferGenerator : encodingStartStringGenerator;
const encodingStartBufferGenerator = async function * (chunks) { for await (const chunk of chunks) { yield new Uint8Array(chunk); }};
const encodingStartStringGenerator = async function * (chunks) { const textDecoder = new TextDecoder();
for await (const chunk of chunks) { yield textDecoder.decode(chunk, {stream: true}); }
const lastChunk = textDecoder.decode(); if (lastChunk !== '') { yield lastChunk; }};modified:lib/stdio/generator.js+5 added-70 removed
1-import {Duplex, Readable, PassThrough, getDefaultHighWaterMark} from 'node:stream';1+import {generatorsToDuplex} from './duplex.js';2+import {getEncodingStartGenerator} from './encoding.js';3 4 /*5 Generators can be used to transform/filter standard streams.6 10 unmodified lines17 18 We ensure `objectMode` is `false` for better buffering.19 20 Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.20-21-We return a `Duplex`, created by `Duplex.from()` made of a writable stream and a readable stream, piped to each other.22-- The writable stream is a simple `PassThrough`, so it only forwards data to the readable part.23-- The `PassThrough` is read as an iterable using `passThrough.iterator()`.24-- This iterable is transformed to another iterable, by applying the encoding generators.25- Those convert the chunk type from `Buffer` to `string | Uint8Array` depending on the encoding option.26-- This new iterable is transformed again to another one, this time by applying the user-supplied generator.27-- Finally, `Readable.from()` is used to convert this final iterable to a `Readable` stream.21 */29-export const generatorToTransformStream = ({value, encoding}) => {30- const objectMode = false;31- const highWaterMark = getDefaultHighWaterMark(objectMode);32- const passThrough = new PassThrough({objectMode, highWaterMark, destroy: destroyPassThrough});33- const iterable = passThrough.iterator();34- const encodedIterable = applyEncoding(iterable, encoding);35- const mappedIterable = value(encodedIterable);36- const readableStream = Readable.from(mappedIterable, {objectMode, highWaterMark});37- const duplexStream = Duplex.from({writable: passThrough, readable: readableStream});22+export const generatorToDuplexStream = ({value, encoding}) => {23+ const generators = [getEncodingStartGenerator(encoding), value];24+ const duplexStream = generatorsToDuplex(generators, {objectMode: false});25 return {value: duplexStream};26 };27 41-/*42-When an error is thrown in a generator, the PassThrough is aborted.43-44-This creates a race condition for which error is propagated, due to the Duplex throwing twice:45-- The writable side is aborted (PassThrough)46-- The readable side propagate the generator's error47-48-In order for the later to win that race, we need to wait one microtask.49-- However we wait one macrotask instead to be on the safe side50-- See https://github.com/sindresorhus/execa/pull/693#discussion_r145380945051-*/52-const destroyPassThrough = (error, done) => {53- setTimeout(() => {54- done(error);55- }, 0);56-};57-58-// When using generators, add an internal generator that converts chunks from `Buffer` to `string` or `Uint8Array`.59-// This allows generator functions to operate with those types instead.60-const applyEncoding = (iterable, encoding) => encoding === 'buffer'61- ? encodingStartBufferGenerator(iterable)62- : encodingStartStringGenerator(iterable);63-64-/*65-Chunks might be Buffer, Uint8Array or strings since:66-- `childProcess.stdout|stderr` emits Buffers67-- `childProcess.stdin.write()` accepts Buffer, Uint8Array or string68-- Previous generators might return Uint8Array or string69-70-However, those are converted to Buffer:71-- on writes: `Duplex.writable` `decodeStrings: true` default option72-- on reads: `Duplex.readable` `readableEncoding: null` default option73-*/74-const encodingStartStringGenerator = async function * (chunks) {75- const textDecoder = new TextDecoder();76-77- for await (const chunk of chunks) {78- yield textDecoder.decode(chunk, {stream: true});79- }80-81- const lastChunk = textDecoder.decode();82- if (lastChunk !== '') {83- yield lastChunk;84- }85-};86-87-const encodingStartBufferGenerator = async function * (chunks) {88- for await (const chunk of chunks) {89- yield new Uint8Array(chunk);90- }91-};92-28 // `childProcess.stdin|stdout|stderr|stdio` is directly mutated.29 export const pipeGenerator = (spawned, {value, direction, index}) => {30 if (direction === 'output') {31 spawned.stdio[index].pipe(value);13 unmodified linesadded:test/stdio/encoding.js+151 added
1+import {Buffer} from 'node:buffer';2+import {exec} from 'node:child_process';3+import {setTimeout} from 'node:timers/promises';4+import {promisify} from 'node:util';5+import test from 'ava';6+import getStream, {getStreamAsBuffer} from 'get-stream';7+import {execa, execaSync} from '../../index.js';8+import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';9+import {fullStdio} from '../helpers/stdio.js';10+11+const pExec = promisify(exec);12+13+setFixtureDir();14+15+const checkEncoding = async (t, encoding, index, execaMethod) => {16+ const {stdio} = await execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding});17+ compareValues(t, stdio[index], encoding);18+19+ if (execaMethod !== execaSync) {20+ const childProcess = execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});21+ const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;22+ const result = await getStreamMethod(childProcess.stdio[index]);23+ compareValues(t, result, encoding);24+ await childProcess;25+ }26+27+ if (index === 3) {28+ return;29+ }30+31+ const {stdout, stderr} = await pExec(`node noop-fd.js ${index} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});32+ compareValues(t, index === 1 ? stdout : stderr, encoding);33+};34+35+const compareValues = (t, value, encoding) => {36+ if (encoding === 'buffer') {37+ t.true(ArrayBuffer.isView(value));38+ t.true(BUFFER_TO_ENCODE.equals(value));39+ } else {40+ t.is(value, BUFFER_TO_ENCODE.toString(encoding));41+ }42+};43+44+// This string gives different outputs with each encoding type45+const STRING_TO_ENCODE = '\u1000.';46+const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);47+48+/* eslint-disable unicorn/text-encoding-identifier-case */49+test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);50+test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);51+test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);52+test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53+test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);54+test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);55+test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);56+test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);57+test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);58+test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);59+test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);60+test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);61+test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);62+test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);63+test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);64+test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);65+test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);66+test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);67+test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);68+test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);69+test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);70+test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);71+test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);72+test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);73+test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);74+test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);75+test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);76+test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);77+test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);78+test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);79+test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);80+test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);81+test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);82+test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);83+test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);84+test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);85+test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);86+test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);87+test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);88+test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);89+test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);90+test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);91+test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);92+test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);93+test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);94+test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);95+test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);96+test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);97+test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);98+test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);99+test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);100+test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);101+test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);102+test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);103+test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);104+test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);105+test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);106+test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);107+test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);108+test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);109+test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);110+test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);111+test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);112+test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);113+test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);114+test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);115+test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);116+test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);117+test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);118+test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);119+test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);120+test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);121+test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);122+test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);123+test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);124+test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);125+test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);126+test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);127+/* eslint-enable unicorn/text-encoding-identifier-case */128+129+test('validate unknown encodings', async t => {130+ await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131+});132+133+const foobarArray = ['fo', 'ob', 'ar', '..'];134+135+const delayedGenerator = async function * (chunks) {136+ // eslint-disable-next-line no-unused-vars137+ for await (const chunk of chunks) {138+ yield foobarArray[0];139+ await setTimeout(0);140+ yield foobarArray[1];141+ await setTimeout(0);142+ yield foobarArray[2];143+ await setTimeout(0);144+ yield foobarArray[3];145+ }146+};147+148+test('Handle multibyte characters', async t => {149+ const {stdout} = await execa('noop.js', {stdout: delayedGenerator, encoding: 'base64'});150+ t.is(stdout, btoa(foobarArray.join('')));151+});deleted:test/encoding.js-151 removed
1-import {Buffer} from 'node:buffer';2-import {exec} from 'node:child_process';3-import {setTimeout} from 'node:timers/promises';4-import {promisify} from 'node:util';5-import test from 'ava';6-import getStream, {getStreamAsBuffer} from 'get-stream';7-import {execa, execaSync} from '../index.js';8-import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';9-import {fullStdio} from './helpers/stdio.js';10-11-const pExec = promisify(exec);12-13-setFixtureDir();14-15-const checkEncoding = async (t, encoding, index, execaMethod) => {16- const {stdio} = await execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding});17- compareValues(t, stdio[index], encoding);18-19- if (execaMethod !== execaSync) {20- const childProcess = execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});21- const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;22- const result = await getStreamMethod(childProcess.stdio[index]);23- compareValues(t, result, encoding);24- await childProcess;25- }26-27- if (index === 3) {28- return;29- }30-31- const {stdout, stderr} = await pExec(`node noop-fd.js ${index} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});32- compareValues(t, index === 1 ? stdout : stderr, encoding);33-};34-35-const compareValues = (t, value, encoding) => {36- if (encoding === 'buffer') {37- t.true(ArrayBuffer.isView(value));38- t.true(BUFFER_TO_ENCODE.equals(value));39- } else {40- t.is(value, BUFFER_TO_ENCODE.toString(encoding));41- }42-};43-44-// This string gives different outputs with each encoding type45-const STRING_TO_ENCODE = '\u1000.';46-const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);47-48-/* eslint-disable unicorn/text-encoding-identifier-case */49-test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);50-test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);51-test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);52-test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53-test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);54-test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);55-test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);56-test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);57-test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);58-test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);59-test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);60-test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);61-test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);62-test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);63-test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);64-test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);65-test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);66-test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);67-test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);68-test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);69-test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);70-test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);71-test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);72-test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);73-test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);74-test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);75-test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);76-test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);77-test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);78-test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);79-test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);80-test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);81-test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);82-test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);83-test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);84-test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);85-test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);86-test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);87-test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);88-test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);89-test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);90-test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);91-test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);92-test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);93-test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);94-test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);95-test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);96-test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);97-test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);98-test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);99-test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);100-test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);101-test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);102-test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);103-test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);104-test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);105-test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);106-test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);107-test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);108-test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);109-test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);110-test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);111-test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);112-test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);113-test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);114-test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);115-test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);116-test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);117-test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);118-test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);119-test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);120-test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);121-test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);122-test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);123-test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);124-test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);125-test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);126-test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);127-/* eslint-enable unicorn/text-encoding-identifier-case */128-129-test('validate unknown encodings', async t => {130- await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131-});132-133-const foobarArray = ['fo', 'ob', 'ar', '..'];134-135-const delayedGenerator = async function * (chunks) {136- // eslint-disable-next-line no-unused-vars137- for await (const chunk of chunks) {138- yield foobarArray[0];139- await setTimeout(0);140- yield foobarArray[1];141- await setTimeout(0);142- yield foobarArray[2];143- await setTimeout(0);144- yield foobarArray[3];145- }146-};147-148-test('Handle multibyte characters', async t => {149- const {stdout} = await execa('noop.js', {stdout: delayedGenerator, encoding: 'base64'});150- t.is(stdout, btoa(foobarArray.join('')));151-});