OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-bridge / pds / lib / fleet-replicate.js

38 lines · javascript ref:
// Fleet replicate (Track 3, the "little heap"): peers replicate a repo's Hypercore DIRECTLY
// over Hyperswarm — no HTTP, no RPC. PROTOTYPE.
//
// fleet-serve fetches a repo on demand (edge asks the home for a CAR). This is deeper: a
// replica continuously replicates the home's underlying Corestore, so it holds the actual
// repo Hypercore locally — verifiable, servable, and live-updating without asking the home
// each time. This is the P2P read path the whole thesis rests on; the HTTP membrane is only
// the bsky on-ramp. A repo replicated by several instances (own devices + a blind peer) is
// also what makes hot-failover real below the edge.
//
// Rendezvous mirrors fleet-serve: the home joins a topic = its swarm pubkey (its serverKey);
// a replica connects there and pipes the connection into its Corestore's replication.

import b4a from 'b4a'
import { connectToServer } from './fleet-serve.js'

// Home: replicate this instance's Corestore over every swarm connection.
export function serveReplication (corestore, swarm) {
  const onconn = (conn) => corestore.replicate(conn)
  swarm.on('connection', onconn)
  const topic = swarm.keyPair.publicKey
  const discovery = swarm.join(topic, { server: true, client: false })
  return {
    serverKey: b4a.toString(topic, 'hex'),
    ready: () => discovery.flushed(),
    close: () => swarm.off('connection', onconn)
  }
}

// Replica: connect to a home instance by serverKey and replicate its Corestore into ours.
// Afterward, open the home's repo core with `new RepoStore(replicaStore, { key })` to read
// (and live-follow) the replicated repo.
export async function connectReplica (corestore, swarm, serverKeyHex, { timeout = 15000 } = {}) {
  const conn = await connectToServer(swarm, b4a.from(serverKeyHex, 'hex'), timeout)
  corestore.replicate(conn)
  return conn
}