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

[WIP] feat: add option to select record #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class KadDHT {
* @param {Buffer} key
* @param {Object} options - get options
* @param {number} options.maxTimeout - optional timeout (default: 60000)
* @param {boolean} options.selectFirst - select first value found (default: false)
* @param {function(Error, Buffer)} callback
* @returns {void}
*/
Expand Down Expand Up @@ -221,6 +222,7 @@ class KadDHT {
* @param {number} nvals
* @param {Object} options - get options
* @param {number} options.maxTimeout - optional timeout (default: 60000)
* @param {boolean} options.selectFirst - select first value found (default: false)
* @param {function(Error, Array<{from: PeerId, val: Buffer}>)} callback
* @returns {void}
*/
Expand Down
3 changes: 2 additions & 1 deletion src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ module.exports = (dht) => ({
* @param {Buffer} key
* @param {Object} options - get options
* @param {number} options.maxTimeout - optional timeout (default: 60000)
* @param {boolean} options.selectFirst - select first value found (default: false)
* @param {function(Error, Record)} callback
* @returns {void}
*
Expand All @@ -271,7 +272,7 @@ module.exports = (dht) => ({
(cb) => dht.getMany(key, 16, options.maxTimeout, cb),
(vals, cb) => {
const recs = vals.map((v) => v.val)
const i = libp2pRecord.selection.bestRecord(dht.selectors, key, recs)
const i = options.selectFirst ? 0 : libp2pRecord.selection.bestRecord(dht.selectors, key, recs)
const best = recs[i]
dht._log('GetValue %b %s', key, best)

Expand Down
24 changes: 24 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,30 @@ describe('KadDHT', () => {
})
})

it('put - get with select first', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()

tdht.spawn(2, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]

waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.put(Buffer.from('hello'), Buffer.from('world'), cb),
(cb) => dhtB.get(Buffer.from('hello'), { maxTimeout: 1000, selectFirst: true }, cb),
(res, cb) => {
expect(res).to.eql(Buffer.from('world'))
cb()
}
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})
})

it('provides', function (done) {
this.timeout(20 * 1000)

Expand Down