Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 27f9aed

Browse files
committed
feat: (BREAKING CHANGE) constructor takes options. + add tag, update deps and fix tests
1 parent 00529f6 commit 27f9aed

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
},
3131
"homepage": "https://github.com/libp2p/js-ipfs-railing",
3232
"devDependencies": {
33-
"aegir": "^13.0.6",
33+
"aegir": "^14.0.0",
3434
"chai": "^4.1.2",
3535
"libp2p-tcp": "~0.12.0",
3636
"pre-commit": "^1.2.2"
3737
},
3838
"dependencies": {
39-
"async": "^2.6.0",
39+
"async": "^2.6.1",
4040
"debug": "^3.1.0",
41-
"lodash": "^4.17.5",
42-
"multiaddr": "^4.0.0",
41+
"lodash": "^4.17.10",
42+
"multiaddr": "^5.0.0",
4343
"mafmt": "^6.0.0",
4444
"peer-id": "~0.10.7",
4545
"peer-info": "~0.14.1"

src/index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,31 @@ const setImmediate = require('async/setImmediate')
1111
const log = debug('libp2p:railing')
1212
log.error = debug('libp2p:railing:error')
1313

14+
function isIPFS (addr) {
15+
try {
16+
return mafmt.IPFS.matches(addr)
17+
} catch (e) {
18+
return false
19+
}
20+
}
21+
1422
class Railing extends EventEmitter {
15-
constructor (bootstrapers) {
23+
constructor (options) {
1624
super()
17-
this.bootstrapers = bootstrapers
18-
this.interval = null
25+
this._list = options.list
26+
this._interval = options.interval || 10000
27+
this._timer = null
1928
}
2029

2130
start (callback) {
2231
setImmediate(() => callback())
2332

24-
if (this.interval) { return }
33+
if (this._timer) { return }
2534

26-
this.interval = setInterval(() => {
27-
this.bootstrapers.forEach((candidate) => {
35+
this._timer = setInterval(() => {
36+
this._list.forEach((candidate) => {
2837
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }
38+
2939
const ma = multiaddr(candidate)
3040

3141
const peerId = PeerId.createFromB58String(ma.getPeerId())
@@ -36,24 +46,18 @@ class Railing extends EventEmitter {
3646
this.emit('peer', peerInfo)
3747
})
3848
})
39-
}, 10000)
49+
}, this._interval)
4050
}
4151

4252
stop (callback) {
4353
setImmediate(callback)
44-
if (this.interval) {
45-
clearInterval(this.interval)
46-
this.interval = null
47-
}
48-
}
49-
}
5054

51-
function isIPFS (addr) {
52-
try {
53-
return mafmt.IPFS.matches(addr)
54-
} catch (e) {
55-
return false
55+
if (this.timer) {
56+
clearInterval(this.timer)
57+
this.timer = null
58+
}
5659
}
5760
}
5861

59-
module.exports = Railing
62+
exports = module.exports = Railing
63+
exports.tag = 'bootstrap'

test/railing.spec.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ const partialValidPeerList = require('./some-invalid-peers')
77
const {expect} = require('chai')
88
const mafmt = require('mafmt')
99

10-
describe('without verify on', () => {
10+
describe('railing', () => {
1111
it('find the other peer', function (done) {
12-
this.timeout(20 * 1000)
13-
const r = new Railing(peerList)
12+
this.timeout(5 * 1000)
13+
const r = new Railing({
14+
list: peerList,
15+
interval: 2000
16+
})
17+
1418
r.once('peer', (peer) => done())
1519
r.start(() => {})
1620
})
1721

1822
it('not fail on malformed peers in peer list', function (done) {
19-
this.timeout(20 * 1000)
20-
const r = new Railing(partialValidPeerList)
23+
this.timeout(5 * 1000)
24+
25+
const r = new Railing({
26+
list: partialValidPeerList,
27+
interval: 2000
28+
})
2129

2230
r.start(() => { })
2331

0 commit comments

Comments
 (0)