Skip to content

Commit 66158b4

Browse files
committed
feat: refactor to use async/await
Also upgrades all the deps, moves from `ipfs-api` to `ipfs-http-client` and removes a lot of results massaging that is now done in the http client.
1 parent 5321cf1 commit 66158b4

File tree

5 files changed

+70
-128
lines changed

5 files changed

+70
-128
lines changed

.aegir.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ const server = createServer()
66

77
module.exports = {
88
hooks: {
9-
pre: server.start.bind(server),
10-
post: server.stop.bind(server)
9+
pre: (callback) => {
10+
server.start()
11+
.then(() => callback(), callback)
12+
},
13+
post: (callback) => {
14+
server.stop()
15+
.then(() => callback(), callback)
16+
}
1117
}
1218
}

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const DelegatedPeerRouting = require('libp2p-delegated-routing')
1313
// default is to use ipfs.io
1414
const routing = new DelegatedPeerRouing()
1515

16-
routing.findPeer('peerid', (err, peerInfo) => {
17-
if (err) {
18-
return console.error(err)
19-
}
16+
try {
17+
const peerInfo = await routing.findPeer('peerid')
2018

2119
console.log('found peer details', peerInfo)
22-
})
20+
} catch (err) {
21+
console.error(err)
22+
}
2323
```
2424

2525
## License

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
"coverage": "aegir coverage"
1919
},
2020
"devDependencies": {
21-
"aegir": "^15.2.0",
22-
"async": "^2.6.1",
21+
"aegir": "^19.0.4",
2322
"chai": "^4.2.0",
2423
"go-ipfs-dep": "~0.4.17",
25-
"ipfsd-ctl": "~0.39.2"
24+
"ipfsd-ctl": "ipfs/js-ipfsd-ctl#async-await"
2625
},
2726
"dependencies": {
28-
"ipfs-api": "^24.0.2",
29-
"peer-id": "~0.11.0",
30-
"peer-info": "~0.14.1"
27+
"ipfs-http-client": "^32.0.1",
28+
"peer-id": "~0.12.2"
3129
},
3230
"contributors": [
3331
"David Dias <[email protected]>",

src/index.js

+13-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

3-
const PeerInfo = require('peer-info')
43
const PeerId = require('peer-id')
5-
const dht = require('ipfs-api/src/dht')
6-
const defaultConfig = require('ipfs-api/src/utils/default-config')
4+
const dht = require('ipfs-http-client/src/dht')
5+
const defaultConfig = require('ipfs-http-client/src/utils/default-config')
76

87
const DEFAULT_MAX_TIMEOUT = 30e3 // 30 second default
98
const DEFAULT_IPFS_API = {
@@ -12,10 +11,6 @@ const DEFAULT_IPFS_API = {
1211
host: 'ipfs.io'
1312
}
1413

15-
const peerNotFoundError = (id) => {
16-
return new Error(`Peer "${id}" not found`)
17-
}
18-
1914
class DelegatedPeerRouting {
2015
constructor (api) {
2116
this.api = Object.assign({}, defaultConfig(), DEFAULT_IPFS_API, api)
@@ -28,54 +23,26 @@ class DelegatedPeerRouting {
2823
* @param {PeerID} id
2924
* @param {object} options
3025
* @param {number} options.maxTimeout How long the query can take. Defaults to 30 seconds
31-
* @param {function(Error, PeerInfo)} callback
32-
* @returns {void}
26+
* @returns {Promise}
3327
*/
34-
findPeer (id, options, callback) {
35-
if (typeof options === 'function') {
36-
callback = options
37-
options = {}
38-
} else if (typeof options === 'number') { // This will be deprecated in a next release
39-
options = {
40-
maxTimeout: options
41-
}
42-
} else {
43-
options = options || {}
44-
}
45-
28+
async findPeer (id, options = {}) {
4629
if (PeerId.isPeerId(id)) {
4730
id = id.toB58String()
4831
}
4932

5033
options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT
5134

52-
this.dht.findpeer(id, {
53-
timeout: `${options.maxTimeout}ms`// The api requires specification of the time unit (s/ms)
54-
}, (err, results) => {
55-
if (err) {
56-
return callback(err)
35+
try {
36+
return await this.dht.findPeer(id, {
37+
timeout: `${options.maxTimeout}ms`// The api requires specification of the time unit (s/ms)
38+
})
39+
} catch (err) {
40+
if (err.message.includes('not found')) {
41+
return undefined
5742
}
5843

59-
// cleanup result from ipfs-api
60-
const actual = results.filter((res) => Boolean(res.Responses))
61-
62-
if (actual.length === 0) {
63-
return callback(peerNotFoundError(id))
64-
}
65-
66-
const wantedResponse = actual.find((el) => el.Type === 2)
67-
if (wantedResponse === undefined) {
68-
return callback(peerNotFoundError(id))
69-
}
70-
const details = wantedResponse.Responses[0]
71-
const info = new PeerInfo(
72-
PeerId.createFromB58String(details.ID)
73-
)
74-
details.Addrs.forEach((addr) => info.multiaddrs.add(addr))
75-
76-
// there should be only one of these
77-
callback(null, info)
78-
})
44+
throw err
45+
}
7946
}
8047
}
8148

test/index.spec.js

+40-69
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33

44
const expect = require('chai').expect
55
const IPFSFactory = require('ipfsd-ctl')
6-
const async = require('async')
76
const PeerID = require('peer-id')
87

98
const DelegatedPeerRouting = require('../src')
109
const factory = IPFSFactory.create({ type: 'go' })
1110

12-
function spawnNode (boostrap, callback) {
13-
if (typeof boostrap === 'function') {
14-
callback = boostrap
15-
boostrap = []
16-
}
17-
18-
factory.spawn({
11+
async function spawnNode (boostrap = []) {
12+
const node = await factory.spawn({
1913
// Lock down the nodes so testing can be deterministic
2014
config: {
2115
Bootstrap: boostrap,
@@ -25,15 +19,13 @@ function spawnNode (boostrap, callback) {
2519
}
2620
}
2721
}
28-
}, (err, node) => {
29-
if (err) return callback(err)
30-
31-
node.api.id((err, id) => {
32-
if (err) return callback(err)
33-
34-
callback(null, node, id)
35-
})
3622
})
23+
const id = await node.api.id()
24+
25+
return {
26+
node,
27+
id
28+
}
3729
}
3830

3931
describe('DelegatedPeerRouting', function () {
@@ -45,37 +37,28 @@ describe('DelegatedPeerRouting', function () {
4537
let bootstrapNode
4638
let bootstrapId
4739

48-
before((done) => {
49-
async.waterfall([
50-
// Spawn a "Boostrap" node that doesnt connect to anything
51-
(cb) => spawnNode(cb),
52-
(ipfsd, id, cb) => {
53-
bootstrapNode = ipfsd
54-
bootstrapId = id
55-
cb()
56-
},
57-
// Spawn our local node and bootstrap the bootstrapper node
58-
(cb) => spawnNode(bootstrapId.addresses, cb),
59-
(ipfsd, id, cb) => {
60-
nodeToFind = ipfsd
61-
peerIdToFind = id
62-
cb()
63-
},
64-
// Spawn the delegate node and bootstrap the bootstrapper node
65-
(cb) => spawnNode(bootstrapId.addresses, cb),
66-
(ipfsd, id, cb) => {
67-
delegatedNode = ipfsd
68-
cb()
69-
}
70-
], done)
40+
before(async () => {
41+
// Spawn a "Boostrap" node that doesnt connect to anything
42+
const bootstrap = await spawnNode()
43+
bootstrapNode = bootstrap.node
44+
bootstrapId = bootstrap.id
45+
46+
// Spawn our local node and bootstrap the bootstrapper node
47+
const local = await spawnNode(bootstrapId.addresses)
48+
nodeToFind = local.node
49+
peerIdToFind = local.id
50+
51+
// Spawn the delegate node and bootstrap the bootstrapper node
52+
const delegate = await spawnNode(bootstrapId.addresses)
53+
delegatedNode = delegate.node
7154
})
7255

73-
after((done) => {
74-
async.parallel([
75-
(cb) => nodeToFind.stop(cb),
76-
(cb) => delegatedNode.stop(cb),
77-
(cb) => bootstrapNode.stop(cb)
78-
], done)
56+
after(() => {
57+
return Promise.all([
58+
nodeToFind.stop(),
59+
delegatedNode.stop(),
60+
bootstrapNode.stop()
61+
])
7962
})
8063

8164
describe('create', () => {
@@ -117,52 +100,43 @@ describe('DelegatedPeerRouting', function () {
117100
})
118101

119102
describe('findPeers', () => {
120-
it('should be able to find peers via the delegate with a peer id string', (done) => {
103+
it('should be able to find peers via the delegate with a peer id string', async () => {
121104
const opts = delegatedNode.apiAddr.toOptions()
122105
const router = new DelegatedPeerRouting({
123106
protocol: 'http',
124107
port: opts.port,
125108
host: opts.host
126109
})
127110

128-
router.findPeer(peerIdToFind.id, (err, peer) => {
129-
expect(err).to.equal(null)
130-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
131-
done()
132-
})
111+
const peer = await router.findPeer(peerIdToFind.id)
112+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
133113
})
134114

135-
it('should be able to find peers via the delegate with a peerid', (done) => {
115+
it('should be able to find peers via the delegate with a peerid', async () => {
136116
const opts = delegatedNode.apiAddr.toOptions()
137117
const router = new DelegatedPeerRouting({
138118
protocol: 'http',
139119
port: opts.port,
140120
host: opts.host
141121
})
142122

143-
router.findPeer(PeerID.createFromB58String(peerIdToFind.id), (err, peer) => {
144-
expect(err).to.equal(null)
145-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
146-
done()
147-
})
123+
const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
124+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
148125
})
149126

150-
it('should be able to specify a maxTimeout', (done) => {
127+
it('should be able to specify a maxTimeout', async () => {
151128
const opts = delegatedNode.apiAddr.toOptions()
152129
const router = new DelegatedPeerRouting({
153130
protocol: 'http',
154131
port: opts.port,
155132
host: opts.host
156133
})
157134

158-
router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { maxTimeout: 2000 }, (err, peer) => {
159-
expect(err).to.equal(null)
160-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
161-
done()
162-
})
135+
const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { maxTimeout: 2000 })
136+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
163137
})
164138

165-
it('should not be able to find peers not on the network', (done) => {
139+
it('should not be able to find peers not on the network', async () => {
166140
const opts = delegatedNode.apiAddr.toOptions()
167141
const router = new DelegatedPeerRouting({
168142
protocol: 'http',
@@ -172,11 +146,8 @@ describe('DelegatedPeerRouting', function () {
172146

173147
// This is one of the default Bootstrap nodes, but we're not connected to it
174148
// so we'll test with it.
175-
router.findPeer('QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', (err, peer) => {
176-
expect(err).to.be.an('error')
177-
expect(peer).to.eql(undefined)
178-
done()
179-
})
149+
const peer = await router.findPeer('QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64')
150+
expect(peer).to.eql(undefined)
180151
})
181152
})
182153
})

0 commit comments

Comments
 (0)