Support `{encoding: 'buffer'}` (#572)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent e7dee28 · Browse files · 5 files·+100 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+100 added−36 removed
- tests:+20 added−2 removed
- other:+80 added−34 removed
Provenance
- Commit
89c69fe- Tree
f818d31- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
e7dee28
5 files changed+100 added−36 removed
modified:lib/stream.js+1 added-1 removed
100 unmodified lines10110210310410510510610710810924 unmodified lines100 unmodified lines if (encoding === 'utf8' || encoding === 'utf-8') { return getStream(stream, {maxBuffer}); }
if (encoding === null) { if (encoding === null || encoding === 'buffer') { return getStreamAsBuffer(stream, {maxBuffer}); }
return applyEncoding(stream, maxBuffer, encoding);24 unmodified linesmodified:test/stream.js+20 added-2 removed
9 unmodified lines10 import tempfile from 'tempfile';11 import {execa, execaSync, $} from '../index.js';12 import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';13 14+const pExec = promisify(exec);15+16 setFixtureDir();17 18 test('buffer', async t => {19 const {stdout} = await execa('noop.js', ['foo'], {encoding: null});2 unmodified lines22 });23 24 const checkEncoding = async (t, encoding) => {25 const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});24- t.is(stdout, Buffer.from(STRING_TO_ENCODE).toString(encoding));26+ t.is(stdout, BUFFER_TO_ENCODE.toString(encoding));27 26- const {stdout: nativeStdout} = await promisify(exec)(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});28+ const {stdout: nativeStdout} = await pExec(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});29 t.is(stdout, nativeStdout);30 };31 32 // This string gives different outputs with each encoding type33 const STRING_TO_ENCODE = '\u1000.';34+const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);35 36 test('can pass encoding "utf8"', checkEncoding, 'utf8');37 test('can pass encoding "utf-8"', checkEncoding, 'utf8');38 test('can pass encoding "utf16le"', checkEncoding, 'utf16le');6 unmodified lines45 test('can pass encoding "hex"', checkEncoding, 'hex');46 test('can pass encoding "base64"', checkEncoding, 'base64');47 test('can pass encoding "base64url"', checkEncoding, 'base64url');48 49+const checkBufferEncoding = async (t, encoding) => {50+ const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});51+ t.true(BUFFER_TO_ENCODE.equals(stdout));52+53+ const {stdout: nativeStdout} = await pExec(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});54+ t.true(BUFFER_TO_ENCODE.equals(nativeStdout));55+};56+57+test('can pass encoding "buffer"', checkBufferEncoding, 'buffer');58+test('can pass encoding null', checkBufferEncoding, null);59+60+test('validate unknown encodings', async t => {61+ await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});62+});63+64 test('pass `stdout` to a file descriptor', async t => {65 const file = tempfile({extension: '.txt'});66 await execa('noop.js', ['foo bar'], {stdout: fs.openSync(file, 'w')});67 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');233 unmodified linesmodified:index.d.ts+34 added-14 removed
10 unmodified lines11 | Stream12 | number13 | undefined;14 15-export type CommonOptions<EncodingType> = {15+type EncodingOption =16+ | 'utf8'17+ // eslint-disable-next-line unicorn/text-encoding-identifier-case18+ | 'utf-8'19+ | 'utf16le'20+ | 'utf-16le'21+ | 'ucs2'22+ | 'ucs-2'23+ | 'latin1'24+ | 'binary'25+ | 'ascii'26+ | 'hex'27+ | 'base64'28+ | 'base64url'29+ | 'buffer'30+ | null31+ | undefined;32+type DefaultEncodingOption = 'utf8';33+type BufferEncodingOption = 'buffer' | null;34+35+export type CommonOptions<EncodingType extends EncodingOption = DefaultEncodingOption> = {36 /**37 Kill the spawned process when the parent process exits unless either:38 - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)39 - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit155 unmodified lines195 */196 readonly shell?: boolean | string;197 198 /**179- Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.199+ Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `'buffer'` or `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.200 201 @default 'utf8'202 */203 readonly encoding?: EncodingType;68 unmodified lines272 */273 readonly verbose?: boolean;274 };275 256-export type Options<EncodingType = string> = {276+export type Options<EncodingType extends EncodingOption = DefaultEncodingOption> = {277 /**278 Write some input to the `stdin` of your binary.279 280 If the input is a file, use the `inputFile` option instead.7 unmodified lines288 */289 readonly inputFile?: string;290 } & CommonOptions<EncodingType>;291 272-export type SyncOptions<EncodingType = string> = {292+export type SyncOptions<EncodingType extends EncodingOption = DefaultEncodingOption> = {293 /**294 Write some input to the `stdin` of your binary.295 296 If the input is a file, use the `inputFile` option instead.7 unmodified lines304 */305 readonly inputFile?: string;306 } & CommonOptions<EncodingType>;307 288-export type NodeOptions<EncodingType = string> = {308+export type NodeOptions<EncodingType extends EncodingOption = DefaultEncodingOption> = {309 /**310 The Node.js executable to use.311 312 @default process.execPath331 unmodified lines644 ): ExecaChildProcess;645 export function execa(646 file: string,647 arguments?: readonly string[],628- options?: Options<null>648+ options?: Options<BufferEncodingOption>649 ): ExecaChildProcess<Buffer>;650 export function execa(file: string, options?: Options): ExecaChildProcess;631-export function execa(file: string, options?: Options<null>): ExecaChildProcess<Buffer>;651+export function execa(file: string, options?: Options<BufferEncodingOption>): ExecaChildProcess<Buffer>;652 653 /**654 Same as `execa()` but synchronous.655 61 unmodified lines717 ): ExecaSyncReturnValue;718 export function execaSync(719 file: string,720 arguments?: readonly string[],701- options?: SyncOptions<null>721+ options?: SyncOptions<BufferEncodingOption>722 ): ExecaSyncReturnValue<Buffer>;723 export function execaSync(file: string, options?: SyncOptions): ExecaSyncReturnValue;724 export function execaSync(725 file: string,706- options?: SyncOptions<null>726+ options?: SyncOptions<BufferEncodingOption>727 ): ExecaSyncReturnValue<Buffer>;728 729 /**730 Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a `childProcess`.17 unmodified lines748 //=> 'unicorns'749 ```750 */751 export function execaCommand(command: string, options?: Options): ExecaChildProcess;732-export function execaCommand(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;752+export function execaCommand(command: string, options?: Options<BufferEncodingOption>): ExecaChildProcess<Buffer>;753 754 /**755 Same as `execaCommand()` but synchronous.756 10 unmodified lines767 //=> 'unicorns'768 ```769 */770 export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;751-export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;771+export function execaCommandSync(command: string, options?: SyncOptions<BufferEncodingOption>): ExecaSyncReturnValue<Buffer>;772 773 type TemplateExpression =774 | string775 | number26 unmodified lines802 ```803 */804 (options: Options<undefined>): Execa$<StdoutStderrType>;805 (options: Options): Execa$;786- (options: Options<null>): Execa$<Buffer>;806+ (options: Options<BufferEncodingOption>): Execa$<Buffer>;807 (808 templates: TemplateStringsArray,809 ...expressions: TemplateExpression[]810 ): ExecaChildProcess<StdoutStderrType>;137 unmodified lines948 ): ExecaChildProcess;949 export function execaNode(950 scriptPath: string,951 arguments?: readonly string[],932- options?: NodeOptions<null>952+ options?: NodeOptions<BufferEncodingOption>953 ): ExecaChildProcess<Buffer>;954 export function execaNode(scriptPath: string, options?: NodeOptions): ExecaChildProcess;935-export function execaNode(scriptPath: string, options?: NodeOptions<null>): ExecaChildProcess<Buffer>;955+export function execaNode(scriptPath: string, options?: NodeOptions<BufferEncodingOption>): ExecaChildProcess<Buffer>;modified:index.test-d.ts+44 added-18 removed
23 unmodified lines24 const execaPromise = execa('unicorns');25 execaPromise.cancel();26 expectType<ReadableStream | undefined>(execaPromise.all);27 28- const execaBufferPromise = execa('unicorns', {encoding: null});28+ const execaBufferPromise = execa('unicorns', {encoding: 'buffer'});29 const writeStream = createWriteStream('output.txt');30 31 expectAssignable<Function | undefined>(execaPromise.pipeStdout);32 expectType<ExecaChildProcess>(execaPromise.pipeStdout!('file.txt'));99 unmodified lines132 execa('unicorns', {cleanup: false});133 execa('unicorns', {preferLocal: false});134 execa('unicorns', {localDir: '.'});135 execa('unicorns', {localDir: new URL('file:///test')});136+expectError(execa('unicorns', {encoding: 'unknownEncoding'}));137 execa('unicorns', {execPath: '/path'});138 execa('unicorns', {buffer: false});139 execa('unicorns', {input: ''});140 execa('unicorns', {input: Buffer.from('')});66 unmodified lines207 expectType<ExecaReturnValue>(await execa('unicorns'));208 expectType<ExecaReturnValue>(209 await execa('unicorns', {encoding: 'utf8'}),210 );211+expectType<ExecaReturnValue<Buffer>>(await execa('unicorns', {encoding: 'buffer'}));212 expectType<ExecaReturnValue<Buffer>>(await execa('unicorns', {encoding: null}));213 expectType<ExecaReturnValue>(214 await execa('unicorns', ['foo'], {encoding: 'utf8'}),215 );216 expectType<ExecaReturnValue<Buffer>>(217+ await execa('unicorns', ['foo'], {encoding: 'buffer'}),218+);219+expectType<ExecaReturnValue<Buffer>>(220 await execa('unicorns', ['foo'], {encoding: null}),221 );222 223 expectType<ExecaSyncReturnValue>(execaSync('unicorns'));224 expectType<ExecaSyncReturnValue>(225 execaSync('unicorns', {encoding: 'utf8'}),226 );227 expectType<ExecaSyncReturnValue<Buffer>>(228+ execaSync('unicorns', {encoding: 'buffer'}),229+);230+expectType<ExecaSyncReturnValue<Buffer>>(231 execaSync('unicorns', {encoding: null}),232 );233 expectType<ExecaSyncReturnValue>(234 execaSync('unicorns', ['foo'], {encoding: 'utf8'}),235 );236 expectType<ExecaSyncReturnValue<Buffer>>(237+ execaSync('unicorns', ['foo'], {encoding: 'buffer'}),238+);239+expectType<ExecaSyncReturnValue<Buffer>>(240 execaSync('unicorns', ['foo'], {encoding: null}),241 );242 243 expectType<ExecaChildProcess>(execaCommand('unicorns'));244 expectType<ExecaReturnValue>(await execaCommand('unicorns'));245 expectType<ExecaReturnValue>(await execaCommand('unicorns', {encoding: 'utf8'}));246+expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns', {encoding: 'buffer'}));247 expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns', {encoding: null}));248 expectType<ExecaReturnValue>(await execaCommand('unicorns foo', {encoding: 'utf8'}));249+expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns foo', {encoding: 'buffer'}));250 expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns foo', {encoding: null}));251 252 expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns'));253 expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns', {encoding: 'utf8'}));254+expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns', {encoding: 'buffer'}));255 expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns', {encoding: null}));256 expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns foo', {encoding: 'utf8'}));257+expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns foo', {encoding: 'buffer'}));258 expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns foo', {encoding: null}));259 260 expectType<ExecaChildProcess>(execaNode('unicorns'));261 expectType<ExecaReturnValue>(await execaNode('unicorns'));262 expectType<ExecaReturnValue>(263 await execaNode('unicorns', {encoding: 'utf8'}),264 );265+expectType<ExecaReturnValue<Buffer>>(await execaNode('unicorns', {encoding: 'buffer'}));266 expectType<ExecaReturnValue<Buffer>>(await execaNode('unicorns', {encoding: null}));267 expectType<ExecaReturnValue>(268 await execaNode('unicorns', ['foo'], {encoding: 'utf8'}),269 );270 expectType<ExecaReturnValue<Buffer>>(271+ await execaNode('unicorns', ['foo'], {encoding: 'buffer'}),272+);273+expectType<ExecaReturnValue<Buffer>>(274 await execaNode('unicorns', ['foo'], {encoding: null}),275 );276 277 expectType<ExecaChildProcess>(execaNode('unicorns', {nodeOptions: ['--async-stack-traces']}));278 expectType<ExecaChildProcess>(execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces']}));279 expectType<ExecaChildProcess<Buffer>>(280+ execaNode('unicorns', {nodeOptions: ['--async-stack-traces'], encoding: 'buffer'}),281+);282+expectType<ExecaChildProcess<Buffer>>(283 execaNode('unicorns', {nodeOptions: ['--async-stack-traces'], encoding: null}),284 );285 expectType<ExecaChildProcess<Buffer>>(286+ execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces'], encoding: 'buffer'}),287+);288+expectType<ExecaChildProcess<Buffer>>(289 execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces'], encoding: null}),290 );291 292 expectType<ExecaChildProcess>($`unicorns`);8 unmodified lines301 expectType<ExecaReturnValue>(await $({encoding: 'utf8'})`unicorns foo`);302 expectType<ExecaSyncReturnValue>($({encoding: 'utf8'}).sync`unicorns foo`);303 304 expectType<ExecaChildProcess<Buffer>>($({encoding: null})`unicorns`);305+expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})`unicorns`);280-expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})`unicorns`);281-expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null}).sync`unicorns`);306+expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})`unicorns`);307+expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'}).sync`unicorns`);308 283-expectType<ExecaChildProcess<Buffer>>($({encoding: null})`unicorns foo`);284-expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})`unicorns foo`);285-expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null}).sync`unicorns foo`);309+expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})`unicorns foo`);310+expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})`unicorns foo`);311+expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'}).sync`unicorns foo`);312 287-expectType<ExecaChildProcess>($({encoding: null})({encoding: 'utf8'})`unicorns`);288-expectType<ExecaReturnValue>(await $({encoding: null})({encoding: 'utf8'})`unicorns`);289-expectType<ExecaSyncReturnValue>($({encoding: null})({encoding: 'utf8'}).sync`unicorns`);313+expectType<ExecaChildProcess>($({encoding: 'buffer'})({encoding: 'utf8'})`unicorns`);314+expectType<ExecaReturnValue>(await $({encoding: 'buffer'})({encoding: 'utf8'})`unicorns`);315+expectType<ExecaSyncReturnValue>($({encoding: 'buffer'})({encoding: 'utf8'}).sync`unicorns`);316 291-expectType<ExecaChildProcess>($({encoding: null})({encoding: 'utf8'})`unicorns foo`);292-expectType<ExecaReturnValue>(await $({encoding: null})({encoding: 'utf8'})`unicorns foo`);293-expectType<ExecaSyncReturnValue>($({encoding: null})({encoding: 'utf8'}).sync`unicorns foo`);317+expectType<ExecaChildProcess>($({encoding: 'buffer'})({encoding: 'utf8'})`unicorns foo`);318+expectType<ExecaReturnValue>(await $({encoding: 'buffer'})({encoding: 'utf8'})`unicorns foo`);319+expectType<ExecaSyncReturnValue>($({encoding: 'buffer'})({encoding: 'utf8'}).sync`unicorns foo`);320 295-expectType<ExecaChildProcess<Buffer>>($({encoding: null})({})`unicorns`);296-expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})({})`unicorns`);297-expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null})({}).sync`unicorns`);321+expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})({})`unicorns`);322+expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})({})`unicorns`);323+expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'})({}).sync`unicorns`);324 299-expectType<ExecaChildProcess<Buffer>>($({encoding: null})({})`unicorns foo`);300-expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})({})`unicorns foo`);301-expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null})({}).sync`unicorns foo`);325+expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})({})`unicorns foo`);326+expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})({})`unicorns foo`);327+expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'})({}).sync`unicorns foo`);328 329 expectType<ExecaReturnValue>(await $`unicorns ${'foo'}`);330 expectType<ExecaSyncReturnValue>($.sync`unicorns ${'foo'}`);331 expectType<ExecaReturnValue>(await $`unicorns ${1}`);15 unmodified linesmodified:readme.md+1 added-1 removed
680 unmodified lines681 682 Type: `string | null`\683 Default: `utf8`684 685-Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.685+Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `'buffer'` or `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.686 687 #### timeout688 689 Type: `number`\133 unmodified lines