From 03a76f7701cb471017b313b7fcd76196f12e7d74 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Tue, 23 Jul 2024 23:22:58 +0200 Subject: [PATCH 1/2] Bugfix: remove initial error handler when a conn opens --- index.js | 20 ++++++++++++-------- test/swarm.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index e726b0a..207d879 100644 --- a/index.js +++ b/index.js @@ -187,13 +187,24 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false + const maybeForceRelayNext = (err) => { + if (this.relayThrough && shouldForceRelaying(err.code)) { + peerInfo.forceRelaying = true + // Reset the attempts in order to fast connect to relay + peerInfo.attempts = 0 + } + } + + // Removed once a connection is opened + conn.on('error', maybeForceRelayNext) + conn.on('open', () => { opened = true this.stats.connects.client.opened++ this._connectDone() this.connections.add(conn) - conn.removeListener('error', noop) + conn.removeListener('error', maybeForceRelayNext) peerInfo._connected() peerInfo.client = true this.emit('connection', conn, peerInfo) @@ -201,13 +212,6 @@ module.exports = class Hyperswarm extends EventEmitter { this.emit('update') }) - conn.on('error', err => { - if (this.relayThrough && shouldForceRelaying(err.code)) { - peerInfo.forceRelaying = true - // Reset the attempts in order to fast connect to relay - peerInfo.attempts = 0 - } - }) conn.on('close', () => { if (!opened) this._connectDone() this.stats.connects.client.closed++ diff --git a/test/swarm.js b/test/swarm.js index da56499..0189195 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -681,6 +681,35 @@ test('peer-discovery object deleted when corresponding connection closes (client await swarm1.destroy() }) +test('no default error handler set when connection event is emitted', async (t) => { + t.plan(2) + + const { bootstrap } = await createTestnet(3, t.teardown) + + const swarm1 = new Hyperswarm({ bootstrap }) + const swarm2 = new Hyperswarm({ bootstrap }) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) + + swarm2.on('connection', (conn) => { + t.is(conn.listeners('error').length, 0, 'no error listeners') + conn.on('error', noop) + conn.end() + }) + swarm1.on('connection', (conn) => { + t.is(conn.listeners('error').length, 0, 'no error listeners') + conn.on('error', noop) + conn.end() + }) + + const topic = Buffer.alloc(32).fill('hello world') + await swarm1.join(topic, { server: true, client: false }).flushed() + swarm2.join(topic, { client: true, server: false }) +}) + function noop () {} function eventFlush () { From 5ddad94d9f240fbd1485f877b91197a6d82571f5 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 24 Jul 2024 08:49:10 +0200 Subject: [PATCH 2/2] rename to onerror --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 207d879..b79a110 100644 --- a/index.js +++ b/index.js @@ -187,7 +187,7 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false - const maybeForceRelayNext = (err) => { + const onerror = (err) => { if (this.relayThrough && shouldForceRelaying(err.code)) { peerInfo.forceRelaying = true // Reset the attempts in order to fast connect to relay @@ -196,7 +196,7 @@ module.exports = class Hyperswarm extends EventEmitter { } // Removed once a connection is opened - conn.on('error', maybeForceRelayNext) + conn.on('error', onerror) conn.on('open', () => { opened = true @@ -204,7 +204,7 @@ module.exports = class Hyperswarm extends EventEmitter { this._connectDone() this.connections.add(conn) - conn.removeListener('error', maybeForceRelayNext) + conn.removeListener('error', onerror) peerInfo._connected() peerInfo.client = true this.emit('connection', conn, peerInfo)