You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@mswjs/interceptors

Package Overview
Dependencies
Maintainers
1
Versions
187
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mswjs/interceptors - npm Package Compare versions

Comparing version
0.41.0
to
0.41.1
+21
-1
lib/browser/interceptors/WebSocket/index.cjs

@@ -190,3 +190,3 @@ const require_createRequestId = require('../../createRequestId-Cs4oXfa1.cjs');

this._onclose = null;
this.url = url.toString();
this.url = resolveWebSocketUrl(url);
this.protocol = "";

@@ -302,2 +302,22 @@ this.extensions = "";

}
/**
* Resolve potentially relative WebSocket URLs the same way
* the browser does (replace the protocol, use the origin, etc).
*
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
*/
function resolveWebSocketUrl(url) {
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
if (url.protocol === "http:") url.protocol = "ws:";
else if (url.protocol === "https:") url.protocol = "wss:";
if (url.protocol !== "ws:" && url.protocol !== "wss:")
/**
* @note These errors are modeled after the browser errors.
* The exact error messages aren't provided in the specification.
* Node.js uses more obscure error messages that I don't wish to replicate.
*/
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
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.`);
return url.href;
}

@@ -304,0 +324,0 @@ //#endregion

@@ -190,3 +190,3 @@ import { r as Interceptor, t as createRequestId } from "../../createRequestId-DQcIlohW.mjs";

this._onclose = null;
this.url = url.toString();
this.url = resolveWebSocketUrl(url);
this.protocol = "";

@@ -302,2 +302,22 @@ this.extensions = "";

}
/**
* Resolve potentially relative WebSocket URLs the same way
* the browser does (replace the protocol, use the origin, etc).
*
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
*/
function resolveWebSocketUrl(url) {
if (typeof url === "string") return resolveWebSocketUrl(new URL(url, typeof location !== "undefined" ? location.href : void 0));
if (url.protocol === "http:") url.protocol = "ws:";
else if (url.protocol === "https:") url.protocol = "wss:";
if (url.protocol !== "ws:" && url.protocol !== "wss:")
/**
* @note These errors are modeled after the browser errors.
* The exact error messages aren't provided in the specification.
* Node.js uses more obscure error messages that I don't wish to replicate.
*/
throw new SyntaxError(`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`);
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.`);
return url.href;
}

@@ -304,0 +324,0 @@ //#endregion

+1
-1

@@ -5,3 +5,3 @@ {

"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
"version": "0.41.0",
"version": "0.41.1",
"main": "./lib/node/index.cjs",

@@ -8,0 +8,0 @@ "module": "./lib/node/index.mjs",

@@ -22,3 +22,3 @@ import type { WebSocketData, WebSocketTransport } from './WebSocketTransport'

public abstract addEventListener<
EventType extends keyof WebSocketClientEventMap
EventType extends keyof WebSocketClientEventMap,
>(

@@ -31,3 +31,3 @@ type: EventType,

public abstract removeEventListener<
EventType extends keyof WebSocketClientEventMap
EventType extends keyof WebSocketClientEventMap,
>(

@@ -45,5 +45,3 @@ event: EventType,

*/
export class WebSocketClientConnection
implements WebSocketClientConnectionProtocol
{
export class WebSocketClientConnection implements WebSocketClientConnectionProtocol {
public readonly id: string

@@ -50,0 +48,0 @@ public readonly url: URL

import { invariant } from 'outvariant'
import { DeferredPromise } from '@open-draft/deferred-promise'
import type { WebSocketData } from './WebSocketTransport'
import { bindEvent } from './utils/bindEvent'
import { CloseEvent } from './utils/events'
import { DeferredPromise } from '@open-draft/deferred-promise'
export type WebSocketEventListener<
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event,
> = (this: WebSocket, event: EventType) => void

@@ -47,3 +47,3 @@

super()
this.url = url.toString()
this.url = resolveWebSocketUrl(url)
this.protocol = ''

@@ -66,4 +66,4 @@ this.extensions = ''

: Array.isArray(protocols) && protocols.length > 0
? protocols[0]
: ''
? protocols[0]
: ''

@@ -254,1 +254,47 @@ /**

}
/**
* Resolve potentially relative WebSocket URLs the same way
* the browser does (replace the protocol, use the origin, etc).
*
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
*/
function resolveWebSocketUrl(url: string | URL): string {
if (typeof url === 'string') {
/**
* @note Cast the string to a URL first so the parsing errors
* are thrown as a part of the WebSocket constructor, not consumers.
*/
const urlRecord = new URL(
url,
typeof location !== 'undefined' ? location.href : undefined
)
return resolveWebSocketUrl(urlRecord)
}
if (url.protocol === 'http:') {
url.protocol = 'ws:'
} else if (url.protocol === 'https:') {
url.protocol = 'wss:'
}
if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {
/**
* @note These errors are modeled after the browser errors.
* The exact error messages aren't provided in the specification.
* Node.js uses more obscure error messages that I don't wish to replicate.
*/
throw new SyntaxError(
`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`
)
}
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.`
)
}
return url.href
}

@@ -33,3 +33,3 @@ import { invariant } from 'outvariant'

public abstract addEventListener<
EventType extends keyof WebSocketServerEventMap
EventType extends keyof WebSocketServerEventMap,
>(

@@ -42,3 +42,3 @@ event: EventType,

public abstract removeEventListener<
EventType extends keyof WebSocketServerEventMap
EventType extends keyof WebSocketServerEventMap,
>(

@@ -56,5 +56,3 @@ event: EventType,

*/
export class WebSocketServerConnection
implements WebSocketServerConnectionProtocol
{
export class WebSocketServerConnection implements WebSocketServerConnectionProtocol {
/**

@@ -61,0 +59,0 @@ * A WebSocket instance connected to the original server.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet