@types/node 16.3.3 → 16.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- node/README.md +2 -2
- node/assert/strict.d.ts +0 -1
- node/assert.d.ts +1352 -40
- node/async_hooks.d.ts +359 -90
- node/buffer.d.ts +1498 -73
- node/child_process.d.ts +1049 -227
- node/cluster.d.ts +317 -99
- node/console.d.ts +305 -32
- node/crypto.d.ts +3079 -704
- node/dgram.d.ts +446 -55
- node/diagnostics_channel.d.ts +83 -10
- node/dns/promises.d.ts +292 -36
- node/dns.d.ts +410 -97
- node/domain.d.ts +152 -8
- node/events.d.ts +361 -15
- node/fs/promises.d.ts +660 -271
- node/fs.d.ts +2240 -819
- node/http.d.ts +889 -81
- node/http2.d.ts +1520 -459
- node/https.d.ts +261 -11
- node/index.d.ts +26 -1
- node/inspector.d.ts +354 -661
- node/module.d.ts +49 -11
- node/net.d.ts +545 -137
- node/os.d.ts +236 -22
- node/package.json +7 -2
- node/path.d.ts +9 -5
- node/perf_hooks.d.ts +288 -90
- node/process.d.ts +1092 -153
- node/punycode.d.ts +65 -26
- node/querystring.d.ts +107 -8
- node/readline.d.ts +425 -79
- node/repl.d.ts +134 -109
- node/stream/promises.d.ts +15 -44
- node/stream.d.ts +924 -222
- node/string_decoder.d.ts +57 -1
- node/timers/promises.d.ts +97 -9
- node/timers.d.ts +30 -11
- node/tls.d.ts +440 -217
- node/trace_events.d.ts +107 -11
- node/tty.d.ts +159 -19
- node/url.d.ts +732 -18
- node/util.d.ts +1354 -70
- node/v8.d.ts +252 -76
- node/vm.d.ts +377 -29
- node/wasi.d.ts +107 -24
- node/worker_threads.d.ts +493 -130
- node/zlib.d.ts +215 -63
node/async_hooks.d.ts
CHANGED
|
@@ -1,16 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `async_hooks` module provides an API to track asynchronous resources. It
|
|
3
|
+
* can be accessed using:
|
|
4
|
+
*
|
|
5
|
+
* ```js
|
|
6
|
+
* const async_hooks = require('async_hooks');
|
|
7
|
+
* ```
|
|
8
|
+
* @experimental
|
|
9
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/async_hooks.js)
|
|
3
10
|
*/
|
|
4
11
|
declare module 'async_hooks' {
|
|
5
12
|
/**
|
|
6
|
-
*
|
|
13
|
+
* ```js
|
|
14
|
+
* const async_hooks = require('async_hooks');
|
|
15
|
+
*
|
|
16
|
+
* console.log(async_hooks.executionAsyncId()); // 1 - bootstrap
|
|
17
|
+
* fs.open(path, 'r', (err, fd) => {
|
|
18
|
+
* console.log(async_hooks.executionAsyncId()); // 6 - open()
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The ID returned from `executionAsyncId()` is related to execution timing, not
|
|
23
|
+
* causality (which is covered by `triggerAsyncId()`):
|
|
24
|
+
*
|
|
25
|
+
* ```js
|
|
26
|
+
* const server = net.createServer((conn) => {
|
|
27
|
+
* // Returns the ID of the server, not of the new connection, because the
|
|
28
|
+
* // callback runs in the execution scope of the server's MakeCallback().
|
|
29
|
+
* async_hooks.executionAsyncId();
|
|
30
|
+
*
|
|
31
|
+
* }).listen(port, () => {
|
|
32
|
+
* // Returns the ID of a TickObject (process.nextTick()) because all
|
|
33
|
+
* // callbacks passed to .listen() are wrapped in a nextTick().
|
|
34
|
+
* async_hooks.executionAsyncId();
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Promise contexts may not get precise `executionAsyncIds` by default.
|
|
39
|
+
* See the section on `promise execution tracking`.
|
|
40
|
+
* @since v8.1.0
|
|
41
|
+
* @return The `asyncId` of the current execution context. Useful to track when something calls.
|
|
7
42
|
*/
|
|
8
43
|
function executionAsyncId(): number;
|
|
9
|
-
|
|
10
44
|
/**
|
|
11
|
-
* The resource representing the current execution.
|
|
12
|
-
* Useful to store data within the resource.
|
|
13
|
-
*
|
|
14
45
|
* Resource objects returned by `executionAsyncResource()` are most often internal
|
|
15
46
|
* Node.js handle objects with undocumented APIs. Using any functions or properties
|
|
16
47
|
* on the object is likely to crash your application and should be avoided.
|
|
@@ -18,14 +49,70 @@ declare module 'async_hooks' {
|
|
|
18
49
|
* Using `executionAsyncResource()` in the top-level execution context will
|
|
19
50
|
* return an empty object as there is no handle or request object to use,
|
|
20
51
|
* but having an object representing the top-level can be helpful.
|
|
52
|
+
*
|
|
53
|
+
* ```js
|
|
54
|
+
* const { open } = require('fs');
|
|
55
|
+
* const { executionAsyncId, executionAsyncResource } = require('async_hooks');
|
|
56
|
+
*
|
|
57
|
+
* console.log(executionAsyncId(), executionAsyncResource()); // 1 {}
|
|
58
|
+
* open(__filename, 'r', (err, fd) => {
|
|
59
|
+
* console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* This can be used to implement continuation local storage without the
|
|
64
|
+
* use of a tracking `Map` to store the metadata:
|
|
65
|
+
*
|
|
66
|
+
* ```js
|
|
67
|
+
* const { createServer } = require('http');
|
|
68
|
+
* const {
|
|
69
|
+
* executionAsyncId,
|
|
70
|
+
* executionAsyncResource,
|
|
71
|
+
* createHook
|
|
72
|
+
* } = require('async_hooks');
|
|
73
|
+
* const sym = Symbol('state'); // Private symbol to avoid pollution
|
|
74
|
+
*
|
|
75
|
+
* createHook({
|
|
76
|
+
* init(asyncId, type, triggerAsyncId, resource) {
|
|
77
|
+
* const cr = executionAsyncResource();
|
|
78
|
+
* if (cr) {
|
|
79
|
+
* resource[sym] = cr[sym];
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* }).enable();
|
|
83
|
+
*
|
|
84
|
+
* const server = createServer((req, res) => {
|
|
85
|
+
* executionAsyncResource()[sym] = { state: req.url };
|
|
86
|
+
* setTimeout(function() {
|
|
87
|
+
* res.end(JSON.stringify(executionAsyncResource()[sym]));
|
|
88
|
+
* }, 100);
|
|
89
|
+
* }).listen(3000);
|
|
90
|
+
* ```
|
|
91
|
+
* @since v13.9.0, v12.17.0
|
|
92
|
+
* @return The resource representing the current execution. Useful to store data within the resource.
|
|
21
93
|
*/
|
|
22
94
|
function executionAsyncResource(): object;
|
|
23
|
-
|
|
24
95
|
/**
|
|
25
|
-
*
|
|
96
|
+
* ```js
|
|
97
|
+
* const server = net.createServer((conn) => {
|
|
98
|
+
* // The resource that caused (or triggered) this callback to be called
|
|
99
|
+
* // was that of the new connection. Thus the return value of triggerAsyncId()
|
|
100
|
+
* // is the asyncId of "conn".
|
|
101
|
+
* async_hooks.triggerAsyncId();
|
|
102
|
+
*
|
|
103
|
+
* }).listen(port, () => {
|
|
104
|
+
* // Even though all callbacks passed to .listen() are wrapped in a nextTick()
|
|
105
|
+
* // the callback itself exists because the call to the server's .listen()
|
|
106
|
+
* // was made. So the return value would be the ID of the server.
|
|
107
|
+
* async_hooks.triggerAsyncId();
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* Promise contexts may not get valid `triggerAsyncId`s by default. See
|
|
112
|
+
* the section on `promise execution tracking`.
|
|
113
|
+
* @return The ID of the resource responsible for calling the callback that is currently being executed.
|
|
26
114
|
*/
|
|
27
115
|
function triggerAsyncId(): number;
|
|
28
|
-
|
|
29
116
|
interface HookCallbacks {
|
|
30
117
|
/**
|
|
31
118
|
* Called when a class is constructed that has the possibility to emit an asynchronous event.
|
|
@@ -35,73 +122,133 @@ declare module 'async_hooks' {
|
|
|
35
122
|
* @param resource reference to the resource representing the async operation, needs to be released during destroy
|
|
36
123
|
*/
|
|
37
124
|
init?(asyncId: number, type: string, triggerAsyncId: number, resource: object): void;
|
|
38
|
-
|
|
39
125
|
/**
|
|
40
126
|
* When an asynchronous operation is initiated or completes a callback is called to notify the user.
|
|
41
127
|
* The before callback is called just before said callback is executed.
|
|
42
128
|
* @param asyncId the unique identifier assigned to the resource about to execute the callback.
|
|
43
129
|
*/
|
|
44
130
|
before?(asyncId: number): void;
|
|
45
|
-
|
|
46
131
|
/**
|
|
47
132
|
* Called immediately after the callback specified in before is completed.
|
|
48
133
|
* @param asyncId the unique identifier assigned to the resource which has executed the callback.
|
|
49
134
|
*/
|
|
50
135
|
after?(asyncId: number): void;
|
|
51
|
-
|
|
52
136
|
/**
|
|
53
137
|
* Called when a promise has resolve() called. This may not be in the same execution id
|
|
54
138
|
* as the promise itself.
|
|
55
139
|
* @param asyncId the unique id for the promise that was resolve()d.
|
|
56
140
|
*/
|
|
57
141
|
promiseResolve?(asyncId: number): void;
|
|
58
|
-
|
|
59
142
|
/**
|
|
60
143
|
* Called after the resource corresponding to asyncId is destroyed
|
|
61
144
|
* @param asyncId a unique ID for the async resource
|
|
62
145
|
*/
|
|
63
146
|
destroy?(asyncId: number): void;
|
|
64
147
|
}
|
|
65
|
-
|
|
66
148
|
interface AsyncHook {
|
|
67
149
|
/**
|
|
68
150
|
* Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
|
|
69
151
|
*/
|
|
70
152
|
enable(): this;
|
|
71
|
-
|
|
72
153
|
/**
|
|
73
154
|
* 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
155
|
*/
|
|
75
156
|
disable(): this;
|
|
76
157
|
}
|
|
77
|
-
|
|
78
158
|
/**
|
|
79
|
-
* Registers functions to be called for different lifetime events of each async
|
|
80
|
-
*
|
|
81
|
-
*
|
|
159
|
+
* Registers functions to be called for different lifetime events of each async
|
|
160
|
+
* operation.
|
|
161
|
+
*
|
|
162
|
+
* The callbacks `init()`/`before()`/`after()`/`destroy()` are called for the
|
|
163
|
+
* respective asynchronous event during a resource's lifetime.
|
|
164
|
+
*
|
|
165
|
+
* All callbacks are optional. For example, if only resource cleanup needs to
|
|
166
|
+
* be tracked, then only the `destroy` callback needs to be passed. The
|
|
167
|
+
* specifics of all functions that can be passed to `callbacks` is in the `Hook Callbacks` section.
|
|
168
|
+
*
|
|
169
|
+
* ```js
|
|
170
|
+
* const async_hooks = require('async_hooks');
|
|
171
|
+
*
|
|
172
|
+
* const asyncHook = async_hooks.createHook({
|
|
173
|
+
* init(asyncId, type, triggerAsyncId, resource) { },
|
|
174
|
+
* destroy(asyncId) { }
|
|
175
|
+
* });
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* The callbacks will be inherited via the prototype chain:
|
|
179
|
+
*
|
|
180
|
+
* ```js
|
|
181
|
+
* class MyAsyncCallbacks {
|
|
182
|
+
* init(asyncId, type, triggerAsyncId, resource) { }
|
|
183
|
+
* destroy(asyncId) {}
|
|
184
|
+
* }
|
|
185
|
+
*
|
|
186
|
+
* class MyAddedCallbacks extends MyAsyncCallbacks {
|
|
187
|
+
* before(asyncId) { }
|
|
188
|
+
* after(asyncId) { }
|
|
189
|
+
* }
|
|
190
|
+
*
|
|
191
|
+
* const asyncHook = async_hooks.createHook(new MyAddedCallbacks());
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* Because promises are asynchronous resources whose lifecycle is tracked
|
|
195
|
+
* via the async hooks mechanism, the `init()`, `before()`, `after()`, and`destroy()` callbacks _must not_ be async functions that return promises.
|
|
196
|
+
* @since v8.1.0
|
|
197
|
+
* @param callbacks The `Hook Callbacks` to register
|
|
198
|
+
* @return Instance used for disabling and enabling hooks
|
|
82
199
|
*/
|
|
83
200
|
function createHook(options: HookCallbacks): AsyncHook;
|
|
84
|
-
|
|
85
201
|
interface AsyncResourceOptions {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
requireManualDestroy?: boolean | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* The ID of the execution context that created this async event.
|
|
204
|
+
* @default executionAsyncId()
|
|
205
|
+
*/
|
|
206
|
+
triggerAsyncId?: number | undefined;
|
|
207
|
+
/**
|
|
208
|
+
* Disables automatic `emitDestroy` when the object is garbage collected.
|
|
209
|
+
* This usually does not need to be set (even if `emitDestroy` is called
|
|
210
|
+
* manually), unless the resource's `asyncId` is retrieved and the
|
|
211
|
+
* sensitive API's `emitDestroy` is called with it.
|
|
212
|
+
* @default false
|
|
213
|
+
*/
|
|
214
|
+
requireManualDestroy?: boolean | undefined;
|
|
100
215
|
}
|
|
101
|
-
|
|
102
216
|
/**
|
|
103
|
-
* The class AsyncResource
|
|
104
|
-
* Using this users can easily trigger the lifetime events of their
|
|
217
|
+
* The class `AsyncResource` is designed to be extended by the embedder's async
|
|
218
|
+
* resources. Using this, users can easily trigger the lifetime events of their
|
|
219
|
+
* own resources.
|
|
220
|
+
*
|
|
221
|
+
* The `init` hook will trigger when an `AsyncResource` is instantiated.
|
|
222
|
+
*
|
|
223
|
+
* The following is an overview of the `AsyncResource` API.
|
|
224
|
+
*
|
|
225
|
+
* ```js
|
|
226
|
+
* const { AsyncResource, executionAsyncId } = require('async_hooks');
|
|
227
|
+
*
|
|
228
|
+
* // AsyncResource() is meant to be extended. Instantiating a
|
|
229
|
+
* // new AsyncResource() also triggers init. If triggerAsyncId is omitted then
|
|
230
|
+
* // async_hook.executionAsyncId() is used.
|
|
231
|
+
* const asyncResource = new AsyncResource(
|
|
232
|
+
* type, { triggerAsyncId: executionAsyncId(), requireManualDestroy: false }
|
|
233
|
+
* );
|
|
234
|
+
*
|
|
235
|
+
* // Run a function in the execution context of the resource. This will
|
|
236
|
+
* // * establish the context of the resource
|
|
237
|
+
* // * trigger the AsyncHooks before callbacks
|
|
238
|
+
* // * call the provided function `fn` with the supplied arguments
|
|
239
|
+
* // * trigger the AsyncHooks after callbacks
|
|
240
|
+
* // * restore the original execution context
|
|
241
|
+
* asyncResource.runInAsyncScope(fn, thisArg, ...args);
|
|
242
|
+
*
|
|
243
|
+
* // Call AsyncHooks destroy callbacks.
|
|
244
|
+
* asyncResource.emitDestroy();
|
|
245
|
+
*
|
|
246
|
+
* // Return the unique ID assigned to the AsyncResource instance.
|
|
247
|
+
* asyncResource.asyncId();
|
|
248
|
+
*
|
|
249
|
+
* // Return the trigger ID for the AsyncResource instance.
|
|
250
|
+
* asyncResource.triggerAsyncId();
|
|
251
|
+
* ```
|
|
105
252
|
*/
|
|
106
253
|
class AsyncResource {
|
|
107
254
|
/**
|
|
@@ -113,116 +260,238 @@ declare module 'async_hooks' {
|
|
|
113
260
|
* this async event (default: `executionAsyncId()`), or an
|
|
114
261
|
* AsyncResourceOptions object (since 9.3)
|
|
115
262
|
*/
|
|
116
|
-
constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions);
|
|
117
|
-
|
|
263
|
+
constructor(type: string, triggerAsyncId?: number | AsyncResourceOptions);
|
|
118
264
|
/**
|
|
119
265
|
* Binds the given function to the current execution context.
|
|
266
|
+
*
|
|
267
|
+
* The returned function will have an `asyncResource` property referencing
|
|
268
|
+
* the `AsyncResource` to which the function is bound.
|
|
269
|
+
* @since v14.8.0, v12.19.0
|
|
120
270
|
* @param fn The function to bind to the current execution context.
|
|
121
271
|
* @param type An optional name to associate with the underlying `AsyncResource`.
|
|
122
272
|
*/
|
|
123
|
-
static bind<Func extends (this: ThisArg, ...args: any[]) => any, ThisArg>(
|
|
124
|
-
|
|
273
|
+
static bind<Func extends (this: ThisArg, ...args: any[]) => any, ThisArg>(
|
|
274
|
+
fn: Func,
|
|
275
|
+
type?: string,
|
|
276
|
+
thisArg?: ThisArg
|
|
277
|
+
): Func & {
|
|
278
|
+
asyncResource: AsyncResource;
|
|
279
|
+
};
|
|
125
280
|
/**
|
|
126
281
|
* Binds the given function to execute to this `AsyncResource`'s scope.
|
|
282
|
+
*
|
|
283
|
+
* The returned function will have an `asyncResource` property referencing
|
|
284
|
+
* the `AsyncResource` to which the function is bound.
|
|
285
|
+
* @since v14.8.0, v12.19.0
|
|
127
286
|
* @param fn The function to bind to the current `AsyncResource`.
|
|
128
287
|
*/
|
|
129
|
-
bind<Func extends (...args: any[]) => any>(
|
|
130
|
-
|
|
288
|
+
bind<Func extends (...args: any[]) => any>(
|
|
289
|
+
fn: Func
|
|
290
|
+
): Func & {
|
|
291
|
+
asyncResource: AsyncResource;
|
|
292
|
+
};
|
|
131
293
|
/**
|
|
132
|
-
* Call the provided function with the provided arguments in the
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* @param fn The function to call in the execution context of this
|
|
138
|
-
* async resource.
|
|
294
|
+
* Call the provided function with the provided arguments in the execution context
|
|
295
|
+
* of the async resource. This will establish the context, trigger the AsyncHooks
|
|
296
|
+
* before callbacks, call the function, trigger the AsyncHooks after callbacks, and
|
|
297
|
+
* then restore the original execution context.
|
|
298
|
+
* @since v9.6.0
|
|
299
|
+
* @param fn The function to call in the execution context of this async resource.
|
|
139
300
|
* @param thisArg The receiver to be used for the function call.
|
|
140
|
-
* @param args Optional arguments to pass to the function.
|
|
301
|
+
* @param ...args Optional arguments to pass to the function.
|
|
141
302
|
*/
|
|
142
303
|
runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
|
|
143
|
-
|
|
144
304
|
/**
|
|
145
|
-
* Call
|
|
305
|
+
* Call all `destroy` hooks. This should only ever be called once. An error will
|
|
306
|
+
* be thrown if it is called more than once. This **must** be manually called. If
|
|
307
|
+
* the resource is left to be collected by the GC then the `destroy` hooks will
|
|
308
|
+
* never be called.
|
|
309
|
+
* @return A reference to `asyncResource`.
|
|
146
310
|
*/
|
|
147
311
|
emitDestroy(): this;
|
|
148
|
-
|
|
149
312
|
/**
|
|
150
|
-
* @return
|
|
313
|
+
* @return The unique `asyncId` assigned to the resource.
|
|
151
314
|
*/
|
|
152
315
|
asyncId(): number;
|
|
153
|
-
|
|
154
316
|
/**
|
|
155
|
-
*
|
|
317
|
+
*
|
|
318
|
+
* @return The same `triggerAsyncId` that is passed to the `AsyncResource` constructor.
|
|
156
319
|
*/
|
|
157
320
|
triggerAsyncId(): number;
|
|
158
321
|
}
|
|
159
|
-
|
|
160
322
|
/**
|
|
161
|
-
*
|
|
162
|
-
*
|
|
323
|
+
* This class creates stores that stay coherent through asynchronous operations.
|
|
324
|
+
*
|
|
325
|
+
* While you can create your own implementation on top of the `async_hooks` module,`AsyncLocalStorage` should be preferred as it is a performant and memory safe
|
|
326
|
+
* implementation that involves significant optimizations that are non-obvious to
|
|
327
|
+
* implement.
|
|
328
|
+
*
|
|
329
|
+
* The following example uses `AsyncLocalStorage` to build a simple logger
|
|
330
|
+
* that assigns IDs to incoming HTTP requests and includes them in messages
|
|
331
|
+
* logged within each request.
|
|
332
|
+
*
|
|
333
|
+
* ```js
|
|
334
|
+
* const http = require('http');
|
|
335
|
+
* const { AsyncLocalStorage } = require('async_hooks');
|
|
336
|
+
*
|
|
337
|
+
* const asyncLocalStorage = new AsyncLocalStorage();
|
|
338
|
+
*
|
|
339
|
+
* function logWithId(msg) {
|
|
340
|
+
* const id = asyncLocalStorage.getStore();
|
|
341
|
+
* console.log(`${id !== undefined ? id : '-'}:`, msg);
|
|
342
|
+
* }
|
|
343
|
+
*
|
|
344
|
+
* let idSeq = 0;
|
|
345
|
+
* http.createServer((req, res) => {
|
|
346
|
+
* asyncLocalStorage.run(idSeq++, () => {
|
|
347
|
+
* logWithId('start');
|
|
348
|
+
* // Imagine any chain of async operations here
|
|
349
|
+
* setImmediate(() => {
|
|
350
|
+
* logWithId('finish');
|
|
351
|
+
* res.end();
|
|
352
|
+
* });
|
|
353
|
+
* });
|
|
354
|
+
* }).listen(8080);
|
|
355
|
+
*
|
|
356
|
+
* http.get('http://localhost:8080');
|
|
357
|
+
* http.get('http://localhost:8080');
|
|
358
|
+
* // Prints:
|
|
359
|
+
* // 0: start
|
|
360
|
+
* // 1: start
|
|
361
|
+
* // 0: finish
|
|
362
|
+
* // 1: finish
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* Each instance of `AsyncLocalStorage` maintains an independent storage context.
|
|
366
|
+
* Multiple instances can safely exist simultaneously without risk of interfering
|
|
367
|
+
* with each other data.
|
|
368
|
+
* @since v13.10.0, v12.17.0
|
|
163
369
|
*/
|
|
164
370
|
class AsyncLocalStorage<T> {
|
|
165
371
|
/**
|
|
166
|
-
*
|
|
167
|
-
* to `asyncLocalStorage.getStore()` will return `undefined` until
|
|
168
|
-
* `asyncLocalStorage.run()` is called again.
|
|
372
|
+
* Disables the instance of `AsyncLocalStorage`. All subsequent calls
|
|
373
|
+
* to `asyncLocalStorage.getStore()` will return `undefined` until`asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()` is called again.
|
|
169
374
|
*
|
|
170
375
|
* When calling `asyncLocalStorage.disable()`, all current contexts linked to the
|
|
171
376
|
* instance will be exited.
|
|
172
377
|
*
|
|
173
|
-
* Calling `asyncLocalStorage.disable()` is required before the
|
|
174
|
-
* `asyncLocalStorage` can be garbage collected. This does not apply to stores
|
|
378
|
+
* Calling `asyncLocalStorage.disable()` is required before the`asyncLocalStorage` can be garbage collected. This does not apply to stores
|
|
175
379
|
* provided by the `asyncLocalStorage`, as those objects are garbage collected
|
|
176
380
|
* along with the corresponding async resources.
|
|
177
381
|
*
|
|
178
|
-
*
|
|
382
|
+
* Use this method when the `asyncLocalStorage` is not in use anymore
|
|
179
383
|
* in the current process.
|
|
384
|
+
* @since v13.10.0, v12.17.0
|
|
385
|
+
* @experimental
|
|
180
386
|
*/
|
|
181
387
|
disable(): void;
|
|
182
|
-
|
|
183
388
|
/**
|
|
184
|
-
*
|
|
185
|
-
* asynchronous context initialized by
|
|
186
|
-
*
|
|
389
|
+
* Returns the current store.
|
|
390
|
+
* If called outside of an asynchronous context initialized by
|
|
391
|
+
* calling `asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()`, it
|
|
392
|
+
* returns `undefined`.
|
|
393
|
+
* @since v13.10.0, v12.17.0
|
|
187
394
|
*/
|
|
188
395
|
getStore(): T | undefined;
|
|
189
|
-
|
|
190
396
|
/**
|
|
191
|
-
*
|
|
397
|
+
* Runs a function synchronously within a context and returns its
|
|
192
398
|
* return value. The store is not accessible outside of the callback function or
|
|
193
399
|
* the asynchronous operations created within the callback.
|
|
194
400
|
*
|
|
195
|
-
*
|
|
196
|
-
* callback function.
|
|
401
|
+
* The optional `args` are passed to the callback function.
|
|
197
402
|
*
|
|
198
|
-
*
|
|
199
|
-
* stacktrace
|
|
403
|
+
* If the callback function throws an error, the error is thrown by `run()` too.
|
|
404
|
+
* The stacktrace is not impacted by this call and the context is exited.
|
|
405
|
+
*
|
|
406
|
+
* Example:
|
|
407
|
+
*
|
|
408
|
+
* ```js
|
|
409
|
+
* const store = { id: 2 };
|
|
410
|
+
* try {
|
|
411
|
+
* asyncLocalStorage.run(store, () => {
|
|
412
|
+
* asyncLocalStorage.getStore(); // Returns the store object
|
|
413
|
+
* throw new Error();
|
|
414
|
+
* });
|
|
415
|
+
* } catch (e) {
|
|
416
|
+
* asyncLocalStorage.getStore(); // Returns undefined
|
|
417
|
+
* // The error will be caught here
|
|
418
|
+
* }
|
|
419
|
+
* ```
|
|
420
|
+
* @since v13.10.0, v12.17.0
|
|
200
421
|
*/
|
|
201
422
|
run<R, TArgs extends any[]>(store: T, callback: (...args: TArgs) => R, ...args: TArgs): R;
|
|
202
|
-
|
|
203
423
|
/**
|
|
204
|
-
*
|
|
205
|
-
* return value. The store is not accessible within the callback function or
|
|
206
|
-
* asynchronous operations created within the callback.
|
|
424
|
+
* Runs a function synchronously outside of a context and returns its
|
|
425
|
+
* return value. The store is not accessible within the callback function or
|
|
426
|
+
* the asynchronous operations created within the callback. Any `getStore()`call done within the callback function will always return `undefined`.
|
|
427
|
+
*
|
|
428
|
+
* The optional `args` are passed to the callback function.
|
|
207
429
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
430
|
+
* If the callback function throws an error, the error is thrown by `exit()` too.
|
|
431
|
+
* The stacktrace is not impacted by this call and the context is re-entered.
|
|
210
432
|
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
433
|
+
* Example:
|
|
434
|
+
*
|
|
435
|
+
* ```js
|
|
436
|
+
* // Within a call to run
|
|
437
|
+
* try {
|
|
438
|
+
* asyncLocalStorage.getStore(); // Returns the store object or value
|
|
439
|
+
* asyncLocalStorage.exit(() => {
|
|
440
|
+
* asyncLocalStorage.getStore(); // Returns undefined
|
|
441
|
+
* throw new Error();
|
|
442
|
+
* });
|
|
443
|
+
* } catch (e) {
|
|
444
|
+
* asyncLocalStorage.getStore(); // Returns the same object or value
|
|
445
|
+
* // The error will be caught here
|
|
446
|
+
* }
|
|
447
|
+
* ```
|
|
448
|
+
* @since v13.10.0, v12.17.0
|
|
449
|
+
* @experimental
|
|
214
450
|
*/
|
|
215
451
|
exit<R, TArgs extends any[]>(callback: (...args: TArgs) => R, ...args: TArgs): R;
|
|
216
|
-
|
|
217
452
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
453
|
+
* Transitions into the context for the remainder of the current
|
|
454
|
+
* synchronous execution and then persists the store through any following
|
|
455
|
+
* asynchronous calls.
|
|
456
|
+
*
|
|
457
|
+
* Example:
|
|
458
|
+
*
|
|
459
|
+
* ```js
|
|
460
|
+
* const store = { id: 1 };
|
|
461
|
+
* // Replaces previous store with the given store object
|
|
462
|
+
* asyncLocalStorage.enterWith(store);
|
|
463
|
+
* asyncLocalStorage.getStore(); // Returns the store object
|
|
464
|
+
* someAsyncOperation(() => {
|
|
465
|
+
* asyncLocalStorage.getStore(); // Returns the same object
|
|
466
|
+
* });
|
|
467
|
+
* ```
|
|
468
|
+
*
|
|
469
|
+
* This transition will continue for the _entire_ synchronous execution.
|
|
470
|
+
* This means that if, for example, the context is entered within an event
|
|
471
|
+
* handler subsequent event handlers will also run within that context unless
|
|
472
|
+
* specifically bound to another context with an `AsyncResource`. That is why`run()` should be preferred over `enterWith()` unless there are strong reasons
|
|
473
|
+
* to use the latter method.
|
|
474
|
+
*
|
|
475
|
+
* ```js
|
|
476
|
+
* const store = { id: 1 };
|
|
477
|
+
*
|
|
478
|
+
* emitter.on('my-event', () => {
|
|
479
|
+
* asyncLocalStorage.enterWith(store);
|
|
480
|
+
* });
|
|
481
|
+
* emitter.on('my-event', () => {
|
|
482
|
+
* asyncLocalStorage.getStore(); // Returns the same object
|
|
483
|
+
* });
|
|
484
|
+
*
|
|
485
|
+
* asyncLocalStorage.getStore(); // Returns undefined
|
|
486
|
+
* emitter.emit('my-event');
|
|
487
|
+
* asyncLocalStorage.getStore(); // Returns the same object
|
|
488
|
+
* ```
|
|
489
|
+
* @since v13.11.0, v12.17.0
|
|
490
|
+
* @experimental
|
|
221
491
|
*/
|
|
222
492
|
enterWith(store: T): void;
|
|
223
493
|
}
|
|
224
494
|
}
|
|
225
|
-
|
|
226
495
|
declare module 'node:async_hooks' {
|
|
227
496
|
export * from 'async_hooks';
|
|
228
497
|
}
|