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

fix: find peer and providers options #45

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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