🤖 Merge PR #46270 Fix types for npm package death by @pranaygp

* Fix types for npm package `death`

when using `uncaughtException: true`, the handler actually receives an `Error` and an `origin` instead of a signal. 

furhtermore, SIGHUP is not supported:

see:
f9453b68b8/lib/death.js (L2-L7)
f9453b68b8/lib/death.js (L31)
https://nodejs.org/api/process.html#process_event_uncaughtexception

* add tests

* prettier
This commit is contained in:
Pranay Prakash 2020-08-11 12:02:37 -07:00 committed by GitHub
parent 9fc4c8ebb8
commit 2e748a87c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 24 deletions

View File

@ -1,18 +1,36 @@
import ON_DEATH = require("death");
import ON_DEATH = require('death');
const unsub: () => void = ON_DEATH(
(value: "SIGINT" | "SIGTERM" | "SIGQUIT") => {}
);
const unsub1: () => void = ON_DEATH((value: 'SIGINT' | 'SIGTERM' | 'SIGQUIT') => {});
const otherUnsub: () => void = ON_DEATH({
const unsub2: () => void = ON_DEATH({
debug: true,
SIGINT: true,
SIGTERM: true,
SIGQUIT: true,
})((value: 'SIGINT' | 'SIGTERM' | 'SIGQUIT') => {});
const unsub3: () => void = ON_DEATH({
debug: true,
uncaughtException: true,
SIGINT: true,
SIGTERM: true,
SIGQUIT: true,
})((value: 'SIGINT' | 'SIGTERM' | 'SIGQUIT' | Error) => {});
const unsub4: () => void = ON_DEATH({
debug: true,
SIGINT: true,
SIGTERM: true,
SIGQUIT: true,
})((value: 'SIGINT' | 'SIGTERM' | 'SIGQUIT') => {});
const unsub5: () => void = ON_DEATH({
debug: true,
uncaughtException: true,
SIGINT: true,
SIGTERM: true,
SIGQUIT: true,
SIGHUP: true
})(
(
value: "uncaughtException" | "SIGINT" | "SIGTERM" | "SIGQUIT" | "SIGHUP"
) => {}
// $ExpectError
(value: 'SIGINT' | 'SIGTERM' | 'SIGQUIT') => {},
);

View File

@ -1,9 +1,12 @@
// Type definitions for death 1.1
// Project: https://github.com/jprichardson/node-death
// Definitions by: Cameron Knight <https://github.com/ckknight>
// Pranay Prakash <https://github.com/pranaygp>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
type Signal = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
/**
* Invokes a callback when a SIGINT, SIGTERM, or SIGQUIT is detected
* on the current node process.
@ -20,9 +23,37 @@
* // later
* OFF_DEATH();
*/
declare function ON_DEATH(
callback: (signal: "SIGINT" | "SIGTERM" | "SIGQUIT") => void
): () => void;
declare function ON_DEATH(callback: (arg: Signal) => void): () => void;
/**
* Invokes a callback when a SIGINT, SIGTERM, or SIGQUIT is detected
* on the current node process. Configurable by the provided options.
*
* @param options
* @returns A function to subscribe to the configured death detection
* @example
* ON_DEATH({
* debug: true,
* })((signal) => {
* console.log('Oh no!');
* });
* @example
* const OFF_DEATH = ON_DEATH({
* debug: true,
* })((signal) => {
* console.log('Oh no!');
* });
* // later
* OFF_DEATH();
*/
declare function ON_DEATH(options: {
debug?: boolean;
SIGINT?: boolean;
SIGTERM?: boolean;
SIGQUIT?: boolean;
uncaughtException?: false;
}): (callback: (signal: Signal) => void) => () => void;
/**
* Invokes a callback when a SIGINT, SIGTERM, or SIGQUIT is detected
* on the current node process. Configurable by the provided options.
@ -51,16 +82,7 @@ declare function ON_DEATH(options: {
SIGINT?: boolean;
SIGTERM?: boolean;
SIGQUIT?: boolean;
SIGHUP?: boolean;
uncaughtException?: boolean;
}): (
callback: (
signal:
| "SIGINT"
| "SIGTERM"
| "SIGQUIT"
| "SIGHUP"
| "uncaughtException"
) => void
) => () => void;
uncaughtException: true;
}): (callback: (signalOrErr: Signal | Error, origin?: string) => void) => () => void;
export = ON_DEATH;