@hyperfrontend/network-protocol/browser/channel

channel

Browser-side channel factories pre-wired with the browser sender and receiver implementations.

Overview

A channel is a named, bidirectional, queue-backed conduit between two endpoints. This entry point composes the runtime-agnostic channel logic from lib/channel with the browser-specific sender (/browser/sender) and receiver (/browser/receiver), so the resulting createChannel factory only needs a label, send/receive transport callbacks, and a ProtocolProvider.

Usage

import { createChannel, createChannelStore } from '@hyperfrontend/network-protocol/browser/channel'
import { createProtocol } from '@hyperfrontend/network-protocol/browser/v1'
import { createLogger } from '@hyperfrontend/logging'

const protocolProvider = createProtocol(createLogger({ level: 'info' }), 60)

const channel = createChannel(
  'app-to-widget',
  (packet) => otherWindow.postMessage(packet, '*'),
  (handler) => window.addEventListener('message', (event) => handler(event.data)),
  protocolProvider
)

const store = createChannelStore()
store.add(channel)

Notes

  • createChannelStore produces a registry for managing multiple channels by label or UUID, with stop/resume controls applied across all entries.
  • Validation helpers (isValidChannel, isValidLabel, isValidReceiver, isValidSender, getFirstInvalidProtocolProperty) are re-exported for upstream guards.
  • Pair this entry point with /browser/v1 or /browser/v2 for end-to-end browser communication; the Node.js counterpart lives at /node/channel.

API Reference

ƒ Functions

§function

getFirstInvalidProtocolProperty(protocol: unknown): "" | unknown

Identifies the first invalid property in a protocol object.

Parameters

NameTypeDescription
§protocol
unknown
The protocol object to validate

Returns

"" | unknown
The name of the first invalid protocol property, or an empty string if all properties are valid

Example

Detecting invalid protocol properties

getFirstInvalidProtocolProperty({ send: () => {}, receive: null })
// => 'receive'

getFirstInvalidProtocolProperty(validProtocol)
// => ''
§function

isValidChannel(channel: unknown): boolean

Validates whether a channel meets the required structure with send/receive methods and inbound/outbound queues.

Parameters

NameTypeDescription
§channel
unknown
The channel object to validate

Returns

boolean
True if the channel is valid, false otherwise

Example

Validating channel structure

isValidChannel(channel) // => true
isValidChannel({}) // => false
§function

isValidLabel(label: string): boolean

Validates whether a label meets the required criteria of being a non-empty string.

Parameters

NameTypeDescription
§label
string
The label string to validate

Returns

boolean
True if the label is valid, false otherwise

Example

Validating channel labels

isValidLabel('channel-1') // => true
isValidLabel('') // => false
§function

isValidReceiver(receiver: unknown): boolean

Validates whether a receiver meets the required criteria of being a function.

Parameters

NameTypeDescription
§receiver
unknown
The receiver to validate

Returns

boolean
True if the receiver is valid, false otherwise

Example

Validating receiver functions

isValidReceiver((packet) => handlePacket(packet)) // => true
isValidReceiver('not-a-function') // => false
§function

isValidSender(sender: unknown): boolean

Validates whether a sender meets the required criteria of being a function.

Parameters

NameTypeDescription
§sender
unknown
The sender to validate

Returns

boolean
True if the sender is valid, false otherwise

Example

Validating sender functions

isValidSender((packet) => transmit(packet)) // => true
isValidSender(null) // => false

Interfaces

§interface

Channel

Secure communication channel with inbound and outbound queues

Properties

§readonly inbound:InboundQueues & StopResumeControl
Inbound message queues
§readonly label:string
Channel label for identification
§readonly outbound:OutboundQueues & StopResumeControl
Outbound message queues
§readonly receive:ReceiveFn
Receives messages from the channel
§resume:() => void
Resumes the operation
§readonly send:SendFn<T>
Sends a message through the channel
§stop:() => void
Stops the operation
§interface

ChannelEntry

Entry in a channel store with metadata

Properties

§readonly channel:Channel<T>
The channel instance
§readonly id:string
Unique channel identifier
§readonly name:string
Channel name
§interface

ChannelStore

Store for managing multiple channels

Properties

§readonly add:(topic: Channel<T>[]) => void
Adds channels to the store
§readonly clear:() => void
Removes all channels
§readonly create:(label: string, send: SendPacketFn, receive: ReceivePacketFn, protocol: ProtocolProvider<T>) => Channel<T>
Creates and returns a new channel
§readonly existsById:(id: string) => boolean
Checks if a channel exists by ID
§readonly existsByName:(name: string) => boolean
Checks if a channel exists by name
§readonly getById:(id: string) => Channel<T>
Gets a channel by ID
§readonly getByName:(name: string) => Channel<T>
Gets a channel by name
§readonly list:unknown
List of all channel entries
§readonly removeById:(id: string[]) => void
Removes channels by ID
§readonly removeByName:(name: string[]) => void
Removes channels by name
§interface

Protocol

Protocol instance with encryption, decryption, and message transport

Properties

§getLogger:() => Logger
Returns the logger instance
§packetDecryption:PacketDecryption<T>
Decrypts received packets
§packetDeobfuscation:PacketDeobfuscation
Deobfuscates received packets
§packetEncryption:PacketEncryption<T>
Encrypts packets before sending
§packetObfuscation:PacketObfuscation
Obfuscates packets for transmission
§receive:ReceivePacketFn<T>
Receives packets
§send:SendPacketFn
Sends a packet
§interface

StopResumeControl

Interface for stopping and resuming operations

Properties

§resume:() => void
Resumes the operation
§stop:() => void
Stops the operation

Types

§type

ChannelCreater

Factory function for creating channels
type ChannelCreater = (label: string, send: SendPacketFn, receive: ReceivePacketFn, protocol: ProtocolProvider<T>) => Channel<T>
§type

ProtocolProvider

Factory function that creates a protocol from send/receive functions
type ProtocolProvider = (send: SendPacketFn, receive: ReceivePacketFn<T>) => Protocol<T>

Variables

§type

createChannel

§type

createChannelStore