From 621375bfede340a9e9876fe6643250ac65feb002 Mon Sep 17 00:00:00 2001 From: Billie Hilton <587740+billiegoose@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:29:12 -0400 Subject: [PATCH] add 'relaying' event to swarm --- index.js | 7 +++++++ test/manual/measure-reconnect.js | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e726b0a..e8eea54 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,13 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false + conn.on('relaying', ({ relayThrough }) => { + this.emit('relaying', { + publicKey: peerInfo.publicKey, + relayThrough + }) + }) + conn.on('open', () => { opened = true this.stats.connects.client.opened++ diff --git a/test/manual/measure-reconnect.js b/test/manual/measure-reconnect.js index 15af75a..4b9f88c 100644 --- a/test/manual/measure-reconnect.js +++ b/test/manual/measure-reconnect.js @@ -9,11 +9,18 @@ function customLogger (data) { console.log(` ... ${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`) } -require('hypertrace').setTraceFunction(customLogger) +try { + require('hypertrace').setTraceFunction(customLogger) +} catch { + console.log('Please run:') + console.log('npm install --no-save hypertrace hypercore-id-encoding @holepunchto/keet-default-config') + process.exit(1) +} const { DEV_BLIND_RELAY_KEYS } = require('@holepunchto/keet-default-config') const HypercoreId = require('hypercore-id-encoding') const DEV_RELAY_KEYS = DEV_BLIND_RELAY_KEYS.map(HypercoreId.decode) +console.log('DEV_RELAY_KEYS', DEV_RELAY_KEYS.map(b => b.toString('hex').slice(0, 8) + '...')) const relayThrough = (force) => force ? DEV_RELAY_KEYS : null const Hyperswarm = require('../..') @@ -30,6 +37,10 @@ swarm.dht.on('network-change', () => { let connected = false +swarm.on('relaying', ({ publicKey, relayThrough }) => { + console.log(`connecting to ${publicKey.toString('hex').slice(0, 8)}... via ${relayThrough.toString('hex').slice(0, 8)}...`) +}) + swarm.on('connection', async (conn) => { console.log(conn.rawStream.remoteHost) conn.on('error', console.log.bind(console)) @@ -48,6 +59,6 @@ swarm.on('connection', async (conn) => { console.time('INITIAL CONNECTION TIME') swarm.join(topic) -// process.on('SIGINT', () => { -// swarm.leave(topic).then(() => process.exit()) -// }) +process.on('SIGINT', () => { + swarm.leave(topic).then(() => process.exit()) +})