@types/node 20.11.25 → 20.11.27

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.
Files changed (57) hide show
  1. node/README.md +1 -1
  2. node/crypto.d.ts +1 -1
  3. node/package.json +3 -10
  4. node/ts4.8/assert/strict.d.ts +0 -8
  5. node/ts4.8/assert.d.ts +0 -996
  6. node/ts4.8/async_hooks.d.ts +0 -539
  7. node/ts4.8/buffer.d.ts +0 -2363
  8. node/ts4.8/child_process.d.ts +0 -1540
  9. node/ts4.8/cluster.d.ts +0 -432
  10. node/ts4.8/console.d.ts +0 -415
  11. node/ts4.8/constants.d.ts +0 -19
  12. node/ts4.8/crypto.d.ts +0 -4487
  13. node/ts4.8/dgram.d.ts +0 -596
  14. node/ts4.8/diagnostics_channel.d.ts +0 -545
  15. node/ts4.8/dns/promises.d.ts +0 -425
  16. node/ts4.8/dns.d.ts +0 -809
  17. node/ts4.8/dom-events.d.ts +0 -122
  18. node/ts4.8/domain.d.ts +0 -170
  19. node/ts4.8/events.d.ts +0 -896
  20. node/ts4.8/fs/promises.d.ts +0 -1239
  21. node/ts4.8/fs.d.ts +0 -4311
  22. node/ts4.8/globals.d.ts +0 -411
  23. node/ts4.8/globals.global.d.ts +0 -1
  24. node/ts4.8/http.d.ts +0 -1889
  25. node/ts4.8/http2.d.ts +0 -2382
  26. node/ts4.8/https.d.ts +0 -550
  27. node/ts4.8/index.d.ts +0 -88
  28. node/ts4.8/inspector.d.ts +0 -2747
  29. node/ts4.8/module.d.ts +0 -315
  30. node/ts4.8/net.d.ts +0 -954
  31. node/ts4.8/os.d.ts +0 -478
  32. node/ts4.8/path.d.ts +0 -191
  33. node/ts4.8/perf_hooks.d.ts +0 -645
  34. node/ts4.8/process.d.ts +0 -1561
  35. node/ts4.8/punycode.d.ts +0 -117
  36. node/ts4.8/querystring.d.ts +0 -141
  37. node/ts4.8/readline/promises.d.ts +0 -150
  38. node/ts4.8/readline.d.ts +0 -539
  39. node/ts4.8/repl.d.ts +0 -430
  40. node/ts4.8/stream/consumers.d.ts +0 -12
  41. node/ts4.8/stream/promises.d.ts +0 -83
  42. node/ts4.8/stream/web.d.ts +0 -366
  43. node/ts4.8/stream.d.ts +0 -1701
  44. node/ts4.8/string_decoder.d.ts +0 -67
  45. node/ts4.8/test.d.ts +0 -1465
  46. node/ts4.8/timers/promises.d.ts +0 -93
  47. node/ts4.8/timers.d.ts +0 -240
  48. node/ts4.8/tls.d.ts +0 -1210
  49. node/ts4.8/trace_events.d.ts +0 -182
  50. node/ts4.8/tty.d.ts +0 -208
  51. node/ts4.8/url.d.ts +0 -944
  52. node/ts4.8/util.d.ts +0 -2183
  53. node/ts4.8/v8.d.ts +0 -764
  54. node/ts4.8/vm.d.ts +0 -903
  55. node/ts4.8/wasi.d.ts +0 -179
  56. node/ts4.8/worker_threads.d.ts +0 -691
  57. node/ts4.8/zlib.d.ts +0 -517
