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

feat: select first record when no selector function #51

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"interface-datastore": "~0.6.0",
"k-bucket": "^4.0.1",
"libp2p-crypto": "~0.13.0",
"libp2p-record": "~0.6.0",
"libp2p-record": "~0.6.1",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"peer-id": "~0.11.0",
Expand Down
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vasco-santos maybe we should add another error test to check this callback behavior. It won't matter now, but if we start doing any checking in the waterfall callback this might fail. callback should be cb so the rest of waterfall executes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, it is better to have that test now than have tests failing later. Will add it now.

vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
}
}

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