Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 7701e9e · Browse files · 2 files·+37 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
2 files+37 added−21 removed
- tests:+15 added−0 removed
- other:+22 added−21 removed
Provenance
- Commit
cff6847- Tree
8b2e7cf- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7701e9e
2 files changed+37 added−21 removed
modified:lib/stream.js+22 added-21 removed
123445678914 unmodified lines242526272728292829303132303132363334353640373839404145464742434445464748495051525351545556545758595758596060616263646550 unmodified lines116117118119119120121120121122123124125126127import {Buffer} from 'node:buffer';import {once} from 'node:events';import {finished} from 'node:stream/promises';import process from 'node:process';import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer} from 'get-stream';import getStream, {getStreamAsArrayBuffer} from 'get-stream';import mergeStreams from '@sindresorhus/merge-streams';
// `all` interleaves `stdout` and `stderr`export const makeAllStream = (spawned, {all}) => {14 unmodified lines
// On failure, `result.stdout|stderr|all` should contain the currently buffered stream// They are automatically closed and flushed by Node.js when the child process exits// We guarantee this by calling `childProcess.kill()`const getBufferedData = async (stream, streamPromise) => { // When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve if (!stream || streamPromise === undefined) {// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieveconst getBufferedData = async (streamPromise, encoding) => { return; }
try { return await streamPromise; } catch (error) { return error.bufferedData; return error.bufferedData === undefined ? undefined : applyEncoding(error.bufferedData, encoding); }};
const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {const getStreamPromise = async (stream, {encoding, buffer, maxBuffer}) => { if (!stream || !buffer) { return; }
// eslint-disable-next-line unicorn/text-encoding-identifier-case if (encoding === 'utf8' || encoding === 'utf-8') { return getStream(stream, {maxBuffer}); const contents = isUtf8Encoding(encoding) ? await getStream(stream, {maxBuffer}) : await getStreamAsArrayBuffer(stream, {maxBuffer}); return applyEncoding(contents, encoding);};
const applyEncoding = (contents, encoding) => { if (isUtf8Encoding(encoding)) { return contents; }
if (encoding === 'buffer') { return getStreamAsArrayBuffer(stream, {maxBuffer}).then(arrayBuffer => new Uint8Array(arrayBuffer)); return new Uint8Array(contents); }
return applyEncoding(stream, maxBuffer, encoding); return Buffer.from(contents).toString(encoding);};
const applyEncoding = async (stream, maxBuffer, encoding) => { const buffer = await getStreamAsBuffer(stream, {maxBuffer}); return buffer.toString(encoding);};// eslint-disable-next-line unicorn/text-encoding-identifier-caseconst isUtf8Encoding = encoding => encoding === 'utf8' || encoding === 'utf-8';
// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.// This makes sure we want for the completion of those streams, in order to catch any error.50 unmodified lines } catch (error) { spawned.kill(); const results = await Promise.all([ {error, signal: error.signal, timedOut: error.timedOut}, getBufferedData(spawned.stdout, stdoutPromise), getBufferedData(spawned.stderr, stderrPromise), getBufferedData(spawned.all, allPromise), getBufferedData(stdoutPromise, encoding), getBufferedData(stderrPromise, encoding), getBufferedData(allPromise, encoding), ]); cleanupStdioStreams(stdioStreams, error); return results; }};modified:test/stream.js+15 added
100 unmodified lines101 102 test('maxBuffer affects stdout', testMaxBuffer, 'stdout');103 test('maxBuffer affects stderr', testMaxBuffer, 'stderr');104 105+test('maxBuffer works with encoding buffer', async t => {106+ const {stdout} = await t.throwsAsync(107+ execa('max-buffer.js', ['stdout', '11'], {maxBuffer: 10, encoding: 'buffer'}),108+ );109+ t.true(stdout instanceof Uint8Array);110+ t.is(Buffer.from(stdout).toString(), '.'.repeat(10));111+});112+113+test('maxBuffer works with other encodings', async t => {114+ const {stdout} = await t.throwsAsync(115+ execa('max-buffer.js', ['stdout', '11'], {maxBuffer: 10, encoding: 'hex'}),116+ );117+ t.is(stdout, Buffer.from('.'.repeat(10)).toString('hex'));118+});119+120 const testNoMaxBuffer = async (t, streamName) => {121 const promise = execa('max-buffer.js', [streamName, '10'], {buffer: false});122 const [result, output] = await Promise.all([123 promise,102 unmodified lines