cdk-common 2.0.1351 → 2.0.1352

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