From 5617a315e0a2b5313ff53bfab3cf5718b26132a4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 19 Mar 2018 18:46:03 -0700 Subject: [PATCH] feat: (BREAKING CHANGE) constructor takes options. + add tag, update deps and fix tests --- package.json | 8 ++++---- src/index.js | 42 +++++++++++++++++++++++------------------- test/railing.spec.js | 18 +++++++++++++----- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index a6035fa..e347777 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.js b/src/index.js index 76bf5b7..f726cd5 100644 --- a/src/index.js +++ b/src/index.js @@ -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()) @@ -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' diff --git a/test/railing.spec.js b/test/railing.spec.js index 0972dc2..c0aa585 100644 --- a/test/railing.spec.js +++ b/test/railing.spec.js @@ -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(() => { })