Skip to content
This repository was 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 1 commit
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
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 13 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
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
})
})