Skip to content

Commit

Permalink
Use safetyCatch err handlers instead of noop
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Dec 19, 2023
1 parent 21390bf commit b9ac6a2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 () {
Expand All @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -516,8 +517,6 @@ module.exports = class Hyperswarm extends EventEmitter {
}
}

function noop () { }

function allowAll () {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion lib/peer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ module.exports = class PeerDiscovery {
resume () {
if (!this.suspended) return
this.suspended = false
this.refresh().catch(noop)
this.refresh().catch(safetyCatch)
}
}

Expand Down
5 changes: 2 additions & 3 deletions test/chaos.js
Original file line number Diff line number Diff line change
@@ -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('..')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -110,5 +111,3 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => {

for (const swarm of swarms) await swarm.destroy()
})

function noop () {}
5 changes: 2 additions & 3 deletions test/dups.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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'))
})
Expand Down Expand Up @@ -51,5 +52,3 @@ test('many servers', async t => {

for (const swarm of swarms) await swarm.destroy()
})

function noop () {}
11 changes: 5 additions & 6 deletions test/peer-join.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const test = require('brittle')
const createTestnet = require('hyperdht/testnet')
const safetyCatch = require('safety-catch')

const Hyperswarm = require('..')

Expand All @@ -21,15 +22,15 @@ 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
}
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
Expand Down Expand Up @@ -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)
Expand All @@ -183,5 +184,3 @@ test('leave peer - no memory leak if other side closed connection first', async

swarm1.leavePeer(swarm2.keyPair.publicKey)
})

function noop () {}
Loading

0 comments on commit b9ac6a2

Please sign in to comment.