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

Commit

Permalink
feat: (BREAKING CHANGE) constructor takes options. + add tag, update …
Browse files Browse the repository at this point in the history
…deps and fix tests
  • Loading branch information
daviddias committed Jun 5, 2018
1 parent 00529f6 commit 27f9aed
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
},
"homepage": "https://github.com/libp2p/js-ipfs-railing",
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^14.0.0",
"chai": "^4.1.2",
"libp2p-tcp": "~0.12.0",
"pre-commit": "^1.2.2"
},
"dependencies": {
"async": "^2.6.0",
"async": "^2.6.1",
"debug": "^3.1.0",
"lodash": "^4.17.5",
"multiaddr": "^4.0.0",
"lodash": "^4.17.10",
"multiaddr": "^5.0.0",
"mafmt": "^6.0.0",
"peer-id": "~0.10.7",
"peer-info": "~0.14.1"
Expand Down
42 changes: 23 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ const setImmediate = require('async/setImmediate')
const log = debug('libp2p:railing')
log.error = debug('libp2p:railing:error')

function isIPFS (addr) {
try {
return mafmt.IPFS.matches(addr)
} catch (e) {
return false
}
}

class Railing extends EventEmitter {
constructor (bootstrapers) {
constructor (options) {
super()
this.bootstrapers = bootstrapers
this.interval = null
this._list = options.list
this._interval = options.interval || 10000
this._timer = null
}

start (callback) {
setImmediate(() => callback())

if (this.interval) { return }
if (this._timer) { return }

this.interval = setInterval(() => {
this.bootstrapers.forEach((candidate) => {
this._timer = setInterval(() => {
this._list.forEach((candidate) => {
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }

const ma = multiaddr(candidate)

const peerId = PeerId.createFromB58String(ma.getPeerId())
Expand All @@ -36,24 +46,18 @@ class Railing extends EventEmitter {
this.emit('peer', peerInfo)
})
})
}, 10000)
}, this._interval)
}

stop (callback) {
setImmediate(callback)
if (this.interval) {
clearInterval(this.interval)
this.interval = null
}
}
}

function isIPFS (addr) {
try {
return mafmt.IPFS.matches(addr)
} catch (e) {
return false
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
}
}

module.exports = Railing
exports = module.exports = Railing
exports.tag = 'bootstrap'
18 changes: 13 additions & 5 deletions test/railing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ const partialValidPeerList = require('./some-invalid-peers')
const {expect} = require('chai')
const mafmt = require('mafmt')

describe('without verify on', () => {
describe('railing', () => {
it('find the other peer', function (done) {
this.timeout(20 * 1000)
const r = new Railing(peerList)
this.timeout(5 * 1000)
const r = new Railing({
list: peerList,
interval: 2000
})

r.once('peer', (peer) => done())
r.start(() => {})
})

it('not fail on malformed peers in peer list', function (done) {
this.timeout(20 * 1000)
const r = new Railing(partialValidPeerList)
this.timeout(5 * 1000)

const r = new Railing({
list: partialValidPeerList,
interval: 2000
})

r.start(() => { })

Expand Down

0 comments on commit 27f9aed

Please sign in to comment.