@hyperfrontend/network-protocol/node/channelchannel
Node-side channel factories pre-wired with the Node.js sender and receiver implementations.
Overview
A channel is a named, bidirectional, queue-backed conduit that binds one protocol instance to one negotiated session. This entry point composes the runtime-agnostic channel logic from lib/channel with the Node sender (/node/sender) and receiver (/node/receiver), so createChannel needs a label and an options object: the transport callbacks, a ProtocolProvider from /node/v3 or /node/v4, the ProtocolSession, and an optional onDrop handler. The channel exposes the protocol's hello exchange (hello, isHello, acceptHello) so the session can be keyed over the same transport.
Usage
import { createChannel, createChannelStore } from '@hyperfrontend/network-protocol/node/channel'
import { createProtocol } from '@hyperfrontend/network-protocol/node/v3'
import { createLogger } from '@hyperfrontend/logging'
const channel = createChannel('parent-to-worker', {
send: (frame) => process.send?.(frame),
receive: (packet) => handle(packet.data.message),
protocolProvider: createProtocol(createLogger({ level: 'info' })),
session: { protocol: 'v3', role: 'initiator', localId, peerId },
onDrop: (drop) => report(drop),
})
process.send?.(await channel.hello())
process.on('message', (data) => (channel.isHello(data) ? channel.acceptHello(data) : channel.receive(data)))
const store = createChannelStore()
store.add(channel)
Notes
createChannelStoreproduces a registry for managing multiple channels by label or UUID;store.create(label, options)creates and registers in one step.channel.outboundandchannel.inboundeach exposequeue.size,stop, andresume;channel.stop()andchannel.resume()act on both.- Validation helpers (
isValidChannel,isValidLabel,isValidReceiver,isValidSender,isValidSession,getFirstInvalidProtocolProperty) are re-exported for upstream guards. - Works over any Node transport that carries
Uint8Arrayframes (IPC, sockets,process.send, message ports); the browser counterpart lives at/browser/channel.
API Reference§
ƒ Functions
Parameters
| Name | Type | Description |
|---|---|---|
§protocol | unknown | The protocol object to inspect |
Returns
stringExample
Reporting a protocol missing its opener
getFirstInvalidProtocolProperty({ seal, send, receive, getLogger })
// => 'open'Parameters
| Name | Type | Description |
|---|---|---|
§channel | unknown | The value to check |
Returns
booleanChannelExample
Guarding a channel before storing it
isValidChannel(createChannel('comms', options))
// => trueParameters
| Name | Type | Description |
|---|---|---|
§label | string | The label string to validate |
Returns
booleanExample
Validating channel labels
isValidLabel('channel-1') // => true
isValidLabel('') // => falseParameters
| Name | Type | Description |
|---|---|---|
§receiver | unknown | The receiver to validate |
Returns
booleanExample
Validating receiver functions
isValidReceiver((packet) => handlePacket(packet)) // => true
isValidReceiver('not-a-function') // => falseParameters
| Name | Type | Description |
|---|---|---|
§sender | unknown | The sender to validate |
Returns
booleanExample
Validating sender functions
isValidSender((packet) => transmit(packet)) // => true
isValidSender(null) // => falseParameters
| Name | Type | Description |
|---|---|---|
§session | unknown | The value to check |
Returns
unknownProtocolSessionExample
Guarding a session before binding a protocol to it
isValidSession({ protocol: 'v3', role: 'initiator', localId, peerId })
// => true◈ Interfaces
Properties
Properties
Properties
readonly create:(label: string, options: ChannelOptions<T>) => Channel<T>Properties
Properties
Properties
Properties
readonly direction:"inbound" | "outbound"outbound) or arriving (inbound)Properties
Properties
◆ Types
type ChannelCreater = (label: string, options: ChannelOptions<T>) => Channel<T>accepted: the peer's material is now known and the session can be keyedduplicate: the same material was already accepted; a retry, ignoredrejected: the frame is not a hello this session can use
type HelloOutcome = "accepted" | "duplicate" | "rejected"type PacketDropHandler = (drop: PacketDrop) => voidtype PacketDropStage = "seal" | "open"type ProtocolProvider = (send: SendPacketFn, receive: ReceivePacketFn<T>, session: ProtocolSession) => Protocol<T>type SessionRole = "initiator" | "responder"