Skip to content

Commit cea0c56

Browse files
authored
Replace noop by safetyCatch where relevant (#466)
1 parent bee72e6 commit cea0c56

File tree

4 files changed

+7
-12
lines changed

4 files changed

+7
-12
lines changed

index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const NoiseSecretStream = require('@hyperswarm/secret-stream')
99
const Protomux = require('protomux')
1010
const z32 = require('z32')
1111
const id = require('hypercore-id-encoding')
12+
const safetyCatch = require('safety-catch')
1213
const { createTracer } = require('hypertrace')
1314

1415
const Replicator = require('./lib/replicator')
@@ -92,7 +93,7 @@ module.exports = class Hypercore extends EventEmitter {
9293
this._active = opts.active !== false
9394

9495
this.opening = this._openSession(key, storage, opts)
95-
this.opening.catch(noop)
96+
this.opening.catch(safetyCatch)
9697
}
9798

9899
[inspect] (depth, opts) {
@@ -972,7 +973,7 @@ module.exports = class Hypercore extends EventEmitter {
972973
const req = this._download(range)
973974

974975
// do not crash in the background...
975-
req.catch(noop)
976+
req.catch(safetyCatch)
976977

977978
return new Download(req)
978979
}
@@ -1127,8 +1128,6 @@ module.exports = class Hypercore extends EventEmitter {
11271128
}
11281129
}
11291130

1130-
function noop () {}
1131-
11321131
function isStream (s) {
11331132
return typeof s === 'object' && s && typeof s.pipe === 'function'
11341133
}

lib/batch.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { BLOCK_NOT_AVAILABLE, SESSION_CLOSED } = require('hypercore-errors')
22
const EventEmitter = require('events')
33
const c = require('compact-encoding')
44
const b4a = require('b4a')
5+
const safetyCatch = require('safety-catch')
56

67
module.exports = class HypercoreBatch extends EventEmitter {
78
constructor (session, checkoutLength, autoClose, restore, clear) {
@@ -28,7 +29,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
2829
this._clear = clear
2930

3031
this.opening = this._open()
31-
this.opening.catch(noop)
32+
this.opening.catch(safetyCatch)
3233
}
3334

3435
get id () {
@@ -404,5 +405,3 @@ module.exports = class HypercoreBatch extends EventEmitter {
404405
this.fork = 0
405406
}
406407
}
407-
408-
function noop () {}

lib/replicator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ module.exports = class Replicator {
20122012
}
20132013

20142014
_closeSession (session) {
2015-
session.close().catch(noop)
2015+
session.close().catch(safetyCatch)
20162016
}
20172017

20182018
attached (protomux) {

test/basic.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test(
101101
function (t) {
102102
const key = b4a.alloc(33).fill('a')
103103

104-
const core = new Hypercore(RAM, key, { crypto: {} })
104+
const core = new Hypercore(RAM, key, { crypto: { discoveryKey: () => key } })
105105

106106
t.is(core.key, key)
107107
t.pass('creating a core with more than 32 byteLength key did not throw')
@@ -335,9 +335,6 @@ test('key is set sync', async function (t) {
335335

336336
t.alike((new Hypercore(RAM, { key })).key, key)
337337
t.is((new Hypercore(RAM, { })).key, null)
338-
339-
t.alike((new Hypercore({ key })).key, key)
340-
t.is((new Hypercore({ })).key, null)
341338
})
342339

343340
test('disable writable option', async function (t) {

0 commit comments

Comments
 (0)