UNPKG

5.96 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 * The resource representing the current execution.
12 * Useful to store data within the resource.
13 *
14 * Resource objects returned by `executionAsyncResource()` are most often internal
15 * Node.js handle objects with undocumented APIs. Using any functions or properties
16 * on the object is likely to crash your application and should be avoided.
17 *
18 * Using `executionAsyncResource()` in the top-level execution context will
19 * return an empty object as there is no handle or request object to use,
20 * but having an object representing the top-level can be helpful.
21 */
22 function executionAsyncResource(): object;
23
24 /**
25 * Returns the ID of the resource responsible for calling the callback that is currently being executed.
26 */
27 function triggerAsyncId(): number;
28
29 interface HookCallbacks {
30 /**
31 * Called when a class is constructed that has the possibility to emit an asynchronous event.
32 * @param asyncId a unique ID for the async resource
33 * @param type the type of the async resource
34 * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created
35 * @param resource reference to the resource representing the async operation, needs to be released during destroy
36 */
37 init?(asyncId: number, type: string, triggerAsyncId: number, resource: object): void;
38
39 /**
40 * When an asynchronous operation is initiated or completes a callback is called to notify the user.
41 * The before callback is called just before said callback is executed.
42 * @param asyncId the unique identifier assigned to the resource about to execute the callback.
43 */
44 before?(asyncId: number): void;
45
46 /**
47 * Called immediately after the callback specified in before is completed.
48 * @param asyncId the unique identifier assigned to the resource which has executed the callback.
49 */
50 after?(asyncId: number): void;
51
52 /**
53 * Called when a promise has resolve() called. This may not be in the same execution id
54 * as the promise itself.
55 * @param asyncId the unique id for the promise that was resolve()d.
56 */
57 promiseResolve?(asyncId: number): void;
58
59 /**
60 * Called after the resource corresponding to asyncId is destroyed
61 * @param asyncId a unique ID for the async resource
62 */
63 destroy?(asyncId: number): void;
64 }
65
66 interface AsyncHook {
67 /**
68 * Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
69 */
70 enable(): this;
71
72 /**
73 * 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.
74 */
75 disable(): this;
76 }
77
78 /**
79 * Registers functions to be called for different lifetime events of each async operation.
80 * @param options the callbacks to register
81 * @return an AsyncHooks instance used for disabling and enabling hooks
82 */
83 function createHook(options: HookCallbacks): AsyncHook;
84
85 interface AsyncResourceOptions {
86 /**
87 * The ID of the execution context that created this async event.
88 * Default: `executionAsyncId()`
89 */
90 triggerAsyncId?: number;
91
92 /**
93 * Disables automatic `emitDestroy` when the object is garbage collected.
94 * This usually does not need to be set (even if `emitDestroy` is called
95 * manually), unless the resource's `asyncId` is retrieved and the
96 * sensitive API's `emitDestroy` is called with it.
97 * Default: `false`
98 */
99 requireManualDestroy?: boolean;
100 }
101
102 /**
103 * The class AsyncResource was designed to be extended by the embedder's async resources.
104 * Using this users can easily trigger the lifetime events of their own resources.
105 */
106 class AsyncResource {
107 /**
108 * AsyncResource() is meant to be extended. Instantiating a
109 * new AsyncResource() also triggers init. If triggerAsyncId is omitted then
110 * async_hook.executionAsyncId() is used.
111 * @param type The type of async event.
112 * @param triggerAsyncId The ID of the execution context that created
113 * this async event (default: `executionAsyncId()`), or an
114 * AsyncResourceOptions object (since 9.3)
115 */
116 constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions);
117
118 /**
119 * Call the provided function with the provided arguments in the
120 * execution context of the async resource. This will establish the
121 * context, trigger the AsyncHooks before callbacks, call the function,
122 * trigger the AsyncHooks after callbacks, and then restore the original
123 * execution context.
124 * @param fn The function to call in the execution context of this
125 * async resource.
126 * @param thisArg The receiver to be used for the function call.
127 * @param args Optional arguments to pass to the function.
128 */
129 runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
130
131 /**
132 * Call AsyncHooks destroy callbacks.
133 */
134 emitDestroy(): void;
135
136 /**
137 * @return the unique ID assigned to this AsyncResource instance.
138 */
139 asyncId(): number;
140
141 /**
142 * @return the trigger ID for this AsyncResource instance.
143 */
144 triggerAsyncId(): number;
145 }
146}