Skip to content

Commit 1827328

Browse files
achingbrainjacobheun
authored andcommitted
feat: refactor to use async/await (#8)
BREAKING CHANGE: API refactored 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 92c8e8b commit 1827328

File tree

5 files changed

+68
-110
lines changed

5 files changed

+68
-110
lines changed

.aegir.js

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

77
module.exports = {
88
hooks: {
9-
pre: server.start.bind(server),
10-
post: server.stop.bind(server)
9+
browser: {
10+
pre: () => server.start(),
11+
post: () => server.stop()
12+
}
1113
}
1214
}

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
"coverage": "aegir coverage"
1919
},
2020
"devDependencies": {
21-
"aegir": "^19.0.5",
22-
"async": "^3.1.0",
21+
"aegir": "^19.0.4",
2322
"chai": "^4.2.0",
2423
"dirty-chai": "^2.0.1",
25-
"go-ipfs-dep": "^0.4.21",
26-
"ipfsd-ctl": "^0.43.0"
24+
"go-ipfs-dep": "~0.4.17",
25+
"ipfsd-ctl": "^0.44.1"
2726
},
2827
"dependencies": {
29-
"ipfs-http-client": "^33.0.1",
30-
"peer-id": "^0.12.2",
31-
"peer-info": "^0.15.1"
28+
"ipfs-http-client": "^33.1.0",
29+
"peer-id": "~0.12.2"
3230
},
3331
"contributors": [
3432
"Alan Shaw <[email protected]>",

src/index.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,26 @@ class DelegatedPeerRouting {
2323
* @param {PeerID} id
2424
* @param {object} options
2525
* @param {number} options.maxTimeout How long the query can take. Defaults to 30 seconds
26-
* @param {function(Error, PeerInfo)} callback
27-
* @returns {void}
26+
* @returns {Promise<PeerInfo>}
2827
*/
29-
findPeer (id, options, callback) {
30-
if (typeof options === 'function') {
31-
callback = options
32-
options = {}
33-
} else if (typeof options === 'number') { // This will be deprecated in a next release
34-
options = {
35-
maxTimeout: options
36-
}
37-
} else {
38-
options = options || {}
39-
}
40-
28+
async findPeer (id, options = {}) {
4129
if (PeerId.isPeerId(id)) {
4230
id = id.toB58String()
4331
}
4432

4533
options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT
4634

47-
this.dht.findPeer(id, {
48-
timeout: `${options.maxTimeout}ms`// The api requires specification of the time unit (s/ms)
49-
}, (err, info) => {
50-
if (err) {
51-
if (err.message.includes('not found')) {
52-
return callback()
53-
}
54-
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
5542
}
5643

57-
callback(null, info)
58-
})
44+
throw err
45+
}
5946
}
6047
}
6148

test/index.spec.js

+43-72
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ const chai = require('chai')
55
const { expect } = chai
66
chai.use(require('dirty-chai'))
77
const IPFSFactory = require('ipfsd-ctl')
8-
const async = require('async')
98
const PeerID = require('peer-id')
109

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

14-
function spawnNode (boostrap, callback) {
15-
if (typeof boostrap === 'function') {
16-
callback = boostrap
17-
boostrap = []
18-
}
19-
20-
factory.spawn({
13+
async function spawnNode (boostrap = []) {
14+
const node = await factory.spawn({
2115
// Lock down the nodes so testing can be deterministic
2216
config: {
2317
Bootstrap: boostrap,
@@ -27,15 +21,13 @@ function spawnNode (boostrap, callback) {
2721
}
2822
}
2923
}
30-
}, (err, node) => {
31-
if (err) return callback(err)
32-
33-
node.api.id((err, id) => {
34-
if (err) return callback(err)
35-
36-
callback(null, node, id)
37-
})
3824
})
25+
const id = await node.api.id()
26+
27+
return {
28+
node,
29+
id
30+
}
3931
}
4032

4133
describe('DelegatedPeerRouting', function () {
@@ -47,37 +39,28 @@ describe('DelegatedPeerRouting', function () {
4739
let bootstrapNode
4840
let bootstrapId
4941

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

75-
after((done) => {
76-
async.parallel([
77-
(cb) => nodeToFind.stop(cb),
78-
(cb) => delegatedNode.stop(cb),
79-
(cb) => bootstrapNode.stop(cb)
80-
], done)
58+
after(() => {
59+
return Promise.all([
60+
nodeToFind.stop(),
61+
delegatedNode.stop(),
62+
bootstrapNode.stop()
63+
])
8164
})
8265

8366
describe('create', () => {
@@ -119,55 +102,46 @@ describe('DelegatedPeerRouting', function () {
119102
})
120103

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

130-
router.findPeer(peerIdToFind.id, (err, peer) => {
131-
expect(err).to.not.exist()
132-
expect(peer).to.exist()
133-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
134-
done()
135-
})
113+
const peer = await router.findPeer(peerIdToFind.id)
114+
expect(peer).to.exist()
115+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
136116
})
137117

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

146-
router.findPeer(PeerID.createFromB58String(peerIdToFind.id), (err, peer) => {
147-
expect(err).to.not.exist()
148-
expect(peer).to.exist()
149-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
150-
done()
151-
})
126+
const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
127+
expect(peer).to.exist()
128+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
152129
})
153130

154-
it('should be able to specify a maxTimeout', (done) => {
131+
it('should be able to specify a maxTimeout', async () => {
155132
const opts = delegatedNode.apiAddr.toOptions()
156133
const router = new DelegatedPeerRouting({
157134
protocol: 'http',
158135
port: opts.port,
159136
host: opts.host
160137
})
161138

162-
router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { maxTimeout: 2000 }, (err, peer) => {
163-
expect(err).to.not.exist()
164-
expect(peer).to.exist()
165-
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
166-
done()
167-
})
139+
const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { maxTimeout: 2000 })
140+
expect(peer).to.exist()
141+
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
168142
})
169143

170-
it('should not be able to find peers not on the network', (done) => {
144+
it('should not be able to find peers not on the network', async () => {
171145
const opts = delegatedNode.apiAddr.toOptions()
172146
const router = new DelegatedPeerRouting({
173147
protocol: 'http',
@@ -177,11 +151,8 @@ describe('DelegatedPeerRouting', function () {
177151

178152
// This is one of the default Bootstrap nodes, but we're not connected to it
179153
// so we'll test with it.
180-
router.findPeer('QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', (err, peer) => {
181-
expect(err).to.not.exist()
182-
expect(peer).to.not.exist()
183-
done()
184-
})
154+
const peer = await router.findPeer('QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64')
155+
expect(peer).to.not.exist()
185156
})
186157
})
187158
})

0 commit comments

Comments
 (0)