node/ts4.8/events.d.ts DELETED
@@ -1,896 +0,0 @@
1
- /**
2
- * Much of the Node.js core API is built around an idiomatic asynchronous
3
- * event-driven architecture in which certain kinds of objects (called "emitters")
4
- * emit named events that cause `Function` objects ("listeners") to be called.
5
- *
6
- * For instance: a `net.Server` object emits an event each time a peer
7
- * connects to it; a `fs.ReadStream` emits an event when the file is opened;
8
- * a `stream` emits an event whenever data is available to be read.
9
- *
10
- * All objects that emit events are instances of the `EventEmitter` class. These
11
- * objects expose an `eventEmitter.on()` function that allows one or more
12
- * functions to be attached to named events emitted by the object. Typically,
13
- * event names are camel-cased strings but any valid JavaScript property key
14
- * can be used.
15
- *
16
- * When the `EventEmitter` object emits an event, all of the functions attached
17
- * to that specific event are called _synchronously_. Any values returned by the
18
- * called listeners are _ignored_ and discarded.
19
- *
20
- * The following example shows a simple `EventEmitter` instance with a single
21
- * listener. The `eventEmitter.on()` method is used to register listeners, while
22
- * the `eventEmitter.emit()` method is used to trigger the event.
23
- *
24
- * ```js
25
- * import { EventEmitter } from 'node:events';
26
- *
27
- * class MyEmitter extends EventEmitter {}
28
- *
29
- * const myEmitter = new MyEmitter();
30
- * myEmitter.on('event', () => {
31
- * console.log('an event occurred!');
32
- * });
33
- * myEmitter.emit('event');
34
- * ```
35
- * @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/events.js)
36
- */
37
- declare module "events" {
38
- import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";
39
- // NOTE: This class is in the docs but is **not actually exported** by Node.
40
- // If https://github.com/nodejs/node/issues/39903 gets resolved and Node
41
- // actually starts exporting the class, uncomment below.
42
- // import { EventListener, EventListenerObject } from '__dom-events';
43
- // /** The NodeEventTarget is a Node.js-specific extension to EventTarget that emulates a subset of the EventEmitter API. */
44
- // interface NodeEventTarget extends EventTarget {
45
- // /**
46
- // * Node.js-specific extension to the `EventTarget` class that emulates the equivalent `EventEmitter` API.
47
- // * The only difference between `addListener()` and `addEventListener()` is that addListener() will return a reference to the EventTarget.
48
- // */
49
- // addListener(type: string, listener: EventListener | EventListenerObject, options?: { once: boolean }): this;
50
- // /** Node.js-specific extension to the `EventTarget` class that returns an array of event `type` names for which event listeners are registered. */
51
- // eventNames(): string[];
52
- // /** Node.js-specific extension to the `EventTarget` class that returns the number of event listeners registered for the `type`. */
53
- // listenerCount(type: string): number;
54
- // /** Node.js-specific alias for `eventTarget.removeListener()`. */
55
- // off(type: string, listener: EventListener | EventListenerObject): this;
56
- // /** Node.js-specific alias for `eventTarget.addListener()`. */
57
- // on(type: string, listener: EventListener | EventListenerObject, options?: { once: boolean }): this;
58
- // /** Node.js-specific extension to the `EventTarget` class that adds a `once` listener for the given event `type`. This is equivalent to calling `on` with the `once` option set to `true`. */
59
- // once(type: string, listener: EventListener | EventListenerObject): this;
60
- // /**
61
- // * Node.js-specific extension to the `EventTarget` class.
62
- // * If `type` is specified, removes all registered listeners for `type`,
63
- // * otherwise removes all registered listeners.
64
- // */
65
- // removeAllListeners(type: string): this;
66
- // /**
67
- // * Node.js-specific extension to the `EventTarget` class that removes the listener for the given `type`.
68
- // * The only difference between `removeListener()` and `removeEventListener()` is that `removeListener()` will return a reference to the `EventTarget`.
69
- // */
70
- // removeListener(type: string, listener: EventListener | EventListenerObject): this;
71
- // }
72
- interface EventEmitterOptions {
73
- /**
74
- * Enables automatic capturing of promise rejection.
75
- */
76
- captureRejections?: boolean | undefined;
77
- }
78
- // Any EventTarget with a Node-style `once` function
79
- interface _NodeEventTarget {
80
- once(eventName: string | symbol, listener: (...args: any[]) => void): this;
81
- }
82
- // Any EventTarget with a DOM-style `addEventListener`
83
- interface _DOMEventTarget {
84
- addEventListener(
85
- eventName: string,
86
- listener: (...args: any[]) => void,
87
- opts?: {
88
- once: boolean;
89
- },
90
- ): any;
91
- }
92
- interface StaticEventEmitterOptions {
93
- signal?: AbortSignal | undefined;
94
- }
95
- interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
96
- type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
97
- type DefaultEventMap = [never];
98
- type AnyRest = [...args: any[]];
99
- type Args<K, T> = T extends DefaultEventMap ? AnyRest : (
100
- K extends keyof T ? T[K] : never
101
- );
102
- type Key<K, T> = T extends DefaultEventMap ? string | symbol : K | keyof T;
103
- type Key2<K, T> = T extends DefaultEventMap ? string | symbol : K & keyof T;
104
- type Listener<K, T, F> = T extends DefaultEventMap ? F : (
105
- K extends keyof T ? (
106
- T[K] extends unknown[] ? (...args: T[K]) => void : never
107
- )
108
- : never
109
- );
110
- type Listener1<K, T> = Listener<K, T, (...args: any[]) => void>;
111
- type Listener2<K, T> = Listener<K, T, Function>;
112
-
113
- /**
114
- * The `EventEmitter` class is defined and exposed by the `node:events` module:
115
- *
116
- * ```js
117
- * import { EventEmitter } from 'node:events';
118
- * ```
119
- *
120
- * All `EventEmitter`s emit the event `'newListener'` when new listeners are
121
- * added and `'removeListener'` when existing listeners are removed.
122
- *
123
- * It supports the following option:
124
- * @since v0.1.26
125
- */
126
- class EventEmitter<T extends EventMap<T> = DefaultEventMap> {
127
- constructor(options?: EventEmitterOptions);
128
-
129
- [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
130
-
131
- /**
132
- * Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
133
- * event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
134
- * The `Promise` will resolve with an array of all the arguments emitted to the
135
- * given event.
136
- *
137
- * This method is intentionally generic and works with the web platform [EventTarget](https://dom.spec.whatwg.org/#interface-eventtarget) interface, which has no special`'error'` event
138
- * semantics and does not listen to the `'error'` event.
139
- *
140
- * ```js
141
- * import { once, EventEmitter } from 'node:events';
142
- * import process from 'node:process';
143
- *
144
- * const ee = new EventEmitter();
145
- *
146
- * process.nextTick(() => {
147
- * ee.emit('myevent', 42);
148
- * });
149
- *
150
- * const [value] = await once(ee, 'myevent');
151
- * console.log(value);
152
- *
153
- * const err = new Error('kaboom');
154
- * process.nextTick(() => {
155
- * ee.emit('error', err);
156
- * });
157
- *
158
- * try {
159
- * await once(ee, 'myevent');
160
- * } catch (err) {
161
- * console.error('error happened', err);
162
- * }
163
- * ```
164
- *
165
- * The special handling of the `'error'` event is only used when `events.once()`is used to wait for another event. If `events.once()` is used to wait for the
166
- * '`error'` event itself, then it is treated as any other kind of event without
167
- * special handling:
168
- *
169
- * ```js
170
- * import { EventEmitter, once } from 'node:events';
171
- *
172
- * const ee = new EventEmitter();
173
- *
174
- * once(ee, 'error')
175
- * .then(([err]) => console.log('ok', err.message))
176
- * .catch((err) => console.error('error', err.message));
177
- *
178
- * ee.emit('error', new Error('boom'));
179
- *
180
- * // Prints: ok boom
181
- * ```
182
- *
183
- * An `AbortSignal` can be used to cancel waiting for the event:
184
- *
185
- * ```js
186
- * import { EventEmitter, once } from 'node:events';
187
- *
188
- * const ee = new EventEmitter();
189
- * const ac = new AbortController();
190
- *
191
- * async function foo(emitter, event, signal) {
192
- * try {
193
- * await once(emitter, event, { signal });
194
- * console.log('event emitted!');
195
- * } catch (error) {
196
- * if (error.name === 'AbortError') {
197
- * console.error('Waiting for the event was canceled!');
198
- * } else {
199
- * console.error('There was an error', error.message);
200
- * }
201
- * }
202
- * }
203
- *
204
- * foo(ee, 'foo', ac.signal);
205
- * ac.abort(); // Abort waiting for the event
206
- * ee.emit('foo'); // Prints: Waiting for the event was canceled!
207
- * ```
208
- * @since v11.13.0, v10.16.0
209
- */
210
- static once(
211
- emitter: _NodeEventTarget,
212
- eventName: string | symbol,
213
- options?: StaticEventEmitterOptions,
214
- ): Promise<any[]>;
215
- static once(emitter: _DOMEventTarget, eventName: string, options?: StaticEventEmitterOptions): Promise<any[]>;
216
- /**
217
- * ```js
218
- * import { on, EventEmitter } from 'node:events';
219
- * import process from 'node:process';
220
- *
221
- * const ee = new EventEmitter();
222
- *
223
- * // Emit later on
224
- * process.nextTick(() => {
225
- * ee.emit('foo', 'bar');
226
- * ee.emit('foo', 42);
227
- * });
228
- *
229
- * for await (const event of on(ee, 'foo')) {
230
- * // The execution of this inner block is synchronous and it
231
- * // processes one event at a time (even with await). Do not use
232
- * // if concurrent execution is required.
233
- * console.log(event); // prints ['bar'] [42]
234
- * }
235
- * // Unreachable here
236
- * ```
237
- *
238
- * Returns an `AsyncIterator` that iterates `eventName` events. It will throw
239
- * if the `EventEmitter` emits `'error'`. It removes all listeners when
240
- * exiting the loop. The `value` returned by each iteration is an array
241
- * composed of the emitted event arguments.
242
- *
243
- * An `AbortSignal` can be used to cancel waiting on events:
244
- *
245
- * ```js
246
- * import { on, EventEmitter } from 'node:events';
247
- * import process from 'node:process';
248
- *
249
- * const ac = new AbortController();
250
- *
251
- * (async () => {
252
- * const ee = new EventEmitter();
253
- *
254
- * // Emit later on
255
- * process.nextTick(() => {
256
- * ee.emit('foo', 'bar');
257
- * ee.emit('foo', 42);
258
- * });
259
- *
260
- * for await (const event of on(ee, 'foo', { signal: ac.signal })) {
261
- * // The execution of this inner block is synchronous and it
262
- * // processes one event at a time (even with await). Do not use
263
- * // if concurrent execution is required.
264
- * console.log(event); // prints ['bar'] [42]
265
- * }
266
- * // Unreachable here
267
- * })();
268
- *
269
- * process.nextTick(() => ac.abort());
270
- * ```
271
- * @since v13.6.0, v12.16.0
272
- * @param eventName The name of the event being listened for
273
- * @return that iterates `eventName` events emitted by the `emitter`
274
- */
275
- static on(
276
- emitter: NodeJS.EventEmitter,
277
- eventName: string,
278
- options?: StaticEventEmitterOptions,
279
- ): AsyncIterableIterator<any>;
280
- /**
281
- * A class method that returns the number of listeners for the given `eventName`registered on the given `emitter`.
282
- *
283
- * ```js
284
- * import { EventEmitter, listenerCount } from 'node:events';
285
- *
286
- * const myEmitter = new EventEmitter();
287
- * myEmitter.on('event', () => {});
288
- * myEmitter.on('event', () => {});
289
- * console.log(listenerCount(myEmitter, 'event'));
290
- * // Prints: 2
291
- * ```
292
- * @since v0.9.12
293
- * @deprecated Since v3.2.0 - Use `listenerCount` instead.
294
- * @param emitter The emitter to query
295
- * @param eventName The event name
296
- */
297
- static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
298
- /**
299
- * Returns a copy of the array of listeners for the event named `eventName`.
300
- *
301
- * For `EventEmitter`s this behaves exactly the same as calling `.listeners` on
302
- * the emitter.
303
- *
304
- * For `EventTarget`s this is the only way to get the event listeners for the
305
- * event target. This is useful for debugging and diagnostic purposes.
306
- *
307
- * ```js
308
- * import { getEventListeners, EventEmitter } from 'node:events';
309
- *
310
- * {
311
- * const ee = new EventEmitter();
312
- * const listener = () => console.log('Events are fun');
313
- * ee.on('foo', listener);
314
- * console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
315
- * }
316
- * {
317
- * const et = new EventTarget();
318
- * const listener = () => console.log('Events are fun');
319
- * et.addEventListener('foo', listener);
320
- * console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
321
- * }
322
- * ```
323
- * @since v15.2.0, v14.17.0
324
- */
325
- static getEventListeners(emitter: _DOMEventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
326
- /**
327
- * Returns the currently set max amount of listeners.
328
- *
329
- * For `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on
330
- * the emitter.
331
- *
332
- * For `EventTarget`s this is the only way to get the max event listeners for the
333
- * event target. If the number of event handlers on a single EventTarget exceeds
334
- * the max set, the EventTarget will print a warning.
335
- *
336
- * ```js
337
- * import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';
338
- *
339
- * {
340
- * const ee = new EventEmitter();
341
- * console.log(getMaxListeners(ee)); // 10
342
- * setMaxListeners(11, ee);
343
- * console.log(getMaxListeners(ee)); // 11
344
- * }
345
- * {
346
- * const et = new EventTarget();
347
- * console.log(getMaxListeners(et)); // 10
348
- * setMaxListeners(11, et);
349
- * console.log(getMaxListeners(et)); // 11
350
- * }
351
- * ```
352
- * @since v19.9.0
353
- */
354
- static getMaxListeners(emitter: _DOMEventTarget | NodeJS.EventEmitter): number;
355
- /**
356
- * ```js
357
- * import { setMaxListeners, EventEmitter } from 'node:events';
358
- *
359
- * const target = new EventTarget();
360
- * const emitter = new EventEmitter();
361
- *
362
- * setMaxListeners(5, target, emitter);
363
- * ```
364
- * @since v15.4.0
365
- * @param n A non-negative number. The maximum number of listeners per `EventTarget` event.
366
- * @param eventsTargets Zero or more {EventTarget} or {EventEmitter} instances. If none are specified, `n` is set as the default max for all newly created {EventTarget} and {EventEmitter}
367
- * objects.
368
- */
369
- static setMaxListeners(n?: number, ...eventTargets: Array<_DOMEventTarget | NodeJS.EventEmitter>): void;
370
- /**
371
- * Listens once to the `abort` event on the provided `signal`.
372
- *
373
- * Listening to the `abort` event on abort signals is unsafe and may
374
- * lead to resource leaks since another third party with the signal can
375
- * call `e.stopImmediatePropagation()`. Unfortunately Node.js cannot change
376
- * this since it would violate the web standard. Additionally, the original
377
- * API makes it easy to forget to remove listeners.
378
- *
379
- * This API allows safely using `AbortSignal`s in Node.js APIs by solving these
380
- * two issues by listening to the event such that `stopImmediatePropagation` does
381
- * not prevent the listener from running.
382
- *
383
- * Returns a disposable so that it may be unsubscribed from more easily.
384
- *
385
- * ```js
386
- * import { addAbortListener } from 'node:events';
387
- *
388
- * function example(signal) {
389
- * let disposable;
390
- * try {
391
- * signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
392
- * disposable = addAbortListener(signal, (e) => {
393
- * // Do something when signal is aborted.
394
- * });
395
- * } finally {
396
- * disposable?.[Symbol.dispose]();
397
- * }
398
- * }
399
- * ```
400
- * @since v20.5.0
401
- * @experimental
402
- * @return Disposable that removes the `abort` listener.
403
- */
404
- static addAbortListener(signal: AbortSignal, resource: (event: Event) => void): Disposable;
405
- /**
406
- * This symbol shall be used to install a listener for only monitoring `'error'`events. Listeners installed using this symbol are called before the regular`'error'` listeners are called.
407
- *
408
- * Installing a listener using this symbol does not change the behavior once an`'error'` event is emitted. Therefore, the process will still crash if no
409
- * regular `'error'` listener is installed.
410
- * @since v13.6.0, v12.17.0
411
- */
412
- static readonly errorMonitor: unique symbol;
413
- /**
414
- * Value: `Symbol.for('nodejs.rejection')`
415
- *
416
- * See how to write a custom `rejection handler`.
417
- * @since v13.4.0, v12.16.0
418
- */
419
- static readonly captureRejectionSymbol: unique symbol;
420
- /**
421
- * Value: [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
422
- *
423
- * Change the default `captureRejections` option on all new `EventEmitter` objects.
424
- * @since v13.4.0, v12.16.0
425
- */
426
- static captureRejections: boolean;
427
- /**
428
- * By default, a maximum of `10` listeners can be registered for any single
429
- * event. This limit can be changed for individual `EventEmitter` instances
430
- * using the `emitter.setMaxListeners(n)` method. To change the default
431
- * for _all_`EventEmitter` instances, the `events.defaultMaxListeners`property can be used. If this value is not a positive number, a `RangeError`is thrown.
432
- *
433
- * Take caution when setting the `events.defaultMaxListeners` because the
434
- * change affects _all_`EventEmitter` instances, including those created before
435
- * the change is made. However, calling `emitter.setMaxListeners(n)` still has
436
- * precedence over `events.defaultMaxListeners`.
437
- *
438
- * This is not a hard limit. The `EventEmitter` instance will allow
439
- * more listeners to be added but will output a trace warning to stderr indicating
440
- * that a "possible EventEmitter memory leak" has been detected. For any single`EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`methods can be used to
441
- * temporarily avoid this warning:
442
- *
443
- * ```js
444
- * import { EventEmitter } from 'node:events';
445
- * const emitter = new EventEmitter();
446
- * emitter.setMaxListeners(emitter.getMaxListeners() + 1);
447
- * emitter.once('event', () => {
448
- * // do stuff
449
- * emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
450
- * });
451
- * ```
452
- *
453
- * The `--trace-warnings` command-line flag can be used to display the
454
- * stack trace for such warnings.
455
- *
456
- * The emitted warning can be inspected with `process.on('warning')` and will
457
- * have the additional `emitter`, `type`, and `count` properties, referring to
458
- * the event emitter instance, the event's name and the number of attached
459
- * listeners, respectively.
460
- * Its `name` property is set to `'MaxListenersExceededWarning'`.
461
- * @since v0.11.2
462
- */
463
- static defaultMaxListeners: number;
464
- }
465
- import internal = require("node:events");
466
- namespace EventEmitter {
467
- // Should just be `export { EventEmitter }`, but that doesn't work in TypeScript 3.4
468
- export { internal as EventEmitter };
469
- export interface Abortable {
470
- /**
471
- * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
472
- */
473
- signal?: AbortSignal | undefined;
474
- }
475
-
476
- export interface EventEmitterReferencingAsyncResource extends AsyncResource {
477
- readonly eventEmitter: EventEmitterAsyncResource;
478
- }
479
-
480
- export interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {
481
- /**
482
- * The type of async event, this is required when instantiating `EventEmitterAsyncResource`
483
- * directly rather than as a child class.
484
- * @default new.target.name if instantiated as a child class.
485
- */
486
- name?: string;
487
- }
488
-
489
- /**
490
- * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that
491
- * require manual async tracking. Specifically, all events emitted by instances
492
- * of `events.EventEmitterAsyncResource` will run within its `async context`.
493
- *
494
- * ```js
495
- * import { EventEmitterAsyncResource, EventEmitter } from 'node:events';
496
- * import { notStrictEqual, strictEqual } from 'node:assert';
497
- * import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';
498
- *
499
- * // Async tracking tooling will identify this as 'Q'.
500
- * const ee1 = new EventEmitterAsyncResource({ name: 'Q' });
501
- *
502
- * // 'foo' listeners will run in the EventEmitters async context.
503
- * ee1.on('foo', () => {
504
- * strictEqual(executionAsyncId(), ee1.asyncId);
505
- * strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
506
- * });
507
- *
508
- * const ee2 = new EventEmitter();
509
- *
510
- * // 'foo' listeners on ordinary EventEmitters that do not track async
511
- * // context, however, run in the same async context as the emit().
512
- * ee2.on('foo', () => {
513
- * notStrictEqual(executionAsyncId(), ee2.asyncId);
514
- * notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
515
- * });
516
- *
517
- * Promise.resolve().then(() => {
518
- * ee1.emit('foo');
519
- * ee2.emit('foo');
520
- * });
521
- * ```
522
- *
523
- * The `EventEmitterAsyncResource` class has the same methods and takes the
524
- * same options as `EventEmitter` and `AsyncResource` themselves.
525
- * @since v17.4.0, v16.14.0
526
- */
527
- export class EventEmitterAsyncResource extends EventEmitter {
528
- /**
529
- * @param options Only optional in child class.
530
- */
531
- constructor(options?: EventEmitterAsyncResourceOptions);
532
- /**
533
- * Call all `destroy` hooks. This should only ever be called once. An error will
534
- * be thrown if it is called more than once. This **must** be manually called. If
535
- * the resource is left to be collected by the GC then the `destroy` hooks will
536
- * never be called.
537
- */
538
- emitDestroy(): void;
539
- /**
540
- * The unique `asyncId` assigned to the resource.
541
- */
542
- readonly asyncId: number;
543
- /**
544
- * The same triggerAsyncId that is passed to the AsyncResource constructor.
545
- */
546
- readonly triggerAsyncId: number;
547
- /**
548
- * The returned `AsyncResource` object has an additional `eventEmitter` property
549
- * that provides a reference to this `EventEmitterAsyncResource`.
550
- */
551
- readonly asyncResource: EventEmitterReferencingAsyncResource;
552
- }
553
- }
554
- global {
555
- namespace NodeJS {
556
- interface EventEmitter<T extends EventMap<T> = DefaultEventMap> {
557
- [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
558
- /**
559
- * Alias for `emitter.on(eventName, listener)`.
560
- * @since v0.1.26
561
- */
562
- addListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
563
- /**
564
- * Adds the `listener` function to the end of the listeners array for the
565
- * event named `eventName`. No checks are made to see if the `listener` has
566
- * already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
567
- * times.
568
- *
569
- * ```js
570
- * server.on('connection', (stream) => {
571
- * console.log('someone connected!');
572
- * });
573
- * ```
574
- *
575
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
576
- *
577
- * By default, event listeners are invoked in the order they are added. The`emitter.prependListener()` method can be used as an alternative to add the
578
- * event listener to the beginning of the listeners array.
579
- *
580
- * ```js
581
- * import { EventEmitter } from 'node:events';
582
- * const myEE = new EventEmitter();
583
- * myEE.on('foo', () => console.log('a'));
584
- * myEE.prependListener('foo', () => console.log('b'));
585
- * myEE.emit('foo');
586
- * // Prints:
587
- * // b
588
- * // a
589
- * ```
590
- * @since v0.1.101
591
- * @param eventName The name of the event.
592
- * @param listener The callback function
593
- */
594
- on<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
595
- /**
596
- * Adds a **one-time**`listener` function for the event named `eventName`. The
597
- * next time `eventName` is triggered, this listener is removed and then invoked.
598
- *
599
- * ```js
600
- * server.once('connection', (stream) => {
601
- * console.log('Ah, we have our first user!');
602
- * });
603
- * ```
604
- *
605
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
606
- *
607
- * By default, event listeners are invoked in the order they are added. The`emitter.prependOnceListener()` method can be used as an alternative to add the
608
- * event listener to the beginning of the listeners array.
609
- *
610
- * ```js
611
- * import { EventEmitter } from 'node:events';
612
- * const myEE = new EventEmitter();
613
- * myEE.once('foo', () => console.log('a'));
614
- * myEE.prependOnceListener('foo', () => console.log('b'));
615
- * myEE.emit('foo');
616
- * // Prints:
617
- * // b
618
- * // a
619
- * ```
620
- * @since v0.3.0
621
- * @param eventName The name of the event.
622
- * @param listener The callback function
623
- */
624
- once<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
625
- /**
626
- * Removes the specified `listener` from the listener array for the event named`eventName`.
627
- *
628
- * ```js
629
- * const callback = (stream) => {
630
- * console.log('someone connected!');
631
- * };
632
- * server.on('connection', callback);
633
- * // ...
634
- * server.removeListener('connection', callback);
635
- * ```
636
- *
637
- * `removeListener()` will remove, at most, one instance of a listener from the
638
- * listener array. If any single listener has been added multiple times to the
639
- * listener array for the specified `eventName`, then `removeListener()` must be
640
- * called multiple times to remove each instance.
641
- *
642
- * Once an event is emitted, all listeners attached to it at the
643
- * time of emitting are called in order. This implies that any`removeListener()` or `removeAllListeners()` calls _after_ emitting and _before_ the last listener finishes execution
644
- * will not remove them from`emit()` in progress. Subsequent events behave as expected.
645
- *
646
- * ```js
647
- * import { EventEmitter } from 'node:events';
648
- * class MyEmitter extends EventEmitter {}
649
- * const myEmitter = new MyEmitter();
650
- *
651
- * const callbackA = () => {
652
- * console.log('A');
653
- * myEmitter.removeListener('event', callbackB);
654
- * };
655
- *
656
- * const callbackB = () => {
657
- * console.log('B');
658
- * };
659
- *
660
- * myEmitter.on('event', callbackA);
661
- *
662
- * myEmitter.on('event', callbackB);
663
- *
664
- * // callbackA removes listener callbackB but it will still be called.
665
- * // Internal listener array at time of emit [callbackA, callbackB]
666
- * myEmitter.emit('event');
667
- * // Prints:
668
- * // A
669
- * // B
670
- *
671
- * // callbackB is now removed.
672
- * // Internal listener array [callbackA]
673
- * myEmitter.emit('event');
674
- * // Prints:
675
- * // A
676
- * ```
677
- *
678
- * Because listeners are managed using an internal array, calling this will
679
- * change the position indices of any listener registered _after_ the listener
680
- * being removed. This will not impact the order in which listeners are called,
681
- * but it means that any copies of the listener array as returned by
682
- * the `emitter.listeners()` method will need to be recreated.
683
- *
684
- * When a single function has been added as a handler multiple times for a single
685
- * event (as in the example below), `removeListener()` will remove the most
686
- * recently added instance. In the example the `once('ping')`listener is removed:
687
- *
688
- * ```js
689
- * import { EventEmitter } from 'node:events';
690
- * const ee = new EventEmitter();
691
- *
692
- * function pong() {
693
- * console.log('pong');
694
- * }
695
- *
696
- * ee.on('ping', pong);
697
- * ee.once('ping', pong);
698
- * ee.removeListener('ping', pong);
699
- *
700
- * ee.emit('ping');
701
- * ee.emit('ping');
702
- * ```
703
- *
704
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
705
- * @since v0.1.26
706
- */
707
- removeListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
708
- /**
709
- * Alias for `emitter.removeListener()`.
710
- * @since v10.0.0
711
- */
712
- off<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
713
- /**
714
- * Removes all listeners, or those of the specified `eventName`.
715
- *
716
- * It is bad practice to remove listeners added elsewhere in the code,
717
- * particularly when the `EventEmitter` instance was created by some other
718
- * component or module (e.g. sockets or file streams).
719
- *
720
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
721
- * @since v0.1.26
722
- */
723
- removeAllListeners(event?: Key<unknown, T>): this;
724
- /**
725
- * By default `EventEmitter`s will print a warning if more than `10` listeners are
726
- * added for a particular event. This is a useful default that helps finding
727
- * memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
728
- * modified for this specific `EventEmitter` instance. The value can be set to`Infinity` (or `0`) to indicate an unlimited number of listeners.
729
- *
730
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
731
- * @since v0.3.5
732
- */
733
- setMaxListeners(n: number): this;
734
- /**
735
- * Returns the current max listener value for the `EventEmitter` which is either
736
- * set by `emitter.setMaxListeners(n)` or defaults to {@link defaultMaxListeners}.
737
- * @since v1.0.0
738
- */
739
- getMaxListeners(): number;
740
- /**
741
- * Returns a copy of the array of listeners for the event named `eventName`.
742
- *
743
- * ```js
744
- * server.on('connection', (stream) => {
745
- * console.log('someone connected!');
746
- * });
747
- * console.log(util.inspect(server.listeners('connection')));
748
- * // Prints: [ [Function] ]
749
- * ```
750
- * @since v0.1.26
751
- */
752
- listeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
753
- /**
754
- * Returns a copy of the array of listeners for the event named `eventName`,
755
- * including any wrappers (such as those created by `.once()`).
756
- *
757
- * ```js
758
- * import { EventEmitter } from 'node:events';
759
- * const emitter = new EventEmitter();
760
- * emitter.once('log', () => console.log('log once'));
761
- *
762
- * // Returns a new Array with a function `onceWrapper` which has a property
763
- * // `listener` which contains the original listener bound above
764
- * const listeners = emitter.rawListeners('log');
765
- * const logFnWrapper = listeners[0];
766
- *
767
- * // Logs "log once" to the console and does not unbind the `once` event
768
- * logFnWrapper.listener();
769
- *
770
- * // Logs "log once" to the console and removes the listener
771
- * logFnWrapper();
772
- *
773
- * emitter.on('log', () => console.log('log persistently'));
774
- * // Will return a new Array with a single function bound by `.on()` above
775
- * const newListeners = emitter.rawListeners('log');
776
- *
777
- * // Logs "log persistently" twice
778
- * newListeners[0]();
779
- * emitter.emit('log');
780
- * ```
781
- * @since v9.4.0
782
- */
783
- rawListeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
784
- /**
785
- * Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
786
- * to each.
787
- *
788
- * Returns `true` if the event had listeners, `false` otherwise.
789
- *
790
- * ```js
791
- * import { EventEmitter } from 'node:events';
792
- * const myEmitter = new EventEmitter();
793
- *
794
- * // First listener
795
- * myEmitter.on('event', function firstListener() {
796
- * console.log('Helloooo! first listener');
797
- * });
798
- * // Second listener
799
- * myEmitter.on('event', function secondListener(arg1, arg2) {
800
- * console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
801
- * });
802
- * // Third listener
803
- * myEmitter.on('event', function thirdListener(...args) {
804
- * const parameters = args.join(', ');
805
- * console.log(`event with parameters ${parameters} in third listener`);
806
- * });
807
- *
808
- * console.log(myEmitter.listeners('event'));
809
- *
810
- * myEmitter.emit('event', 1, 2, 3, 4, 5);
811
- *
812
- * // Prints:
813
- * // [
814
- * // [Function: firstListener],
815
- * // [Function: secondListener],
816
- * // [Function: thirdListener]
817
- * // ]
818
- * // Helloooo! first listener
819
- * // event with parameters 1, 2 in second listener
820
- * // event with parameters 1, 2, 3, 4, 5 in third listener
821
- * ```
822
- * @since v0.1.26
823
- */
824
- emit<K>(eventName: Key<K, T>, ...args: Args<K, T>): boolean;
825
- /**
826
- * Returns the number of listeners listening for the event named `eventName`.
827
- * If `listener` is provided, it will return how many times the listener is found
828
- * in the list of the listeners of the event.
829
- * @since v3.2.0
830
- * @param eventName The name of the event being listened for
831
- * @param listener The event handler function
832
- */
833
- listenerCount<K>(eventName: Key<K, T>, listener?: Listener2<K, T>): number;
834
- /**
835
- * Adds the `listener` function to the _beginning_ of the listeners array for the
836
- * event named `eventName`. No checks are made to see if the `listener` has
837
- * already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
838
- * times.
839
- *
840
- * ```js
841
- * server.prependListener('connection', (stream) => {
842
- * console.log('someone connected!');
843
- * });
844
- * ```
845
- *
846
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
847
- * @since v6.0.0
848
- * @param eventName The name of the event.
849
- * @param listener The callback function
850
- */
851
- prependListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
852
- /**
853
- * Adds a **one-time**`listener` function for the event named `eventName` to the _beginning_ of the listeners array. The next time `eventName` is triggered, this
854
- * listener is removed, and then invoked.
855
- *
856
- * ```js
857
- * server.prependOnceListener('connection', (stream) => {
858
- * console.log('Ah, we have our first user!');
859
- * });
860
- * ```
861
- *
862
- * Returns a reference to the `EventEmitter`, so that calls can be chained.
863
- * @since v6.0.0
864
- * @param eventName The name of the event.
865
- * @param listener The callback function
866
- */
867
- prependOnceListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
868
- /**
869
- * Returns an array listing the events for which the emitter has registered
870
- * listeners. The values in the array are strings or `Symbol`s.
871
- *
872
- * ```js
873
- * import { EventEmitter } from 'node:events';
874
- *
875
- * const myEE = new EventEmitter();
876
- * myEE.on('foo', () => {});
877
- * myEE.on('bar', () => {});
878
- *
879
- * const sym = Symbol('symbol');
880
- * myEE.on(sym, () => {});
881
- *
882
- * console.log(myEE.eventNames());
883
- * // Prints: [ 'foo', 'bar', Symbol(symbol) ]
884
- * ```
885
- * @since v6.0.0
886
- */
887
- eventNames(): Array<(string | symbol) & Key2<unknown, T>>;
888
- }
889
- }
890
- }
891
- export = EventEmitter;
892
- }
893
- declare module "node:events" {
894
- import events = require("events");
895
- export = events;
896
- }