Files
bare-operating-system/packages/bare-os-protocol/lib/meshdrop-channel.js
T

29 lines
935 B
JavaScript

import { PROTOCOL_MESHDROP_CHANNEL_NAME } from '../constants.js'
import { msgMeshdropEnvelopeEncoding } from './meshdrop-messages.js'
/**
* Dedicated Protomux channel for meshdrop control/chunk frames.
*
* @param {import('protomux').Protomux} mux
* @param {{
* onEnvelope?: (m: Record<string, unknown>, ch: import('protomux').Channel) => void
* onChannelOpened?: (ch: import('protomux').Channel, mux: import('protomux').Protomux) => void
* }} [handlers]
*/
export function setupBareOsMeshdropChannel(mux, handlers = {}) {
const chan = mux.createChannel({
protocol: PROTOCOL_MESHDROP_CHANNEL_NAME,
unique: true
})
if (!chan) return
chan.addMessage({
encoding: msgMeshdropEnvelopeEncoding,
onmessage: (m, ch) =>
handlers.onEnvelope ? handlers.onEnvelope(m, ch) : undefined
})
chan.open()
if (typeof handlers.onChannelOpened === 'function') {
handlers.onChannelOpened(chan, mux)
}
}