1 | /**
|
2 | * Async Hooks module: https://nodejs.org/api/async_hooks.html
|
3 | */
|
4 | declare 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 | undefined;
|
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 | undefined;
|
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 | * Binds the given function to the current execution context.
|
120 | * @param fn The function to bind to the current execution context.
|
121 | * @param type An optional name to associate with the underlying `AsyncResource`.
|
122 | */
|
123 | static bind<Func extends (...args: any[]) => any>(fn: Func, type?: string): Func & { asyncResource: AsyncResource };
|
124 |
|
125 | /**
|
126 | * Binds the given function to execute to this `AsyncResource`'s scope.
|
127 | * @param fn The function to bind to the current `AsyncResource`.
|
128 | */
|
129 | bind<Func extends (...args: any[]) => any>(fn: Func): Func & { asyncResource: AsyncResource };
|
130 |
|
131 | /**
|
132 | * Call the provided function with the provided arguments in the
|
133 | * execution context of the async resource. This will establish the
|
134 | * context, trigger the AsyncHooks before callbacks, call the function,
|
135 | * trigger the AsyncHooks after callbacks, and then restore the original
|
136 | * execution context.
|
137 | * @param fn The function to call in the execution context of this
|
138 | * async resource.
|
139 | * @param thisArg The receiver to be used for the function call.
|
140 | * @param args Optional arguments to pass to the function.
|
141 | */
|
142 | runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
|
143 |
|
144 | /**
|
145 | * Call AsyncHooks destroy callbacks.
|
146 | */
|
147 | emitDestroy(): this;
|
148 |
|
149 | /**
|
150 | * @return the unique ID assigned to this AsyncResource instance.
|
151 | */
|
152 | asyncId(): number;
|
153 |
|
154 | /**
|
155 | * @return the trigger ID for this AsyncResource instance.
|
156 | */
|
157 | triggerAsyncId(): number;
|
158 | }
|
159 |
|
160 | /**
|
161 | * When having multiple instances of `AsyncLocalStorage`, they are independent
|
162 | * from each other. It is safe to instantiate this class multiple times.
|
163 | */
|
164 | class AsyncLocalStorage<T> {
|
165 | /**
|
166 | * This method disables the instance of `AsyncLocalStorage`. All subsequent calls
|
167 | * to `asyncLocalStorage.getStore()` will return `undefined` until
|
168 | * `asyncLocalStorage.run()` is called again.
|
169 | *
|
170 | * When calling `asyncLocalStorage.disable()`, all current contexts linked to the
|
171 | * instance will be exited.
|
172 | *
|
173 | * Calling `asyncLocalStorage.disable()` is required before the
|
174 | * `asyncLocalStorage` can be garbage collected. This does not apply to stores
|
175 | * provided by the `asyncLocalStorage`, as those objects are garbage collected
|
176 | * along with the corresponding async resources.
|
177 | *
|
178 | * This method is to be used when the `asyncLocalStorage` is not in use anymore
|
179 | * in the current process.
|
180 | */
|
181 | disable(): void;
|
182 |
|
183 | /**
|
184 | * This method returns the current store. If this method is called outside of an
|
185 | * asynchronous context initialized by calling `asyncLocalStorage.run`, it will
|
186 | * return `undefined`.
|
187 | */
|
188 | getStore(): T | undefined;
|
189 |
|
190 | /**
|
191 | * This methods runs a function synchronously within a context and return its
|
192 | * return value. The store is not accessible outside of the callback function or
|
193 | * the asynchronous operations created within the callback.
|
194 | *
|
195 | * Optionally, arguments can be passed to the function. They will be passed to the
|
196 | * callback function.
|
197 | *
|
198 | * I the callback function throws an error, it will be thrown by `run` too. The
|
199 | * stacktrace will not be impacted by this call and the context will be exited.
|
200 | */
|
201 | // TODO: Apply generic vararg once available
|
202 | run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R;
|
203 |
|
204 | /**
|
205 | * This methods runs a function synchronously outside of a context and return its
|
206 | * return value. The store is not accessible within the callback function or the
|
207 | * asynchronous operations created within the callback.
|
208 | *
|
209 | * Optionally, arguments can be passed to the function. They will be passed to the
|
210 | * callback function.
|
211 | *
|
212 | * If the callback function throws an error, it will be thrown by `exit` too. The
|
213 | * stacktrace will not be impacted by this call and the context will be
|
214 | * re-entered.
|
215 | */
|
216 | // TODO: Apply generic vararg once available
|
217 | exit<R>(callback: (...args: any[]) => R, ...args: any[]): R;
|
218 |
|
219 | /**
|
220 | * Calling `asyncLocalStorage.enterWith(store)` will transition into the context
|
221 | * for the remainder of the current synchronous execution and will persist
|
222 | * through any following asynchronous calls.
|
223 | */
|
224 | enterWith(store: T): void;
|
225 | }
|
226 | }
|
227 |
|
\ | No newline at end of file |