Skip to content

Commit e6e1772

Browse files
committed
fix: validate dht config
1 parent 581a1de commit e6e1772

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"libp2p-ping": "~0.8.3",
5353
"libp2p-switch": "~0.41.2",
5454
"libp2p-websockets": "~0.12.0",
55+
"lodash.get": "^4.4.2",
5556
"mafmt": "^6.0.2",
5657
"multiaddr": "^5.0.2",
5758
"peer-book": "~0.8.0",

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ log.error = debug('libp2p:error')
1010
const each = require('async/each')
1111
const series = require('async/series')
1212
const parallel = require('async/parallel')
13+
const get = require('lodash.get')
1314

1415
const PeerBook = require('peer-book')
1516
const Switch = require('libp2p-switch')
@@ -99,10 +100,10 @@ class Node extends EventEmitter {
99100
// dht provided components (peerRouting, contentRouting, dht)
100101
if (this._config.EXPERIMENTAL.dht) {
101102
const DHT = this._modules.dht
102-
const enabledDiscovery = this._config.dht.enabledDiscovery !== false
103+
const enabledDiscovery = Boolean(get(this._config, 'dht.enabledDiscovery', true))
103104

104105
this._dht = new DHT(this._switch, {
105-
kBucketSize: this._config.dht.kBucketSize || 20,
106+
kBucketSize: get(this._config, 'dht.kBucketSize', 20),
106107
enabledDiscovery,
107108
datastore: this.datastore
108109
})

test/create.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ describe('libp2p creation', () => {
8181
})
8282
})
8383

84+
it('should create using dht defaults', (done) => {
85+
createNode([], {
86+
config: {
87+
EXPERIMENTAL: {
88+
dht: true
89+
},
90+
dht: {}
91+
}
92+
}, (err, node) => {
93+
expect(err).to.not.exist()
94+
expect(node._dht).to.exist()
95+
done()
96+
})
97+
})
98+
8499
it('should not throw errors from switch if node has no error listeners', (done) => {
85100
createNode([], {}, (err, node) => {
86101
expect(err).to.not.exist()

0 commit comments

Comments
 (0)