Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

981 B
Raw Blame
import Protomux from 'protomux'
import c from 'compact-encoding'
import { allRelaysBusy, markRelayBusy } from './config'

const PROTOCOL = 'altersend-relay-status'

function parseState(raw: string): string | null {
  try {
    const parsed = JSON.parse(raw) as { state?: unknown }
    return typeof parsed.state === 'string' ? parsed.state : null
  } catch {
    return null
  }
}

export function watchRelayStatus(
  socket: unknown,
  relayKey: Uint8Array,
  onAllRelaysBusy: () => void
): void {
  try {
    const channel = Protomux.from(socket).createChannel({ protocol: PROTOCOL })
    if (!channel) return

    channel.addMessage<string>({
      encoding: c.string,
      onmessage: (raw) => {
        if (parseState(raw) !== 'busy') return
        markRelayBusy(relayKey)
        if (allRelaysBusy()) onAllRelaysBusy()
      }
    })
    channel.open()
  } catch (err) {
    console.warn('relay status: channel setup failed', err instanceof Error ? err.message : err)
  }
}