OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-bridge / pds / test / serve-announce.js

31 lines · javascript ref:
// Test helper (NOT a test — excluded by the test/*.test.js glob): push an atproto/did-announce/v1
// announce over a connection, mirroring hyper-proto-peer serve-membrane's push. After the Step-4
// cutover, announce-push is the ONLY discovery path (get-coords is gone), so test peers announce
// their {repoKey,firehoseKey,lengths} instead of answering a get-coords request.
import Protomux from 'protomux'
import c from 'compact-encoding'
import b4a from 'b4a'

export function pushAnnounce (conn, { did, handle = null, repo, seq }) {
  const chan = Protomux.from(conn).createChannel({ protocol: 'atproto/did-announce/v1' })
  if (!chan) return
  const msg = chan.addMessage({ encoding: c.json, onmessage: () => {} })
  chan.open()
  const send = () => {
    try {
      msg.send({
        did,
        handle,
        repoKey: b4a.toString(repo.core.key, 'hex'),
        firehoseKey: b4a.toString(seq.core.key, 'hex'),
        repoLength: repo.core.length,
        firehoseLength: seq.core.length
      })
    } catch {}
  }
  send()
  const onapp = () => send()
  seq.core.on('append', onapp)
  conn.on('close', () => { try { seq.core.off('append', onapp) } catch {} })
}