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

refactor: callbacks -> async/await #89

Merged
merged 8 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
"devDependencies": {
"aegir": "^18.2.1",
"chai": "^4.2.0",
"libp2p-tcp": "~0.13.0"
},
"dependencies": {
"async": "^2.6.2",
"debug": "^4.1.1",
"mafmt": "^6.0.7",
"multiaddr": "^6.0.6",
"peer-id": "~0.12.2",
"peer-info": "~0.15.1"
"peer-id": "^0.13.2",
"peer-info": "^0.16.0"
},
"pre-push": [
"lint",
Expand Down
54 changes: 39 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ 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')

/**
* Indicates if an address is in IPFS multi-address format.
*
* @param {string} addr
* @returns {boolean}
*/
function isIPFS (addr) {
try {
return mafmt.IPFS.matches(addr)
Expand All @@ -19,46 +24,65 @@ function isIPFS (addr) {
}
}
dirkmc marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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<string>} options.list - the list of peer addresses in multi-address format
* @param {number} options.interval - the interval between emitting addresses
dirkmc marked this conversation as resolved.
Show resolved Hide resolved
*
*/
constructor (options) {
super()
this._list = options.list
this._interval = options.interval || 10000
this._timer = null
}

start (callback) {
/**
* Start emitting events.
*/
start () {
dirkmc marked this conversation as resolved.
Show resolved Hide resolved
if (this._timer) {
return nextTick(() => callback())
return
}

this._timer = setInterval(() => this._discoverBootstrapPeers(), this._interval)

nextTick(() => {
callback()
this._discoverBootstrapPeers()
})
this._discoverBootstrapPeers()
}

/**
* Emit each address in the list as a PeerInfo.
*/
_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 emitting events.
*/
stop () {
if (this._timer) {
clearInterval(this._timer)
this._timer = null
Expand Down
27 changes: 16 additions & 11 deletions test/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@ 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({
list: partialValidPeerList,
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
})
})