@shopify/ui-extensions-server-kit 5.2.1 → 5.3.0

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