@types/node 18.19.5 → 18.19.6

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 v18.19/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for node (https://nodejs.org/).
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node/v18.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Sun, 07 Jan 2024 15:35:36 GMT
11
+ * Last updated: Tue, 09 Jan 2024 15:35:40 GMT
12
12
  * Dependencies: [undici-types](https://npmjs.com/package/undici-types)
13
13
 
14
14
  # Credits
@@ -23,6 +23,7 @@
23
23
  * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/diagnostics_channel.js)
24
24
  */
25
25
  declare module "diagnostics_channel" {
26
+ import { AsyncLocalStorage } from "node:async_hooks";
26
27
  /**
27
28
  * Check if there are active subscribers to the named channel. This is helpful if
28
29
  * the message you want to send might be expensive to prepare.
@@ -97,6 +98,36 @@ declare module "diagnostics_channel" {
97
98
  * @returns `true` if the handler was found, `false` otherwise
98
99
  */
99
100
  function unsubscribe(name: string | symbol, onMessage: ChannelListener): boolean;
101
+ /**
102
+ * Creates a `TracingChannel` wrapper for the given `TracingChannel Channels`. If a name is given, the corresponding tracing
103
+ * channels will be created in the form of `tracing:${name}:${eventType}` where`eventType` corresponds to the types of `TracingChannel Channels`.
104
+ *
105
+ * ```js
106
+ * import diagnostics_channel from 'node:diagnostics_channel';
107
+ *
108
+ * const channelsByName = diagnostics_channel.tracingChannel('my-channel');
109
+ *
110
+ * // or...
111
+ *
112
+ * const channelsByCollection = diagnostics_channel.tracingChannel({
113
+ * start: diagnostics_channel.channel('tracing:my-channel:start'),
114
+ * end: diagnostics_channel.channel('tracing:my-channel:end'),
115
+ * asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
116
+ * asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
117
+ * error: diagnostics_channel.channel('tracing:my-channel:error'),
118
+ * });
119
+ * ```
120
+ * @since v19.9.0
121
+ * @experimental
122
+ * @param nameOrChannels Channel name or object containing all the `TracingChannel Channels`
123
+ * @return Collection of channels to trace with
124
+ */
125
+ function tracingChannel<
126
+ StoreType = unknown,
127
+ ContextType extends object = StoreType extends object ? StoreType : object,
128
+ >(
129
+ nameOrChannels: string | TracingChannelCollection<StoreType, ContextType>,
130
+ ): TracingChannel<StoreType, ContextType>;
100
131
  /**
101
132
  * The class `Channel` represents an individual named channel within the data
102
133
  * pipeline. It is use to track subscribers and to publish messages when there
@@ -106,7 +137,7 @@ declare module "diagnostics_channel" {
106
137
  * with `new Channel(name)` is not supported.
107
138
  * @since v15.1.0, v14.17.0
108
139
  */
109
- class Channel {
140
+ class Channel<StoreType = unknown, ContextType = StoreType> {
110
141
  readonly name: string | symbol;
111
142
  /**
112
143
  * Check if there are active subscribers to this channel. This is helpful if
@@ -185,6 +216,329 @@ declare module "diagnostics_channel" {
185
216
  * @return `true` if the handler was found, `false` otherwise.
186
217
  */
187
218
  unsubscribe(onMessage: ChannelListener): void;
219
+ /**
220
+ * When `channel.runStores(context, ...)` is called, the given context data
221
+ * will be applied to any store bound to the channel. If the store has already been
222
+ * bound the previous `transform` function will be replaced with the new one.
223
+ * The `transform` function may be omitted to set the given context data as the
224
+ * context directly.
225
+ *
226
+ * ```js
227
+ * import diagnostics_channel from 'node:diagnostics_channel';
228
+ * import { AsyncLocalStorage } from 'node:async_hooks';
229
+ *
230
+ * const store = new AsyncLocalStorage();
231
+ *
232
+ * const channel = diagnostics_channel.channel('my-channel');
233
+ *
234
+ * channel.bindStore(store, (data) => {
235
+ * return { data };
236
+ * });
237
+ * ```
238
+ * @since v18.19.0
239
+ * @experimental
240
+ * @param store The store to which to bind the context data
241
+ * @param transform Transform context data before setting the store context
242
+ */
243
+ bindStore(store: AsyncLocalStorage<StoreType>, transform?: (context: ContextType) => StoreType): void;
244
+ /**
245
+ * Remove a message handler previously registered to this channel with `channel.bindStore(store)`.
246
+ *
247
+ * ```js
248
+ * import diagnostics_channel from 'node:diagnostics_channel';
249
+ * import { AsyncLocalStorage } from 'node:async_hooks';
250
+ *
251
+ * const store = new AsyncLocalStorage();
252
+ *
253
+ * const channel = diagnostics_channel.channel('my-channel');
254
+ *
255
+ * channel.bindStore(store);
256
+ * channel.unbindStore(store);
257
+ * ```
258
+ * @since v18.19.0
259
+ * @experimental
260
+ * @param store The store to unbind from the channel.
261
+ * @return `true` if the store was found, `false` otherwise.
262
+ */
263
+ unbindStore(store: any): void;
264
+ /**
265
+ * Applies the given data to any AsyncLocalStorage instances bound to the channel
266
+ * for the duration of the given function, then publishes to the channel within
267
+ * the scope of that data is applied to the stores.
268
+ *
269
+ * If a transform function was given to `channel.bindStore(store)` it will be
270
+ * applied to transform the message data before it becomes the context value for
271
+ * the store. The prior storage context is accessible from within the transform
272
+ * function in cases where context linking is required.
273
+ *
274
+ * The context applied to the store should be accessible in any async code which
275
+ * continues from execution which began during the given function, however
276
+ * there are some situations in which `context loss` may occur.
277
+ *
278
+ * ```js
279
+ * import diagnostics_channel from 'node:diagnostics_channel';
280
+ * import { AsyncLocalStorage } from 'node:async_hooks';
281
+ *
282
+ * const store = new AsyncLocalStorage();
283
+ *
284
+ * const channel = diagnostics_channel.channel('my-channel');
285
+ *
286
+ * channel.bindStore(store, (message) => {
287
+ * const parent = store.getStore();
288
+ * return new Span(message, parent);
289
+ * });
290
+ * channel.runStores({ some: 'message' }, () => {
291
+ * store.getStore(); // Span({ some: 'message' })
292
+ * });
293
+ * ```
294
+ * @since v19.9.0
295
+ * @experimental
296
+ * @param context Message to send to subscribers and bind to stores
297
+ * @param fn Handler to run within the entered storage context
298
+ * @param thisArg The receiver to be used for the function call.
299
+ * @param args Optional arguments to pass to the function.
300
+ */
301
+ runStores(): void;
302
+ }
303
+ interface TracingChannelSubscribers<ContextType extends object> {
304
+ start: (message: ContextType) => void;
305
+ end: (
306
+ message: ContextType & {
307
+ error?: unknown;
308
+ result?: unknown;
309
+ },
310
+ ) => void;
311
+ asyncStart: (
312
+ message: ContextType & {
313
+ error?: unknown;
314
+ result?: unknown;
315
+ },
316
+ ) => void;
317
+ asyncEnd: (
318
+ message: ContextType & {
319
+ error?: unknown;
320
+ result?: unknown;
321
+ },
322
+ ) => void;
323
+ error: (
324
+ message: ContextType & {
325
+ error: unknown;
326
+ },
327
+ ) => void;
328
+ }
329
+ interface TracingChannelCollection<StoreType = unknown, ContextType = StoreType> {
330
+ start: Channel<StoreType, ContextType>;
331
+ end: Channel<StoreType, ContextType>;
332
+ asyncStart: Channel<StoreType, ContextType>;
333
+ asyncEnd: Channel<StoreType, ContextType>;
334
+ error: Channel<StoreType, ContextType>;
335
+ }
336
+ /**
337
+ * The class `TracingChannel` is a collection of `TracingChannel Channels` which
338
+ * together express a single traceable action. It is used to formalize and
339
+ * simplify the process of producing events for tracing application flow.{@link tracingChannel} is used to construct a`TracingChannel`. As with `Channel` it is recommended to create and reuse a
340
+ * single `TracingChannel` at the top-level of the file rather than creating them
341
+ * dynamically.
342
+ * @since v18.19.0
343
+ * @experimental
344
+ */
345
+ class TracingChannel<StoreType = unknown, ContextType extends object = {}> implements TracingChannelCollection {
346
+ start: Channel<StoreType, ContextType>;
347
+ end: Channel<StoreType, ContextType>;
348
+ asyncStart: Channel<StoreType, ContextType>;
349
+ asyncEnd: Channel<StoreType, ContextType>;
350
+ error: Channel<StoreType, ContextType>;
351
+ /**
352
+ * Helper to subscribe a collection of functions to the corresponding channels.
353
+ * This is the same as calling `channel.subscribe(onMessage)` on each channel
354
+ * individually.
355
+ *
356
+ * ```js
357
+ * import diagnostics_channel from 'node:diagnostics_channel';
358
+ *
359
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
360
+ *
361
+ * channels.subscribe({
362
+ * start(message) {
363
+ * // Handle start message
364
+ * },
365
+ * end(message) {
366
+ * // Handle end message
367
+ * },
368
+ * asyncStart(message) {
369
+ * // Handle asyncStart message
370
+ * },
371
+ * asyncEnd(message) {
372
+ * // Handle asyncEnd message
373
+ * },
374
+ * error(message) {
375
+ * // Handle error message
376
+ * },
377
+ * });
378
+ * ```
379
+ * @since v18.19.0
380
+ * @experimental
381
+ * @param subscribers Set of `TracingChannel Channels` subscribers
382
+ */
383
+ subscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
384
+ /**
385
+ * Helper to unsubscribe a collection of functions from the corresponding channels.
386
+ * This is the same as calling `channel.unsubscribe(onMessage)` on each channel
387
+ * individually.
388
+ *
389
+ * ```js
390
+ * import diagnostics_channel from 'node:diagnostics_channel';
391
+ *
392
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
393
+ *
394
+ * channels.unsubscribe({
395
+ * start(message) {
396
+ * // Handle start message
397
+ * },
398
+ * end(message) {
399
+ * // Handle end message
400
+ * },
401
+ * asyncStart(message) {
402
+ * // Handle asyncStart message
403
+ * },
404
+ * asyncEnd(message) {
405
+ * // Handle asyncEnd message
406
+ * },
407
+ * error(message) {
408
+ * // Handle error message
409
+ * },
410
+ * });
411
+ * ```
412
+ * @since v18.19.0
413
+ * @experimental
414
+ * @param subscribers Set of `TracingChannel Channels` subscribers
415
+ * @return `true` if all handlers were successfully unsubscribed, and `false` otherwise.
416
+ */
417
+ unsubscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
418
+ /**
419
+ * Trace a synchronous function call. This will always produce a `start event` and `end event` around the execution and may produce an `error event` if the given function throws an error.
420
+ * This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
421
+ * events should have any bound stores set to match this trace context.
422
+ *
423
+ * ```js
424
+ * import diagnostics_channel from 'node:diagnostics_channel';
425
+ *
426
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
427
+ *
428
+ * channels.traceSync(() => {
429
+ * // Do something
430
+ * }, {
431
+ * some: 'thing',
432
+ * });
433
+ * ```
434
+ * @since v18.19.0
435
+ * @experimental
436
+ * @param fn Function to wrap a trace around
437
+ * @param context Shared object to correlate events through
438
+ * @param thisArg The receiver to be used for the function call
439
+ * @param args Optional arguments to pass to the function
440
+ * @return The return value of the given function
441
+ */
442
+ traceSync<ThisArg = any, Args extends any[] = any[]>(
443
+ fn: (this: ThisArg, ...args: Args) => any,
444
+ context?: ContextType,
445
+ thisArg?: ThisArg,
446
+ ...args: Args
447
+ ): void;
448
+ /**
449
+ * Trace a promise-returning function call. This will always produce a `start event` and `end event` around the synchronous portion of the
450
+ * function execution, and will produce an `asyncStart event` and `asyncEnd event` when a promise continuation is reached. It may also
451
+ * produce an `error event` if the given function throws an error or the
452
+ * returned promise rejects. This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
453
+ * events should have any bound stores set to match this trace context.
454
+ *
455
+ * ```js
456
+ * import diagnostics_channel from 'node:diagnostics_channel';
457
+ *
458
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
459
+ *
460
+ * channels.tracePromise(async () => {
461
+ * // Do something
462
+ * }, {
463
+ * some: 'thing',
464
+ * });
465
+ * ```
466
+ * @since v18.19.0
467
+ * @experimental
468
+ * @param fn Promise-returning function to wrap a trace around
469
+ * @param context Shared object to correlate trace events through
470
+ * @param thisArg The receiver to be used for the function call
471
+ * @param args Optional arguments to pass to the function
472
+ * @return Chained from promise returned by the given function
473
+ */
474
+ tracePromise<ThisArg = any, Args extends any[] = any[]>(
475
+ fn: (this: ThisArg, ...args: Args) => Promise<any>,
476
+ context?: ContextType,
477
+ thisArg?: ThisArg,
478
+ ...args: Args
479
+ ): void;
480
+ /**
481
+ * Trace a callback-receiving function call. This will always produce a `start event` and `end event` around the synchronous portion of the
482
+ * function execution, and will produce a `asyncStart event` and `asyncEnd event` around the callback execution. It may also produce an `error event` if the given function throws an error or
483
+ * the returned
484
+ * promise rejects. This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
485
+ * events should have any bound stores set to match this trace context.
486
+ *
487
+ * The `position` will be -1 by default to indicate the final argument should
488
+ * be used as the callback.
489
+ *
490
+ * ```js
491
+ * import diagnostics_channel from 'node:diagnostics_channel';
492
+ *
493
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
494
+ *
495
+ * channels.traceCallback((arg1, callback) => {
496
+ * // Do something
497
+ * callback(null, 'result');
498
+ * }, 1, {
499
+ * some: 'thing',
500
+ * }, thisArg, arg1, callback);
501
+ * ```
502
+ *
503
+ * The callback will also be run with `channel.runStores(context, ...)` which
504
+ * enables context loss recovery in some cases.
505
+ *
506
+ * ```js
507
+ * import diagnostics_channel from 'node:diagnostics_channel';
508
+ * import { AsyncLocalStorage } from 'node:async_hooks';
509
+ *
510
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
511
+ * const myStore = new AsyncLocalStorage();
512
+ *
513
+ * // The start channel sets the initial store data to something
514
+ * // and stores that store data value on the trace context object
515
+ * channels.start.bindStore(myStore, (data) => {
516
+ * const span = new Span(data);
517
+ * data.span = span;
518
+ * return span;
519
+ * });
520
+ *
521
+ * // Then asyncStart can restore from that data it stored previously
522
+ * channels.asyncStart.bindStore(myStore, (data) => {
523
+ * return data.span;
524
+ * });
525
+ * ```
526
+ * @since v18.19.0
527
+ * @experimental
528
+ * @param fn callback using function to wrap a trace around
529
+ * @param position Zero-indexed argument position of expected callback
530
+ * @param context Shared object to correlate trace events through
531
+ * @param thisArg The receiver to be used for the function call
532
+ * @param args Optional arguments to pass to the function
533
+ * @return The return value of the given function
534
+ */
535
+ traceCallback<Fn extends (this: any, ...args: any) => any>(
536
+ fn: Fn,
537
+ position: number | undefined,
538
+ context: ContextType | undefined,
539
+ thisArg: any,
540
+ ...args: Parameters<Fn>
541
+ ): void;
188
542
  }
189
543
  }
190
544
  declare module "node:diagnostics_channel" {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/node",
3
- "version": "18.19.5",
3
+ "version": "18.19.6",
4
4
  "description": "TypeScript definitions for node",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node",
6
6
  "license": "MIT",
@@ -229,7 +229,7 @@
229
229
  "dependencies": {
230
230
  "undici-types": "~5.26.4"
231
231
  },
232
- "typesPublisherContentHash": "dcdc78cd657aa105c570b15ae7596b589ad1e4c097fa5d8d51fe01b075ab1651",
232
+ "typesPublisherContentHash": "c192698b825a6e4bf13508c2587c250a898f477277c678f78c688ca5b31e38a0",
233
233
  "typeScriptVersion": "4.6",
234
234
  "nonNpm": true
235
235
  }
@@ -20,9 +20,10 @@
20
20
  * should generally include the module name to avoid collisions with data from
21
21
  * other modules.
22
22
  * @experimental
23
- * @see [source](https://github.com/nodejs/node/blob/v18.7.0/lib/diagnostics_channel.js)
23
+ * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/diagnostics_channel.js)
24
24
  */
25
25
  declare module "diagnostics_channel" {
26
+ import { AsyncLocalStorage } from "node:async_hooks";
26
27
  /**
27
28
  * Check if there are active subscribers to the named channel. This is helpful if
28
29
  * the message you want to send might be expensive to prepare.
@@ -97,6 +98,36 @@ declare module "diagnostics_channel" {
97
98
  * @returns `true` if the handler was found, `false` otherwise
98
99
  */
99
100
  function unsubscribe(name: string | symbol, onMessage: ChannelListener): boolean;
101
+ /**
102
+ * Creates a `TracingChannel` wrapper for the given `TracingChannel Channels`. If a name is given, the corresponding tracing
103
+ * channels will be created in the form of `tracing:${name}:${eventType}` where`eventType` corresponds to the types of `TracingChannel Channels`.
104
+ *
105
+ * ```js
106
+ * import diagnostics_channel from 'node:diagnostics_channel';
107
+ *
108
+ * const channelsByName = diagnostics_channel.tracingChannel('my-channel');
109
+ *
110
+ * // or...
111
+ *
112
+ * const channelsByCollection = diagnostics_channel.tracingChannel({
113
+ * start: diagnostics_channel.channel('tracing:my-channel:start'),
114
+ * end: diagnostics_channel.channel('tracing:my-channel:end'),
115
+ * asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
116
+ * asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
117
+ * error: diagnostics_channel.channel('tracing:my-channel:error'),
118
+ * });
119
+ * ```
120
+ * @since v19.9.0
121
+ * @experimental
122
+ * @param nameOrChannels Channel name or object containing all the `TracingChannel Channels`
123
+ * @return Collection of channels to trace with
124
+ */
125
+ function tracingChannel<
126
+ StoreType = unknown,
127
+ ContextType extends object = StoreType extends object ? StoreType : object,
128
+ >(
129
+ nameOrChannels: string | TracingChannelCollection<StoreType, ContextType>,
130
+ ): TracingChannel<StoreType, ContextType>;
100
131
  /**
101
132
  * The class `Channel` represents an individual named channel within the data
102
133
  * pipeline. It is use to track subscribers and to publish messages when there
@@ -106,7 +137,7 @@ declare module "diagnostics_channel" {
106
137
  * with `new Channel(name)` is not supported.
107
138
  * @since v15.1.0, v14.17.0
108
139
  */
109
- class Channel {
140
+ class Channel<StoreType = unknown, ContextType = StoreType> {
110
141
  readonly name: string | symbol;
111
142
  /**
112
143
  * Check if there are active subscribers to this channel. This is helpful if
@@ -185,6 +216,329 @@ declare module "diagnostics_channel" {
185
216
  * @return `true` if the handler was found, `false` otherwise.
186
217
  */
187
218
  unsubscribe(onMessage: ChannelListener): void;
219
+ /**
220
+ * When `channel.runStores(context, ...)` is called, the given context data
221
+ * will be applied to any store bound to the channel. If the store has already been
222
+ * bound the previous `transform` function will be replaced with the new one.
223
+ * The `transform` function may be omitted to set the given context data as the
224
+ * context directly.
225
+ *
226
+ * ```js
227
+ * import diagnostics_channel from 'node:diagnostics_channel';
228
+ * import { AsyncLocalStorage } from 'node:async_hooks';
229
+ *
230
+ * const store = new AsyncLocalStorage();
231
+ *
232
+ * const channel = diagnostics_channel.channel('my-channel');
233
+ *
234
+ * channel.bindStore(store, (data) => {
235
+ * return { data };
236
+ * });
237
+ * ```
238
+ * @since v18.19.0
239
+ * @experimental
240
+ * @param store The store to which to bind the context data
241
+ * @param transform Transform context data before setting the store context
242
+ */
243
+ bindStore(store: AsyncLocalStorage<StoreType>, transform?: (context: ContextType) => StoreType): void;
244
+ /**
245
+ * Remove a message handler previously registered to this channel with `channel.bindStore(store)`.
246
+ *
247
+ * ```js
248
+ * import diagnostics_channel from 'node:diagnostics_channel';
249
+ * import { AsyncLocalStorage } from 'node:async_hooks';
250
+ *
251
+ * const store = new AsyncLocalStorage();
252
+ *
253
+ * const channel = diagnostics_channel.channel('my-channel');
254
+ *
255
+ * channel.bindStore(store);
256
+ * channel.unbindStore(store);
257
+ * ```
258
+ * @since v18.19.0
259
+ * @experimental
260
+ * @param store The store to unbind from the channel.
261
+ * @return `true` if the store was found, `false` otherwise.
262
+ */
263
+ unbindStore(store: any): void;
264
+ /**
265
+ * Applies the given data to any AsyncLocalStorage instances bound to the channel
266
+ * for the duration of the given function, then publishes to the channel within
267
+ * the scope of that data is applied to the stores.
268
+ *
269
+ * If a transform function was given to `channel.bindStore(store)` it will be
270
+ * applied to transform the message data before it becomes the context value for
271
+ * the store. The prior storage context is accessible from within the transform
272
+ * function in cases where context linking is required.
273
+ *
274
+ * The context applied to the store should be accessible in any async code which
275
+ * continues from execution which began during the given function, however
276
+ * there are some situations in which `context loss` may occur.
277
+ *
278
+ * ```js
279
+ * import diagnostics_channel from 'node:diagnostics_channel';
280
+ * import { AsyncLocalStorage } from 'node:async_hooks';
281
+ *
282
+ * const store = new AsyncLocalStorage();
283
+ *
284
+ * const channel = diagnostics_channel.channel('my-channel');
285
+ *
286
+ * channel.bindStore(store, (message) => {
287
+ * const parent = store.getStore();
288
+ * return new Span(message, parent);
289
+ * });
290
+ * channel.runStores({ some: 'message' }, () => {
291
+ * store.getStore(); // Span({ some: 'message' })
292
+ * });
293
+ * ```
294
+ * @since v19.9.0
295
+ * @experimental
296
+ * @param context Message to send to subscribers and bind to stores
297
+ * @param fn Handler to run within the entered storage context
298
+ * @param thisArg The receiver to be used for the function call.
299
+ * @param args Optional arguments to pass to the function.
300
+ */
301
+ runStores(): void;
302
+ }
303
+ interface TracingChannelSubscribers<ContextType extends object> {
304
+ start: (message: ContextType) => void;
305
+ end: (
306
+ message: ContextType & {
307
+ error?: unknown;
308
+ result?: unknown;
309
+ },
310
+ ) => void;
311
+ asyncStart: (
312
+ message: ContextType & {
313
+ error?: unknown;
314
+ result?: unknown;
315
+ },
316
+ ) => void;
317
+ asyncEnd: (
318
+ message: ContextType & {
319
+ error?: unknown;
320
+ result?: unknown;
321
+ },
322
+ ) => void;
323
+ error: (
324
+ message: ContextType & {
325
+ error: unknown;
326
+ },
327
+ ) => void;
328
+ }
329
+ interface TracingChannelCollection<StoreType = unknown, ContextType = StoreType> {
330
+ start: Channel<StoreType, ContextType>;
331
+ end: Channel<StoreType, ContextType>;
332
+ asyncStart: Channel<StoreType, ContextType>;
333
+ asyncEnd: Channel<StoreType, ContextType>;
334
+ error: Channel<StoreType, ContextType>;
335
+ }
336
+ /**
337
+ * The class `TracingChannel` is a collection of `TracingChannel Channels` which
338
+ * together express a single traceable action. It is used to formalize and
339
+ * simplify the process of producing events for tracing application flow.{@link tracingChannel} is used to construct a`TracingChannel`. As with `Channel` it is recommended to create and reuse a
340
+ * single `TracingChannel` at the top-level of the file rather than creating them
341
+ * dynamically.
342
+ * @since v18.19.0
343
+ * @experimental
344
+ */
345
+ class TracingChannel<StoreType = unknown, ContextType extends object = {}> implements TracingChannelCollection {
346
+ start: Channel<StoreType, ContextType>;
347
+ end: Channel<StoreType, ContextType>;
348
+ asyncStart: Channel<StoreType, ContextType>;
349
+ asyncEnd: Channel<StoreType, ContextType>;
350
+ error: Channel<StoreType, ContextType>;
351
+ /**
352
+ * Helper to subscribe a collection of functions to the corresponding channels.
353
+ * This is the same as calling `channel.subscribe(onMessage)` on each channel
354
+ * individually.
355
+ *
356
+ * ```js
357
+ * import diagnostics_channel from 'node:diagnostics_channel';
358
+ *
359
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
360
+ *
361
+ * channels.subscribe({
362
+ * start(message) {
363
+ * // Handle start message
364
+ * },
365
+ * end(message) {
366
+ * // Handle end message
367
+ * },
368
+ * asyncStart(message) {
369
+ * // Handle asyncStart message
370
+ * },
371
+ * asyncEnd(message) {
372
+ * // Handle asyncEnd message
373
+ * },
374
+ * error(message) {
375
+ * // Handle error message
376
+ * },
377
+ * });
378
+ * ```
379
+ * @since v18.19.0
380
+ * @experimental
381
+ * @param subscribers Set of `TracingChannel Channels` subscribers
382
+ */
383
+ subscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
384
+ /**
385
+ * Helper to unsubscribe a collection of functions from the corresponding channels.
386
+ * This is the same as calling `channel.unsubscribe(onMessage)` on each channel
387
+ * individually.
388
+ *
389
+ * ```js
390
+ * import diagnostics_channel from 'node:diagnostics_channel';
391
+ *
392
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
393
+ *
394
+ * channels.unsubscribe({
395
+ * start(message) {
396
+ * // Handle start message
397
+ * },
398
+ * end(message) {
399
+ * // Handle end message
400
+ * },
401
+ * asyncStart(message) {
402
+ * // Handle asyncStart message
403
+ * },
404
+ * asyncEnd(message) {
405
+ * // Handle asyncEnd message
406
+ * },
407
+ * error(message) {
408
+ * // Handle error message
409
+ * },
410
+ * });
411
+ * ```
412
+ * @since v18.19.0
413
+ * @experimental
414
+ * @param subscribers Set of `TracingChannel Channels` subscribers
415
+ * @return `true` if all handlers were successfully unsubscribed, and `false` otherwise.
416
+ */
417
+ unsubscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
418
+ /**
419
+ * Trace a synchronous function call. This will always produce a `start event` and `end event` around the execution and may produce an `error event` if the given function throws an error.
420
+ * This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
421
+ * events should have any bound stores set to match this trace context.
422
+ *
423
+ * ```js
424
+ * import diagnostics_channel from 'node:diagnostics_channel';
425
+ *
426
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
427
+ *
428
+ * channels.traceSync(() => {
429
+ * // Do something
430
+ * }, {
431
+ * some: 'thing',
432
+ * });
433
+ * ```
434
+ * @since v18.19.0
435
+ * @experimental
436
+ * @param fn Function to wrap a trace around
437
+ * @param context Shared object to correlate events through
438
+ * @param thisArg The receiver to be used for the function call
439
+ * @param args Optional arguments to pass to the function
440
+ * @return The return value of the given function
441
+ */
442
+ traceSync<ThisArg = any, Args extends any[] = any[]>(
443
+ fn: (this: ThisArg, ...args: Args) => any,
444
+ context?: ContextType,
445
+ thisArg?: ThisArg,
446
+ ...args: Args
447
+ ): void;
448
+ /**
449
+ * Trace a promise-returning function call. This will always produce a `start event` and `end event` around the synchronous portion of the
450
+ * function execution, and will produce an `asyncStart event` and `asyncEnd event` when a promise continuation is reached. It may also
451
+ * produce an `error event` if the given function throws an error or the
452
+ * returned promise rejects. This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
453
+ * events should have any bound stores set to match this trace context.
454
+ *
455
+ * ```js
456
+ * import diagnostics_channel from 'node:diagnostics_channel';
457
+ *
458
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
459
+ *
460
+ * channels.tracePromise(async () => {
461
+ * // Do something
462
+ * }, {
463
+ * some: 'thing',
464
+ * });
465
+ * ```
466
+ * @since v18.19.0
467
+ * @experimental
468
+ * @param fn Promise-returning function to wrap a trace around
469
+ * @param context Shared object to correlate trace events through
470
+ * @param thisArg The receiver to be used for the function call
471
+ * @param args Optional arguments to pass to the function
472
+ * @return Chained from promise returned by the given function
473
+ */
474
+ tracePromise<ThisArg = any, Args extends any[] = any[]>(
475
+ fn: (this: ThisArg, ...args: Args) => Promise<any>,
476
+ context?: ContextType,
477
+ thisArg?: ThisArg,
478
+ ...args: Args
479
+ ): void;
480
+ /**
481
+ * Trace a callback-receiving function call. This will always produce a `start event` and `end event` around the synchronous portion of the
482
+ * function execution, and will produce a `asyncStart event` and `asyncEnd event` around the callback execution. It may also produce an `error event` if the given function throws an error or
483
+ * the returned
484
+ * promise rejects. This will run the given function using `channel.runStores(context, ...)` on the `start` channel which ensures all
485
+ * events should have any bound stores set to match this trace context.
486
+ *
487
+ * The `position` will be -1 by default to indicate the final argument should
488
+ * be used as the callback.
489
+ *
490
+ * ```js
491
+ * import diagnostics_channel from 'node:diagnostics_channel';
492
+ *
493
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
494
+ *
495
+ * channels.traceCallback((arg1, callback) => {
496
+ * // Do something
497
+ * callback(null, 'result');
498
+ * }, 1, {
499
+ * some: 'thing',
500
+ * }, thisArg, arg1, callback);
501
+ * ```
502
+ *
503
+ * The callback will also be run with `channel.runStores(context, ...)` which
504
+ * enables context loss recovery in some cases.
505
+ *
506
+ * ```js
507
+ * import diagnostics_channel from 'node:diagnostics_channel';
508
+ * import { AsyncLocalStorage } from 'node:async_hooks';
509
+ *
510
+ * const channels = diagnostics_channel.tracingChannel('my-channel');
511
+ * const myStore = new AsyncLocalStorage();
512
+ *
513
+ * // The start channel sets the initial store data to something
514
+ * // and stores that store data value on the trace context object
515
+ * channels.start.bindStore(myStore, (data) => {
516
+ * const span = new Span(data);
517
+ * data.span = span;
518
+ * return span;
519
+ * });
520
+ *
521
+ * // Then asyncStart can restore from that data it stored previously
522
+ * channels.asyncStart.bindStore(myStore, (data) => {
523
+ * return data.span;
524
+ * });
525
+ * ```
526
+ * @since v18.19.0
527
+ * @experimental
528
+ * @param fn callback using function to wrap a trace around
529
+ * @param position Zero-indexed argument position of expected callback
530
+ * @param context Shared object to correlate trace events through
531
+ * @param thisArg The receiver to be used for the function call
532
+ * @param args Optional arguments to pass to the function
533
+ * @return The return value of the given function
534
+ */
535
+ traceCallback<Fn extends (this: any, ...args: any) => any>(
536
+ fn: Fn,
537
+ position: number | undefined,
538
+ context: ContextType | undefined,
539
+ thisArg: any,
540
+ ...args: Parameters<Fn>
541
+ ): void;
188
542
  }
189
543
  }
190
544
  declare module "node:diagnostics_channel" {