importmap_mocha-rails 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,609 @@
1
- // src/interceptors/fetch/index.ts
2
- import { invariant as invariant2 } from "outvariant";
3
- import { DeferredPromise as DeferredPromise3 } from "@open-draft/deferred-promise";
1
+ // node_modules/.pnpm/outvariant@1.4.3/node_modules/outvariant/lib/index.mjs
2
+ var POSITIONALS_EXP = /(%?)(%([sdijo]))/g;
3
+ function serializePositional(positional, flag) {
4
+ switch (flag) {
5
+ case "s":
6
+ return positional;
7
+ case "d":
8
+ case "i":
9
+ return Number(positional);
10
+ case "j":
11
+ return JSON.stringify(positional);
12
+ case "o": {
13
+ if (typeof positional === "string") {
14
+ return positional;
15
+ }
16
+ const json = JSON.stringify(positional);
17
+ if (json === "{}" || json === "[]" || /^\[object .+?\]$/.test(json)) {
18
+ return positional;
19
+ }
20
+ return json;
21
+ }
22
+ }
23
+ }
24
+ function format(message, ...positionals) {
25
+ if (positionals.length === 0) {
26
+ return message;
27
+ }
28
+ let positionalIndex = 0;
29
+ let formattedMessage = message.replace(
30
+ POSITIONALS_EXP,
31
+ (match, isEscaped, _, flag) => {
32
+ const positional = positionals[positionalIndex];
33
+ const value = serializePositional(positional, flag);
34
+ if (!isEscaped) {
35
+ positionalIndex++;
36
+ return value;
37
+ }
38
+ return match;
39
+ }
40
+ );
41
+ if (positionalIndex < positionals.length) {
42
+ formattedMessage += ` ${positionals.slice(positionalIndex).join(" ")}`;
43
+ }
44
+ formattedMessage = formattedMessage.replace(/%{2,2}/g, "%");
45
+ return formattedMessage;
46
+ }
47
+ var STACK_FRAMES_TO_IGNORE = 2;
48
+ function cleanErrorStack(error2) {
49
+ if (!error2.stack) {
50
+ return;
51
+ }
52
+ const nextStack = error2.stack.split("\n");
53
+ nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
54
+ error2.stack = nextStack.join("\n");
55
+ }
56
+ var InvariantError = class extends Error {
57
+ constructor(message, ...positionals) {
58
+ super(message);
59
+ this.message = message;
60
+ this.name = "Invariant Violation";
61
+ this.message = format(message, ...positionals);
62
+ cleanErrorStack(this);
63
+ }
64
+ };
65
+ var invariant = (predicate, message, ...positionals) => {
66
+ if (!predicate) {
67
+ throw new InvariantError(message, ...positionals);
68
+ }
69
+ };
70
+ invariant.as = (ErrorConstructor, predicate, message, ...positionals) => {
71
+ if (!predicate) {
72
+ const formatMessage = positionals.length === 0 ? message : format(message, ...positionals);
73
+ let error2;
74
+ try {
75
+ error2 = Reflect.construct(ErrorConstructor, [
76
+ formatMessage
77
+ ]);
78
+ } catch (err) {
79
+ error2 = ErrorConstructor(formatMessage);
80
+ }
81
+ throw error2;
82
+ }
83
+ };
84
+
85
+ // node_modules/.pnpm/@open-draft+deferred-promise@2.2.0/node_modules/@open-draft/deferred-promise/build/index.mjs
86
+ function createDeferredExecutor() {
87
+ const executor = (resolve, reject) => {
88
+ executor.state = "pending";
89
+ executor.resolve = (data) => {
90
+ if (executor.state !== "pending") {
91
+ return;
92
+ }
93
+ executor.result = data;
94
+ const onFulfilled = (value) => {
95
+ executor.state = "fulfilled";
96
+ return value;
97
+ };
98
+ return resolve(
99
+ data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
100
+ );
101
+ };
102
+ executor.reject = (reason) => {
103
+ if (executor.state !== "pending") {
104
+ return;
105
+ }
106
+ queueMicrotask(() => {
107
+ executor.state = "rejected";
108
+ });
109
+ return reject(executor.rejectionReason = reason);
110
+ };
111
+ };
112
+ return executor;
113
+ }
114
+ var DeferredPromise = class extends Promise {
115
+ #executor;
116
+ resolve;
117
+ reject;
118
+ constructor(executor = null) {
119
+ const deferredExecutor = createDeferredExecutor();
120
+ super((originalResolve, originalReject) => {
121
+ deferredExecutor(originalResolve, originalReject);
122
+ executor?.(deferredExecutor.resolve, deferredExecutor.reject);
123
+ });
124
+ this.#executor = deferredExecutor;
125
+ this.resolve = this.#executor.resolve;
126
+ this.reject = this.#executor.reject;
127
+ }
128
+ get state() {
129
+ return this.#executor.state;
130
+ }
131
+ get rejectionReason() {
132
+ return this.#executor.rejectionReason;
133
+ }
134
+ then(onFulfilled, onRejected) {
135
+ return this.#decorate(super.then(onFulfilled, onRejected));
136
+ }
137
+ catch(onRejected) {
138
+ return this.#decorate(super.catch(onRejected));
139
+ }
140
+ finally(onfinally) {
141
+ return this.#decorate(super.finally(onfinally));
142
+ }
143
+ #decorate(promise) {
144
+ return Object.defineProperties(promise, {
145
+ resolve: { configurable: true, value: this.resolve },
146
+ reject: { configurable: true, value: this.reject }
147
+ });
148
+ }
149
+ };
4
150
 
5
151
  // src/glossary.ts
6
152
  var IS_PATCHED_MODULE = Symbol("isPatchedModule");
7
153
 
