Projekt

Obecné

Profil

Stáhnout (10.6 KB) Statistiky
| Větev: | Revize:
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;
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

    
147
    /**
148
     * When having multiple instances of `AsyncLocalStorage`, they are independent
149
     * from each other. It is safe to instantiate this class multiple times.
150
     */
151
    class AsyncLocalStorage<T> {
152
        /**
153
         * This method disables the instance of `AsyncLocalStorage`. All subsequent calls
154
         * to `asyncLocalStorage.getStore()` will return `undefined` until
155
         * `asyncLocalStorage.run()` or `asyncLocalStorage.runSyncAndReturn()`
156
         * is called again.
157
         *
158
         * When calling `asyncLocalStorage.disable()`, all current contexts linked to the
159
         * instance will be exited.
160
         *
161
         * Calling `asyncLocalStorage.disable()` is required before the
162
         * `asyncLocalStorage` can be garbage collected. This does not apply to stores
163
         * provided by the `asyncLocalStorage`, as those objects are garbage collected
164
         * along with the corresponding async resources.
165
         *
166
         * This method is to be used when the `asyncLocalStorage` is not in use anymore
167
         * in the current process.
168
         */
169
        disable(): void;
170

    
171
        /**
172
         * This method returns the current store.
173
         * If this method is called outside of an asynchronous context initialized by
174
         * calling `asyncLocalStorage.run` or `asyncLocalStorage.runAndReturn`, it will
175
         * return `undefined`.
176
         */
177
        getStore(): T | undefined;
178

    
179
        /**
180
         * Calling `asyncLocalStorage.run(callback)` will create a new asynchronous
181
         * context.
182
         * Within the callback function and the asynchronous operations from the callback,
183
         * `asyncLocalStorage.getStore()` will return an instance of `Map` known as
184
         * "the store". This store will be persistent through the following
185
         * asynchronous calls.
186
         *
187
         * The callback will be ran asynchronously. Optionally, arguments can be passed
188
         * to the function. They will be passed to the callback function.
189
         *
190
         * If an error is thrown by the callback function, it will not be caught by
191
         * a `try/catch` block as the callback is ran in a new asynchronous resource.
192
         * Also, the stacktrace will be impacted by the asynchronous call.
193
         */
194
        // TODO: Apply generic vararg once available
195
        run(store: T, callback: (...args: any[]) => void, ...args: any[]): void;
196

    
197
        /**
198
         * Calling `asyncLocalStorage.exit(callback)` will create a new asynchronous
199
         * context.
200
         * Within the callback function and the asynchronous operations from the callback,
201
         * `asyncLocalStorage.getStore()` will return `undefined`.
202
         *
203
         * The callback will be ran asynchronously. Optionally, arguments can be passed
204
         * to the function. They will be passed to the callback function.
205
         *
206
         * If an error is thrown by the callback function, it will not be caught by
207
         * a `try/catch` block as the callback is ran in a new asynchronous resource.
208
         * Also, the stacktrace will be impacted by the asynchronous call.
209
         */
210
        exit(callback: (...args: any[]) => void, ...args: any[]): void;
211

    
212
        /**
213
         * This methods runs a function synchronously within a context and return its
214
         * return value. The store is not accessible outside of the callback function or
215
         * the asynchronous operations created within the callback.
216
         *
217
         * Optionally, arguments can be passed to the function. They will be passed to
218
         * the callback function.
219
         *
220
         * If the callback function throws an error, it will be thrown by
221
         * `runSyncAndReturn` too. The stacktrace will not be impacted by this call and
222
         * the context will be exited.
223
         */
224
        runSyncAndReturn<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R;
225

    
226
        /**
227
         * This methods runs a function synchronously outside of a context and return its
228
         * return value. The store is not accessible within the callback function or
229
         * the asynchronous operations created within the callback.
230
         *
231
         * Optionally, arguments can be passed to the function. They will be passed to
232
         * the callback function.
233
         *
234
         * If the callback function throws an error, it will be thrown by
235
         * `exitSyncAndReturn` too. The stacktrace will not be impacted by this call and
236
         * the context will be re-entered.
237
         */
238
        exitSyncAndReturn<R>(callback: (...args: any[]) => R, ...args: any[]): R;
239

    
240
        /**
241
         * Calling `asyncLocalStorage.enterWith(store)` will transition into the context
242
         * for the remainder of the current synchronous execution and will persist
243
         * through any following asynchronous calls.
244
         */
245
        enterWith(store: T): void;
246
    }
247
}
(4-4/45)