@mswjs/interceptors 0.39.8 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ClientRequest/package.json +7 -2
- package/RemoteHttpInterceptor/package.json +7 -2
- package/WebSocket/package.json +9 -2
- package/XMLHttpRequest/package.json +9 -3
- package/fetch/package.json +9 -3
- package/lib/browser/Interceptor-2mUoKZL1.d.mts +65 -0
- package/lib/browser/Interceptor-Deczogc8.d.cts +65 -0
- package/lib/browser/XMLHttpRequest-BUfglQD1.cjs +761 -0
- package/lib/browser/XMLHttpRequest-BUfglQD1.cjs.map +1 -0
- package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs +756 -0
- package/lib/browser/XMLHttpRequest-DS5fc8Qs.mjs.map +1 -0
- package/lib/browser/bufferUtils-BiiO6HZv.mjs +20 -0
- package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +1 -0
- package/lib/browser/bufferUtils-Uc0eRItL.cjs +38 -0
- package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +1 -0
- package/lib/browser/createRequestId-Cs4oXfa1.cjs +205 -0
- package/lib/browser/createRequestId-Cs4oXfa1.cjs.map +1 -0
- package/lib/browser/createRequestId-DQcIlohW.mjs +170 -0
- package/lib/browser/createRequestId-DQcIlohW.mjs.map +1 -0
- package/lib/browser/fetch-BHcqM3z7.cjs +253 -0
- package/lib/browser/fetch-BHcqM3z7.cjs.map +1 -0
- package/lib/browser/fetch-DSJoynSF.mjs +248 -0
- package/lib/browser/fetch-DSJoynSF.mjs.map +1 -0
- package/lib/browser/getRawRequest-BTaNLFr0.mjs +218 -0
- package/lib/browser/getRawRequest-BTaNLFr0.mjs.map +1 -0
- package/lib/browser/getRawRequest-zx8rUJL2.cjs +259 -0
- package/lib/browser/getRawRequest-zx8rUJL2.cjs.map +1 -0
- package/lib/browser/glossary-BdLS4k1H.d.cts +70 -0
- package/lib/browser/glossary-gEEJhK4S.d.mts +70 -0
- package/lib/browser/handleRequest-DI6a7Dty.cjs +189 -0
- package/lib/browser/handleRequest-DI6a7Dty.cjs.map +1 -0
- package/lib/browser/handleRequest-DxGbCTbb.mjs +178 -0
- package/lib/browser/handleRequest-DxGbCTbb.mjs.map +1 -0
- package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs +33 -0
- package/lib/browser/hasConfigurableGlobal-C8kXFDic.mjs.map +1 -0
- package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs +45 -0
- package/lib/browser/hasConfigurableGlobal-D7S3l5h6.cjs.map +1 -0
- package/lib/browser/index.cjs +68 -0
- package/lib/browser/index.cjs.map +1 -0
- package/lib/browser/index.d.cts +87 -0
- package/lib/browser/index.d.mts +87 -0
- package/lib/browser/index.mjs +49 -75
- package/lib/browser/index.mjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +621 -0
- package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -0
- package/lib/browser/interceptors/WebSocket/index.d.cts +277 -0
- package/lib/browser/interceptors/WebSocket/index.d.mts +277 -0
- package/lib/browser/interceptors/WebSocket/index.mjs +587 -694
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +7 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +15 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +15 -0
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +7 -12
- package/lib/browser/interceptors/fetch/index.cjs +6 -0
- package/lib/browser/interceptors/fetch/index.d.cts +13 -0
- package/lib/browser/interceptors/fetch/index.d.mts +13 -0
- package/lib/browser/interceptors/fetch/index.mjs +6 -11
- package/lib/browser/presets/browser.cjs +17 -0
- package/lib/browser/presets/browser.cjs.map +1 -0
- package/lib/browser/presets/browser.d.cts +12 -0
- package/lib/browser/presets/browser.d.mts +14 -0
- package/lib/browser/presets/browser.mjs +15 -19
- package/lib/browser/presets/browser.mjs.map +1 -1
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs +49 -0
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs.map +1 -0
- package/lib/node/BatchInterceptor-D7mXzHcQ.d.mts +26 -0
- package/lib/node/BatchInterceptor-DFaBPilf.mjs +44 -0
- package/lib/node/BatchInterceptor-DFaBPilf.mjs.map +1 -0
- package/lib/node/BatchInterceptor-D_YqR8qU.d.cts +26 -0
- package/lib/node/ClientRequest-2rDe54Ui.cjs +1043 -0
- package/lib/node/ClientRequest-2rDe54Ui.cjs.map +1 -0
- package/lib/node/ClientRequest-Ca8Qykuv.mjs +1034 -0
- package/lib/node/ClientRequest-Ca8Qykuv.mjs.map +1 -0
- package/lib/node/Interceptor-DEazpLJd.d.mts +133 -0
- package/lib/node/Interceptor-DJ2akVWI.d.cts +133 -0
- package/lib/node/RemoteHttpInterceptor.cjs +154 -0
- package/lib/node/RemoteHttpInterceptor.cjs.map +1 -0
- package/lib/node/RemoteHttpInterceptor.d.cts +39 -0
- package/lib/node/RemoteHttpInterceptor.d.mts +39 -0
- package/lib/node/RemoteHttpInterceptor.mjs +145 -186
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs +763 -0
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs.map +1 -0
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs +757 -0
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs.map +1 -0
- package/lib/node/bufferUtils-DiCTqG-7.cjs +38 -0
- package/lib/node/bufferUtils-DiCTqG-7.cjs.map +1 -0
- package/lib/node/bufferUtils-_8XfKIfX.mjs +20 -0
- package/lib/node/bufferUtils-_8XfKIfX.mjs.map +1 -0
- package/lib/node/chunk-CbDLau6x.cjs +34 -0
- package/lib/node/fetch-BmXpK10r.cjs +272 -0
- package/lib/node/fetch-BmXpK10r.cjs.map +1 -0
- package/lib/node/fetch-G1DVwDKG.mjs +265 -0
- package/lib/node/fetch-G1DVwDKG.mjs.map +1 -0
- package/lib/node/fetchUtils-BaY5iWXw.cjs +419 -0
- package/lib/node/fetchUtils-BaY5iWXw.cjs.map +1 -0
- package/lib/node/fetchUtils-CoU35g3M.mjs +359 -0
- package/lib/node/fetchUtils-CoU35g3M.mjs.map +1 -0
- package/lib/node/getRawRequest-BavnMWh_.cjs +36 -0
- package/lib/node/getRawRequest-BavnMWh_.cjs.map +1 -0
- package/lib/node/getRawRequest-DnwmXyOW.mjs +24 -0
- package/lib/node/getRawRequest-DnwmXyOW.mjs.map +1 -0
- package/lib/node/glossary-BLKRyLBd.cjs +12 -0
- package/lib/node/glossary-BLKRyLBd.cjs.map +1 -0
- package/lib/node/glossary-glQBRnVD.mjs +6 -0
- package/lib/node/glossary-glQBRnVD.mjs.map +1 -0
- package/lib/node/handleRequest-Bb7Y-XLw.cjs +220 -0
- package/lib/node/handleRequest-Bb7Y-XLw.cjs.map +1 -0
- package/lib/node/handleRequest-Y97UwBbF.mjs +190 -0
- package/lib/node/handleRequest-Y97UwBbF.mjs.map +1 -0
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs +26 -0
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs.map +1 -0
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs +20 -0
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs.map +1 -0
- package/lib/node/index-BMbJ8FXL.d.cts +113 -0
- package/lib/node/index-C0YAQ36w.d.mts +113 -0
- package/lib/node/index.cjs +30 -0
- package/lib/node/index.cjs.map +1 -0
- package/lib/node/index.d.cts +66 -0
- package/lib/node/index.d.mts +66 -0
- package/lib/node/index.mjs +13 -39
- package/lib/node/index.mjs.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +6 -0
- package/lib/node/interceptors/ClientRequest/index.d.cts +2 -0
- package/lib/node/interceptors/ClientRequest/index.d.mts +3 -0
- package/lib/node/interceptors/ClientRequest/index.mjs +6 -11
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +6 -0
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +14 -0
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +14 -0
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +6 -13
- package/lib/node/interceptors/fetch/index.cjs +5 -0
- package/lib/node/interceptors/fetch/index.d.cts +12 -0
- package/lib/node/interceptors/fetch/index.d.mts +12 -0
- package/lib/node/interceptors/fetch/index.mjs +5 -12
- package/lib/node/node-DwCc6iuP.mjs +27 -0
- package/lib/node/node-DwCc6iuP.mjs.map +1 -0
- package/lib/node/node-dKdAf3tC.cjs +39 -0
- package/lib/node/node-dKdAf3tC.cjs.map +1 -0
- package/lib/node/presets/node.cjs +22 -0
- package/lib/node/presets/node.cjs.map +1 -0
- package/lib/node/presets/node.d.cts +13 -0
- package/lib/node/presets/node.d.mts +15 -0
- package/lib/node/presets/node.mjs +18 -23
- package/lib/node/presets/node.mjs.map +1 -1
- package/lib/node/utils/node/index.cjs +4 -0
- package/lib/node/utils/node/{index.d.ts → index.d.cts} +5 -2
- package/lib/node/utils/node/index.d.mts +16 -0
- package/lib/node/utils/node/index.mjs +3 -10
- package/package.json +34 -59
- package/presets/browser/package.json +2 -3
- package/presets/node/package.json +7 -2
- package/src/RemoteHttpInterceptor.ts +18 -13
- package/src/RequestController.test.ts +78 -31
- package/src/RequestController.ts +63 -39
- package/src/index.ts +4 -0
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +43 -9
- package/src/interceptors/ClientRequest/index.ts +14 -18
- package/src/interceptors/ClientRequest/utils/parserUtils.ts +48 -0
- package/src/interceptors/WebSocket/index.ts +4 -1
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +45 -35
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +24 -21
- package/src/interceptors/XMLHttpRequest/utils/getBodyByteLength.test.ts +2 -2
- package/src/interceptors/fetch/index.ts +61 -50
- package/src/utils/handleRequest.ts +65 -95
- package/lib/browser/Interceptor-af98b768.d.ts +0 -63
- package/lib/browser/chunk-2HUMWGRD.js +0 -37
- package/lib/browser/chunk-2HUMWGRD.js.map +0 -1
- package/lib/browser/chunk-2QICSCCS.js +0 -238
- package/lib/browser/chunk-2QICSCCS.js.map +0 -1
- package/lib/browser/chunk-3RXCRGL2.mjs +0 -117
- package/lib/browser/chunk-3RXCRGL2.mjs.map +0 -1
- package/lib/browser/chunk-6HYIRFX2.mjs +0 -22
- package/lib/browser/chunk-6HYIRFX2.mjs.map +0 -1
- package/lib/browser/chunk-E3CCOBRX.js +0 -846
- package/lib/browser/chunk-E3CCOBRX.js.map +0 -1
- package/lib/browser/chunk-E7UVBHVO.mjs +0 -846
- package/lib/browser/chunk-E7UVBHVO.mjs.map +0 -1
- package/lib/browser/chunk-H74PGQ4Y.js +0 -296
- package/lib/browser/chunk-H74PGQ4Y.js.map +0 -1
- package/lib/browser/chunk-LK6DILFK.js +0 -22
- package/lib/browser/chunk-LK6DILFK.js.map +0 -1
- package/lib/browser/chunk-PTTUYYVR.mjs +0 -238
- package/lib/browser/chunk-PTTUYYVR.mjs.map +0 -1
- package/lib/browser/chunk-Q7K2XAEP.mjs +0 -296
- package/lib/browser/chunk-Q7K2XAEP.mjs.map +0 -1
- package/lib/browser/chunk-QED3Q6Z2.mjs +0 -169
- package/lib/browser/chunk-QED3Q6Z2.mjs.map +0 -1
- package/lib/browser/chunk-T7TBRNJZ.js +0 -117
- package/lib/browser/chunk-T7TBRNJZ.js.map +0 -1
- package/lib/browser/chunk-TIPR373R.js +0 -169
- package/lib/browser/chunk-TIPR373R.js.map +0 -1
- package/lib/browser/chunk-VYSDLBSS.mjs +0 -37
- package/lib/browser/chunk-VYSDLBSS.mjs.map +0 -1
- package/lib/browser/glossary-7152281e.d.ts +0 -69
- package/lib/browser/index.d.ts +0 -83
- package/lib/browser/index.js +0 -81
- package/lib/browser/index.js.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.d.ts +0 -271
- package/lib/browser/interceptors/WebSocket/index.js +0 -721
- package/lib/browser/interceptors/WebSocket/index.js.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.js +0 -12
- package/lib/browser/interceptors/XMLHttpRequest/index.js.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs.map +0 -1
- package/lib/browser/interceptors/fetch/index.d.ts +0 -14
- package/lib/browser/interceptors/fetch/index.js +0 -11
- package/lib/browser/interceptors/fetch/index.js.map +0 -1
- package/lib/browser/interceptors/fetch/index.mjs.map +0 -1
- package/lib/browser/presets/browser.d.ts +0 -15
- package/lib/browser/presets/browser.js +0 -21
- package/lib/browser/presets/browser.js.map +0 -1
- package/lib/node/BatchInterceptor-5b72232f.d.ts +0 -24
- package/lib/node/Interceptor-bc5a9d8e.d.ts +0 -130
- package/lib/node/RemoteHttpInterceptor.d.ts +0 -45
- package/lib/node/RemoteHttpInterceptor.js +0 -193
- package/lib/node/RemoteHttpInterceptor.js.map +0 -1
- package/lib/node/chunk-3CNGDJFB.mjs +0 -313
- package/lib/node/chunk-3CNGDJFB.mjs.map +0 -1
- package/lib/node/chunk-3GJB4JDF.mjs +0 -14
- package/lib/node/chunk-3GJB4JDF.mjs.map +0 -1
- package/lib/node/chunk-4NEYTVWD.mjs +0 -848
- package/lib/node/chunk-4NEYTVWD.mjs.map +0 -1
- package/lib/node/chunk-4YBV77DG.js +0 -32
- package/lib/node/chunk-4YBV77DG.js.map +0 -1
- package/lib/node/chunk-6HYIRFX2.mjs +0 -22
- package/lib/node/chunk-6HYIRFX2.mjs.map +0 -1
- package/lib/node/chunk-6YM4PLBI.mjs +0 -7
- package/lib/node/chunk-6YM4PLBI.mjs.map +0 -1
- package/lib/node/chunk-72ZIHMEB.js +0 -249
- package/lib/node/chunk-72ZIHMEB.js.map +0 -1
- package/lib/node/chunk-73NOP3T5.js +0 -7
- package/lib/node/chunk-73NOP3T5.js.map +0 -1
- package/lib/node/chunk-A7Q4RTDJ.mjs +0 -249
- package/lib/node/chunk-A7Q4RTDJ.mjs.map +0 -1
- package/lib/node/chunk-A7U44ARP.js +0 -268
- package/lib/node/chunk-A7U44ARP.js.map +0 -1
- package/lib/node/chunk-EKNRB5ZS.mjs +0 -1115
- package/lib/node/chunk-EKNRB5ZS.mjs.map +0 -1
- package/lib/node/chunk-IHJSPMYM.mjs +0 -268
- package/lib/node/chunk-IHJSPMYM.mjs.map +0 -1
- package/lib/node/chunk-LK6DILFK.js +0 -22
- package/lib/node/chunk-LK6DILFK.js.map +0 -1
- package/lib/node/chunk-N4ZZFE24.js +0 -1115
- package/lib/node/chunk-N4ZZFE24.js.map +0 -1
- package/lib/node/chunk-PFGO5BSM.js +0 -25
- package/lib/node/chunk-PFGO5BSM.js.map +0 -1
- package/lib/node/chunk-R6JVCM7X.js +0 -51
- package/lib/node/chunk-R6JVCM7X.js.map +0 -1
- package/lib/node/chunk-RC2XPCC4.mjs +0 -51
- package/lib/node/chunk-RC2XPCC4.mjs.map +0 -1
- package/lib/node/chunk-SMXZPJEA.js +0 -14
- package/lib/node/chunk-SMXZPJEA.js.map +0 -1
- package/lib/node/chunk-TJDMZZXE.mjs +0 -32
- package/lib/node/chunk-TJDMZZXE.mjs.map +0 -1
- package/lib/node/chunk-TX5GBTFY.mjs +0 -25
- package/lib/node/chunk-TX5GBTFY.mjs.map +0 -1
- package/lib/node/chunk-VV2LUF5K.js +0 -848
- package/lib/node/chunk-VV2LUF5K.js.map +0 -1
- package/lib/node/chunk-Z5LWCBZS.js +0 -313
- package/lib/node/chunk-Z5LWCBZS.js.map +0 -1
- package/lib/node/index.d.ts +0 -62
- package/lib/node/index.js +0 -43
- package/lib/node/index.js.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.d.ts +0 -111
- package/lib/node/interceptors/ClientRequest/index.js +0 -11
- package/lib/node/interceptors/ClientRequest/index.js.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.mjs.map +0 -1
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.js +0 -13
- package/lib/node/interceptors/XMLHttpRequest/index.js.map +0 -1
- package/lib/node/interceptors/XMLHttpRequest/index.mjs.map +0 -1
- package/lib/node/interceptors/fetch/index.d.ts +0 -13
- package/lib/node/interceptors/fetch/index.js +0 -12
- package/lib/node/interceptors/fetch/index.js.map +0 -1
- package/lib/node/interceptors/fetch/index.mjs.map +0 -1
- package/lib/node/presets/node.d.ts +0 -16
- package/lib/node/presets/node.js +0 -27
- package/lib/node/presets/node.js.map +0 -1
- package/lib/node/utils/node/index.js +0 -10
- package/lib/node/utils/node/index.js.map +0 -1
- package/lib/node/utils/node/index.mjs.map +0 -1
- package/src/utils/RequestController.ts +0 -21
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
const require_getRawRequest = require('./getRawRequest-zx8rUJL2.cjs');
|
|
2
|
+
const require_hasConfigurableGlobal = require('./hasConfigurableGlobal-D7S3l5h6.cjs');
|
|
3
|
+
let _open_draft_deferred_promise = require("@open-draft/deferred-promise");
|
|
4
|
+
let _open_draft_until = require("@open-draft/until");
|
|
5
|
+
|
|
6
|
+
//#region src/utils/isObject.ts
|
|
7
|
+
/**
|
|
8
|
+
* Determines if a given value is an instance of object.
|
|
9
|
+
*/
|
|
10
|
+
function isObject(value, loose = false) {
|
|
11
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/isPropertyAccessible.ts
|
|
16
|
+
/**
|
|
17
|
+
* A function that validates if property access is possible on an object
|
|
18
|
+
* without throwing. It returns `true` if the property access is possible
|
|
19
|
+
* and `false` otherwise.
|
|
20
|
+
*
|
|
21
|
+
* Environments like miniflare will throw on property access on certain objects
|
|
22
|
+
* like Request and Response, for unimplemented properties.
|
|
23
|
+
*/
|
|
24
|
+
function isPropertyAccessible(obj, key) {
|
|
25
|
+
try {
|
|
26
|
+
obj[key];
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/utils/responseUtils.ts
|
|
35
|
+
/**
|
|
36
|
+
* Creates a generic 500 Unhandled Exception response.
|
|
37
|
+
*/
|
|
38
|
+
function createServerErrorResponse(body) {
|
|
39
|
+
return new Response(JSON.stringify(body instanceof Error ? {
|
|
40
|
+
name: body.name,
|
|
41
|
+
message: body.message,
|
|
42
|
+
stack: body.stack
|
|
43
|
+
} : body), {
|
|
44
|
+
status: 500,
|
|
45
|
+
statusText: "Unhandled Exception",
|
|
46
|
+
headers: { "Content-Type": "application/json" }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if the given response is a `Response.error()`.
|
|
51
|
+
*
|
|
52
|
+
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
53
|
+
* implement the "Response.type" property and throw on its access.
|
|
54
|
+
* Safely check if we can access "type" on "Response" before continuing.
|
|
55
|
+
* @see https://github.com/mswjs/msw/issues/1834
|
|
56
|
+
*/
|
|
57
|
+
function isResponseError(response) {
|
|
58
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if the given value is a `Response` or a Response-like object.
|
|
62
|
+
* This is different from `value instanceof Response` because it supports
|
|
63
|
+
* custom `Response` constructors, like the one when using Undici directly.
|
|
64
|
+
*/
|
|
65
|
+
function isResponseLike(value) {
|
|
66
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/utils/isNodeLikeError.ts
|
|
71
|
+
function isNodeLikeError(error) {
|
|
72
|
+
if (error == null) return false;
|
|
73
|
+
if (!(error instanceof Error)) return false;
|
|
74
|
+
return "code" in error && "errno" in error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/utils/handleRequest.ts
|
|
79
|
+
async function handleRequest(options) {
|
|
80
|
+
const handleResponse = async (response) => {
|
|
81
|
+
if (response instanceof Error) {
|
|
82
|
+
await options.controller.errorWith(response);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (isResponseError(response)) {
|
|
86
|
+
await options.controller.respondWith(response);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handle normal responses or response-like objects.
|
|
91
|
+
* @note This must come before the arbitrary object check
|
|
92
|
+
* since Response instances are, in fact, objects.
|
|
93
|
+
*/
|
|
94
|
+
if (isResponseLike(response)) {
|
|
95
|
+
await options.controller.respondWith(response);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (isObject(response)) {
|
|
99
|
+
await options.controller.errorWith(response);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
const handleResponseError = async (error) => {
|
|
105
|
+
if (error instanceof require_getRawRequest.InterceptorError) throw result.error;
|
|
106
|
+
if (isNodeLikeError(error)) {
|
|
107
|
+
await options.controller.errorWith(error);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (error instanceof Response) return await handleResponse(error);
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
const requestAbortPromise = new _open_draft_deferred_promise.DeferredPromise();
|
|
114
|
+
/**
|
|
115
|
+
* @note `signal` is not always defined in React Native.
|
|
116
|
+
*/
|
|
117
|
+
if (options.request.signal) {
|
|
118
|
+
if (options.request.signal.aborted) {
|
|
119
|
+
await options.controller.errorWith(options.request.signal.reason);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
options.request.signal.addEventListener("abort", () => {
|
|
123
|
+
requestAbortPromise.reject(options.request.signal.reason);
|
|
124
|
+
}, { once: true });
|
|
125
|
+
}
|
|
126
|
+
const result = await (0, _open_draft_until.until)(async () => {
|
|
127
|
+
const requestListenersPromise = require_hasConfigurableGlobal.emitAsync(options.emitter, "request", {
|
|
128
|
+
requestId: options.requestId,
|
|
129
|
+
request: options.request,
|
|
130
|
+
controller: options.controller
|
|
131
|
+
});
|
|
132
|
+
await Promise.race([
|
|
133
|
+
requestAbortPromise,
|
|
134
|
+
requestListenersPromise,
|
|
135
|
+
options.controller.handled
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
if (requestAbortPromise.state === "rejected") {
|
|
139
|
+
await options.controller.errorWith(requestAbortPromise.rejectionReason);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (result.error) {
|
|
143
|
+
if (await handleResponseError(result.error)) return;
|
|
144
|
+
if (options.emitter.listenerCount("unhandledException") > 0) {
|
|
145
|
+
const unhandledExceptionController = new require_getRawRequest.RequestController(options.request, {
|
|
146
|
+
passthrough() {},
|
|
147
|
+
async respondWith(response) {
|
|
148
|
+
await handleResponse(response);
|
|
149
|
+
},
|
|
150
|
+
async errorWith(reason) {
|
|
151
|
+
/**
|
|
152
|
+
* @note Handle the result of the unhandled controller
|
|
153
|
+
* in the same way as the original request controller.
|
|
154
|
+
* The exception here is that thrown errors within the
|
|
155
|
+
* "unhandledException" event do NOT result in another
|
|
156
|
+
* emit of the same event. They are forwarded as-is.
|
|
157
|
+
*/
|
|
158
|
+
await options.controller.errorWith(reason);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
await require_hasConfigurableGlobal.emitAsync(options.emitter, "unhandledException", {
|
|
162
|
+
error: result.error,
|
|
163
|
+
request: options.request,
|
|
164
|
+
requestId: options.requestId,
|
|
165
|
+
controller: unhandledExceptionController
|
|
166
|
+
});
|
|
167
|
+
if (unhandledExceptionController.readyState !== require_getRawRequest.RequestController.PENDING) return;
|
|
168
|
+
}
|
|
169
|
+
await options.controller.respondWith(createServerErrorResponse(result.error));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (options.controller.readyState === require_getRawRequest.RequestController.PENDING) return await options.controller.passthrough();
|
|
173
|
+
return options.controller.handled;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
Object.defineProperty(exports, 'handleRequest', {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
get: function () {
|
|
180
|
+
return handleRequest;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
Object.defineProperty(exports, 'isResponseError', {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
get: function () {
|
|
186
|
+
return isResponseError;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
//# sourceMappingURL=handleRequest-DI6a7Dty.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleRequest-DI6a7Dty.cjs","names":["InterceptorError","DeferredPromise","emitAsync","RequestController"],"sources":["../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n}\n\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<void> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n await options.controller.errorWith(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n await options.controller.errorWith(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n await options.controller.errorWith(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n // options.emitter.once('request', async ({ requestId: pendingRequestId }) => {\n // if (\n // pendingRequestId === options.requestId &&\n // options.controller.readyState === RequestController.PENDING\n // ) {\n // await options.controller.passthrough()\n // }\n // })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n await options.controller.errorWith(options.request.signal.reason)\n return\n }\n\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller.handled,\n ])\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n await options.controller.errorWith(requestAbortPromise.rejectionReason)\n return\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request,\n {\n /**\n * @note Intentionally empty passthrough handle.\n * This controller is created within another controller and we only need\n * to know if `unhandledException` listeners handled the request.\n */\n passthrough() {},\n async respondWith(response) {\n await handleResponse(response)\n },\n async errorWith(reason) {\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n await options.controller.errorWith(reason)\n },\n }\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n })\n\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the request in any way, passthrough.\n if (\n unhandledExceptionController.readyState !== RequestController.PENDING\n ) {\n return\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n await options.controller.respondWith(\n createServerErrorResponse(result.error)\n )\n return\n }\n\n // If the request hasn't been handled by this point, passthrough.\n if (options.controller.readyState === RequestController.PENDING) {\n return await options.controller.passthrough()\n }\n\n return options.controller.handled\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { a as RequestController, o as InterceptorError } from "./getRawRequest-BTaNLFr0.mjs";
|
|
2
|
+
import { n as emitAsync } from "./hasConfigurableGlobal-C8kXFDic.mjs";
|
|
3
|
+
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
4
|
+
import { until } from "@open-draft/until";
|
|
5
|
+
|
|
6
|
+
//#region src/utils/isObject.ts
|
|
7
|
+
/**
|
|
8
|
+
* Determines if a given value is an instance of object.
|
|
9
|
+
*/
|
|
10
|
+
function isObject(value, loose = false) {
|
|
11
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/isPropertyAccessible.ts
|
|
16
|
+
/**
|
|
17
|
+
* A function that validates if property access is possible on an object
|
|
18
|
+
* without throwing. It returns `true` if the property access is possible
|
|
19
|
+
* and `false` otherwise.
|
|
20
|
+
*
|
|
21
|
+
* Environments like miniflare will throw on property access on certain objects
|
|
22
|
+
* like Request and Response, for unimplemented properties.
|
|
23
|
+
*/
|
|
24
|
+
function isPropertyAccessible(obj, key) {
|
|
25
|
+
try {
|
|
26
|
+
obj[key];
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/utils/responseUtils.ts
|
|
35
|
+
/**
|
|
36
|
+
* Creates a generic 500 Unhandled Exception response.
|
|
37
|
+
*/
|
|
38
|
+
function createServerErrorResponse(body) {
|
|
39
|
+
return new Response(JSON.stringify(body instanceof Error ? {
|
|
40
|
+
name: body.name,
|
|
41
|
+
message: body.message,
|
|
42
|
+
stack: body.stack
|
|
43
|
+
} : body), {
|
|
44
|
+
status: 500,
|
|
45
|
+
statusText: "Unhandled Exception",
|
|
46
|
+
headers: { "Content-Type": "application/json" }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if the given response is a `Response.error()`.
|
|
51
|
+
*
|
|
52
|
+
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
53
|
+
* implement the "Response.type" property and throw on its access.
|
|
54
|
+
* Safely check if we can access "type" on "Response" before continuing.
|
|
55
|
+
* @see https://github.com/mswjs/msw/issues/1834
|
|
56
|
+
*/
|
|
57
|
+
function isResponseError(response) {
|
|
58
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if the given value is a `Response` or a Response-like object.
|
|
62
|
+
* This is different from `value instanceof Response` because it supports
|
|
63
|
+
* custom `Response` constructors, like the one when using Undici directly.
|
|
64
|
+
*/
|
|
65
|
+
function isResponseLike(value) {
|
|
66
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/utils/isNodeLikeError.ts
|
|
71
|
+
function isNodeLikeError(error) {
|
|
72
|
+
if (error == null) return false;
|
|
73
|
+
if (!(error instanceof Error)) return false;
|
|
74
|
+
return "code" in error && "errno" in error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/utils/handleRequest.ts
|
|
79
|
+
async function handleRequest(options) {
|
|
80
|
+
const handleResponse = async (response) => {
|
|
81
|
+
if (response instanceof Error) {
|
|
82
|
+
await options.controller.errorWith(response);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (isResponseError(response)) {
|
|
86
|
+
await options.controller.respondWith(response);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handle normal responses or response-like objects.
|
|
91
|
+
* @note This must come before the arbitrary object check
|
|
92
|
+
* since Response instances are, in fact, objects.
|
|
93
|
+
*/
|
|
94
|
+
if (isResponseLike(response)) {
|
|
95
|
+
await options.controller.respondWith(response);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (isObject(response)) {
|
|
99
|
+
await options.controller.errorWith(response);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
const handleResponseError = async (error) => {
|
|
105
|
+
if (error instanceof InterceptorError) throw result.error;
|
|
106
|
+
if (isNodeLikeError(error)) {
|
|
107
|
+
await options.controller.errorWith(error);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (error instanceof Response) return await handleResponse(error);
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
const requestAbortPromise = new DeferredPromise();
|
|
114
|
+
/**
|
|
115
|
+
* @note `signal` is not always defined in React Native.
|
|
116
|
+
*/
|
|
117
|
+
if (options.request.signal) {
|
|
118
|
+
if (options.request.signal.aborted) {
|
|
119
|
+
await options.controller.errorWith(options.request.signal.reason);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
options.request.signal.addEventListener("abort", () => {
|
|
123
|
+
requestAbortPromise.reject(options.request.signal.reason);
|
|
124
|
+
}, { once: true });
|
|
125
|
+
}
|
|
126
|
+
const result = await until(async () => {
|
|
127
|
+
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
128
|
+
requestId: options.requestId,
|
|
129
|
+
request: options.request,
|
|
130
|
+
controller: options.controller
|
|
131
|
+
});
|
|
132
|
+
await Promise.race([
|
|
133
|
+
requestAbortPromise,
|
|
134
|
+
requestListenersPromise,
|
|
135
|
+
options.controller.handled
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
if (requestAbortPromise.state === "rejected") {
|
|
139
|
+
await options.controller.errorWith(requestAbortPromise.rejectionReason);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (result.error) {
|
|
143
|
+
if (await handleResponseError(result.error)) return;
|
|
144
|
+
if (options.emitter.listenerCount("unhandledException") > 0) {
|
|
145
|
+
const unhandledExceptionController = new RequestController(options.request, {
|
|
146
|
+
passthrough() {},
|
|
147
|
+
async respondWith(response) {
|
|
148
|
+
await handleResponse(response);
|
|
149
|
+
},
|
|
150
|
+
async errorWith(reason) {
|
|
151
|
+
/**
|
|
152
|
+
* @note Handle the result of the unhandled controller
|
|
153
|
+
* in the same way as the original request controller.
|
|
154
|
+
* The exception here is that thrown errors within the
|
|
155
|
+
* "unhandledException" event do NOT result in another
|
|
156
|
+
* emit of the same event. They are forwarded as-is.
|
|
157
|
+
*/
|
|
158
|
+
await options.controller.errorWith(reason);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
await emitAsync(options.emitter, "unhandledException", {
|
|
162
|
+
error: result.error,
|
|
163
|
+
request: options.request,
|
|
164
|
+
requestId: options.requestId,
|
|
165
|
+
controller: unhandledExceptionController
|
|
166
|
+
});
|
|
167
|
+
if (unhandledExceptionController.readyState !== RequestController.PENDING) return;
|
|
168
|
+
}
|
|
169
|
+
await options.controller.respondWith(createServerErrorResponse(result.error));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (options.controller.readyState === RequestController.PENDING) return await options.controller.passthrough();
|
|
173
|
+
return options.controller.handled;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
export { isResponseError as n, handleRequest as t };
|
|
178
|
+
//# sourceMappingURL=handleRequest-DxGbCTbb.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleRequest-DxGbCTbb.mjs","names":[],"sources":["../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n}\n\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<void> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n await options.controller.errorWith(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n await options.controller.errorWith(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n await options.controller.errorWith(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n // options.emitter.once('request', async ({ requestId: pendingRequestId }) => {\n // if (\n // pendingRequestId === options.requestId &&\n // options.controller.readyState === RequestController.PENDING\n // ) {\n // await options.controller.passthrough()\n // }\n // })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n await options.controller.errorWith(options.request.signal.reason)\n return\n }\n\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller.handled,\n ])\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n await options.controller.errorWith(requestAbortPromise.rejectionReason)\n return\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request,\n {\n /**\n * @note Intentionally empty passthrough handle.\n * This controller is created within another controller and we only need\n * to know if `unhandledException` listeners handled the request.\n */\n passthrough() {},\n async respondWith(response) {\n await handleResponse(response)\n },\n async errorWith(reason) {\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n await options.controller.errorWith(reason)\n },\n }\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n })\n\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the request in any way, passthrough.\n if (\n unhandledExceptionController.readyState !== RequestController.PENDING\n ) {\n return\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n await options.controller.respondWith(\n createServerErrorResponse(result.error)\n )\n return\n }\n\n // If the request hasn't been handled by this point, passthrough.\n if (options.controller.readyState === RequestController.PENDING) {\n return await options.controller.passthrough()\n }\n\n return options.controller.handled\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/utils/emitAsync.ts
|
|
2
|
+
/**
|
|
3
|
+
* Emits an event on the given emitter but executes
|
|
4
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
5
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
6
|
+
*/
|
|
7
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
8
|
+
const listeners = emitter.listeners(eventName);
|
|
9
|
+
if (listeners.length === 0) return;
|
|
10
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
15
|
+
/**
|
|
16
|
+
* Returns a boolean indicating whether the given global property
|
|
17
|
+
* is defined and is configurable.
|
|
18
|
+
*/
|
|
19
|
+
function hasConfigurableGlobal(propertyName) {
|
|
20
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
|
|
21
|
+
if (typeof descriptor === "undefined") return false;
|
|
22
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
23
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
24
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
25
|
+
console.error(`[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.`);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
export { emitAsync as n, hasConfigurableGlobal as t };
|
|
33
|
+
//# sourceMappingURL=hasConfigurableGlobal-C8kXFDic.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-C8kXFDic.mjs","names":[],"sources":["../../src/utils/emitAsync.ts","../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listeners = emitter.listeners(eventName)\n\n if (listeners.length === 0) {\n return\n }\n\n for (const listener of listeners) {\n await listener.apply(emitter, data)\n }\n}\n","/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(propertyName: string): boolean {\n const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName)\n\n // The property is not set at all.\n if (typeof descriptor === 'undefined') {\n return false\n }\n\n // The property is set to a getter that returns undefined.\n if (\n typeof descriptor.get === 'function' &&\n typeof descriptor.get() === 'undefined'\n ) {\n return false\n }\n\n // The property is set to a value equal to undefined.\n if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n return false\n }\n\n if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n console.error(\n `[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.`\n )\n return false\n }\n\n return true\n}\n"],"mappings":";;;;;;AAOA,eAAsB,UAIpB,SACA,WACA,GAAG,MACY;CACf,MAAM,YAAY,QAAQ,UAAU,UAAU;AAE9C,KAAI,UAAU,WAAW,EACvB;AAGF,MAAK,MAAM,YAAY,UACrB,OAAM,SAAS,MAAM,SAAS,KAAK;;;;;;;;;AClBvC,SAAgB,sBAAsB,cAA+B;CACnE,MAAM,aAAa,OAAO,yBAAyB,YAAY,aAAa;AAG5E,KAAI,OAAO,eAAe,YACxB,QAAO;AAIT,KACE,OAAO,WAAW,QAAQ,cAC1B,OAAO,WAAW,KAAK,KAAK,YAE5B,QAAO;AAIT,KAAI,OAAO,WAAW,QAAQ,eAAe,WAAW,SAAS,KAC/D,QAAO;AAGT,KAAI,OAAO,WAAW,QAAQ,eAAe,CAAC,WAAW,cAAc;AACrE,UAAQ,MACN,mDAAmD,aAAa,oKACjE;AACD,SAAO;;AAGT,QAAO"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/emitAsync.ts
|
|
3
|
+
/**
|
|
4
|
+
* Emits an event on the given emitter but executes
|
|
5
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
6
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
7
|
+
*/
|
|
8
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
9
|
+
const listeners = emitter.listeners(eventName);
|
|
10
|
+
if (listeners.length === 0) return;
|
|
11
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
16
|
+
/**
|
|
17
|
+
* Returns a boolean indicating whether the given global property
|
|
18
|
+
* is defined and is configurable.
|
|
19
|
+
*/
|
|
20
|
+
function hasConfigurableGlobal(propertyName) {
|
|
21
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
|
|
22
|
+
if (typeof descriptor === "undefined") return false;
|
|
23
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
24
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
25
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
26
|
+
console.error(`[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.`);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
Object.defineProperty(exports, 'emitAsync', {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () {
|
|
36
|
+
return emitAsync;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, 'hasConfigurableGlobal', {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () {
|
|
42
|
+
return hasConfigurableGlobal;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=hasConfigurableGlobal-D7S3l5h6.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-D7S3l5h6.cjs","names":[],"sources":["../../src/utils/emitAsync.ts","../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listeners = emitter.listeners(eventName)\n\n if (listeners.length === 0) {\n return\n }\n\n for (const listener of listeners) {\n await listener.apply(emitter, data)\n }\n}\n","/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(propertyName: string): boolean {\n const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName)\n\n // The property is not set at all.\n if (typeof descriptor === 'undefined') {\n return false\n }\n\n // The property is set to a getter that returns undefined.\n if (\n typeof descriptor.get === 'function' &&\n typeof descriptor.get() === 'undefined'\n ) {\n return false\n }\n\n // The property is set to a value equal to undefined.\n if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n return false\n }\n\n if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n console.error(\n `[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.`\n )\n return false\n }\n\n return true\n}\n"],"mappings":";;;;;;;AAOA,eAAsB,UAIpB,SACA,WACA,GAAG,MACY;CACf,MAAM,YAAY,QAAQ,UAAU,UAAU;AAE9C,KAAI,UAAU,WAAW,EACvB;AAGF,MAAK,MAAM,YAAY,UACrB,OAAM,SAAS,MAAM,SAAS,KAAK;;;;;;;;;AClBvC,SAAgB,sBAAsB,cAA+B;CACnE,MAAM,aAAa,OAAO,yBAAyB,YAAY,aAAa;AAG5E,KAAI,OAAO,eAAe,YACxB,QAAO;AAIT,KACE,OAAO,WAAW,QAAQ,cAC1B,OAAO,WAAW,KAAK,KAAK,YAE5B,QAAO;AAIT,KAAI,OAAO,WAAW,QAAQ,eAAe,WAAW,SAAS,KAC/D,QAAO;AAGT,KAAI,OAAO,WAAW,QAAQ,eAAe,CAAC,WAAW,cAAc;AACrE,UAAQ,MACN,mDAAmD,aAAa,oKACjE;AACD,SAAO;;AAGT,QAAO"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const require_getRawRequest = require('./getRawRequest-zx8rUJL2.cjs');
|
|
2
|
+
const require_createRequestId = require('./createRequestId-Cs4oXfa1.cjs');
|
|
3
|
+
const require_bufferUtils = require('./bufferUtils-Uc0eRItL.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/BatchInterceptor.ts
|
|
6
|
+
/**
|
|
7
|
+
* A batch interceptor that exposes a single interface
|
|
8
|
+
* to apply and operate with multiple interceptors at once.
|
|
9
|
+
*/
|
|
10
|
+
var BatchInterceptor = class BatchInterceptor extends require_createRequestId.Interceptor {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
BatchInterceptor.symbol = Symbol(options.name);
|
|
13
|
+
super(BatchInterceptor.symbol);
|
|
14
|
+
this.interceptors = options.interceptors;
|
|
15
|
+
}
|
|
16
|
+
setup() {
|
|
17
|
+
const logger = this.logger.extend("setup");
|
|
18
|
+
logger.info("applying all %d interceptors...", this.interceptors.length);
|
|
19
|
+
for (const interceptor of this.interceptors) {
|
|
20
|
+
logger.info("applying \"%s\" interceptor...", interceptor.constructor.name);
|
|
21
|
+
interceptor.apply();
|
|
22
|
+
logger.info("adding interceptor dispose subscription");
|
|
23
|
+
this.subscriptions.push(() => interceptor.dispose());
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
on(event, listener) {
|
|
27
|
+
for (const interceptor of this.interceptors) interceptor.on(event, listener);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
once(event, listener) {
|
|
31
|
+
for (const interceptor of this.interceptors) interceptor.once(event, listener);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
off(event, listener) {
|
|
35
|
+
for (const interceptor of this.interceptors) interceptor.off(event, listener);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
removeAllListeners(event) {
|
|
39
|
+
for (const interceptors of this.interceptors) interceptors.removeAllListeners(event);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/utils/getCleanUrl.ts
|
|
46
|
+
/**
|
|
47
|
+
* Removes query parameters and hashes from a given URL.
|
|
48
|
+
*/
|
|
49
|
+
function getCleanUrl(url, isAbsolute = true) {
|
|
50
|
+
return [isAbsolute && url.origin, url.pathname].filter(Boolean).join("");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
exports.BatchInterceptor = BatchInterceptor;
|
|
55
|
+
exports.FetchResponse = require_getRawRequest.FetchResponse;
|
|
56
|
+
exports.INTERNAL_REQUEST_ID_HEADER_NAME = require_createRequestId.INTERNAL_REQUEST_ID_HEADER_NAME;
|
|
57
|
+
exports.IS_PATCHED_MODULE = require_getRawRequest.IS_PATCHED_MODULE;
|
|
58
|
+
exports.Interceptor = require_createRequestId.Interceptor;
|
|
59
|
+
exports.InterceptorReadyState = require_createRequestId.InterceptorReadyState;
|
|
60
|
+
exports.RequestController = require_getRawRequest.RequestController;
|
|
61
|
+
exports.createRequestId = require_createRequestId.createRequestId;
|
|
62
|
+
exports.decodeBuffer = require_bufferUtils.decodeBuffer;
|
|
63
|
+
exports.deleteGlobalSymbol = require_createRequestId.deleteGlobalSymbol;
|
|
64
|
+
exports.encodeBuffer = require_bufferUtils.encodeBuffer;
|
|
65
|
+
exports.getCleanUrl = getCleanUrl;
|
|
66
|
+
exports.getGlobalSymbol = require_createRequestId.getGlobalSymbol;
|
|
67
|
+
exports.getRawRequest = require_getRawRequest.getRawRequest;
|
|
68
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Interceptor"],"sources":["../../src/BatchInterceptor.ts","../../src/utils/getCleanUrl.ts"],"sourcesContent":["import { EventMap, Listener } from 'strict-event-emitter'\nimport { Interceptor, ExtractEventNames } from './Interceptor'\n\nexport interface BatchInterceptorOptions<\n InterceptorList extends ReadonlyArray<Interceptor<any>>\n> {\n name: string\n interceptors: InterceptorList\n}\n\nexport type ExtractEventMapType<\n InterceptorList extends ReadonlyArray<Interceptor<any>>\n> = InterceptorList extends ReadonlyArray<infer InterceptorType>\n ? InterceptorType extends Interceptor<infer EventMap>\n ? EventMap\n : never\n : never\n\n/**\n * A batch interceptor that exposes a single interface\n * to apply and operate with multiple interceptors at once.\n */\nexport class BatchInterceptor<\n InterceptorList extends ReadonlyArray<Interceptor<any>>,\n Events extends EventMap = ExtractEventMapType<InterceptorList>\n> extends Interceptor<Events> {\n static symbol: symbol\n\n private interceptors: InterceptorList\n\n constructor(options: BatchInterceptorOptions<InterceptorList>) {\n BatchInterceptor.symbol = Symbol(options.name)\n super(BatchInterceptor.symbol)\n this.interceptors = options.interceptors\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n logger.info('applying all %d interceptors...', this.interceptors.length)\n\n for (const interceptor of this.interceptors) {\n logger.info('applying \"%s\" interceptor...', interceptor.constructor.name)\n interceptor.apply()\n\n logger.info('adding interceptor dispose subscription')\n this.subscriptions.push(() => interceptor.dispose())\n }\n }\n\n public on<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n // Instead of adding a listener to the batch interceptor,\n // propagate the listener to each of the individual interceptors.\n for (const interceptor of this.interceptors) {\n interceptor.on(event, listener)\n }\n\n return this\n }\n\n public once<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n for (const interceptor of this.interceptors) {\n interceptor.once(event, listener)\n }\n\n return this\n }\n\n public off<EventName extends ExtractEventNames<Events>>(\n event: EventName,\n listener: Listener<Events[EventName]>\n ): this {\n for (const interceptor of this.interceptors) {\n interceptor.off(event, listener)\n }\n\n return this\n }\n\n public removeAllListeners<EventName extends ExtractEventNames<Events>>(\n event?: EventName | undefined\n ): this {\n for (const interceptors of this.interceptors) {\n interceptors.removeAllListeners(event)\n }\n\n return this\n }\n}\n","/**\n * Removes query parameters and hashes from a given URL.\n */\nexport function getCleanUrl(url: URL, isAbsolute: boolean = true): string {\n return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('')\n}\n"],"mappings":";;;;;;;;;AAsBA,IAAa,mBAAb,MAAa,yBAGHA,oCAAoB;CAK5B,YAAY,SAAmD;AAC7D,mBAAiB,SAAS,OAAO,QAAQ,KAAK;AAC9C,QAAM,iBAAiB,OAAO;AAC9B,OAAK,eAAe,QAAQ;;CAG9B,AAAU,QAAQ;EAChB,MAAM,SAAS,KAAK,OAAO,OAAO,QAAQ;AAE1C,SAAO,KAAK,mCAAmC,KAAK,aAAa,OAAO;AAExE,OAAK,MAAM,eAAe,KAAK,cAAc;AAC3C,UAAO,KAAK,kCAAgC,YAAY,YAAY,KAAK;AACzE,eAAY,OAAO;AAEnB,UAAO,KAAK,0CAA0C;AACtD,QAAK,cAAc,WAAW,YAAY,SAAS,CAAC;;;CAIxD,AAAO,GACL,OACA,UACM;AAGN,OAAK,MAAM,eAAe,KAAK,aAC7B,aAAY,GAAG,OAAO,SAAS;AAGjC,SAAO;;CAGT,AAAO,KACL,OACA,UACM;AACN,OAAK,MAAM,eAAe,KAAK,aAC7B,aAAY,KAAK,OAAO,SAAS;AAGnC,SAAO;;CAGT,AAAO,IACL,OACA,UACM;AACN,OAAK,MAAM,eAAe,KAAK,aAC7B,aAAY,IAAI,OAAO,SAAS;AAGlC,SAAO;;CAGT,AAAO,mBACL,OACM;AACN,OAAK,MAAM,gBAAgB,KAAK,aAC9B,cAAa,mBAAmB,MAAM;AAGxC,SAAO;;;;;;;;;ACzFX,SAAgB,YAAY,KAAU,aAAsB,MAAc;AACxE,QAAO,CAAC,cAAc,IAAI,QAAQ,IAAI,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG"}
|