fix: shared-swarm safe connection handlers + dep pins

This commit is contained in:
Raven Scott
2026-07-13 03:14:38 -04:00
parent 19e0ff3492
commit 858ff8d953
2 changed files with 38 additions and 13 deletions
+35 -10
View File
@@ -636,19 +636,37 @@ class PearcordContacts extends EventEmitter {
if (this._meshJoined && this.swarm === swarm) return this
await this.leaveMesh().catch(() => {})
this.swarm = swarm
this.swarm.removeAllListeners('connection')
this.swarm.on('connection', (conn) => {
if (this._connectionHandler) {
try {
this.swarm.removeListener('connection', this._connectionHandler)
} catch {
// ignore
}
}
this._connectionHandler = (conn, peerInfo) => {
const { wireSwarmConnection } = require('pearcord-shared')
wireSwarmConnection(conn)
const peerId = b4a.toString(conn.remotePublicKey, 'hex')
this.peers.set(peerId, conn)
const wire = attachContactsMesh(this, conn)
if (wire) this._channels.set(peerId, wire)
this.emit('peer', { peerId, type: 'join' })
conn.on('close', () => {
this.peers.delete(peerId)
this._channels.delete(peerId)
this.emit('peer', { peerId, type: 'leave' })
})
})
this.emit('peer', { peerId, type: 'join', peerInfo: peerInfo || null })
if (!conn._pearcordContactsClose) {
conn._pearcordContactsClose = true
conn.on('close', () => {
if (this.peers.get(peerId) !== conn) return
this.peers.delete(peerId)
this._channels.delete(peerId)
this.emit('peer', { peerId, type: 'leave' })
})
}
}
this.swarm.on('connection', this._connectionHandler)
if (this.swarm.connections?.size) {
for (const conn of this.swarm.connections) {
if (!conn.destroyed && !conn.closed) this._connectionHandler(conn, null)
}
}
const joinMs = Number(process.env.PEARCORD_MESH_JOIN_MS) || 8000
await Promise.race([
this.swarm.join(topicToBuffer(contactsTopic(this.userId)), {
@@ -690,8 +708,15 @@ class PearcordContacts extends EventEmitter {
if (this.swarm) {
await this.swarm.leave(topicToBuffer(contactsTopic(this.userId))).catch(() => {})
await this.swarm.leave(topicToBuffer(globalPresenceTopic(this.userId))).catch(() => {})
this.swarm.removeAllListeners('connection')
if (this._connectionHandler) {
try {
this.swarm.removeListener('connection', this._connectionHandler)
} catch {
// ignore
}
}
}
this._connectionHandler = null
this.swarm = null
}
+3 -3
View File
@@ -6,9 +6,9 @@
"description": "P2P friend requests and contact list for Pearcord",
"dependencies": {
"bare-events": "^2.8.0",
"b4a": "^1.6.7",
"hyperswarm": "^4.8.0",
"protomux": "^3.0.0",
"b4a": "^1.8.1",
"hyperswarm": "^4.17.0",
"protomux": "^3.11.0",
"pearcord-db": "git+https://git.ssh.surf/pearcord/pearcord-db.git#main",
"pearcord-drive": "git+https://git.ssh.surf/pearcord/pearcord-drive.git#main",
"pearcord-shared": "git+https://git.ssh.surf/pearcord/pearcord-shared.git#main",