[6a3a178] | 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()` or `asyncLocalStorage.runSyncAndReturn()`
|
---|
| 169 | * is called again.
|
---|
| 170 | *
|
---|
| 171 | * When calling `asyncLocalStorage.disable()`, all current contexts linked to the
|
---|
| 172 | * instance will be exited.
|
---|
| 173 | *
|
---|
| 174 | * Calling `asyncLocalStorage.disable()` is required before the
|
---|
| 175 | * `asyncLocalStorage` can be garbage collected. This does not apply to stores
|
---|
| 176 | * provided by the `asyncLocalStorage`, as those objects are garbage collected
|
---|
| 177 | * along with the corresponding async resources.
|
---|
| 178 | *
|
---|
| 179 | * This method is to be used when the `asyncLocalStorage` is not in use anymore
|
---|
| 180 | * in the current process.
|
---|
| 181 | */
|
---|
| 182 | disable(): void;
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * This method returns the current store.
|
---|
| 186 | * If this method is called outside of an asynchronous context initialized by
|
---|
| 187 | * calling `asyncLocalStorage.run` or `asyncLocalStorage.runAndReturn`, it will
|
---|
| 188 | * return `undefined`.
|
---|
| 189 | */
|
---|
| 190 | getStore(): T | undefined;
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * Calling `asyncLocalStorage.run(callback)` will create a new asynchronous
|
---|
| 194 | * context.
|
---|
| 195 | * Within the callback function and the asynchronous operations from the callback,
|
---|
| 196 | * `asyncLocalStorage.getStore()` will return an instance of `Map` known as
|
---|
| 197 | * "the store". This store will be persistent through the following
|
---|
| 198 | * asynchronous calls.
|
---|
| 199 | *
|
---|
| 200 | * The callback will be ran asynchronously. Optionally, arguments can be passed
|
---|
| 201 | * to the function. They will be passed to the callback function.
|
---|
| 202 | *
|
---|
| 203 | * If an error is thrown by the callback function, it will not be caught by
|
---|
| 204 | * a `try/catch` block as the callback is ran in a new asynchronous resource.
|
---|
| 205 | * Also, the stacktrace will be impacted by the asynchronous call.
|
---|
| 206 | */
|
---|
| 207 | // TODO: Apply generic vararg once available
|
---|
| 208 | run(store: T, callback: (...args: any[]) => void, ...args: any[]): void;
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * Calling `asyncLocalStorage.exit(callback)` will create a new asynchronous
|
---|
| 212 | * context.
|
---|
| 213 | * Within the callback function and the asynchronous operations from the callback,
|
---|
| 214 | * `asyncLocalStorage.getStore()` will return `undefined`.
|
---|
| 215 | *
|
---|
| 216 | * The callback will be ran asynchronously. Optionally, arguments can be passed
|
---|
| 217 | * to the function. They will be passed to the callback function.
|
---|
| 218 | *
|
---|
| 219 | * If an error is thrown by the callback function, it will not be caught by
|
---|
| 220 | * a `try/catch` block as the callback is ran in a new asynchronous resource.
|
---|
| 221 | * Also, the stacktrace will be impacted by the asynchronous call.
|
---|
| 222 | */
|
---|
| 223 | exit(callback: (...args: any[]) => void, ...args: any[]): void;
|
---|
| 224 |
|
---|
| 225 | /**
|
---|
| 226 | * This methods runs a function synchronously outside of a context and return its
|
---|
| 227 | * return value. The store is not accessible within the callback function or
|
---|
| 228 | * the asynchronous operations created within the callback.
|
---|
| 229 | *
|
---|
| 230 | * Optionally, arguments can be passed to the function. They will be passed to
|
---|
| 231 | * the callback function.
|
---|
| 232 | *
|
---|
| 233 | * If the callback function throws an error, it will be thrown by
|
---|
| 234 | * `exitSyncAndReturn` too. The stacktrace will not be impacted by this call and
|
---|
| 235 | * the context will be re-entered.
|
---|
| 236 | */
|
---|
| 237 | exitSyncAndReturn<R>(callback: (...args: any[]) => R, ...args: any[]): R;
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * Calling `asyncLocalStorage.enterWith(store)` will transition into the context
|
---|
| 241 | * for the remainder of the current synchronous execution and will persist
|
---|
| 242 | * through any following asynchronous calls.
|
---|
| 243 | */
|
---|
| 244 | enterWith(store: T): void;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|