diff --git a/index.js b/index.js index e726b0a..b79a110 100644 --- a/index.js +++ b/index.js @@ -187,13 +187,24 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false + const onerror = (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', onerror) + conn.on('open', () => { opened = true this.stats.connects.client.opened++ this._connectDone() this.connections.add(conn) - conn.removeListener('error', noop) + conn.removeListener('error', onerror) 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 () {