1
1
'use strict'
2
2
3
+ const mergeOptions = require ( 'merge-options' )
3
4
const { struct, superstruct } = require ( 'superstruct' )
4
5
const { optional, list } = struct
5
6
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
+
6
41
// Define custom types
7
42
const s = superstruct ( {
8
43
types : {
@@ -35,50 +70,15 @@ const modulesSchema = s({
35
70
} )
36
71
37
72
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
+ } )
76
78
77
79
const optionsSchema = s ( {
78
80
switch : 'object?' ,
79
- connectionManager : s ( 'object' , {
80
- minPeers : 25
81
- } ) ,
81
+ connectionManager : 'object?' ,
82
82
datastore : 'object?' ,
83
83
peerInfo : 'object' ,
84
84
peerBook : 'object?' ,
@@ -87,6 +87,7 @@ const optionsSchema = s({
87
87
} )
88
88
89
89
module . exports . validate = ( opts ) => {
90
+ opts = mergeOptions ( DefaultConfig , opts )
90
91
const [ error , options ] = optionsSchema . validate ( opts )
91
92
92
93
// Improve errors throwed, reduce stack by throwing here and add reason to the message
@@ -99,9 +100,5 @@ module.exports.validate = (opts) => {
99
100
}
100
101
}
101
102
102
- if ( options . config . peerDiscovery . autoDial === undefined ) {
103
- options . config . peerDiscovery . autoDial = true
104
- }
105
-
106
103
return options
107
104
}
0 commit comments