@hyperfrontend/immutable-api-utils/built-in-copy/messagingmessaging
Locked, prototype-pollution-resistant copies of the global cross-context messaging APIs.
structuredClone, MessageChannel, and BroadcastChannel constructors are wrapped in factories, and safe postMessage* helpers for Window, Worker, MessagePort, and BroadcastChannel targets are captured at module-load time and frozen into a tamper-proof namespace, so cross-context communication stays trustworthy even if the globals are later patched. Effective only when imported before any untrusted code has had a chance to mutate the prototype chain.
API Reference
ƒ Functions
(Safe copy) Posts a message to a BroadcastChannel.
Parameters
Example
Broadcasting to channel
const channel = new BroadcastChannel('app-updates')
postMessageToBroadcast(channel, { type: 'cache-invalidate' })(Safe copy) Posts a message to a MessagePort.
Parameters
Example
Posting message to MessagePort
const channel = new MessageChannel()
postMessageToPort(channel.port1, { status: 'ready' })§function
postMessageToWindow(target: Window, message: unknown, options: PostMessageToWindowOptions): void
(Safe copy) Posts a message to a Window object using captured postMessage. This is a helper that wraps Window.postMessage for safe messaging.
Parameters
Example
Posting message to Window
const iframe = document.querySelector('iframe')
postMessageToWindow(iframe.contentWindow, { action: 'refresh' }, {
targetOrigin: 'https://trusted.example.com',
})(Safe copy) Posts a message to a Worker using captured postMessage.
Parameters
Example
Posting message to Worker
const worker = new Worker('worker.js')
postMessageToWorker(worker, { task: 'process', data: [1, 2, 3] })◈ Interfaces
● Variables
(Safe copy) Creates a new BroadcastChannel using the captured constructor. BroadcastChannel enables communication between browsing contexts with the same origin.
(Safe copy) Creates a new MessageChannel using the captured constructor. MessageChannel provides two ports for bidirectional communication.
(Safe copy) Namespace object containing all Messaging functions. Note: Importing this imports all methods in this namespace (no tree-shaking).
(Safe copy) Creates a deep clone of a value using the structured clone algorithm. Available in modern browsers and Node.js 17+.