Fix using both `objectMode` and `encoding` options (#736)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent fcc8e11 · Browse files · 3 files·+40 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−21 removed
- tests:+29 added−19 removed
- other:+11 added−2 removed
Provenance
- Commit
9f205f6- Tree
ba9ddbf- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fcc8e11
3 files changed+40 added−21 removed
modified:lib/stdio/encoding.js+11 added-2 removed
8 unmodified lines910111213131415161718196 unmodified lines262728292930313233344 unmodified lines3940414243444546474849505152535440 unmodified lines8 unmodified lines if (newStdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) { return newStdioStreams; }
const transform = encodingEndGenerator.bind(undefined, encoding); const objectMode = newStdioStreams.findLast(({type}) => type === 'generator')?.value.objectMode === true; const encodingEndGenerator = objectMode ? encodingEndObjectGenerator : encodingEndStringGenerator; const transform = encodingEndGenerator.bind(undefined, encoding); return [ ...newStdioStreams, { ...newStdioStreams[0],6 unmodified lines
// eslint-disable-next-line unicorn/text-encoding-identifier-caseconst IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);
const encodingEndGenerator = async function * (encoding, chunks) {const encodingEndStringGenerator = async function * (encoding, chunks) { const stringDecoder = new StringDecoder(encoding);
for await (const chunk of chunks) { yield stringDecoder.write(chunk);4 unmodified lines yield lastChunk; }};
const encodingEndObjectGenerator = async function * (encoding, chunks) { const stringDecoder = new StringDecoder(encoding);
for await (const chunk of chunks) { yield isUint8Array(chunk) ? stringDecoder.end(chunk) : chunk; }};
/*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:40 unmodified linesmodified:test/helpers/generator.js+15 added
1234531 unmodified lines373839404142434445464748495051525354import {setImmediate} from 'node:timers/promises';import {foobarObject} from './input.js';
export const noopGenerator = objectMode => ({ async * transform(lines) {31 unmodified lines objectMode,});
export const outputObjectGenerator = getOutputGenerator(foobarObject, true);
export const getChunksGenerator = (chunks, objectMode) => ({ async * transform(lines) { // eslint-disable-next-line no-unused-vars for await (const line of lines) { for (const chunk of chunks) { yield chunk; // eslint-disable-next-line no-await-in-loop await setImmediate(); } } }, objectMode,});modified:test/stdio/encoding.js+14 added-19 removed
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3-import {setImmediate} from 'node:timers/promises';3 import {promisify} from 'node:util';4 import test from 'ava';5 import getStream, {getStreamAsBuffer} from 'get-stream';6 import {execa, execaSync} from '../../index.js';7 import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';8 import {fullStdio} from '../helpers/stdio.js';9+import {outputObjectGenerator, getChunksGenerator} from '../helpers/generator.js';10+import {foobarObject} from '../helpers/input.js';11 12 const pExec = promisify(exec);13 14 setFixtureDir();117 unmodified lines132 });133 134 const foobarArray = ['fo', 'ob', 'ar', '..'];135 135-const delayedGenerator = async function * (lines) {136- // eslint-disable-next-line no-unused-vars137- for await (const line of lines) {138- yield foobarArray[0];139- await setImmediate();140- yield foobarArray[1];141- await setImmediate();142- yield foobarArray[2];143- await setImmediate();144- yield foobarArray[3];145- }146-};136+test('Handle multibyte characters', async t => {137+ const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, false), encoding: 'base64'});138+ t.is(stdout, btoa(foobarArray.join('')));139+});140 148-const testMultiByteCharacter = async (t, objectMode) => {149- const {stdout} = await execa('noop.js', {stdout: {transform: delayedGenerator, objectMode}, encoding: 'base64'});150- t.is(objectMode ? stdout.join('') : stdout, btoa(foobarArray.join('')));151-};141+test('Handle multibyte characters, with objectMode', async t => {142+ const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, true), encoding: 'base64'});143+ t.deepEqual(stdout, foobarArray.map(chunk => btoa(chunk)));144+});145 153-test('Handle multibyte characters', testMultiByteCharacter, false);154-test('Handle multibyte characters, with objectMode', testMultiByteCharacter, true);146+test('Other encodings work with transforms that return objects', async t => {147+ const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});148+ t.deepEqual(stdout, [foobarObject]);149+});