Skip to content

Commit

Permalink
feat: refactor to use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jul 5, 2019
1 parent d6455bc commit 5ba7dfa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 201 deletions.
6 changes: 4 additions & 2 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const server = createServer()

module.exports = {
hooks: {
pre: server.start.bind(server),
post: server.stop.bind(server)
browser: {
pre: () => server.start(),
post: () => server.stop()
}
}
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
"coverage": "aegir coverage"
},
"devDependencies": {
"aegir": "^15.2.0",
"aegir": "^19.0.5",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"cids": "~0.5.5",
"cids": "^0.7.1",
"go-ipfs-dep": "~0.4.17",
"ipfsd-ctl": "~0.39.2"
"ipfsd-ctl": "~0.44.0"
},
"dependencies": {
"async": "^2.6.1",
"ipfs-api": "^24.0.2",
"multiaddr": "^5.0.0",
"peer-id": "~0.11.0",
"peer-info": "~0.14.1"
"ipfs": "^0.36.4",
"ipfs-http-client": "^32.0.1",
"multiaddr": "^6.1.0",
"peer-id": "^0.12.2",
"peer-info": "^0.15.1"
},
"contributors": [
"David Dias <[email protected]>",
Expand Down
92 changes: 28 additions & 64 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

const PeerInfo = require('peer-info')
const PeerID = require('peer-id')
const dht = require('ipfs-api/src/dht')
const swarm = require('ipfs-api/src/swarm')
const refs = require('ipfs-api/src/refs')
const defaultConfig = require('ipfs-api/src/utils/default-config')
const series = require('async/series')
const parallel = require('async/parallel')
const reflect = require('async/reflect')
const dht = require('ipfs-http-client/src/dht')
const swarm = require('ipfs-http-client/src/swarm')
const refs = require('ipfs-http-client/src/files-regular/refs')
const defaultConfig = require('ipfs-http-client/src/utils/default-config')
const multiaddr = require('multiaddr')

const DEFAULT_MAX_TIMEOUT = 30e3 // 30 second default
Expand Down Expand Up @@ -62,48 +59,18 @@ class DelegatedContentRouting {
* @param {CID} key
* @param {object} options
* @param {number} options.maxTimeout How long the query can take. Defaults to 30 seconds
* @param {function(Error, Array<PeerInfo>)} callback
* @returns {void}
* @returns {AsyncIterable<PeerInfo>}
*/
findProviders (key, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
} else if (typeof options === 'number') { // This will be deprecated in a next release
options = {
maxTimeout: options
}
} else {
options = options || {}
}

async * findProviders (key, options = {}) {
options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT

this.dht.findprovs(key.toBaseEncodedString(), {
const results = await this.dht.findProvs(key.toBaseEncodedString(), {
timeout: `${options.maxTimeout}ms` // The api requires specification of the time unit (s/ms)
}, (err, results) => {
if (err) {
return callback(err)
}

// cleanup result from ipfs-api
const infos = []
results
.filter((res) => Boolean(res.Responses))
.forEach((res) => {
res.Responses.forEach((raw) => {
const info = new PeerInfo(
PeerID.createFromB58String(raw.ID)
)
if (raw.Addrs) {
raw.Addrs.forEach((addr) => info.multiaddrs.add(addr))
}
infos.push(info)
})
})

callback(null, infos)
})

for (let i = 0; i < results.length; i++) {
yield results[i]
}
}

/**
Expand All @@ -115,32 +82,29 @@ class DelegatedContentRouting {
*
* @param {CID} key
* @param {function(Error)} callback
* @returns {void}
* @returns {Promise}
*/
provide (key, callback) {
async provide (key) {
const addrs = this.bootstrappers.map((addr) => {
return addr.encapsulate(`/p2p-circuit/ipfs/${this.peerId.toB58String()}`)
})

series([
(cb) => parallel(addrs.map((addr) => {
return reflect((cb) => this.swarm.connect(addr.toString(), cb))
}), (err, results) => {
if (err) {
return cb(err)
}
const results = await Promise.all(
addrs.map((addr) => {
return this.swarm.connect(addr.toString()).catch(() => {})
})
)

// only some need to succeed
const success = results.filter((res) => res && res.error == null)

// only some need to succeed
const success = results.filter((res) => res.error == null)
if (success.length === 0) {
return cb(new Error('unable to swarm.connect using p2p-circuit'))
}
cb()
}),
(cb) => {
this.refs(key.toBaseEncodedString(), {recursive: true}, cb)
}
], (err) => callback(err))
if (success.length === 0) {
throw new Error('unable to swarm.connect using p2p-circuit')
}

return this.refs(key.toBaseEncodedString(), {
recursive: true
})
}
}

Expand Down
Loading

0 comments on commit 5ba7dfa

Please sign in to comment.