@hyperfrontend/immutable-api-utils/built-in-copy/websocket

websocket

Locked, prototype-pollution-resistant copy of the global WebSocket constructor.

A factory wrapping WebSocket is captured at module-load time and frozen into a tamper-proof namespace, so socket construction stays trustworthy even if the global is later patched. Effective only when imported before any untrusted code has had a chance to mutate the prototype chain.

API Reference

ƒ Functions

§function

createWebSocket(url: string | URL, protocols?: string | string[]): WebSocket

(Safe copy) Creates a new WebSocket using the captured WebSocket constructor. Use this instead of new WebSocket().

Parameters

NameTypeDescription
§url
string | URL
The URL to connect to.
§protocols?
string | string[]
Optional sub-protocol string or array of strings.

Returns

WebSocket
A new WebSocket instance.

Example

Creating WebSocket connection

const socket = createWebSocket('wss://api.example.com/stream')
socket.onmessage = (event) => console.log(event.data)

Variables

§type

CLOSED

§type

CLOSING

§type

CONNECTING

WebSocket ready state constants (safe copies).
§type

OPEN

§type

WebSocket

(Safe copy) Namespace object containing all WebSocket utilities. Note: Importing this imports all methods in this namespace (no tree-shaking).