Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: find peer and providers options (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Sep 27, 2018
1 parent 6726453 commit bba7500
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"lint": "aegir lint",
"test": "aegir test -t node",
"test:node": "aegir test -t node",
"build": "aegir build",
"docs": "aegir docs",
"release": "aegir release --docs -t node",
Expand Down
46 changes: 30 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,22 +443,29 @@ class KadDHT {
* Search the dht for up to `K` providers of the given CID.
*
* @param {CID} key
* @param {number} timeout - how long the query should maximally run, in milliseconds.
* @param {Object} options - findProviders options
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
* @param {function(Error, Array<PeerInfo>)} callback
* @returns {void}
*/
findProviders (key, timeout, callback) {
if (typeof timeout === 'function') {
callback = timeout
timeout = null
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 || {}
}

if (timeout == null) {
timeout = c.minute
if (!options.maxTimeout) {
options.maxTimeout = c.minute
}

this._log('findProviders %s', key.toBaseEncodedString())
this._findNProviders(key, timeout, c.K, callback)
this._findNProviders(key, options.maxTimeout, c.K, callback)
}

// ----------- Peer Routing
Expand All @@ -467,18 +474,25 @@ class KadDHT {
* Search for a peer with the given ID.
*
* @param {PeerId} id
* @param {number} [maxTimeout=60000]
* @param {Object} options - findPeer options
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
* @param {function(Error, PeerInfo)} callback
* @returns {void}
*/
findPeer (id, maxTimeout, callback) {
if (typeof maxTimeout === 'function') {
callback = maxTimeout
maxTimeout = null
findPeer (id, 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 || {}
}

if (maxTimeout == null) {
maxTimeout = c.minute
if (!options.maxTimeout) {
options.maxTimeout = c.minute
}

this._log('findPeer %s', id.toB58String())
Expand Down Expand Up @@ -534,7 +548,7 @@ class KadDHT {

timeout((cb) => {
query.run(peers, cb)
}, maxTimeout)(cb)
}, options.maxTimeout)(cb)
},
(result, cb) => {
this._log('findPeer %s: %s', id.toB58String(), result.success)
Expand Down
4 changes: 2 additions & 2 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('KadDHT', () => {
let n = 0
each(values, (v, cb) => {
n = (n + 1) % 3
dhts[n].findProviders(v.cid, 5000, (err, provs) => {
dhts[n].findProviders(v.cid, { maxTimeout: 5000 }, (err, provs) => {
expect(err).to.not.exist()
expect(provs).to.have.length(1)
expect(provs[0].id.id).to.be.eql(ids[3].id)
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('KadDHT', () => {
(cb) => connect(dhts[0], dhts[1], cb),
(cb) => connect(dhts[1], dhts[2], cb),
(cb) => connect(dhts[2], dhts[3], cb),
(cb) => dhts[0].findPeer(ids[3], 1000, cb),
(cb) => dhts[0].findPeer(ids[3], { maxTimeout: 1000 }, cb),
(res, cb) => {
expect(res.id.isEqual(ids[3])).to.eql(true)
cb()
Expand Down

0 comments on commit bba7500

Please sign in to comment.