mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-16 07:30:51 +00:00
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 <alan@tableflip.io>
This commit is contained in:
parent
1af5ba9093
commit
4ad70efb00
@ -2,40 +2,19 @@
|
||||
|
||||
const Joi = require('joi')
|
||||
|
||||
const schema = Joi.object({
|
||||
const ModuleSchema = Joi.alternatives().try(Joi.func(), Joi.object())
|
||||
|
||||
const OptionsSchema = Joi.object({
|
||||
// TODO: create proper validators for the generics
|
||||
connectionManager: Joi.object(),
|
||||
peerInfo: Joi.object().required(),
|
||||
peerBook: Joi.object(),
|
||||
modules: Joi.object().keys({
|
||||
transport: Joi.array().items(
|
||||
Joi.alternatives().try(
|
||||
Joi.func(),
|
||||
Joi.object()
|
||||
)
|
||||
).min(1).required(),
|
||||
streamMuxer: Joi.array().items(
|
||||
Joi.alternatives().try(
|
||||
Joi.func(),
|
||||
Joi.object()
|
||||
)
|
||||
).allow(null),
|
||||
connEncryption: Joi.array().items(
|
||||
Joi.alternatives().try(
|
||||
Joi.func(),
|
||||
Joi.object()
|
||||
)
|
||||
).allow(null),
|
||||
peerDiscovery: Joi.array().items(
|
||||
Joi.alternatives().try(
|
||||
Joi.func(),
|
||||
Joi.object()
|
||||
)
|
||||
).allow(null),
|
||||
dht: Joi.alternatives().try(
|
||||
Joi.func(),
|
||||
Joi.object()
|
||||
).allow(null)
|
||||
transport: Joi.array().items(ModuleSchema).min(1).required(),
|
||||
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
|
||||
connEncryption: Joi.array().items(ModuleSchema).allow(null),
|
||||
peerDiscovery: Joi.array().items(ModuleSchema).allow(null),
|
||||
dht: ModuleSchema.allow(null)
|
||||
}).required(),
|
||||
config: Joi.object().keys({
|
||||
peerDiscovery: Joi.object().allow(null),
|
||||
@ -57,27 +36,12 @@ const schema = Joi.object({
|
||||
})
|
||||
|
||||
module.exports.validate = (options) => {
|
||||
let newSchema = schema
|
||||
// Throw an intial error early for required props
|
||||
let config = Joi.attempt(options, newSchema)
|
||||
|
||||
// Ensure discoveries are properly configured
|
||||
if (config.modules.peerDiscovery) {
|
||||
config.modules.peerDiscovery.forEach((discovery) => {
|
||||
// If it's a function, validate we have configs for it
|
||||
if (typeof discovery === 'function') {
|
||||
Joi.reach(schema, 'config.peerDiscovery').keys({
|
||||
[discovery.tag]: Joi.object().required()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
options = Joi.attempt(options, OptionsSchema)
|
||||
|
||||
// Ensure dht is correct
|
||||
if (config.config.EXPERIMENTAL && config.config.EXPERIMENTAL.dht) {
|
||||
newSchema = newSchema.requiredKeys('modules.dht')
|
||||
if (options.config.EXPERIMENTAL.dht) {
|
||||
Joi.assert(options.modules.dht, ModuleSchema.required())
|
||||
}
|
||||
|
||||
// Finish validation and return the updated config
|
||||
return Joi.attempt(config, newSchema)
|
||||
return options
|
||||
}
|
||||
|
@ -34,8 +34,8 @@ class Node extends EventEmitter {
|
||||
this.peerInfo = _options.peerInfo
|
||||
this.peerBook = _options.peerBook || new PeerBook()
|
||||
|
||||
this._modules = _options.modules || {}
|
||||
this._config = _options.config || {}
|
||||
this._modules = _options.modules
|
||||
this._config = _options.config
|
||||
this._isStarted = false
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = [] // Discovery service instances/references
|
||||
@ -76,7 +76,7 @@ class Node extends EventEmitter {
|
||||
}
|
||||
|
||||
// dht provided components (peerRouting, contentRouting, dht)
|
||||
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.dht) {
|
||||
if (this._config.EXPERIMENTAL.dht) {
|
||||
const DHT = this._modules.dht
|
||||
this._dht = new DHT(this._switch, {
|
||||
kBucketSize: this._config.dht.kBucketSize || 20,
|
||||
@ -87,7 +87,7 @@ class Node extends EventEmitter {
|
||||
}
|
||||
|
||||
// enable/disable pubsub
|
||||
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.pubsub) {
|
||||
if (this._config.EXPERIMENTAL.pubsub) {
|
||||
this.pubsub = pubsub(this)
|
||||
}
|
||||
|
||||
|
@ -113,21 +113,4 @@ describe('configuration', () => {
|
||||
|
||||
expect(() => validateConfig(options)).to.throw()
|
||||
})
|
||||
|
||||
it('should require a non instanced peerDiscovery module to have associated options', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(() => validateConfig(options)).to.throw()
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user