diff --git a/index.js b/index.js index 2b56c73..43a5bac 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const { EventEmitter } = require('events') const DHT = require('hyperdht') const spq = require('shuffled-priority-queue') const b4a = require('b4a') +const safetyCatch = require('safety-catch') const PeerInfo = require('./lib/peer-info') const RetryTimer = require('./lib/retry-timer') @@ -175,7 +176,7 @@ module.exports = class Hyperswarm extends EventEmitter { opened = true this._connectDone() this.connections.add(conn) - conn.removeListener('error', noop) + conn.removeListener('error', safetyCatch) peerInfo._connected() peerInfo.client = true this.emit('connection', conn, peerInfo) @@ -247,13 +248,13 @@ module.exports = class Hyperswarm extends EventEmitter { existing.on('close', () => { if (closed) return - conn.removeListener('error', noop) + conn.removeListener('error', safetyCatch) conn.removeListener('close', onclose) this._handleServerConnection(conn) }) - conn.on('error', noop) + conn.on('error', safetyCatch) conn.on('close', onclose) function onclose () { @@ -265,7 +266,7 @@ module.exports = class Hyperswarm extends EventEmitter { _handleServerConnection (conn) { if (this.destroyed) { // TODO: Investigate why a final server connection can be received after close - conn.on('error', noop) + conn.on('error', safetyCatch) return conn.destroy(ERR_DESTROYED) } @@ -278,12 +279,12 @@ module.exports = class Hyperswarm extends EventEmitter { if (keepNew === false) { existing.write(KEEP_ALIVE) // check to see if its still alive actually - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy(new Error(ERR_DUPLICATE)) return } - existing.on('error', noop) + existing.on('error', safetyCatch) existing.destroy(new Error(ERR_DUPLICATE)) this._handleServerConnectionSwap(existing, conn) return @@ -516,8 +517,6 @@ module.exports = class Hyperswarm extends EventEmitter { } } -function noop () { } - function allowAll () { return false } diff --git a/lib/peer-discovery.js b/lib/peer-discovery.js index aeefe6d..675782f 100644 --- a/lib/peer-discovery.js +++ b/lib/peer-discovery.js @@ -229,7 +229,7 @@ module.exports = class PeerDiscovery { resume () { if (!this.suspended) return this.suspended = false - this.refresh().catch(noop) + this.refresh().catch(safetyCatch) } } diff --git a/test/chaos.js b/test/chaos.js index 5ad34b4..47c0113 100644 --- a/test/chaos.js +++ b/test/chaos.js @@ -1,6 +1,7 @@ const test = require('brittle') const crypto = require('hypercore-crypto') const createTestnet = require('hyperdht/testnet') +const safetyCatch = require('safety-catch') const { timeout } = require('./helpers') const Hyperswarm = require('..') @@ -37,7 +38,7 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { swarm.on('connection', conn => { connections.push(conn) - conn.on('error', noop) + conn.on('error', safetyCatch) conn.on('close', () => { clearInterval(timer) const idx = connections.indexOf(conn) @@ -110,5 +111,3 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { for (const swarm of swarms) await swarm.destroy() }) - -function noop () {} diff --git a/test/dups.js b/test/dups.js index 5bc7841..ace1275 100644 --- a/test/dups.js +++ b/test/dups.js @@ -1,5 +1,6 @@ const test = require('brittle') const createTestnet = require('hyperdht/testnet') +const safetyCatch = require('safety-catch') const Hyperswarm = require('../') test('many servers', async t => { @@ -23,7 +24,7 @@ test('many servers', async t => { swarm.on('connection', conn => { missing.add(conn.remotePublicKey.toString('hex')) - conn.on('error', noop) + conn.on('error', safetyCatch) conn.on('close', function () { missing.delete(conn.remotePublicKey.toString('hex')) }) @@ -51,5 +52,3 @@ test('many servers', async t => { for (const swarm of swarms) await swarm.destroy() }) - -function noop () {} diff --git a/test/peer-join.js b/test/peer-join.js index b0042be..6e9a694 100644 --- a/test/peer-join.js +++ b/test/peer-join.js @@ -1,5 +1,6 @@ const test = require('brittle') const createTestnet = require('hyperdht/testnet') +const safetyCatch = require('safety-catch') const Hyperswarm = require('..') @@ -21,7 +22,7 @@ test('join peer - can establish direct connections to public keys', async (t) => let s1Connected = false swarm2.on('connection', conn => { - conn.on('error', noop) + conn.on('error', safetyCatch) if (!s2Connected) { firstConnection.pass('swarm2 got its first connection') s2Connected = true @@ -29,7 +30,7 @@ test('join peer - can establish direct connections to public keys', async (t) => connections.pass('swarm2 got a connection') }) swarm1.on('connection', conn => { - conn.on('error', noop) + conn.on('error', safetyCatch) if (!s1Connected) { firstConnection.pass('swarm1 got its first connection') s1Connected = true @@ -157,12 +158,12 @@ test('leave peer - no memory leak if other side closed connection first', async swarm2.on('connection', conn => { conn.once('close', () => close.pass('swarm2 connection closed')) open.pass('swarm2 got a connection') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm1.on('connection', conn => { conn.once('close', () => close.pass('swarm1 connection closed')) open.pass('swarm1 got a connection') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm1.joinPeer(swarm2.keyPair.publicKey) @@ -183,5 +184,3 @@ test('leave peer - no memory leak if other side closed connection first', async swarm1.leavePeer(swarm2.keyPair.publicKey) }) - -function noop () {} diff --git a/test/swarm.js b/test/swarm.js index bac92a5..8650937 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -1,5 +1,6 @@ const test = require('brittle') const createTestnet = require('hyperdht/testnet') +const safetyCatch = require('safety-catch') const { timeout, flushConnections } = require('./helpers') const Hyperswarm = require('..') @@ -26,11 +27,11 @@ test('one server, one client - first connection', async (t) => { swarm2.on('connection', (conn) => { t.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) conn.end() }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) conn.end() }) @@ -57,12 +58,12 @@ test('two servers - first connection', async (t) => { }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) connection1Test.pass('swarm1') conn.end() }) swarm2.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) connection2Test.pass('swarm2') conn.end() }) @@ -94,7 +95,7 @@ test('one server, one client - single reconnect', async (t) => { let serverDisconnected = false swarm2.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) if (!hasClientConnected) { hasClientConnected = true @@ -105,7 +106,7 @@ test('one server, one client - single reconnect', async (t) => { }) swarm1.on('connection', async (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) if (!serverDisconnected) { serverDisconnected = true @@ -135,11 +136,11 @@ test('one server, one client - maximum reconnects', async (t) => { let connections = 0 swarm2.on('connection', (conn, info) => { connections++ - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy() }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -164,11 +165,11 @@ test('one server, one client - banned peer does not reconnect', async (t) => { swarm2.on('connection', (conn, info) => { connections++ info.ban(true) - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy() }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -201,11 +202,11 @@ test('two servers, two clients - simple deduplication', async (t) => { swarm1.on('connection', (conn) => { connection1Test.pass('Swarm 1 connection') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm2.on('connection', (conn) => { connection2Test.pass('Swarm 2 connection') - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -225,10 +226,10 @@ test('one server, two clients - topic multiplexing', async (t) => { swarm2.on('connection', (conn, info) => { clientConnections++ peerInfo = info - conn.on('error', noop) + conn.on('error', safetyCatch) }) - swarm1.on('connection', (conn) => conn.on('error', noop)) + swarm1.on('connection', (conn) => conn.on('error', safetyCatch)) const topic1 = Buffer.alloc(32).fill('hello world') const topic2 = Buffer.alloc(32).fill('hi world') @@ -271,17 +272,17 @@ test('one server, two clients - first connection', async (t) => { swarm1.on('connection', (conn) => { connection1Test.pass('swarm1') - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy() }) swarm2.on('connection', (conn) => { connection2Test.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy() }) swarm3.on('connection', (conn) => { connection3Test.pass('swarm3') - conn.on('error', noop) + conn.on('error', safetyCatch) conn.destroy() }) @@ -304,11 +305,11 @@ test('one server, two clients - if a second client joins after the server leaves const swarm3 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) }) - swarm2.on('connection', (conn) => conn.on('error', noop)) - swarm3.on('connection', (conn) => conn.on('error', noop)) + swarm2.on('connection', (conn) => conn.on('error', safetyCatch)) + swarm3.on('connection', (conn) => conn.on('error', safetyCatch)) const topic = Buffer.alloc(32).fill('hello world') await swarm1.join(topic).flushed() @@ -341,11 +342,11 @@ test('two servers, one client - refreshing a peer discovery instance discovers n let clientConnections = 0 swarm3.on('connection', (conn) => { clientConnections++ - conn.on('error', noop) + conn.on('error', safetyCatch) }) - swarm1.on('connection', (conn) => conn.on('error', noop)) - swarm2.on('connection', (conn) => conn.on('error', noop)) + swarm1.on('connection', (conn) => conn.on('error', safetyCatch)) + swarm2.on('connection', (conn) => conn.on('error', safetyCatch)) const topic = Buffer.alloc(32).fill('hello world') await swarm1.join(topic).flushed() @@ -385,7 +386,7 @@ test('one server, one client - correct deduplication when a client connection is swarm1.on('connection', (conn) => { serverConnections++ - conn.on('error', noop) + conn.on('error', safetyCatch) conn.on('data', () => { if (++serverData >= 2) { t.is(serverConnections, 2, 'Server opened second connection') @@ -396,7 +397,7 @@ test('one server, one client - correct deduplication when a client connection is }) swarm2.on('connection', (conn) => { clientConnections++ - conn.on('error', noop) + conn.on('error', safetyCatch) conn.on('data', () => { if (++clientData >= 2) { t.is(clientConnections, 2, 'Client opened second connection') @@ -443,11 +444,11 @@ test('constructor options - debug options forwarded to DHT constructor', async ( swarm1.once('connection', (conn) => { connected.pass('swarm1') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm2.once('connection', (conn) => { connected.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -477,12 +478,12 @@ test('flush when max connections reached', async (t) => { await swarm1.join(topic, { server: true }).flushed() await swarm2 - .on('connection', (conn) => conn.on('error', noop)) + .on('connection', (conn) => conn.on('error', safetyCatch)) .join(topic, { client: true }) .flushed() await swarm3 - .on('connection', (conn) => conn.on('error', noop)) + .on('connection', (conn) => conn.on('error', safetyCatch)) .join(topic, { client: true }) .flushed() @@ -508,7 +509,7 @@ test('rejoining with different client/server opts refreshes', async (t) => { await swarm1.join(topic, { client: true, server: true }).flushed() await swarm2 - .on('connection', (conn) => conn.on('error', noop)) + .on('connection', (conn) => conn.on('error', safetyCatch)) .join(topic, { client: true }) .flushed() @@ -559,12 +560,12 @@ test('multiple discovery sessions with different opts', async (t) => { swarm1.on('connection', (conn) => { connection1Test.pass('swarm1') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm2.on('connection', (conn) => { connection2Test.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) }) await swarm1.join(topic).flushed() @@ -610,11 +611,11 @@ test('peer-discovery object deleted when corresponding connection closes (server swarm2.on('connection', (conn) => { connected.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm1.on('connection', (conn) => { otherConnected.pass('swarm1') - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -661,10 +662,10 @@ test('peer-discovery object deleted when corresponding connection closes (client swarm2.on('connection', (conn) => { connected.pass('swarm2') - conn.on('error', noop) + conn.on('error', safetyCatch) }) swarm1.on('connection', (conn) => { - conn.on('error', noop) + conn.on('error', safetyCatch) }) const topic = Buffer.alloc(32).fill('hello world') @@ -677,8 +678,6 @@ test('peer-discovery object deleted when corresponding connection closes (client await swarm1.destroy() }) -function noop () {} - function eventFlush () { return new Promise(resolve => setImmediate(resolve)) }