1 | /**
|
2 | * Async Hooks module: https://nodejs.org/api/async_hooks.html
|
3 | */
|
4 | declare module 'node:async_hooks' {
|
5 | export * from 'async_hooks';
|
6 | }
|
7 |
|
8 | /**
|
9 | * Async Hooks module: https://nodejs.org/api/async_hooks.html
|
10 | */
|
11 | declare module 'async_hooks' {
|
12 | /**
|
13 | * Returns the asyncId of the current execution context.
|
14 | */
|
15 | function executionAsyncId(): number;
|
16 |
|
17 | /**
|
18 | * The resource representing the current execution.
|
19 | * Useful to store data within the resource.
|
20 | *
|
21 | * Resource objects returned by `executionAsyncResource()` are most often internal
|
22 | * Node.js handle objects with undocumented APIs. Using any functions or properties
|
23 | * on the object is likely to crash your application and should be avoided.
|
24 | *
|
25 | * Using `executionAsyncResource()` in the top-level execution context will
|
26 | * return an empty object as there is no handle or request object to use,
|
27 | * but having an object representing the top-level can be helpful.
|
28 | */
|
29 | function executionAsyncResource(): object;
|
30 |
|
31 | /**
|
32 | * Returns the ID of the resource responsible for calling the callback that is currently being executed.
|
33 | */
|
34 | function triggerAsyncId(): number;
|
35 |
|
36 | interface HookCallbacks {
|
37 | /**
|
38 | * Called when a class is constructed that has the possibility to emit an asynchronous event.
|
39 | * @param asyncId a unique ID for the async resource
|
40 | * @param type the type of the async resource
|
41 | * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created
|
42 | * @param resource reference to the resource representing the async operation, needs to be released during destroy
|
43 | */
|
44 | init?(asyncId: number, type: string, triggerAsyncId: number, resource: object): void;
|
45 |
|
46 | /**
|
47 | * When an asynchronous operation is initiated or completes a callback is called to notify the user.
|
48 | * The before callback is called just before said callback is executed.
|
49 | * @param asyncId the unique identifier assigned to the resource about to execute the callback.
|
50 | */
|
51 | before?(asyncId: number): void;
|
52 |
|
53 | /**
|
54 | * Called immediately after the callback specified in before is completed.
|
55 | * @param asyncId the unique identifier assigned to the resource which has executed the callback.
|
56 | */
|
57 | after?(asyncId: number): void;
|
58 |
|
59 | /**
|
60 | * Called when a promise has resolve() called. This may not be in the same execution id
|
61 | * as the promise itself.
|
62 | * @param asyncId the unique id for the promise that was resolve()d.
|
63 | */
|
64 | promiseResolve?(asyncId: number): void;
|
65 |
|
66 | /**
|
67 | * Called after the resource corresponding to asyncId is destroyed
|
68 | * @param asyncId a unique ID for the async resource
|
69 | */
|
70 | destroy?(asyncId: number): void;
|
71 | }
|
72 |
|
73 | interface AsyncHook {
|
74 | /**
|
75 | * Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
|
76 | */
|
77 | enable(): this;
|
78 |
|
79 | /**
|
80 | * 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.
|
81 | */
|
82 | disable(): this;
|
83 | }
|
84 |
|
85 | /**
|
86 | * Registers functions to be called for different lifetime events of each async operation.
|
87 | * @param options the callbacks to register
|
88 | * @return an AsyncHooks instance used for disabling and enabling hooks
|
89 | */
|
90 | function createHook(options: HookCallbacks): AsyncHook;
|
91 |
|
92 | interface AsyncResourceOptions {
|
93 | /**
|
94 | * The ID of the execution context that created this async event.
|
95 | * Default: `executionAsyncId()`
|
96 | */
|
97 | triggerAsyncId?: number;
|
98 |
|
99 | /**
|
100 | * Disables automatic `emitDestroy` when the object is garbage collected.
|
101 | * This usually does not need to be set (even if `emitDestroy` is called
|
102 | * manually), unless the resource's `asyncId` is retrieved and the
|
103 | * sensitive API's `emitDestroy` is called with it.
|
104 | * Default: `false`
|
105 | */
|
106 | requireManualDestroy?: boolean;
|
107 | }
|
108 |
|
109 | /**
|
110 | * The class AsyncResource was designed to be extended by the embedder's async resources.
|
111 | * Using this users can easily trigger the lifetime events of their own resources.
|
112 | */
|
113 | class AsyncResource {
|
114 | /**
|
115 | * AsyncResource() is meant to be extended. Instantiating a
|
116 | * new AsyncResource() also triggers init. If triggerAsyncId is omitted then
|
117 | * async_hook.executionAsyncId() is used.
|
118 | * @param type The type of async event.
|
119 | * @param triggerAsyncId The ID of the execution context that created
|
120 | * this async event (default: `executionAsyncId()`), or an
|
121 | * AsyncResourceOptions object (since 9.3)
|
122 | */
|
123 | constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions);
|
124 |
|
125 | /**
|
126 | * Binds the given function to the current execution context.
|
127 | * @param fn The function to bind to the current execution context.
|
128 | * @param type An optional name to associate with the underlying `AsyncResource`.
|
129 | */
|
130 | static bind<Func extends (...args: any[]) => any>(fn: Func, type?: string): Func & { asyncResource: AsyncResource };
|
131 |
|
132 | /**
|
133 | * Binds the given function to execute to this `AsyncResource`'s scope.
|
134 | * @param fn The function to bind to the current `AsyncResource`.
|
135 | */
|
136 | bind<Func extends (...args: any[]) => any>(fn: Func): Func & { asyncResource: AsyncResource };
|
137 |
|
138 | /**
|
139 | * Call the provided function with the provided arguments in the
|
140 | * execution context of the async resource. This will establish the
|
141 | * context, trigger the AsyncHooks before callbacks, call the function,
|
142 | * trigger the AsyncHooks after callbacks, and then restore the original
|
143 | * execution context.
|
144 | * @param fn The function to call in the execution context of this
|
145 | * async resource.
|
146 | * @param thisArg The receiver to be used for the function call.
|
147 | * @param args Optional arguments to pass to the function.
|
148 | */
|
149 | runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
|
150 |
|
151 | /**
|
152 | * Call AsyncHooks destroy callbacks.
|
153 | */
|
154 | emitDestroy(): void;
|
155 |
|
156 | /**
|
157 | * @return the unique ID assigned to this AsyncResource instance.
|
158 | */
|
159 | asyncId(): number;
|
160 |
|
161 | /**
|
162 | * @return the trigger ID for this AsyncResource instance.
|
163 | */
|
164 | triggerAsyncId(): number;
|
165 | }
|
166 |
|
167 | /**
|
168 | * When having multiple instances of `AsyncLocalStorage`, they are independent
|
169 | * from each other. It is safe to instantiate this class multiple times.
|
170 | */
|
171 | class AsyncLocalStorage<T> {
|
172 | /**
|
173 | * This method disables the instance of `AsyncLocalStorage`. All subsequent calls
|
174 | * to `asyncLocalStorage.getStore()` will return `undefined` until
|
175 | * `asyncLocalStorage.run()` is called again.
|
176 | *
|
177 | * When calling `asyncLocalStorage.disable()`, all current contexts linked to the
|
178 | * instance will be exited.
|
179 | *
|
180 | * Calling `asyncLocalStorage.disable()` is required before the
|
181 | * `asyncLocalStorage` can be garbage collected. This does not apply to stores
|
182 | * provided by the `asyncLocalStorage`, as those objects are garbage collected
|
183 | * along with the corresponding async resources.
|
184 | *
|
185 | * This method is to be used when the `asyncLocalStorage` is not in use anymore
|
186 | * in the current process.
|
187 | */
|
188 | disable(): void;
|
189 |
|
190 | /**
|
191 | * This method returns the current store. If this method is called outside of an
|
192 | * asynchronous context initialized by calling `asyncLocalStorage.run`, it will
|
193 | * return `undefined`.
|
194 | */
|
195 | getStore(): T | undefined;
|
196 |
|
197 | /**
|
198 | * This methods runs a function synchronously within a context and return its
|
199 | * return value. The store is not accessible outside of the callback function or
|
200 | * the asynchronous operations created within the callback.
|
201 | *
|
202 | * Optionally, arguments can be passed to the function. They will be passed to the
|
203 | * callback function.
|
204 | *
|
205 | * I the callback function throws an error, it will be thrown by `run` too. The
|
206 | * stacktrace will not be impacted by this call and the context will be exited.
|
207 | */
|
208 | // TODO: Apply generic vararg once available
|
209 | run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R;
|
210 |
|
211 | /**
|
212 | * This methods runs a function synchronously outside of a context and return its
|
213 | * return value. The store is not accessible within the callback function or the
|
214 | * asynchronous operations created within the callback.
|
215 | *
|
216 | * Optionally, arguments can be passed to the function. They will be passed to the
|
217 | * callback function.
|
218 | *
|
219 | * If the callback function throws an error, it will be thrown by `exit` too. The
|
220 | * stacktrace will not be impacted by this call and the context will be
|
221 | * re-entered.
|
222 | */
|
223 | // TODO: Apply generic vararg once available
|
224 | exit<R>(callback: (...args: any[]) => R, ...args: any[]): R;
|
225 |
|
226 | /**
|
227 | * Calling `asyncLocalStorage.enterWith(store)` will transition into the context
|
228 | * for the remainder of the current synchronous execution and will persist
|
229 | * through any following asynchronous calls.
|
230 | */
|
231 | enterWith(store: T): void;
|
232 | }
|
233 | }
|
234 |
|
\ | No newline at end of file |