@netlify/plugin-nextjs 4.40.1 → 4.40.2

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 (161) hide show
  1. package/lib/helpers/edge.js +1 -0
  2. package/lib/templates/vendor.js +20 -0
  3. package/package.json +7 -4
  4. package/src/templates/edge/next-dev.js +3 -3
  5. package/src/templates/edge/shims.js +6 -6
  6. package/src/templates/edge-shared/next-utils.ts +3 -3
  7. package/src/templates/edge-shared/utils.ts +1 -1
  8. package/src/templates/vendor/deno.land/std@0.134.0/fmt/colors.ts +536 -0
  9. package/src/templates/vendor/deno.land/std@0.134.0/testing/_diff.ts +360 -0
  10. package/src/templates/vendor/deno.land/std@0.134.0/testing/asserts.ts +866 -0
  11. package/src/templates/vendor/deno.land/std@0.175.0/_util/asserts.ts +25 -0
  12. package/src/templates/vendor/deno.land/std@0.175.0/_util/os.ts +23 -0
  13. package/src/templates/vendor/deno.land/std@0.175.0/async/abortable.ts +149 -0
  14. package/src/templates/vendor/deno.land/std@0.175.0/async/deadline.ts +30 -0
  15. package/src/templates/vendor/deno.land/std@0.175.0/async/debounce.ts +79 -0
  16. package/src/templates/vendor/deno.land/std@0.175.0/async/deferred.ts +48 -0
  17. package/src/templates/vendor/deno.land/std@0.175.0/async/delay.ts +67 -0
  18. package/src/templates/vendor/deno.land/std@0.175.0/async/mod.ts +18 -0
  19. package/src/templates/vendor/deno.land/std@0.175.0/async/mux_async_iterator.ts +97 -0
  20. package/src/templates/vendor/deno.land/std@0.175.0/async/pool.ts +95 -0
  21. package/src/templates/vendor/deno.land/std@0.175.0/async/retry.ts +81 -0
  22. package/src/templates/vendor/deno.land/std@0.175.0/async/tee.ts +100 -0
  23. package/src/templates/vendor/deno.land/std@0.175.0/bytes/index_of_needle.ts +49 -0
  24. package/src/templates/vendor/deno.land/std@0.175.0/crypto/timing_safe_equal.ts +29 -0
  25. package/src/templates/vendor/deno.land/std@0.175.0/datetime/to_imf.ts +45 -0
  26. package/src/templates/vendor/deno.land/std@0.175.0/encoding/base64.ts +144 -0
  27. package/src/templates/vendor/deno.land/std@0.175.0/encoding/base64url.ts +70 -0
  28. package/src/templates/vendor/deno.land/std@0.175.0/flags/mod.ts +785 -0
  29. package/src/templates/vendor/deno.land/std@0.175.0/fmt/colors.ts +569 -0
  30. package/src/templates/vendor/deno.land/std@0.175.0/fmt/printf.ts +939 -0
  31. package/src/templates/vendor/deno.land/std@0.175.0/http/cookie.ts +403 -0
  32. package/src/templates/vendor/deno.land/std@0.175.0/node/_core.ts +77 -0
  33. package/src/templates/vendor/deno.land/std@0.175.0/node/_events.d.ts +848 -0
  34. package/src/templates/vendor/deno.land/std@0.175.0/node/_events.mjs +1033 -0
  35. package/src/templates/vendor/deno.land/std@0.175.0/node/_global.d.ts +66 -0
  36. package/src/templates/vendor/deno.land/std@0.175.0/node/_next_tick.ts +173 -0
  37. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/exiting.ts +4 -0
  38. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/process.ts +131 -0
  39. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/stdio.mjs +7 -0
  40. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/streams.mjs +146 -0
  41. package/src/templates/vendor/deno.land/std@0.175.0/node/_stream.d.ts +1488 -0
  42. package/src/templates/vendor/deno.land/std@0.175.0/node/_stream.mjs +746 -0
  43. package/src/templates/vendor/deno.land/std@0.175.0/node/_util/_util_callbackify.ts +129 -0
  44. package/src/templates/vendor/deno.land/std@0.175.0/node/_utils.ts +206 -0
  45. package/src/templates/vendor/deno.land/std@0.175.0/node/assert.ts +940 -0
  46. package/src/templates/vendor/deno.land/std@0.175.0/node/assertion_error.ts +579 -0
  47. package/src/templates/vendor/deno.land/std@0.175.0/node/async_hooks.ts +331 -0
  48. package/src/templates/vendor/deno.land/std@0.175.0/node/buffer.ts +13 -0
  49. package/src/templates/vendor/deno.land/std@0.175.0/node/events.ts +14 -0
  50. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/buffer.d.ts +2074 -0
  51. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/buffer.mjs +2607 -0
  52. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/crypto/_keys.ts +16 -0
  53. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/crypto/constants.ts +5 -0
  54. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/error_codes.ts +7 -0
  55. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/errors.ts +2867 -0
  56. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/fixed_queue.ts +123 -0
  57. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/hide_stack_frames.ts +16 -0
  58. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/net.ts +95 -0
  59. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/normalize_encoding.mjs +72 -0
  60. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/options.ts +45 -0
  61. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/primordials.mjs +30 -0
  62. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/process/per_thread.mjs +272 -0
  63. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/readline/callbacks.mjs +137 -0
  64. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/readline/utils.mjs +580 -0
  65. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/destroy.mjs +320 -0
  66. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/end-of-stream.mjs +229 -0
  67. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/utils.mjs +242 -0
  68. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/comparisons.ts +669 -0
  69. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/debuglog.ts +118 -0
  70. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/inspect.mjs +2237 -0
  71. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/types.ts +113 -0
  72. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util.mjs +143 -0
  73. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/validators.mjs +317 -0
  74. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_libuv_winerror.ts +229 -0
  75. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_listen.ts +16 -0
  76. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_node.ts +18 -0
  77. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_timingSafeEqual.ts +12 -0
  78. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_utils.ts +86 -0
  79. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_winerror.ts +16873 -0
  80. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/ares.ts +66 -0
  81. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/async_wrap.ts +152 -0
  82. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/buffer.ts +130 -0
  83. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/cares_wrap.ts +541 -0
  84. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/config.ts +3 -0
  85. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/connection_wrap.ts +80 -0
  86. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/constants.ts +900 -0
  87. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/contextify.ts +3 -0
  88. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/credentials.ts +3 -0
  89. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/crypto.ts +14 -0
  90. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/errors.ts +3 -0
  91. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs.ts +3 -0
  92. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs_dir.ts +3 -0
  93. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs_event_wrap.ts +3 -0
  94. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/handle_wrap.ts +50 -0
  95. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/heap_utils.ts +3 -0
  96. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/http_parser.ts +3 -0
  97. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/icu.ts +3 -0
  98. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/inspector.ts +3 -0
  99. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/js_stream.ts +3 -0
  100. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/messaging.ts +3 -0
  101. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/mod.ts +108 -0
  102. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/module_wrap.ts +3 -0
  103. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/native_module.ts +3 -0
  104. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/natives.ts +3 -0
  105. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/node_file.ts +84 -0
  106. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/node_options.ts +39 -0
  107. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/options.ts +3 -0
  108. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/os.ts +3 -0
  109. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/performance.ts +3 -0
  110. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/pipe_wrap.ts +392 -0
  111. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/process_methods.ts +3 -0
  112. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/report.ts +3 -0
  113. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/serdes.ts +3 -0
  114. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/signal_wrap.ts +3 -0
  115. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/spawn_sync.ts +3 -0
  116. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/stream_wrap.ts +354 -0
  117. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/string_decoder.ts +15 -0
  118. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/symbols.ts +27 -0
  119. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/task_queue.ts +3 -0
  120. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tcp_wrap.ts +488 -0
  121. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/timers.ts +3 -0
  122. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tls_wrap.ts +3 -0
  123. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/trace_events.ts +3 -0
  124. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tty_wrap.ts +3 -0
  125. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/types.ts +186 -0
  126. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/udp_wrap.ts +496 -0
  127. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/url.ts +3 -0
  128. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/util.ts +126 -0
  129. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/uv.ts +437 -0
  130. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/v8.ts +3 -0
  131. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/worker.ts +3 -0
  132. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/zlib.ts +3 -0
  133. package/src/templates/vendor/deno.land/std@0.175.0/node/process.ts +705 -0
  134. package/src/templates/vendor/deno.land/std@0.175.0/node/stream.ts +37 -0
  135. package/src/templates/vendor/deno.land/std@0.175.0/node/string_decoder.ts +337 -0
  136. package/src/templates/vendor/deno.land/std@0.175.0/node/util/types.ts +4 -0
  137. package/src/templates/vendor/deno.land/std@0.175.0/node/util.ts +289 -0
  138. package/src/templates/vendor/deno.land/std@0.175.0/path/_constants.ts +49 -0
  139. package/src/templates/vendor/deno.land/std@0.175.0/path/_interface.ts +30 -0
  140. package/src/templates/vendor/deno.land/std@0.175.0/path/_util.ts +194 -0
  141. package/src/templates/vendor/deno.land/std@0.175.0/path/common.ts +40 -0
  142. package/src/templates/vendor/deno.land/std@0.175.0/path/glob.ts +418 -0
  143. package/src/templates/vendor/deno.land/std@0.175.0/path/mod.ts +53 -0
  144. package/src/templates/vendor/deno.land/std@0.175.0/path/posix.ts +487 -0
  145. package/src/templates/vendor/deno.land/std@0.175.0/path/separator.ts +7 -0
  146. package/src/templates/vendor/deno.land/std@0.175.0/path/win32.ts +962 -0
  147. package/src/templates/vendor/deno.land/std@0.175.0/streams/write_all.ts +64 -0
  148. package/src/templates/vendor/deno.land/std@0.175.0/testing/_diff.ts +440 -0
  149. package/src/templates/vendor/deno.land/std@0.175.0/testing/_format.ts +23 -0
  150. package/src/templates/vendor/deno.land/std@0.175.0/testing/asserts.ts +906 -0
  151. package/src/templates/vendor/deno.land/std@0.175.0/types.d.ts +89 -0
  152. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts +133 -0
  153. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/asyncify.js +112 -0
  154. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/html_rewriter.d.ts +88 -0
  155. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/html_rewriter.js +974 -0
  156. package/src/templates/vendor/deno.land/x/path_to_regexp@v6.2.1/index.ts +621 -0
  157. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/compiled/cookie.js +13 -0
  158. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/server/web/spec-extension/request.js +12 -0
  159. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/server/web/spec-extension/response.js +5 -0
  160. package/src/templates/vendor/import_map.json +13 -0
  161. package/src/templates/vendor/raw.githubusercontent.com/worker-tools/resolvable-promise/master/index.ts +50 -0
