UNPKG

5.29 kBTypeScriptView Raw
1/**
2 * Async Hooks module: https://nodejs.org/api/async_hooks.html
3 */
4declare module "async_hooks" {
5 /**
6 * Returns the asyncId of the current execution context.
7 */
8 function executionAsyncId(): number;
9
10 /**
11 * Returns the ID of the resource responsible for calling the callback that is currently being executed.
12 */
13 function triggerAsyncId(): number;
14
15 interface HookCallbacks {
16 /**
17 * Called when a class is constructed that has the possibility to emit an asynchronous event.
18 * @param asyncId a unique ID for the async resource
19 * @param type the type of the async resource
20 * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created
21 * @param resource reference to the resource representing the async operation, needs to be released during destroy
22 */
23 init?(asyncId: number, type: string, triggerAsyncId: number, resource: object): void;
24
25 /**
26 * When an asynchronous operation is initiated or completes a callback is called to notify the user.
27 * The before callback is called just before said callback is executed.
28 * @param asyncId the unique identifier assigned to the resource about to execute the callback.
29 */
30 before?(asyncId: number): void;
31
32 /**
33 * Called immediately after the callback specified in before is completed.
34 * @param asyncId the unique identifier assigned to the resource which has executed the callback.
35 */
36 after?(asyncId: number): void;
37
38 /**
39 * Called when a promise has resolve() called. This may not be in the same execution id
40 * as the promise itself.
41 * @param asyncId the unique id for the promise that was resolve()d.
42 */
43 promiseResolve?(asyncId: number): void;
44
45 /**
46 * Called after the resource corresponding to asyncId is destroyed
47 * @param asyncId a unique ID for the async resource
48 */
49 destroy?(asyncId: number): void;
50 }
51
52 interface AsyncHook {
53 /**
54 * Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
55 */
56 enable(): this;
57
58 /**
59 * Disable the callbacks for a given AsyncHook instance from the global pool of AsyncHook callbacks to be executed. Once a hook has been disabled it will not be called again until enabled.
60 */
61 disable(): this;
62 }
63
64 /**
65 * Registers functions to be called for different lifetime events of each async operation.
66 * @param options the callbacks to register
67 * @return an AsyncHooks instance used for disabling and enabling hooks
68 */
69 function createHook(options: HookCallbacks): AsyncHook;
70
71 interface AsyncResourceOptions {
72 /**
73 * The ID of the execution context that created this async event.
74 * Default: `executionAsyncId()`
75 */
76 triggerAsyncId?: number;
77
78 /**
79 * Disables automatic `emitDestroy` when the object is garbage collected.
80 * This usually does not need to be set (even if `emitDestroy` is called
81 * manually), unless the resource's `asyncId` is retrieved and the
82 * sensitive API's `emitDestroy` is called with it.
83 * Default: `false`
84 */
85 requireManualDestroy?: boolean;
86 }
87
88 /**
89 * The class AsyncResource was designed to be extended by the embedder's async resources.
90 * Using this users can easily trigger the lifetime events of their own resources.
91 */
92 class AsyncResource {
93 /**
94 * AsyncResource() is meant to be extended. Instantiating a
95 * new AsyncResource() also triggers init. If triggerAsyncId is omitted then
96 * async_hook.executionAsyncId() is used.
97 * @param type The type of async event.
98 * @param triggerAsyncId The ID of the execution context that created
99 * this async event (default: `executionAsyncId()`), or an
100 * AsyncResourceOptions object (since 9.3)
101 */
102 constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions);
103
104 /**
105 * Call the provided function with the provided arguments in the
106 * execution context of the async resource. This will establish the
107 * context, trigger the AsyncHooks before callbacks, call the function,
108 * trigger the AsyncHooks after callbacks, and then restore the original
109 * execution context.
110 * @param fn The function to call in the execution context of this
111 * async resource.
112 * @param thisArg The receiver to be used for the function call.
113 * @param args Optional arguments to pass to the function.
114 */
115 runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
116
117 /**
118 * Call AsyncHooks destroy callbacks.
119 */
120 emitDestroy(): void;
121
122 /**
123 * @return the unique ID assigned to this AsyncResource instance.
124 */
125 asyncId(): number;
126
127 /**
128 * @return the trigger ID for this AsyncResource instance.
129 */
130 triggerAsyncId(): number;
131 }
132}