@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,621 @@
|
|
|
1
|
+
const require_createRequestId = require('../../createRequestId-Cs4oXfa1.cjs');
|
|
2
|
+
const require_hasConfigurableGlobal = require('../../hasConfigurableGlobal-D7S3l5h6.cjs');
|
|
3
|
+
let _open_draft_deferred_promise = require("@open-draft/deferred-promise");
|
|
4
|
+
let outvariant = require("outvariant");
|
|
5
|
+
|
|
6
|
+
//#region src/interceptors/WebSocket/utils/bindEvent.ts
|
|
7
|
+
function bindEvent(target, event) {
|
|
8
|
+
Object.defineProperties(event, {
|
|
9
|
+
target: {
|
|
10
|
+
value: target,
|
|
11
|
+
enumerable: true,
|
|
12
|
+
writable: true
|
|
13
|
+
},
|
|
14
|
+
currentTarget: {
|
|
15
|
+
value: target,
|
|
16
|
+
enumerable: true,
|
|
17
|
+
writable: true
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return event;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/interceptors/WebSocket/utils/events.ts
|
|
25
|
+
const kCancelable = Symbol("kCancelable");
|
|
26
|
+
const kDefaultPrevented = Symbol("kDefaultPrevented");
|
|
27
|
+
/**
|
|
28
|
+
* A `MessageEvent` superset that supports event cancellation
|
|
29
|
+
* in Node.js. It's rather non-intrusive so it can be safely
|
|
30
|
+
* used in the browser as well.
|
|
31
|
+
*
|
|
32
|
+
* @see https://github.com/nodejs/node/issues/51767
|
|
33
|
+
*/
|
|
34
|
+
var CancelableMessageEvent = class extends MessageEvent {
|
|
35
|
+
constructor(type, init) {
|
|
36
|
+
super(type, init);
|
|
37
|
+
this[kCancelable] = !!init.cancelable;
|
|
38
|
+
this[kDefaultPrevented] = false;
|
|
39
|
+
}
|
|
40
|
+
get cancelable() {
|
|
41
|
+
return this[kCancelable];
|
|
42
|
+
}
|
|
43
|
+
set cancelable(nextCancelable) {
|
|
44
|
+
this[kCancelable] = nextCancelable;
|
|
45
|
+
}
|
|
46
|
+
get defaultPrevented() {
|
|
47
|
+
return this[kDefaultPrevented];
|
|
48
|
+
}
|
|
49
|
+
set defaultPrevented(nextDefaultPrevented) {
|
|
50
|
+
this[kDefaultPrevented] = nextDefaultPrevented;
|
|
51
|
+
}
|
|
52
|
+
preventDefault() {
|
|
53
|
+
if (this.cancelable && !this[kDefaultPrevented]) this[kDefaultPrevented] = true;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var CloseEvent = class extends Event {
|
|
57
|
+
constructor(type, init = {}) {
|
|
58
|
+
super(type, init);
|
|
59
|
+
this.code = init.code === void 0 ? 0 : init.code;
|
|
60
|
+
this.reason = init.reason === void 0 ? "" : init.reason;
|
|
61
|
+
this.wasClean = init.wasClean === void 0 ? false : init.wasClean;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var CancelableCloseEvent = class extends CloseEvent {
|
|
65
|
+
constructor(type, init = {}) {
|
|
66
|
+
super(type, init);
|
|
67
|
+
this[kCancelable] = !!init.cancelable;
|
|
68
|
+
this[kDefaultPrevented] = false;
|
|
69
|
+
}
|
|
70
|
+
get cancelable() {
|
|
71
|
+
return this[kCancelable];
|
|
72
|
+
}
|
|
73
|
+
set cancelable(nextCancelable) {
|
|
74
|
+
this[kCancelable] = nextCancelable;
|
|
75
|
+
}
|
|
76
|
+
get defaultPrevented() {
|
|
77
|
+
return this[kDefaultPrevented];
|
|
78
|
+
}
|
|
79
|
+
set defaultPrevented(nextDefaultPrevented) {
|
|
80
|
+
this[kDefaultPrevented] = nextDefaultPrevented;
|
|
81
|
+
}
|
|
82
|
+
preventDefault() {
|
|
83
|
+
if (this.cancelable && !this[kDefaultPrevented]) this[kDefaultPrevented] = true;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/interceptors/WebSocket/WebSocketClientConnection.ts
|
|
89
|
+
const kEmitter$1 = Symbol("kEmitter");
|
|
90
|
+
const kBoundListener$1 = Symbol("kBoundListener");
|
|
91
|
+
var WebSocketClientConnectionProtocol = class {};
|
|
92
|
+
/**
|
|
93
|
+
* The WebSocket client instance represents an incoming
|
|
94
|
+
* client connection. The user can control the connection,
|
|
95
|
+
* send and receive events.
|
|
96
|
+
*/
|
|
97
|
+
var WebSocketClientConnection = class {
|
|
98
|
+
constructor(socket, transport) {
|
|
99
|
+
this.socket = socket;
|
|
100
|
+
this.transport = transport;
|
|
101
|
+
this.id = require_createRequestId.createRequestId();
|
|
102
|
+
this.url = new URL(socket.url);
|
|
103
|
+
this[kEmitter$1] = new EventTarget();
|
|
104
|
+
this.transport.addEventListener("outgoing", (event) => {
|
|
105
|
+
const message = bindEvent(this.socket, new CancelableMessageEvent("message", {
|
|
106
|
+
data: event.data,
|
|
107
|
+
origin: event.origin,
|
|
108
|
+
cancelable: true
|
|
109
|
+
}));
|
|
110
|
+
this[kEmitter$1].dispatchEvent(message);
|
|
111
|
+
if (message.defaultPrevented) event.preventDefault();
|
|
112
|
+
});
|
|
113
|
+
/**
|
|
114
|
+
* Emit the "close" event on the "client" connection
|
|
115
|
+
* whenever the underlying transport is closed.
|
|
116
|
+
* @note "client.close()" does NOT dispatch the "close"
|
|
117
|
+
* event on the WebSocket because it uses non-configurable
|
|
118
|
+
* close status code. Thus, we listen to the transport
|
|
119
|
+
* instead of the WebSocket's "close" event.
|
|
120
|
+
*/
|
|
121
|
+
this.transport.addEventListener("close", (event) => {
|
|
122
|
+
this[kEmitter$1].dispatchEvent(bindEvent(this.socket, new CloseEvent("close", event)));
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Listen for the outgoing events from the connected WebSocket client.
|
|
127
|
+
*/
|
|
128
|
+
addEventListener(type, listener, options) {
|
|
129
|
+
if (!Reflect.has(listener, kBoundListener$1)) {
|
|
130
|
+
const boundListener = listener.bind(this.socket);
|
|
131
|
+
Object.defineProperty(listener, kBoundListener$1, {
|
|
132
|
+
value: boundListener,
|
|
133
|
+
enumerable: false,
|
|
134
|
+
configurable: false
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
this[kEmitter$1].addEventListener(type, Reflect.get(listener, kBoundListener$1), options);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Removes the listener for the given event.
|
|
141
|
+
*/
|
|
142
|
+
removeEventListener(event, listener, options) {
|
|
143
|
+
this[kEmitter$1].removeEventListener(event, Reflect.get(listener, kBoundListener$1), options);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Send data to the connected client.
|
|
147
|
+
*/
|
|
148
|
+
send(data) {
|
|
149
|
+
this.transport.send(data);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Close the WebSocket connection.
|
|
153
|
+
* @param {number} code A status code (see https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1).
|
|
154
|
+
* @param {string} reason A custom connection close reason.
|
|
155
|
+
*/
|
|
156
|
+
close(code, reason) {
|
|
157
|
+
this.transport.close(code, reason);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/interceptors/WebSocket/WebSocketOverride.ts
|
|
163
|
+
const WEBSOCKET_CLOSE_CODE_RANGE_ERROR = "InvalidAccessError: close code out of user configurable range";
|
|
164
|
+
const kPassthroughPromise = Symbol("kPassthroughPromise");
|
|
165
|
+
const kOnSend = Symbol("kOnSend");
|
|
166
|
+
const kClose = Symbol("kClose");
|
|
167
|
+
var WebSocketOverride = class extends EventTarget {
|
|
168
|
+
static {
|
|
169
|
+
this.CONNECTING = 0;
|
|
170
|
+
}
|
|
171
|
+
static {
|
|
172
|
+
this.OPEN = 1;
|
|
173
|
+
}
|
|
174
|
+
static {
|
|
175
|
+
this.CLOSING = 2;
|
|
176
|
+
}
|
|
177
|
+
static {
|
|
178
|
+
this.CLOSED = 3;
|
|
179
|
+
}
|
|
180
|
+
constructor(url, protocols) {
|
|
181
|
+
super();
|
|
182
|
+
this.CONNECTING = 0;
|
|
183
|
+
this.OPEN = 1;
|
|
184
|
+
this.CLOSING = 2;
|
|
185
|
+
this.CLOSED = 3;
|
|
186
|
+
this._onopen = null;
|
|
187
|
+
this._onmessage = null;
|
|
188
|
+
this._onerror = null;
|
|
189
|
+
this._onclose = null;
|
|
190
|
+
this.url = url.toString();
|
|
191
|
+
this.protocol = "";
|
|
192
|
+
this.extensions = "";
|
|
193
|
+
this.binaryType = "blob";
|
|
194
|
+
this.readyState = this.CONNECTING;
|
|
195
|
+
this.bufferedAmount = 0;
|
|
196
|
+
this[kPassthroughPromise] = new _open_draft_deferred_promise.DeferredPromise();
|
|
197
|
+
queueMicrotask(async () => {
|
|
198
|
+
if (await this[kPassthroughPromise]) return;
|
|
199
|
+
this.protocol = typeof protocols === "string" ? protocols : Array.isArray(protocols) && protocols.length > 0 ? protocols[0] : "";
|
|
200
|
+
/**
|
|
201
|
+
* @note Check that nothing has prevented this connection
|
|
202
|
+
* (e.g. called `client.close()` in the connection listener).
|
|
203
|
+
* If the connection has been prevented, never dispatch the open event,.
|
|
204
|
+
*/
|
|
205
|
+
if (this.readyState === this.CONNECTING) {
|
|
206
|
+
this.readyState = this.OPEN;
|
|
207
|
+
this.dispatchEvent(bindEvent(this, new Event("open")));
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
set onopen(listener) {
|
|
212
|
+
this.removeEventListener("open", this._onopen);
|
|
213
|
+
this._onopen = listener;
|
|
214
|
+
if (listener !== null) this.addEventListener("open", listener);
|
|
215
|
+
}
|
|
216
|
+
get onopen() {
|
|
217
|
+
return this._onopen;
|
|
218
|
+
}
|
|
219
|
+
set onmessage(listener) {
|
|
220
|
+
this.removeEventListener("message", this._onmessage);
|
|
221
|
+
this._onmessage = listener;
|
|
222
|
+
if (listener !== null) this.addEventListener("message", listener);
|
|
223
|
+
}
|
|
224
|
+
get onmessage() {
|
|
225
|
+
return this._onmessage;
|
|
226
|
+
}
|
|
227
|
+
set onerror(listener) {
|
|
228
|
+
this.removeEventListener("error", this._onerror);
|
|
229
|
+
this._onerror = listener;
|
|
230
|
+
if (listener !== null) this.addEventListener("error", listener);
|
|
231
|
+
}
|
|
232
|
+
get onerror() {
|
|
233
|
+
return this._onerror;
|
|
234
|
+
}
|
|
235
|
+
set onclose(listener) {
|
|
236
|
+
this.removeEventListener("close", this._onclose);
|
|
237
|
+
this._onclose = listener;
|
|
238
|
+
if (listener !== null) this.addEventListener("close", listener);
|
|
239
|
+
}
|
|
240
|
+
get onclose() {
|
|
241
|
+
return this._onclose;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* @see https://websockets.spec.whatwg.org/#ref-for-dom-websocket-send%E2%91%A0
|
|
245
|
+
*/
|
|
246
|
+
send(data) {
|
|
247
|
+
if (this.readyState === this.CONNECTING) {
|
|
248
|
+
this.close();
|
|
249
|
+
throw new DOMException("InvalidStateError");
|
|
250
|
+
}
|
|
251
|
+
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) return;
|
|
252
|
+
this.bufferedAmount += getDataSize(data);
|
|
253
|
+
queueMicrotask(() => {
|
|
254
|
+
this.bufferedAmount = 0;
|
|
255
|
+
/**
|
|
256
|
+
* @note Notify the parent about outgoing data.
|
|
257
|
+
* This notifies the transport and the connection
|
|
258
|
+
* listens to the outgoing data to emit the "message" event.
|
|
259
|
+
*/
|
|
260
|
+
this[kOnSend]?.(data);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
close(code = 1e3, reason) {
|
|
264
|
+
(0, outvariant.invariant)(code, WEBSOCKET_CLOSE_CODE_RANGE_ERROR);
|
|
265
|
+
(0, outvariant.invariant)(code === 1e3 || code >= 3e3 && code <= 4999, WEBSOCKET_CLOSE_CODE_RANGE_ERROR);
|
|
266
|
+
this[kClose](code, reason);
|
|
267
|
+
}
|
|
268
|
+
[kClose](code = 1e3, reason, wasClean = true) {
|
|
269
|
+
/**
|
|
270
|
+
* @note Move this check here so that even internal closures,
|
|
271
|
+
* like those triggered by the `server` connection, are not
|
|
272
|
+
* performed twice.
|
|
273
|
+
*/
|
|
274
|
+
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) return;
|
|
275
|
+
this.readyState = this.CLOSING;
|
|
276
|
+
queueMicrotask(() => {
|
|
277
|
+
this.readyState = this.CLOSED;
|
|
278
|
+
this.dispatchEvent(bindEvent(this, new CloseEvent("close", {
|
|
279
|
+
code,
|
|
280
|
+
reason,
|
|
281
|
+
wasClean
|
|
282
|
+
})));
|
|
283
|
+
this._onopen = null;
|
|
284
|
+
this._onmessage = null;
|
|
285
|
+
this._onerror = null;
|
|
286
|
+
this._onclose = null;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
addEventListener(type, listener, options) {
|
|
290
|
+
return super.addEventListener(type, listener, options);
|
|
291
|
+
}
|
|
292
|
+
removeEventListener(type, callback, options) {
|
|
293
|
+
return super.removeEventListener(type, callback, options);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
function getDataSize(data) {
|
|
297
|
+
if (typeof data === "string") return data.length;
|
|
298
|
+
if (data instanceof Blob) return data.size;
|
|
299
|
+
return data.byteLength;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/interceptors/WebSocket/WebSocketServerConnection.ts
|
|
304
|
+
const kEmitter = Symbol("kEmitter");
|
|
305
|
+
const kBoundListener = Symbol("kBoundListener");
|
|
306
|
+
const kSend = Symbol("kSend");
|
|
307
|
+
var WebSocketServerConnectionProtocol = class {};
|
|
308
|
+
/**
|
|
309
|
+
* The WebSocket server instance represents the actual production
|
|
310
|
+
* WebSocket server connection. It's idle by default but you can
|
|
311
|
+
* establish it by calling `server.connect()`.
|
|
312
|
+
*/
|
|
313
|
+
var WebSocketServerConnection = class {
|
|
314
|
+
constructor(client, transport, createConnection) {
|
|
315
|
+
this.client = client;
|
|
316
|
+
this.transport = transport;
|
|
317
|
+
this.createConnection = createConnection;
|
|
318
|
+
this[kEmitter] = new EventTarget();
|
|
319
|
+
this.mockCloseController = new AbortController();
|
|
320
|
+
this.realCloseController = new AbortController();
|
|
321
|
+
this.transport.addEventListener("outgoing", (event) => {
|
|
322
|
+
if (typeof this.realWebSocket === "undefined") return;
|
|
323
|
+
queueMicrotask(() => {
|
|
324
|
+
if (!event.defaultPrevented)
|
|
325
|
+
/**
|
|
326
|
+
* @note Use the internal send mechanism so consumers can tell
|
|
327
|
+
* apart direct user calls to `server.send()` and internal calls.
|
|
328
|
+
* E.g. MSW has to ignore this internal call to log out messages correctly.
|
|
329
|
+
*/
|
|
330
|
+
this[kSend](event.data);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
this.transport.addEventListener("incoming", this.handleIncomingMessage.bind(this));
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* The `WebSocket` instance connected to the original server.
|
|
337
|
+
* Accessing this before calling `server.connect()` will throw.
|
|
338
|
+
*/
|
|
339
|
+
get socket() {
|
|
340
|
+
(0, outvariant.invariant)(this.realWebSocket, "Cannot access \"socket\" on the original WebSocket server object: the connection is not open. Did you forget to call `server.connect()`?");
|
|
341
|
+
return this.realWebSocket;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Open connection to the original WebSocket server.
|
|
345
|
+
*/
|
|
346
|
+
connect() {
|
|
347
|
+
(0, outvariant.invariant)(!this.realWebSocket || this.realWebSocket.readyState !== WebSocket.OPEN, "Failed to call \"connect()\" on the original WebSocket instance: the connection already open");
|
|
348
|
+
const realWebSocket = this.createConnection();
|
|
349
|
+
realWebSocket.binaryType = this.client.binaryType;
|
|
350
|
+
realWebSocket.addEventListener("open", (event) => {
|
|
351
|
+
this[kEmitter].dispatchEvent(bindEvent(this.realWebSocket, new Event("open", event)));
|
|
352
|
+
}, { once: true });
|
|
353
|
+
realWebSocket.addEventListener("message", (event) => {
|
|
354
|
+
this.transport.dispatchEvent(bindEvent(this.realWebSocket, new MessageEvent("incoming", {
|
|
355
|
+
data: event.data,
|
|
356
|
+
origin: event.origin
|
|
357
|
+
})));
|
|
358
|
+
});
|
|
359
|
+
this.client.addEventListener("close", (event) => {
|
|
360
|
+
this.handleMockClose(event);
|
|
361
|
+
}, { signal: this.mockCloseController.signal });
|
|
362
|
+
realWebSocket.addEventListener("close", (event) => {
|
|
363
|
+
this.handleRealClose(event);
|
|
364
|
+
}, { signal: this.realCloseController.signal });
|
|
365
|
+
realWebSocket.addEventListener("error", () => {
|
|
366
|
+
const errorEvent = bindEvent(realWebSocket, new Event("error", { cancelable: true }));
|
|
367
|
+
this[kEmitter].dispatchEvent(errorEvent);
|
|
368
|
+
if (!errorEvent.defaultPrevented) this.client.dispatchEvent(bindEvent(this.client, new Event("error")));
|
|
369
|
+
});
|
|
370
|
+
this.realWebSocket = realWebSocket;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Listen for the incoming events from the original WebSocket server.
|
|
374
|
+
*/
|
|
375
|
+
addEventListener(event, listener, options) {
|
|
376
|
+
if (!Reflect.has(listener, kBoundListener)) {
|
|
377
|
+
const boundListener = listener.bind(this.client);
|
|
378
|
+
Object.defineProperty(listener, kBoundListener, {
|
|
379
|
+
value: boundListener,
|
|
380
|
+
enumerable: false
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
this[kEmitter].addEventListener(event, Reflect.get(listener, kBoundListener), options);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Remove the listener for the given event.
|
|
387
|
+
*/
|
|
388
|
+
removeEventListener(event, listener, options) {
|
|
389
|
+
this[kEmitter].removeEventListener(event, Reflect.get(listener, kBoundListener), options);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Send data to the original WebSocket server.
|
|
393
|
+
* @example
|
|
394
|
+
* server.send('hello')
|
|
395
|
+
* server.send(new Blob(['hello']))
|
|
396
|
+
* server.send(new TextEncoder().encode('hello'))
|
|
397
|
+
*/
|
|
398
|
+
send(data) {
|
|
399
|
+
this[kSend](data);
|
|
400
|
+
}
|
|
401
|
+
[kSend](data) {
|
|
402
|
+
const { realWebSocket } = this;
|
|
403
|
+
(0, outvariant.invariant)(realWebSocket, "Failed to call \"server.send()\" for \"%s\": the connection is not open. Did you forget to call \"server.connect()\"?", this.client.url);
|
|
404
|
+
if (realWebSocket.readyState === WebSocket.CLOSING || realWebSocket.readyState === WebSocket.CLOSED) return;
|
|
405
|
+
if (realWebSocket.readyState === WebSocket.CONNECTING) {
|
|
406
|
+
realWebSocket.addEventListener("open", () => {
|
|
407
|
+
realWebSocket.send(data);
|
|
408
|
+
}, { once: true });
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
realWebSocket.send(data);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Close the actual server connection.
|
|
415
|
+
*/
|
|
416
|
+
close() {
|
|
417
|
+
const { realWebSocket } = this;
|
|
418
|
+
(0, outvariant.invariant)(realWebSocket, "Failed to close server connection for \"%s\": the connection is not open. Did you forget to call \"server.connect()\"?", this.client.url);
|
|
419
|
+
this.realCloseController.abort();
|
|
420
|
+
if (realWebSocket.readyState === WebSocket.CLOSING || realWebSocket.readyState === WebSocket.CLOSED) return;
|
|
421
|
+
realWebSocket.close();
|
|
422
|
+
queueMicrotask(() => {
|
|
423
|
+
this[kEmitter].dispatchEvent(bindEvent(this.realWebSocket, new CancelableCloseEvent("close", {
|
|
424
|
+
code: 1e3,
|
|
425
|
+
cancelable: true
|
|
426
|
+
})));
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
handleIncomingMessage(event) {
|
|
430
|
+
const messageEvent = bindEvent(event.target, new CancelableMessageEvent("message", {
|
|
431
|
+
data: event.data,
|
|
432
|
+
origin: event.origin,
|
|
433
|
+
cancelable: true
|
|
434
|
+
}));
|
|
435
|
+
/**
|
|
436
|
+
* @note Emit "message" event on the server connection
|
|
437
|
+
* instance to let the interceptor know about these
|
|
438
|
+
* incoming events from the original server. In that listener,
|
|
439
|
+
* the interceptor can modify or skip the event forwarding
|
|
440
|
+
* to the mock WebSocket instance.
|
|
441
|
+
*/
|
|
442
|
+
this[kEmitter].dispatchEvent(messageEvent);
|
|
443
|
+
/**
|
|
444
|
+
* @note Forward the incoming server events to the client.
|
|
445
|
+
* Preventing the default on the message event stops this.
|
|
446
|
+
*/
|
|
447
|
+
if (!messageEvent.defaultPrevented) this.client.dispatchEvent(bindEvent(
|
|
448
|
+
/**
|
|
449
|
+
* @note Bind the forwarded original server events
|
|
450
|
+
* to the mock WebSocket instance so it would
|
|
451
|
+
* dispatch them straight away.
|
|
452
|
+
*/
|
|
453
|
+
this.client,
|
|
454
|
+
new MessageEvent("message", {
|
|
455
|
+
data: event.data,
|
|
456
|
+
origin: event.origin
|
|
457
|
+
})
|
|
458
|
+
));
|
|
459
|
+
}
|
|
460
|
+
handleMockClose(_event) {
|
|
461
|
+
if (this.realWebSocket) this.realWebSocket.close();
|
|
462
|
+
}
|
|
463
|
+
handleRealClose(event) {
|
|
464
|
+
this.mockCloseController.abort();
|
|
465
|
+
const closeEvent = bindEvent(this.realWebSocket, new CancelableCloseEvent("close", {
|
|
466
|
+
code: event.code,
|
|
467
|
+
reason: event.reason,
|
|
468
|
+
wasClean: event.wasClean,
|
|
469
|
+
cancelable: true
|
|
470
|
+
}));
|
|
471
|
+
this[kEmitter].dispatchEvent(closeEvent);
|
|
472
|
+
if (!closeEvent.defaultPrevented) this.client[kClose](event.code, event.reason);
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/interceptors/WebSocket/WebSocketClassTransport.ts
|
|
478
|
+
/**
|
|
479
|
+
* Abstraction over the given mock `WebSocket` instance that allows
|
|
480
|
+
* for controlling that instance (e.g. sending and receiving messages).
|
|
481
|
+
*/
|
|
482
|
+
var WebSocketClassTransport = class extends EventTarget {
|
|
483
|
+
constructor(socket) {
|
|
484
|
+
super();
|
|
485
|
+
this.socket = socket;
|
|
486
|
+
this.socket.addEventListener("close", (event) => {
|
|
487
|
+
this.dispatchEvent(bindEvent(this.socket, new CloseEvent("close", event)));
|
|
488
|
+
});
|
|
489
|
+
/**
|
|
490
|
+
* Emit the "outgoing" event on the transport
|
|
491
|
+
* whenever the WebSocket client sends data ("ws.send()").
|
|
492
|
+
*/
|
|
493
|
+
this.socket[kOnSend] = (data) => {
|
|
494
|
+
this.dispatchEvent(bindEvent(this.socket, new CancelableMessageEvent("outgoing", {
|
|
495
|
+
data,
|
|
496
|
+
origin: this.socket.url,
|
|
497
|
+
cancelable: true
|
|
498
|
+
})));
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
addEventListener(type, callback, options) {
|
|
502
|
+
return super.addEventListener(type, callback, options);
|
|
503
|
+
}
|
|
504
|
+
dispatchEvent(event) {
|
|
505
|
+
return super.dispatchEvent(event);
|
|
506
|
+
}
|
|
507
|
+
send(data) {
|
|
508
|
+
queueMicrotask(() => {
|
|
509
|
+
if (this.socket.readyState === this.socket.CLOSING || this.socket.readyState === this.socket.CLOSED) return;
|
|
510
|
+
const dispatchEvent = () => {
|
|
511
|
+
this.socket.dispatchEvent(bindEvent(
|
|
512
|
+
/**
|
|
513
|
+
* @note Setting this event's "target" to the
|
|
514
|
+
* WebSocket override instance is important.
|
|
515
|
+
* This way it can tell apart original incoming events
|
|
516
|
+
* (must be forwarded to the transport) from the
|
|
517
|
+
* mocked message events like the one below
|
|
518
|
+
* (must be dispatched on the client instance).
|
|
519
|
+
*/
|
|
520
|
+
this.socket,
|
|
521
|
+
new MessageEvent("message", {
|
|
522
|
+
data,
|
|
523
|
+
origin: this.socket.url
|
|
524
|
+
})
|
|
525
|
+
));
|
|
526
|
+
};
|
|
527
|
+
if (this.socket.readyState === this.socket.CONNECTING) this.socket.addEventListener("open", () => {
|
|
528
|
+
dispatchEvent();
|
|
529
|
+
}, { once: true });
|
|
530
|
+
else dispatchEvent();
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
close(code, reason) {
|
|
534
|
+
/**
|
|
535
|
+
* @note Call the internal close method directly
|
|
536
|
+
* to allow closing the connection with the status codes
|
|
537
|
+
* that are non-configurable by the user (> 1000 <= 1015).
|
|
538
|
+
*/
|
|
539
|
+
this.socket[kClose](code, reason);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
//#endregion
|
|
544
|
+
//#region src/interceptors/WebSocket/index.ts
|
|
545
|
+
/**
|
|
546
|
+
* Intercept the outgoing WebSocket connections created using
|
|
547
|
+
* the global `WebSocket` class.
|
|
548
|
+
*/
|
|
549
|
+
var WebSocketInterceptor = class WebSocketInterceptor extends require_createRequestId.Interceptor {
|
|
550
|
+
static {
|
|
551
|
+
this.symbol = Symbol("websocket");
|
|
552
|
+
}
|
|
553
|
+
constructor() {
|
|
554
|
+
super(WebSocketInterceptor.symbol);
|
|
555
|
+
}
|
|
556
|
+
checkEnvironment() {
|
|
557
|
+
return require_hasConfigurableGlobal.hasConfigurableGlobal("WebSocket");
|
|
558
|
+
}
|
|
559
|
+
setup() {
|
|
560
|
+
const originalWebSocketDescriptor = Object.getOwnPropertyDescriptor(globalThis, "WebSocket");
|
|
561
|
+
const WebSocketProxy = new Proxy(globalThis.WebSocket, { construct: (target, args, newTarget) => {
|
|
562
|
+
const [url, protocols] = args;
|
|
563
|
+
const createConnection = () => {
|
|
564
|
+
return Reflect.construct(target, args, newTarget);
|
|
565
|
+
};
|
|
566
|
+
const socket = new WebSocketOverride(url, protocols);
|
|
567
|
+
const transport = new WebSocketClassTransport(socket);
|
|
568
|
+
queueMicrotask(async () => {
|
|
569
|
+
try {
|
|
570
|
+
const server = new WebSocketServerConnection(socket, transport, createConnection);
|
|
571
|
+
const hasConnectionListeners = this.emitter.listenerCount("connection") > 0;
|
|
572
|
+
await require_hasConfigurableGlobal.emitAsync(this.emitter, "connection", {
|
|
573
|
+
client: new WebSocketClientConnection(socket, transport),
|
|
574
|
+
server,
|
|
575
|
+
info: { protocols }
|
|
576
|
+
});
|
|
577
|
+
if (hasConnectionListeners) socket[kPassthroughPromise].resolve(false);
|
|
578
|
+
else {
|
|
579
|
+
socket[kPassthroughPromise].resolve(true);
|
|
580
|
+
server.connect();
|
|
581
|
+
server.addEventListener("open", () => {
|
|
582
|
+
socket.dispatchEvent(bindEvent(socket, new Event("open")));
|
|
583
|
+
if (server["realWebSocket"]) socket.protocol = server["realWebSocket"].protocol;
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
} catch (error) {
|
|
587
|
+
/**
|
|
588
|
+
* @note Translate unhandled exceptions during the connection
|
|
589
|
+
* handling (i.e. interceptor exceptions) as WebSocket connection
|
|
590
|
+
* closures with error. This prevents from the exceptions occurring
|
|
591
|
+
* in `queueMicrotask` from being process-wide and uncatchable.
|
|
592
|
+
*/
|
|
593
|
+
if (error instanceof Error) {
|
|
594
|
+
socket.dispatchEvent(new Event("error"));
|
|
595
|
+
if (socket.readyState !== WebSocket.CLOSING && socket.readyState !== WebSocket.CLOSED) socket[kClose](1011, error.message, false);
|
|
596
|
+
console.error(error);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
return socket;
|
|
601
|
+
} });
|
|
602
|
+
Object.defineProperty(globalThis, "WebSocket", {
|
|
603
|
+
value: WebSocketProxy,
|
|
604
|
+
configurable: true
|
|
605
|
+
});
|
|
606
|
+
this.subscriptions.push(() => {
|
|
607
|
+
Object.defineProperty(globalThis, "WebSocket", originalWebSocketDescriptor);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
//#endregion
|
|
613
|
+
exports.CancelableCloseEvent = CancelableCloseEvent;
|
|
614
|
+
exports.CancelableMessageEvent = CancelableMessageEvent;
|
|
615
|
+
exports.CloseEvent = CloseEvent;
|
|
616
|
+
exports.WebSocketClientConnection = WebSocketClientConnection;
|
|
617
|
+
exports.WebSocketClientConnectionProtocol = WebSocketClientConnectionProtocol;
|
|
618
|
+
exports.WebSocketInterceptor = WebSocketInterceptor;
|
|
619
|
+
exports.WebSocketServerConnection = WebSocketServerConnection;
|
|
620
|
+
exports.WebSocketServerConnectionProtocol = WebSocketServerConnectionProtocol;
|
|
621
|
+
//# sourceMappingURL=index.cjs.map
|