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

Commit

Permalink
feat: select first record when no selector function
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 8, 2018
1 parent d1869ed commit 8e28554
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,17 @@ 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)
let i = 0

try {
i = libp2pRecord.selection.bestRecord(dht.selectors, key, recs)
} catch (err) {
// Assume the first record if no selector available
if (err.code !== 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY') {
return callback(err)
}
}

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 using key with no selector', 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 }, cb),
(res, cb) => {
expect(res).to.eql(Buffer.from('world'))
cb()
}
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})
})

it('put - get with update', function (done) {
this.timeout(20 * 1000)
const tdht = new TestDHT()
Expand Down

0 comments on commit 8e28554

Please sign in to comment.