Improve handling of `encoding` option (#926)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent af3aefd · Browse files · 29 files·+207 added−109 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
29 files+207 added−109 removed
- tests:+78 added−49 removed
- other:+129 added−60 removed
Provenance
- Commit
d786fcf- Tree
5a4ac00- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
af3aefd
29 files changed+207 added−109 removed
modified:docs/transform.md+2 added-2 removed
17 unmodified lines18 19 ## Encoding20 21 The `line` argument passed to the transform is a string by default.\22-However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `Uint8Array` instead.22+However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding` option](../readme.md#encoding) is binary, it is an `Uint8Array` instead.23 24 The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `line` argument's type.25 26 ## Filtering15 unmodified lines42 43 ## Binary data44 45 The transform iterates over lines by default.\46-However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#encoding) option is `buffer`, it iterates over arbitrary chunks of data instead.46+However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding` option](../readme.md#encoding) is binary, it iterates over arbitrary chunks of data instead.47 48 ```js49 await execa('./binary.js', {stdout: {transform, binary: true}});50 ```153 unmodified linesmodified:lib/convert/add.js+6 added-5 removed
12345678991010111212131413141516import {isBinaryEncoding} from '../encoding.js';import {initializeConcurrentStreams} from './concurrent.js';import {createReadable} from './readable.js';import {createWritable} from './writable.js';import {createDuplex} from './duplex.js';import {createIterable} from './iterable.js';
export const addConvertedStreams = (subprocess, {encoding}) => { const concurrentStreams = initializeConcurrentStreams(); const isBuffer = encoding === 'buffer'; subprocess.readable = createReadable.bind(undefined, {subprocess, concurrentStreams, isBuffer}); const useBinaryEncoding = isBinaryEncoding(encoding); subprocess.readable = createReadable.bind(undefined, {subprocess, concurrentStreams, useBinaryEncoding}); subprocess.writable = createWritable.bind(undefined, {subprocess, concurrentStreams}); subprocess.duplex = createDuplex.bind(undefined, {subprocess, concurrentStreams, isBuffer}); subprocess.iterable = createIterable.bind(undefined, subprocess, isBuffer); subprocess[Symbol.asyncIterator] = createIterable.bind(undefined, subprocess, isBuffer, {}); subprocess.duplex = createDuplex.bind(undefined, {subprocess, concurrentStreams, useBinaryEncoding}); subprocess.iterable = createIterable.bind(undefined, subprocess, useBinaryEncoding); subprocess[Symbol.asyncIterator] = createIterable.bind(undefined, subprocess, useBinaryEncoding, {});};modified:lib/convert/duplex.js+2 added-2 removed
13 unmodified lines14151617181918192021222321 unmodified lines13 unmodified lines onWritableDestroy,} from './writable.js';
// Create a `Duplex` stream combining bothexport const createDuplex = ({subprocess, concurrentStreams, isBuffer}, {from, to, binary: binaryOption = true, preserveNewlines = true} = {}) => { const binary = binaryOption || isBuffer;export const createDuplex = ({subprocess, concurrentStreams, useBinaryEncoding}, {from, to, binary: binaryOption = true, preserveNewlines = true} = {}) => { const binary = binaryOption || useBinaryEncoding; const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams); const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams); const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary); const {read, onStdoutDataDone} = getReadableMethods({subprocessStdout, subprocess, binary, preserveNewlines});21 unmodified linesmodified:lib/convert/iterable.js+2 added-2 removed
123445678991011121312 unmodified linesimport {getReadable} from '../pipe/validate.js';import {iterateOnStdout} from './loop.js';
export const createIterable = (subprocess, isBuffer, {export const createIterable = (subprocess, useBinaryEncoding, { from, binary: binaryOption = false, preserveNewlines = false,} = {}) => { const binary = binaryOption || isBuffer; const binary = binaryOption || useBinaryEncoding; const subprocessStdout = getReadable(subprocess, from); const onStdoutData = iterateOnStdout({subprocessStdout, subprocess, binary, preserveNewlines, isStream: false}); return iterateOnStdoutData(onStdoutData, subprocessStdout, subprocess);};12 unmodified linesmodified:lib/convert/readable.js+2 added-2 removed
10 unmodified lines11121314151615161718192074 unmodified lines10 unmodified lines} from './shared.js';import {iterateOnStdout, DEFAULT_OBJECT_HIGH_WATER_MARK} from './loop.js';
// Create a `Readable` stream that forwards from `stdout` and awaits the subprocessexport const createReadable = ({subprocess, concurrentStreams, isBuffer}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => { const binary = binaryOption || isBuffer;export const createReadable = ({subprocess, concurrentStreams, useBinaryEncoding}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => { const binary = binaryOption || useBinaryEncoding; const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams); const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary); const {read, onStdoutDataDone} = getReadableMethods({subprocessStdout, subprocess, binary, preserveNewlines}); const readable = new Readable({74 unmodified linesmodified:lib/return/error.js+1 added-1 removed
1 import {signalsByName} from 'human-signals';2 import stripFinalNewline from 'strip-final-newline';3-import {isUint8Array, uint8ArrayToString} from '../utils.js';3+import {isUint8Array, uint8ArrayToString} from '../encoding.js';4 import {fixCwdError} from '../arguments/cwd.js';5 import {escapeLines} from '../arguments/escape.js';6 import {getDurationMs} from './duration.js';7 import {getFinalError, DiscardedError, isExecaError} from './cause.js';189 unmodified linesmodified:lib/return/output.js+1 added-1 removed
1 import {Buffer} from 'node:buffer';2 import stripFinalNewline from 'strip-final-newline';3-import {bufferToUint8Array} from '../utils.js';3+import {bufferToUint8Array} from '../encoding.js';4 import {logFinalResult} from '../verbose/complete.js';5 6 export const handleOutput = (options, value) => {7 if (value === undefined || value === null) {23 unmodified linesmodified:lib/stdio/encoding-final.js+2 added-4 removed
1 import {StringDecoder} from 'node:string_decoder';2+import {isSimpleEncoding} from '../encoding.js';3 import {willPipeStreams} from './forward.js';4 5 // Apply the `encoding` option using an implicit generator.6 // This encodes the final output of `stdout`/`stderr`.24 unmodified lines31 ];32 };33 34 const shouldEncodeOutput = (stdioStreams, encoding, isSync) => stdioStreams[0].direction === 'output'34- && !IGNORED_ENCODINGS.has(encoding)35+ && !isSimpleEncoding(encoding)36 && !isSync37 && willPipeStreams(stdioStreams);38 38-// eslint-disable-next-line unicorn/text-encoding-identifier-case39-const IGNORED_ENCODINGS = new Set(['utf8', 'utf-8', 'buffer']);40-39 const encodingStringGenerator = function * (stringDecoder, chunk) {40 yield stringDecoder.write(chunk);41 };42 6 unmodified linesmodified:lib/stdio/generator.js+2 added-1 removed
1+import {isBinaryEncoding} from '../encoding.js';2 import {generatorsToTransform} from './transform.js';3 import {getEncodingTransformGenerator} from './encoding-transform.js';4 import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';5 import {pipeStreams} from './pipeline.js';20 unmodified lines26 binary: binaryOption = false,27 preserveNewlines = false,28 objectMode,29 } = isGeneratorOptions(value) ? value : {transform: value};29- const binary = binaryOption || encoding === 'buffer';30+ const binary = binaryOption || isBinaryEncoding(encoding);31 const objectModes = stdioStream.direction === 'output'32 ? getOutputObjectModes(objectMode, index, newGenerators)33 : getInputObjectModes(objectMode, index, newGenerators);34 return {...stdioStream, value: {transform, final, binary, preserveNewlines, ...objectModes}};79 unmodified linesmodified:lib/stdio/input.js+1 added-1 removed
1 import {isReadableStream} from 'is-stream';2-import {isUint8Array} from '../utils.js';2+import {isUint8Array} from '../encoding.js';3 import {isUrl, isFilePathString} from './type.js';4 5 // Append the `stdin` option with the `input` and `inputFile` options6 export const handleInputOptions = ({input, inputFile}) => [44 unmodified linesmodified:lib/stdio/lines.js+2 added-1 removed
1+import {isBinaryEncoding} from '../encoding.js';2 import {willPipeStreams} from './forward.js';3 4 // Split chunks line-wise for streams exposed to users like `subprocess.stdout`.5 // Appending a noop transform in object mode is enough to do this, since every non-binary transform iterates line-wise.9 unmodified lines15 : stdioStreams;16 17 const shouldSplitLines = ({stdioStreams, lines, encoding, isSync}) => stdioStreams[0].direction === 'output'18 && lines18- && encoding !== 'buffer'19+ && !isBinaryEncoding(encoding)20 && !isSync21 && willPipeStreams(stdioStreams);22 23 const linesEndGenerator = function * (chunk) {2 unmodified linesmodified:lib/stdio/sync.js+1 added-1 removed
1 import {readFileSync, writeFileSync} from 'node:fs';2-import {bufferToUint8Array, uint8ArrayToString} from '../utils.js';2+import {bufferToUint8Array, uint8ArrayToString} from '../encoding.js';3 import {handleInput} from './handle.js';4 import {TYPE_TO_MESSAGE} from './type.js';5 6 // Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode64 unmodified linesmodified:lib/stdio/type.js+1 added-1 removed
1 import {isStream as isNodeStream} from 'is-stream';2-import {isUint8Array} from '../utils.js';2+import {isUint8Array} from '../encoding.js';3 4 // The `stdin`/`stdout`/`stderr` option can be of many types. This detects it.5 export const getStdioOptionType = (stdioOption, optionName) => {6 if (isGenerator(stdioOption)) {94 unmodified linesmodified:lib/stdio/validate.js+1 added-1 removed
1 import {Buffer} from 'node:buffer';2-import {isUint8Array} from '../utils.js';2+import {isUint8Array} from '../encoding.js';3 4 export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode5 ? validateObjectTransformReturn.bind(undefined, optionName)6 : validateStringTransformReturn.bind(undefined, optionName);24 unmodified linesmodified:lib/stream/all.js+2 added-1 removed
1 import mergeStreams from '@sindresorhus/merge-streams';2 import {generatorToDuplexStream} from '../stdio/generator.js';3+import {isBufferEncoding} from '../encoding.js';4 import {waitForSubprocessStream} from './subprocess.js';5 6 // `all` interleaves `stdout` and `stderr`7 export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)21 unmodified lines29 30 const getAllStreamTransform = encoding => ({31 value: {32 * final() {},32- binary: encoding === 'buffer',33+ binary: isBufferEncoding(encoding),34 writableObjectMode: true,35 readableObjectMode: true,36 preserveNewlines: true,37 },2 unmodified linesmodified:lib/stream/subprocess.js+3 added-2 removed
1 import {setImmediate} from 'node:timers/promises';2 import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';3+import {isBufferEncoding} from '../encoding.js';4 import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';5 6 export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, streamInfo}) => {7 if (!stream) {38 unmodified lines46 if (stream.readableObjectMode) {47 return getStreamAsArray(stream, {maxBuffer});48 }49 49- const contents = encoding === 'buffer'50+ const contents = isBufferEncoding(encoding)51 ? await getStreamAsArrayBuffer(stream, {maxBuffer})52 : await getStream(stream, {maxBuffer});53 return applyEncoding(contents, encoding);54 };12 unmodified lines67 const handleBufferedData = (error, encoding) => error.bufferedData === undefined || Array.isArray(error.bufferedData)68 ? error.bufferedData69 : applyEncoding(error.bufferedData, encoding);70 70-const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;71+const applyEncoding = (contents, encoding) => isBufferEncoding(encoding) ? new Uint8Array(contents) : contents;modified:lib/verbose/output.js+2 added-5 removed
1 import {inspect} from 'node:util';2 import {escapeLines} from '../arguments/escape.js';3+import {isBinaryEncoding} from '../encoding.js';4 import {PIPED_STDIO_VALUES} from '../stdio/forward.js';5 import {verboseLog} from './log.js';6 7 export const handleStreamsVerbose = ({stdioStreams, options, isSync, stdioState, verboseInfo}) => {15 unmodified lines23 // They can also lead to double printing when passing file descriptor integers or `process.std*`.24 // This only leaves with `pipe` and `overlapped`.25 const shouldLogOutput = (stdioStreams, {encoding}, isSync, {verbose}) => verbose === 'full'26 && !isSync26- && ALLOWED_ENCODINGS.has(encoding)27+ && !isBinaryEncoding(encoding)28 && fdUsesVerbose(stdioStreams)29 && (stdioStreams.some(({type, value}) => type === 'native' && PIPED_STDIO_VALUES.has(value))30 || stdioStreams.every(({type}) => type === 'generator'));31 31-// Only print text output, not binary32-// eslint-disable-next-line unicorn/text-encoding-identifier-case33-const ALLOWED_ENCODINGS = new Set(['utf8', 'utf-8']);34-32 // Printing input streams would be confusing.33 // Files and streams can produce big outputs, which we don't want to print.34 // We could print `stdio[3+]` but it often is redirected to files and streams, with the same issue.35 // So we only print stdout and stderr.29 unmodified linesadded:lib/encoding.js+19 added
1+import {Buffer} from 'node:buffer';2+3+export const isSimpleEncoding = encoding => isBufferEncoding(encoding) || DEFAULT_ENCODING.has(encoding.toLowerCase());4+5+// eslint-disable-next-line unicorn/text-encoding-identifier-case6+const DEFAULT_ENCODING = new Set(['utf8', 'utf-8']);7+8+export const isBufferEncoding = encoding => encoding === null || encoding.toLowerCase() === 'buffer';9+10+export const isBinaryEncoding = encoding => isBufferEncoding(encoding) || BINARY_ENCODINGS.has(encoding.toLowerCase());11+12+const BINARY_ENCODINGS = new Set(['hex', 'base64', 'base64url', 'latin1', 'binary', 'ascii']);13+14+export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);15+16+export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);17+18+const textDecoder = new TextDecoder();19+export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);modified:lib/script.js+2 added-1 removed
1 import isPlainObject from 'is-plain-obj';2-import {isUint8Array, uint8ArrayToString, isSubprocess} from './utils.js';2+import {isUint8Array, uint8ArrayToString} from './encoding.js';3+import {isSubprocess} from './utils.js';4 import {execa} from './async.js';5 import {execaSync} from './sync.js';6 7 export const create$ = options => {170 unmodified linesmodified:lib/utils.js-8 removed
1-import {Buffer} from 'node:buffer';1 import {ChildProcess} from 'node:child_process';2 import {addAbortListener} from 'node:events';3 import process from 'node:process';4 6-export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);7-8-export const isUint8Array = value => Object.prototype.toString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);9-10-const textDecoder = new TextDecoder();11-export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);12-5 export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);6 export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];7 export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];8 13 unmodified linesmodified:test/convert/loop.js+36 added-25 removed
19 unmodified lines20 noNewlinesFull,21 complexFull,22 singleComplexBuffer,23 singleComplexUint8Array,24+ singleComplexHex,25+ singleComplexHexBuffer,26+ singleComplexHexUint8Array,27 complexChunks,28 complexChunksEnd,29 } from '../helpers/lines.js';30 import {outputObjectGenerator, getOutputGenerator} from '../helpers/generator.js';19 unmodified lines50 await assertMethod(t, streamOrIterable, expectedChunks);51 };52 53 // eslint-disable-next-line max-params51-const testText = async (t, expectedChunks, methodName, binary, preserveNewlines, isUint8Array) => {52- const options = isUint8Array ? {encoding: 'buffer'} : {};53- const subprocess = getSubprocess(methodName, complexFull, options);54+const testText = async (t, expectedChunks, methodName, binary, preserveNewlines, encoding) => {55+ const subprocess = getSubprocess(methodName, complexFull, {encoding});56 const stream = subprocess[methodName]({binary, preserveNewlines});57 58 await assertChunks(t, stream, expectedChunks, methodName);57- await assertSubprocessOutput(t, subprocess, stringToUint8Arrays(complexFull, isUint8Array));59+ const expectedOutput = encoding === 'hex'60+ ? singleComplexHex61+ : stringToUint8Arrays(complexFull, encoding === 'buffer');62+ await assertSubprocessOutput(t, subprocess, expectedOutput);63 };64 60-test('.iterable() can use "binary: true"', testText, [singleComplexUint8Array], 'iterable', true, undefined, false);61-test('.iterable() can use "binary: undefined"', testText, complexChunks, 'iterable', undefined, undefined, false);62-test('.iterable() can use "binary: undefined" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', undefined, undefined, true);63-test('.iterable() can use "binary: false"', testText, complexChunks, 'iterable', false, undefined, false);64-test('.iterable() can use "binary: false" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', false, undefined, true);65-test('.iterable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'iterable', false, true, false);66-test('.iterable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'iterable', false, false, false);67-test('.readable() can use "binary: true"', testText, singleComplexBuffer, 'readable', true, undefined, false);68-test('.readable() can use "binary: undefined"', testText, singleComplexBuffer, 'readable', undefined, undefined, false);69-test('.readable() can use "binary: undefined" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', undefined, undefined, true);70-test('.readable() can use "binary: false"', testText, complexChunksEnd, 'readable', false, undefined, false);71-test('.readable() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', false, undefined, true);72-test('.readable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'readable', false, true, false);73-test('.readable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'readable', false, false, false);74-test('.duplex() can use "binary: true"', testText, singleComplexBuffer, 'duplex', true, undefined, false);75-test('.duplex() can use "binary: undefined"', testText, singleComplexBuffer, 'duplex', undefined, undefined, false);76-test('.duplex() can use "binary: undefined" + "encoding: "buffer"', testText, singleComplexBuffer, 'duplex', undefined, undefined, true);77-test('.duplex() can use "binary: false"', testText, complexChunksEnd, 'duplex', false, undefined, false);78-test('.duplex() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'duplex', false, undefined, true);79-test('.duplex() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'duplex', false, true, false);80-test('.duplex() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'duplex', false, false, false);65+test('.iterable() can use "binary: true"', testText, [singleComplexUint8Array], 'iterable', true, undefined, 'utf8');66+test('.iterable() can use "binary: undefined"', testText, complexChunks, 'iterable', undefined, undefined, 'utf8');67+test('.iterable() can use "binary: undefined" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', undefined, undefined, 'buffer');68+test('.iterable() can use "binary: undefined" + "encoding: hex"', testText, [singleComplexHexUint8Array], 'iterable', undefined, undefined, 'hex');69+test('.iterable() can use "binary: false"', testText, complexChunks, 'iterable', false, undefined, 'utf8');70+test('.iterable() can use "binary: false" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', false, undefined, 'buffer');71+test('.iterable() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexUint8Array], 'iterable', false, undefined, 'hex');72+test('.iterable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'iterable', false, true, 'utf8');73+test('.iterable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'iterable', false, false, 'utf8');74+test('.readable() can use "binary: true"', testText, singleComplexBuffer, 'readable', true, undefined, 'utf8');75+test('.readable() can use "binary: undefined"', testText, singleComplexBuffer, 'readable', undefined, undefined, 'utf8');76+test('.readable() can use "binary: undefined" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', undefined, undefined, 'buffer');77+test('.readable() can use "binary: undefined" + "encoding: hex"', testText, [singleComplexHexBuffer], 'readable', undefined, undefined, 'hex');78+test('.readable() can use "binary: false"', testText, complexChunksEnd, 'readable', false, undefined, 'utf8');79+test('.readable() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', false, undefined, 'buffer');80+test('.readable() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexBuffer], 'readable', false, undefined, 'hex');81+test('.readable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'readable', false, true, 'utf8');82+test('.readable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'readable', false, false, 'utf8');83+test('.duplex() can use "binary: true"', testText, singleComplexBuffer, 'duplex', true, undefined, 'utf8');84+test('.duplex() can use "binary: undefined"', testText, singleComplexBuffer, 'duplex', undefined, undefined, 'utf8');85+test('.duplex() can use "binary: undefined" + "encoding: "buffer"', testText, singleComplexBuffer, 'duplex', undefined, undefined, 'buffer');86+test('.duplex() can use "binary: undefined" + "encoding: "hex"', testText, [singleComplexHexBuffer], 'duplex', undefined, undefined, 'hex');87+test('.duplex() can use "binary: false"', testText, complexChunksEnd, 'duplex', false, undefined, 'utf8');88+test('.duplex() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'duplex', false, undefined, 'buffer');89+test('.duplex() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexBuffer], 'duplex', false, undefined, 'hex');90+test('.duplex() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'duplex', false, true, 'utf8');91+test('.duplex() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'duplex', false, false, 'utf8');92 93 const testTextOutput = async (t, expectedOutput, methodName, preserveNewlines) => {94 const subprocess = getSubprocess(methodName, complexFull);95 const stream = subprocess[methodName]({binary: false, preserveNewlines});88 unmodified linesmodified:test/helpers/lines.js+4 added
10 unmodified lines11 export const simpleFull = 'aaa\nbbb\nccc';12 export const simpleChunks = [simpleFull];13 export const simpleFullUint8Array = textEncoder.encode(simpleFull);14 export const simpleChunksUint8Array = [simpleFullUint8Array];15+export const simpleFullHex = Buffer.from(simpleFull).toString('hex');16 export const simpleChunksBuffer = [Buffer.from(simpleFull)];17 export const simpleLines = ['aaa\n', 'bbb\n', 'ccc'];18 export const simpleFullEndLines = ['aaa\n', 'bbb\n', 'ccc\n'];19 export const noNewlinesFull = 'aaabbbccc';20 export const noNewlinesChunks = ['aaa', 'bbb', 'ccc'];21 export const complexFull = '\naaa\r\nbbb\n\nccc';22 export const singleComplexBuffer = [Buffer.from(complexFull)];23 export const singleComplexUint8Array = textEncoder.encode(complexFull);24+export const singleComplexHex = Buffer.from(complexFull).toString('hex');25+export const singleComplexHexBuffer = Buffer.from(singleComplexHex);26+export const singleComplexHexUint8Array = textEncoder.encode(singleComplexHex);27 export const complexChunksEnd = ['\n', 'aaa\r\n', 'bbb\n', '\n', 'ccc'];28 export const complexChunks = ['', 'aaa', 'bbb', '', 'ccc'];modified:test/stdio/encoding-transform.js+6 added-6 removed
35 unmodified lines36 test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', 'String', false);37 test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false);38 test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', 'Uint8Array', false);39 test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', 'Uint8Array', false);40-test('First generator argument is string with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', 'String', false);41-test('First generator argument is string with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', 'String', false);42-test('First generator argument is string with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', 'String', false);40+test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false);41+test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', 'Uint8Array', false);42+test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', 'Uint8Array', false);43 test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', true);44 test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', 'Object', true);45 test('First generator argument is string with default encoding, with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', false, false);46 test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'utf8', 'Uint8Array', false, true);47 test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, false);48 test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, true);49-test('First generator argument is string with encoding "hex", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'hex', 'String', false, false);49+test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, false);50 test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, true);51 52 const testEncodingIgnored = async (t, encoding) => {53 const input = Buffer.from(foobarString).toString(encoding);32 unmodified lines86 test('Next generator argument is string with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'String');87 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');88 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');89 test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');90-test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');91-test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');90+test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'Uint8Array');91+test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'Uint8Array');92 test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');93 test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');94 95 const testFirstOutputGeneratorArgument = async (t, fdNumber) => {94 unmodified linesmodified:test/stdio/generator.js+19 added-1 removed
1+import {Buffer} from 'node:buffer';2 import {readFile, writeFile, rm} from 'node:fs/promises';3 import {PassThrough} from 'node:stream';4 import test from 'ava';5 import getStream, {getStreamAsArray} from 'get-stream';186 unmodified lines192 if (objectMode) {193 return outputObjectGenerator;194 }195 195- return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator();196+ return encoding === 'utf8' ? uppercaseGenerator() : uppercaseBufferGenerator;197 };198 199 const getStdoutStderrOutput = (output, stdioOption, encoding, objectMode) => {200 if (objectMode && !stdioOption) {201 return [foobarObject];202 }203 204 const stdioOutput = stdioOption ? output : output.toUpperCase();205+206+ if (encoding === 'hex') {207+ return Buffer.from(stdioOutput).toString('hex');208+ }209+210 return encoding === 'buffer' ? textEncoder.encode(stdioOutput) : stdioOutput;211 };212 213 const getAllOutput = (stdoutOutput, stderrOutput, encoding, objectMode) => {29 unmodified lines243 test('Can use generators with result.all = transform + transform', testGeneratorAll, true, 'utf8', false, false, false);244 test('Can use generators with error.all = transform + transform', testGeneratorAll, false, 'utf8', false, false, false);245 test('Can use generators with result.all = transform + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, false);246 test('Can use generators with error.all = transform + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, false);247+test('Can use generators with result.all = transform + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, false, false);248+test('Can use generators with error.all = transform + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, false, false);249 test('Can use generators with result.all = transform + pipe', testGeneratorAll, true, 'utf8', false, false, true);250 test('Can use generators with error.all = transform + pipe', testGeneratorAll, false, 'utf8', false, false, true);251 test('Can use generators with result.all = transform + pipe, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, true);252 test('Can use generators with error.all = transform + pipe, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, true);253+test('Can use generators with result.all = transform + pipe, encoding "hex"', testGeneratorAll, true, 'hex', false, false, true);254+test('Can use generators with error.all = transform + pipe, encoding "hex"', testGeneratorAll, false, 'hex', false, false, true);255 test('Can use generators with result.all = pipe + transform', testGeneratorAll, true, 'utf8', false, true, false);256 test('Can use generators with error.all = pipe + transform', testGeneratorAll, false, 'utf8', false, true, false);257 test('Can use generators with result.all = pipe + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, true, false);258 test('Can use generators with error.all = pipe + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, true, false);259+test('Can use generators with result.all = pipe + transform, encoding "hex"', testGeneratorAll, true, 'hex', false, true, false);260+test('Can use generators with error.all = pipe + transform, encoding "hex"', testGeneratorAll, false, 'hex', false, true, false);261 test('Can use generators with result.all = transform + transform, objectMode', testGeneratorAll, true, 'utf8', true, false, false);262 test('Can use generators with error.all = transform + transform, objectMode', testGeneratorAll, false, 'utf8', true, false, false);263 test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, false);264 test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, false);265+test('Can use generators with result.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, false);266+test('Can use generators with error.all = transform + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, false);267 test('Can use generators with result.all = transform + pipe, objectMode', testGeneratorAll, true, 'utf8', true, false, true);268 test('Can use generators with error.all = transform + pipe, objectMode', testGeneratorAll, false, 'utf8', true, false, true);269 test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, true);270 test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, true);271+test('Can use generators with result.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, false, true);272+test('Can use generators with error.all = transform + pipe, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, false, true);273 test('Can use generators with result.all = pipe + transform, objectMode', testGeneratorAll, true, 'utf8', true, true, false);274 test('Can use generators with error.all = pipe + transform, objectMode', testGeneratorAll, false, 'utf8', true, true, false);275 test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false);276 test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false);277+test('Can use generators with result.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, true, 'hex', true, true, false);278+test('Can use generators with error.all = pipe + transform, objectMode, encoding "hex"', testGeneratorAll, false, 'hex', true, true, false);279 280 test('Can use generators with input option', async t => {281 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: uppercaseGenerator(), input: foobarUint8Array});282 t.is(stdout, foobarUppercase);84 unmodified linesmodified:test/stdio/lines.js+8 added-17 removed
8 unmodified lines9 import {10 simpleFull,11 simpleChunks,12 simpleFullUint8Array,13+ simpleFullHex,14 simpleLines,15 simpleFullEndLines,16 noNewlinesChunks,17 } from '../helpers/lines.js';18 19 setFixtureDir();20 20-test('"lines: true" is a noop when using "encoding: buffer"', async t => {21- const {stdout} = await execa('noop-fd.js', ['1', simpleFull], {lines: true, encoding: 'buffer'});22- t.deepEqual(stdout, simpleFullUint8Array);23-});24-21 // eslint-disable-next-line max-params22 const testStreamLines = async (t, fdNumber, input, expectedOutput, stripFinalNewline) => {23 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, input], {24 ...fullStdio,50 unmodified lines75 });76 t.deepEqual(stdout, [foobarObject]);77 });78 83-const singleLine = 'a\n';84-const singleLineStrip = 'a';85-86-const testOtherEncoding = async (t, stripFinalNewline, strippedLine) => {87- const {stdout} = await execa('noop-fd.js', ['1', `${singleLine}${singleLine}`], {88- lines: true,89- encoding: 'base64',90- stripFinalNewline,91- });92- t.deepEqual(stdout, [strippedLine, strippedLine]);79+const testOtherEncoding = async (t, expectedOutput, encoding, stripFinalNewline) => {80+ const {stdout} = await execa('noop-fd.js', ['1', simpleFull], {lines: true, encoding, stripFinalNewline});81+ t.deepEqual(stdout, expectedOutput);82 };83 95-test('"lines: true" does not work with other encodings', testOtherEncoding, false, singleLine);96-test('"lines: true" does not work with other encodings, stripFinalNewline', testOtherEncoding, true, singleLineStrip);84+test('"lines: true" is a noop with "encoding: buffer"', testOtherEncoding, simpleFullUint8Array, 'buffer', false);85+test('"lines: true" is a noop with "encoding: buffer", stripFinalNewline', testOtherEncoding, simpleFullUint8Array, 'buffer', false);86+test('"lines: true" is a noop with "encoding: hex"', testOtherEncoding, simpleFullHex, 'hex', false);87+test('"lines: true" is a noop with "encoding: hex", stripFinalNewline', testOtherEncoding, simpleFullHex, 'hex', true);88 89 const getSimpleChunkSubprocess = (stripFinalNewline, options) => execa('noop-fd.js', ['1', ...simpleChunks], {90 lines: true,91 stripFinalNewline,36 unmodified linesmodified:test/stdio/split.js+5 added
12 unmodified lines13 simpleFull,14 simpleChunks,15 simpleFullUint8Array,16 simpleChunksUint8Array,17+ simpleFullHex,18 simpleLines,19 simpleFullEndLines,20 noNewlinesFull,21 noNewlinesChunks,130 unmodified lines152 test('Splits lines when "binary" is false', testBinaryOption, false, simpleChunks, simpleLines, simpleFull, false, true);153 test('Splits lines when "binary" is undefined', testBinaryOption, undefined, simpleChunks, simpleLines, simpleFull, false, true);154 test('Does not split lines when "binary" is undefined, encoding "buffer"', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullUint8Array, false, true, 'buffer');155 test('Does not split lines when "binary" is false, encoding "buffer"', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullUint8Array, false, true, 'buffer');156+test('Does not split lines when "binary" is undefined, encoding "hex"', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, true, 'hex');157+test('Does not split lines when "binary" is false, encoding "hex"', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, true, 'hex');158 test('Does not split lines when "binary" is true, objectMode', testBinaryOption, true, simpleChunks, simpleChunksUint8Array, simpleChunksUint8Array, true, true);159 test('Splits lines when "binary" is false, objectMode', testBinaryOption, false, simpleChunks, simpleLines, simpleLines, true, true);160 test('Splits lines when "binary" is undefined, objectMode', testBinaryOption, undefined, simpleChunks, simpleLines, simpleLines, true, true);161 test('Does not split lines when "binary" is true, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunksUint8Array, simpleFull, false, false);1 unmodified line163 test('Splits lines when "binary" is undefined, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, simpleFullEnd, false, false);164 test('Does not split lines when "binary" is undefined, encoding "buffer", preserveNewlines', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullUint8Array, false, false, 'buffer');165 test('Does not split lines when "binary" is false, encoding "buffer", preserveNewlines', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullUint8Array, false, false, 'buffer');166 test('Does not split lines when "binary" is true, objectMode, preserveNewlines', testBinaryOption, true, simpleChunks, simpleChunksUint8Array, simpleChunksUint8Array, true, false);167+test('Does not split lines when "binary" is undefined, encoding "hex", preserveNewlines', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex');168+test('Does not split lines when "binary" is false, encoding "hex", preserveNewlines', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex');169 test('Splits lines when "binary" is false, objectMode, preserveNewlines', testBinaryOption, false, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);170 test('Splits lines when "binary" is undefined, objectMode, preserveNewlines', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false);171 172 const resultUint8ArrayGenerator = function * (lines, chunk) {82 unmodified linesmodified:index.d.ts+23 added-13 removed
83 unmodified lines84 85 type StdioOptions<IsSync extends boolean = boolean> = BaseStdioOption | StdioOptionsArray<IsSync>;86 87 type DefaultEncodingOption = 'utf8';88-type BufferEncodingOption = 'buffer';89-type EncodingOption =88+type TextEncodingOption =89 | DefaultEncodingOption90 // eslint-disable-next-line unicorn/text-encoding-identifier-case91 | 'utf-8'92 | 'utf16le'93 | 'utf-16le'94 | 'ucs2'96- | 'ucs-2'97- | 'latin1'98- | 'binary'99- | 'ascii'95+ | 'ucs-2';96+type BufferEncodingOption = 'buffer';97+type BinaryEncodingOption =98+ | BufferEncodingOption99 | 'hex'100 | 'base64'101 | 'base64url'102+ | 'latin1'103+ | 'binary'104+ | 'ascii';105+type EncodingOption =106+ | TextEncodingOption103- | BufferEncodingOption104- | undefined;107+ | BinaryEncodingOption108+ | undefined;109 110 // Whether `result.stdout|stderr|all` is an array of values due to `objectMode: true`111 type IsObjectStream<112 FdNumber extends string,298 unmodified lines411 - `result.stdout`, `result.stderr`, `result.all` and `result.stdio` are arrays of lines.412 - `subprocess.stdout`, `subprocess.stderr`, `subprocess.all`, `subprocess.stdio`, `subprocess.readable()` and `subprocess.duplex()` iterate over lines instead of arbitrary chunks.413 - Any stream passed to the `stdout`, `stderr` or `stdio` option receives lines instead of arbitrary chunks.414 411- This cannot be used if the `encoding` option is `'buffer'`.415+ This cannot be used if the `encoding` option is binary.416 417 @default false418 */419 readonly lines?: IfAsync<IsSync, boolean>;67 unmodified lines487 */488 readonly shell?: boolean | string | URL;489 490 /**487- Specify the character encoding used to decode the `stdout`, `stderr` and `stdio` output. If set to `'buffer'`, then `stdout`, `stderr` and `stdio` will be `Uint8Array`s instead of strings.491+ If the subprocess outputs text, specifies its character encoding, either `'utf8'` or `'utf16le'`.492 493+ If it outputs binary data instead, this should be either:494+ - `'buffer'`: returns the binary output as an `Uint8Array`.495+ - `'hex'`, `'base64'`, `'base64url'`, [`'latin1'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) or [`'ascii'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings): encodes the binary output as a string.496+497+ The output is available with `result.stdout`, `result.stderr` and `result.stdio`.498+499 @default 'utf8'500 */501 readonly encoding?: EncodingOption;502 67 unmodified lines570 571 If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:572 - the `stdout`/`stderr` option is `ignore` or `inherit`.573 - the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), a file, a file descriptor, or another subprocess.564- - the `encoding` option is set.574+ - the `encoding` option is binary.575 576 This can also be set to `'full'` by setting the `NODE_DEBUG=execa` environment variable in the current process.577 578 @default 'none'427 unmodified lines1006 If `false`, the stream iterates over lines. Each line is a string. Also, the stream is in [object mode](https://nodejs.org/api/stream.html#object-mode).1007 1008 If `true`, the stream iterates over arbitrary chunks of data. Each line is an `Uint8Array` (with `.iterable()`) or a [`Buffer`](https://nodejs.org/api/buffer.html#class-buffer) (otherwise).1009 1000- This is always `true` if the `encoding` option is `'buffer'`.1010+ This is always `true` when the `encoding` option is binary.1011 1012 @default `false` with `.iterable()`, `true` otherwise1013 */1014 readonly binary?: boolean;20 unmodified lines1035 type SubprocessAsyncIterable<1036 BinaryOption extends boolean | undefined,1037 EncodingOption extends Options['encoding'],1038 > = AsyncIterableIterator<1029-EncodingOption extends BufferEncodingOption1039+EncodingOption extends BinaryEncodingOption1040 ? Uint8Array1041 : BinaryOption extends true1042 ? Uint8Array1043 : string551 unmodified linesmodified:index.test-d.ts+41 added
84 unmodified lines85 86 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});87 const bufferResult = await execaBufferPromise;88 89+ const execaHexPromise = execa('unicorns', {encoding: 'hex', all: true});90+ const hexResult = await execaHexPromise;91+92 const scriptPromise = $`unicorns`;93 94 const pipeOptions = {from: 'stderr', to: 'fd3', all: true} as const;95 225 unmodified lines321 expectType<AsyncIterableIterator<Uint8Array>>(execaBufferPromise.iterable());322 expectType<AsyncIterableIterator<Uint8Array>>(execaBufferPromise.iterable({binary: false}));323 expectType<AsyncIterableIterator<Uint8Array>>(execaBufferPromise.iterable({binary: true}));324 expectType<AsyncIterableIterator<Uint8Array>>(execaBufferPromise.iterable({} as {binary: boolean}));325+ expectType<AsyncIterableIterator<Uint8Array>>(execaHexPromise.iterable());326+ expectType<AsyncIterableIterator<Uint8Array>>(execaHexPromise.iterable({binary: false}));327+ expectType<AsyncIterableIterator<Uint8Array>>(execaHexPromise.iterable({binary: true}));328+ expectType<AsyncIterableIterator<Uint8Array>>(execaHexPromise.iterable({} as {binary: boolean}));329 330 expectType<Readable>(scriptPromise.readable());331 expectType<Writable>(scriptPromise.writable());332 expectType<Duplex>(scriptPromise.duplex());95 unmodified lines428 expectType<Uint8Array>(bufferResult.stderr);429 expectType<Uint8Array>(bufferResult.stdio[2]);430 expectType<Uint8Array>(bufferResult.all);431 432+ expectType<Writable>(execaHexPromise.stdin);433+ expectType<Readable>(execaHexPromise.stdout);434+ expectType<Readable>(execaHexPromise.stderr);435+ expectType<Readable>(execaHexPromise.all);436+ expectType<string>(hexResult.stdout);437+ expectType<string>(hexResult.stdio[1]);438+ expectType<string>(hexResult.stderr);439+ expectType<string>(hexResult.stdio[2]);440+ expectType<string>(hexResult.all);441+442 const linesResult = await execa('unicorns', {lines: true, all: true});443 expectType<string[]>(linesResult.stdout);444 expectType<string[]>(linesResult.stdio[1]);445 expectType<string[]>(linesResult.stderr);616 unmodified lines1062 execa('unicorns', {localDir: '.'});1063 execaSync('unicorns', {localDir: '.'});1064 execa('unicorns', {localDir: fileUrl});1065 execaSync('unicorns', {localDir: fileUrl});1066+// eslint-disable-next-line unicorn/text-encoding-identifier-case1067+execa('unicorns', {encoding: 'utf-8'});1068+// eslint-disable-next-line unicorn/text-encoding-identifier-case1069+execaSync('unicorns', {encoding: 'utf-8'});1070+execa('unicorns', {encoding: 'utf16le'});1071+execaSync('unicorns', {encoding: 'utf16le'});1072+execa('unicorns', {encoding: 'utf-16le'});1073+execaSync('unicorns', {encoding: 'utf-16le'});1074+execa('unicorns', {encoding: 'ucs2'});1075+execaSync('unicorns', {encoding: 'ucs2'});1076+execa('unicorns', {encoding: 'ucs-2'});1077+execaSync('unicorns', {encoding: 'ucs-2'});1078+execa('unicorns', {encoding: 'hex'});1079+execaSync('unicorns', {encoding: 'hex'});1080+execa('unicorns', {encoding: 'base64'});1081+execaSync('unicorns', {encoding: 'base64'});1082+execa('unicorns', {encoding: 'base64url'});1083+execaSync('unicorns', {encoding: 'base64url'});1084+execa('unicorns', {encoding: 'latin1'});1085+execaSync('unicorns', {encoding: 'latin1'});1086+execa('unicorns', {encoding: 'binary'});1087+execaSync('unicorns', {encoding: 'binary'});1088+execa('unicorns', {encoding: 'ascii'});1089+execaSync('unicorns', {encoding: 'ascii'});1090 expectError(execa('unicorns', {encoding: 'unknownEncoding'}));1091 expectError(execaSync('unicorns', {encoding: 'unknownEncoding'}));1092 execa('unicorns', {buffer: false});1093 expectError(execaSync('unicorns', {buffer: false}));687 unmodified linesmodified:readme.md+11 added-5 removed
518 unmodified lines519 If `false`, the stream iterates over lines. Each line is a string. Also, the stream is in [object mode](https://nodejs.org/api/stream.html#object-mode).520 521 If `true`, the stream iterates over arbitrary chunks of data. Each line is an `Uint8Array` (with [`.iterable()`](#iterablereadableoptions)) or a [`Buffer`](https://nodejs.org/api/buffer.html#class-buffer) (otherwise).522 523-This is always `true` if the [`encoding` option](#encoding) is `'buffer'`.523+This is always `true` when the [`encoding` option](#encoding) is binary.524 525 ##### readableOptions.preserveNewlines526 527 Type: `boolean`\299 unmodified lines827 828 If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:829 - the [`stdout`](#stdout-1)/[`stderr`](#stderr-1) option is `ignore` or `inherit`.830 - the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), [a file](#stdout-1), a file descriptor, or [another subprocess](#pipefile-arguments-options).831-- the [`encoding`](#encoding) option is set.831+- the [`encoding` option](#encoding) is binary.832 833 This can also be set to `'full'` by setting the `NODE_DEBUG=execa` environment variable in the current process.834 835 #### buffer112 unmodified lines948 - [`result.stdout`](#stdout), [`result.stderr`](#stderr), [`result.all`](#all-1) and [`result.stdio`](#stdio) are arrays of lines.949 - [`subprocess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout), [`subprocess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr), [`subprocess.all`](#all), [`subprocess.stdio`](https://nodejs.org/api/child_process.html#subprocessstdio), [`subprocess.readable()`](#readablereadableoptions) and [`subprocess.duplex`](#duplexduplexoptions) iterate over lines instead of arbitrary chunks.950 - Any stream passed to the [`stdout`](#stdout-1), [`stderr`](#stderr-1) or [`stdio`](#stdio-1) option receives lines instead of arbitrary chunks.951 952-This cannot be used if the [`encoding` option](#encoding) is `'buffer'`.952+This cannot be used if the [`encoding` option](#encoding) is binary.953 954 #### encoding955 956 Type: `string`\957-Default: `utf8`957+Default: `'utf8'`958 959-Specify the character encoding used to decode the [`stdout`](#stdout), [`stderr`](#stderr) and [`stdio`](#stdio) output. If set to `'buffer'`, then `stdout`, `stderr` and `stdio` will be `Uint8Array`s instead of strings.959+If the subprocess outputs text, specifies its character encoding, either `'utf8'` or `'utf16le'`.960 961+If it outputs binary data instead, this should be either:962+- `'buffer'`: returns the binary output as an `Uint8Array`.963+- `'hex'`, `'base64'`, `'base64url'`, [`'latin1'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) or [`'ascii'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings): encodes the binary output as a string.964+965+The output is available with [`result.stdout`](#stdout), [`result.stderr`](#stderr) and [`result.stdio`](#stdio).966+967 #### stripFinalNewline968 969 Type: `boolean`\970 Default: `true`249 unmodified lines