updates
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
import Hyperswarm from 'hyperswarm'
|
||||
import b4a from 'b4a'
|
||||
import { getCorestore, getDb } from './index.js'
|
||||
import { openRemoteDb } from './remote.js'
|
||||
import { getServerPublicKeyHex } from '../core/auth-keys.js'
|
||||
import logger from '../utils/logger.js'
|
||||
|
||||
const log = logger.child('db:replicate')
|
||||
@@ -61,6 +63,11 @@ export async function startReplication() {
|
||||
discoveryKeyHex: model.discoveryKeyHex,
|
||||
dbKeyHex: model.publicKeyHex,
|
||||
})
|
||||
|
||||
await rejoinLinkedPeers().catch((err) => {
|
||||
log.warn('rejoinLinkedPeers failed', { error: err.message })
|
||||
})
|
||||
|
||||
return swarm
|
||||
}
|
||||
|
||||
@@ -85,6 +92,58 @@ export async function joinRemoteTopic(discoveryKeyHexOrBuf) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist + open a linked peer for warm pull.
|
||||
* @param {{ discoveryKeyHex?: string|null, dbKeyHex?: string|null, syncMode?: string }} link
|
||||
*/
|
||||
export async function attachLinkedPeer(link) {
|
||||
const mode = link.syncMode || 'both'
|
||||
if (mode !== 'pull' && mode !== 'both') return { joined: false, opened: false }
|
||||
|
||||
let joined = false
|
||||
if (isSwarmEnabled() && link.discoveryKeyHex) {
|
||||
try {
|
||||
await joinRemoteTopic(link.discoveryKeyHex)
|
||||
joined = true
|
||||
} catch (err) {
|
||||
log.warn('joinRemoteTopic failed', { error: err.message })
|
||||
}
|
||||
}
|
||||
|
||||
let opened = false
|
||||
if (link.dbKeyHex) {
|
||||
const remote = await openRemoteDb(link.dbKeyHex)
|
||||
opened = Boolean(remote)
|
||||
}
|
||||
return { joined, opened }
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-join swarm topics + open remote bees from persisted peer-links (boot).
|
||||
*/
|
||||
export async function rejoinLinkedPeers() {
|
||||
const db = getDb()
|
||||
if (!db) return { links: 0, joined: 0, opened: 0 }
|
||||
let localNodeId
|
||||
try {
|
||||
localNodeId = getServerPublicKeyHex().slice(0, 16)
|
||||
} catch {
|
||||
return { links: 0, joined: 0, opened: 0 }
|
||||
}
|
||||
const links = await db.listPeerLinks(localNodeId)
|
||||
let joined = 0
|
||||
let opened = 0
|
||||
for (const link of links) {
|
||||
const res = await attachLinkedPeer(link)
|
||||
if (res.joined) joined++
|
||||
if (res.opened) opened++
|
||||
}
|
||||
if (links.length) {
|
||||
log.info('Rejoined linked peers', { links: links.length, joined, opened })
|
||||
}
|
||||
return { links: links.length, joined, opened }
|
||||
}
|
||||
|
||||
export async function stopReplication() {
|
||||
if (!swarm) return
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user