Skip to content

Commit 4ad70ef

Browse files
alanshawJacob Heun
authored andcommitted
fix: remove peer discovery module config checks
Configuration for the peer discovery modules is now optional so this does not need to be validated. This also cleans up the config module to reduce repetition. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 1af5ba9 commit 4ad70ef

File tree

3 files changed

+16
-69
lines changed

3 files changed

+16
-69
lines changed

src/config.js

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,19 @@
22

33
const Joi = require('joi')
44

5-
const schema = Joi.object({
5+
const ModuleSchema = Joi.alternatives().try(Joi.func(), Joi.object())
6+
7+
const OptionsSchema = Joi.object({
68
// TODO: create proper validators for the generics
79
connectionManager: Joi.object(),
810
peerInfo: Joi.object().required(),
911
peerBook: Joi.object(),
1012
modules: Joi.object().keys({
11-
transport: Joi.array().items(
12-
Joi.alternatives().try(
13-
Joi.func(),
14-
Joi.object()
15-
)
16-
).min(1).required(),
17-
streamMuxer: Joi.array().items(
18-
Joi.alternatives().try(
19-
Joi.func(),
20-
Joi.object()
21-
)
22-
).allow(null),
23-
connEncryption: Joi.array().items(
24-
Joi.alternatives().try(
25-
Joi.func(),
26-
Joi.object()
27-
)
28-
).allow(null),
29-
peerDiscovery: Joi.array().items(
30-
Joi.alternatives().try(
31-
Joi.func(),
32-
Joi.object()
33-
)
34-
).allow(null),
35-
dht: Joi.alternatives().try(
36-
Joi.func(),
37-
Joi.object()
38-
).allow(null)
13+
transport: Joi.array().items(ModuleSchema).min(1).required(),
14+
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
15+
connEncryption: Joi.array().items(ModuleSchema).allow(null),
16+
peerDiscovery: Joi.array().items(ModuleSchema).allow(null),
17+
dht: ModuleSchema.allow(null)
3918
}).required(),
4019
config: Joi.object().keys({
4120
peerDiscovery: Joi.object().allow(null),
@@ -57,27 +36,12 @@ const schema = Joi.object({
5736
})
5837

5938
module.exports.validate = (options) => {
60-
let newSchema = schema
61-
// Throw an intial error early for required props
62-
let config = Joi.attempt(options, newSchema)
63-
64-
// Ensure discoveries are properly configured
65-
if (config.modules.peerDiscovery) {
66-
config.modules.peerDiscovery.forEach((discovery) => {
67-
// If it's a function, validate we have configs for it
68-
if (typeof discovery === 'function') {
69-
Joi.reach(schema, 'config.peerDiscovery').keys({
70-
[discovery.tag]: Joi.object().required()
71-
})
72-
}
73-
})
74-
}
39+
options = Joi.attempt(options, OptionsSchema)
7540

7641
// Ensure dht is correct
77-
if (config.config.EXPERIMENTAL && config.config.EXPERIMENTAL.dht) {
78-
newSchema = newSchema.requiredKeys('modules.dht')
42+
if (options.config.EXPERIMENTAL.dht) {
43+
Joi.assert(options.modules.dht, ModuleSchema.required())
7944
}
8045

81-
// Finish validation and return the updated config
82-
return Joi.attempt(config, newSchema)
46+
return options
8347
}

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Node extends EventEmitter {
3434
this.peerInfo = _options.peerInfo
3535
this.peerBook = _options.peerBook || new PeerBook()
3636

37-
this._modules = _options.modules || {}
38-
this._config = _options.config || {}
37+
this._modules = _options.modules
38+
this._config = _options.config
3939
this._isStarted = false
4040
this._transport = [] // Transport instances/references
4141
this._discovery = [] // Discovery service instances/references
@@ -76,7 +76,7 @@ class Node extends EventEmitter {
7676
}
7777

7878
// dht provided components (peerRouting, contentRouting, dht)
79-
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.dht) {
79+
if (this._config.EXPERIMENTAL.dht) {
8080
const DHT = this._modules.dht
8181
this._dht = new DHT(this._switch, {
8282
kBucketSize: this._config.dht.kBucketSize || 20,
@@ -87,7 +87,7 @@ class Node extends EventEmitter {
8787
}
8888

8989
// enable/disable pubsub
90-
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.pubsub) {
90+
if (this._config.EXPERIMENTAL.pubsub) {
9191
this.pubsub = pubsub(this)
9292
}
9393

test/config.spec.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,4 @@ describe('configuration', () => {
113113

114114
expect(() => validateConfig(options)).to.throw()
115115
})
116-
117-
it('should require a non instanced peerDiscovery module to have associated options', () => {
118-
const options = {
119-
peerInfo,
120-
modules: {
121-
transport: [ WS ],
122-
peerDiscovery: [ Bootstrap ]
123-
},
124-
config: {
125-
EXPERIMENTAL: {
126-
dht: false
127-
}
128-
}
129-
}
130-
131-
expect(() => validateConfig(options)).to.throw()
132-
})
133116
})

0 commit comments

Comments
 (0)