Add `.kill(signal, error)` (#836)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent f5283d5 · Browse files · 5 files·+51 added−18 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+51 added−18 removed
- tests:+21 added−5 removed
- other:+30 added−13 removed
Provenance
- Commit
30a662d- Tree
cc43d9c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f5283d5
5 files changed+51 added−18 removed
modified:lib/kill.js+19 added-9 removed
6 unmodified lines789101112111213141516171818192019202122232425262723242528293031283233343536373839404142434480 unmodified lines6 unmodified lines
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;
// Monkey-patches `childProcess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`export const spawnedKill = ({kill, spawned, options: {forceKillAfterDelay, killSignal}, controller}, signalOrError = killSignal) => { const signal = handleKillError(signalOrError, spawned, killSignal);export const spawnedKill = ({kill, spawned, options: {forceKillAfterDelay, killSignal}, controller}, signalOrError, errorArgument) => { const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal); emitKillError(spawned, error); const killResult = kill(signal); setKillTimeout({kill, signal, forceKillAfterDelay, killSignal, killResult, controller}); return killResult;};
const handleKillError = (signalOrError, spawned, killSignal) => { if (typeof signalOrError === 'string' || typeof signalOrError === 'number') { return signalOrError;const parseKillArguments = (signalOrError, errorArgument, killSignal) => { const [signal = killSignal, error] = isErrorInstance(signalOrError) ? [undefined, signalOrError] : [signalOrError, errorArgument];
if (typeof signal !== 'string' && typeof signal !== 'number') { throw new TypeError(`The first argument must be an error instance or a signal name string/number: ${signal}`); }
if (isErrorInstance(signalOrError)) { spawned.emit(errorSignal, signalOrError); return killSignal; if (error !== undefined && !isErrorInstance(error)) { throw new TypeError(`The second argument is optional. If specified, it must be an error instance: ${error}`); }
throw new TypeError(`The first argument must be an error instance or a signal name string/number: ${signalOrError}`); return {signal, error};};
const emitKillError = (spawned, error) => { if (error !== undefined) { spawned.emit(errorSignal, error); }};
// Like `error` signal but internal to Execa.// E.g. does not make process crash when no `error` listener is set.export const errorSignal = Symbol('error');
80 unmodified linesmodified:test/kill.js+21 added-5 removed
425 unmodified lines426 t.true(thrownError.isTerminated);427 t.is(thrownError.signal, 'SIGINT');428 });429 430-const testInvalidKillArgument = async (t, killArgument) => {430+const testInvalidKillArgument = async (t, killArgument, secondKillArgument) => {431 const subprocess = execa('empty.js');432+ const message = secondKillArgument instanceof Error || secondKillArgument === undefined433+ ? /error instance or a signal name/434+ : /second argument is optional/;435 t.throws(() => {433- subprocess.kill(killArgument);434- }, {message: /error instance or a signal name/});436+ subprocess.kill(killArgument, secondKillArgument);437+ }, {message});438 await subprocess;439 };440 441 test('Cannot call .kill(null)', testInvalidKillArgument, null);442 test('Cannot call .kill(0n)', testInvalidKillArgument, 0n);443 test('Cannot call .kill(true)', testInvalidKillArgument, true);444 test('Cannot call .kill(errorObject)', testInvalidKillArgument, {name: '', message: '', stack: ''});445 test('Cannot call .kill([error])', testInvalidKillArgument, [new Error('test')]);446+test('Cannot call .kill(undefined, true)', testInvalidKillArgument, undefined, true);447+test('Cannot call .kill("SIGTERM", true)', testInvalidKillArgument, 'SIGTERM', true);448+test('Cannot call .kill(true, error)', testInvalidKillArgument, true, new Error('test'));449 450 test('.kill(error) propagates error', async t => {451 const subprocess = execa('forever.js');452 const originalMessage = 'test';10 unmodified lines463 });464 465 test('.kill(error) uses killSignal', async t => {466 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});461- subprocess.kill(new Error('test'));462- t.like(await t.throwsAsync(subprocess), {signal: 'SIGINT'});467+ const error = new Error('test');468+ subprocess.kill(error);469+ t.is(await t.throwsAsync(subprocess), error);470+ t.is(error.signal, 'SIGINT');471 });472 473+test('.kill(signal, error) uses signal', async t => {474+ const subprocess = execa('forever.js');475+ const error = new Error('test');476+ subprocess.kill('SIGINT', error);477+ t.is(await t.throwsAsync(subprocess), error);478+ t.is(error.signal, 'SIGINT');479+});480+481 test('.kill(error) is a noop if process already exited', async t => {482 const subprocess = execa('empty.js');483 await subprocess;484 t.false(isRunning(subprocess.pid));33 unmodified linesmodified:index.d.ts+2 added-1 removed
849 unmodified lines850 When an error is passed as argument, its message and stack trace are kept in the child process' error. The child process is then terminated with the default signal. This does not emit the [`error` event](https://nodejs.org/api/child_process.html#event-error).851 852 [More info.](https://nodejs.org/api/child_process.html#subprocesskillsignal)853 */854- kill(signalOrError: Parameters<ChildProcess['kill']>[0] | Error): ReturnType<ChildProcess['kill']>;854+ kill(signal: Parameters<ChildProcess['kill']>[0], error?: Error): ReturnType<ChildProcess['kill']>;855+ kill(error?: Error): ReturnType<ChildProcess['kill']>;856 };857 858 export type ExecaChildProcess<OptionsType extends Options = Options> = ChildProcess &859 ExecaChildPromise<OptionsType> &464 unmodified linesmodified:index.test-d.ts+5 added-1 removed
1298 unmodified lines1299 expectType<boolean>(execa('unicorns').kill());1300 execa('unicorns').kill('SIGKILL');1301 execa('unicorns').kill(undefined);1302 execa('unicorns').kill(new Error('test'));1303-expectError(execa('unicorns').kill('SIGKILL', {}));1303+execa('unicorns').kill('SIGKILL', new Error('test'));1304+execa('unicorns').kill(undefined, new Error('test'));1305 expectError(execa('unicorns').kill(null));1306 expectError(execa('unicorns').kill(0n));1307 expectError(execa('unicorns').kill([new Error('test')]));1308 expectError(execa('unicorns').kill({message: 'test'}));1309+expectError(execa('unicorns').kill(undefined, {}));1310+expectError(execa('unicorns').kill('SIGKILL', {}));1311+expectError(execa('unicorns').kill(null, new Error('test')));1312 1313 expectError(execa(['unicorns', 'arg']));1314 expectType<ExecaChildProcess>(execa('unicorns'));1315 expectType<ExecaChildProcess>(execa(fileUrl));99 unmodified linesmodified:readme.md+4 added-2 removed
323 unmodified lines324 [`childProcess.stdout`](#stdout) (and/or [`childProcess.stderr`](#stderr) depending on `streamName`) must not be `undefined`. When `streamName` is `"all"`, the [`all` option](#all-2) must be set to `true`.325 326 Returns `execaChildProcess`, which allows chaining `.pipe()` then `await`ing the [final result](#childprocessresult).327 328-#### kill(signalOrError?)328+#### kill(signal, error?)329+#### kill(error?)330 330-`signalOrError`: `string | number | Error`\331+`signal`: `string | number`\332+`error`: `Error`\333 _Returns_: `boolean`334 335 Sends a [signal](https://nodejs.org/api/os.html#signal-constants) to the child process. The default signal is the [`killSignal`](#killsignal) option. `killSignal` defaults to `SIGTERM`, which [terminates](#isterminated) the child process.336 623 unmodified lines