Skip to content

Commit 3eef695

Browse files
authored
fix: improve config defaults (#409)
This removes defaults from superstruct and instead uses mergeOptions to deeply set the defaults on configuration. This ensures that defaults are properly set. This is a step toward removing superstruct altogether, #406, but it is still being used for basic type validation.
1 parent b3deb35 commit 3eef695

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"libp2p-crypto": "~0.16.1",
5757
"libp2p-websockets": "^0.12.2",
5858
"mafmt": "^6.0.7",
59+
"merge-options": "^1.0.1",
5960
"moving-average": "^1.0.0",
6061
"multiaddr": "^6.1.0",
6162
"multistream-select": "~0.14.6",
@@ -99,7 +100,6 @@
99100
"libp2p-websocket-star": "~0.10.2",
100101
"libp2p-websocket-star-rendezvous": "~0.4.1",
101102
"lodash.times": "^4.3.2",
102-
"merge-options": "^1.0.1",
103103
"nock": "^10.0.6",
104104
"portfinder": "^1.0.20",
105105
"pull-goodbye": "0.0.2",

src/config.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
'use strict'
22

3+
const mergeOptions = require('merge-options')
34
const { struct, superstruct } = require('superstruct')
45
const { optional, list } = struct
56

7+
const DefaultConfig = {
8+
connectionManager: {
9+
minPeers: 25
10+
},
11+
config: {
12+
dht: {
13+
enabled: false,
14+
kBucketSize: 20,
15+
randomWalk: {
16+
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
17+
queriesPerPeriod: 1,
18+
interval: 300e3,
19+
timeout: 10e3
20+
}
21+
},
22+
peerDiscovery: {
23+
autoDial: true
24+
},
25+
pubsub: {
26+
enabled: true,
27+
emitSelf: true,
28+
signMessages: true,
29+
strictSigning: true
30+
},
31+
relay: {
32+
enabled: true,
33+
hop: {
34+
enabled: false,
35+
active: false
36+
}
37+
}
38+
}
39+
}
40+
641
// Define custom types
742
const s = superstruct({
843
types: {
@@ -35,50 +70,15 @@ const modulesSchema = s({
3570
})
3671

3772
const configSchema = s({
38-
peerDiscovery: s('object', {
39-
autoDial: true
40-
}),
41-
relay: s({
42-
enabled: 'boolean',
43-
hop: optional(s({
44-
enabled: 'boolean',
45-
active: 'boolean'
46-
}, {
47-
// HOP defaults
48-
enabled: false,
49-
active: false
50-
}))
51-
}, {
52-
// Relay defaults
53-
enabled: true
54-
}),
55-
// DHT config
56-
dht: s('object?', {
57-
// DHT defaults
58-
enabled: false,
59-
kBucketSize: 20,
60-
randomWalk: {
61-
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
62-
queriesPerPeriod: 1,
63-
interval: 300e3,
64-
timeout: 10e3
65-
}
66-
}),
67-
// Pubsub config
68-
pubsub: s('object?', {
69-
// Pubsub defaults
70-
enabled: true,
71-
emitSelf: true,
72-
signMessages: true,
73-
strictSigning: true
74-
})
75-
}, {})
73+
peerDiscovery: 'object?',
74+
relay: 'object?',
75+
dht: 'object?',
76+
pubsub: 'object?'
77+
})
7678

7779
const optionsSchema = s({
7880
switch: 'object?',
79-
connectionManager: s('object', {
80-
minPeers: 25
81-
}),
81+
connectionManager: 'object?',
8282
datastore: 'object?',
8383
peerInfo: 'object',
8484
peerBook: 'object?',
@@ -87,6 +87,7 @@ const optionsSchema = s({
8787
})
8888

8989
module.exports.validate = (opts) => {
90+
opts = mergeOptions(DefaultConfig, opts)
9091
const [error, options] = optionsSchema.validate(opts)
9192

9293
// Improve errors throwed, reduce stack by throwing here and add reason to the message
@@ -99,9 +100,5 @@ module.exports.validate = (opts) => {
99100
}
100101
}
101102

102-
if (options.config.peerDiscovery.autoDial === undefined) {
103-
options.config.peerDiscovery.autoDial = true
104-
}
105-
106103
return options
107104
}

test/config.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ describe('configuration', () => {
125125
interval: 1000,
126126
enabled: true
127127
}
128+
},
129+
dht: {
130+
enabled: false
131+
},
132+
relay: {
133+
enabled: true
134+
},
135+
pubsub: {
136+
enabled: true
128137
}
129138
}
130139
}
@@ -292,6 +301,14 @@ describe('configuration', () => {
292301
}
293302
},
294303
dht: {
304+
kBucketSize: 20,
305+
enabled: false,
306+
randomWalk: {
307+
enabled: false,
308+
queriesPerPeriod: 1,
309+
interval: 300000,
310+
timeout: 10000
311+
},
295312
selectors,
296313
validators
297314
}

test/pubsub.node.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('.pubsub', () => {
376376
constructor (node, config) {
377377
expect(config).to.be.eql({
378378
enabled: true,
379-
selfEmit: false,
379+
emitSelf: false,
380380
signMessages: false,
381381
strictSigning: false
382382
}).mark()
@@ -390,7 +390,7 @@ describe('.pubsub', () => {
390390
config: {
391391
pubsub: {
392392
enabled: true,
393-
selfEmit: false,
393+
emitSelf: false,
394394
signMessages: false,
395395
strictSigning: false
396396
}
@@ -408,7 +408,7 @@ describe('.pubsub', () => {
408408
constructor (node, config) {
409409
expect(config).to.be.eql({
410410
enabled: true,
411-
selfEmit: true,
411+
emitSelf: true,
412412
signMessages: true,
413413
strictSigning: true
414414
}).mark()
@@ -422,7 +422,7 @@ describe('.pubsub', () => {
422422
config: {
423423
pubsub: {
424424
enabled: true,
425-
selfEmit: true,
425+
emitSelf: true,
426426
signMessages: true,
427427
strictSigning: true
428428
}

0 commit comments

Comments
 (0)