Fix `encoding` option not working with `ignore` and `inherit` (#738)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent a5761a8 · Browse files · 4 files·+51 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+51 added−16 removed
- tests:+24 added−0 removed
- other:+27 added−16 removed
Provenance
- Commit
61ed169- Tree
330bc32- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a5761a8
4 files changed+51 added−16 removed
modified:lib/stdio/encoding.js+7 added-1 removed
123456789910111213149 unmodified lines2425262728293031323334353664 unmodified linesimport {StringDecoder} from 'node:string_decoder';import {Buffer} from 'node:buffer';import {isUint8Array} from './utils.js';import {willPipeStreams} from './pipe.js';
// Apply the `encoding` option using an implicit generator.// This encodes the final output of `stdout`/`stderr`.export const handleStreamsEncoding = (stdioStreams, {encoding}, isSync) => { const newStdioStreams = stdioStreams.map(stdioStream => ({...stdioStream, encoding})); if (newStdioStreams[0].direction === 'input' || IGNORED_ENCODINGS.has(encoding) || isSync) { if (!shouldEncodeOutput(newStdioStreams, encoding, isSync)) { return newStdioStreams; }
const objectMode = newStdioStreams.findLast(({type}) => type === 'generator')?.value.objectMode === true;9 unmodified lines }, ];};
const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output' && !IGNORED_ENCODINGS.has(encoding) && !isSync && willPipeStreams(stdioStreams);
// eslint-disable-next-line unicorn/text-encoding-identifier-caseconst IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);
const encodingEndStringGenerator = async function * (encoding, chunks) {64 unmodified linesmodified:lib/stdio/handle.js+2 added-15 removed
3 unmodified lines4567891011123 unmodified lines1617181919202122232466 unmodified lines919293949495969798991001011021031041051061073 unmodified linesimport {handleNativeStream} from './native.js';import {handleInputOptions} from './input.js';import {handleStreamsEncoding} from './encoding.js';import {normalizeGenerators} from './generator.js';import {updateStdio} from './pipe.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options, isSync) => { const stdio = normalizeStdio(options);3 unmodified lines .map(stdioStreams => addStreamDirection(stdioStreams)) .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync)) .map(stdioStreams => normalizeGenerators(stdioStreams)) .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties)); options.stdio = transformStdio(stdioStreamsGroups); options.stdio = updateStdio(stdioStreamsGroups); return stdioStreamsGroups;};
// We make sure passing an array with a single item behaves the same as passing that item without an array.66 unmodified linesconst addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({ ...stdioStream, ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),}));
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => transformStdioItem(stdioStreams));
const transformStdioItem = stdioStreams => { if (stdioStreams.length > 1) { return stdioStreams.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe'; }
const [stdioStream] = stdioStreams; return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;};added:lib/stdio/pipe.js+18 added
1+// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `childProcess.std*`.2+// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `childProcess.std*`.3+// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `childProcess.std*`.4+export const updateStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => updateStdioItem(stdioStreams));5+6+// Whether `childProcess.std*` will be set7+export const willPipeStreams = stdioStreams => PIPED_STDIO_VALUES.has(updateStdioItem(stdioStreams));8+9+const PIPED_STDIO_VALUES = new Set(['pipe', 'overlapped', undefined, null]);10+11+const updateStdioItem = stdioStreams => {12+ if (stdioStreams.length > 1) {13+ return stdioStreams.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';14+ }15+16+ const [stdioStream] = stdioStreams;17+ return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;18+};modified:test/stdio/encoding.js+24 added
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3+import process from 'node:process';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';139 unmodified lines147 test('Other encodings work with transforms that return objects', async t => {148 const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});149 t.deepEqual(stdout, [foobarObject]);150 });151+152+const testIgnoredEncoding = async (t, stdoutOption, isUndefined) => {153+ const {stdout} = await execa('empty.js', {stdout: stdoutOption, encoding: 'base64'});154+ t.is(stdout === undefined, isUndefined);155+};156+157+test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true);158+test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true);159+test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true);160+test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true);161+test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true);162+test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true);163+test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true);164+test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true);165+test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true);166+test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true);167+test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false);168+test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false);169+test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false);170+test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false);171+test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false);172+test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false);173+test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false);