From 2e748a87c27d308829d3ea6ab44aed1a7f4af60e Mon Sep 17 00:00:00 2001 From: Pranay Prakash Date: Tue, 11 Aug 2020 12:02:37 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#46270=20Fix=20type?= =?UTF-8?q?s=20for=20npm=20package=20`death`=20by=20@pranaygp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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: https://github.com/jprichardson/node-death/blob/f9453b68b8168def79fa8350f294e2b757f64d75/lib/death.js#L2-L7 https://github.com/jprichardson/node-death/blob/f9453b68b8168def79fa8350f294e2b757f64d75/lib/death.js#L31 https://nodejs.org/api/process.html#process_event_uncaughtexception * add tests * prettier --- types/death/death-tests.ts | 36 +++++++++++++++++++------- types/death/index.d.ts | 52 +++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/types/death/death-tests.ts b/types/death/death-tests.ts index 4ab4ecb234..4d0ac82bc7 100644 --- a/types/death/death-tests.ts +++ b/types/death/death-tests.ts @@ -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') => {}, ); diff --git a/types/death/index.d.ts b/types/death/index.d.ts index 629d413660..af34d6dead 100644 --- a/types/death/index.d.ts +++ b/types/death/index.d.ts @@ -1,9 +1,12 @@ // Type definitions for death 1.1 // Project: https://github.com/jprichardson/node-death // Definitions by: Cameron Knight +// Pranay Prakash // 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;