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

messaging

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

§function

postMessageToBroadcast(channel: BroadcastChannel, message: unknown): void

(Safe copy) Posts a message to a BroadcastChannel.

Parameters

NameTypeDescription
§channel
BroadcastChannel
Named broadcast channel for same-origin contexts.
§message
unknown
The data payload to send, must be structured-cloneable.

Example

Broadcasting to channel

const channel = new BroadcastChannel('app-updates')
postMessageToBroadcast(channel, { type: 'cache-invalidate' })
§function

postMessageToPort(port: MessagePort, message: unknown, transfer?: Transferable[]): void

(Safe copy) Posts a message to a MessagePort.

Parameters

NameTypeDescription
§port
MessagePort
One end of a two-way communication channel.
§message
unknown
The data payload to send, must be structured-cloneable.
§transfer?
Transferable[]
Optional array of Transferable objects.

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

NameTypeDescription
§target
Window
Destination window, iframe, or popup context.
§message
unknown
The data payload to send, must be structured-cloneable.
§options
PostMessageToWindowOptions
Target origin and optional transferables.

Example

Posting message to Window

const iframe = document.querySelector('iframe')
postMessageToWindow(iframe.contentWindow, { action: 'refresh' }, {
  targetOrigin: 'https://trusted.example.com',
})
§function

postMessageToWorker(target: Worker, message: unknown, transfer?: Transferable[]): void

(Safe copy) Posts a message to a Worker using captured postMessage.

Parameters

NameTypeDescription
§target
Worker
Web Worker or service worker instance.
§message
unknown
The data payload to send, must be structured-cloneable.
§transfer?
Transferable[]
Optional array of Transferable objects.

Example

Posting message to Worker

const worker = new Worker('worker.js')
postMessageToWorker(worker, { task: 'process', data: [1, 2, 3] })

Interfaces

§interface

PostMessageToWindowOptions

PostMessage options for Window.postMessage.

Properties

§targetOrigin:string
The target origin for the message (e.g., "https://example.com" or "*").
§transfer?:Transferable[]
Array of Transferable objects to transfer ownership.

Variables

§type

createBroadcastChannel

(Safe copy) Creates a new BroadcastChannel using the captured constructor. BroadcastChannel enables communication between browsing contexts with the same origin.
§type

createMessageChannel

(Safe copy) Creates a new MessageChannel using the captured constructor. MessageChannel provides two ports for bidirectional communication.
§type

Messaging

(Safe copy) Namespace object containing all Messaging functions. Note: Importing this imports all methods in this namespace (no tree-shaking).
§type

structuredClone

(Safe copy) Creates a deep clone of a value using the structured clone algorithm. Available in modern browsers and Node.js 17+.