Skip to content

Commit

Permalink
fix: dht get options
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 20, 2018
1 parent 69f7264 commit db971a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ module.exports = (node) => {

node._dht.put(key, value, callback)
},
get: (key, maxTimeout, callback) => {
if (typeof maxTimeout === 'function') {
callback = maxTimeout
maxTimeout = null
get: (key, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.get(key, maxTimeout, callback)
node._dht.get(key, options, callback)
},
getMany: (key, nVals, maxTimeout, callback) => {
if (typeof maxTimeout === 'function') {
callback = maxTimeout
maxTimeout = null
getMany: (key, nVals, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.getMany(key, nVals, maxTimeout, callback)
node._dht.getMany(key, nVals, options, callback)
}
}
}
12 changes: 6 additions & 6 deletions test/dht.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ describe('.dht', () => {
})
})

it('should be able to dht.get a value from the DHT with a maxTimeout', (done) => {
it('should be able to dht.get a value from the DHT with options', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.get(key, 3000, (err, res) => {
nodeA.dht.get(key, { maxTimeout: 3000 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.eql(value)
done()
})
})
})

it('should be able to dht.get a value from the DHT with no maxTimeout defined', (done) => {
it('should be able to dht.get a value from the DHT with no options defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

Expand All @@ -83,22 +83,22 @@ describe('.dht', () => {
})
})

it('should be able to dht.getMany a value from the DHT with a maxTimeout', (done) => {
it('should be able to dht.getMany a value from the DHT with options', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.getMany(key, 1, (err, res) => {
nodeA.dht.getMany(key, 1, { maxTimeout: 3000 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
})

it('should be able to dht.getMany a value from the DHT with no maxTimeout defined', (done) => {
it('should be able to dht.getMany a value from the DHT with no options defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

Expand Down

0 comments on commit db971a3

Please sign in to comment.