154
+ // node_modules/.pnpm/is-node-process@1.2.0/node_modules/is-node-process/lib/index.mjs
155
+ function isNodeProcess() {
156
+ if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
157
+ return true;
158
+ }
159
+ if (typeof process !== "undefined") {
160
+ const type = process.type;
161
+ if (type === "renderer" || type === "worker") {
162
+ return false;
163
+ }
164
+ return !!(process.versions && process.versions.node);
165
+ }
166
+ return false;
167
+ }
168
+
169
+ // node_modules/.pnpm/@open-draft+logger@0.3.0/node_modules/@open-draft/logger/lib/index.mjs
170
+ var __defProp = Object.defineProperty;
171
+ var __export = (target, all) => {
172
+ for (var name in all)
173
+ __defProp(target, name, { get: all[name], enumerable: true });
174
+ };
175
+ var colors_exports = {};
176
+ __export(colors_exports, {
177
+ blue: () => blue,
178
+ gray: () => gray,
179
+ green: () => green,
180
+ red: () => red,
181
+ yellow: () => yellow
182
+ });
183
+ function yellow(text) {
184
+ return `\x1B[33m${text}\x1B[0m`;
185
+ }
186
+ function blue(text) {
187
+ return `\x1B[34m${text}\x1B[0m`;
188
+ }
189
+ function gray(text) {
190
+ return `\x1B[90m${text}\x1B[0m`;
191
+ }
192
+ function red(text) {
193
+ return `\x1B[31m${text}\x1B[0m`;
194
+ }
195
+ function green(text) {
196
+ return `\x1B[32m${text}\x1B[0m`;
197
+ }
198
+ var IS_NODE = isNodeProcess();
199
+ var Logger = class {
200
+ constructor(name) {
201
+ this.name = name;
202
+ this.prefix = `[${this.name}]`;
203
+ const LOGGER_NAME = getVariable("DEBUG");
204
+ const LOGGER_LEVEL = getVariable("LOG_LEVEL");
205
+ const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME);
206
+ if (isLoggingEnabled) {
207
+ this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug;
208
+ this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info;
209
+ this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success;
210
+ this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning;
211
+ this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error;
212
+ } else {
213
+ this.info = noop;
214
+ this.success = noop;
215
+ this.warning = noop;
216
+ this.error = noop;
217
+ this.only = noop;
218
+ }
219
+ }
220
+ prefix;
221
+ extend(domain) {
222
+ return new Logger(`${this.name}:${domain}`);
223
+ }
224
+ /**
225
+ * Print a debug message.
226
+ * @example
227
+ * logger.debug('no duplicates found, creating a document...')
228
+ */
229
+ debug(message, ...positionals) {
230
+ this.logEntry({
231
+ level: "debug",
232
+ message: gray(message),
233
+ positionals,
234
+ prefix: this.prefix,
235
+ colors: {
236
+ prefix: "gray"
237
+ }
238
+ });
239
+ }
240
+ /**
241
+ * Print an info message.
242
+ * @example
243
+ * logger.info('start parsing...')
244
+ */
245
+ info(message, ...positionals) {
246
+ this.logEntry({
247
+ level: "info",
248
+ message,
249
+ positionals,
250
+ prefix: this.prefix,
251
+ colors: {
252
+ prefix: "blue"
253
+ }
254
+ });
255
+ const performance2 = new PerformanceEntry();
256
+ return (message2, ...positionals2) => {
257
+ performance2.measure();
258
+ this.logEntry({
259
+ level: "info",
260
+ message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`,
261
+ positionals: positionals2,
262
+ prefix: this.prefix,
263
+ colors: {
264
+ prefix: "blue"
265
+ }
266
+ });
267
+ };
268
+ }
269
+ /**
270
+ * Print a success message.
271
+ * @example
272
+ * logger.success('successfully created document')
273
+ */
274
+ success(message, ...positionals) {
275
+ this.logEntry({
276
+ level: "info",
277
+ message,
278
+ positionals,
279
+ prefix: `\u2714 ${this.prefix}`,
280
+ colors: {
281
+ timestamp: "green",
282
+ prefix: "green"
283
+ }
284
+ });
285
+ }
286
+ /**
287
+ * Print a warning.
288
+ * @example
289
+ * logger.warning('found legacy document format')
290
+ */
291
+ warning(message, ...positionals) {
292
+ this.logEntry({
293
+ level: "warning",
294
+ message,
295
+ positionals,
296
+ prefix: `\u26A0 ${this.prefix}`,
297
+ colors: {
298
+ timestamp: "yellow",
299
+ prefix: "yellow"
300
+ }
301
+ });
302
+ }
303
+ /**
304
+ * Print an error message.
305
+ * @example
306
+ * logger.error('something went wrong')
307
+ */
308
+ error(message, ...positionals) {
309
+ this.logEntry({
310
+ level: "error",
311
+ message,
312
+ positionals,
313
+ prefix: `\u2716 ${this.prefix}`,
314
+ colors: {
315
+ timestamp: "red",
316
+ prefix: "red"
317
+ }
318
+ });
319
+ }
320
+ /**
321
+ * Execute the given callback only when the logging is enabled.
322
+ * This is skipped in its entirety and has no runtime cost otherwise.
323
+ * This executes regardless of the log level.
324
+ * @example
325
+ * logger.only(() => {
326
+ * logger.info('additional info')
327
+ * })
328
+ */
329
+ only(callback) {
330
+ callback();
331
+ }
332
+ createEntry(level, message) {
333
+ return {
334
+ timestamp: /* @__PURE__ */ new Date(),
335
+ level,
336
+ message
337
+ };
338
+ }
339
+ logEntry(args) {
340
+ const {
341
+ level,
342
+ message,
343
+ prefix,
344
+ colors: customColors,
345
+ positionals = []
346
+ } = args;
347
+ const entry = this.createEntry(level, message);
348
+ const timestampColor = customColors?.timestamp || "gray";
349
+ const prefixColor = customColors?.prefix || "gray";
350
+ const colorize = {
351
+ timestamp: colors_exports[timestampColor],
352
+ prefix: colors_exports[prefixColor]
353
+ };
354
+ const write = this.getWriter(level);
355
+ write(
356
+ [colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "),
357
+ ...positionals.map(serializeInput)
358
+ );
359
+ }
360
+ formatTimestamp(timestamp) {
361
+ return `${timestamp.toLocaleTimeString(
362
+ "en-GB"
363
+ )}:${timestamp.getMilliseconds()}`;
364
+ }
365
+ getWriter(level) {
366
+ switch (level) {
367
+ case "debug":
368
+ case "success":
369
+ case "info": {
370
+ return log;
371
+ }
372
+ case "warning": {
373
+ return warn;
374
+ }
375
+ case "error": {
376
+ return error;
377
+ }
378
+ }
379
+ }
380
+ };
381
+ var PerformanceEntry = class {
382
+ startTime;
383
+ endTime;
384
+ deltaTime;
385
+ constructor() {
386
+ this.startTime = performance.now();
387
+ }
388
+ measure() {
389
+ this.endTime = performance.now();
390
+ const deltaTime = this.endTime - this.startTime;
391
+ this.deltaTime = deltaTime.toFixed(2);
392
+ }
393
+ };
394
+ var noop = () => void 0;
395
+ function log(message, ...positionals) {
396
+ if (IS_NODE) {
397
+ process.stdout.write(format(message, ...positionals) + "\n");
398
+ return;
399
+ }
400
+ console.log(message, ...positionals);
401
+ }
402
+ function warn(message, ...positionals) {
403
+ if (IS_NODE) {
404
+ process.stderr.write(format(message, ...positionals) + "\n");
405
+ return;
406
+ }
407
+ console.warn(message, ...positionals);
408
+ }
409
+ function error(message, ...positionals) {
410
+ if (IS_NODE) {
411
+ process.stderr.write(format(message, ...positionals) + "\n");
412
+ return;
413
+ }
414
+ console.error(message, ...positionals);
415
+ }
416
+ function getVariable(variableName) {
417
+ if (IS_NODE) {
418
+ return process.env[variableName];
419
+ }
420
+ return globalThis[variableName]?.toString();
421
+ }
422
+ function isDefinedAndNotEquals(value, expected) {
423
+ return value !== void 0 && value !== expected;
424
+ }
425
+ function serializeInput(message) {
426
+ if (typeof message === "undefined") {
427
+ return "undefined";
428
+ }
429
+ if (message === null) {
430
+ return "null";
431
+ }
432
+ if (typeof message === "string") {
433
+ return message;
434
+ }
435
+ if (typeof message === "object") {
436
+ return JSON.stringify(message);
437
+ }
438
+ return message.toString();
439
+ }
440
+
441
+ // node_modules/.pnpm/strict-event-emitter@0.5.1/node_modules/strict-event-emitter/lib/index.mjs
442
+ var MemoryLeakError = class extends Error {
443
+ constructor(emitter, type, count) {
444
+ super(
445
+ `Possible EventEmitter memory leak detected. ${count} ${type.toString()} listeners added. Use emitter.setMaxListeners() to increase limit`
446
+ );
447
+ this.emitter = emitter;
448
+ this.type = type;
449
+ this.count = count;
450
+ this.name = "MaxListenersExceededWarning";
451
+ }
452
+ };
453
+ var _Emitter = class {
454
+ static listenerCount(emitter, eventName) {
455
+ return emitter.listenerCount(eventName);
456
+ }
457
+ constructor() {
458
+ this.events = /* @__PURE__ */ new Map();
459
+ this.maxListeners = _Emitter.defaultMaxListeners;
460
+ this.hasWarnedAboutPotentialMemoryLeak = false;
461
+ }
462
+ _emitInternalEvent(internalEventName, eventName, listener) {
463
+ this.emit(
464
+ internalEventName,
465
+ ...[eventName, listener]
466
+ );
467
+ }
468
+ _getListeners(eventName) {
469
+ return Array.prototype.concat.apply([], this.events.get(eventName)) || [];
470
+ }
471
+ _removeListener(listeners, listener) {
472
+ const index = listeners.indexOf(listener);
473
+ if (index > -1) {
474
+ listeners.splice(index, 1);
475
+ }
476
+ return [];
477
+ }
478
+ _wrapOnceListener(eventName, listener) {
479
+ const onceListener = (...data) => {
480
+ this.removeListener(eventName, onceListener);
481
+ return listener.apply(this, data);
482
+ };
483
+ Object.defineProperty(onceListener, "name", { value: listener.name });
484
+ return onceListener;
485
+ }
486
+ setMaxListeners(maxListeners) {
487
+ this.maxListeners = maxListeners;
488
+ return this;
489
+ }
490
+ /**
491
+ * Returns the current max listener value for the `Emitter` which is
492
+ * either set by `emitter.setMaxListeners(n)` or defaults to
493
+ * `Emitter.defaultMaxListeners`.
494
+ */
495
+ getMaxListeners() {
496
+ return this.maxListeners;
497
+ }
498
+ /**
499
+ * Returns an array listing the events for which the emitter has registered listeners.
500
+ * The values in the array will be strings or Symbols.
501
+ */
502
+ eventNames() {
503
+ return Array.from(this.events.keys());
504
+ }
505
+ /**
506
+ * Synchronously calls each of the listeners registered for the event named `eventName`,
507
+ * in the order they were registered, passing the supplied arguments to each.
508
+ * Returns `true` if the event has listeners, `false` otherwise.
509
+ *
510
+ * @example
511
+ * const emitter = new Emitter<{ hello: [string] }>()
512
+ * emitter.emit('hello', 'John')
513
+ */
514
+ emit(eventName, ...data) {
515
+ const listeners = this._getListeners(eventName);
516
+ listeners.forEach((listener) => {
517
+ listener.apply(this, data);
518
+ });
519
+ return listeners.length > 0;
520
+ }
521
+ addListener(eventName, listener) {
522
+ this._emitInternalEvent("newListener", eventName, listener);
523
+ const nextListeners = this._getListeners(eventName).concat(listener);
524
+ this.events.set(eventName, nextListeners);
525
+ if (this.maxListeners > 0 && this.listenerCount(eventName) > this.maxListeners && !this.hasWarnedAboutPotentialMemoryLeak) {
526
+ this.hasWarnedAboutPotentialMemoryLeak = true;
527
+ const memoryLeakWarning = new MemoryLeakError(
528
+ this,
529
+ eventName,
530
+ this.listenerCount(eventName)
531
+ );
532
+ console.warn(memoryLeakWarning);
533
+ }
534
+ return this;
535
+ }
536
+ on(eventName, listener) {
537
+ return this.addListener(eventName, listener);
538
+ }
539
+ once(eventName, listener) {
540
+ return this.addListener(
541
+ eventName,
542
+ this._wrapOnceListener(eventName, listener)
543
+ );
544
+ }
545
+ prependListener(eventName, listener) {
546
+ const listeners = this._getListeners(eventName);
547
+ if (listeners.length > 0) {
548
+ const nextListeners = [listener].concat(listeners);
549
+ this.events.set(eventName, nextListeners);
550
+ } else {
551
+ this.events.set(eventName, listeners.concat(listener));
552
+ }
553
+ return this;
554
+ }
555
+ prependOnceListener(eventName, listener) {
556
+ return this.prependListener(
557
+ eventName,
558
+ this._wrapOnceListener(eventName, listener)
559
+ );
560
+ }
561
+ removeListener(eventName, listener) {
562
+ const listeners = this._getListeners(eventName);
563
+ if (listeners.length > 0) {
564
+ this._removeListener(listeners, listener);
565
+ this.events.set(eventName, listeners);
566
+ this._emitInternalEvent("removeListener", eventName, listener);
567
+ }
568
+ return this;
569
+ }
570
+ /**
571
+ * Alias for `emitter.removeListener()`.
572
+ *
573
+ * @example
574
+ * emitter.off('hello', listener)
575
+ */
576
+ off(eventName, listener) {
577
+ return this.removeListener(eventName, listener);
578
+ }
579
+ removeAllListeners(eventName) {
580
+ if (eventName) {
581
+ this.events.delete(eventName);
582
+ } else {
583
+ this.events.clear();
584
+ }
585
+ return this;
586
+ }
587
+ /**
588
+ * Returns a copy of the array of listeners for the event named `eventName`.
589
+ */
590
+ listeners(eventName) {
591
+ return Array.from(this._getListeners(eventName));
592
+ }
593
+ /**
594
+ * Returns the number of listeners listening to the event named `eventName`.
595
+ */
596
+ listenerCount(eventName) {
597
+ return this._getListeners(eventName).length;
598
+ }
599
+ rawListeners(eventName) {
600
+ return this.listeners(eventName);
601
+ }
602
+ };
603
+ var Emitter = _Emitter;
604
+ Emitter.defaultMaxListeners = 10;
605
+
8
606
  // src/Interceptor.ts
9
- import { Logger } from "@open-draft/logger";
10
- import { Emitter } from "strict-event-emitter";
11
607
  var INTERNAL_REQUEST_ID_HEADER_NAME = "x-interceptors-internal-request-id";
12
608
  function getGlobalSymbol(symbol) {
13
609
  return (
@@ -137,9 +733,8 @@ var Interceptor = class {
137
733
  this.readyState = "DISPOSED" /* DISPOSED */;
138
734
  }
139
735
  getInstance() {
140
- var _a;
141
736
  const instance = getGlobalSymbol(this.symbol);
142
- this.logger.info("retrieved global instance:", (_a = instance == null ? void 0 : instance.constructor) == null ? void 0 : _a.name);
737
+ this.logger.info("retrieved global instance:", instance?.constructor?.name);
143
738
  return instance;
144
739
  }
145
740
  setInstance() {
@@ -152,22 +747,19 @@ var Interceptor = class {
152
747
  }
153
748
  };
154
749
 
155
- // src/RequestController.ts
156
- import { invariant } from "outvariant";
157
- import { DeferredPromise } from "@open-draft/deferred-promise";
158
-
159
750
  // src/InterceptorError.ts
160
- var InterceptorError = class extends Error {
751
+ var InterceptorError = class _InterceptorError extends Error {
161
752
  constructor(message) {
162
753
  super(message);
163
754
  this.name = "InterceptorError";
164
- Object.setPrototypeOf(this, InterceptorError.prototype);
755
+ Object.setPrototypeOf(this, _InterceptorError.prototype);
165
756
  }
166
757
  };
167
758
 
168
759
  // src/RequestController.ts
169
760
  var kRequestHandled = Symbol("kRequestHandled");
170
761
  var kResponsePromise = Symbol("kResponsePromise");
762
+ kResponsePromise, kRequestHandled;
171
763
  var RequestController = class {
172
764
  constructor(request) {
173
765
  this.request = request;
@@ -198,7 +790,7 @@ var RequestController = class {
198
790
  * controller.errorWith()
199
791
  * controller.errorWith(new Error('Oops!'))
200
792
  */
201
- errorWith(error) {
793
+ errorWith(error2) {
202
794
  invariant.as(
203
795
  InterceptorError,
204
796
  !this[kRequestHandled],
@@ -207,10 +799,9 @@ var RequestController = class {
207
799
  this.request.url
208
800
  );
209
801
  this[kRequestHandled] = true;
210
- this[kResponsePromise].resolve(error);
802
+ this[kResponsePromise].resolve(error2);
211
803
  }
212
804
  };
213
- kResponsePromise, kRequestHandled;
214
805
 
215
806
  // src/utils/emitAsync.ts
216
807
  async function emitAsync(emitter, eventName, ...data) {
@@ -223,16 +814,24 @@ async function emitAsync(emitter, eventName, ...data) {
223
814
  }
224
815
  }
225
816
 
226
- // src/utils/handleRequest.ts
227
- import { DeferredPromise as DeferredPromise2 } from "@open-draft/deferred-promise";
228
- import { until } from "@open-draft/until";
817
+ // node_modules/.pnpm/@open-draft+until@2.1.0/node_modules/@open-draft/until/lib/index.mjs
818
+ var until = async (promise) => {
819
+ try {
820
+ const data = await promise().catch((error2) => {
821
+ throw error2;
822
+ });
823
+ return { error: null, data };
824
+ } catch (error2) {
825
+ return { error: error2, data: null };
826
+ }
827
+ };
229
828
 
230
829
  // src/utils/isPropertyAccessible.ts
231
830
  function isPropertyAccessible(obj, key) {
232
831
  try {
233
832
  obj[key];
234
833
  return true;
235
- } catch (e) {
834
+ } catch {
236
835
  return false;
237
836
  }
238
837
  }
@@ -245,6 +844,13 @@ var RESPONSE_STATUS_CODES_WITHOUT_BODY = /* @__PURE__ */ new Set([
245
844
  205,
246
845
  304
247
846
  ]);
847
+ var RESPONSE_STATUS_CODES_WITH_REDIRECT = /* @__PURE__ */ new Set([
848
+ 301,
849
+ 302,
850
+ 303,
851
+ 307,
852
+ 308
853
+ ]);
248
854
  function isResponseWithoutBody(status) {
249
855
  return RESPONSE_STATUS_CODES_WITHOUT_BODY.has(status);
250
856
  }
@@ -271,38 +877,38 @@ function isResponseError(response) {
271
877
  }
272
878
 
273
879
  // src/utils/isNodeLikeError.ts
274
- function isNodeLikeError(error) {
275
- if (error == null) {
880
+ function isNodeLikeError(error2) {
881
+ if (error2 == null) {
276
882
  return false;
277
883
  }
278
- if (!(error instanceof Error)) {
884
+ if (!(error2 instanceof Error)) {
279
885
  return false;
280
886
  }
281
- return "code" in error && "errno" in error;
887
+ return "code" in error2 && "errno" in error2;
282
888
  }
283
889
 
284
890
  // src/utils/handleRequest.ts
285
891
  async function handleRequest(options) {
286
- const handleResponse = (response) => {
892
+ const handleResponse = async (response) => {
287
893
  if (response instanceof Error) {
288
894
  options.onError(response);
289
895
  } else if (isResponseError(response)) {
290
896
  options.onRequestError(response);
291
897
  } else {
292
- options.onResponse(response);
898
+ await options.onResponse(response);
293
899
  }
294
900
  return true;
295
901
  };
296
- const handleResponseError = (error) => {
297
- if (error instanceof InterceptorError) {
902
+ const handleResponseError = async (error2) => {
903
+ if (error2 instanceof InterceptorError) {
298
904
  throw result.error;
299
905
  }
300
- if (isNodeLikeError(error)) {
301
- options.onError(error);
906
+ if (isNodeLikeError(error2)) {
907
+ options.onError(error2);
302
908
  return true;
303
909
  }
304
- if (error instanceof Response) {
305
- return handleResponse(error);
910
+ if (error2 instanceof Response) {
911
+ return await handleResponse(error2);
306
912
  }
307
913
  return false;
308
914
  };
@@ -314,7 +920,7 @@ async function handleRequest(options) {
314
920
  options.controller[kResponsePromise].resolve(void 0);
315
921
  }
316
922
  });
317
- const requestAbortPromise = new DeferredPromise2();
923
+ const requestAbortPromise = new DeferredPromise();
318
924
  if (options.request.signal) {
319
925
  options.request.signal.addEventListener(
320
926
  "abort",
@@ -344,7 +950,7 @@ async function handleRequest(options) {
344
950
  return true;
345
951
  }
346
952
  if (result.error) {
347
- if (handleResponseError(result.error)) {
953
+ if (await handleResponseError(result.error)) {
348
954
  return true;
349
955
  }
350
956
  if (options.emitter.listenerCount("unhandledException") > 0) {
@@ -395,17 +1001,175 @@ function createRequestId() {
395
1001
  return Math.random().toString(16).slice(2);
396
1002
  }
397
1003
 
1004
+ // src/interceptors/fetch/utils/createNetworkError.ts
1005
+ function createNetworkError(cause) {
1006
+ return Object.assign(new TypeError("Failed to fetch"), {
1007
+ cause
1008
+ });
1009
+ }
1010
+
1011
+ // src/interceptors/fetch/utils/followRedirect.ts
1012
+ var REQUEST_BODY_HEADERS = [
1013
+ "content-encoding",
1014
+ "content-language",
1015
+ "content-location",
1016
+ "content-type",
1017
+ "content-length"
1018
+ ];
1019
+ var kRedirectCount = Symbol("kRedirectCount");
1020
+ async function followFetchRedirect(request, response) {
1021
+ if (response.status !== 303 && request.body != null) {
1022
+ return Promise.reject(createNetworkError());
1023
+ }
1024
+ const requestUrl = new URL(request.url);
1025
+ let locationUrl;
1026
+ try {
1027
+ locationUrl = new URL(response.headers.get("location"), request.url);
1028
+ } catch (error2) {
1029
+ return Promise.reject(createNetworkError(error2));
1030
+ }
1031
+ if (!(locationUrl.protocol === "http:" || locationUrl.protocol === "https:")) {
1032
+ return Promise.reject(
1033
+ createNetworkError("URL scheme must be a HTTP(S) scheme")
1034
+ );
1035
+ }
1036
+ if (Reflect.get(request, kRedirectCount) > 20) {
1037
+ return Promise.reject(createNetworkError("redirect count exceeded"));
1038
+ }
1039
+ Object.defineProperty(request, kRedirectCount, {
1040
+ value: (Reflect.get(request, kRedirectCount) || 0) + 1
1041
+ });
1042
+ if (request.mode === "cors" && (locationUrl.username || locationUrl.password) && !sameOrigin(requestUrl, locationUrl)) {
1043
+ return Promise.reject(
1044
+ createNetworkError('cross origin not allowed for request mode "cors"')
1045
+ );
1046
+ }
1047
+ const requestInit = {};
1048
+ if ([301, 302].includes(response.status) && request.method === "POST" || response.status === 303 && !["HEAD", "GET"].includes(request.method)) {
1049
+ requestInit.method = "GET";
1050
+ requestInit.body = null;
1051
+ REQUEST_BODY_HEADERS.forEach((headerName) => {
1052
+ request.headers.delete(headerName);
1053
+ });
1054
+ }
1055
+ if (!sameOrigin(requestUrl, locationUrl)) {
1056
+ request.headers.delete("authorization");
1057
+ request.headers.delete("proxy-authorization");
1058
+ request.headers.delete("cookie");
1059
+ request.headers.delete("host");
1060
+ }
1061
+ requestInit.headers = request.headers;
1062
+ return fetch(new Request(locationUrl, requestInit));
1063
+ }
1064
+ function sameOrigin(left, right) {
1065
+ if (left.origin === right.origin && left.origin === "null") {
1066
+ return true;
1067
+ }
1068
+ if (left.protocol === right.protocol && left.hostname === right.hostname && left.port === right.port) {
1069
+ return true;
1070
+ }
1071
+ return false;
1072
+ }
1073
+
1074
+ // src/interceptors/fetch/utils/brotli-decompress.browser.ts
1075
+ var BrotliDecompressionStream = class extends TransformStream {
1076
+ constructor() {
1077
+ console.warn(
1078
+ "[Interceptors]: Brotli decompression of response streams is not supported in the browser"
1079
+ );
1080
+ super({
1081
+ transform(chunk, controller) {
1082
+ controller.enqueue(chunk);
1083
+ }
1084
+ });
1085
+ }
1086
+ };
1087
+
1088
+ // src/interceptors/fetch/utils/decompression.ts
1089
+ var PipelineStream = class extends TransformStream {
1090
+ constructor(transformStreams, ...strategies) {
1091
+ super({}, ...strategies);
1092
+ const readable = [super.readable, ...transformStreams].reduce(
1093
+ (readable2, transform) => readable2.pipeThrough(transform)
1094
+ );
1095
+ Object.defineProperty(this, "readable", {
1096
+ get() {
1097
+ return readable;
1098
+ }
1099
+ });
1100
+ }
1101
+ };
1102
+ function parseContentEncoding(contentEncoding) {
1103
+ return contentEncoding.toLowerCase().split(",").map((coding) => coding.trim());
1104
+ }
1105
+ function createDecompressionStream(contentEncoding) {
1106
+ if (contentEncoding === "") {
1107
+ return null;
1108
+ }
1109
+ const codings = parseContentEncoding(contentEncoding);
1110
+ if (codings.length === 0) {
1111
+ return null;
1112
+ }
1113
+ const transformers = codings.reduceRight(
1114
+ (transformers2, coding) => {
1115
+ if (coding === "gzip" || coding === "x-gzip") {
1116
+ return transformers2.concat(new DecompressionStream("gzip"));
1117
+ } else if (coding === "deflate") {
1118
+ return transformers2.concat(new DecompressionStream("deflate"));
1119
+ } else if (coding === "br") {
1120
+ return transformers2.concat(new BrotliDecompressionStream());
1121
+ } else {
1122
+ transformers2.length = 0;
1123
+ }
1124
+ return transformers2;
1125
+ },
1126
+ []
1127
+ );
1128
+ return new PipelineStream(transformers);
1129
+ }
1130
+ function decompressResponse(response) {
1131
+ if (response.body === null) {
1132
+ return null;
1133
+ }
1134
+ const decompressionStream = createDecompressionStream(
1135
+ response.headers.get("content-encoding") || ""
1136
+ );
1137
+ if (!decompressionStream) {
1138
+ return null;
1139
+ }
1140
+ response.body.pipeTo(decompressionStream.writable);
1141
+ return decompressionStream.readable;
1142
+ }
1143
+
1144
+ // src/utils/hasConfigurableGlobal.ts
1145
+ function hasConfigurableGlobal(propertyName) {
1146
+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
1147
+ if (typeof descriptor === "undefined") {
1148
+ return false;
1149
+ }
1150
+ if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
1151
+ console.error(
1152
+ `[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`
1153
+ );
1154
+ return false;
1155
+ }
1156
+ return true;
1157
+ }
1158
+
398
1159
  // src/interceptors/fetch/index.ts
399
- var _FetchInterceptor = class extends Interceptor {
1160
+ var FetchInterceptor = class _FetchInterceptor extends Interceptor {
1161
+ static {
1162
+ this.symbol = Symbol("fetch");
1163
+ }
400
1164
  constructor() {
401
1165
  super(_FetchInterceptor.symbol);
402
1166
  }
403
1167
  checkEnvironment() {
404
- return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
1168
+ return hasConfigurableGlobal("fetch");
405
1169
  }
406
1170
  async setup() {
407
1171
  const pureFetch = globalThis.fetch;
408
- invariant2(
1172
+ invariant(
409
1173
  !pureFetch[IS_PATCHED_MODULE],
410
1174
  'Failed to patch the "fetch" module: already patched.'
411
1175
  );
@@ -413,7 +1177,7 @@ var _FetchInterceptor = class extends Interceptor {
413
1177
  const requestId = createRequestId();
414
1178
  const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
415
1179
  const request = new Request(resolvedInput, init);
416
- const responsePromise = new DeferredPromise3();
1180
+ const responsePromise = new DeferredPromise();
417
1181
  const controller = new RequestController(request);
418
1182
  this.logger.info("[%s] %s", request.method, request.url);
419
1183
  this.logger.info("awaiting for the mocked response...");
@@ -426,9 +1190,34 @@ var _FetchInterceptor = class extends Interceptor {
426
1190
  requestId,
427
1191
  emitter: this.emitter,
428
1192
  controller,
429
- onResponse: async (response) => {
1193
+ onResponse: async (rawResponse) => {
430
1194
  this.logger.info("received mocked response!", {
431
- response
1195
+ rawResponse
1196
+ });
1197
+ const decompressedStream = decompressResponse(rawResponse);
1198
+ const response = decompressedStream === null ? rawResponse : new Response(decompressedStream, rawResponse);
1199
+ if (RESPONSE_STATUS_CODES_WITH_REDIRECT.has(response.status)) {
1200
+ if (request.redirect === "error") {
1201
+ responsePromise.reject(createNetworkError("unexpected redirect"));
1202
+ return;
1203
+ }
1204
+ if (request.redirect === "follow") {
1205
+ followFetchRedirect(request, response).then(
1206
+ (response2) => {
1207
+ responsePromise.resolve(response2);
1208
+ },
1209
+ (reason) => {
1210
+ responsePromise.reject(reason);
1211
+ }
1212
+ );
1213
+ return;
1214
+ }
1215
+ }
1216
+ Object.defineProperty(response, "url", {
1217
+ writable: false,
1218
+ enumerable: true,
1219
+ configurable: false,
1220
+ value: request.url
432
1221
  });
433
1222
  if (this.emitter.listenerCount("response") > 0) {
434
1223
  this.logger.info('emitting the "response" event...');
@@ -442,21 +1231,15 @@ var _FetchInterceptor = class extends Interceptor {
442
1231
  requestId
443
1232
  });
444
1233
  }
445
- Object.defineProperty(response, "url", {
446
- writable: false,
447
- enumerable: true,
448
- configurable: false,
449
- value: request.url
450
- });
451
1234
  responsePromise.resolve(response);
452
1235
  },
453
1236
  onRequestError: (response) => {
454
1237
  this.logger.info("request has errored!", { response });
455
1238
  responsePromise.reject(createNetworkError(response));
456
1239
  },
457
- onError: (error) => {
458
- this.logger.info("request has been aborted!", { error });
459
- responsePromise.reject(error);
1240
+ onError: (error2) => {
1241
+ this.logger.info("request has been aborted!", { error: error2 });
1242
+ responsePromise.reject(error2);
460
1243
  }
461
1244
  });
462
1245
  if (isRequestHandled) {
@@ -466,12 +1249,12 @@ var _FetchInterceptor = class extends Interceptor {
466
1249
  this.logger.info(
467
1250
  "no mocked response received, performing request as-is..."
468
1251
  );
469
- return pureFetch(request).then((response) => {
1252
+ return pureFetch(request).then(async (response) => {
470
1253
  this.logger.info("original fetch performed", response);
471
1254
  if (this.emitter.listenerCount("response") > 0) {
472
1255
  this.logger.info('emitting the "response" event...');
473
1256
  const responseClone = response.clone();
474
- this.emitter.emit("response", {
1257
+ await emitAsync(this.emitter, "response", {
475
1258
  response: responseClone,
476
1259
  isMockedResponse: false,
477
1260
  request,
@@ -498,20 +1281,6 @@ var _FetchInterceptor = class extends Interceptor {
498
1281
  });
499
1282
  }
500
1283
  };
501
- var FetchInterceptor = _FetchInterceptor;
502
- FetchInterceptor.symbol = Symbol("fetch");
503
- function createNetworkError(cause) {
504
- return Object.assign(new TypeError("Failed to fetch"), {
505
- cause
506
- });
507
- }
508
-
509
- // src/interceptors/XMLHttpRequest/index.ts
510
- import { invariant as invariant4 } from "outvariant";
511
-
512
- // src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts
513
- import { invariant as invariant3 } from "outvariant";
514
- import { isNodeProcess } from "is-node-process";
515
1284
 
516
1285
  // src/interceptors/XMLHttpRequest/utils/concatArrayBuffer.ts
517
1286
  function concatArrayBuffer(left, right) {
@@ -524,10 +1293,10 @@ function concatArrayBuffer(left, right) {
524
1293
  // src/interceptors/XMLHttpRequest/polyfills/EventPolyfill.ts
525
1294
  var EventPolyfill = class {
526
1295
  constructor(type, options) {
527
- this.AT_TARGET = 0;
528
- this.BUBBLING_PHASE = 0;
529
- this.CAPTURING_PHASE = 0;
530
1296
  this.NONE = 0;
1297
+ this.CAPTURING_PHASE = 1;
1298
+ this.AT_TARGET = 2;
1299
+ this.BUBBLING_PHASE = 3;
531
1300
  this.type = "";
532
1301
  this.srcElement = null;
533
1302
  this.currentTarget = null;
@@ -543,8 +1312,8 @@ var EventPolyfill = class {
543
1312
  this.cancelBubble = false;
544
1313
  this.returnValue = true;
545
1314
  this.type = type;
546
- this.target = (options == null ? void 0 : options.target) || null;
547
- this.currentTarget = (options == null ? void 0 : options.currentTarget) || null;
1315
+ this.target = options?.target || null;
1316
+ this.currentTarget = options?.currentTarget || null;
548
1317
  this.timeStamp = Date.now();
549
1318
  }
550
1319
  composedPath() {
@@ -568,10 +1337,10 @@ var EventPolyfill = class {
568
1337
  var ProgressEventPolyfill = class extends EventPolyfill {
569
1338
  constructor(type, init) {
570
1339
  super(type);
571
- this.lengthComputable = (init == null ? void 0 : init.lengthComputable) || false;
572
- this.composed = (init == null ? void 0 : init.composed) || false;
573
- this.loaded = (init == null ? void 0 : init.loaded) || 0;
574
- this.total = (init == null ? void 0 : init.total) || 0;
1340
+ this.lengthComputable = init?.lengthComputable || false;
1341
+ this.composed = init?.composed || false;
1342
+ this.loaded = init?.loaded || 0;
1343
+ this.total = init?.total || 0;
575
1344
  }
576
1345
  };
577
1346
 
@@ -590,8 +1359,8 @@ function createEvent(target, type, init) {
590
1359
  const ProgressEventClass = SUPPORTS_PROGRESS_EVENT ? ProgressEvent : ProgressEventPolyfill;
591
1360
  const event = progressEvents.includes(type) ? new ProgressEventClass(type, {
592
1361
  lengthComputable: true,
593
- loaded: (init == null ? void 0 : init.loaded) || 0,
594
- total: (init == null ? void 0 : init.total) || 0
1362
+ loaded: init?.loaded || 0,
1363
+ total: init?.total || 0
595
1364
  }) : new EventPolyfill(type, {
596
1365
  target,
597
1366
  currentTarget: target
@@ -649,7 +1418,7 @@ function optionsToProxyHandler(options) {
649
1418
  propertySource,
650
1419
  propertyName
651
1420
  );
652
- if (typeof (ownDescriptors == null ? void 0 : ownDescriptors.set) !== "undefined") {
1421
+ if (typeof ownDescriptors?.set !== "undefined") {
653
1422
  ownDescriptors.set.apply(target, [nextValue]);
654
1423
  return true;
655
1424
  }
@@ -731,16 +1500,30 @@ function createHeadersFromXMLHttpReqestHeaders(headersString) {
731
1500
  return headers;
732
1501
  }
733
1502
 
1503
+ // src/interceptors/XMLHttpRequest/utils/getBodyByteLength.ts
1504
+ async function getBodyByteLength(input) {
1505
+ const explicitContentLength = input.headers.get("content-length");
1506
+ if (explicitContentLength != null && explicitContentLength !== "") {
1507
+ return Number(explicitContentLength);
1508
+ }
1509
+ const buffer = await input.arrayBuffer();
1510
+ return buffer.byteLength;
1511
+ }
1512
+
734
1513
  // src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts
735
- var IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
736
- var IS_NODE = isNodeProcess();
1514
+ var kIsRequestHandled = Symbol("kIsRequestHandled");
1515
+ var IS_NODE2 = isNodeProcess();
1516
+ var kFetchRequest = Symbol("kFetchRequest");
1517
+ kIsRequestHandled, kFetchRequest;
737
1518
  var XMLHttpRequestController = class {
738
1519
  constructor(initialRequest, logger) {
739
1520
  this.initialRequest = initialRequest;
740
1521
  this.logger = logger;
741
1522
  this.method = "GET";
742
1523
  this.url = null;
1524
+ this[kIsRequestHandled] = false;
743
1525
  this.events = /* @__PURE__ */ new Map();
1526
+ this.uploadEvents = /* @__PURE__ */ new Map();
744
1527
  this.requestId = createRequestId();
745
1528
  this.requestHeaders = new Headers();
746
1529
  this.responseBuffer = new Uint8Array();
@@ -760,7 +1543,6 @@ var XMLHttpRequestController = class {
760
1543
  }
761
1544
  },
762
1545
  methodCall: ([methodName, args], invoke) => {
763
- var _a;
764
1546
  switch (methodName) {
765
1547
  case "open": {
766
1548
  const [method, url] = args;
@@ -789,9 +1571,6 @@ var XMLHttpRequestController = class {
789
1571
  }
790
1572
  case "send": {
791
1573
  const [body] = args;
792
- if (body != null) {
793
- this.requestBody = typeof body === "string" ? encodeBuffer(body) : body;
794
- }
795
1574
  this.request.addEventListener("load", () => {
796
1575
  if (typeof this.onResponse !== "undefined") {
797
1576
  const fetchResponse = createResponse(
@@ -805,24 +1584,26 @@ var XMLHttpRequestController = class {
805
1584
  );
806
1585
  this.onResponse.call(this, {
807
1586
  response: fetchResponse,
808
- isMockedResponse: IS_MOCKED_RESPONSE in this.request,
1587
+ isMockedResponse: this[kIsRequestHandled],
809
1588
  request: fetchRequest,
810
1589
  requestId: this.requestId
811
1590
  });
812
1591
  }
813
1592
  });
814
- const fetchRequest = this.toFetchApiRequest();
815
- const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
1593
+ const requestBody = typeof body === "string" ? encodeBuffer(body) : body;
1594
+ const fetchRequest = this.toFetchApiRequest(requestBody);
1595
+ this[kFetchRequest] = fetchRequest.clone();
1596
+ const onceRequestSettled = this.onRequest?.call(this, {
816
1597
  request: fetchRequest,
817
1598
  requestId: this.requestId
818
- })) || Promise.resolve();
1599
+ }) || Promise.resolve();
819
1600
  onceRequestSettled.finally(() => {
820
- if (this.request.readyState < this.request.LOADING) {
1601
+ if (!this[kIsRequestHandled]) {
821
1602
  this.logger.info(
822
1603
  "request callback settled but request has not been handled (readystate %d), performing as-is...",
823
1604
  this.request.readyState
824
1605
  );
825
- if (IS_NODE) {
1606
+ if (IS_NODE2) {
826
1607
  this.request.setRequestHeader(
827
1608
  INTERNAL_REQUEST_ID_HEADER_NAME,
828
1609
  this.requestId
@@ -839,6 +1620,39 @@ var XMLHttpRequestController = class {
839
1620
  }
840
1621
  }
841
1622
  });
1623
+ define(
1624
+ this.request,
1625
+ "upload",
1626
+ createProxy(this.request.upload, {
1627
+ setProperty: ([propertyName, nextValue], invoke) => {
1628
+ switch (propertyName) {
1629
+ case "onloadstart":
1630
+ case "onprogress":
1631
+ case "onaboart":
1632
+ case "onerror":
1633
+ case "onload":
1634
+ case "ontimeout":
1635
+ case "onloadend": {
1636
+ const eventName = propertyName.slice(
1637
+ 2
1638
+ );
1639
+ this.registerUploadEvent(eventName, nextValue);
1640
+ }
1641
+ }
1642
+ return invoke();
1643
+ },
1644
+ methodCall: ([methodName, args], invoke) => {
1645
+ switch (methodName) {
1646
+ case "addEventListener": {
1647
+ const [eventName, listener] = args;
1648
+ this.registerUploadEvent(eventName, listener);
1649
+ this.logger.info("upload.addEventListener", eventName, listener);
1650
+ return invoke();
1651
+ }
1652
+ }
1653
+ }
1654
+ })
1655
+ );
842
1656
  }
843
1657
  registerEvent(eventName, listener) {
844
1658
  const prevEvents = this.events.get(eventName) || [];
@@ -846,17 +1660,44 @@ var XMLHttpRequestController = class {
846
1660
  this.events.set(eventName, nextEvents);
847
1661
  this.logger.info('registered event "%s"', eventName, listener);
848
1662
  }
1663
+ registerUploadEvent(eventName, listener) {
1664
+ const prevEvents = this.uploadEvents.get(eventName) || [];
1665
+ const nextEvents = prevEvents.concat(listener);
1666
+ this.uploadEvents.set(eventName, nextEvents);
1667
+ this.logger.info('registered upload event "%s"', eventName, listener);
1668
+ }
849
1669
  /**
850
1670
  * Responds to the current request with the given
851
1671
  * Fetch API `Response` instance.
852
1672
  */
853
- respondWith(response) {
1673
+ async respondWith(response) {
1674
+ this[kIsRequestHandled] = true;
1675
+ if (this[kFetchRequest]) {
1676
+ const totalRequestBodyLength = await getBodyByteLength(
1677
+ this[kFetchRequest]
1678
+ );
1679
+ this.trigger("loadstart", this.request.upload, {
1680
+ loaded: 0,
1681
+ total: totalRequestBodyLength
1682
+ });
1683
+ this.trigger("progress", this.request.upload, {
1684
+ loaded: totalRequestBodyLength,
1685
+ total: totalRequestBodyLength
1686
+ });
1687
+ this.trigger("load", this.request.upload, {
1688
+ loaded: totalRequestBodyLength,
1689
+ total: totalRequestBodyLength
1690
+ });
1691
+ this.trigger("loadend", this.request.upload, {
1692
+ loaded: totalRequestBodyLength,
1693
+ total: totalRequestBodyLength
1694
+ });
1695
+ }
854
1696
  this.logger.info(
855
1697
  "responding with a mocked response: %d %s",
856
1698
  response.status,
857
1699
  response.statusText
858
1700
  );
859
- define(this.request, IS_MOCKED_RESPONSE, true);
860
1701
  define(this.request, "status", response.status);
861
1702
  define(this.request, "statusText", response.statusText);
862
1703
  define(this.request, "responseURL", this.url.href);
@@ -911,14 +1752,9 @@ var XMLHttpRequestController = class {
911
1752
  get: () => this.responseXML
912
1753
  }
913
1754
  });
914
- const totalResponseBodyLength = response.headers.has("Content-Length") ? Number(response.headers.get("Content-Length")) : (
915
- /**
916
- * @todo Infer the response body length from the response body.
917
- */
918
- void 0
919
- );
1755
+ const totalResponseBodyLength = await getBodyByteLength(response.clone());
920
1756
  this.logger.info("calculated response body length", totalResponseBodyLength);
921
- this.trigger("loadstart", {
1757
+ this.trigger("loadstart", this.request, {
922
1758
  loaded: 0,
923
1759
  total: totalResponseBodyLength
924
1760
  });
@@ -927,11 +1763,11 @@ var XMLHttpRequestController = class {
927
1763
  const finalizeResponse = () => {
928
1764
  this.logger.info("finalizing the mocked response...");
929
1765
  this.setReadyState(this.request.DONE);
930
- this.trigger("load", {
1766
+ this.trigger("load", this.request, {
931
1767
  loaded: this.responseBuffer.byteLength,
932
1768
  total: totalResponseBodyLength
933
1769
  });
934
- this.trigger("loadend", {
1770
+ this.trigger("loadend", this.request, {
935
1771
  loaded: this.responseBuffer.byteLength,
936
1772
  total: totalResponseBodyLength
937
1773
  });
@@ -949,7 +1785,7 @@ var XMLHttpRequestController = class {
949
1785
  if (value) {
950
1786
  this.logger.info("read response body chunk:", value);
951
1787
  this.responseBuffer = concatArrayBuffer(this.responseBuffer, value);
952
- this.trigger("progress", {
1788
+ this.trigger("progress", this.request, {
953
1789
  loaded: this.responseBuffer.byteLength,
954
1790
  total: totalResponseBodyLength
955
1791
  });
@@ -1007,7 +1843,7 @@ var XMLHttpRequestController = class {
1007
1843
  }
1008
1844
  }
1009
1845
  get responseText() {
1010
- invariant3(
1846
+ invariant(
1011
1847
  this.request.responseType === "" || this.request.responseType === "text",
1012
1848
  "InvalidStateError: The object is in invalid state."
1013
1849
  );
@@ -1019,7 +1855,7 @@ var XMLHttpRequestController = class {
1019
1855
  return responseText;
1020
1856
  }
1021
1857
  get responseXML() {
1022
- invariant3(
1858
+ invariant(
1023
1859
  this.request.responseType === "" || this.request.responseType === "document",
1024
1860
  "InvalidStateError: The object is in invalid state."
1025
1861
  );
@@ -1041,11 +1877,12 @@ var XMLHttpRequestController = class {
1041
1877
  }
1042
1878
  return null;
1043
1879
  }
1044
- errorWith(error) {
1880
+ errorWith(error2) {
1881
+ this[kIsRequestHandled] = true;
1045
1882
  this.logger.info("responding with an error");
1046
1883
  this.setReadyState(this.request.DONE);
1047
- this.trigger("error");
1048
- this.trigger("loadend");
1884
+ this.trigger("error", this.request);
1885
+ this.trigger("loadend", this.request);
1049
1886
  }
1050
1887
  /**
1051
1888
  * Transitions this request's `readyState` to the given one.
@@ -1064,36 +1901,38 @@ var XMLHttpRequestController = class {
1064
1901
  this.logger.info("set readyState to: %d", nextReadyState);
1065
1902
  if (nextReadyState !== this.request.UNSENT) {
1066
1903
  this.logger.info('triggerring "readystatechange" event...');
1067
- this.trigger("readystatechange");
1904
+ this.trigger("readystatechange", this.request);
1068
1905
  }
1069
1906
  }
1070
1907
  /**
1071
1908
  * Triggers given event on the `XMLHttpRequest` instance.
1072
1909
  */
1073
- trigger(eventName, options) {
1074
- const callback = this.request[`on${eventName}`];
1075
- const event = createEvent(this.request, eventName, options);
1910
+ trigger(eventName, target, options) {
1911
+ const callback = target[`on${eventName}`];
1912
+ const event = createEvent(target, eventName, options);
1076
1913
  this.logger.info('trigger "%s"', eventName, options || "");
1077
1914
  if (typeof callback === "function") {
1078
1915
  this.logger.info('found a direct "%s" callback, calling...', eventName);
1079
- callback.call(this.request, event);
1916
+ callback.call(target, event);
1080
1917
  }
1081
- for (const [registeredEventName, listeners] of this.events) {
1918
+ const events = target instanceof XMLHttpRequestUpload ? this.uploadEvents : this.events;
1919
+ for (const [registeredEventName, listeners] of events) {
1082
1920
  if (registeredEventName === eventName) {
1083
1921
  this.logger.info(
1084
1922
  'found %d listener(s) for "%s" event, calling...',
1085
1923
  listeners.length,
1086
1924
  eventName
1087
1925
  );
1088
- listeners.forEach((listener) => listener.call(this.request, event));
1926
+ listeners.forEach((listener) => listener.call(target, event));
1089
1927
  }
1090
1928
  }
1091
1929
  }
1092
1930
  /**
1093
1931
  * Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.
1094
1932
  */
1095
- toFetchApiRequest() {
1933
+ toFetchApiRequest(body) {
1096
1934
  this.logger.info("converting request to a Fetch API Request...");
1935
+ const resolvedBody = body instanceof Document ? body.documentElement.innerText : body;
1097
1936
  const fetchRequest = new Request(this.url.href, {
1098
1937
  method: this.method,
1099
1938
  headers: this.requestHeaders,
@@ -1101,7 +1940,7 @@ var XMLHttpRequestController = class {
1101
1940
  * @see https://xhr.spec.whatwg.org/#cross-origin-credentials
1102
1941
  */
1103
1942
  credentials: this.request.withCredentials ? "include" : "same-origin",
1104
- body: ["GET", "HEAD"].includes(this.method) ? null : this.requestBody
1943
+ body: ["GET", "HEAD"].includes(this.method.toUpperCase()) ? null : resolvedBody
1105
1944
  });
1106
1945
  const proxyHeaders = createProxy(fetchRequest.headers, {
1107
1946
  methodCall: ([methodName, args], invoke) => {
@@ -1182,16 +2021,16 @@ function createXMLHttpRequestProxy({
1182
2021
  requestId,
1183
2022
  controller,
1184
2023
  emitter,
1185
- onResponse: (response) => {
1186
- this.respondWith(response);
2024
+ onResponse: async (response) => {
2025
+ await this.respondWith(response);
1187
2026
  },
1188
2027
  onRequestError: () => {
1189
2028
  this.errorWith(new TypeError("Network error"));
1190
2029
  },
1191
- onError: (error) => {
1192
- this.logger.info("request errored!", { error });
1193
- if (error instanceof Error) {
1194
- this.errorWith(error);
2030
+ onError: (error2) => {
2031
+ this.logger.info("request errored!", { error: error2 });
2032
+ if (error2 instanceof Error) {
2033
+ this.errorWith(error2);
1195
2034
  }
1196
2035
  }
1197
2036
  });
@@ -1225,18 +2064,21 @@ function createXMLHttpRequestProxy({
1225
2064
  }
1226
2065
 
1227
2066
  // src/interceptors/XMLHttpRequest/index.ts
1228
- var _XMLHttpRequestInterceptor = class extends Interceptor {
2067
+ var XMLHttpRequestInterceptor = class _XMLHttpRequestInterceptor extends Interceptor {
2068
+ static {
2069
+ this.interceptorSymbol = Symbol("xhr");
2070
+ }
1229
2071
  constructor() {
1230
2072
  super(_XMLHttpRequestInterceptor.interceptorSymbol);
1231
2073
  }
1232
2074
  checkEnvironment() {
1233
- return typeof globalThis.XMLHttpRequest !== "undefined";
2075
+ return hasConfigurableGlobal("XMLHttpRequest");
1234
2076
  }
1235
2077
  setup() {
1236
2078
  const logger = this.logger.extend("setup");
1237
2079
  logger.info('patching "XMLHttpRequest" module...');
1238
2080
  const PureXMLHttpRequest = globalThis.XMLHttpRequest;
1239
- invariant4(
2081
+ invariant(
1240
2082
  !PureXMLHttpRequest[IS_PATCHED_MODULE],
1241
2083
  'Failed to patch the "XMLHttpRequest" module: already patched.'
1242
2084
  );
@@ -1265,8 +2107,6 @@ var _XMLHttpRequestInterceptor = class extends Interceptor {
1265
2107
  });
1266
2108
  }
1267
2109
  };
1268
- var XMLHttpRequestInterceptor = _XMLHttpRequestInterceptor;
1269
- XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
1270
2110
 
1271
2111
  // src/presets/browser.ts
1272
2112
  var browser_default = [