Skip to content

Commit 5ba7dfa

Browse files
committed
feat: refactor to use async/await
1 parent d6455bc commit 5ba7dfa

File tree

4 files changed

+123
-201
lines changed

4 files changed

+123
-201
lines changed

.aegir.js

Lines changed: 4 additions & 2 deletions
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
}

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
"coverage": "aegir coverage"
1919
},
2020
"devDependencies": {
21-
"aegir": "^15.2.0",
21+
"aegir": "^19.0.5",
22+
"async-iterator-all": "^1.0.0",
2223
"chai": "^4.2.0",
23-
"cids": "~0.5.5",
24+
"cids": "^0.7.1",
2425
"go-ipfs-dep": "~0.4.17",
25-
"ipfsd-ctl": "~0.39.2"
26+
"ipfsd-ctl": "~0.44.0"
2627
},
2728
"dependencies": {
28-
"async": "^2.6.1",
29-
"ipfs-api": "^24.0.2",
30-
"multiaddr": "^5.0.0",
31-
"peer-id": "~0.11.0",
32-
"peer-info": "~0.14.1"
29+
"ipfs": "^0.36.4",
30+
"ipfs-http-client": "^32.0.1",
31+
"multiaddr": "^6.1.0",
32+
"peer-id": "^0.12.2",
33+
"peer-info": "^0.15.1"
3334
},
3435
"contributors": [
3536
"David Dias <[email protected]>",

src/index.js

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
const PeerInfo = require('peer-info')
44
const PeerID = require('peer-id')
5-
const dht = require('ipfs-api/src/dht')
6-
const swarm = require('ipfs-api/src/swarm')
7-
const refs = require('ipfs-api/src/refs')
8-
const defaultConfig = require('ipfs-api/src/utils/default-config')
9-
const series = require('async/series')
10-
const parallel = require('async/parallel')
11-
const reflect = require('async/reflect')
5+
const dht = require('ipfs-http-client/src/dht')
6+
const swarm = require('ipfs-http-client/src/swarm')
7+
const refs = require('ipfs-http-client/src/files-regular/refs')
8+
const defaultConfig = require('ipfs-http-client/src/utils/default-config')
129
const multiaddr = require('multiaddr')
1310

1411
const DEFAULT_MAX_TIMEOUT = 30e3 // 30 second default
@@ -62,48 +59,18 @@ class DelegatedContentRouting {
6259
* @param {CID} key
6360
* @param {object} options
6461
* @param {number} options.maxTimeout How long the query can take. Defaults to 30 seconds
65-
* @param {function(Error, Array<PeerInfo>)} callback
66-
* @returns {void}
62+
* @returns {AsyncIterable<PeerInfo>}
6763
*/
68-
findProviders (key, options, callback) {
69-
if (typeof options === 'function') {
70-
callback = options
71-
options = {}
72-
} else if (typeof options === 'number') { // This will be deprecated in a next release
73-
options = {
74-
maxTimeout: options
75-
}
76-
} else {
77-
options = options || {}
78-
}
79-
64+
async * findProviders (key, options = {}) {
8065
options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT
8166

82-
this.dht.findprovs(key.toBaseEncodedString(), {
67+
const results = await this.dht.findProvs(key.toBaseEncodedString(), {
8368
timeout: `${options.maxTimeout}ms` // The api requires specification of the time unit (s/ms)
84-
}, (err, results) => {
85-
if (err) {
86-
return callback(err)
87-
}
88-
89-
// cleanup result from ipfs-api
90-
const infos = []
91-
results
92-
.filter((res) => Boolean(res.Responses))
93-
.forEach((res) => {
94-
res.Responses.forEach((raw) => {
95-
const info = new PeerInfo(
96-
PeerID.createFromB58String(raw.ID)
97-
)
98-
if (raw.Addrs) {
99-
raw.Addrs.forEach((addr) => info.multiaddrs.add(addr))
100-
}
101-
infos.push(info)
102-
})
103-
})
104-
105-
callback(null, infos)
10669
})
70+
71+
for (let i = 0; i < results.length; i++) {
72+
yield results[i]
73+
}
10774
}
10875

10976
/**
@@ -115,32 +82,29 @@ class DelegatedContentRouting {
11582
*
11683
* @param {CID} key
11784
* @param {function(Error)} callback
118-
* @returns {void}
85+
* @returns {Promise}
11986
*/
120-
provide (key, callback) {
87+
async provide (key) {
12188
const addrs = this.bootstrappers.map((addr) => {
12289
return addr.encapsulate(`/p2p-circuit/ipfs/${this.peerId.toB58String()}`)
12390
})
12491

125-
series([
126-
(cb) => parallel(addrs.map((addr) => {
127-
return reflect((cb) => this.swarm.connect(addr.toString(), cb))
128-
}), (err, results) => {
129-
if (err) {
130-
return cb(err)
131-
}
92+
const results = await Promise.all(
93+
addrs.map((addr) => {
94+
return this.swarm.connect(addr.toString()).catch(() => {})
95+
})
96+
)
97+
98+
// only some need to succeed
99+
const success = results.filter((res) => res && res.error == null)
132100

133-
// only some need to succeed
134-
const success = results.filter((res) => res.error == null)
135-
if (success.length === 0) {
136-
return cb(new Error('unable to swarm.connect using p2p-circuit'))
137-
}
138-
cb()
139-
}),
140-
(cb) => {
141-
this.refs(key.toBaseEncodedString(), {recursive: true}, cb)
142-
}
143-
], (err) => callback(err))
101+
if (success.length === 0) {
102+
throw new Error('unable to swarm.connect using p2p-circuit')
103+
}
104+
105+
return this.refs(key.toBaseEncodedString(), {
106+
recursive: true
107+
})
144108
}
145109
}
146110

0 commit comments

Comments
 (0)