Fix `execaNode()` and the `node` option missing IPC in their types
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`execaNode()` and the `node` option enable the `ipc` option by default, but their types did not: `subprocess.sendMessage()` was `undefined`, `result.ipcOutput` was `[]`, and `result.stdio` and `subprocess.stdio` missed the IPC channel's file descriptor. Also, `ipc: undefined` disabled IPC with the `node` option, instead of using the default value.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent b2c3a54 · Browse files · 13 files·+99 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
13 files+99 added−36 removed
- tests:+4 added−0 removed
- other:+95 added−36 removed
Provenance
- Commit
2633abb- Tree
6d3b285- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
b2c3a54
13 files changed+99 added−36 removed
modified:lib/methods/node.js+2 added-1 removed
80 unmodified lines81828384858586878889909180 unmodified lines ...commandArguments, ], { __proto__: null, ipc: true, ...newOptions, // Like other options, `undefined` uses the default value ipc: newOptions.ipc === undefined ? true : newOptions.ipc, shell: false, }, ];};modified:test/methods/node.js+3 added
311 unmodified lines312 };313 314 test('execaNode() adds an ipc channel', () => testIpc(execaNode, {}));315 test('The "node" option adds an ipc channel', () => testIpc(runWithNodeOption, {}));316+// Like other options, `undefined` uses the default value317+test('execaNode() adds an ipc channel, with "ipc: undefined"', () => testIpc(execaNode, {ipc: undefined}));318+test('The "node" option adds an ipc channel, with "ipc: undefined"', () => testIpc(runWithNodeOption, {ipc: undefined}));319 test('The "ipc" option adds an ipc channel', () => testIpc(runWithIpc, {}));320 test('The "ipc" option works with "stdio: \'pipe\'"', () => testIpc(runWithIpc, {stdio: 'pipe'}));321 test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', () => testIpc(runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']}));322 test('The "ipc" option works with "stdout: \'pipe\'"', () => testIpc(runWithIpc, {stdout: 'pipe'}));46 unmodified linesmodified:test/methods/parameters-options.js+1 added
125 unmodified lines126 test('The "all" option must use a boolean', () => testInvalidBooleanOption(execa, 'all', 'false'));127 test('The "reject" option must use a boolean', () => testInvalidBooleanOption(execa, 'reject', 'no'));128 test('The "cleanup" option must use a boolean', () => testInvalidBooleanOption(execa, 'cleanup', 'false'));129 test('The "ipc" option must use a boolean', () => testInvalidBooleanOption(execa, 'ipc', 'yes'));130+test('The "ipc" option must use a boolean - execaNode()', () => testInvalidBooleanOption(execaNode, 'ipc', null));131 test('The "extendEnv" option must use a boolean', () => testInvalidBooleanOption(execa, 'extendEnv', 'false'));132 test('The "preferLocal" option must use a boolean', () => testInvalidBooleanOption(execa, 'preferLocal', 1));133 test('The "node" option must use a boolean', () => testInvalidBooleanOption(execa, 'node', 'yes'));134 test('The "gracefulCancel" option must use a boolean', () => testInvalidBooleanOption(execa, 'gracefulCancel', 'yes'));25 unmodified linesmodified:test-d/ipc/send.test-d.ts+7 added
12345678912 unmodified lines22232425262728293031325 unmodified lines383940414243444546474811 unmodified linesimport {expectTypeOf, expectError} from 'nz';import { sendMessage, execa, execaNode, type Message, type Options,} from '../../index.js';
12 unmodified lines
await execa('test', {ipcInput: ''}).sendMessage('');await execa('test', {ipcInput: '' as Message}).sendMessage('');await execa('test', {gracefulCancel: true, cancelSignal: AbortSignal.abort()}).sendMessage('');await execa('test', {node: true}).sendMessage('');await execaNode('test').sendMessage('');await execaNode({})`test`.sendMessage('');await execa('test', {} as Options).sendMessage?.('');await execa('test', {ipc: true as boolean}).sendMessage?.('');await execa('test', {ipcInput: '' as '' | undefined}).sendMessage?.('');await execa('test', {gracefulCancel: true as boolean | undefined, cancelSignal: AbortSignal.abort()}).sendMessage?.('');5 unmodified linesexpectTypeOf(execa('test', {gracefulCancel: undefined}).sendMessage).toBe<undefined>();expectTypeOf(execa('test', {gracefulCancel: false}).sendMessage).toBe<undefined>();expectTypeOf(execa('test', {ipc: false, ipcInput: ''}).sendMessage).toBe<undefined>();expectTypeOf(execa('test', {ipc: false, gracefulCancel: true, cancelSignal: AbortSignal.abort()}).sendMessage).toBe<undefined>();expectTypeOf(execa('test', {node: false}).sendMessage).toBe<undefined>();expectTypeOf(execa('test', {ipc: false, node: true}).sendMessage).toBe<undefined>();expectTypeOf(execaNode('test', {ipc: false}).sendMessage).toBe<undefined>();
await subprocess.sendMessage('', {} as const);await sendMessage('', {} as const);await subprocess.sendMessage('', {strict: true} as const);11 unmodified linesmodified:test-d/methods/command.test-d.ts+1 added-1 removed
18 unmodified lines1920212223232425262718 unmodified linesexpectTypeOf(await execa`${parseCommandString('unicorns foo bar')}`).toBe<Result<{}>>();expectTypeOf(execaSync`${parseCommandString('unicorns foo bar')}`).toBe<SyncResult<{}>>();expectTypeOf(await $`${parseCommandString('unicorns foo bar')}`).toBe<Result<{}>>();expectTypeOf($.sync`${parseCommandString('unicorns foo bar')}`).toBe<SyncResult<{}>>();expectTypeOf(await execaNode`${parseCommandString('foo bar')}`).toBe<Result<{}>>();expectTypeOf(await execaNode`${parseCommandString('foo bar')}`).toBe<Result<{readonly node: true}>>();
expectTypeOf(await execa`unicorns ${parseCommandString('foo bar')}`).toBe<Result<{}>>();expectTypeOf(await execa('unicorns', parseCommandString('foo bar'))).toBe<Result<{}>>();expectTypeOf(await execa('unicorns', ['foo', ...parseCommandString('bar')])).toBe<Result<{}>>();modified:test-d/methods/node.test-d.ts+12 added-12 removed
19 unmodified lines20 expectError(execaNode('unicorns', [], []));21 expectError(execaNode('unicorns', {other: true}));22 23 expectTypeOf(execaNode`unicorns`).toBeAssignableTo<ResultPromise>();24-expectTypeOf(await execaNode('unicorns')).toBe<Result<{}>>();25-expectTypeOf(await execaNode`unicorns`).toBe<Result<{}>>();24+expectTypeOf(await execaNode('unicorns')).toBe<Result<{readonly node: true}>>();25+expectTypeOf(await execaNode`unicorns`).toBe<Result<{readonly node: true}>>();26 27 expectTypeOf(execaNode({})).toBeAssignableTo<typeof execaNode>();28 expectTypeOf(execaNode({})('unicorns')).toBeAssignableTo<ResultPromise>();29 expectTypeOf(execaNode({})`unicorns`).toBeAssignableTo<ResultPromise>();10 unmodified lines40 expectTypeOf(await execaNode({encoding: 'buffer'})`unicorns`).toBeAssignableTo<{stdout: Uint8Array}>();41 expectTypeOf(await execaNode({})({encoding: 'buffer'})`unicorns`).toBeAssignableTo<{stdout: Uint8Array}>();42 expectTypeOf(await execaNode({encoding: 'buffer'})({})`unicorns`).toBeAssignableTo<{stdout: Uint8Array}>();43 44-expectTypeOf(await execaNode`${'unicorns'}`).toBe<Result<{}>>();45-expectTypeOf(await execaNode`unicorns ${'foo'}`).toBe<Result<{}>>();46-expectTypeOf(await execaNode`unicorns ${'foo'} ${'bar'}`).toBe<Result<{}>>();47-expectTypeOf(await execaNode`unicorns ${1}`).toBe<Result<{}>>();48-expectTypeOf(await execaNode`unicorns ${stringArray}`).toBe<Result<{}>>();49-expectTypeOf(await execaNode`unicorns ${[1, 2]}`).toBe<Result<{}>>();50-expectTypeOf(await execaNode`unicorns ${false.toString()}`).toBe<Result<{}>>();44+expectTypeOf(await execaNode`${'unicorns'}`).toBe<Result<{readonly node: true}>>();45+expectTypeOf(await execaNode`unicorns ${'foo'}`).toBe<Result<{readonly node: true}>>();46+expectTypeOf(await execaNode`unicorns ${'foo'} ${'bar'}`).toBe<Result<{readonly node: true}>>();47+expectTypeOf(await execaNode`unicorns ${1}`).toBe<Result<{readonly node: true}>>();48+expectTypeOf(await execaNode`unicorns ${stringArray}`).toBe<Result<{readonly node: true}>>();49+expectTypeOf(await execaNode`unicorns ${[1, 2]}`).toBe<Result<{readonly node: true}>>();50+expectTypeOf(await execaNode`unicorns ${false.toString()}`).toBe<Result<{readonly node: true}>>();51 expectError(await execaNode`unicorns ${false}`);52 53-expectTypeOf(await execaNode`unicorns ${await execaNode`echo foo`}`).toBe<Result<{}>>();54-expectTypeOf(await execaNode`unicorns ${await execaNode({reject: false})`echo foo`}`).toBe<Result<{}>>();53+expectTypeOf(await execaNode`unicorns ${await execaNode`echo foo`}`).toBe<Result<{readonly node: true}>>();54+expectTypeOf(await execaNode`unicorns ${await execaNode({reject: false})`echo foo`}`).toBe<Result<{readonly node: true}>>();55 expectError(await execaNode`unicorns ${execaNode`echo foo`}`);56-expectTypeOf(await execaNode`unicorns ${[await execaNode`echo foo`, 'bar']}`).toBe<Result<{}>>();56+expectTypeOf(await execaNode`unicorns ${[await execaNode`echo foo`, 'bar']}`).toBe<Result<{readonly node: true}>>();57 expectError(await execaNode`unicorns ${[execaNode`echo foo`, 'bar']}`);58 59 expectTypeOf(execaNode('unicorns', {nodePath: './node'})).toBeAssignableTo<ResultPromise>();60 expectTypeOf(execaNode('unicorns', {nodePath: fileUrl})).toBeAssignableTo<ResultPromise>();4 unmodified linesmodified:test-d/return/result-ipc.ts+16 added
1 import {expectTypeOf} from 'nz';2 import {3 execa,4 execaSync,5+ execaNode,6 type Result,7 type SyncResult,8 type ExecaError,9 type ExecaSyncError,21 unmodified lines31 32 const gracefulResult = await execa('unicorns', {gracefulCancel: true, cancelSignal: AbortSignal.abort()});33 expectTypeOf(gracefulResult.ipcOutput).toBe<Array<Message<'advanced'>>>();34 35+const nodeResult = await execa('unicorns', {node: true});36+expectTypeOf(nodeResult.ipcOutput).toBe<Array<Message<'advanced'>>>();37+38+const execaNodeResult = await execaNode('unicorns');39+expectTypeOf(execaNodeResult.ipcOutput).toBe<Array<Message<'advanced'>>>();40+41+const execaNodeJsonResult = await execaNode({serialization: 'json'})`unicorns`;42+expectTypeOf(execaNodeJsonResult.ipcOutput).toBe<Array<Message<'json'>>>();43+44 const genericResult = await execa('unicorns', {} as Options);45 expectTypeOf(genericResult.ipcOutput).toBe<Message[] | []>();46 47 const genericIpc = await execa('unicorns', {ipc: true as boolean});28 unmodified lines76 77 const gracefulNoIpcResult = await execa('unicorns', {ipc: false, gracefulCancel: true, cancelSignal: AbortSignal.abort()});78 expectTypeOf(gracefulNoIpcResult.ipcOutput).toBe<[]>();79 80+const falseNodeResult = await execa('unicorns', {node: false});81+expectTypeOf(falseNodeResult.ipcOutput).toBe<[]>();82+83+const execaNodeNoIpcResult = await execaNode('unicorns', {ipc: false});84+expectTypeOf(execaNodeNoIpcResult.ipcOutput).toBe<[]>();85+86 const noBufferResult = await execa('unicorns', {ipc: true, buffer: false});87 expectTypeOf(noBufferResult.ipcOutput).toBe<[]>();88 89 const noBufferFdResult = await execa('unicorns', {ipc: true, buffer: {ipc: false}});21 unmodified linesmodified:test-d/return/result-stdio.test-d.ts+13 added
1 unmodified line2 import {expectTypeOf} from 'nz';3 import {4 execa,5 execaSync,6+ execaNode,7 type ExecaError,8 type ExecaSyncError,9 } from '../../index.js';10 153 unmodified lines164 165 const gracefulCancelResult = await execa('unicorns', {cancelSignal: AbortSignal.abort(), gracefulCancel: true});166 expectTypeOf(gracefulCancelResult.stdio).toBe<[undefined, string, string, undefined]>();167 168+const nodeResult = await execa('unicorns', {node: true});169+expectTypeOf(nodeResult.stdio).toBe<[undefined, string, string, undefined]>();170+171+const execaNodeResult = await execaNode('unicorns');172+expectTypeOf(execaNodeResult.stdio).toBe<[undefined, string, string, undefined]>();173+174+const execaNodeNoIpcResult = await execaNode('unicorns', {ipc: false});175+expectTypeOf(execaNodeNoIpcResult.stdio).toBe<[undefined, string, string]>();176+177 const fd3IpcResult = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'pipe'], ipc: true});178 expectTypeOf(fd3IpcResult.stdio).toBe<[undefined, string, string, string, undefined]>();179 180 const noIpcResult = await execa('unicorns');181 expectTypeOf(noIpcResult.stdio).toBe<[undefined, string, string]>();182 183 const noIpcResultSync = execaSync('unicorns');184 expectTypeOf(noIpcResultSync.stdio).toBe<[undefined, string, string]>();185+186+const nodeResultSync = execaSync('unicorns', {node: true});187+expectTypeOf(nodeResultSync.stdio).toBe<[undefined, string, string]>();modified:test-d/subprocess/stdio.test-d.ts+4 added-1 removed
1 import type {Readable, Writable} from 'node:stream';2 import {expectTypeOf} from 'nz';3-import {execa, type Subprocess} from '../../index.js';3+import {execa, execaNode, type Subprocess} from '../../index.js';4 5 expectTypeOf<Subprocess['stdin']>().toBe<Writable | null>();6 expectTypeOf<Subprocess['stdout']>().toBe<Readable | null>();7 expectTypeOf<Subprocess['stderr']>().toBe<Readable | null>();80 unmodified lines88 89 const ipcInputSubprocess = execa('unicorns', {ipcInput: ''});90 expectTypeOf(ipcInputSubprocess.stdio[3]).toBe<null>();91 92+const execaNodeSubprocess = execaNode('unicorns');93+expectTypeOf(execaNodeSubprocess.stdio[3]).toBe<null>();94+95 const fd3IpcSubprocess = execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'pipe'], ipc: true});96 expectTypeOf(fd3IpcSubprocess.stdio[3]).toBe<Readable>();97 expectTypeOf(fd3IpcSubprocess.stdio[4]).toBe<null>();98 6 unmodified linesmodified:types/methods/node.d.ts+6 added-3 removed
47 unmodified lines48 49 // `execaNode`command`` template syntax50 type ExecaNodeTemplate<OptionsType extends Options> =51 (...templateString: TemplateString)52- => ResultPromise<OptionsType>;52+ => ResultPromise<NodeOptions<OptionsType>>;53 54 // `execaNode('script', ['argument'], {})` array syntax55 type ExecaNodeArrayLong<OptionsType extends Options> =56 <NewOptionsType extends Options = {}>(scriptPath: string | URL, arguments?: readonly string[], options?: NewOptionsType)57- => ResultPromise<OptionsType & NewOptionsType>;57+ => ResultPromise<NodeOptions<OptionsType & NewOptionsType>>;58 59 // `execaNode('script', {})` array syntax60 type ExecaNodeArrayShort<OptionsType extends Options> =61 <NewOptionsType extends Options = {}>(scriptPath: string | URL, options?: NewOptionsType)62- => ResultPromise<OptionsType & NewOptionsType>;62+ => ResultPromise<NodeOptions<OptionsType & NewOptionsType>>;63 64+// `execaNode()` always uses the `node` option, which also enables the `ipc` option by default65+type NodeOptions<OptionsType extends Options> = OptionsType & {readonly node: true};66+67 export {};modified:types/return/result-stdio.d.ts+23 added-11 removed
2 unmodified lines3 import type {CommonOptions, Options, StricterOptions} from '../arguments/options.js';4 import type {ResultStdioNotAll} from './result-stdout.js';5 6 // `result.stdio`7-// With the `ipc` option, the subprocess' IPC channel adds one last file descriptor, which has no output8-export type ResultStdioArray<OptionsType extends CommonOptions> = MapResultStdio<StdioOptionNormalizedArray<OptionsType>, OptionsType>;7+export type ResultStdioArray<8+ IsSync extends boolean,9+ OptionsType extends CommonOptions,10+> = [11+ ...MapResultStdio<StdioOptionNormalizedArray<OptionsType>, OptionsType>,12+ ...ResultIpcStdio<IsSync, OptionsType>,13+];14 15 type MapResultStdio<16 StdioOptionsArrayType extends readonly unknown[],17 OptionsType extends CommonOptions,13-> = [14- ...{15- -readonly [FdNumber in keyof StdioOptionsArrayType]: ResultStdioNotAll<16- FdNumber extends string ? FdNumber : string,17- OptionsType18- >19- },18+> = {19+ -readonly [FdNumber in keyof StdioOptionsArrayType]: ResultStdioNotAll<20+ FdNumber extends string ? FdNumber : string,21+ OptionsType22+ >23+};20- ...(HasIpc<StricterOptions<OptionsType, Options>> extends true ? [undefined] : []),21-];24 25+// With the `ipc` option, the subprocess' IPC channel adds one last file descriptor, which has no output. Synchronous methods do not support IPC.26+type ResultIpcStdio<27+ IsSync extends boolean,28+ OptionsType extends CommonOptions,29+> = IsSync extends true30+ ? []31+ : HasIpc<StricterOptions<OptionsType, Options>> extends true32+ ? [undefined]33+ : [];34+35 export {};modified:types/return/result.d.ts+1 added-1 removed
47 unmodified lines48 With the `ipc` option, the last item is the subprocess' IPC channel, which is always `undefined`.49 50 Items are arrays when their corresponding `stdio` option is a transform in object mode.51 */52- stdio: ResultStdioArray<OptionsType>;52+ stdio: ResultStdioArray<IsSync, OptionsType>;53 54 /**55 All the messages sent by the subprocess to the current process.56 151 unmodified linesmodified:types/ipc.d.ts+10 added-6 removed
132 unmodified lines133 getOneMessage: undefined;134 getEachMessage: undefined;135 };136 137-// Whether IPC is enabled, based on the `ipc`, `ipcInput` and `gracefulCancel` options137+// Whether IPC is enabled, based on the `ipc`, `node`, `ipcInput` and `gracefulCancel` options138 export type HasIpc<OptionsType extends Options> = HasIpcOption<139 OptionsType['ipc'],140+ 'node' extends keyof OptionsType ? OptionsType['node'] : undefined,141 'ipcInput' extends keyof OptionsType ? OptionsType['ipcInput'] : undefined,142 'gracefulCancel' extends keyof OptionsType ? OptionsType['gracefulCancel'] : undefined143 >;144 145 type HasIpcOption<146 IpcOption extends Options['ipc'],147+ NodeOption extends Options['node'],148 IpcInputOption extends Options['ipcInput'],149 GracefulCancelOption extends Options['gracefulCancel'],150 > = IpcOption extends true151 ? true152 : IpcOption extends false153 ? false154+ : NodeOption extends true155+ ? true152- : IpcInputOption extends undefined153- ? GracefulCancelOption extends true154- ? true155- : false156- : true;156+ : IpcInputOption extends undefined157+ ? GracefulCancelOption extends true158+ ? true159+ : false160+ : true;161 162 export {};