msw 2.1.3 → 2.1.5
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.
- package/lib/browser/index.js +1775 -32
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +1769 -28
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/handlers/GraphQLHandler.js +2 -2
- package/lib/core/handlers/GraphQLHandler.js.map +1 -1
- package/lib/core/handlers/GraphQLHandler.mjs +2 -2
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
- package/lib/core/handlers/HttpHandler.js +2 -2
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs +2 -2
- package/lib/core/handlers/HttpHandler.mjs.map +1 -1
- package/lib/core/sharedOptions.d.mts +0 -2
- package/lib/core/sharedOptions.d.ts +0 -2
- package/lib/core/utils/handleRequest.js +1 -1
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.js +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.d.mts +1 -4
- package/lib/core/utils/request/onUnhandledRequest.d.ts +1 -4
- package/lib/core/utils/request/onUnhandledRequest.js +14 -115
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs +14 -107
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
- package/lib/core/utils/request/toPublicUrl.d.mts +7 -0
- package/lib/core/utils/request/toPublicUrl.d.ts +7 -0
- package/lib/core/utils/request/{getPublicUrlFromRequest.js → toPublicUrl.js} +9 -9
- package/lib/core/utils/request/toPublicUrl.js.map +1 -0
- package/lib/core/utils/request/toPublicUrl.mjs +11 -0
- package/lib/core/utils/request/toPublicUrl.mjs.map +1 -0
- package/lib/iife/index.js +46 -233
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +5 -8
- package/src/browser/setupWorker/glossary.ts +1 -0
- package/src/browser/setupWorker/setupWorker.ts +1 -0
- package/src/browser/setupWorker/start/createRequestListener.ts +9 -0
- package/src/browser/setupWorker/start/createResponseListener.ts +9 -7
- package/src/core/handlers/GraphQLHandler.ts +2 -2
- package/src/core/handlers/HttpHandler.ts +2 -2
- package/src/core/utils/handleRequest.ts +1 -1
- package/src/core/utils/internal/parseGraphQLRequest.ts +2 -2
- package/src/core/utils/request/onUnhandledRequest.test.ts +5 -101
- package/src/core/utils/request/onUnhandledRequest.ts +16 -185
- package/src/core/utils/request/toPublicUrl.test.ts +18 -0
- package/src/core/utils/request/toPublicUrl.ts +15 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +0 -7
- package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +0 -7
- package/lib/core/utils/request/getPublicUrlFromRequest.js.map +0 -1
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs +0 -11
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +0 -1
- package/src/core/utils/request/getPublicUrlFromRequest.test.ts +0 -26
- package/src/core/utils/request/getPublicUrlFromRequest.ts +0 -15
package/lib/browser/index.js
CHANGED
|
@@ -25,16 +25,119 @@ __export(browser_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(browser_exports);
|
|
27
27
|
|
|
28
|
-
//
|
|
29
|
-
var
|
|
30
|
-
|
|
28
|
+
// node_modules/.pnpm/outvariant@1.4.2/node_modules/outvariant/lib/index.mjs
|
|
29
|
+
var POSITIONALS_EXP = /(%?)(%([sdijo]))/g;
|
|
30
|
+
function serializePositional(positional, flag) {
|
|
31
|
+
switch (flag) {
|
|
32
|
+
case "s":
|
|
33
|
+
return positional;
|
|
34
|
+
case "d":
|
|
35
|
+
case "i":
|
|
36
|
+
return Number(positional);
|
|
37
|
+
case "j":
|
|
38
|
+
return JSON.stringify(positional);
|
|
39
|
+
case "o": {
|
|
40
|
+
if (typeof positional === "string") {
|
|
41
|
+
return positional;
|
|
42
|
+
}
|
|
43
|
+
const json = JSON.stringify(positional);
|
|
44
|
+
if (json === "{}" || json === "[]" || /^\[object .+?\]$/.test(json)) {
|
|
45
|
+
return positional;
|
|
46
|
+
}
|
|
47
|
+
return json;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function format(message, ...positionals) {
|
|
52
|
+
if (positionals.length === 0) {
|
|
53
|
+
return message;
|
|
54
|
+
}
|
|
55
|
+
let positionalIndex = 0;
|
|
56
|
+
let formattedMessage = message.replace(
|
|
57
|
+
POSITIONALS_EXP,
|
|
58
|
+
(match, isEscaped, _, flag) => {
|
|
59
|
+
const positional = positionals[positionalIndex];
|
|
60
|
+
const value = serializePositional(positional, flag);
|
|
61
|
+
if (!isEscaped) {
|
|
62
|
+
positionalIndex++;
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
return match;
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
if (positionalIndex < positionals.length) {
|
|
69
|
+
formattedMessage += ` ${positionals.slice(positionalIndex).join(" ")}`;
|
|
70
|
+
}
|
|
71
|
+
formattedMessage = formattedMessage.replace(/%{2,2}/g, "%");
|
|
72
|
+
return formattedMessage;
|
|
73
|
+
}
|
|
74
|
+
var STACK_FRAMES_TO_IGNORE = 2;
|
|
75
|
+
function cleanErrorStack(error2) {
|
|
76
|
+
if (!error2.stack) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const nextStack = error2.stack.split("\n");
|
|
80
|
+
nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
|
|
81
|
+
error2.stack = nextStack.join("\n");
|
|
82
|
+
}
|
|
83
|
+
var InvariantError = class extends Error {
|
|
84
|
+
constructor(message, ...positionals) {
|
|
85
|
+
super(message);
|
|
86
|
+
this.message = message;
|
|
87
|
+
this.name = "Invariant Violation";
|
|
88
|
+
this.message = format(message, ...positionals);
|
|
89
|
+
cleanErrorStack(this);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var invariant = (predicate, message, ...positionals) => {
|
|
93
|
+
if (!predicate) {
|
|
94
|
+
throw new InvariantError(message, ...positionals);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
invariant.as = (ErrorConstructor, predicate, message, ...positionals) => {
|
|
98
|
+
if (!predicate) {
|
|
99
|
+
const formatMessage = positionals.length === 0 ? message : format(message, positionals);
|
|
100
|
+
let error2;
|
|
101
|
+
try {
|
|
102
|
+
error2 = Reflect.construct(ErrorConstructor, [formatMessage]);
|
|
103
|
+
} catch (err) {
|
|
104
|
+
error2 = ErrorConstructor(formatMessage);
|
|
105
|
+
}
|
|
106
|
+
throw error2;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// node_modules/.pnpm/is-node-process@1.2.0/node_modules/is-node-process/lib/index.mjs
|
|
111
|
+
function isNodeProcess() {
|
|
112
|
+
if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
if (typeof process !== "undefined") {
|
|
116
|
+
const type = process.type;
|
|
117
|
+
if (type === "renderer" || type === "worker") {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return !!(process.versions && process.versions.node);
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// node_modules/.pnpm/@open-draft+until@2.1.0/node_modules/@open-draft/until/lib/index.mjs
|
|
126
|
+
var until = async (promise) => {
|
|
127
|
+
try {
|
|
128
|
+
const data = await promise().catch((error2) => {
|
|
129
|
+
throw error2;
|
|
130
|
+
});
|
|
131
|
+
return { error: null, data };
|
|
132
|
+
} catch (error2) {
|
|
133
|
+
return { error: error2, data: null };
|
|
134
|
+
}
|
|
135
|
+
};
|
|
31
136
|
|
|
32
137
|
// src/browser/setupWorker/start/createStartHandler.ts
|
|
33
|
-
var import_until2 = require("@open-draft/until");
|
|
34
138
|
var import_devUtils6 = require("../core/utils/internal/devUtils.js");
|
|
35
139
|
|
|
36
140
|
// src/browser/setupWorker/start/utils/getWorkerInstance.ts
|
|
37
|
-
var import_until = require("@open-draft/until");
|
|
38
141
|
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
39
142
|
|
|
40
143
|
// src/browser/utils/getAbsoluteWorkerUrl.ts
|
|
@@ -82,7 +185,7 @@ var getWorkerInstance = async (url, options = {}, findWorker) => {
|
|
|
82
185
|
];
|
|
83
186
|
});
|
|
84
187
|
}
|
|
85
|
-
const registrationResult = await
|
|
188
|
+
const registrationResult = await until(
|
|
86
189
|
async () => {
|
|
87
190
|
const registration = await navigator.serviceWorker.register(url, options);
|
|
88
191
|
return [
|
|
@@ -190,6 +293,7 @@ function parseWorkerRequest(incomingRequest) {
|
|
|
190
293
|
}
|
|
191
294
|
|
|
192
295
|
// src/browser/setupWorker/start/createRequestListener.ts
|
|
296
|
+
var import_RequestHandler = require("../core/handlers/RequestHandler.js");
|
|
193
297
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
194
298
|
var import_devUtils4 = require("../core/utils/internal/devUtils.js");
|
|
195
299
|
var import_toResponseInit = require("../core/utils/toResponseInit.js");
|
|
@@ -199,6 +303,9 @@ var createRequestListener = (context, options) => {
|
|
|
199
303
|
const requestId = message.payload.id;
|
|
200
304
|
const request = parseWorkerRequest(message.payload);
|
|
201
305
|
const requestCloneForLogs = request.clone();
|
|
306
|
+
const requestClone = request.clone();
|
|
307
|
+
import_RequestHandler.RequestHandler.cache.set(request, requestClone);
|
|
308
|
+
context.requests.set(requestId, requestClone);
|
|
202
309
|
try {
|
|
203
310
|
await (0, import_handleRequest.handleRequest)(
|
|
204
311
|
request,
|
|
@@ -243,8 +350,8 @@ var createRequestListener = (context, options) => {
|
|
|
243
350
|
}
|
|
244
351
|
}
|
|
245
352
|
);
|
|
246
|
-
} catch (
|
|
247
|
-
if (
|
|
353
|
+
} catch (error2) {
|
|
354
|
+
if (error2 instanceof Error) {
|
|
248
355
|
import_devUtils4.devUtils.error(
|
|
249
356
|
`Uncaught exception in the request handler for "%s %s":
|
|
250
357
|
|
|
@@ -253,7 +360,7 @@ var createRequestListener = (context, options) => {
|
|
|
253
360
|
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses`,
|
|
254
361
|
request.method,
|
|
255
362
|
request.url,
|
|
256
|
-
|
|
363
|
+
error2.stack ?? error2
|
|
257
364
|
);
|
|
258
365
|
messageChannel.postMessage("MOCK_RESPONSE", {
|
|
259
366
|
status: 500,
|
|
@@ -262,9 +369,9 @@ This exception has been gracefully handled as a 500 response, however, it's stro
|
|
|
262
369
|
"Content-Type": "application/json"
|
|
263
370
|
},
|
|
264
371
|
body: JSON.stringify({
|
|
265
|
-
name:
|
|
266
|
-
message:
|
|
267
|
-
stack:
|
|
372
|
+
name: error2.name,
|
|
373
|
+
message: error2.message,
|
|
374
|
+
stack: error2.stack
|
|
268
375
|
})
|
|
269
376
|
});
|
|
270
377
|
}
|
|
@@ -286,11 +393,664 @@ async function requestIntegrityCheck(context, serviceWorker) {
|
|
|
286
393
|
return serviceWorker;
|
|
287
394
|
}
|
|
288
395
|
|
|
396
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/chunk-UJZOJSMP.mjs
|
|
397
|
+
var encoder = new TextEncoder();
|
|
398
|
+
function encodeBuffer(text) {
|
|
399
|
+
return encoder.encode(text);
|
|
400
|
+
}
|
|
401
|
+
function decodeBuffer(buffer, encoding) {
|
|
402
|
+
const decoder = new TextDecoder(encoding);
|
|
403
|
+
return decoder.decode(buffer);
|
|
404
|
+
}
|
|
405
|
+
function toArrayBuffer(array) {
|
|
406
|
+
return array.buffer.slice(
|
|
407
|
+
array.byteOffset,
|
|
408
|
+
array.byteOffset + array.byteLength
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
var RESPONSE_STATUS_CODES_WITHOUT_BODY = /* @__PURE__ */ new Set([
|
|
412
|
+
101,
|
|
413
|
+
103,
|
|
414
|
+
204,
|
|
415
|
+
205,
|
|
416
|
+
304
|
|
417
|
+
]);
|
|
418
|
+
function isResponseWithoutBody(status) {
|
|
419
|
+
return RESPONSE_STATUS_CODES_WITHOUT_BODY.has(status);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// node_modules/.pnpm/@open-draft+logger@0.3.0/node_modules/@open-draft/logger/lib/index.mjs
|
|
423
|
+
var __defProp2 = Object.defineProperty;
|
|
424
|
+
var __export2 = (target, all) => {
|
|
425
|
+
for (var name in all)
|
|
426
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
427
|
+
};
|
|
428
|
+
var colors_exports = {};
|
|
429
|
+
__export2(colors_exports, {
|
|
430
|
+
blue: () => blue,
|
|
431
|
+
gray: () => gray,
|
|
432
|
+
green: () => green,
|
|
433
|
+
red: () => red,
|
|
434
|
+
yellow: () => yellow
|
|
435
|
+
});
|
|
436
|
+
function yellow(text) {
|
|
437
|
+
return `\x1B[33m${text}\x1B[0m`;
|
|
438
|
+
}
|
|
439
|
+
function blue(text) {
|
|
440
|
+
return `\x1B[34m${text}\x1B[0m`;
|
|
441
|
+
}
|
|
442
|
+
function gray(text) {
|
|
443
|
+
return `\x1B[90m${text}\x1B[0m`;
|
|
444
|
+
}
|
|
445
|
+
function red(text) {
|
|
446
|
+
return `\x1B[31m${text}\x1B[0m`;
|
|
447
|
+
}
|
|
448
|
+
function green(text) {
|
|
449
|
+
return `\x1B[32m${text}\x1B[0m`;
|
|
450
|
+
}
|
|
451
|
+
var IS_NODE = isNodeProcess();
|
|
452
|
+
var Logger = class {
|
|
453
|
+
constructor(name) {
|
|
454
|
+
this.name = name;
|
|
455
|
+
this.prefix = `[${this.name}]`;
|
|
456
|
+
const LOGGER_NAME = getVariable("DEBUG");
|
|
457
|
+
const LOGGER_LEVEL = getVariable("LOG_LEVEL");
|
|
458
|
+
const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME);
|
|
459
|
+
if (isLoggingEnabled) {
|
|
460
|
+
this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug;
|
|
461
|
+
this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info;
|
|
462
|
+
this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success;
|
|
463
|
+
this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning;
|
|
464
|
+
this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error;
|
|
465
|
+
} else {
|
|
466
|
+
this.info = noop;
|
|
467
|
+
this.success = noop;
|
|
468
|
+
this.warning = noop;
|
|
469
|
+
this.error = noop;
|
|
470
|
+
this.only = noop;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
prefix;
|
|
474
|
+
extend(domain) {
|
|
475
|
+
return new Logger(`${this.name}:${domain}`);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Print a debug message.
|
|
479
|
+
* @example
|
|
480
|
+
* logger.debug('no duplicates found, creating a document...')
|
|
481
|
+
*/
|
|
482
|
+
debug(message, ...positionals) {
|
|
483
|
+
this.logEntry({
|
|
484
|
+
level: "debug",
|
|
485
|
+
message: gray(message),
|
|
486
|
+
positionals,
|
|
487
|
+
prefix: this.prefix,
|
|
488
|
+
colors: {
|
|
489
|
+
prefix: "gray"
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Print an info message.
|
|
495
|
+
* @example
|
|
496
|
+
* logger.info('start parsing...')
|
|
497
|
+
*/
|
|
498
|
+
info(message, ...positionals) {
|
|
499
|
+
this.logEntry({
|
|
500
|
+
level: "info",
|
|
501
|
+
message,
|
|
502
|
+
positionals,
|
|
503
|
+
prefix: this.prefix,
|
|
504
|
+
colors: {
|
|
505
|
+
prefix: "blue"
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
const performance2 = new PerformanceEntry();
|
|
509
|
+
return (message2, ...positionals2) => {
|
|
510
|
+
performance2.measure();
|
|
511
|
+
this.logEntry({
|
|
512
|
+
level: "info",
|
|
513
|
+
message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`,
|
|
514
|
+
positionals: positionals2,
|
|
515
|
+
prefix: this.prefix,
|
|
516
|
+
colors: {
|
|
517
|
+
prefix: "blue"
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Print a success message.
|
|
524
|
+
* @example
|
|
525
|
+
* logger.success('successfully created document')
|
|
526
|
+
*/
|
|
527
|
+
success(message, ...positionals) {
|
|
528
|
+
this.logEntry({
|
|
529
|
+
level: "info",
|
|
530
|
+
message,
|
|
531
|
+
positionals,
|
|
532
|
+
prefix: `\u2714 ${this.prefix}`,
|
|
533
|
+
colors: {
|
|
534
|
+
timestamp: "green",
|
|
535
|
+
prefix: "green"
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Print a warning.
|
|
541
|
+
* @example
|
|
542
|
+
* logger.warning('found legacy document format')
|
|
543
|
+
*/
|
|
544
|
+
warning(message, ...positionals) {
|
|
545
|
+
this.logEntry({
|
|
546
|
+
level: "warning",
|
|
547
|
+
message,
|
|
548
|
+
positionals,
|
|
549
|
+
prefix: `\u26A0 ${this.prefix}`,
|
|
550
|
+
colors: {
|
|
551
|
+
timestamp: "yellow",
|
|
552
|
+
prefix: "yellow"
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Print an error message.
|
|
558
|
+
* @example
|
|
559
|
+
* logger.error('something went wrong')
|
|
560
|
+
*/
|
|
561
|
+
error(message, ...positionals) {
|
|
562
|
+
this.logEntry({
|
|
563
|
+
level: "error",
|
|
564
|
+
message,
|
|
565
|
+
positionals,
|
|
566
|
+
prefix: `\u2716 ${this.prefix}`,
|
|
567
|
+
colors: {
|
|
568
|
+
timestamp: "red",
|
|
569
|
+
prefix: "red"
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Execute the given callback only when the logging is enabled.
|
|
575
|
+
* This is skipped in its entirety and has no runtime cost otherwise.
|
|
576
|
+
* This executes regardless of the log level.
|
|
577
|
+
* @example
|
|
578
|
+
* logger.only(() => {
|
|
579
|
+
* logger.info('additional info')
|
|
580
|
+
* })
|
|
581
|
+
*/
|
|
582
|
+
only(callback) {
|
|
583
|
+
callback();
|
|
584
|
+
}
|
|
585
|
+
createEntry(level, message) {
|
|
586
|
+
return {
|
|
587
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
588
|
+
level,
|
|
589
|
+
message
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
logEntry(args) {
|
|
593
|
+
const {
|
|
594
|
+
level,
|
|
595
|
+
message,
|
|
596
|
+
prefix,
|
|
597
|
+
colors: customColors,
|
|
598
|
+
positionals = []
|
|
599
|
+
} = args;
|
|
600
|
+
const entry = this.createEntry(level, message);
|
|
601
|
+
const timestampColor = customColors?.timestamp || "gray";
|
|
602
|
+
const prefixColor = customColors?.prefix || "gray";
|
|
603
|
+
const colorize = {
|
|
604
|
+
timestamp: colors_exports[timestampColor],
|
|
605
|
+
prefix: colors_exports[prefixColor]
|
|
606
|
+
};
|
|
607
|
+
const write = this.getWriter(level);
|
|
608
|
+
write(
|
|
609
|
+
[colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "),
|
|
610
|
+
...positionals.map(serializeInput)
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
formatTimestamp(timestamp) {
|
|
614
|
+
return `${timestamp.toLocaleTimeString(
|
|
615
|
+
"en-GB"
|
|
616
|
+
)}:${timestamp.getMilliseconds()}`;
|
|
617
|
+
}
|
|
618
|
+
getWriter(level) {
|
|
619
|
+
switch (level) {
|
|
620
|
+
case "debug":
|
|
621
|
+
case "success":
|
|
622
|
+
case "info": {
|
|
623
|
+
return log;
|
|
624
|
+
}
|
|
625
|
+
case "warning": {
|
|
626
|
+
return warn;
|
|
627
|
+
}
|
|
628
|
+
case "error": {
|
|
629
|
+
return error;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
var PerformanceEntry = class {
|
|
635
|
+
startTime;
|
|
636
|
+
endTime;
|
|
637
|
+
deltaTime;
|
|
638
|
+
constructor() {
|
|
639
|
+
this.startTime = performance.now();
|
|
640
|
+
}
|
|
641
|
+
measure() {
|
|
642
|
+
this.endTime = performance.now();
|
|
643
|
+
const deltaTime = this.endTime - this.startTime;
|
|
644
|
+
this.deltaTime = deltaTime.toFixed(2);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
var noop = () => void 0;
|
|
648
|
+
function log(message, ...positionals) {
|
|
649
|
+
if (IS_NODE) {
|
|
650
|
+
process.stdout.write(format(message, ...positionals) + "\n");
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
console.log(message, ...positionals);
|
|
654
|
+
}
|
|
655
|
+
function warn(message, ...positionals) {
|
|
656
|
+
if (IS_NODE) {
|
|
657
|
+
process.stderr.write(format(message, ...positionals) + "\n");
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
console.warn(message, ...positionals);
|
|
661
|
+
}
|
|
662
|
+
function error(message, ...positionals) {
|
|
663
|
+
if (IS_NODE) {
|
|
664
|
+
process.stderr.write(format(message, ...positionals) + "\n");
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
console.error(message, ...positionals);
|
|
668
|
+
}
|
|
669
|
+
function getVariable(variableName) {
|
|
670
|
+
if (IS_NODE) {
|
|
671
|
+
return process.env[variableName];
|
|
672
|
+
}
|
|
673
|
+
return globalThis[variableName]?.toString();
|
|
674
|
+
}
|
|
675
|
+
function isDefinedAndNotEquals(value, expected) {
|
|
676
|
+
return value !== void 0 && value !== expected;
|
|
677
|
+
}
|
|
678
|
+
function serializeInput(message) {
|
|
679
|
+
if (typeof message === "undefined") {
|
|
680
|
+
return "undefined";
|
|
681
|
+
}
|
|
682
|
+
if (message === null) {
|
|
683
|
+
return "null";
|
|
684
|
+
}
|
|
685
|
+
if (typeof message === "string") {
|
|
686
|
+
return message;
|
|
687
|
+
}
|
|
688
|
+
if (typeof message === "object") {
|
|
689
|
+
return JSON.stringify(message);
|
|
690
|
+
}
|
|
691
|
+
return message.toString();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// node_modules/.pnpm/strict-event-emitter@0.5.1/node_modules/strict-event-emitter/lib/index.mjs
|
|
695
|
+
var MemoryLeakError = class extends Error {
|
|
696
|
+
constructor(emitter, type, count) {
|
|
697
|
+
super(
|
|
698
|
+
`Possible EventEmitter memory leak detected. ${count} ${type.toString()} listeners added. Use emitter.setMaxListeners() to increase limit`
|
|
699
|
+
);
|
|
700
|
+
this.emitter = emitter;
|
|
701
|
+
this.type = type;
|
|
702
|
+
this.count = count;
|
|
703
|
+
this.name = "MaxListenersExceededWarning";
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
var _Emitter = class {
|
|
707
|
+
static listenerCount(emitter, eventName) {
|
|
708
|
+
return emitter.listenerCount(eventName);
|
|
709
|
+
}
|
|
710
|
+
constructor() {
|
|
711
|
+
this.events = /* @__PURE__ */ new Map();
|
|
712
|
+
this.maxListeners = _Emitter.defaultMaxListeners;
|
|
713
|
+
this.hasWarnedAboutPotentialMemoryLeak = false;
|
|
714
|
+
}
|
|
715
|
+
_emitInternalEvent(internalEventName, eventName, listener) {
|
|
716
|
+
this.emit(
|
|
717
|
+
internalEventName,
|
|
718
|
+
...[eventName, listener]
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
_getListeners(eventName) {
|
|
722
|
+
return Array.prototype.concat.apply([], this.events.get(eventName)) || [];
|
|
723
|
+
}
|
|
724
|
+
_removeListener(listeners, listener) {
|
|
725
|
+
const index = listeners.indexOf(listener);
|
|
726
|
+
if (index > -1) {
|
|
727
|
+
listeners.splice(index, 1);
|
|
728
|
+
}
|
|
729
|
+
return [];
|
|
730
|
+
}
|
|
731
|
+
_wrapOnceListener(eventName, listener) {
|
|
732
|
+
const onceListener = (...data) => {
|
|
733
|
+
this.removeListener(eventName, onceListener);
|
|
734
|
+
return listener.apply(this, data);
|
|
735
|
+
};
|
|
736
|
+
Object.defineProperty(onceListener, "name", { value: listener.name });
|
|
737
|
+
return onceListener;
|
|
738
|
+
}
|
|
739
|
+
setMaxListeners(maxListeners) {
|
|
740
|
+
this.maxListeners = maxListeners;
|
|
741
|
+
return this;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Returns the current max listener value for the `Emitter` which is
|
|
745
|
+
* either set by `emitter.setMaxListeners(n)` or defaults to
|
|
746
|
+
* `Emitter.defaultMaxListeners`.
|
|
747
|
+
*/
|
|
748
|
+
getMaxListeners() {
|
|
749
|
+
return this.maxListeners;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Returns an array listing the events for which the emitter has registered listeners.
|
|
753
|
+
* The values in the array will be strings or Symbols.
|
|
754
|
+
*/
|
|
755
|
+
eventNames() {
|
|
756
|
+
return Array.from(this.events.keys());
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Synchronously calls each of the listeners registered for the event named `eventName`,
|
|
760
|
+
* in the order they were registered, passing the supplied arguments to each.
|
|
761
|
+
* Returns `true` if the event has listeners, `false` otherwise.
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* const emitter = new Emitter<{ hello: [string] }>()
|
|
765
|
+
* emitter.emit('hello', 'John')
|
|
766
|
+
*/
|
|
767
|
+
emit(eventName, ...data) {
|
|
768
|
+
const listeners = this._getListeners(eventName);
|
|
769
|
+
listeners.forEach((listener) => {
|
|
770
|
+
listener.apply(this, data);
|
|
771
|
+
});
|
|
772
|
+
return listeners.length > 0;
|
|
773
|
+
}
|
|
774
|
+
addListener(eventName, listener) {
|
|
775
|
+
this._emitInternalEvent("newListener", eventName, listener);
|
|
776
|
+
const nextListeners = this._getListeners(eventName).concat(listener);
|
|
777
|
+
this.events.set(eventName, nextListeners);
|
|
778
|
+
if (this.maxListeners > 0 && this.listenerCount(eventName) > this.maxListeners && !this.hasWarnedAboutPotentialMemoryLeak) {
|
|
779
|
+
this.hasWarnedAboutPotentialMemoryLeak = true;
|
|
780
|
+
const memoryLeakWarning = new MemoryLeakError(
|
|
781
|
+
this,
|
|
782
|
+
eventName,
|
|
783
|
+
this.listenerCount(eventName)
|
|
784
|
+
);
|
|
785
|
+
console.warn(memoryLeakWarning);
|
|
786
|
+
}
|
|
787
|
+
return this;
|
|
788
|
+
}
|
|
789
|
+
on(eventName, listener) {
|
|
790
|
+
return this.addListener(eventName, listener);
|
|
791
|
+
}
|
|
792
|
+
once(eventName, listener) {
|
|
793
|
+
return this.addListener(
|
|
794
|
+
eventName,
|
|
795
|
+
this._wrapOnceListener(eventName, listener)
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
prependListener(eventName, listener) {
|
|
799
|
+
const listeners = this._getListeners(eventName);
|
|
800
|
+
if (listeners.length > 0) {
|
|
801
|
+
const nextListeners = [listener].concat(listeners);
|
|
802
|
+
this.events.set(eventName, nextListeners);
|
|
803
|
+
} else {
|
|
804
|
+
this.events.set(eventName, listeners.concat(listener));
|
|
805
|
+
}
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
808
|
+
prependOnceListener(eventName, listener) {
|
|
809
|
+
return this.prependListener(
|
|
810
|
+
eventName,
|
|
811
|
+
this._wrapOnceListener(eventName, listener)
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
removeListener(eventName, listener) {
|
|
815
|
+
const listeners = this._getListeners(eventName);
|
|
816
|
+
if (listeners.length > 0) {
|
|
817
|
+
this._removeListener(listeners, listener);
|
|
818
|
+
this.events.set(eventName, listeners);
|
|
819
|
+
this._emitInternalEvent("removeListener", eventName, listener);
|
|
820
|
+
}
|
|
821
|
+
return this;
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Alias for `emitter.removeListener()`.
|
|
825
|
+
*
|
|
826
|
+
* @example
|
|
827
|
+
* emitter.off('hello', listener)
|
|
828
|
+
*/
|
|
829
|
+
off(eventName, listener) {
|
|
830
|
+
return this.removeListener(eventName, listener);
|
|
831
|
+
}
|
|
832
|
+
removeAllListeners(eventName) {
|
|
833
|
+
if (eventName) {
|
|
834
|
+
this.events.delete(eventName);
|
|
835
|
+
} else {
|
|
836
|
+
this.events.clear();
|
|
837
|
+
}
|
|
838
|
+
return this;
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
842
|
+
*/
|
|
843
|
+
listeners(eventName) {
|
|
844
|
+
return Array.from(this._getListeners(eventName));
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Returns the number of listeners listening to the event named `eventName`.
|
|
848
|
+
*/
|
|
849
|
+
listenerCount(eventName) {
|
|
850
|
+
return this._getListeners(eventName).length;
|
|
851
|
+
}
|
|
852
|
+
rawListeners(eventName) {
|
|
853
|
+
return this.listeners(eventName);
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
var Emitter = _Emitter;
|
|
857
|
+
Emitter.defaultMaxListeners = 10;
|
|
858
|
+
|
|
859
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/chunk-WZQN3FMY.mjs
|
|
860
|
+
var IS_PATCHED_MODULE = Symbol("isPatchedModule");
|
|
861
|
+
function getGlobalSymbol(symbol) {
|
|
862
|
+
return (
|
|
863
|
+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/24587
|
|
864
|
+
globalThis[symbol] || void 0
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
function setGlobalSymbol(symbol, value) {
|
|
868
|
+
globalThis[symbol] = value;
|
|
869
|
+
}
|
|
870
|
+
function deleteGlobalSymbol(symbol) {
|
|
871
|
+
delete globalThis[symbol];
|
|
872
|
+
}
|
|
873
|
+
var Interceptor = class {
|
|
874
|
+
constructor(symbol) {
|
|
875
|
+
this.symbol = symbol;
|
|
876
|
+
this.readyState = "INACTIVE";
|
|
877
|
+
this.emitter = new Emitter();
|
|
878
|
+
this.subscriptions = [];
|
|
879
|
+
this.logger = new Logger(symbol.description);
|
|
880
|
+
this.emitter.setMaxListeners(0);
|
|
881
|
+
this.logger.info("constructing the interceptor...");
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Determine if this interceptor can be applied
|
|
885
|
+
* in the current environment.
|
|
886
|
+
*/
|
|
887
|
+
checkEnvironment() {
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Apply this interceptor to the current process.
|
|
892
|
+
* Returns an already running interceptor instance if it's present.
|
|
893
|
+
*/
|
|
894
|
+
apply() {
|
|
895
|
+
const logger = this.logger.extend("apply");
|
|
896
|
+
logger.info("applying the interceptor...");
|
|
897
|
+
if (this.readyState === "APPLIED") {
|
|
898
|
+
logger.info("intercepted already applied!");
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
const shouldApply = this.checkEnvironment();
|
|
902
|
+
if (!shouldApply) {
|
|
903
|
+
logger.info("the interceptor cannot be applied in this environment!");
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
this.readyState = "APPLYING";
|
|
907
|
+
const runningInstance = this.getInstance();
|
|
908
|
+
if (runningInstance) {
|
|
909
|
+
logger.info("found a running instance, reusing...");
|
|
910
|
+
this.on = (event, listener) => {
|
|
911
|
+
logger.info('proxying the "%s" listener', event);
|
|
912
|
+
runningInstance.emitter.addListener(event, listener);
|
|
913
|
+
this.subscriptions.push(() => {
|
|
914
|
+
runningInstance.emitter.removeListener(event, listener);
|
|
915
|
+
logger.info('removed proxied "%s" listener!', event);
|
|
916
|
+
});
|
|
917
|
+
return this;
|
|
918
|
+
};
|
|
919
|
+
this.readyState = "APPLIED";
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
logger.info("no running instance found, setting up a new instance...");
|
|
923
|
+
this.setup();
|
|
924
|
+
this.setInstance();
|
|
925
|
+
this.readyState = "APPLIED";
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Setup the module augments and stubs necessary for this interceptor.
|
|
929
|
+
* This method is not run if there's a running interceptor instance
|
|
930
|
+
* to prevent instantiating an interceptor multiple times.
|
|
931
|
+
*/
|
|
932
|
+
setup() {
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Listen to the interceptor's public events.
|
|
936
|
+
*/
|
|
937
|
+
on(event, listener) {
|
|
938
|
+
const logger = this.logger.extend("on");
|
|
939
|
+
if (this.readyState === "DISPOSING" || this.readyState === "DISPOSED") {
|
|
940
|
+
logger.info("cannot listen to events, already disposed!");
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
logger.info('adding "%s" event listener:', event, listener);
|
|
944
|
+
this.emitter.on(event, listener);
|
|
945
|
+
return this;
|
|
946
|
+
}
|
|
947
|
+
once(event, listener) {
|
|
948
|
+
this.emitter.once(event, listener);
|
|
949
|
+
return this;
|
|
950
|
+
}
|
|
951
|
+
off(event, listener) {
|
|
952
|
+
this.emitter.off(event, listener);
|
|
953
|
+
return this;
|
|
954
|
+
}
|
|
955
|
+
removeAllListeners(event) {
|
|
956
|
+
this.emitter.removeAllListeners(event);
|
|
957
|
+
return this;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Disposes of any side-effects this interceptor has introduced.
|
|
961
|
+
*/
|
|
962
|
+
dispose() {
|
|
963
|
+
const logger = this.logger.extend("dispose");
|
|
964
|
+
if (this.readyState === "DISPOSED") {
|
|
965
|
+
logger.info("cannot dispose, already disposed!");
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
logger.info("disposing the interceptor...");
|
|
969
|
+
this.readyState = "DISPOSING";
|
|
970
|
+
if (!this.getInstance()) {
|
|
971
|
+
logger.info("no interceptors running, skipping dispose...");
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
this.clearInstance();
|
|
975
|
+
logger.info("global symbol deleted:", getGlobalSymbol(this.symbol));
|
|
976
|
+
if (this.subscriptions.length > 0) {
|
|
977
|
+
logger.info("disposing of %d subscriptions...", this.subscriptions.length);
|
|
978
|
+
for (const dispose of this.subscriptions) {
|
|
979
|
+
dispose();
|
|
980
|
+
}
|
|
981
|
+
this.subscriptions = [];
|
|
982
|
+
logger.info("disposed of all subscriptions!", this.subscriptions.length);
|
|
983
|
+
}
|
|
984
|
+
this.emitter.removeAllListeners();
|
|
985
|
+
logger.info("destroyed the listener!");
|
|
986
|
+
this.readyState = "DISPOSED";
|
|
987
|
+
}
|
|
988
|
+
getInstance() {
|
|
989
|
+
var _a;
|
|
990
|
+
const instance = getGlobalSymbol(this.symbol);
|
|
991
|
+
this.logger.info("retrieved global instance:", (_a = instance == null ? void 0 : instance.constructor) == null ? void 0 : _a.name);
|
|
992
|
+
return instance;
|
|
993
|
+
}
|
|
994
|
+
setInstance() {
|
|
995
|
+
setGlobalSymbol(this.symbol, this);
|
|
996
|
+
this.logger.info("set global instance!", this.symbol.description);
|
|
997
|
+
}
|
|
998
|
+
clearInstance() {
|
|
999
|
+
deleteGlobalSymbol(this.symbol);
|
|
1000
|
+
this.logger.info("cleared global instance!", this.symbol.description);
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/index.mjs
|
|
1005
|
+
var BatchInterceptor = class extends Interceptor {
|
|
1006
|
+
constructor(options) {
|
|
1007
|
+
BatchInterceptor.symbol = Symbol(options.name);
|
|
1008
|
+
super(BatchInterceptor.symbol);
|
|
1009
|
+
this.interceptors = options.interceptors;
|
|
1010
|
+
}
|
|
1011
|
+
setup() {
|
|
1012
|
+
const logger = this.logger.extend("setup");
|
|
1013
|
+
logger.info("applying all %d interceptors...", this.interceptors.length);
|
|
1014
|
+
for (const interceptor of this.interceptors) {
|
|
1015
|
+
logger.info('applying "%s" interceptor...', interceptor.constructor.name);
|
|
1016
|
+
interceptor.apply();
|
|
1017
|
+
logger.info("adding interceptor dispose subscription");
|
|
1018
|
+
this.subscriptions.push(() => interceptor.dispose());
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
on(event, listener) {
|
|
1022
|
+
for (const interceptor of this.interceptors) {
|
|
1023
|
+
interceptor.on(event, listener);
|
|
1024
|
+
}
|
|
1025
|
+
return this;
|
|
1026
|
+
}
|
|
1027
|
+
once(event, listener) {
|
|
1028
|
+
for (const interceptor of this.interceptors) {
|
|
1029
|
+
interceptor.once(event, listener);
|
|
1030
|
+
}
|
|
1031
|
+
return this;
|
|
1032
|
+
}
|
|
1033
|
+
off(event, listener) {
|
|
1034
|
+
for (const interceptor of this.interceptors) {
|
|
1035
|
+
interceptor.off(event, listener);
|
|
1036
|
+
}
|
|
1037
|
+
return this;
|
|
1038
|
+
}
|
|
1039
|
+
removeAllListeners(event) {
|
|
1040
|
+
for (const interceptors of this.interceptors) {
|
|
1041
|
+
interceptors.removeAllListeners(event);
|
|
1042
|
+
}
|
|
1043
|
+
return this;
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
|
|
289
1047
|
// src/browser/setupWorker/start/createResponseListener.ts
|
|
290
|
-
var import_interceptors = require("@mswjs/interceptors");
|
|
291
1048
|
function createResponseListener(context) {
|
|
292
1049
|
return (_, message) => {
|
|
293
1050
|
const { payload: responseJson } = message;
|
|
1051
|
+
const { requestId } = responseJson;
|
|
1052
|
+
const request = context.requests.get(requestId);
|
|
1053
|
+
context.requests.delete(requestId);
|
|
294
1054
|
if (responseJson.type?.includes("opaque")) {
|
|
295
1055
|
return;
|
|
296
1056
|
}
|
|
@@ -301,18 +1061,14 @@ function createResponseListener(context) {
|
|
|
301
1061
|
* throw when passed a non-null body, so ensure it's null here
|
|
302
1062
|
* for those codes
|
|
303
1063
|
*/
|
|
304
|
-
|
|
1064
|
+
isResponseWithoutBody(responseJson.status) ? null : responseJson.body,
|
|
305
1065
|
responseJson
|
|
306
1066
|
);
|
|
307
1067
|
context.emitter.emit(
|
|
308
1068
|
responseJson.isMockedResponse ? "response:mocked" : "response:bypass",
|
|
309
1069
|
{
|
|
310
1070
|
response,
|
|
311
|
-
|
|
312
|
-
* @todo @fixme In this context, we don't know anything about
|
|
313
|
-
* the request.
|
|
314
|
-
*/
|
|
315
|
-
request: null,
|
|
1071
|
+
request,
|
|
316
1072
|
requestId: responseJson.requestId
|
|
317
1073
|
}
|
|
318
1074
|
);
|
|
@@ -375,7 +1131,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
375
1131
|
}
|
|
376
1132
|
window.clearInterval(context.keepAliveInterval);
|
|
377
1133
|
});
|
|
378
|
-
const integrityCheckResult = await
|
|
1134
|
+
const integrityCheckResult = await until(
|
|
379
1135
|
() => requestIntegrityCheck(context, worker)
|
|
380
1136
|
);
|
|
381
1137
|
if (integrityCheckResult.error) {
|
|
@@ -407,8 +1163,8 @@ If this message still persists after updating, please report an issue: https://g
|
|
|
407
1163
|
});
|
|
408
1164
|
});
|
|
409
1165
|
}
|
|
410
|
-
await enableMocking(context, options).catch((
|
|
411
|
-
throw new Error(`Failed to enable mocking: ${
|
|
1166
|
+
await enableMocking(context, options).catch((error2) => {
|
|
1167
|
+
throw new Error(`Failed to enable mocking: ${error2?.message}`);
|
|
412
1168
|
});
|
|
413
1169
|
return registration;
|
|
414
1170
|
}
|
|
@@ -463,15 +1219,1001 @@ var DEFAULT_START_OPTIONS = {
|
|
|
463
1219
|
}
|
|
464
1220
|
};
|
|
465
1221
|
|
|
1222
|
+
// node_modules/.pnpm/@open-draft+deferred-promise@2.2.0/node_modules/@open-draft/deferred-promise/build/index.mjs
|
|
1223
|
+
function createDeferredExecutor() {
|
|
1224
|
+
const executor = (resolve, reject) => {
|
|
1225
|
+
executor.state = "pending";
|
|
1226
|
+
executor.resolve = (data) => {
|
|
1227
|
+
if (executor.state !== "pending") {
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
executor.result = data;
|
|
1231
|
+
const onFulfilled = (value) => {
|
|
1232
|
+
executor.state = "fulfilled";
|
|
1233
|
+
return value;
|
|
1234
|
+
};
|
|
1235
|
+
return resolve(
|
|
1236
|
+
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
|
1237
|
+
);
|
|
1238
|
+
};
|
|
1239
|
+
executor.reject = (reason) => {
|
|
1240
|
+
if (executor.state !== "pending") {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
queueMicrotask(() => {
|
|
1244
|
+
executor.state = "rejected";
|
|
1245
|
+
});
|
|
1246
|
+
return reject(executor.rejectionReason = reason);
|
|
1247
|
+
};
|
|
1248
|
+
};
|
|
1249
|
+
return executor;
|
|
1250
|
+
}
|
|
1251
|
+
var DeferredPromise = class extends Promise {
|
|
1252
|
+
#executor;
|
|
1253
|
+
resolve;
|
|
1254
|
+
reject;
|
|
1255
|
+
constructor(executor = null) {
|
|
1256
|
+
const deferredExecutor = createDeferredExecutor();
|
|
1257
|
+
super((originalResolve, originalReject) => {
|
|
1258
|
+
deferredExecutor(originalResolve, originalReject);
|
|
1259
|
+
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
|
1260
|
+
});
|
|
1261
|
+
this.#executor = deferredExecutor;
|
|
1262
|
+
this.resolve = this.#executor.resolve;
|
|
1263
|
+
this.reject = this.#executor.reject;
|
|
1264
|
+
}
|
|
1265
|
+
get state() {
|
|
1266
|
+
return this.#executor.state;
|
|
1267
|
+
}
|
|
1268
|
+
get rejectionReason() {
|
|
1269
|
+
return this.#executor.rejectionReason;
|
|
1270
|
+
}
|
|
1271
|
+
then(onFulfilled, onRejected) {
|
|
1272
|
+
return this.#decorate(super.then(onFulfilled, onRejected));
|
|
1273
|
+
}
|
|
1274
|
+
catch(onRejected) {
|
|
1275
|
+
return this.#decorate(super.catch(onRejected));
|
|
1276
|
+
}
|
|
1277
|
+
finally(onfinally) {
|
|
1278
|
+
return this.#decorate(super.finally(onfinally));
|
|
1279
|
+
}
|
|
1280
|
+
#decorate(promise) {
|
|
1281
|
+
return Object.defineProperties(promise, {
|
|
1282
|
+
resolve: { configurable: true, value: this.resolve },
|
|
1283
|
+
reject: { configurable: true, value: this.reject }
|
|
1284
|
+
});
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
|
|
1288
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/chunk-72HT65NX.mjs
|
|
1289
|
+
function uuidv4() {
|
|
1290
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
1291
|
+
const r = Math.random() * 16 | 0;
|
|
1292
|
+
const v = c == "x" ? r : r & 3 | 8;
|
|
1293
|
+
return v.toString(16);
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
var RequestController = class {
|
|
1297
|
+
constructor(request) {
|
|
1298
|
+
this.request = request;
|
|
1299
|
+
this.responsePromise = new DeferredPromise();
|
|
1300
|
+
}
|
|
1301
|
+
respondWith(response) {
|
|
1302
|
+
invariant(
|
|
1303
|
+
this.responsePromise.state === "pending",
|
|
1304
|
+
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
1305
|
+
this.request.method,
|
|
1306
|
+
this.request.url
|
|
1307
|
+
);
|
|
1308
|
+
this.responsePromise.resolve(response);
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
function toInteractiveRequest(request) {
|
|
1312
|
+
const requestController = new RequestController(request);
|
|
1313
|
+
Reflect.set(
|
|
1314
|
+
request,
|
|
1315
|
+
"respondWith",
|
|
1316
|
+
requestController.respondWith.bind(requestController)
|
|
1317
|
+
);
|
|
1318
|
+
return {
|
|
1319
|
+
interactiveRequest: request,
|
|
1320
|
+
requestController
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
1324
|
+
const listners = emitter.listeners(eventName);
|
|
1325
|
+
if (listners.length === 0) {
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
for (const listener of listners) {
|
|
1329
|
+
await listener.apply(emitter, data);
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/chunk-44V5AUD6.mjs
|
|
1334
|
+
function isPropertyAccessible(obj, key) {
|
|
1335
|
+
try {
|
|
1336
|
+
obj[key];
|
|
1337
|
+
return true;
|
|
1338
|
+
} catch (e) {
|
|
1339
|
+
return false;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
function canParseUrl(url) {
|
|
1343
|
+
try {
|
|
1344
|
+
new URL(url);
|
|
1345
|
+
return true;
|
|
1346
|
+
} catch (_error) {
|
|
1347
|
+
return false;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
var _FetchInterceptor = class extends Interceptor {
|
|
1351
|
+
constructor() {
|
|
1352
|
+
super(_FetchInterceptor.symbol);
|
|
1353
|
+
}
|
|
1354
|
+
checkEnvironment() {
|
|
1355
|
+
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
1356
|
+
}
|
|
1357
|
+
setup() {
|
|
1358
|
+
const pureFetch = globalThis.fetch;
|
|
1359
|
+
invariant(
|
|
1360
|
+
!pureFetch[IS_PATCHED_MODULE],
|
|
1361
|
+
'Failed to patch the "fetch" module: already patched.'
|
|
1362
|
+
);
|
|
1363
|
+
globalThis.fetch = async (input, init) => {
|
|
1364
|
+
var _a;
|
|
1365
|
+
const requestId = uuidv4();
|
|
1366
|
+
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
|
|
1367
|
+
const request = new Request(resolvedInput, init);
|
|
1368
|
+
this.logger.info("[%s] %s", request.method, request.url);
|
|
1369
|
+
const { interactiveRequest, requestController } = toInteractiveRequest(request);
|
|
1370
|
+
this.logger.info(
|
|
1371
|
+
'emitting the "request" event for %d listener(s)...',
|
|
1372
|
+
this.emitter.listenerCount("request")
|
|
1373
|
+
);
|
|
1374
|
+
this.emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
1375
|
+
if (pendingRequestId !== requestId) {
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
if (requestController.responsePromise.state === "pending") {
|
|
1379
|
+
requestController.responsePromise.resolve(void 0);
|
|
1380
|
+
}
|
|
1381
|
+
});
|
|
1382
|
+
this.logger.info("awaiting for the mocked response...");
|
|
1383
|
+
const signal = interactiveRequest.signal;
|
|
1384
|
+
const requestAborted = new DeferredPromise();
|
|
1385
|
+
signal.addEventListener(
|
|
1386
|
+
"abort",
|
|
1387
|
+
() => {
|
|
1388
|
+
requestAborted.reject(signal.reason);
|
|
1389
|
+
},
|
|
1390
|
+
{ once: true }
|
|
1391
|
+
);
|
|
1392
|
+
const resolverResult = await until(async () => {
|
|
1393
|
+
const listenersFinished = emitAsync(this.emitter, "request", {
|
|
1394
|
+
request: interactiveRequest,
|
|
1395
|
+
requestId
|
|
1396
|
+
});
|
|
1397
|
+
await Promise.race([
|
|
1398
|
+
requestAborted,
|
|
1399
|
+
// Put the listeners invocation Promise in the same race condition
|
|
1400
|
+
// with the request abort Promise because otherwise awaiting the listeners
|
|
1401
|
+
// would always yield some response (or undefined).
|
|
1402
|
+
listenersFinished,
|
|
1403
|
+
requestController.responsePromise
|
|
1404
|
+
]);
|
|
1405
|
+
this.logger.info("all request listeners have been resolved!");
|
|
1406
|
+
const mockedResponse2 = await requestController.responsePromise;
|
|
1407
|
+
this.logger.info("event.respondWith called with:", mockedResponse2);
|
|
1408
|
+
return mockedResponse2;
|
|
1409
|
+
});
|
|
1410
|
+
if (requestAborted.state === "rejected") {
|
|
1411
|
+
return Promise.reject(requestAborted.rejectionReason);
|
|
1412
|
+
}
|
|
1413
|
+
if (resolverResult.error) {
|
|
1414
|
+
return Promise.reject(createNetworkError(resolverResult.error));
|
|
1415
|
+
}
|
|
1416
|
+
const mockedResponse = resolverResult.data;
|
|
1417
|
+
if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
|
|
1418
|
+
this.logger.info("received mocked response:", mockedResponse);
|
|
1419
|
+
if (isPropertyAccessible(mockedResponse, "type") && mockedResponse.type === "error") {
|
|
1420
|
+
this.logger.info(
|
|
1421
|
+
"received a network error response, rejecting the request promise..."
|
|
1422
|
+
);
|
|
1423
|
+
return Promise.reject(createNetworkError(mockedResponse));
|
|
1424
|
+
}
|
|
1425
|
+
const responseClone = mockedResponse.clone();
|
|
1426
|
+
this.emitter.emit("response", {
|
|
1427
|
+
response: responseClone,
|
|
1428
|
+
isMockedResponse: true,
|
|
1429
|
+
request: interactiveRequest,
|
|
1430
|
+
requestId
|
|
1431
|
+
});
|
|
1432
|
+
const response = new Response(mockedResponse.body, mockedResponse);
|
|
1433
|
+
Object.defineProperty(response, "url", {
|
|
1434
|
+
writable: false,
|
|
1435
|
+
enumerable: true,
|
|
1436
|
+
configurable: false,
|
|
1437
|
+
value: request.url
|
|
1438
|
+
});
|
|
1439
|
+
return response;
|
|
1440
|
+
}
|
|
1441
|
+
this.logger.info("no mocked response received!");
|
|
1442
|
+
return pureFetch(request).then((response) => {
|
|
1443
|
+
const responseClone = response.clone();
|
|
1444
|
+
this.logger.info("original fetch performed", responseClone);
|
|
1445
|
+
this.emitter.emit("response", {
|
|
1446
|
+
response: responseClone,
|
|
1447
|
+
isMockedResponse: false,
|
|
1448
|
+
request: interactiveRequest,
|
|
1449
|
+
requestId
|
|
1450
|
+
});
|
|
1451
|
+
return response;
|
|
1452
|
+
});
|
|
1453
|
+
};
|
|
1454
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
1455
|
+
enumerable: true,
|
|
1456
|
+
configurable: true,
|
|
1457
|
+
value: true
|
|
1458
|
+
});
|
|
1459
|
+
this.subscriptions.push(() => {
|
|
1460
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
1461
|
+
value: void 0
|
|
1462
|
+
});
|
|
1463
|
+
globalThis.fetch = pureFetch;
|
|
1464
|
+
this.logger.info(
|
|
1465
|
+
'restored native "globalThis.fetch"!',
|
|
1466
|
+
globalThis.fetch.name
|
|
1467
|
+
);
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
var FetchInterceptor = _FetchInterceptor;
|
|
1472
|
+
FetchInterceptor.symbol = Symbol("fetch");
|
|
1473
|
+
function createNetworkError(cause) {
|
|
1474
|
+
return Object.assign(new TypeError("Failed to fetch"), {
|
|
1475
|
+
cause
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.25.15/node_modules/@mswjs/interceptors/lib/browser/chunk-DZVB7JEV.mjs
|
|
1480
|
+
function concatArrayBuffer(left, right) {
|
|
1481
|
+
const result = new Uint8Array(left.byteLength + right.byteLength);
|
|
1482
|
+
result.set(left, 0);
|
|
1483
|
+
result.set(right, left.byteLength);
|
|
1484
|
+
return result;
|
|
1485
|
+
}
|
|
1486
|
+
var EventPolyfill = class {
|
|
1487
|
+
constructor(type, options) {
|
|
1488
|
+
this.AT_TARGET = 0;
|
|
1489
|
+
this.BUBBLING_PHASE = 0;
|
|
1490
|
+
this.CAPTURING_PHASE = 0;
|
|
1491
|
+
this.NONE = 0;
|
|
1492
|
+
this.type = "";
|
|
1493
|
+
this.srcElement = null;
|
|
1494
|
+
this.currentTarget = null;
|
|
1495
|
+
this.eventPhase = 0;
|
|
1496
|
+
this.isTrusted = true;
|
|
1497
|
+
this.composed = false;
|
|
1498
|
+
this.cancelable = true;
|
|
1499
|
+
this.defaultPrevented = false;
|
|
1500
|
+
this.bubbles = true;
|
|
1501
|
+
this.lengthComputable = true;
|
|
1502
|
+
this.loaded = 0;
|
|
1503
|
+
this.total = 0;
|
|
1504
|
+
this.cancelBubble = false;
|
|
1505
|
+
this.returnValue = true;
|
|
1506
|
+
this.type = type;
|
|
1507
|
+
this.target = (options == null ? void 0 : options.target) || null;
|
|
1508
|
+
this.currentTarget = (options == null ? void 0 : options.currentTarget) || null;
|
|
1509
|
+
this.timeStamp = Date.now();
|
|
1510
|
+
}
|
|
1511
|
+
composedPath() {
|
|
1512
|
+
return [];
|
|
1513
|
+
}
|
|
1514
|
+
initEvent(type, bubbles, cancelable) {
|
|
1515
|
+
this.type = type;
|
|
1516
|
+
this.bubbles = !!bubbles;
|
|
1517
|
+
this.cancelable = !!cancelable;
|
|
1518
|
+
}
|
|
1519
|
+
preventDefault() {
|
|
1520
|
+
this.defaultPrevented = true;
|
|
1521
|
+
}
|
|
1522
|
+
stopPropagation() {
|
|
1523
|
+
}
|
|
1524
|
+
stopImmediatePropagation() {
|
|
1525
|
+
}
|
|
1526
|
+
};
|
|
1527
|
+
var ProgressEventPolyfill = class extends EventPolyfill {
|
|
1528
|
+
constructor(type, init) {
|
|
1529
|
+
super(type);
|
|
1530
|
+
this.lengthComputable = (init == null ? void 0 : init.lengthComputable) || false;
|
|
1531
|
+
this.composed = (init == null ? void 0 : init.composed) || false;
|
|
1532
|
+
this.loaded = (init == null ? void 0 : init.loaded) || 0;
|
|
1533
|
+
this.total = (init == null ? void 0 : init.total) || 0;
|
|
1534
|
+
}
|
|
1535
|
+
};
|
|
1536
|
+
var SUPPORTS_PROGRESS_EVENT = typeof ProgressEvent !== "undefined";
|
|
1537
|
+
function createEvent(target, type, init) {
|
|
1538
|
+
const progressEvents = [
|
|
1539
|
+
"error",
|
|
1540
|
+
"progress",
|
|
1541
|
+
"loadstart",
|
|
1542
|
+
"loadend",
|
|
1543
|
+
"load",
|
|
1544
|
+
"timeout",
|
|
1545
|
+
"abort"
|
|
1546
|
+
];
|
|
1547
|
+
const ProgressEventClass = SUPPORTS_PROGRESS_EVENT ? ProgressEvent : ProgressEventPolyfill;
|
|
1548
|
+
const event = progressEvents.includes(type) ? new ProgressEventClass(type, {
|
|
1549
|
+
lengthComputable: true,
|
|
1550
|
+
loaded: (init == null ? void 0 : init.loaded) || 0,
|
|
1551
|
+
total: (init == null ? void 0 : init.total) || 0
|
|
1552
|
+
}) : new EventPolyfill(type, {
|
|
1553
|
+
target,
|
|
1554
|
+
currentTarget: target
|
|
1555
|
+
});
|
|
1556
|
+
return event;
|
|
1557
|
+
}
|
|
1558
|
+
function findPropertySource(target, propertyName) {
|
|
1559
|
+
if (!(propertyName in target)) {
|
|
1560
|
+
return null;
|
|
1561
|
+
}
|
|
1562
|
+
const hasProperty = Object.prototype.hasOwnProperty.call(target, propertyName);
|
|
1563
|
+
if (hasProperty) {
|
|
1564
|
+
return target;
|
|
1565
|
+
}
|
|
1566
|
+
const prototype = Reflect.getPrototypeOf(target);
|
|
1567
|
+
return prototype ? findPropertySource(prototype, propertyName) : null;
|
|
1568
|
+
}
|
|
1569
|
+
function createProxy(target, options) {
|
|
1570
|
+
const proxy = new Proxy(target, optionsToProxyHandler(options));
|
|
1571
|
+
return proxy;
|
|
1572
|
+
}
|
|
1573
|
+
function optionsToProxyHandler(options) {
|
|
1574
|
+
const { constructorCall, methodCall, getProperty, setProperty } = options;
|
|
1575
|
+
const handler = {};
|
|
1576
|
+
if (typeof constructorCall !== "undefined") {
|
|
1577
|
+
handler.construct = function(target, args, newTarget) {
|
|
1578
|
+
const next = Reflect.construct.bind(null, target, args, newTarget);
|
|
1579
|
+
return constructorCall.call(newTarget, args, next);
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1582
|
+
handler.set = function(target, propertyName, nextValue) {
|
|
1583
|
+
const next = () => {
|
|
1584
|
+
const propertySource = findPropertySource(target, propertyName) || target;
|
|
1585
|
+
const ownDescriptors = Reflect.getOwnPropertyDescriptor(
|
|
1586
|
+
propertySource,
|
|
1587
|
+
propertyName
|
|
1588
|
+
);
|
|
1589
|
+
if (typeof (ownDescriptors == null ? void 0 : ownDescriptors.set) !== "undefined") {
|
|
1590
|
+
ownDescriptors.set.apply(target, [nextValue]);
|
|
1591
|
+
return true;
|
|
1592
|
+
}
|
|
1593
|
+
return Reflect.defineProperty(propertySource, propertyName, {
|
|
1594
|
+
writable: true,
|
|
1595
|
+
enumerable: true,
|
|
1596
|
+
configurable: true,
|
|
1597
|
+
value: nextValue
|
|
1598
|
+
});
|
|
1599
|
+
};
|
|
1600
|
+
if (typeof setProperty !== "undefined") {
|
|
1601
|
+
return setProperty.call(target, [propertyName, nextValue], next);
|
|
1602
|
+
}
|
|
1603
|
+
return next();
|
|
1604
|
+
};
|
|
1605
|
+
handler.get = function(target, propertyName, receiver) {
|
|
1606
|
+
const next = () => target[propertyName];
|
|
1607
|
+
const value = typeof getProperty !== "undefined" ? getProperty.call(target, [propertyName, receiver], next) : next();
|
|
1608
|
+
if (typeof value === "function") {
|
|
1609
|
+
return (...args) => {
|
|
1610
|
+
const next2 = value.bind(target, ...args);
|
|
1611
|
+
if (typeof methodCall !== "undefined") {
|
|
1612
|
+
return methodCall.call(target, [propertyName, args], next2);
|
|
1613
|
+
}
|
|
1614
|
+
return next2();
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
return value;
|
|
1618
|
+
};
|
|
1619
|
+
return handler;
|
|
1620
|
+
}
|
|
1621
|
+
function isDomParserSupportedType(type) {
|
|
1622
|
+
const supportedTypes = [
|
|
1623
|
+
"application/xhtml+xml",
|
|
1624
|
+
"application/xml",
|
|
1625
|
+
"image/svg+xml",
|
|
1626
|
+
"text/html",
|
|
1627
|
+
"text/xml"
|
|
1628
|
+
];
|
|
1629
|
+
return supportedTypes.some((supportedType) => {
|
|
1630
|
+
return type.startsWith(supportedType);
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
function parseJson(data) {
|
|
1634
|
+
try {
|
|
1635
|
+
const json = JSON.parse(data);
|
|
1636
|
+
return json;
|
|
1637
|
+
} catch (_) {
|
|
1638
|
+
return null;
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
function createResponse(request, body) {
|
|
1642
|
+
const responseBodyOrNull = isResponseWithoutBody(request.status) ? null : body;
|
|
1643
|
+
return new Response(responseBodyOrNull, {
|
|
1644
|
+
status: request.status,
|
|
1645
|
+
statusText: request.statusText,
|
|
1646
|
+
headers: createHeadersFromXMLHttpReqestHeaders(
|
|
1647
|
+
request.getAllResponseHeaders()
|
|
1648
|
+
)
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
function createHeadersFromXMLHttpReqestHeaders(headersString) {
|
|
1652
|
+
const headers = new Headers();
|
|
1653
|
+
const lines = headersString.split(/[\r\n]+/);
|
|
1654
|
+
for (const line of lines) {
|
|
1655
|
+
if (line.trim() === "") {
|
|
1656
|
+
continue;
|
|
1657
|
+
}
|
|
1658
|
+
const [name, ...parts] = line.split(": ");
|
|
1659
|
+
const value = parts.join(": ");
|
|
1660
|
+
headers.append(name, value);
|
|
1661
|
+
}
|
|
1662
|
+
return headers;
|
|
1663
|
+
}
|
|
1664
|
+
var IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
|
|
1665
|
+
var IS_NODE2 = isNodeProcess();
|
|
1666
|
+
var XMLHttpRequestController = class {
|
|
1667
|
+
constructor(initialRequest, logger) {
|
|
1668
|
+
this.initialRequest = initialRequest;
|
|
1669
|
+
this.logger = logger;
|
|
1670
|
+
this.method = "GET";
|
|
1671
|
+
this.url = null;
|
|
1672
|
+
this.events = /* @__PURE__ */ new Map();
|
|
1673
|
+
this.requestId = uuidv4();
|
|
1674
|
+
this.requestHeaders = new Headers();
|
|
1675
|
+
this.responseBuffer = new Uint8Array();
|
|
1676
|
+
this.request = createProxy(initialRequest, {
|
|
1677
|
+
setProperty: ([propertyName, nextValue], invoke) => {
|
|
1678
|
+
switch (propertyName) {
|
|
1679
|
+
case "ontimeout": {
|
|
1680
|
+
const eventName = propertyName.slice(
|
|
1681
|
+
2
|
|
1682
|
+
);
|
|
1683
|
+
this.request.addEventListener(eventName, nextValue);
|
|
1684
|
+
return invoke();
|
|
1685
|
+
}
|
|
1686
|
+
default: {
|
|
1687
|
+
return invoke();
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
},
|
|
1691
|
+
methodCall: ([methodName, args], invoke) => {
|
|
1692
|
+
var _a;
|
|
1693
|
+
switch (methodName) {
|
|
1694
|
+
case "open": {
|
|
1695
|
+
const [method, url] = args;
|
|
1696
|
+
if (typeof url === "undefined") {
|
|
1697
|
+
this.method = "GET";
|
|
1698
|
+
this.url = toAbsoluteUrl(method);
|
|
1699
|
+
} else {
|
|
1700
|
+
this.method = method;
|
|
1701
|
+
this.url = toAbsoluteUrl(url);
|
|
1702
|
+
}
|
|
1703
|
+
this.logger = this.logger.extend(`${this.method} ${this.url.href}`);
|
|
1704
|
+
this.logger.info("open", this.method, this.url.href);
|
|
1705
|
+
return invoke();
|
|
1706
|
+
}
|
|
1707
|
+
case "addEventListener": {
|
|
1708
|
+
const [eventName, listener] = args;
|
|
1709
|
+
this.registerEvent(eventName, listener);
|
|
1710
|
+
this.logger.info("addEventListener", eventName, listener);
|
|
1711
|
+
return invoke();
|
|
1712
|
+
}
|
|
1713
|
+
case "setRequestHeader": {
|
|
1714
|
+
const [name, value] = args;
|
|
1715
|
+
this.requestHeaders.set(name, value);
|
|
1716
|
+
this.logger.info("setRequestHeader", name, value);
|
|
1717
|
+
return invoke();
|
|
1718
|
+
}
|
|
1719
|
+
case "send": {
|
|
1720
|
+
const [body] = args;
|
|
1721
|
+
if (body != null) {
|
|
1722
|
+
this.requestBody = typeof body === "string" ? encodeBuffer(body) : body;
|
|
1723
|
+
}
|
|
1724
|
+
this.request.addEventListener("load", () => {
|
|
1725
|
+
if (typeof this.onResponse !== "undefined") {
|
|
1726
|
+
const fetchResponse = createResponse(
|
|
1727
|
+
this.request,
|
|
1728
|
+
/**
|
|
1729
|
+
* The `response` property is the right way to read
|
|
1730
|
+
* the ambiguous response body, as the request's "responseType" may differ.
|
|
1731
|
+
* @see https://xhr.spec.whatwg.org/#the-response-attribute
|
|
1732
|
+
*/
|
|
1733
|
+
this.request.response
|
|
1734
|
+
);
|
|
1735
|
+
this.onResponse.call(this, {
|
|
1736
|
+
response: fetchResponse,
|
|
1737
|
+
isMockedResponse: IS_MOCKED_RESPONSE in this.request,
|
|
1738
|
+
request: fetchRequest,
|
|
1739
|
+
requestId: this.requestId
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
});
|
|
1743
|
+
const fetchRequest = this.toFetchApiRequest();
|
|
1744
|
+
const onceRequestSettled = ((_a = this.onRequest) == null ? void 0 : _a.call(this, {
|
|
1745
|
+
request: fetchRequest,
|
|
1746
|
+
requestId: this.requestId
|
|
1747
|
+
})) || Promise.resolve();
|
|
1748
|
+
onceRequestSettled.finally(() => {
|
|
1749
|
+
if (this.request.readyState < this.request.LOADING) {
|
|
1750
|
+
this.logger.info(
|
|
1751
|
+
"request callback settled but request has not been handled (readystate %d), performing as-is...",
|
|
1752
|
+
this.request.readyState
|
|
1753
|
+
);
|
|
1754
|
+
if (IS_NODE2) {
|
|
1755
|
+
this.request.setRequestHeader("X-Request-Id", this.requestId);
|
|
1756
|
+
}
|
|
1757
|
+
return invoke();
|
|
1758
|
+
}
|
|
1759
|
+
});
|
|
1760
|
+
break;
|
|
1761
|
+
}
|
|
1762
|
+
default: {
|
|
1763
|
+
return invoke();
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
});
|
|
1768
|
+
}
|
|
1769
|
+
registerEvent(eventName, listener) {
|
|
1770
|
+
const prevEvents = this.events.get(eventName) || [];
|
|
1771
|
+
const nextEvents = prevEvents.concat(listener);
|
|
1772
|
+
this.events.set(eventName, nextEvents);
|
|
1773
|
+
this.logger.info('registered event "%s"', eventName, listener);
|
|
1774
|
+
}
|
|
1775
|
+
/**
|
|
1776
|
+
* Responds to the current request with the given
|
|
1777
|
+
* Fetch API `Response` instance.
|
|
1778
|
+
*/
|
|
1779
|
+
respondWith(response) {
|
|
1780
|
+
this.logger.info(
|
|
1781
|
+
"responding with a mocked response: %d %s",
|
|
1782
|
+
response.status,
|
|
1783
|
+
response.statusText
|
|
1784
|
+
);
|
|
1785
|
+
define(this.request, IS_MOCKED_RESPONSE, true);
|
|
1786
|
+
define(this.request, "status", response.status);
|
|
1787
|
+
define(this.request, "statusText", response.statusText);
|
|
1788
|
+
define(this.request, "responseURL", this.url.href);
|
|
1789
|
+
this.request.getResponseHeader = new Proxy(this.request.getResponseHeader, {
|
|
1790
|
+
apply: (_, __, args) => {
|
|
1791
|
+
this.logger.info("getResponseHeader", args[0]);
|
|
1792
|
+
if (this.request.readyState < this.request.HEADERS_RECEIVED) {
|
|
1793
|
+
this.logger.info("headers not received yet, returning null");
|
|
1794
|
+
return null;
|
|
1795
|
+
}
|
|
1796
|
+
const headerValue = response.headers.get(args[0]);
|
|
1797
|
+
this.logger.info(
|
|
1798
|
+
'resolved response header "%s" to',
|
|
1799
|
+
args[0],
|
|
1800
|
+
headerValue
|
|
1801
|
+
);
|
|
1802
|
+
return headerValue;
|
|
1803
|
+
}
|
|
1804
|
+
});
|
|
1805
|
+
this.request.getAllResponseHeaders = new Proxy(
|
|
1806
|
+
this.request.getAllResponseHeaders,
|
|
1807
|
+
{
|
|
1808
|
+
apply: () => {
|
|
1809
|
+
this.logger.info("getAllResponseHeaders");
|
|
1810
|
+
if (this.request.readyState < this.request.HEADERS_RECEIVED) {
|
|
1811
|
+
this.logger.info("headers not received yet, returning empty string");
|
|
1812
|
+
return "";
|
|
1813
|
+
}
|
|
1814
|
+
const headersList = Array.from(response.headers.entries());
|
|
1815
|
+
const allHeaders = headersList.map(([headerName, headerValue]) => {
|
|
1816
|
+
return `${headerName}: ${headerValue}`;
|
|
1817
|
+
}).join("\r\n");
|
|
1818
|
+
this.logger.info("resolved all response headers to", allHeaders);
|
|
1819
|
+
return allHeaders;
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
);
|
|
1823
|
+
Object.defineProperties(this.request, {
|
|
1824
|
+
response: {
|
|
1825
|
+
enumerable: true,
|
|
1826
|
+
configurable: false,
|
|
1827
|
+
get: () => this.response
|
|
1828
|
+
},
|
|
1829
|
+
responseText: {
|
|
1830
|
+
enumerable: true,
|
|
1831
|
+
configurable: false,
|
|
1832
|
+
get: () => this.responseText
|
|
1833
|
+
},
|
|
1834
|
+
responseXML: {
|
|
1835
|
+
enumerable: true,
|
|
1836
|
+
configurable: false,
|
|
1837
|
+
get: () => this.responseXML
|
|
1838
|
+
}
|
|
1839
|
+
});
|
|
1840
|
+
const totalResponseBodyLength = response.headers.has("Content-Length") ? Number(response.headers.get("Content-Length")) : (
|
|
1841
|
+
/**
|
|
1842
|
+
* @todo Infer the response body length from the response body.
|
|
1843
|
+
*/
|
|
1844
|
+
void 0
|
|
1845
|
+
);
|
|
1846
|
+
this.logger.info("calculated response body length", totalResponseBodyLength);
|
|
1847
|
+
this.trigger("loadstart", {
|
|
1848
|
+
loaded: 0,
|
|
1849
|
+
total: totalResponseBodyLength
|
|
1850
|
+
});
|
|
1851
|
+
this.setReadyState(this.request.HEADERS_RECEIVED);
|
|
1852
|
+
this.setReadyState(this.request.LOADING);
|
|
1853
|
+
const finalizeResponse = () => {
|
|
1854
|
+
this.logger.info("finalizing the mocked response...");
|
|
1855
|
+
this.setReadyState(this.request.DONE);
|
|
1856
|
+
this.trigger("load", {
|
|
1857
|
+
loaded: this.responseBuffer.byteLength,
|
|
1858
|
+
total: totalResponseBodyLength
|
|
1859
|
+
});
|
|
1860
|
+
this.trigger("loadend", {
|
|
1861
|
+
loaded: this.responseBuffer.byteLength,
|
|
1862
|
+
total: totalResponseBodyLength
|
|
1863
|
+
});
|
|
1864
|
+
};
|
|
1865
|
+
if (response.body) {
|
|
1866
|
+
this.logger.info("mocked response has body, streaming...");
|
|
1867
|
+
const reader = response.body.getReader();
|
|
1868
|
+
const readNextResponseBodyChunk = async () => {
|
|
1869
|
+
const { value, done } = await reader.read();
|
|
1870
|
+
if (done) {
|
|
1871
|
+
this.logger.info("response body stream done!");
|
|
1872
|
+
finalizeResponse();
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
if (value) {
|
|
1876
|
+
this.logger.info("read response body chunk:", value);
|
|
1877
|
+
this.responseBuffer = concatArrayBuffer(this.responseBuffer, value);
|
|
1878
|
+
this.trigger("progress", {
|
|
1879
|
+
loaded: this.responseBuffer.byteLength,
|
|
1880
|
+
total: totalResponseBodyLength
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
readNextResponseBodyChunk();
|
|
1884
|
+
};
|
|
1885
|
+
readNextResponseBodyChunk();
|
|
1886
|
+
} else {
|
|
1887
|
+
finalizeResponse();
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
responseBufferToText() {
|
|
1891
|
+
return decodeBuffer(this.responseBuffer);
|
|
1892
|
+
}
|
|
1893
|
+
get response() {
|
|
1894
|
+
this.logger.info(
|
|
1895
|
+
"getResponse (responseType: %s)",
|
|
1896
|
+
this.request.responseType
|
|
1897
|
+
);
|
|
1898
|
+
if (this.request.readyState !== this.request.DONE) {
|
|
1899
|
+
return null;
|
|
1900
|
+
}
|
|
1901
|
+
switch (this.request.responseType) {
|
|
1902
|
+
case "json": {
|
|
1903
|
+
const responseJson = parseJson(this.responseBufferToText());
|
|
1904
|
+
this.logger.info("resolved response JSON", responseJson);
|
|
1905
|
+
return responseJson;
|
|
1906
|
+
}
|
|
1907
|
+
case "arraybuffer": {
|
|
1908
|
+
const arrayBuffer = toArrayBuffer(this.responseBuffer);
|
|
1909
|
+
this.logger.info("resolved response ArrayBuffer", arrayBuffer);
|
|
1910
|
+
return arrayBuffer;
|
|
1911
|
+
}
|
|
1912
|
+
case "blob": {
|
|
1913
|
+
const mimeType = this.request.getResponseHeader("Content-Type") || "text/plain";
|
|
1914
|
+
const responseBlob = new Blob([this.responseBufferToText()], {
|
|
1915
|
+
type: mimeType
|
|
1916
|
+
});
|
|
1917
|
+
this.logger.info(
|
|
1918
|
+
"resolved response Blob (mime type: %s)",
|
|
1919
|
+
responseBlob,
|
|
1920
|
+
mimeType
|
|
1921
|
+
);
|
|
1922
|
+
return responseBlob;
|
|
1923
|
+
}
|
|
1924
|
+
default: {
|
|
1925
|
+
const responseText = this.responseBufferToText();
|
|
1926
|
+
this.logger.info(
|
|
1927
|
+
'resolving "%s" response type as text',
|
|
1928
|
+
this.request.responseType,
|
|
1929
|
+
responseText
|
|
1930
|
+
);
|
|
1931
|
+
return responseText;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
get responseText() {
|
|
1936
|
+
invariant(
|
|
1937
|
+
this.request.responseType === "" || this.request.responseType === "text",
|
|
1938
|
+
"InvalidStateError: The object is in invalid state."
|
|
1939
|
+
);
|
|
1940
|
+
if (this.request.readyState !== this.request.LOADING && this.request.readyState !== this.request.DONE) {
|
|
1941
|
+
return "";
|
|
1942
|
+
}
|
|
1943
|
+
const responseText = this.responseBufferToText();
|
|
1944
|
+
this.logger.info('getResponseText: "%s"', responseText);
|
|
1945
|
+
return responseText;
|
|
1946
|
+
}
|
|
1947
|
+
get responseXML() {
|
|
1948
|
+
invariant(
|
|
1949
|
+
this.request.responseType === "" || this.request.responseType === "document",
|
|
1950
|
+
"InvalidStateError: The object is in invalid state."
|
|
1951
|
+
);
|
|
1952
|
+
if (this.request.readyState !== this.request.DONE) {
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
const contentType = this.request.getResponseHeader("Content-Type") || "";
|
|
1956
|
+
if (typeof DOMParser === "undefined") {
|
|
1957
|
+
console.warn(
|
|
1958
|
+
"Cannot retrieve XMLHttpRequest response body as XML: DOMParser is not defined. You are likely using an environment that is not browser or does not polyfill browser globals correctly."
|
|
1959
|
+
);
|
|
1960
|
+
return null;
|
|
1961
|
+
}
|
|
1962
|
+
if (isDomParserSupportedType(contentType)) {
|
|
1963
|
+
return new DOMParser().parseFromString(
|
|
1964
|
+
this.responseBufferToText(),
|
|
1965
|
+
contentType
|
|
1966
|
+
);
|
|
1967
|
+
}
|
|
1968
|
+
return null;
|
|
1969
|
+
}
|
|
1970
|
+
errorWith(error2) {
|
|
1971
|
+
this.logger.info("responding with an error");
|
|
1972
|
+
this.setReadyState(this.request.DONE);
|
|
1973
|
+
this.trigger("error");
|
|
1974
|
+
this.trigger("loadend");
|
|
1975
|
+
}
|
|
1976
|
+
/**
|
|
1977
|
+
* Transitions this request's `readyState` to the given one.
|
|
1978
|
+
*/
|
|
1979
|
+
setReadyState(nextReadyState) {
|
|
1980
|
+
this.logger.info(
|
|
1981
|
+
"setReadyState: %d -> %d",
|
|
1982
|
+
this.request.readyState,
|
|
1983
|
+
nextReadyState
|
|
1984
|
+
);
|
|
1985
|
+
if (this.request.readyState === nextReadyState) {
|
|
1986
|
+
this.logger.info("ready state identical, skipping transition...");
|
|
1987
|
+
return;
|
|
1988
|
+
}
|
|
1989
|
+
define(this.request, "readyState", nextReadyState);
|
|
1990
|
+
this.logger.info("set readyState to: %d", nextReadyState);
|
|
1991
|
+
if (nextReadyState !== this.request.UNSENT) {
|
|
1992
|
+
this.logger.info('triggerring "readystatechange" event...');
|
|
1993
|
+
this.trigger("readystatechange");
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Triggers given event on the `XMLHttpRequest` instance.
|
|
1998
|
+
*/
|
|
1999
|
+
trigger(eventName, options) {
|
|
2000
|
+
const callback = this.request[`on${eventName}`];
|
|
2001
|
+
const event = createEvent(this.request, eventName, options);
|
|
2002
|
+
this.logger.info('trigger "%s"', eventName, options || "");
|
|
2003
|
+
if (typeof callback === "function") {
|
|
2004
|
+
this.logger.info('found a direct "%s" callback, calling...', eventName);
|
|
2005
|
+
callback.call(this.request, event);
|
|
2006
|
+
}
|
|
2007
|
+
for (const [registeredEventName, listeners] of this.events) {
|
|
2008
|
+
if (registeredEventName === eventName) {
|
|
2009
|
+
this.logger.info(
|
|
2010
|
+
'found %d listener(s) for "%s" event, calling...',
|
|
2011
|
+
listeners.length,
|
|
2012
|
+
eventName
|
|
2013
|
+
);
|
|
2014
|
+
listeners.forEach((listener) => listener.call(this.request, event));
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.
|
|
2020
|
+
*/
|
|
2021
|
+
toFetchApiRequest() {
|
|
2022
|
+
this.logger.info("converting request to a Fetch API Request...");
|
|
2023
|
+
const fetchRequest = new Request(this.url.href, {
|
|
2024
|
+
method: this.method,
|
|
2025
|
+
headers: this.requestHeaders,
|
|
2026
|
+
/**
|
|
2027
|
+
* @see https://xhr.spec.whatwg.org/#cross-origin-credentials
|
|
2028
|
+
*/
|
|
2029
|
+
credentials: this.request.withCredentials ? "include" : "same-origin",
|
|
2030
|
+
body: ["GET", "HEAD"].includes(this.method) ? null : this.requestBody
|
|
2031
|
+
});
|
|
2032
|
+
const proxyHeaders = createProxy(fetchRequest.headers, {
|
|
2033
|
+
methodCall: ([methodName, args], invoke) => {
|
|
2034
|
+
switch (methodName) {
|
|
2035
|
+
case "append":
|
|
2036
|
+
case "set": {
|
|
2037
|
+
const [headerName, headerValue] = args;
|
|
2038
|
+
this.request.setRequestHeader(headerName, headerValue);
|
|
2039
|
+
break;
|
|
2040
|
+
}
|
|
2041
|
+
case "delete": {
|
|
2042
|
+
const [headerName] = args;
|
|
2043
|
+
console.warn(
|
|
2044
|
+
`XMLHttpRequest: Cannot remove a "${headerName}" header from the Fetch API representation of the "${fetchRequest.method} ${fetchRequest.url}" request. XMLHttpRequest headers cannot be removed.`
|
|
2045
|
+
);
|
|
2046
|
+
break;
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
return invoke();
|
|
2050
|
+
}
|
|
2051
|
+
});
|
|
2052
|
+
define(fetchRequest, "headers", proxyHeaders);
|
|
2053
|
+
this.logger.info("converted request to a Fetch API Request!", fetchRequest);
|
|
2054
|
+
return fetchRequest;
|
|
2055
|
+
}
|
|
2056
|
+
};
|
|
2057
|
+
function toAbsoluteUrl(url) {
|
|
2058
|
+
if (typeof location === "undefined") {
|
|
2059
|
+
return new URL(url);
|
|
2060
|
+
}
|
|
2061
|
+
return new URL(url.toString(), location.href);
|
|
2062
|
+
}
|
|
2063
|
+
function define(target, property, value) {
|
|
2064
|
+
Reflect.defineProperty(target, property, {
|
|
2065
|
+
// Ensure writable properties to allow redefining readonly properties.
|
|
2066
|
+
writable: true,
|
|
2067
|
+
enumerable: true,
|
|
2068
|
+
value
|
|
2069
|
+
});
|
|
2070
|
+
}
|
|
2071
|
+
function createXMLHttpRequestProxy({
|
|
2072
|
+
emitter,
|
|
2073
|
+
logger
|
|
2074
|
+
}) {
|
|
2075
|
+
const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {
|
|
2076
|
+
construct(target, args, newTarget) {
|
|
2077
|
+
logger.info("constructed new XMLHttpRequest");
|
|
2078
|
+
const originalRequest = Reflect.construct(target, args, newTarget);
|
|
2079
|
+
const prototypeDescriptors = Object.getOwnPropertyDescriptors(
|
|
2080
|
+
target.prototype
|
|
2081
|
+
);
|
|
2082
|
+
for (const propertyName in prototypeDescriptors) {
|
|
2083
|
+
Reflect.defineProperty(
|
|
2084
|
+
originalRequest,
|
|
2085
|
+
propertyName,
|
|
2086
|
+
prototypeDescriptors[propertyName]
|
|
2087
|
+
);
|
|
2088
|
+
}
|
|
2089
|
+
const xhrRequestController = new XMLHttpRequestController(
|
|
2090
|
+
originalRequest,
|
|
2091
|
+
logger
|
|
2092
|
+
);
|
|
2093
|
+
xhrRequestController.onRequest = async function({ request, requestId }) {
|
|
2094
|
+
const { interactiveRequest, requestController } = toInteractiveRequest(request);
|
|
2095
|
+
this.logger.info("awaiting mocked response...");
|
|
2096
|
+
emitter.once("request", ({ requestId: pendingRequestId }) => {
|
|
2097
|
+
if (pendingRequestId !== requestId) {
|
|
2098
|
+
return;
|
|
2099
|
+
}
|
|
2100
|
+
if (requestController.responsePromise.state === "pending") {
|
|
2101
|
+
requestController.respondWith(void 0);
|
|
2102
|
+
}
|
|
2103
|
+
});
|
|
2104
|
+
const resolverResult = await until(async () => {
|
|
2105
|
+
this.logger.info(
|
|
2106
|
+
'emitting the "request" event for %s listener(s)...',
|
|
2107
|
+
emitter.listenerCount("request")
|
|
2108
|
+
);
|
|
2109
|
+
await emitAsync(emitter, "request", {
|
|
2110
|
+
request: interactiveRequest,
|
|
2111
|
+
requestId
|
|
2112
|
+
});
|
|
2113
|
+
this.logger.info('all "request" listeners settled!');
|
|
2114
|
+
const mockedResponse2 = await requestController.responsePromise;
|
|
2115
|
+
this.logger.info("event.respondWith called with:", mockedResponse2);
|
|
2116
|
+
return mockedResponse2;
|
|
2117
|
+
});
|
|
2118
|
+
if (resolverResult.error) {
|
|
2119
|
+
this.logger.info(
|
|
2120
|
+
"request listener threw an exception, aborting request...",
|
|
2121
|
+
resolverResult.error
|
|
2122
|
+
);
|
|
2123
|
+
xhrRequestController.errorWith(resolverResult.error);
|
|
2124
|
+
return;
|
|
2125
|
+
}
|
|
2126
|
+
const mockedResponse = resolverResult.data;
|
|
2127
|
+
if (typeof mockedResponse !== "undefined") {
|
|
2128
|
+
this.logger.info(
|
|
2129
|
+
"received mocked response: %d %s",
|
|
2130
|
+
mockedResponse.status,
|
|
2131
|
+
mockedResponse.statusText
|
|
2132
|
+
);
|
|
2133
|
+
if (mockedResponse.type === "error") {
|
|
2134
|
+
this.logger.info(
|
|
2135
|
+
"received a network error response, rejecting the request promise..."
|
|
2136
|
+
);
|
|
2137
|
+
xhrRequestController.errorWith(new TypeError("Network error"));
|
|
2138
|
+
return;
|
|
2139
|
+
}
|
|
2140
|
+
return xhrRequestController.respondWith(mockedResponse);
|
|
2141
|
+
}
|
|
2142
|
+
this.logger.info(
|
|
2143
|
+
"no mocked response received, performing request as-is..."
|
|
2144
|
+
);
|
|
2145
|
+
};
|
|
2146
|
+
xhrRequestController.onResponse = async function({
|
|
2147
|
+
response,
|
|
2148
|
+
isMockedResponse,
|
|
2149
|
+
request,
|
|
2150
|
+
requestId
|
|
2151
|
+
}) {
|
|
2152
|
+
this.logger.info(
|
|
2153
|
+
'emitting the "response" event for %s listener(s)...',
|
|
2154
|
+
emitter.listenerCount("response")
|
|
2155
|
+
);
|
|
2156
|
+
emitter.emit("response", {
|
|
2157
|
+
response,
|
|
2158
|
+
isMockedResponse,
|
|
2159
|
+
request,
|
|
2160
|
+
requestId
|
|
2161
|
+
});
|
|
2162
|
+
};
|
|
2163
|
+
return xhrRequestController.request;
|
|
2164
|
+
}
|
|
2165
|
+
});
|
|
2166
|
+
return XMLHttpRequestProxy;
|
|
2167
|
+
}
|
|
2168
|
+
var _XMLHttpRequestInterceptor = class extends Interceptor {
|
|
2169
|
+
constructor() {
|
|
2170
|
+
super(_XMLHttpRequestInterceptor.interceptorSymbol);
|
|
2171
|
+
}
|
|
2172
|
+
checkEnvironment() {
|
|
2173
|
+
return typeof globalThis.XMLHttpRequest !== "undefined";
|
|
2174
|
+
}
|
|
2175
|
+
setup() {
|
|
2176
|
+
const logger = this.logger.extend("setup");
|
|
2177
|
+
logger.info('patching "XMLHttpRequest" module...');
|
|
2178
|
+
const PureXMLHttpRequest = globalThis.XMLHttpRequest;
|
|
2179
|
+
invariant(
|
|
2180
|
+
!PureXMLHttpRequest[IS_PATCHED_MODULE],
|
|
2181
|
+
'Failed to patch the "XMLHttpRequest" module: already patched.'
|
|
2182
|
+
);
|
|
2183
|
+
globalThis.XMLHttpRequest = createXMLHttpRequestProxy({
|
|
2184
|
+
emitter: this.emitter,
|
|
2185
|
+
logger: this.logger
|
|
2186
|
+
});
|
|
2187
|
+
logger.info(
|
|
2188
|
+
'native "XMLHttpRequest" module patched!',
|
|
2189
|
+
globalThis.XMLHttpRequest.name
|
|
2190
|
+
);
|
|
2191
|
+
Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {
|
|
2192
|
+
enumerable: true,
|
|
2193
|
+
configurable: true,
|
|
2194
|
+
value: true
|
|
2195
|
+
});
|
|
2196
|
+
this.subscriptions.push(() => {
|
|
2197
|
+
Object.defineProperty(globalThis.XMLHttpRequest, IS_PATCHED_MODULE, {
|
|
2198
|
+
value: void 0
|
|
2199
|
+
});
|
|
2200
|
+
globalThis.XMLHttpRequest = PureXMLHttpRequest;
|
|
2201
|
+
logger.info(
|
|
2202
|
+
'native "XMLHttpRequest" module restored!',
|
|
2203
|
+
globalThis.XMLHttpRequest.name
|
|
2204
|
+
);
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
};
|
|
2208
|
+
var XMLHttpRequestInterceptor = _XMLHttpRequestInterceptor;
|
|
2209
|
+
XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
|
|
2210
|
+
|
|
466
2211
|
// src/browser/setupWorker/start/createFallbackRequestListener.ts
|
|
467
|
-
var import_interceptors2 = require("@mswjs/interceptors");
|
|
468
|
-
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
469
|
-
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
470
2212
|
var import_handleRequest2 = require("../core/utils/handleRequest.js");
|
|
471
2213
|
function createFallbackRequestListener(context, options) {
|
|
472
|
-
const interceptor = new
|
|
2214
|
+
const interceptor = new BatchInterceptor({
|
|
473
2215
|
name: "fallback",
|
|
474
|
-
interceptors: [new
|
|
2216
|
+
interceptors: [new FetchInterceptor(), new XMLHttpRequestInterceptor()]
|
|
475
2217
|
});
|
|
476
2218
|
interceptor.on("request", async ({ request, requestId }) => {
|
|
477
2219
|
const requestCloneForLogs = request.clone();
|
|
@@ -553,7 +2295,7 @@ function supportsReadableStreamTransfer() {
|
|
|
553
2295
|
const message = new MessageChannel();
|
|
554
2296
|
message.port1.postMessage(stream, [stream]);
|
|
555
2297
|
return true;
|
|
556
|
-
} catch (
|
|
2298
|
+
} catch (error2) {
|
|
557
2299
|
return false;
|
|
558
2300
|
}
|
|
559
2301
|
}
|
|
@@ -566,8 +2308,8 @@ var SetupWorkerApi = class extends import_SetupApi.SetupApi {
|
|
|
566
2308
|
listeners;
|
|
567
2309
|
constructor(...handlers) {
|
|
568
2310
|
super(...handlers);
|
|
569
|
-
|
|
570
|
-
!
|
|
2311
|
+
invariant(
|
|
2312
|
+
!isNodeProcess(),
|
|
571
2313
|
import_devUtils9.devUtils.formatMessage(
|
|
572
2314
|
"Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead."
|
|
573
2315
|
)
|
|
@@ -584,6 +2326,7 @@ var SetupWorkerApi = class extends import_SetupApi.SetupApi {
|
|
|
584
2326
|
worker: null,
|
|
585
2327
|
registration: null,
|
|
586
2328
|
requestHandlers: this.currentHandlers,
|
|
2329
|
+
requests: /* @__PURE__ */ new Map(),
|
|
587
2330
|
emitter: this.emitter,
|
|
588
2331
|
workerChannel: {
|
|
589
2332
|
on: (eventType, callback) => {
|
|
@@ -631,8 +2374,8 @@ var SetupWorkerApi = class extends import_SetupApi.SetupApi {
|
|
|
631
2374
|
if (message.type === eventType) {
|
|
632
2375
|
resolve(message);
|
|
633
2376
|
}
|
|
634
|
-
} catch (
|
|
635
|
-
reject(
|
|
2377
|
+
} catch (error2) {
|
|
2378
|
+
reject(error2);
|
|
636
2379
|
}
|
|
637
2380
|
};
|
|
638
2381
|
bindings.push(
|