Skip to content
Myr

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

6 files+238 added−224 removed

modified:lib/stdio/async.js+3 added-3 removed
added:lib/stdio/duplex.js+43 added
modified:lib/stdio/encoding.js+36 added
modified:lib/stdio/generator.js+5 added-70 removed
1import {Duplex, Readable, PassThrough, getDefaultHighWaterMark} from 'node:stream';1import {generatorsToDuplex} from './duplex.js';2import {getEncodingStartGenerator} from './encoding.js';34/*5Generators can be used to transform/filter standard streams.6
10 unmodified lines
1718We ensure `objectMode` is `false` for better buffering.1920Chunks are currently processed serially. We could add a `concurrency` option to parallelize in the future.2021We 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*/29export 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});22export const generatorToDuplexStream = ({value, encoding}) => {23 const generators = [getEncodingStartGenerator(encoding), value];24 const duplexStream = generatorsToDuplex(generators, {objectMode: false});25 return {value: duplexStream};26};2741/*42When an error is thrown in a generator, the PassThrough is aborted.4344This 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 error4748In 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*/52const destroyPassThrough = (error, done) => {53 setTimeout(() => {54 done(error);55 }, 0);56};5758// 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.60const applyEncoding = (iterable, encoding) => encoding === 'buffer'61 ? encodingStartBufferGenerator(iterable)62 : encodingStartStringGenerator(iterable);6364/*65Chunks 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 string6970However, those are converted to Buffer:71- on writes: `Duplex.writable` `decodeStrings: true` default option72- on reads: `Duplex.readable` `readableEncoding: null` default option73*/74const encodingStartStringGenerator = async function * (chunks) {75 const textDecoder = new TextDecoder();7677 for await (const chunk of chunks) {78 yield textDecoder.decode(chunk, {stream: true});79 }8081 const lastChunk = textDecoder.decode();82 if (lastChunk !== '') {83 yield lastChunk;84 }85};8687const encodingStartBufferGenerator = async function * (chunks) {88 for await (const chunk of chunks) {89 yield new Uint8Array(chunk);90 }91};9228// `childProcess.stdin|stdout|stderr|stdio` is directly mutated.29export const pipeGenerator = (spawned, {value, direction, index}) => {30 if (direction === 'output') {31 spawned.stdio[index].pipe(value);
13 unmodified lines
added:test/stdio/encoding.js+151 added
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import {setTimeout} from 'node:timers/promises';4import {promisify} from 'node:util';5import test from 'ava';6import getStream, {getStreamAsBuffer} from 'get-stream';7import {execa, execaSync} from '../../index.js';8import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';9import {fullStdio} from '../helpers/stdio.js';1011const pExec = promisify(exec);1213setFixtureDir();1415const 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);1819 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 }2627 if (index === 3) {28 return;29 }3031 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};3435const 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};4344// This string gives different outputs with each encoding type45const STRING_TO_ENCODE = '\u1000.';46const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4748/* eslint-disable unicorn/text-encoding-identifier-case */49test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);50test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);51test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);52test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);54test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);55test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);56test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);57test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);58test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);59test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);60test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);61test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);62test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);63test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);64test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);65test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);66test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);67test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);68test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);69test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);70test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);71test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);72test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);73test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);74test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);75test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);76test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);77test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);78test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);79test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);80test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);81test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);82test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);83test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);84test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);85test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);86test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);87test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);88test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);89test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);90test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);91test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);92test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);93test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);94test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);95test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);96test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);97test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);98test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);99test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);100test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);101test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);102test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);103test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);104test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);105test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);106test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);107test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);108test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);109test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);110test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);111test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);112test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);113test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);114test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);115test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);116test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);117test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);118test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);119test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);120test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);121test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);122test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);123test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);124test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);125test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);126test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);127/* eslint-enable unicorn/text-encoding-identifier-case */128129test('validate unknown encodings', async t => {130 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131});132133const foobarArray = ['fo', 'ob', 'ar', '..'];134135const 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};147148test('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
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import {setTimeout} from 'node:timers/promises';4import {promisify} from 'node:util';5import test from 'ava';6import getStream, {getStreamAsBuffer} from 'get-stream';7import {execa, execaSync} from '../index.js';8import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';9import {fullStdio} from './helpers/stdio.js';1011const pExec = promisify(exec);1213setFixtureDir();1415const 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);1819 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 }2627 if (index === 3) {28 return;29 }3031 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};3435const 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};4344// This string gives different outputs with each encoding type45const STRING_TO_ENCODE = '\u1000.';46const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4748/* eslint-disable unicorn/text-encoding-identifier-case */49test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);50test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);51test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);52test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);54test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);55test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);56test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);57test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);58test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);59test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);60test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);61test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);62test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);63test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);64test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);65test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);66test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);67test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);68test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);69test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);70test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);71test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);72test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);73test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);74test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);75test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);76test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);77test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);78test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);79test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);80test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);81test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);82test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);83test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);84test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);85test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);86test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);87test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);88test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);89test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);90test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);91test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);92test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);93test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);94test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);95test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);96test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);97test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);98test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);99test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);100test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);101test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);102test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);103test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);104test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);105test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);106test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);107test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);108test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);109test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);110test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);111test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);112test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);113test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);114test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);115test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);116test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);117test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);118test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);119test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);120test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);121test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);122test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);123test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);124test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);125test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);126test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);127/* eslint-enable unicorn/text-encoding-identifier-case */128129test('validate unknown encodings', async t => {130 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131});132133const foobarArray = ['fo', 'ob', 'ar', '..'];134135const 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};147148test('Handle multibyte characters', async t => {149 const {stdout} = await execa('noop.js', {stdout: delayedGenerator, encoding: 'base64'});150 t.is(stdout, btoa(foobarArray.join('')));151});