From 75acfc998bed5a0e4aafc785186aced40733e8c7 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Wed, 13 Mar 2019 11:30:11 -0400 Subject: [PATCH 1/7] refactor: callbacks -> async/await BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await --- package.json | 8 +++----- src/index.js | 28 +++++++++++++--------------- test/bootstrap.spec.js | 27 ++++++++++++++++----------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 42fa8b4..3753975 100644 --- a/package.json +++ b/package.json @@ -29,16 +29,14 @@ }, "devDependencies": { "aegir": "^18.1.1", - "chai": "^4.2.0", - "libp2p-tcp": "~0.13.0" + "chai": "^4.2.0" }, "dependencies": { - "async": "^2.6.2", "debug": "^4.1.1", "mafmt": "^6.0.6", "multiaddr": "^6.0.4", - "peer-id": "~0.12.2", - "peer-info": "~0.15.1" + "peer-id": "libp2p/js-peer-id#feat/async-await", + "peer-info": "libp2p/js-peer-info#feat/async-await" }, "pre-push": [ "lint", diff --git a/src/index.js b/src/index.js index b692399..7631ecf 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,6 @@ const multiaddr = require('multiaddr') const mafmt = require('mafmt') const EventEmitter = require('events').EventEmitter const debug = require('debug') -const nextTick = require('async/nextTick') const log = debug('libp2p:bootstrap') log.error = debug('libp2p:bootstrap:error') @@ -27,38 +26,37 @@ class Bootstrap extends EventEmitter { this._timer = null } - start (callback) { + start () { if (this._timer) { - return nextTick(() => callback()) + return } this._timer = setInterval(() => this._discoverBootstrapPeers(), this._interval) - nextTick(() => { - callback() - this._discoverBootstrapPeers() - }) + this._discoverBootstrapPeers() } _discoverBootstrapPeers () { - this._list.forEach((candidate) => { - if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') } + this._list.forEach(async (candidate) => { + if (!isIPFS(candidate)) { + return log.error('Invalid multiaddr') + } const ma = multiaddr(candidate) const peerId = PeerId.createFromB58String(ma.getPeerId()) - PeerInfo.create(peerId, (err, peerInfo) => { - if (err) { return log.error('Invalid bootstrap peer id', err) } + try { + const peerInfo = await PeerInfo.create(peerId) peerInfo.multiaddrs.add(ma) this.emit('peer', peerInfo) - }) + } catch (err) { + log.error('Invalid bootstrap peer id', err) + } }) } - stop (callback) { - nextTick(callback) - + stop () { if (this._timer) { clearInterval(this._timer) this._timer = null diff --git a/test/bootstrap.spec.js b/test/bootstrap.spec.js index c7397f8..b1a039e 100644 --- a/test/bootstrap.spec.js +++ b/test/bootstrap.spec.js @@ -8,18 +8,19 @@ const { expect } = require('chai') const mafmt = require('mafmt') describe('bootstrap', () => { - it('find the other peer', function (done) { + it('find the other peer', function () { this.timeout(5 * 1000) const r = new Bootstrap({ list: peerList, interval: 2000 }) - r.once('peer', (peer) => done()) - r.start(() => {}) + const p = new Promise((resolve) => r.once('peer', resolve)) + r.start() + return p }) - it('not fail on malformed peers in peer list', function (done) { + it('not fail on malformed peers in peer list', function () { this.timeout(5 * 1000) const r = new Bootstrap({ @@ -27,13 +28,17 @@ describe('bootstrap', () => { interval: 2000 }) - r.start(() => { }) - - r.on('peer', (peer) => { - const peerList = peer.multiaddrs.toArray() - expect(peerList.length).to.eq(1) - expect(mafmt.IPFS.matches(peerList[0].toString())) - done() + const p = new Promise((resolve) => { + r.on('peer', (peer) => { + const peerList = peer.multiaddrs.toArray() + expect(peerList.length).to.eq(1) + expect(mafmt.IPFS.matches(peerList[0].toString())) + resolve() + }) }) + + r.start() + + return p }) }) From 482fc849fd07ddd0a306a157909d9ca0b3d9f3a0 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Tue, 26 Mar 2019 21:52:38 +0000 Subject: [PATCH 2/7] chore: add documentation --- src/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/index.js b/src/index.js index 7631ecf..809b6d6 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,12 @@ const debug = require('debug') const log = debug('libp2p:bootstrap') log.error = debug('libp2p:bootstrap:error') +/** + * Indicates if an address is in IPFS multi-address format. + * + * @param {string} addr + * @returns {boolean} + */ function isIPFS (addr) { try { return mafmt.IPFS.matches(addr) @@ -18,7 +24,18 @@ function isIPFS (addr) { } } +/** + * Emits 'peer' events on a regular interval for each peer in the provided list. + */ class Bootstrap extends EventEmitter { + /** + * Constructs a new Bootstrap. + * + * @param {Object} options + * @param {Array} options.list - the list of peer addresses in multi-address format + * @param {number} options.interval - the interval between emitting addresses + * + */ constructor (options) { super() this._list = options.list @@ -26,6 +43,9 @@ class Bootstrap extends EventEmitter { this._timer = null } + /** + * Start emitting events. + */ start () { if (this._timer) { return @@ -36,6 +56,9 @@ class Bootstrap extends EventEmitter { this._discoverBootstrapPeers() } + /** + * Emit each address in the list as a PeerInfo. + */ _discoverBootstrapPeers () { this._list.forEach(async (candidate) => { if (!isIPFS(candidate)) { @@ -56,6 +79,9 @@ class Bootstrap extends EventEmitter { }) } + /** + * Stop emitting events. + */ stop () { if (this._timer) { clearInterval(this._timer) From ce85f146574779b7e4b4d73badaedd1342de784e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 15 Jul 2019 09:07:34 +0100 Subject: [PATCH 3/7] chore: update peer-id and peer-info deps --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3753975..4dfadca 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,8 @@ "debug": "^4.1.1", "mafmt": "^6.0.6", "multiaddr": "^6.0.4", - "peer-id": "libp2p/js-peer-id#feat/async-await", - "peer-info": "libp2p/js-peer-info#feat/async-await" + "peer-id": "^0.13.2", + "peer-info": "^0.16.0" }, "pre-push": [ "lint", From a6297d09c16e27824379897a9d8086ca4d3b431d Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 15 Jul 2019 09:13:50 +0100 Subject: [PATCH 4/7] fix: json syntax err --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 606841f..e635365 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ }, "devDependencies": { "aegir": "^18.2.1", - "chai": "^4.2.0", + "chai": "^4.2.0" }, "dependencies": { "debug": "^4.1.1", From b0f0c7540bac3fad1d01b22e5e280814082ce76c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 15 Jul 2019 09:25:45 +0100 Subject: [PATCH 5/7] chore: update aegir dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e635365..0165a2c 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "npm": ">=3.0.0" }, "devDependencies": { - "aegir": "^18.2.1", + "aegir": "^20.0.0", "chai": "^4.2.0" }, "dependencies": { From 002b36c8efb9e9bacf6558c5c19508c05d7d309f Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Mon, 15 Jul 2019 08:30:26 -0400 Subject: [PATCH 6/7] test: add start / stop test --- src/index.js | 16 +--------------- test/bootstrap.spec.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 809b6d6..7566d9c 100644 --- a/src/index.js +++ b/src/index.js @@ -10,20 +10,6 @@ const debug = require('debug') const log = debug('libp2p:bootstrap') log.error = debug('libp2p:bootstrap:error') -/** - * Indicates if an address is in IPFS multi-address format. - * - * @param {string} addr - * @returns {boolean} - */ -function isIPFS (addr) { - try { - return mafmt.IPFS.matches(addr) - } catch (e) { - return false - } -} - /** * Emits 'peer' events on a regular interval for each peer in the provided list. */ @@ -61,7 +47,7 @@ class Bootstrap extends EventEmitter { */ _discoverBootstrapPeers () { this._list.forEach(async (candidate) => { - if (!isIPFS(candidate)) { + if (!mafmt.IPFS.matches(candidate)) { return log.error('Invalid multiaddr') } diff --git a/test/bootstrap.spec.js b/test/bootstrap.spec.js index b1a039e..e529f64 100644 --- a/test/bootstrap.spec.js +++ b/test/bootstrap.spec.js @@ -29,10 +29,10 @@ describe('bootstrap', () => { }) const p = new Promise((resolve) => { - r.on('peer', (peer) => { + r.once('peer', (peer) => { const peerList = peer.multiaddrs.toArray() expect(peerList.length).to.eq(1) - expect(mafmt.IPFS.matches(peerList[0].toString())) + expect(mafmt.IPFS.matches(peerList[0].toString())).equals(true) resolve() }) }) @@ -41,4 +41,28 @@ describe('bootstrap', () => { return p }) + + it('stop emitting events when stop() called', async function () { + const interval = 100 + const r = new Bootstrap({ + list: peerList, + interval + }) + + let emitted = [] + r.on('peer', p => emitted.push(p)) + + // Should fire emit event for each peer in list on start, + // so wait 50 milliseconds then check + const p = new Promise((resolve) => setTimeout(resolve, 50)) + r.start() + await p + expect(emitted).to.have.length(peerList.length) + + // After stop is called, no more peers should be emitted + emitted = [] + r.stop() + await new Promise((resolve) => setTimeout(resolve, interval)) + expect(emitted).to.have.length(0) + }) }) From a15b6c1b290384b337f023d3a8cf40d26de72d63 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Mon, 15 Jul 2019 09:19:37 -0400 Subject: [PATCH 7/7] docs: note on interval unit --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7566d9c..16a1dd4 100644 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,7 @@ class Bootstrap extends EventEmitter { * * @param {Object} options * @param {Array} options.list - the list of peer addresses in multi-address format - * @param {number} options.interval - the interval between emitting addresses + * @param {number} options.interval - the interval between emitting addresses (in milli-seconds) * */ constructor (options) {