@mswjs/interceptors 0.41.1 → 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 -22
- 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 +2 -21
- 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/WebSocketOverride.ts +1 -46
- package/src/utils/resolveWebSocketUrl.ts +45 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
require('../getRawRequest-zx8rUJL2.cjs');
|
|
2
2
|
require('../createRequestId-Cs4oXfa1.cjs');
|
|
3
3
|
require('../bufferUtils-Uc0eRItL.cjs');
|
|
4
|
-
require('../handleRequest-
|
|
5
|
-
const require_fetch = require('../fetch-
|
|
6
|
-
const require_XMLHttpRequest = require('../XMLHttpRequest-
|
|
4
|
+
require('../handleRequest-CvX2G-Lz.cjs');
|
|
5
|
+
const require_fetch = require('../fetch-U3v3Y4ap.cjs');
|
|
6
|
+
const require_XMLHttpRequest = require('../XMLHttpRequest-BACqefB-.cjs');
|
|
7
7
|
|
|
8
8
|
//#region src/presets/browser.ts
|
|
9
9
|
/**
|
|
@@ -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'
|
|
@@ -3,6 +3,7 @@ import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
|
3
3
|
import type { WebSocketData } from './WebSocketTransport'
|
|
4
4
|
import { bindEvent } from './utils/bindEvent'
|
|
5
5
|
import { CloseEvent } from './utils/events'
|
|
6
|
+
import { resolveWebSocketUrl } from '../../utils/resolveWebSocketUrl'
|
|
6
7
|
|
|
7
8
|
export type WebSocketEventListener<
|
|
8
9
|
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event,
|
|
@@ -249,49 +250,3 @@ function getDataSize(data: WebSocketData): number {
|
|
|
249
250
|
|
|
250
251
|
return data.byteLength
|
|
251
252
|
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Resolve potentially relative WebSocket URLs the same way
|
|
255
|
-
* the browser does (replace the protocol, use the origin, etc).
|
|
256
|
-
*
|
|
257
|
-
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
|
|
258
|
-
*/
|
|
259
|
-
function resolveWebSocketUrl(url: string | URL): string {
|
|
260
|
-
if (typeof url === 'string') {
|
|
261
|
-
/**
|
|
262
|
-
* @note Cast the string to a URL first so the parsing errors
|
|
263
|
-
* are thrown as a part of the WebSocket constructor, not consumers.
|
|
264
|
-
*/
|
|
265
|
-
const urlRecord = new URL(
|
|
266
|
-
url,
|
|
267
|
-
typeof location !== 'undefined' ? location.href : undefined
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
return resolveWebSocketUrl(urlRecord)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (url.protocol === 'http:') {
|
|
274
|
-
url.protocol = 'ws:'
|
|
275
|
-
} else if (url.protocol === 'https:') {
|
|
276
|
-
url.protocol = 'wss:'
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {
|
|
280
|
-
/**
|
|
281
|
-
* @note These errors are modeled after the browser errors.
|
|
282
|
-
* The exact error messages aren't provided in the specification.
|
|
283
|
-
* Node.js uses more obscure error messages that I don't wish to replicate.
|
|
284
|
-
*/
|
|
285
|
-
throw new SyntaxError(
|
|
286
|
-
`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`
|
|
287
|
-
)
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (url.hash !== '') {
|
|
291
|
-
throw new SyntaxError(
|
|
292
|
-
`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`
|
|
293
|
-
)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return url.href
|
|
297
|
-
}
|
|
@@ -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
|
+
}
|