@@ -0,0 +1,1033 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // Copyright Joyent, Inc. and other Node contributors.
3
+ //
4
+ // Permission is hereby granted, free of charge, to any person obtaining a
5
+ // copy of this software and associated documentation files (the
6
+ // "Software"), to deal in the Software without restriction, including
7
+ // without limitation the rights to use, copy, modify, merge, publish,
8
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
9
+ // persons to whom the Software is furnished to do so, subject to the
10
+ // following conditions:
11
+ //
12
+ // The above copyright notice and this permission notice shall be included
13
+ // in all copies or substantial portions of the Software.
14
+ //
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ "use strict";
24
+
25
+ const kRejection = Symbol.for("nodejs.rejection");
26
+
27
+ import { inspect } from "./internal/util/inspect.mjs";
28
+ import {
29
+ AbortError,
30
+ // kEnhanceStackBeforeInspector,
31
+ ERR_INVALID_ARG_TYPE,
32
+ ERR_OUT_OF_RANGE,
33
+ ERR_UNHANDLED_ERROR,
34
+ } from "./internal/errors.ts";
35
+
36
+ import {
37
+ validateAbortSignal,
38
+ validateBoolean,
39
+ validateFunction,
40
+ } from "./internal/validators.mjs";
41
+ import { spliceOne } from "./_utils.ts";
42
+
43
+ const kCapture = Symbol("kCapture");
44
+ const kErrorMonitor = Symbol("events.errorMonitor");
45
+ const kMaxEventTargetListeners = Symbol("events.maxEventTargetListeners");
46
+ const kMaxEventTargetListenersWarned = Symbol(
47
+ "events.maxEventTargetListenersWarned",
48
+ );
49
+
50
+ /**
51
+ * Creates a new `EventEmitter` instance.
52
+ * @param {{ captureRejections?: boolean; }} [opts]
53
+ * @returns {EventEmitter}
54
+ */
55
+ export function EventEmitter(opts) {
56
+ EventEmitter.init.call(this, opts);
57
+ }
58
+ export default EventEmitter;
59
+ EventEmitter.on = on;
60
+ EventEmitter.once = once;
61
+ EventEmitter.getEventListeners = getEventListeners;
62
+ EventEmitter.setMaxListeners = setMaxListeners;
63
+ EventEmitter.listenerCount = listenerCount;
64
+ // Backwards-compat with node 0.10.x
65
+ EventEmitter.EventEmitter = EventEmitter;
66
+ EventEmitter.usingDomains = false;
67
+
68
+ EventEmitter.captureRejectionSymbol = kRejection;
69
+ export const captureRejectionSymbol = EventEmitter.captureRejectionSymbol;
70
+ export const errorMonitor = EventEmitter.errorMonitor;
71
+
72
+ Object.defineProperty(EventEmitter, "captureRejections", {
73
+ get() {
74
+ return EventEmitter.prototype[kCapture];
75
+ },
76
+ set(value) {
77
+ validateBoolean(value, "EventEmitter.captureRejections");
78
+
79
+ EventEmitter.prototype[kCapture] = value;
80
+ },
81
+ enumerable: true,
82
+ });
83
+
84
+ EventEmitter.errorMonitor = kErrorMonitor;
85
+
86
+ // The default for captureRejections is false
87
+ Object.defineProperty(EventEmitter.prototype, kCapture, {
88
+ value: false,
89
+ writable: true,
90
+ enumerable: false,
91
+ });
92
+
93
+ EventEmitter.prototype._events = undefined;
94
+ EventEmitter.prototype._eventsCount = 0;
95
+ EventEmitter.prototype._maxListeners = undefined;
96
+
97
+ // By default EventEmitters will print a warning if more than 10 listeners are
98
+ // added to it. This is a useful default which helps finding memory leaks.
99
+ export let defaultMaxListeners = 10;
100
+
101
+ function checkListener(listener) {
102
+ validateFunction(listener, "listener");
103
+ }
104
+
105
+ Object.defineProperty(EventEmitter, "defaultMaxListeners", {
106
+ enumerable: true,
107
+ get: function () {
108
+ return defaultMaxListeners;
109
+ },
110
+ set: function (arg) {
111
+ if (typeof arg !== "number" || arg < 0 || Number.isNaN(arg)) {
112
+ throw new ERR_OUT_OF_RANGE(
113
+ "defaultMaxListeners",
114
+ "a non-negative number",
115
+ arg,
116
+ );
117
+ }
118
+ defaultMaxListeners = arg;
119
+ },
120
+ });
121
+
122
+ Object.defineProperties(EventEmitter, {
123
+ kMaxEventTargetListeners: {
124
+ value: kMaxEventTargetListeners,
125
+ enumerable: false,
126
+ configurable: false,
127
+ writable: false,
128
+ },
129
+ kMaxEventTargetListenersWarned: {
130
+ value: kMaxEventTargetListenersWarned,
131
+ enumerable: false,
132
+ configurable: false,
133
+ writable: false,
134
+ },
135
+ });
136
+
137
+ /**
138
+ * Sets the max listeners.
139
+ * @param {number} n
140
+ * @param {EventTarget[] | EventEmitter[]} [eventTargets]
141
+ * @returns {void}
142
+ */
143
+ export function setMaxListeners(
144
+ n = defaultMaxListeners,
145
+ ...eventTargets
146
+ ) {
147
+ if (typeof n !== "number" || n < 0 || Number.isNaN(n)) {
148
+ throw new ERR_OUT_OF_RANGE("n", "a non-negative number", n);
149
+ }
150
+ if (eventTargets.length === 0) {
151
+ defaultMaxListeners = n;
152
+ } else {
153
+ for (let i = 0; i < eventTargets.length; i++) {
154
+ const target = eventTargets[i];
155
+ if (target instanceof EventTarget) {
156
+ target[kMaxEventTargetListeners] = n;
157
+ target[kMaxEventTargetListenersWarned] = false;
158
+ } else if (typeof target.setMaxListeners === "function") {
159
+ target.setMaxListeners(n);
160
+ } else {
161
+ throw new ERR_INVALID_ARG_TYPE(
162
+ "eventTargets",
163
+ ["EventEmitter", "EventTarget"],
164
+ target,
165
+ );
166
+ }
167
+ }
168
+ }
169
+ }
170
+
171
+ EventEmitter.init = function (opts) {
172
+ if (
173
+ this._events === undefined ||
174
+ this._events === Object.getPrototypeOf(this)._events
175
+ ) {
176
+ this._events = Object.create(null);
177
+ this._eventsCount = 0;
178
+ }
179
+
180
+ this._maxListeners = this._maxListeners || undefined;
181
+
182
+ if (opts?.captureRejections) {
183
+ validateBoolean(opts.captureRejections, "options.captureRejections");
184
+ this[kCapture] = Boolean(opts.captureRejections);
185
+ } else {
186
+ // Assigning the kCapture property directly saves an expensive
187
+ // prototype lookup in a very sensitive hot path.
188
+ this[kCapture] = EventEmitter.prototype[kCapture];
189
+ }
190
+ };
191
+
192
+ function addCatch(that, promise, type, args) {
193
+ if (!that[kCapture]) {
194
+ return;
195
+ }
196
+
197
+ // Handle Promises/A+ spec, then could be a getter
198
+ // that throws on second use.
199
+ try {
200
+ const then = promise.then;
201
+
202
+ if (typeof then === "function") {
203
+ then.call(promise, undefined, function (err) {
204
+ // The callback is called with nextTick to avoid a follow-up
205
+ // rejection from this promise.
206
+ process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
207
+ });
208
+ }
209
+ } catch (err) {
210
+ that.emit("error", err);
211
+ }
212
+ }
213
+
214
+ function emitUnhandledRejectionOrErr(ee, err, type, args) {
215
+ if (typeof ee[kRejection] === "function") {
216
+ ee[kRejection](err, type, ...args);
217
+ } else {
218
+ // We have to disable the capture rejections mechanism, otherwise
219
+ // we might end up in an infinite loop.
220
+ const prev = ee[kCapture];
221
+
222
+ // If the error handler throws, it is not catcheable and it
223
+ // will end up in 'uncaughtException'. We restore the previous
224
+ // value of kCapture in case the uncaughtException is present
225
+ // and the exception is handled.
226
+ try {
227
+ ee[kCapture] = false;
228
+ ee.emit("error", err);
229
+ } finally {
230
+ ee[kCapture] = prev;
231
+ }
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Increases the max listeners of the event emitter.
237
+ * @param {number} n
238
+ * @returns {EventEmitter}
239
+ */
240
+ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
241
+ if (typeof n !== "number" || n < 0 || Number.isNaN(n)) {
242
+ throw new ERR_OUT_OF_RANGE("n", "a non-negative number", n);
243
+ }
244
+ this._maxListeners = n;
245
+ return this;
246
+ };
247
+
248
+ function _getMaxListeners(that) {
249
+ if (that._maxListeners === undefined) {
250
+ return EventEmitter.defaultMaxListeners;
251
+ }
252
+ return that._maxListeners;
253
+ }
254
+
255
+ /**
256
+ * Returns the current max listener value for the event emitter.
257
+ * @returns {number}
258
+ */
259
+ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
260
+ return _getMaxListeners(this);
261
+ };
262
+
263
+ // Returns the length and line number of the first sequence of `a` that fully
264
+ // appears in `b` with a length of at least 4.
265
+ function identicalSequenceRange(a, b) {
266
+ for (let i = 0; i < a.length - 3; i++) {
267
+ // Find the first entry of b that matches the current entry of a.
268
+ const pos = b.indexOf(a[i]);
269
+ if (pos !== -1) {
270
+ const rest = b.length - pos;
271
+ if (rest > 3) {
272
+ let len = 1;
273
+ const maxLen = Math.min(a.length - i, rest);
274
+ // Count the number of consecutive entries.
275
+ while (maxLen > len && a[i + len] === b[pos + len]) {
276
+ len++;
277
+ }
278
+ if (len > 3) {
279
+ return [len, i];
280
+ }
281
+ }
282
+ }
283
+ }
284
+
285
+ return [0, 0];
286
+ }
287
+
288
+ // deno-lint-ignore no-unused-vars
289
+ function enhanceStackTrace(err, own) {
290
+ let ctorInfo = "";
291
+ try {
292
+ const { name } = this.constructor;
293
+ if (name !== "EventEmitter") {
294
+ ctorInfo = ` on ${name} instance`;
295
+ }
296
+ } catch {
297
+ // pass
298
+ }
299
+ const sep = `\nEmitted 'error' event${ctorInfo} at:\n`;
300
+
301
+ const errStack = err.stack.split("\n").slice(1);
302
+ const ownStack = own.stack.split("\n").slice(1);
303
+
304
+ const { 0: len, 1: off } = identicalSequenceRange(ownStack, errStack);
305
+ if (len > 0) {
306
+ ownStack.splice(
307
+ off + 1,
308
+ len - 2,
309
+ " [... lines matching original stack trace ...]",
310
+ );
311
+ }
312
+
313
+ return err.stack + sep + ownStack.join("\n");
314
+ }
315
+
316
+ /**
317
+ * Synchronously calls each of the listeners registered
318
+ * for the event.
319
+ * @param {string | symbol} type
320
+ * @param {...any} [args]
321
+ * @returns {boolean}
322
+ */
323
+ EventEmitter.prototype.emit = function emit(type, ...args) {
324
+ let doError = type === "error";
325
+
326
+ const events = this._events;
327
+ if (events !== undefined) {
328
+ if (doError && events[kErrorMonitor] !== undefined) {
329
+ this.emit(kErrorMonitor, ...args);
330
+ }
331
+ doError = doError && events.error === undefined;
332
+ } else if (!doError) {
333
+ return false;
334
+ }
335
+
336
+ // If there is no 'error' event listener then throw.
337
+ if (doError) {
338
+ let er;
339
+ if (args.length > 0) {
340
+ er = args[0];
341
+ }
342
+ if (er instanceof Error) {
343
+ try {
344
+ const capture = {};
345
+ Error.captureStackTrace(capture, EventEmitter.prototype.emit);
346
+ // Object.defineProperty(er, kEnhanceStackBeforeInspector, {
347
+ // value: enhanceStackTrace.bind(this, er, capture),
348
+ // configurable: true
349
+ // });
350
+ } catch {
351
+ // pass
352
+ }
353
+
354
+ // Note: The comments on the `throw` lines are intentional, they show
355
+ // up in Node's output if this results in an unhandled exception.
356
+ throw er; // Unhandled 'error' event
357
+ }
358
+
359
+ let stringifiedEr;
360
+ try {
361
+ stringifiedEr = inspect(er);
362
+ } catch {
363
+ stringifiedEr = er;
364
+ }
365
+
366
+ // At least give some kind of context to the user
367
+ const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
368
+ err.context = er;
369
+ throw err; // Unhandled 'error' event
370
+ }
371
+
372
+ const handler = events[type];
373
+
374
+ if (handler === undefined) {
375
+ return false;
376
+ }
377
+
378
+ if (typeof handler === "function") {
379
+ const result = handler.apply(this, args);
380
+
381
+ // We check if result is undefined first because that
382
+ // is the most common case so we do not pay any perf
383
+ // penalty
384
+ if (result !== undefined && result !== null) {
385
+ addCatch(this, result, type, args);
386
+ }
387
+ } else {
388
+ const len = handler.length;
389
+ const listeners = arrayClone(handler);
390
+ for (let i = 0; i < len; ++i) {
391
+ const result = listeners[i].apply(this, args);
392
+
393
+ // We check if result is undefined first because that
394
+ // is the most common case so we do not pay any perf
395
+ // penalty.
396
+ // This code is duplicated because extracting it away
397
+ // would make it non-inlineable.
398
+ if (result !== undefined && result !== null) {
399
+ addCatch(this, result, type, args);
400
+ }
401
+ }
402
+ }
403
+
404
+ return true;
405
+ };
406
+
407
+ function _addListener(target, type, listener, prepend) {
408
+ let m;
409
+ let events;
410
+ let existing;
411
+
412
+ checkListener(listener);
413
+
414
+ events = target._events;
415
+ if (events === undefined) {
416
+ events = target._events = Object.create(null);
417
+ target._eventsCount = 0;
418
+ } else {
419
+ // To avoid recursion in the case that type === "newListener"! Before
420
+ // adding it to the listeners, first emit "newListener".
421
+ if (events.newListener !== undefined) {
422
+ target.emit("newListener", type, listener.listener ?? listener);
423
+
424
+ // Re-assign `events` because a newListener handler could have caused the
425
+ // this._events to be assigned to a new object
426
+ events = target._events;
427
+ }
428
+ existing = events[type];
429
+ }
430
+
431
+ if (existing === undefined) {
432
+ // Optimize the case of one listener. Don't need the extra array object.
433
+ events[type] = listener;
434
+ ++target._eventsCount;
435
+ } else {
436
+ if (typeof existing === "function") {
437
+ // Adding the second element, need to change to array.
438
+ existing = events[type] = prepend
439
+ ? [listener, existing]
440
+ : [existing, listener];
441
+ // If we've already got an array, just append.
442
+ } else if (prepend) {
443
+ existing.unshift(listener);
444
+ } else {
445
+ existing.push(listener);
446
+ }
447
+
448
+ // Check for listener leak
449
+ m = _getMaxListeners(target);
450
+ if (m > 0 && existing.length > m && !existing.warned) {
451
+ existing.warned = true;
452
+ // No error code for this since it is a Warning
453
+ // eslint-disable-next-line no-restricted-syntax
454
+ const w = new Error(
455
+ "Possible EventEmitter memory leak detected. " +
456
+ `${existing.length} ${String(type)} listeners ` +
457
+ `added to ${inspect(target, { depth: -1 })}. Use ` +
458
+ "emitter.setMaxListeners() to increase limit",
459
+ );
460
+ w.name = "MaxListenersExceededWarning";
461
+ w.emitter = target;
462
+ w.type = type;
463
+ w.count = existing.length;
464
+ process.emitWarning(w);
465
+ }
466
+ }
467
+
468
+ return target;
469
+ }
470
+
471
+ /**
472
+ * Adds a listener to the event emitter.
473
+ * @param {string | symbol} type
474
+ * @param {Function} listener
475
+ * @returns {EventEmitter}
476
+ */
477
+ EventEmitter.prototype.addListener = function addListener(type, listener) {
478
+ return _addListener(this, type, listener, false);
479
+ };
480
+
481
+ EventEmitter.prototype.on = EventEmitter.prototype.addListener;
482
+
483
+ /**
484
+ * Adds the `listener` function to the beginning of
485
+ * the listeners array.
486
+ * @param {string | symbol} type
487
+ * @param {Function} listener
488
+ * @returns {EventEmitter}
489
+ */
490
+ EventEmitter.prototype.prependListener = function prependListener(
491
+ type,
492
+ listener,
493
+ ) {
494
+ return _addListener(this, type, listener, true);
495
+ };
496
+
497
+ function onceWrapper() {
498
+ if (!this.fired) {
499
+ this.target.removeListener(this.type, this.wrapFn);
500
+ this.fired = true;
501
+ if (arguments.length === 0) {
502
+ return this.listener.call(this.target);
503
+ }
504
+ return this.listener.apply(this.target, arguments);
505
+ }
506
+ }
507
+
508
+ function _onceWrap(target, type, listener) {
509
+ const state = { fired: false, wrapFn: undefined, target, type, listener };
510
+ const wrapped = onceWrapper.bind(state);
511
+ wrapped.listener = listener;
512
+ state.wrapFn = wrapped;
513
+ return wrapped;
514
+ }
515
+
516
+ /**
517
+ * Adds a one-time `listener` function to the event emitter.
518
+ * @param {string | symbol} type
519
+ * @param {Function} listener
520
+ * @returns {EventEmitter}
521
+ */
522
+ EventEmitter.prototype.once = function once(type, listener) {
523
+ checkListener(listener);
524
+
525
+ this.on(type, _onceWrap(this, type, listener));
526
+ return this;
527
+ };
528
+
529
+ /**
530
+ * Adds a one-time `listener` function to the beginning of
531
+ * the listeners array.
532
+ * @param {string | symbol} type
533
+ * @param {Function} listener
534
+ * @returns {EventEmitter}
535
+ */
536
+ EventEmitter.prototype.prependOnceListener = function prependOnceListener(
537
+ type,
538
+ listener,
539
+ ) {
540
+ checkListener(listener);
541
+
542
+ this.prependListener(type, _onceWrap(this, type, listener));
543
+ return this;
544
+ };
545
+
546
+ /**
547
+ * Removes the specified `listener` from the listeners array.
548
+ * @param {string | symbol} type
549
+ * @param {Function} listener
550
+ * @returns {EventEmitter}
551
+ */
552
+ EventEmitter.prototype.removeListener = function removeListener(
553
+ type,
554
+ listener,
555
+ ) {
556
+ checkListener(listener);
557
+
558
+ const events = this._events;
559
+ if (events === undefined) {
560
+ return this;
561
+ }
562
+
563
+ const list = events[type];
564
+ if (list === undefined) {
565
+ return this;
566
+ }
567
+
568
+ if (list === listener || list.listener === listener) {
569
+ if (--this._eventsCount === 0) {
570
+ this._events = Object.create(null);
571
+ } else {
572
+ delete events[type];
573
+ if (events.removeListener) {
574
+ this.emit("removeListener", type, list.listener || listener);
575
+ }
576
+ }
577
+ } else if (typeof list !== "function") {
578
+ let position = -1;
579
+
580
+ for (let i = list.length - 1; i >= 0; i--) {
581
+ if (list[i] === listener || list[i].listener === listener) {
582
+ position = i;
583
+ break;
584
+ }
585
+ }
586
+
587
+ if (position < 0) {
588
+ return this;
589
+ }
590
+
591
+ if (position === 0) {
592
+ list.shift();
593
+ } else {
594
+ spliceOne(list, position);
595
+ }
596
+
597
+ if (list.length === 1) {
598
+ events[type] = list[0];
599
+ }
600
+
601
+ if (events.removeListener !== undefined) {
602
+ this.emit("removeListener", type, listener);
603
+ }
604
+ }
605
+
606
+ return this;
607
+ };
608
+
609
+ EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
610
+
611
+ /**
612
+ * Removes all listeners from the event emitter. (Only
613
+ * removes listeners for a specific event name if specified
614
+ * as `type`).
615
+ * @param {string | symbol} [type]
616
+ * @returns {EventEmitter}
617
+ */
618
+ EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) {
619
+ const events = this._events;
620
+ if (events === undefined) {
621
+ return this;
622
+ }
623
+
624
+ // Not listening for removeListener, no need to emit
625
+ if (events.removeListener === undefined) {
626
+ if (arguments.length === 0) {
627
+ this._events = Object.create(null);
628
+ this._eventsCount = 0;
629
+ } else if (events[type] !== undefined) {
630
+ if (--this._eventsCount === 0) {
631
+ this._events = Object.create(null);
632
+ } else {
633
+ delete events[type];
634
+ }
635
+ }
636
+ return this;
637
+ }
638
+
639
+ // Emit removeListener for all listeners on all events
640
+ if (arguments.length === 0) {
641
+ for (const key of Reflect.ownKeys(events)) {
642
+ if (key === "removeListener") continue;
643
+ this.removeAllListeners(key);
644
+ }
645
+ this.removeAllListeners("removeListener");
646
+ this._events = Object.create(null);
647
+ this._eventsCount = 0;
648
+ return this;
649
+ }
650
+
651
+ const listeners = events[type];
652
+
653
+ if (typeof listeners === "function") {
654
+ this.removeListener(type, listeners);
655
+ } else if (listeners !== undefined) {
656
+ // LIFO order
657
+ for (let i = listeners.length - 1; i >= 0; i--) {
658
+ this.removeListener(type, listeners[i]);
659
+ }
660
+ }
661
+
662
+ return this;
663
+ };
664
+
665
+ function _listeners(target, type, unwrap) {
666
+ const events = target._events;
667
+
668
+ if (events === undefined) {
669
+ return [];
670
+ }
671
+
672
+ const evlistener = events[type];
673
+ if (evlistener === undefined) {
674
+ return [];
675
+ }
676
+
677
+ if (typeof evlistener === "function") {
678
+ return unwrap ? [evlistener.listener || evlistener] : [evlistener];
679
+ }
680
+
681
+ return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener);
682
+ }
683
+
684
+ /**
685
+ * Returns a copy of the array of listeners for the event name
686
+ * specified as `type`.
687
+ * @param {string | symbol} type
688
+ * @returns {Function[]}
689
+ */
690
+ EventEmitter.prototype.listeners = function listeners(type) {
691
+ return _listeners(this, type, true);
692
+ };
693
+
694
+ /**
695
+ * Returns a copy of the array of listeners and wrappers for
696
+ * the event name specified as `type`.
697
+ * @param {string | symbol} type
698
+ * @returns {Function[]}
699
+ */
700
+ EventEmitter.prototype.rawListeners = function rawListeners(type) {
701
+ return _listeners(this, type, false);
702
+ };
703
+
704
+ /**
705
+ * Returns the number of listeners listening to event name
706
+ * specified as `type`.
707
+ * @param {string | symbol} type
708
+ * @returns {number}
709
+ */
710
+ const _listenerCount = function listenerCount(type) {
711
+ const events = this._events;
712
+
713
+ if (events !== undefined) {
714
+ const evlistener = events[type];
715
+
716
+ if (typeof evlistener === "function") {
717
+ return 1;
718
+ } else if (evlistener !== undefined) {
719
+ return evlistener.length;
720
+ }
721
+ }
722
+
723
+ return 0;
724
+ };
725
+
726
+ EventEmitter.prototype.listenerCount = _listenerCount;
727
+
728
+ /**
729
+ * Returns the number of listeners listening to the event name
730
+ * specified as `type`.
731
+ * @deprecated since v3.2.0
732
+ * @param {EventEmitter} emitter
733
+ * @param {string | symbol} type
734
+ * @returns {number}
735
+ */
736
+ export function listenerCount(emitter, type) {
737
+ if (typeof emitter.listenerCount === "function") {
738
+ return emitter.listenerCount(type);
739
+ }
740
+ return _listenerCount.call(emitter, type);
741
+ }
742
+
743
+ /**
744
+ * Returns an array listing the events for which
745
+ * the emitter has registered listeners.
746
+ * @returns {any[]}
747
+ */
748
+ EventEmitter.prototype.eventNames = function eventNames() {
749
+ return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
750
+ };
751
+
752
+ function arrayClone(arr) {
753
+ // At least since V8 8.3, this implementation is faster than the previous
754
+ // which always used a simple for-loop
755
+ switch (arr.length) {
756
+ case 2:
757
+ return [arr[0], arr[1]];
758
+ case 3:
759
+ return [arr[0], arr[1], arr[2]];
760
+ case 4:
761
+ return [arr[0], arr[1], arr[2], arr[3]];
762
+ case 5:
763
+ return [arr[0], arr[1], arr[2], arr[3], arr[4]];
764
+ case 6:
765
+ return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
766
+ }
767
+ return arr.slice();
768
+ }
769
+
770
+ function unwrapListeners(arr) {
771
+ const ret = arrayClone(arr);
772
+ for (let i = 0; i < ret.length; ++i) {
773
+ const orig = ret[i].listener;
774
+ if (typeof orig === "function") {
775
+ ret[i] = orig;
776
+ }
777
+ }
778
+ return ret;
779
+ }
780
+
781
+ /**
782
+ * Returns a copy of the array of listeners for the event name
783
+ * specified as `type`.
784
+ * @param {EventEmitter | EventTarget} emitterOrTarget
785
+ * @param {string | symbol} type
786
+ * @returns {Function[]}
787
+ */
788
+ export function getEventListeners(emitterOrTarget, type) {
789
+ // First check if EventEmitter
790
+ if (typeof emitterOrTarget.listeners === "function") {
791
+ return emitterOrTarget.listeners(type);
792
+ }
793
+ if (emitterOrTarget instanceof EventTarget) {
794
+ // TODO: kEvents is not defined
795
+ const root = emitterOrTarget[kEvents].get(type);
796
+ const listeners = [];
797
+ let handler = root?.next;
798
+ while (handler?.listener !== undefined) {
799
+ const listener = handler.listener?.deref
800
+ ? handler.listener.deref()
801
+ : handler.listener;
802
+ listeners.push(listener);
803
+ handler = handler.next;
804
+ }
805
+ return listeners;
806
+ }
807
+ throw new ERR_INVALID_ARG_TYPE(
808
+ "emitter",
809
+ ["EventEmitter", "EventTarget"],
810
+ emitterOrTarget,
811
+ );
812
+ }
813
+
814
+ /**
815
+ * Creates a `Promise` that is fulfilled when the emitter
816
+ * emits the given event.
817
+ * @param {EventEmitter} emitter
818
+ * @param {string} name
819
+ * @param {{ signal: AbortSignal; }} [options]
820
+ * @returns {Promise}
821
+ */
822
+ // deno-lint-ignore require-await
823
+ export async function once(emitter, name, options = {}) {
824
+ const signal = options?.signal;
825
+ validateAbortSignal(signal, "options.signal");
826
+ if (signal?.aborted) {
827
+ throw new AbortError();
828
+ }
829
+ return new Promise((resolve, reject) => {
830
+ const errorListener = (err) => {
831
+ emitter.removeListener(name, resolver);
832
+ if (signal != null) {
833
+ eventTargetAgnosticRemoveListener(signal, "abort", abortListener);
834
+ }
835
+ reject(err);
836
+ };
837
+ const resolver = (...args) => {
838
+ if (typeof emitter.removeListener === "function") {
839
+ emitter.removeListener("error", errorListener);
840
+ }
841
+ if (signal != null) {
842
+ eventTargetAgnosticRemoveListener(signal, "abort", abortListener);
843
+ }
844
+ resolve(args);
845
+ };
846
+ eventTargetAgnosticAddListener(emitter, name, resolver, { once: true });
847
+ if (name !== "error" && typeof emitter.once === "function") {
848
+ emitter.once("error", errorListener);
849
+ }
850
+ function abortListener() {
851
+ eventTargetAgnosticRemoveListener(emitter, name, resolver);
852
+ eventTargetAgnosticRemoveListener(emitter, "error", errorListener);
853
+ reject(new AbortError());
854
+ }
855
+ if (signal != null) {
856
+ eventTargetAgnosticAddListener(
857
+ signal,
858
+ "abort",
859
+ abortListener,
860
+ { once: true },
861
+ );
862
+ }
863
+ });
864
+ }
865
+
866
+ const AsyncIteratorPrototype = Object.getPrototypeOf(
867
+ Object.getPrototypeOf(async function* () {}).prototype,
868
+ );
869
+
870
+ function createIterResult(value, done) {
871
+ return { value, done };
872
+ }
873
+
874
+ function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
875
+ if (typeof emitter.removeListener === "function") {
876
+ emitter.removeListener(name, listener);
877
+ } else if (typeof emitter.removeEventListener === "function") {
878
+ emitter.removeEventListener(name, listener, flags);
879
+ } else {
880
+ throw new ERR_INVALID_ARG_TYPE("emitter", "EventEmitter", emitter);
881
+ }
882
+ }
883
+
884
+ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
885
+ if (typeof emitter.on === "function") {
886
+ if (flags?.once) {
887
+ emitter.once(name, listener);
888
+ } else {
889
+ emitter.on(name, listener);
890
+ }
891
+ } else if (typeof emitter.addEventListener === "function") {
892
+ // EventTarget does not have `error` event semantics like Node
893
+ // EventEmitters, we do not listen to `error` events here.
894
+ emitter.addEventListener(name, (arg) => {
895
+ listener(arg);
896
+ }, flags);
897
+ } else {
898
+ throw new ERR_INVALID_ARG_TYPE("emitter", "EventEmitter", emitter);
899
+ }
900
+ }
901
+
902
+ /**
903
+ * Returns an `AsyncIterator` that iterates `event` events.
904
+ * @param {EventEmitter} emitter
905
+ * @param {string | symbol} event
906
+ * @param {{ signal: AbortSignal; }} [options]
907
+ * @returns {AsyncIterator}
908
+ */
909
+ export function on(emitter, event, options) {
910
+ const signal = options?.signal;
911
+ validateAbortSignal(signal, "options.signal");
912
+ if (signal?.aborted) {
913
+ throw new AbortError();
914
+ }
915
+
916
+ const unconsumedEvents = [];
917
+ const unconsumedPromises = [];
918
+ let error = null;
919
+ let finished = false;
920
+
921
+ const iterator = Object.setPrototypeOf({
922
+ next() {
923
+ // First, we consume all unread events
924
+ const value = unconsumedEvents.shift();
925
+ if (value) {
926
+ return Promise.resolve(createIterResult(value, false));
927
+ }
928
+
929
+ // Then we error, if an error happened
930
+ // This happens one time if at all, because after 'error'
931
+ // we stop listening
932
+ if (error) {
933
+ const p = Promise.reject(error);
934
+ // Only the first element errors
935
+ error = null;
936
+ return p;
937
+ }
938
+
939
+ // If the iterator is finished, resolve to done
940
+ if (finished) {
941
+ return Promise.resolve(createIterResult(undefined, true));
942
+ }
943
+
944
+ // Wait until an event happens
945
+ return new Promise(function (resolve, reject) {
946
+ unconsumedPromises.push({ resolve, reject });
947
+ });
948
+ },
949
+
950
+ return() {
951
+ eventTargetAgnosticRemoveListener(emitter, event, eventHandler);
952
+ eventTargetAgnosticRemoveListener(emitter, "error", errorHandler);
953
+
954
+ if (signal) {
955
+ eventTargetAgnosticRemoveListener(
956
+ signal,
957
+ "abort",
958
+ abortListener,
959
+ { once: true },
960
+ );
961
+ }
962
+
963
+ finished = true;
964
+
965
+ for (const promise of unconsumedPromises) {
966
+ promise.resolve(createIterResult(undefined, true));
967
+ }
968
+
969
+ return Promise.resolve(createIterResult(undefined, true));
970
+ },
971
+
972
+ throw(err) {
973
+ if (!err || !(err instanceof Error)) {
974
+ throw new ERR_INVALID_ARG_TYPE(
975
+ "EventEmitter.AsyncIterator",
976
+ "Error",
977
+ err,
978
+ );
979
+ }
980
+ error = err;
981
+ eventTargetAgnosticRemoveListener(emitter, event, eventHandler);
982
+ eventTargetAgnosticRemoveListener(emitter, "error", errorHandler);
983
+ },
984
+
985
+ [Symbol.asyncIterator]() {
986
+ return this;
987
+ },
988
+ }, AsyncIteratorPrototype);
989
+
990
+ eventTargetAgnosticAddListener(emitter, event, eventHandler);
991
+ if (event !== "error" && typeof emitter.on === "function") {
992
+ emitter.on("error", errorHandler);
993
+ }
994
+
995
+ if (signal) {
996
+ eventTargetAgnosticAddListener(
997
+ signal,
998
+ "abort",
999
+ abortListener,
1000
+ { once: true },
1001
+ );
1002
+ }
1003
+
1004
+ return iterator;
1005
+
1006
+ function abortListener() {
1007
+ errorHandler(new AbortError());
1008
+ }
1009
+
1010
+ function eventHandler(...args) {
1011
+ const promise = unconsumedPromises.shift();
1012
+ if (promise) {
1013
+ promise.resolve(createIterResult(args, false));
1014
+ } else {
1015
+ unconsumedEvents.push(args);
1016
+ }
1017
+ }
1018
+
1019
+ function errorHandler(err) {
1020
+ finished = true;
1021
+
1022
+ const toError = unconsumedPromises.shift();
1023
+
1024
+ if (toError) {
1025
+ toError.reject(err);
1026
+ } else {
1027
+ // The next time we call next()
1028
+ error = err;
1029
+ }
1030
+
1031
+ iterator.return();
1032
+ }
1033
+ }