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

Commit 0a2f9fe

Browse files
authored
fix: dht get options (#40)
1 parent edab8a3 commit 0a2f9fe

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/index.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -179,39 +179,54 @@ class KadDHT {
179179
* Times out after 1 minute.
180180
*
181181
* @param {Buffer} key
182-
* @param {number} [maxTimeout=60000] - optional timeout
182+
* @param {Object} options - get options
183+
* @param {number} options.maxTimeout - optional timeout (default: 60000)
183184
* @param {function(Error, Buffer)} callback
184185
* @returns {void}
185186
*/
186-
get (key, maxTimeout, callback) {
187-
if (typeof maxTimeout === 'function') {
188-
callback = maxTimeout
189-
maxTimeout = null
187+
get (key, options, callback) {
188+
if (typeof options === 'function') {
189+
callback = options
190+
options = {}
191+
} else if (typeof options === 'number') { // This will be deprecated in a next release
192+
options = {
193+
maxTimeout: options
194+
}
195+
} else {
196+
options = options || {}
190197
}
191198

192-
if (maxTimeout == null) {
193-
maxTimeout = c.minute
199+
if (!options.maxTimeout) {
200+
options.maxTimeout = c.minute
194201
}
195202

196-
this._get(key, maxTimeout, callback)
203+
this._get(key, options, callback)
197204
}
198205

199206
/**
200207
* Get the `n` values to the given key without sorting.
201208
*
202209
* @param {Buffer} key
203210
* @param {number} nvals
204-
* @param {number} [maxTimeout=60000]
211+
* @param {Object} options - get options
212+
* @param {number} options.maxTimeout - optional timeout (default: 60000)
205213
* @param {function(Error, Array<{from: PeerId, val: Buffer}>)} callback
206214
* @returns {void}
207215
*/
208-
getMany (key, nvals, maxTimeout, callback) {
209-
if (typeof maxTimeout === 'function') {
210-
callback = maxTimeout
211-
maxTimeout = null
216+
getMany (key, nvals, options, callback) {
217+
if (typeof options === 'function') {
218+
callback = options
219+
options = {}
220+
} else if (typeof options === 'number') { // This will be deprecated in a next release
221+
options = {
222+
maxTimeout: options
223+
}
224+
} else {
225+
options = options || {}
212226
}
213-
if (maxTimeout == null) {
214-
maxTimeout = c.minute
227+
228+
if (!options.maxTimeout) {
229+
options.maxTimeout = c.minute
215230
}
216231

217232
this._log('getMany %b (%s)', key, nvals)
@@ -274,7 +289,7 @@ class KadDHT {
274289
})
275290

276291
// run our query
277-
timeout((cb) => query.run(rtp, cb), maxTimeout)(cb)
292+
timeout((cb) => query.run(rtp, cb), options.maxTimeout)(cb)
278293
}
279294
], (err) => {
280295
if (err && vals.length === 0) {

src/private.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,17 @@ module.exports = (dht) => ({
283283
* Get the value to the given key.
284284
*
285285
* @param {Buffer} key
286-
* @param {number} maxTimeout
287-
* @param {function(Error, Buffer)} callback
286+
* @param {Object} options - get options
287+
* @param {number} options.maxTimeout - optional timeout (default: 60000)
288+
* @param {function(Error, Record)} callback
288289
* @returns {void}
289290
*
290291
* @private
291292
*/
292-
_get (key, maxTimeout, callback) {
293+
_get (key, options, callback) {
293294
dht._log('_get %b', key)
294295
waterfall([
295-
(cb) => dht.getMany(key, 16, maxTimeout, cb),
296+
(cb) => dht.getMany(key, 16, options.maxTimeout, cb),
296297
(vals, cb) => {
297298
const recs = vals.map((v) => v.val)
298299
const i = libp2pRecord.selection.bestRecord(dht.selectors, key, recs)

test/kad-dht.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('KadDHT', () => {
147147
waterfall([
148148
(cb) => connect(dhtA, dhtB, cb),
149149
(cb) => dhtA.put(Buffer.from('/v/hello'), Buffer.from('world'), cb),
150-
(cb) => dhtB.get(Buffer.from('/v/hello'), 1000, cb),
150+
(cb) => dhtB.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb),
151151
(res, cb) => {
152152
expect(res).to.eql(Buffer.from('world'))
153153
cb()
@@ -244,7 +244,7 @@ describe('KadDHT', () => {
244244
Buffer.from('world'),
245245
cb
246246
),
247-
(cb) => dhts[0].get(Buffer.from('/v/hello'), 1000, cb),
247+
(cb) => dhts[0].get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb),
248248
(res, cb) => {
249249
expect(res).to.eql(Buffer.from('world'))
250250
cb()

0 commit comments

Comments
 (0)