Skip to content
Myr

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

5 files+100 added−36 removed

modified:lib/stream.js+1 added-1 removed
modified:test/stream.js+20 added-2 removed
9 unmodified lines
10import tempfile from 'tempfile';11import {execa, execaSync, $} from '../index.js';12import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';1314const pExec = promisify(exec);1516setFixtureDir();1718test('buffer', async t => {19 const {stdout} = await execa('noop.js', ['foo'], {encoding: null});
2 unmodified lines
22});2324const 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));2726 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};3132// This string gives different outputs with each encoding type33const STRING_TO_ENCODE = '\u1000.';34const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);3536test('can pass encoding "utf8"', checkEncoding, 'utf8');37test('can pass encoding "utf-8"', checkEncoding, 'utf8');38test('can pass encoding "utf16le"', checkEncoding, 'utf16le');
6 unmodified lines
45test('can pass encoding "hex"', checkEncoding, 'hex');46test('can pass encoding "base64"', checkEncoding, 'base64');47test('can pass encoding "base64url"', checkEncoding, 'base64url');4849const 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));5253 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};5657test('can pass encoding "buffer"', checkBufferEncoding, 'buffer');58test('can pass encoding null', checkBufferEncoding, null);5960test('validate unknown encodings', async t => {61 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});62});6364test('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 lines
modified:index.d.ts+34 added-14 removed
10 unmodified lines
11 | Stream12 | number13 | undefined;1415export type CommonOptions<EncodingType> = {15type 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;32type DefaultEncodingOption = 'utf8';33type BufferEncodingOption = 'buffer' | null;3435export 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 exit
155 unmodified lines
195 */196 readonly shell?: boolean | string;197198 /**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.200201 @default 'utf8'202 */203 readonly encoding?: EncodingType;
68 unmodified lines
272 */273 readonly verbose?: boolean;274};275256export type Options<EncodingType = string> = {276export type Options<EncodingType extends EncodingOption = DefaultEncodingOption> = {277 /**278 Write some input to the `stdin` of your binary.279280 If the input is a file, use the `inputFile` option instead.
7 unmodified lines
288 */289 readonly inputFile?: string;290} & CommonOptions<EncodingType>;291272export type SyncOptions<EncodingType = string> = {292export type SyncOptions<EncodingType extends EncodingOption = DefaultEncodingOption> = {293 /**294 Write some input to the `stdin` of your binary.295296 If the input is a file, use the `inputFile` option instead.
7 unmodified lines
304 */305 readonly inputFile?: string;306} & CommonOptions<EncodingType>;307288export type NodeOptions<EncodingType = string> = {308export type NodeOptions<EncodingType extends EncodingOption = DefaultEncodingOption> = {309 /**310 The Node.js executable to use.311312 @default process.execPath
331 unmodified lines
644): ExecaChildProcess;645export function execa(646 file: string,647 arguments?: readonly string[],628 options?: Options<null>648 options?: Options<BufferEncodingOption>649): ExecaChildProcess<Buffer>;650export function execa(file: string, options?: Options): ExecaChildProcess;631export function execa(file: string, options?: Options<null>): ExecaChildProcess<Buffer>;651export function execa(file: string, options?: Options<BufferEncodingOption>): ExecaChildProcess<Buffer>;652653/**654Same as `execa()` but synchronous.655
61 unmodified lines
717): ExecaSyncReturnValue;718export function execaSync(719 file: string,720 arguments?: readonly string[],701 options?: SyncOptions<null>721 options?: SyncOptions<BufferEncodingOption>722): ExecaSyncReturnValue<Buffer>;723export function execaSync(file: string, options?: SyncOptions): ExecaSyncReturnValue;724export function execaSync(725 file: string,706 options?: SyncOptions<null>726 options?: SyncOptions<BufferEncodingOption>727): ExecaSyncReturnValue<Buffer>;728729/**730Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a `childProcess`.
17 unmodified lines
748//=> 'unicorns'749```750*/751export function execaCommand(command: string, options?: Options): ExecaChildProcess;732export function execaCommand(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;752export function execaCommand(command: string, options?: Options<BufferEncodingOption>): ExecaChildProcess<Buffer>;753754/**755Same as `execaCommand()` but synchronous.756
10 unmodified lines
767//=> 'unicorns'768```769*/770export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;751export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;771export function execaCommandSync(command: string, options?: SyncOptions<BufferEncodingOption>): ExecaSyncReturnValue<Buffer>;772773type TemplateExpression =774 | string775 | number
26 unmodified lines
802 ```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 lines
948): ExecaChildProcess;949export function execaNode(950 scriptPath: string,951 arguments?: readonly string[],932 options?: NodeOptions<null>952 options?: NodeOptions<BufferEncodingOption>953): ExecaChildProcess<Buffer>;954export function execaNode(scriptPath: string, options?: NodeOptions): ExecaChildProcess;935export function execaNode(scriptPath: string, options?: NodeOptions<null>): ExecaChildProcess<Buffer>;955export function execaNode(scriptPath: string, options?: NodeOptions<BufferEncodingOption>): ExecaChildProcess<Buffer>;
modified:index.test-d.ts+44 added-18 removed
23 unmodified lines
24 const execaPromise = execa('unicorns');25 execaPromise.cancel();26 expectType<ReadableStream | undefined>(execaPromise.all);2728 const execaBufferPromise = execa('unicorns', {encoding: null});28 const execaBufferPromise = execa('unicorns', {encoding: 'buffer'});29 const writeStream = createWriteStream('output.txt');3031 expectAssignable<Function | undefined>(execaPromise.pipeStdout);32 expectType<ExecaChildProcess>(execaPromise.pipeStdout!('file.txt'));
99 unmodified lines
132execa('unicorns', {cleanup: false});133execa('unicorns', {preferLocal: false});134execa('unicorns', {localDir: '.'});135execa('unicorns', {localDir: new URL('file:///test')});136expectError(execa('unicorns', {encoding: 'unknownEncoding'}));137execa('unicorns', {execPath: '/path'});138execa('unicorns', {buffer: false});139execa('unicorns', {input: ''});140execa('unicorns', {input: Buffer.from('')});
66 unmodified lines
207expectType<ExecaReturnValue>(await execa('unicorns'));208expectType<ExecaReturnValue>(209 await execa('unicorns', {encoding: 'utf8'}),210);211expectType<ExecaReturnValue<Buffer>>(await execa('unicorns', {encoding: 'buffer'}));212expectType<ExecaReturnValue<Buffer>>(await execa('unicorns', {encoding: null}));213expectType<ExecaReturnValue>(214 await execa('unicorns', ['foo'], {encoding: 'utf8'}),215);216expectType<ExecaReturnValue<Buffer>>(217 await execa('unicorns', ['foo'], {encoding: 'buffer'}),218);219expectType<ExecaReturnValue<Buffer>>(220 await execa('unicorns', ['foo'], {encoding: null}),221);222223expectType<ExecaSyncReturnValue>(execaSync('unicorns'));224expectType<ExecaSyncReturnValue>(225 execaSync('unicorns', {encoding: 'utf8'}),226);227expectType<ExecaSyncReturnValue<Buffer>>(228 execaSync('unicorns', {encoding: 'buffer'}),229);230expectType<ExecaSyncReturnValue<Buffer>>(231 execaSync('unicorns', {encoding: null}),232);233expectType<ExecaSyncReturnValue>(234 execaSync('unicorns', ['foo'], {encoding: 'utf8'}),235);236expectType<ExecaSyncReturnValue<Buffer>>(237 execaSync('unicorns', ['foo'], {encoding: 'buffer'}),238);239expectType<ExecaSyncReturnValue<Buffer>>(240 execaSync('unicorns', ['foo'], {encoding: null}),241);242243expectType<ExecaChildProcess>(execaCommand('unicorns'));244expectType<ExecaReturnValue>(await execaCommand('unicorns'));245expectType<ExecaReturnValue>(await execaCommand('unicorns', {encoding: 'utf8'}));246expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns', {encoding: 'buffer'}));247expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns', {encoding: null}));248expectType<ExecaReturnValue>(await execaCommand('unicorns foo', {encoding: 'utf8'}));249expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns foo', {encoding: 'buffer'}));250expectType<ExecaReturnValue<Buffer>>(await execaCommand('unicorns foo', {encoding: null}));251252expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns'));253expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns', {encoding: 'utf8'}));254expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns', {encoding: 'buffer'}));255expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns', {encoding: null}));256expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns foo', {encoding: 'utf8'}));257expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns foo', {encoding: 'buffer'}));258expectType<ExecaSyncReturnValue<Buffer>>(execaCommandSync('unicorns foo', {encoding: null}));259260expectType<ExecaChildProcess>(execaNode('unicorns'));261expectType<ExecaReturnValue>(await execaNode('unicorns'));262expectType<ExecaReturnValue>(263 await execaNode('unicorns', {encoding: 'utf8'}),264);265expectType<ExecaReturnValue<Buffer>>(await execaNode('unicorns', {encoding: 'buffer'}));266expectType<ExecaReturnValue<Buffer>>(await execaNode('unicorns', {encoding: null}));267expectType<ExecaReturnValue>(268 await execaNode('unicorns', ['foo'], {encoding: 'utf8'}),269);270expectType<ExecaReturnValue<Buffer>>(271 await execaNode('unicorns', ['foo'], {encoding: 'buffer'}),272);273expectType<ExecaReturnValue<Buffer>>(274 await execaNode('unicorns', ['foo'], {encoding: null}),275);276277expectType<ExecaChildProcess>(execaNode('unicorns', {nodeOptions: ['--async-stack-traces']}));278expectType<ExecaChildProcess>(execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces']}));279expectType<ExecaChildProcess<Buffer>>(280 execaNode('unicorns', {nodeOptions: ['--async-stack-traces'], encoding: 'buffer'}),281);282expectType<ExecaChildProcess<Buffer>>(283 execaNode('unicorns', {nodeOptions: ['--async-stack-traces'], encoding: null}),284);285expectType<ExecaChildProcess<Buffer>>(286 execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces'], encoding: 'buffer'}),287);288expectType<ExecaChildProcess<Buffer>>(289 execaNode('unicorns', ['foo'], {nodeOptions: ['--async-stack-traces'], encoding: null}),290);291292expectType<ExecaChildProcess>($`unicorns`);
8 unmodified lines
301expectType<ExecaReturnValue>(await $({encoding: 'utf8'})`unicorns foo`);302expectType<ExecaSyncReturnValue>($({encoding: 'utf8'}).sync`unicorns foo`);303304expectType<ExecaChildProcess<Buffer>>($({encoding: null})`unicorns`);305expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})`unicorns`);280expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})`unicorns`);281expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null}).sync`unicorns`);306expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})`unicorns`);307expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'}).sync`unicorns`);308283expectType<ExecaChildProcess<Buffer>>($({encoding: null})`unicorns foo`);284expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})`unicorns foo`);285expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null}).sync`unicorns foo`);309expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})`unicorns foo`);310expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})`unicorns foo`);311expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'}).sync`unicorns foo`);312287expectType<ExecaChildProcess>($({encoding: null})({encoding: 'utf8'})`unicorns`);288expectType<ExecaReturnValue>(await $({encoding: null})({encoding: 'utf8'})`unicorns`);289expectType<ExecaSyncReturnValue>($({encoding: null})({encoding: 'utf8'}).sync`unicorns`);313expectType<ExecaChildProcess>($({encoding: 'buffer'})({encoding: 'utf8'})`unicorns`);314expectType<ExecaReturnValue>(await $({encoding: 'buffer'})({encoding: 'utf8'})`unicorns`);315expectType<ExecaSyncReturnValue>($({encoding: 'buffer'})({encoding: 'utf8'}).sync`unicorns`);316291expectType<ExecaChildProcess>($({encoding: null})({encoding: 'utf8'})`unicorns foo`);292expectType<ExecaReturnValue>(await $({encoding: null})({encoding: 'utf8'})`unicorns foo`);293expectType<ExecaSyncReturnValue>($({encoding: null})({encoding: 'utf8'}).sync`unicorns foo`);317expectType<ExecaChildProcess>($({encoding: 'buffer'})({encoding: 'utf8'})`unicorns foo`);318expectType<ExecaReturnValue>(await $({encoding: 'buffer'})({encoding: 'utf8'})`unicorns foo`);319expectType<ExecaSyncReturnValue>($({encoding: 'buffer'})({encoding: 'utf8'}).sync`unicorns foo`);320295expectType<ExecaChildProcess<Buffer>>($({encoding: null})({})`unicorns`);296expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})({})`unicorns`);297expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null})({}).sync`unicorns`);321expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})({})`unicorns`);322expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})({})`unicorns`);323expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'})({}).sync`unicorns`);324299expectType<ExecaChildProcess<Buffer>>($({encoding: null})({})`unicorns foo`);300expectType<ExecaReturnValue<Buffer>>(await $({encoding: null})({})`unicorns foo`);301expectType<ExecaSyncReturnValue<Buffer>>($({encoding: null})({}).sync`unicorns foo`);325expectType<ExecaChildProcess<Buffer>>($({encoding: 'buffer'})({})`unicorns foo`);326expectType<ExecaReturnValue<Buffer>>(await $({encoding: 'buffer'})({})`unicorns foo`);327expectType<ExecaSyncReturnValue<Buffer>>($({encoding: 'buffer'})({}).sync`unicorns foo`);328329expectType<ExecaReturnValue>(await $`unicorns ${'foo'}`);330expectType<ExecaSyncReturnValue>($.sync`unicorns ${'foo'}`);331expectType<ExecaReturnValue>(await $`unicorns ${1}`);
15 unmodified lines
modified:readme.md+1 added-1 removed
680 unmodified lines
681682Type: `string | null`\683Default: `utf8`684685Specify 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.685Specify 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.686687#### timeout688689Type: `number`\
133 unmodified lines