connection events has .pubkey field

This commit is contained in:
Andre Staltz 2024-01-15 23:01:51 +02:00
parent 0ce68f2770
commit 4101e023cd
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 27 additions and 5 deletions

View File

@ -18,6 +18,7 @@ const IP = require('ip')
* | 'disconnecting'
* | 'disconnected';
* address: Address;
* pubkey: string | undefined;
* details?: any;
* }} ConnectionEvent
* @typedef {{
@ -77,6 +78,20 @@ class Connections {
return
}
/**
* @param {string} addresses
* @returns {string | undefined}
*/
static extractSHSEPubkey(addresses) {
for (const address of addresses.split(';')) {
const [transport, transform] = address.split('~')
const [name, pubkey, extra] = transform.split(':')
if (name === 'shse') {
return pubkey
}
}
}
#assertNotClosed() {
if (this.#closed) {
throw new Error('This Connections instance is closed, create a new one.')
@ -102,12 +117,13 @@ class Connections {
const initiator = weAreClient ? 'we' : 'they'
debug('Connected to %s, %s initiated it', address, initiator)
const pubkey = Connections.extractSHSEPubkey(address)
this.#rpcs.set(address, rpc)
rpc.once('closed', () => {
debug('Disconnected from %s', address)
this.#rpcs.delete(address)
this.#infos.update(address, { state: 'disconnected' })
this.#notifyEvent({ type: 'disconnected', address })
this.#notifyEvent({ type: 'disconnected', address, pubkey })
this.#infos.emit()
})
@ -117,6 +133,7 @@ class Connections {
this.#notifyEvent({
type: state,
address,
pubkey,
details: { rpc, weAreClient },
})
this.#infos.emit()
@ -166,8 +183,9 @@ class Connections {
case 'disconnected': {
debug('Connecting to %s', address)
const state = /**@type {Info['state']}*/ ('connecting')
const pubkey = Connections.extractSHSEPubkey(address)
this.#infos.update(address, { state })
this.#notifyEvent({ type: state, address })
this.#notifyEvent({ type: state, address, pubkey })
this.#infos.emit()
const [err, rpc] = await run(this.#peer.connect)(address)
@ -177,6 +195,7 @@ class Connections {
this.#notifyEvent({
type: 'connecting-failed',
address,
pubkey,
details: err,
})
this.#infos.emit()
@ -244,8 +263,9 @@ class Connections {
debug('Disconnecting from %s', address)
const state = /**@type {Info['state']}*/ ('disconnecting')
const pubkey = Connections.extractSHSEPubkey(address)
this.#infos.update(address, { state })
this.#notifyEvent({ type: state, address })
this.#notifyEvent({ type: state, address, pubkey })
this.#infos.emit()
// @ts-ignore
await run(rpc.close)(true)

View File

@ -181,6 +181,7 @@ function initNet(peer, config) {
}
exports.name = 'net'
exports.needs = ['shse']
exports.manifest = {
start: 'sync',
stop: 'sync',

View File

@ -4,8 +4,8 @@ const p = require('node:util').promisify
const pull = require('pull-stream')
const { createPeer } = require('./util')
const TEST_ADDR =
'net:localhost:9752~shse:EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC'
const PUBKEY = 'EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC'
const TEST_ADDR = `net:localhost:9752~shse:${PUBKEY}`
test('net', async (t) => {
await t.test('connect() rejects given unreachable address', async () => {
@ -69,6 +69,7 @@ test('net', async (t) => {
if (i === 1) {
assert.equal(ev.type, 'connecting', 'event.type ok')
assert.equal(ev.address, TEST_ADDR, 'event.address ok')
assert.equal(ev.pubkey, PUBKEY, 'event.pubkey ok')
} else if (i === 2) {
assert.equal(ev.type, 'connecting-failed', 'event.type ok')
assert.equal(ev.address, TEST_ADDR, 'event.address ok')