|
| 1 | +/** Changes the return type of a function to `any`. */ |
| 2 | +type ReturningAny<T extends (...args: any[]) => any> = (...a: Parameters<T>) => any; |
| 3 | + |
| 4 | +/** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/globals.d.ts|Process.on()} */ |
| 5 | +type ProcessEventOrSignal = |
| 6 | + NodeJS.Signals |
| 7 | + | 'beforeExit' |
| 8 | + | 'disconnect' |
| 9 | + | 'exit' |
| 10 | + | 'rejectionHandled' |
| 11 | + | 'uncaughtException' |
| 12 | + | 'unhandledRejection' |
| 13 | + | 'warning' |
| 14 | + | 'message' |
| 15 | + | 'newListener' |
| 16 | + | 'removeListener' |
| 17 | + | 'multipleResolves'; |
| 18 | + |
| 19 | +declare namespace AsyncExitHook { |
| 20 | + |
| 21 | + /** The callback to call when the event handling is finished. */ |
| 22 | + type AsycHookCallback = () => void; |
| 23 | + |
| 24 | + /** Async hook called for uncaught exceptions or unhandled rejections. Call the callback when finished. */ |
| 25 | + type AsyncErrorHook = (err: Error | any, stepTowardExit: AsycHookCallback) => void; |
| 26 | + |
| 27 | + /** Async hook. Call the callback when finished. */ |
| 28 | + type AsyncHook = (stepTowardExit: AsycHookCallback) => void; |
| 29 | + |
| 30 | + /** Sync hook called for uncaught exception or unhandled rejection. */ |
| 31 | + type SyncErrorHook = (err: Error | any) => void; |
| 32 | + |
| 33 | + /** Sync hook. */ |
| 34 | + type SyncHook = () => void; |
| 35 | + |
| 36 | + /** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/globals.d.ts|Process.on()} */ |
| 37 | + interface HookEventFunction { |
| 38 | + (event: 'beforeExit', code?: number | null, filter?: ReturningAny<NodeJS.BeforeExitListener>): void; |
| 39 | + |
| 40 | + (event: 'disconnect', code?: number | null, filter?: ReturningAny<NodeJS.DisconnectListener>): void; |
| 41 | + |
| 42 | + (event: 'exit', code?: number | null, filter?: ReturningAny<NodeJS.ExitListener>): void; |
| 43 | + |
| 44 | + (event: 'rejectionHandled', code?: number | null, filter?: ReturningAny<NodeJS.RejectionHandledListener>): void; |
| 45 | + |
| 46 | + (event: 'uncaughtException', code?: number | null, filter?: ReturningAny<NodeJS.UncaughtExceptionListener>): void; |
| 47 | + |
| 48 | + (event: 'unhandledRejection', code?: number | null, filter?: ReturningAny<NodeJS.UnhandledRejectionListener>): void; |
| 49 | + |
| 50 | + (event: 'warning', code?: number | null, filter?: ReturningAny<NodeJS.WarningListener>): void; |
| 51 | + |
| 52 | + (event: 'message', code?: number | null, filter?: ReturningAny<NodeJS.MessageListener>): void; |
| 53 | + |
| 54 | + (event: NodeJS.Signals, code?: number | null, filter?: ReturningAny<NodeJS.SignalsListener>): void; |
| 55 | + |
| 56 | + (event: 'newListener', code?: number | null, filter?: ReturningAny<NodeJS.NewListenerListener>): void; |
| 57 | + |
| 58 | + (event: 'removeListener', code?: number | null, filter?: ReturningAny<NodeJS.RemoveListenerListener>): void; |
| 59 | + |
| 60 | + (event: 'multipleResolves', code?: number | null, filter?: ReturningAny<NodeJS.MultipleResolveListener>): void; |
| 61 | + } |
| 62 | + |
| 63 | + interface AsyncExitHook { |
| 64 | + /** Register a new exit hook. */ |
| 65 | + (hook: SyncHook | AsyncHook): void; |
| 66 | + |
| 67 | + /** |
| 68 | + * Register new signal / event to hook to. |
| 69 | + * |
| 70 | + * @param event The signal or event name to listen to, using `process.on()`. eg 'SIGBREAK' or 'beforeExit'. |
| 71 | + * @param code The code to exit the process with. |
| 72 | + * @param filter A function that will be called with the `process.on()` signal/event handler arguments. |
| 73 | + * Returning `true` will not cause the exit hook to fire. |
| 74 | + */ |
| 75 | + hookEvent: HookEventFunction; |
| 76 | + |
| 77 | + /** |
| 78 | + * Unhook signal / event. |
| 79 | + * |
| 80 | + * @param event The signal or event name to stop listen to. eg 'SIGBREAK' or 'beforeExit'. |
| 81 | + */ |
| 82 | + unhookEvent: (event: ProcessEventOrSignal) => void; |
| 83 | + |
| 84 | + /** List hooked events. */ |
| 85 | + hookedEvents: () => ProcessEventOrSignal[]; |
| 86 | + |
| 87 | + /** |
| 88 | + * Add an uncaught exception handler. |
| 89 | + * Note: it will be called for 'uncaughtException', but also for 'unhandledRejection' |
| 90 | + */ |
| 91 | + uncaughtExceptionHandler: (hook: SyncErrorHook | AsyncErrorHook) => void; |
| 92 | + |
| 93 | + /** Add an unhandled rejection handler. */ |
| 94 | + unhandledRejectionHandler: (hook: SyncErrorHook | AsyncErrorHook) => void; |
| 95 | + |
| 96 | + /** |
| 97 | + * Configure the time to wait for async hooks to finish, in total, after which the process will forcefully exit. |
| 98 | + * The default is 10000 = 10 seconds. |
| 99 | + * The maximum is the upper bound of a signed integer, which is (2^31 - 1) = 2147483647 = ~25 days! |
| 100 | + * |
| 101 | + * @param ms The time in milliseconds. |
| 102 | + */ |
| 103 | + forceExitTimeout: (ms: number) => void; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +declare const add: AsyncExitHook.AsyncExitHook; |
| 109 | + |
| 110 | +declare module 'async-exit-hook' { |
| 111 | + export = add; |
| 112 | +} |
0 commit comments