@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,220 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CbDLau6x.cjs');
|
|
2
|
+
const require_fetchUtils = require('./fetchUtils-BaY5iWXw.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/isPropertyAccessible.ts
|
|
7
|
+
/**
|
|
8
|
+
* A function that validates if property access is possible on an object
|
|
9
|
+
* without throwing. It returns `true` if the property access is possible
|
|
10
|
+
* and `false` otherwise.
|
|
11
|
+
*
|
|
12
|
+
* Environments like miniflare will throw on property access on certain objects
|
|
13
|
+
* like Request and Response, for unimplemented properties.
|
|
14
|
+
*/
|
|
15
|
+
function isPropertyAccessible(obj, key) {
|
|
16
|
+
try {
|
|
17
|
+
obj[key];
|
|
18
|
+
return true;
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/utils/emitAsync.ts
|
|
26
|
+
/**
|
|
27
|
+
* Emits an event on the given emitter but executes
|
|
28
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
29
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
30
|
+
*/
|
|
31
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
32
|
+
const listeners = emitter.listeners(eventName);
|
|
33
|
+
if (listeners.length === 0) return;
|
|
34
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/utils/isObject.ts
|
|
39
|
+
/**
|
|
40
|
+
* Determines if a given value is an instance of object.
|
|
41
|
+
*/
|
|
42
|
+
function isObject(value, loose = false) {
|
|
43
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/utils/responseUtils.ts
|
|
48
|
+
/**
|
|
49
|
+
* Creates a generic 500 Unhandled Exception response.
|
|
50
|
+
*/
|
|
51
|
+
function createServerErrorResponse(body) {
|
|
52
|
+
return new Response(JSON.stringify(body instanceof Error ? {
|
|
53
|
+
name: body.name,
|
|
54
|
+
message: body.message,
|
|
55
|
+
stack: body.stack
|
|
56
|
+
} : body), {
|
|
57
|
+
status: 500,
|
|
58
|
+
statusText: "Unhandled Exception",
|
|
59
|
+
headers: { "Content-Type": "application/json" }
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Check if the given response is a `Response.error()`.
|
|
64
|
+
*
|
|
65
|
+
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
66
|
+
* implement the "Response.type" property and throw on its access.
|
|
67
|
+
* Safely check if we can access "type" on "Response" before continuing.
|
|
68
|
+
* @see https://github.com/mswjs/msw/issues/1834
|
|
69
|
+
*/
|
|
70
|
+
function isResponseError(response) {
|
|
71
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Check if the given value is a `Response` or a Response-like object.
|
|
75
|
+
* This is different from `value instanceof Response` because it supports
|
|
76
|
+
* custom `Response` constructors, like the one when using Undici directly.
|
|
77
|
+
*/
|
|
78
|
+
function isResponseLike(value) {
|
|
79
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/utils/isNodeLikeError.ts
|
|
84
|
+
function isNodeLikeError(error) {
|
|
85
|
+
if (error == null) return false;
|
|
86
|
+
if (!(error instanceof Error)) return false;
|
|
87
|
+
return "code" in error && "errno" in error;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/utils/handleRequest.ts
|
|
92
|
+
async function handleRequest(options) {
|
|
93
|
+
const handleResponse = async (response) => {
|
|
94
|
+
if (response instanceof Error) {
|
|
95
|
+
await options.controller.errorWith(response);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (isResponseError(response)) {
|
|
99
|
+
await options.controller.respondWith(response);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Handle normal responses or response-like objects.
|
|
104
|
+
* @note This must come before the arbitrary object check
|
|
105
|
+
* since Response instances are, in fact, objects.
|
|
106
|
+
*/
|
|
107
|
+
if (isResponseLike(response)) {
|
|
108
|
+
await options.controller.respondWith(response);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (isObject(response)) {
|
|
112
|
+
await options.controller.errorWith(response);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
};
|
|
117
|
+
const handleResponseError = async (error) => {
|
|
118
|
+
if (error instanceof require_fetchUtils.InterceptorError) throw result.error;
|
|
119
|
+
if (isNodeLikeError(error)) {
|
|
120
|
+
await options.controller.errorWith(error);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
if (error instanceof Response) return await handleResponse(error);
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
const requestAbortPromise = new _open_draft_deferred_promise.DeferredPromise();
|
|
127
|
+
/**
|
|
128
|
+
* @note `signal` is not always defined in React Native.
|
|
129
|
+
*/
|
|
130
|
+
if (options.request.signal) {
|
|
131
|
+
if (options.request.signal.aborted) {
|
|
132
|
+
await options.controller.errorWith(options.request.signal.reason);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
options.request.signal.addEventListener("abort", () => {
|
|
136
|
+
requestAbortPromise.reject(options.request.signal.reason);
|
|
137
|
+
}, { once: true });
|
|
138
|
+
}
|
|
139
|
+
const result = await (0, _open_draft_until.until)(async () => {
|
|
140
|
+
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
141
|
+
requestId: options.requestId,
|
|
142
|
+
request: options.request,
|
|
143
|
+
controller: options.controller
|
|
144
|
+
});
|
|
145
|
+
await Promise.race([
|
|
146
|
+
requestAbortPromise,
|
|
147
|
+
requestListenersPromise,
|
|
148
|
+
options.controller.handled
|
|
149
|
+
]);
|
|
150
|
+
});
|
|
151
|
+
if (requestAbortPromise.state === "rejected") {
|
|
152
|
+
await options.controller.errorWith(requestAbortPromise.rejectionReason);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (result.error) {
|
|
156
|
+
if (await handleResponseError(result.error)) return;
|
|
157
|
+
if (options.emitter.listenerCount("unhandledException") > 0) {
|
|
158
|
+
const unhandledExceptionController = new require_fetchUtils.RequestController(options.request, {
|
|
159
|
+
passthrough() {},
|
|
160
|
+
async respondWith(response) {
|
|
161
|
+
await handleResponse(response);
|
|
162
|
+
},
|
|
163
|
+
async errorWith(reason) {
|
|
164
|
+
/**
|
|
165
|
+
* @note Handle the result of the unhandled controller
|
|
166
|
+
* in the same way as the original request controller.
|
|
167
|
+
* The exception here is that thrown errors within the
|
|
168
|
+
* "unhandledException" event do NOT result in another
|
|
169
|
+
* emit of the same event. They are forwarded as-is.
|
|
170
|
+
*/
|
|
171
|
+
await options.controller.errorWith(reason);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
await emitAsync(options.emitter, "unhandledException", {
|
|
175
|
+
error: result.error,
|
|
176
|
+
request: options.request,
|
|
177
|
+
requestId: options.requestId,
|
|
178
|
+
controller: unhandledExceptionController
|
|
179
|
+
});
|
|
180
|
+
if (unhandledExceptionController.readyState !== require_fetchUtils.RequestController.PENDING) return;
|
|
181
|
+
}
|
|
182
|
+
await options.controller.respondWith(createServerErrorResponse(result.error));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (options.controller.readyState === require_fetchUtils.RequestController.PENDING) return await options.controller.passthrough();
|
|
186
|
+
return options.controller.handled;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
190
|
+
Object.defineProperty(exports, 'emitAsync', {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function () {
|
|
193
|
+
return emitAsync;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
Object.defineProperty(exports, 'handleRequest', {
|
|
197
|
+
enumerable: true,
|
|
198
|
+
get: function () {
|
|
199
|
+
return handleRequest;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
Object.defineProperty(exports, 'isObject', {
|
|
203
|
+
enumerable: true,
|
|
204
|
+
get: function () {
|
|
205
|
+
return isObject;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
Object.defineProperty(exports, 'isPropertyAccessible', {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get: function () {
|
|
211
|
+
return isPropertyAccessible;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
Object.defineProperty(exports, 'isResponseError', {
|
|
215
|
+
enumerable: true,
|
|
216
|
+
get: function () {
|
|
217
|
+
return isResponseError;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
//# sourceMappingURL=handleRequest-Bb7Y-XLw.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleRequest-Bb7Y-XLw.cjs","names":["InterceptorError","DeferredPromise","RequestController"],"sources":["../../src/utils/isPropertyAccessible.ts","../../src/utils/emitAsync.ts","../../src/utils/isObject.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\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 { 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 * 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","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":";;;;;;;;;;;;;;AAQA,SAAgB,qBACd,KACA,KACA;AACA,KAAI;AACF,MAAI;AACJ,SAAO;SACD;AACN,SAAO;;;;;;;;;;;ACTX,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;;;;;;;;ACnBvC,SAAgB,SAAY,OAAY,QAAQ,OAAmB;AACjE,QAAO,QACH,OAAO,UAAU,SAAS,KAAK,MAAM,CAAC,WAAW,WAAW,GAC5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qCAAkB,QACtD,QAAO,MAAM,QAAQ,WAAW,aAAa;AAG/C,QAAO,QAAQ,WAAW"}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { a as InterceptorError, i as RequestController } from "./fetchUtils-CoU35g3M.mjs";
|
|
2
|
+
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
3
|
+
import { until } from "@open-draft/until";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/isPropertyAccessible.ts
|
|
6
|
+
/**
|
|
7
|
+
* A function that validates if property access is possible on an object
|
|
8
|
+
* without throwing. It returns `true` if the property access is possible
|
|
9
|
+
* and `false` otherwise.
|
|
10
|
+
*
|
|
11
|
+
* Environments like miniflare will throw on property access on certain objects
|
|
12
|
+
* like Request and Response, for unimplemented properties.
|
|
13
|
+
*/
|
|
14
|
+
function isPropertyAccessible(obj, key) {
|
|
15
|
+
try {
|
|
16
|
+
obj[key];
|
|
17
|
+
return true;
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/utils/emitAsync.ts
|
|
25
|
+
/**
|
|
26
|
+
* Emits an event on the given emitter but executes
|
|
27
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
28
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
29
|
+
*/
|
|
30
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
31
|
+
const listeners = emitter.listeners(eventName);
|
|
32
|
+
if (listeners.length === 0) return;
|
|
33
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/utils/isObject.ts
|
|
38
|
+
/**
|
|
39
|
+
* Determines if a given value is an instance of object.
|
|
40
|
+
*/
|
|
41
|
+
function isObject(value, loose = false) {
|
|
42
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/utils/responseUtils.ts
|
|
47
|
+
/**
|
|
48
|
+
* Creates a generic 500 Unhandled Exception response.
|
|
49
|
+
*/
|
|
50
|
+
function createServerErrorResponse(body) {
|
|
51
|
+
return new Response(JSON.stringify(body instanceof Error ? {
|
|
52
|
+
name: body.name,
|
|
53
|
+
message: body.message,
|
|
54
|
+
stack: body.stack
|
|
55
|
+
} : body), {
|
|
56
|
+
status: 500,
|
|
57
|
+
statusText: "Unhandled Exception",
|
|
58
|
+
headers: { "Content-Type": "application/json" }
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Check if the given response is a `Response.error()`.
|
|
63
|
+
*
|
|
64
|
+
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
65
|
+
* implement the "Response.type" property and throw on its access.
|
|
66
|
+
* Safely check if we can access "type" on "Response" before continuing.
|
|
67
|
+
* @see https://github.com/mswjs/msw/issues/1834
|
|
68
|
+
*/
|
|
69
|
+
function isResponseError(response) {
|
|
70
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check if the given value is a `Response` or a Response-like object.
|
|
74
|
+
* This is different from `value instanceof Response` because it supports
|
|
75
|
+
* custom `Response` constructors, like the one when using Undici directly.
|
|
76
|
+
*/
|
|
77
|
+
function isResponseLike(value) {
|
|
78
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/utils/isNodeLikeError.ts
|
|
83
|
+
function isNodeLikeError(error) {
|
|
84
|
+
if (error == null) return false;
|
|
85
|
+
if (!(error instanceof Error)) return false;
|
|
86
|
+
return "code" in error && "errno" in error;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/utils/handleRequest.ts
|
|
91
|
+
async function handleRequest(options) {
|
|
92
|
+
const handleResponse = async (response) => {
|
|
93
|
+
if (response instanceof Error) {
|
|
94
|
+
await options.controller.errorWith(response);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (isResponseError(response)) {
|
|
98
|
+
await options.controller.respondWith(response);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Handle normal responses or response-like objects.
|
|
103
|
+
* @note This must come before the arbitrary object check
|
|
104
|
+
* since Response instances are, in fact, objects.
|
|
105
|
+
*/
|
|
106
|
+
if (isResponseLike(response)) {
|
|
107
|
+
await options.controller.respondWith(response);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (isObject(response)) {
|
|
111
|
+
await options.controller.errorWith(response);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
const handleResponseError = async (error) => {
|
|
117
|
+
if (error instanceof InterceptorError) throw result.error;
|
|
118
|
+
if (isNodeLikeError(error)) {
|
|
119
|
+
await options.controller.errorWith(error);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (error instanceof Response) return await handleResponse(error);
|
|
123
|
+
return false;
|
|
124
|
+
};
|
|
125
|
+
const requestAbortPromise = new DeferredPromise();
|
|
126
|
+
/**
|
|
127
|
+
* @note `signal` is not always defined in React Native.
|
|
128
|
+
*/
|
|
129
|
+
if (options.request.signal) {
|
|
130
|
+
if (options.request.signal.aborted) {
|
|
131
|
+
await options.controller.errorWith(options.request.signal.reason);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
options.request.signal.addEventListener("abort", () => {
|
|
135
|
+
requestAbortPromise.reject(options.request.signal.reason);
|
|
136
|
+
}, { once: true });
|
|
137
|
+
}
|
|
138
|
+
const result = await until(async () => {
|
|
139
|
+
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
140
|
+
requestId: options.requestId,
|
|
141
|
+
request: options.request,
|
|
142
|
+
controller: options.controller
|
|
143
|
+
});
|
|
144
|
+
await Promise.race([
|
|
145
|
+
requestAbortPromise,
|
|
146
|
+
requestListenersPromise,
|
|
147
|
+
options.controller.handled
|
|
148
|
+
]);
|
|
149
|
+
});
|
|
150
|
+
if (requestAbortPromise.state === "rejected") {
|
|
151
|
+
await options.controller.errorWith(requestAbortPromise.rejectionReason);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (result.error) {
|
|
155
|
+
if (await handleResponseError(result.error)) return;
|
|
156
|
+
if (options.emitter.listenerCount("unhandledException") > 0) {
|
|
157
|
+
const unhandledExceptionController = new RequestController(options.request, {
|
|
158
|
+
passthrough() {},
|
|
159
|
+
async respondWith(response) {
|
|
160
|
+
await handleResponse(response);
|
|
161
|
+
},
|
|
162
|
+
async errorWith(reason) {
|
|
163
|
+
/**
|
|
164
|
+
* @note Handle the result of the unhandled controller
|
|
165
|
+
* in the same way as the original request controller.
|
|
166
|
+
* The exception here is that thrown errors within the
|
|
167
|
+
* "unhandledException" event do NOT result in another
|
|
168
|
+
* emit of the same event. They are forwarded as-is.
|
|
169
|
+
*/
|
|
170
|
+
await options.controller.errorWith(reason);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
await emitAsync(options.emitter, "unhandledException", {
|
|
174
|
+
error: result.error,
|
|
175
|
+
request: options.request,
|
|
176
|
+
requestId: options.requestId,
|
|
177
|
+
controller: unhandledExceptionController
|
|
178
|
+
});
|
|
179
|
+
if (unhandledExceptionController.readyState !== RequestController.PENDING) return;
|
|
180
|
+
}
|
|
181
|
+
await options.controller.respondWith(createServerErrorResponse(result.error));
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (options.controller.readyState === RequestController.PENDING) return await options.controller.passthrough();
|
|
185
|
+
return options.controller.handled;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
//#endregion
|
|
189
|
+
export { isPropertyAccessible as a, emitAsync as i, isResponseError as n, isObject as r, handleRequest as t };
|
|
190
|
+
//# sourceMappingURL=handleRequest-Y97UwBbF.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleRequest-Y97UwBbF.mjs","names":[],"sources":["../../src/utils/isPropertyAccessible.ts","../../src/utils/emitAsync.ts","../../src/utils/isObject.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\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 { 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 * 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","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":";;;;;;;;;;;;;AAQA,SAAgB,qBACd,KACA,KACA;AACA,KAAI;AACF,MAAI;AACJ,SAAO;SACD;AACN,SAAO;;;;;;;;;;;ACTX,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,KAA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B,eAAe,kBAAkB,QAE9D;;AAKJ,QAAM,QAAQ,WAAW,YACvB,0BAA0B,OAAO,MAAM,CACxC;AACD;;AAIF,KAAI,QAAQ,WAAW,eAAe,kBAAkB,QACtD,QAAO,MAAM,QAAQ,WAAW,aAAa;AAG/C,QAAO,QAAQ,WAAW"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
3
|
+
/**
|
|
4
|
+
* Returns a boolean indicating whether the given global property
|
|
5
|
+
* is defined and is configurable.
|
|
6
|
+
*/
|
|
7
|
+
function hasConfigurableGlobal(propertyName) {
|
|
8
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
|
|
9
|
+
if (typeof descriptor === "undefined") return false;
|
|
10
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
11
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
12
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
13
|
+
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.`);
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
Object.defineProperty(exports, 'hasConfigurableGlobal', {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () {
|
|
23
|
+
return hasConfigurableGlobal;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=hasConfigurableGlobal-C97fWuaA.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-C97fWuaA.cjs","names":[],"sources":["../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["/**\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":";;;;;;AAIA,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,20 @@
|
|
|
1
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns a boolean indicating whether the given global property
|
|
4
|
+
* is defined and is configurable.
|
|
5
|
+
*/
|
|
6
|
+
function hasConfigurableGlobal(propertyName) {
|
|
7
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName);
|
|
8
|
+
if (typeof descriptor === "undefined") return false;
|
|
9
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
10
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
11
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
12
|
+
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.`);
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { hasConfigurableGlobal as t };
|
|
20
|
+
//# sourceMappingURL=hasConfigurableGlobal-DBJA0vjm.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-DBJA0vjm.mjs","names":[],"sources":["../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["/**\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":";;;;;AAIA,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,113 @@
|
|
|
1
|
+
import { l as HttpRequestEventMap, r as Interceptor } from "./Interceptor-DJ2akVWI.cjs";
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
|
|
4
|
+
//#region src/interceptors/Socket/utils/normalizeSocketWriteArgs.d.ts
|
|
5
|
+
type WriteCallback = (error?: Error | null) => void;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/interceptors/Socket/MockSocket.d.ts
|
|
8
|
+
interface MockSocketOptions {
|
|
9
|
+
write: (chunk: Buffer | string, encoding: BufferEncoding | undefined, callback?: WriteCallback) => void;
|
|
10
|
+
read: (chunk: Buffer, encoding: BufferEncoding | undefined) => void;
|
|
11
|
+
}
|
|
12
|
+
declare class MockSocket extends net.Socket {
|
|
13
|
+
protected readonly options: MockSocketOptions;
|
|
14
|
+
connecting: boolean;
|
|
15
|
+
constructor(options: MockSocketOptions);
|
|
16
|
+
connect(): this;
|
|
17
|
+
write(...args: Array<unknown>): boolean;
|
|
18
|
+
end(...args: Array<unknown>): this;
|
|
19
|
+
push(chunk: any, encoding?: BufferEncoding): boolean;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/interceptors/ClientRequest/MockHttpSocket.d.ts
|
|
23
|
+
type HttpConnectionOptions = any;
|
|
24
|
+
type MockHttpSocketRequestCallback = (args: {
|
|
25
|
+
requestId: string;
|
|
26
|
+
request: Request;
|
|
27
|
+
socket: MockHttpSocket;
|
|
28
|
+
}) => void;
|
|
29
|
+
type MockHttpSocketResponseCallback = (args: {
|
|
30
|
+
requestId: string;
|
|
31
|
+
request: Request;
|
|
32
|
+
response: Response;
|
|
33
|
+
isMockedResponse: boolean;
|
|
34
|
+
socket: MockHttpSocket;
|
|
35
|
+
}) => Promise<void>;
|
|
36
|
+
interface MockHttpSocketOptions {
|
|
37
|
+
connectionOptions: HttpConnectionOptions;
|
|
38
|
+
createConnection: () => net.Socket;
|
|
39
|
+
onRequest: MockHttpSocketRequestCallback;
|
|
40
|
+
onResponse: MockHttpSocketResponseCallback;
|
|
41
|
+
}
|
|
42
|
+
declare class MockHttpSocket extends MockSocket {
|
|
43
|
+
private connectionOptions;
|
|
44
|
+
private createConnection;
|
|
45
|
+
private baseUrl;
|
|
46
|
+
private onRequest;
|
|
47
|
+
private onResponse;
|
|
48
|
+
private responseListenersPromise?;
|
|
49
|
+
private requestRawHeadersBuffer;
|
|
50
|
+
private responseRawHeadersBuffer;
|
|
51
|
+
private writeBuffer;
|
|
52
|
+
private request?;
|
|
53
|
+
private requestParser;
|
|
54
|
+
private requestStream?;
|
|
55
|
+
private shouldKeepAlive?;
|
|
56
|
+
private socketState;
|
|
57
|
+
private responseParser;
|
|
58
|
+
private responseStream?;
|
|
59
|
+
private originalSocket?;
|
|
60
|
+
constructor(options: MockHttpSocketOptions);
|
|
61
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
|
62
|
+
destroy(error?: Error | undefined): this;
|
|
63
|
+
/**
|
|
64
|
+
* Establish this Socket connection as-is and pipe
|
|
65
|
+
* its data/events through this Socket.
|
|
66
|
+
*/
|
|
67
|
+
passthrough(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Convert the given Fetch API `Response` instance to an
|
|
70
|
+
* HTTP message and push it to the socket.
|
|
71
|
+
*/
|
|
72
|
+
respondWith(response: Response): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Close this socket connection with the given error.
|
|
75
|
+
*/
|
|
76
|
+
errorWith(error?: Error): void;
|
|
77
|
+
private mockConnect;
|
|
78
|
+
private flushWriteBuffer;
|
|
79
|
+
/**
|
|
80
|
+
* This callback might be called when the request is "slow":
|
|
81
|
+
* - Request headers were fragmented across multiple TCP packages;
|
|
82
|
+
* - Request headers were too large to be processed in a single run
|
|
83
|
+
* (e.g. more than 30 request headers).
|
|
84
|
+
* @note This is called before request start.
|
|
85
|
+
*/
|
|
86
|
+
private onRequestHeaders;
|
|
87
|
+
private onRequestStart;
|
|
88
|
+
private onRequestBody;
|
|
89
|
+
private onRequestEnd;
|
|
90
|
+
/**
|
|
91
|
+
* This callback might be called when the response is "slow":
|
|
92
|
+
* - Response headers were fragmented across multiple TCP packages;
|
|
93
|
+
* - Response headers were too large to be processed in a single run
|
|
94
|
+
* (e.g. more than 30 response headers).
|
|
95
|
+
* @note This is called before response start.
|
|
96
|
+
*/
|
|
97
|
+
private onResponseHeaders;
|
|
98
|
+
private onResponseStart;
|
|
99
|
+
private onResponseBody;
|
|
100
|
+
private onResponseEnd;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/interceptors/ClientRequest/index.d.ts
|
|
104
|
+
declare class ClientRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
105
|
+
static symbol: symbol;
|
|
106
|
+
constructor();
|
|
107
|
+
protected setup(): void;
|
|
108
|
+
private onRequest;
|
|
109
|
+
onResponse: MockHttpSocketResponseCallback;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { ClientRequestInterceptor as t };
|
|
113
|
+
//# sourceMappingURL=index-BMbJ8FXL.d.cts.map
|