@mswjs/interceptors 0.41.0 → 0.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/{Interceptor-2mUoKZL1.d.mts → Interceptor-gqKgs-aF.d.mts} +1 -1
- package/lib/browser/{XMLHttpRequest-BUfglQD1.cjs → XMLHttpRequest-BACqefB-.cjs} +3 -3
- package/lib/browser/{XMLHttpRequest-BUfglQD1.cjs.map → XMLHttpRequest-BACqefB-.cjs.map} +1 -1
- package/lib/browser/{XMLHttpRequest-DS5fc8Qs.mjs → XMLHttpRequest-BvxZV0WU.mjs} +3 -3
- package/lib/browser/{XMLHttpRequest-DS5fc8Qs.mjs.map → XMLHttpRequest-BvxZV0WU.mjs.map} +1 -1
- package/lib/browser/{fetch-DSJoynSF.mjs → fetch-DdKEdDOR.mjs} +3 -3
- package/lib/browser/{fetch-DSJoynSF.mjs.map → fetch-DdKEdDOR.mjs.map} +1 -1
- package/lib/browser/{fetch-BHcqM3z7.cjs → fetch-U3v3Y4ap.cjs} +3 -3
- package/lib/browser/{fetch-BHcqM3z7.cjs.map → fetch-U3v3Y4ap.cjs.map} +1 -1
- package/lib/browser/{glossary-gEEJhK4S.d.mts → glossary-DYwOrogs.d.mts} +1 -1
- package/lib/browser/{handleRequest-DI6a7Dty.cjs → handleRequest-CvX2G-Lz.cjs} +2 -2
- package/lib/browser/{handleRequest-DI6a7Dty.cjs.map → handleRequest-CvX2G-Lz.cjs.map} +1 -1
- package/lib/browser/{handleRequest-DxGbCTbb.mjs → handleRequest-D7kpTI5U.mjs} +2 -2
- package/lib/browser/{handleRequest-DxGbCTbb.mjs.map → handleRequest-D7kpTI5U.mjs.map} +1 -1
- package/lib/browser/{hasConfigurableGlobal-D7S3l5h6.cjs → hasConfigurableGlobal-BvCTG97d.cjs} +1 -1
- package/lib/browser/{hasConfigurableGlobal-D7S3l5h6.cjs.map → hasConfigurableGlobal-BvCTG97d.cjs.map} +1 -1
- package/lib/browser/{hasConfigurableGlobal-C8kXFDic.mjs → hasConfigurableGlobal-npXitu1-.mjs} +1 -1
- package/lib/browser/{hasConfigurableGlobal-C8kXFDic.mjs.map → hasConfigurableGlobal-npXitu1-.mjs.map} +1 -1
- package/lib/browser/index.cjs +2 -0
- package/lib/browser/index.cjs.map +1 -1
- package/lib/browser/index.d.cts +10 -1
- package/lib/browser/index.d.mts +12 -3
- package/lib/browser/index.mjs +2 -1
- package/lib/browser/index.mjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +3 -2
- package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.d.mts +1 -1
- package/lib/browser/interceptors/WebSocket/index.mjs +3 -2
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +2 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +2 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +2 -2
- package/lib/browser/interceptors/fetch/index.cjs +2 -2
- package/lib/browser/interceptors/fetch/index.d.mts +2 -2
- package/lib/browser/interceptors/fetch/index.mjs +2 -2
- package/lib/browser/presets/browser.cjs +3 -3
- package/lib/browser/presets/browser.d.mts +1 -1
- package/lib/browser/presets/browser.mjs +3 -3
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs +31 -0
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs.map +1 -0
- package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs +25 -0
- package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs.map +1 -0
- package/lib/node/index.cjs +24 -0
- package/lib/node/index.cjs.map +1 -1
- package/lib/node/index.d.cts +10 -1
- package/lib/node/index.d.mts +10 -1
- package/lib/node/index.mjs +24 -1
- package/lib/node/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/interceptors/WebSocket/WebSocketClientConnection.ts +3 -5
- package/src/interceptors/WebSocket/WebSocketOverride.ts +6 -5
- package/src/interceptors/WebSocket/WebSocketServerConnection.ts +3 -5
- package/src/utils/resolveWebSocketUrl.ts +45 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import "../getRawRequest-BTaNLFr0.mjs";
|
|
2
2
|
import "../createRequestId-DQcIlohW.mjs";
|
|
3
3
|
import "../bufferUtils-BiiO6HZv.mjs";
|
|
4
|
-
import "../handleRequest-
|
|
5
|
-
import { t as FetchInterceptor } from "../fetch-
|
|
6
|
-
import { t as XMLHttpRequestInterceptor } from "../XMLHttpRequest-
|
|
4
|
+
import "../handleRequest-D7kpTI5U.mjs";
|
|
5
|
+
import { t as FetchInterceptor } from "../fetch-DdKEdDOR.mjs";
|
|
6
|
+
import { t as XMLHttpRequestInterceptor } from "../XMLHttpRequest-BvxZV0WU.mjs";
|
|
7
7
|
|
|
8
8
|
//#region src/presets/browser.ts
|
|
9
9
|
/**
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/resolveWebSocketUrl.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
5
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
6
|
+
*
|
|
7
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
8
|
+
*/
|
|
9
|
+
function resolveWebSocketUrl(url) {
|
|
10
|
+
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
|
|
11
|
+
if (url.protocol === "http:") url.protocol = "ws:";
|
|
12
|
+
else if (url.protocol === "https:") url.protocol = "wss:";
|
|
13
|
+
if (url.protocol !== "ws:" && url.protocol !== "wss:")
|
|
14
|
+
/**
|
|
15
|
+
* @note These errors are modeled after the browser errors.
|
|
16
|
+
* The exact error messages aren't provided in the specification.
|
|
17
|
+
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
18
|
+
*/
|
|
19
|
+
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
|
|
20
|
+
if (url.hash !== "") throw new SyntaxError(`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`);
|
|
21
|
+
return url.href;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
Object.defineProperty(exports, 'resolveWebSocketUrl', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return resolveWebSocketUrl;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=resolveWebSocketUrl-6K6EgqsA.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveWebSocketUrl-6K6EgqsA.cjs","names":[],"sources":["../../src/utils/resolveWebSocketUrl.ts"],"sourcesContent":["/**\n * Resolve potentially relative WebSocket URLs the same way\n * the browser does (replace the protocol, use the origin, etc).\n *\n * @see https://websockets.spec.whatwg.org//#dom-websocket-websocket\n */\nexport function resolveWebSocketUrl(url: string | URL): string {\n if (typeof url === 'string') {\n /**\n * @note Cast the string to a URL first so the parsing errors\n * are thrown as a part of the WebSocket constructor, not consumers.\n */\n const urlRecord = new URL(\n url,\n typeof location !== 'undefined' ? location.href : undefined\n )\n\n return resolveWebSocketUrl(urlRecord)\n }\n\n if (url.protocol === 'http:') {\n url.protocol = 'ws:'\n } else if (url.protocol === 'https:') {\n url.protocol = 'wss:'\n }\n\n if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {\n /**\n * @note These errors are modeled after the browser errors.\n * The exact error messages aren't provided in the specification.\n * Node.js uses more obscure error messages that I don't wish to replicate.\n */\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`\n )\n }\n\n if (url.hash !== '') {\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`\n )\n }\n\n return url.href\n}\n"],"mappings":";;;;;;;;AAMA,SAAgB,oBAAoB,KAA2B;AAC7D,KAAI,OAAO,QAAQ,SAUjB,QAAO,oBALW,IAAI,IACpB,KACA,OAAO,aAAa,cAAc,SAAS,OAAO,OACnD,CAEoC;AAGvC,KAAI,IAAI,aAAa,QACnB,KAAI,WAAW;UACN,IAAI,aAAa,SAC1B,KAAI,WAAW;AAGjB,KAAI,IAAI,aAAa,SAAS,IAAI,aAAa;;;;;;AAM7C,OAAM,IAAI,YACR,sGAAsG,IAAI,SAAS,mBACpH;AAGH,KAAI,IAAI,SAAS,GACf,OAAM,IAAI,YACR,6EAA6E,IAAI,KAAK,6DACvF;AAGH,QAAO,IAAI"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/utils/resolveWebSocketUrl.ts
|
|
2
|
+
/**
|
|
3
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
4
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
5
|
+
*
|
|
6
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
7
|
+
*/
|
|
8
|
+
function resolveWebSocketUrl(url) {
|
|
9
|
+
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
|
|
10
|
+
if (url.protocol === "http:") url.protocol = "ws:";
|
|
11
|
+
else if (url.protocol === "https:") url.protocol = "wss:";
|
|
12
|
+
if (url.protocol !== "ws:" && url.protocol !== "wss:")
|
|
13
|
+
/**
|
|
14
|
+
* @note These errors are modeled after the browser errors.
|
|
15
|
+
* The exact error messages aren't provided in the specification.
|
|
16
|
+
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
17
|
+
*/
|
|
18
|
+
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
|
|
19
|
+
if (url.hash !== "") throw new SyntaxError(`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`);
|
|
20
|
+
return url.href;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { resolveWebSocketUrl as t };
|
|
25
|
+
//# sourceMappingURL=resolveWebSocketUrl-C83-x9iE.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveWebSocketUrl-C83-x9iE.mjs","names":[],"sources":["../../src/utils/resolveWebSocketUrl.ts"],"sourcesContent":["/**\n * Resolve potentially relative WebSocket URLs the same way\n * the browser does (replace the protocol, use the origin, etc).\n *\n * @see https://websockets.spec.whatwg.org//#dom-websocket-websocket\n */\nexport function resolveWebSocketUrl(url: string | URL): string {\n if (typeof url === 'string') {\n /**\n * @note Cast the string to a URL first so the parsing errors\n * are thrown as a part of the WebSocket constructor, not consumers.\n */\n const urlRecord = new URL(\n url,\n typeof location !== 'undefined' ? location.href : undefined\n )\n\n return resolveWebSocketUrl(urlRecord)\n }\n\n if (url.protocol === 'http:') {\n url.protocol = 'ws:'\n } else if (url.protocol === 'https:') {\n url.protocol = 'wss:'\n }\n\n if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {\n /**\n * @note These errors are modeled after the browser errors.\n * The exact error messages aren't provided in the specification.\n * Node.js uses more obscure error messages that I don't wish to replicate.\n */\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`\n )\n }\n\n if (url.hash !== '') {\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`\n )\n }\n\n return url.href\n}\n"],"mappings":";;;;;;;AAMA,SAAgB,oBAAoB,KAA2B;AAC7D,KAAI,OAAO,QAAQ,SAUjB,QAAO,oBALW,IAAI,IACpB,KACA,OAAO,aAAa,cAAc,SAAS,OAAO,OACnD,CAEoC;AAGvC,KAAI,IAAI,aAAa,QACnB,KAAI,WAAW;UACN,IAAI,aAAa,SAC1B,KAAI,WAAW;AAGjB,KAAI,IAAI,aAAa,SAAS,IAAI,aAAa;;;;;;AAM7C,OAAM,IAAI,YACR,sGAAsG,IAAI,SAAS,mBACpH;AAGH,KAAI,IAAI,SAAS,GACf,OAAM,IAAI,YACR,6EAA6E,IAAI,KAAK,6DACvF;AAGH,QAAO,IAAI"}
|
package/lib/node/index.cjs
CHANGED
|
@@ -12,6 +12,29 @@ function getCleanUrl(url, isAbsolute = true) {
|
|
|
12
12
|
return [isAbsolute && url.origin, url.pathname].filter(Boolean).join("");
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/utils/resolveWebSocketUrl.ts
|
|
17
|
+
/**
|
|
18
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
19
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
20
|
+
*
|
|
21
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
22
|
+
*/
|
|
23
|
+
function resolveWebSocketUrl(url) {
|
|
24
|
+
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
|
|
25
|
+
if (url.protocol === "http:") url.protocol = "ws:";
|
|
26
|
+
else if (url.protocol === "https:") url.protocol = "wss:";
|
|
27
|
+
if (url.protocol !== "ws:" && url.protocol !== "wss:")
|
|
28
|
+
/**
|
|
29
|
+
* @note These errors are modeled after the browser errors.
|
|
30
|
+
* The exact error messages aren't provided in the specification.
|
|
31
|
+
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
32
|
+
*/
|
|
33
|
+
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
|
|
34
|
+
if (url.hash !== "") throw new SyntaxError(`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`);
|
|
35
|
+
return url.href;
|
|
36
|
+
}
|
|
37
|
+
|
|
15
38
|
//#endregion
|
|
16
39
|
exports.BatchInterceptor = require_BatchInterceptor.BatchInterceptor;
|
|
17
40
|
exports.FetchResponse = require_fetchUtils.FetchResponse;
|
|
@@ -27,4 +50,5 @@ exports.encodeBuffer = require_bufferUtils.encodeBuffer;
|
|
|
27
50
|
exports.getCleanUrl = getCleanUrl;
|
|
28
51
|
exports.getGlobalSymbol = require_fetchUtils.getGlobalSymbol;
|
|
29
52
|
exports.getRawRequest = require_getRawRequest.getRawRequest;
|
|
53
|
+
exports.resolveWebSocketUrl = resolveWebSocketUrl;
|
|
30
54
|
//# sourceMappingURL=index.cjs.map
|
package/lib/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/utils/getCleanUrl.ts"],"sourcesContent":["/**\n * Removes query parameters and hashes from a given URL.\n */\nexport function getCleanUrl(url: URL, isAbsolute: boolean = true): string {\n return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('')\n}\n"],"mappings":";;;;;;;;;;AAGA,SAAgB,YAAY,KAAU,aAAsB,MAAc;AACxE,QAAO,CAAC,cAAc,IAAI,QAAQ,IAAI,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/utils/getCleanUrl.ts","../../src/utils/resolveWebSocketUrl.ts"],"sourcesContent":["/**\n * Removes query parameters and hashes from a given URL.\n */\nexport function getCleanUrl(url: URL, isAbsolute: boolean = true): string {\n return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('')\n}\n","/**\n * Resolve potentially relative WebSocket URLs the same way\n * the browser does (replace the protocol, use the origin, etc).\n *\n * @see https://websockets.spec.whatwg.org//#dom-websocket-websocket\n */\nexport function resolveWebSocketUrl(url: string | URL): string {\n if (typeof url === 'string') {\n /**\n * @note Cast the string to a URL first so the parsing errors\n * are thrown as a part of the WebSocket constructor, not consumers.\n */\n const urlRecord = new URL(\n url,\n typeof location !== 'undefined' ? location.href : undefined\n )\n\n return resolveWebSocketUrl(urlRecord)\n }\n\n if (url.protocol === 'http:') {\n url.protocol = 'ws:'\n } else if (url.protocol === 'https:') {\n url.protocol = 'wss:'\n }\n\n if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {\n /**\n * @note These errors are modeled after the browser errors.\n * The exact error messages aren't provided in the specification.\n * Node.js uses more obscure error messages that I don't wish to replicate.\n */\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`\n )\n }\n\n if (url.hash !== '') {\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`\n )\n }\n\n return url.href\n}\n"],"mappings":";;;;;;;;;;AAGA,SAAgB,YAAY,KAAU,aAAsB,MAAc;AACxE,QAAO,CAAC,cAAc,IAAI,QAAQ,IAAI,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG;;;;;;;;;;;ACE1E,SAAgB,oBAAoB,KAA2B;AAC7D,KAAI,OAAO,QAAQ,SAUjB,QAAO,oBALW,IAAI,IACpB,KACA,OAAO,aAAa,cAAc,SAAS,OAAO,OACnD,CAEoC;AAGvC,KAAI,IAAI,aAAa,QACnB,KAAI,WAAW;UACN,IAAI,aAAa,SAC1B,KAAI,WAAW;AAGjB,KAAI,IAAI,aAAa,SAAS,IAAI,aAAa;;;;;;AAM7C,OAAM,IAAI,YACR,sGAAsG,IAAI,SAAS,mBACpH;AAGH,KAAI,IAAI,SAAS,GACf,OAAM,IAAI,YACR,6EAA6E,IAAI,KAAK,6DACvF;AAGH,QAAO,IAAI"}
|
package/lib/node/index.d.cts
CHANGED
|
@@ -62,5 +62,14 @@ declare class FetchResponse extends Response {
|
|
|
62
62
|
*/
|
|
63
63
|
declare function getRawRequest(request: Request): unknown | undefined;
|
|
64
64
|
//#endregion
|
|
65
|
-
|
|
65
|
+
//#region src/utils/resolveWebSocketUrl.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
68
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
69
|
+
*
|
|
70
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
71
|
+
*/
|
|
72
|
+
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
73
|
+
//#endregion
|
|
74
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
66
75
|
//# sourceMappingURL=index.d.cts.map
|
package/lib/node/index.d.mts
CHANGED
|
@@ -62,5 +62,14 @@ declare class FetchResponse extends Response {
|
|
|
62
62
|
*/
|
|
63
63
|
declare function getRawRequest(request: Request): unknown | undefined;
|
|
64
64
|
//#endregion
|
|
65
|
-
|
|
65
|
+
//#region src/utils/resolveWebSocketUrl.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
68
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
69
|
+
*
|
|
70
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
71
|
+
*/
|
|
72
|
+
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
73
|
+
//#endregion
|
|
74
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
66
75
|
//# sourceMappingURL=index.d.mts.map
|
package/lib/node/index.mjs
CHANGED
|
@@ -13,5 +13,28 @@ function getCleanUrl(url, isAbsolute = true) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
//#endregion
|
|
16
|
-
|
|
16
|
+
//#region src/utils/resolveWebSocketUrl.ts
|
|
17
|
+
/**
|
|
18
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
19
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
20
|
+
*
|
|
21
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
22
|
+
*/
|
|
23
|
+
function resolveWebSocketUrl(url) {
|
|
24
|
+
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
|
|
25
|
+
if (url.protocol === "http:") url.protocol = "ws:";
|
|
26
|
+
else if (url.protocol === "https:") url.protocol = "wss:";
|
|
27
|
+
if (url.protocol !== "ws:" && url.protocol !== "wss:")
|
|
28
|
+
/**
|
|
29
|
+
* @note These errors are modeled after the browser errors.
|
|
30
|
+
* The exact error messages aren't provided in the specification.
|
|
31
|
+
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
32
|
+
*/
|
|
33
|
+
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
|
|
34
|
+
if (url.hash !== "") throw new SyntaxError(`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`);
|
|
35
|
+
return url.href;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { BatchInterceptor, FetchResponse, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorReadyState, RequestController, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
17
40
|
//# sourceMappingURL=index.mjs.map
|
package/lib/node/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/utils/getCleanUrl.ts"],"sourcesContent":["/**\n * Removes query parameters and hashes from a given URL.\n */\nexport function getCleanUrl(url: URL, isAbsolute: boolean = true): string {\n return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('')\n}\n"],"mappings":";;;;;;;;;;AAGA,SAAgB,YAAY,KAAU,aAAsB,MAAc;AACxE,QAAO,CAAC,cAAc,IAAI,QAAQ,IAAI,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/utils/getCleanUrl.ts","../../src/utils/resolveWebSocketUrl.ts"],"sourcesContent":["/**\n * Removes query parameters and hashes from a given URL.\n */\nexport function getCleanUrl(url: URL, isAbsolute: boolean = true): string {\n return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('')\n}\n","/**\n * Resolve potentially relative WebSocket URLs the same way\n * the browser does (replace the protocol, use the origin, etc).\n *\n * @see https://websockets.spec.whatwg.org//#dom-websocket-websocket\n */\nexport function resolveWebSocketUrl(url: string | URL): string {\n if (typeof url === 'string') {\n /**\n * @note Cast the string to a URL first so the parsing errors\n * are thrown as a part of the WebSocket constructor, not consumers.\n */\n const urlRecord = new URL(\n url,\n typeof location !== 'undefined' ? location.href : undefined\n )\n\n return resolveWebSocketUrl(urlRecord)\n }\n\n if (url.protocol === 'http:') {\n url.protocol = 'ws:'\n } else if (url.protocol === 'https:') {\n url.protocol = 'wss:'\n }\n\n if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {\n /**\n * @note These errors are modeled after the browser errors.\n * The exact error messages aren't provided in the specification.\n * Node.js uses more obscure error messages that I don't wish to replicate.\n */\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`\n )\n }\n\n if (url.hash !== '') {\n throw new SyntaxError(\n `Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`\n )\n }\n\n return url.href\n}\n"],"mappings":";;;;;;;;;;AAGA,SAAgB,YAAY,KAAU,aAAsB,MAAc;AACxE,QAAO,CAAC,cAAc,IAAI,QAAQ,IAAI,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG;;;;;;;;;;;ACE1E,SAAgB,oBAAoB,KAA2B;AAC7D,KAAI,OAAO,QAAQ,SAUjB,QAAO,oBALW,IAAI,IACpB,KACA,OAAO,aAAa,cAAc,SAAS,OAAO,OACnD,CAEoC;AAGvC,KAAI,IAAI,aAAa,QACnB,KAAI,WAAW;UACN,IAAI,aAAa,SAC1B,KAAI,WAAW;AAGjB,KAAI,IAAI,aAAa,SAAS,IAAI,aAAa;;;;;;AAM7C,OAAM,IAAI,YACR,sGAAsG,IAAI,SAAS,mBACpH;AAGH,KAAI,IAAI,SAAS,GACf,OAAM,IAAI,YACR,6EAA6E,IAAI,KAAK,6DACvF;AAGH,QAAO,IAAI"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"type": "commonjs",
|
|
3
3
|
"name": "@mswjs/interceptors",
|
|
4
4
|
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
|
|
5
|
-
"version": "0.41.
|
|
5
|
+
"version": "0.41.2",
|
|
6
6
|
"main": "./lib/node/index.cjs",
|
|
7
7
|
"module": "./lib/node/index.mjs",
|
|
8
8
|
"types": "./lib/node/index.d.cts",
|
package/src/index.ts
CHANGED
|
@@ -12,3 +12,4 @@ export { getCleanUrl } from './utils/getCleanUrl'
|
|
|
12
12
|
export { encodeBuffer, decodeBuffer } from './utils/bufferUtils'
|
|
13
13
|
export { FetchResponse } from './utils/fetchUtils'
|
|
14
14
|
export { getRawRequest } from './getRawRequest'
|
|
15
|
+
export { resolveWebSocketUrl } from './utils/resolveWebSocketUrl'
|
|
@@ -19,7 +19,7 @@ export abstract class WebSocketClientConnectionProtocol {
|
|
|
19
19
|
public abstract close(code?: number, reason?: string): void
|
|
20
20
|
|
|
21
21
|
public abstract addEventListener<
|
|
22
|
-
EventType extends keyof WebSocketClientEventMap
|
|
22
|
+
EventType extends keyof WebSocketClientEventMap,
|
|
23
23
|
>(
|
|
24
24
|
type: EventType,
|
|
25
25
|
listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>,
|
|
@@ -27,7 +27,7 @@ export abstract class WebSocketClientConnectionProtocol {
|
|
|
27
27
|
): void
|
|
28
28
|
|
|
29
29
|
public abstract removeEventListener<
|
|
30
|
-
EventType extends keyof WebSocketClientEventMap
|
|
30
|
+
EventType extends keyof WebSocketClientEventMap,
|
|
31
31
|
>(
|
|
32
32
|
event: EventType,
|
|
33
33
|
listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>,
|
|
@@ -40,9 +40,7 @@ export abstract class WebSocketClientConnectionProtocol {
|
|
|
40
40
|
* client connection. The user can control the connection,
|
|
41
41
|
* send and receive events.
|
|
42
42
|
*/
|
|
43
|
-
export class WebSocketClientConnection
|
|
44
|
-
implements WebSocketClientConnectionProtocol
|
|
45
|
-
{
|
|
43
|
+
export class WebSocketClientConnection implements WebSocketClientConnectionProtocol {
|
|
46
44
|
public readonly id: string
|
|
47
45
|
public readonly url: URL
|
|
48
46
|
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { invariant } from 'outvariant'
|
|
2
|
+
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
2
3
|
import type { WebSocketData } from './WebSocketTransport'
|
|
3
4
|
import { bindEvent } from './utils/bindEvent'
|
|
4
5
|
import { CloseEvent } from './utils/events'
|
|
5
|
-
import {
|
|
6
|
+
import { resolveWebSocketUrl } from '../../utils/resolveWebSocketUrl'
|
|
6
7
|
|
|
7
8
|
export type WebSocketEventListener<
|
|
8
|
-
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event
|
|
9
|
+
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event,
|
|
9
10
|
> = (this: WebSocket, event: EventType) => void
|
|
10
11
|
|
|
11
12
|
const WEBSOCKET_CLOSE_CODE_RANGE_ERROR =
|
|
@@ -44,7 +45,7 @@ export class WebSocketOverride extends EventTarget implements WebSocket {
|
|
|
44
45
|
|
|
45
46
|
constructor(url: string | URL, protocols?: string | Array<string>) {
|
|
46
47
|
super()
|
|
47
|
-
this.url = url
|
|
48
|
+
this.url = resolveWebSocketUrl(url)
|
|
48
49
|
this.protocol = ''
|
|
49
50
|
this.extensions = ''
|
|
50
51
|
this.binaryType = 'blob'
|
|
@@ -62,8 +63,8 @@ export class WebSocketOverride extends EventTarget implements WebSocket {
|
|
|
62
63
|
typeof protocols === 'string'
|
|
63
64
|
? protocols
|
|
64
65
|
: Array.isArray(protocols) && protocols.length > 0
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
? protocols[0]
|
|
67
|
+
: ''
|
|
67
68
|
|
|
68
69
|
/**
|
|
69
70
|
* @note Check that nothing has prevented this connection
|
|
@@ -30,7 +30,7 @@ export abstract class WebSocketServerConnectionProtocol {
|
|
|
30
30
|
public abstract close(): void
|
|
31
31
|
|
|
32
32
|
public abstract addEventListener<
|
|
33
|
-
EventType extends keyof WebSocketServerEventMap
|
|
33
|
+
EventType extends keyof WebSocketServerEventMap,
|
|
34
34
|
>(
|
|
35
35
|
event: EventType,
|
|
36
36
|
listener: WebSocketEventListener<WebSocketServerEventMap[EventType]>,
|
|
@@ -38,7 +38,7 @@ export abstract class WebSocketServerConnectionProtocol {
|
|
|
38
38
|
): void
|
|
39
39
|
|
|
40
40
|
public abstract removeEventListener<
|
|
41
|
-
EventType extends keyof WebSocketServerEventMap
|
|
41
|
+
EventType extends keyof WebSocketServerEventMap,
|
|
42
42
|
>(
|
|
43
43
|
event: EventType,
|
|
44
44
|
listener: WebSocketEventListener<WebSocketServerEventMap[EventType]>,
|
|
@@ -51,9 +51,7 @@ export abstract class WebSocketServerConnectionProtocol {
|
|
|
51
51
|
* WebSocket server connection. It's idle by default but you can
|
|
52
52
|
* establish it by calling `server.connect()`.
|
|
53
53
|
*/
|
|
54
|
-
export class WebSocketServerConnection
|
|
55
|
-
implements WebSocketServerConnectionProtocol
|
|
56
|
-
{
|
|
54
|
+
export class WebSocketServerConnection implements WebSocketServerConnectionProtocol {
|
|
57
55
|
/**
|
|
58
56
|
* A WebSocket instance connected to the original server.
|
|
59
57
|
*/
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve potentially relative WebSocket URLs the same way
|
|
3
|
+
* the browser does (replace the protocol, use the origin, etc).
|
|
4
|
+
*
|
|
5
|
+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
6
|
+
*/
|
|
7
|
+
export function resolveWebSocketUrl(url: string | URL): string {
|
|
8
|
+
if (typeof url === 'string') {
|
|
9
|
+
/**
|
|
10
|
+
* @note Cast the string to a URL first so the parsing errors
|
|
11
|
+
* are thrown as a part of the WebSocket constructor, not consumers.
|
|
12
|
+
*/
|
|
13
|
+
const urlRecord = new URL(
|
|
14
|
+
url,
|
|
15
|
+
typeof location !== 'undefined' ? location.href : undefined
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
return resolveWebSocketUrl(urlRecord)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (url.protocol === 'http:') {
|
|
22
|
+
url.protocol = 'ws:'
|
|
23
|
+
} else if (url.protocol === 'https:') {
|
|
24
|
+
url.protocol = 'wss:'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {
|
|
28
|
+
/**
|
|
29
|
+
* @note These errors are modeled after the browser errors.
|
|
30
|
+
* The exact error messages aren't provided in the specification.
|
|
31
|
+
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
32
|
+
*/
|
|
33
|
+
throw new SyntaxError(
|
|
34
|
+
`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (url.hash !== '') {
|
|
39
|
+
throw new SyntaxError(
|
|
40
|
+
`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return url.href
|
|
45
|
+
}
|