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

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