mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-21 00:22:14 +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 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
|
// TODO: create proper validators for the generics
|
||||||
connectionManager: Joi.object(),
|
connectionManager: Joi.object(),
|
||||||
peerInfo: Joi.object().required(),
|
peerInfo: Joi.object().required(),
|
||||||
peerBook: Joi.object(),
|
peerBook: Joi.object(),
|
||||||
modules: Joi.object().keys({
|
modules: Joi.object().keys({
|
||||||
transport: Joi.array().items(
|
transport: Joi.array().items(ModuleSchema).min(1).required(),
|
||||||
Joi.alternatives().try(
|
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
|
||||||
Joi.func(),
|
connEncryption: Joi.array().items(ModuleSchema).allow(null),
|
||||||
Joi.object()
|
peerDiscovery: Joi.array().items(ModuleSchema).allow(null),
|
||||||
)
|
dht: ModuleSchema.allow(null)
|
||||||
).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)
|
|
||||||
}).required(),
|
}).required(),
|
||||||
config: Joi.object().keys({
|
config: Joi.object().keys({
|
||||||
peerDiscovery: Joi.object().allow(null),
|
peerDiscovery: Joi.object().allow(null),
|
||||||
@ -57,27 +36,12 @@ const schema = Joi.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
module.exports.validate = (options) => {
|
module.exports.validate = (options) => {
|
||||||
let newSchema = schema
|
options = Joi.attempt(options, OptionsSchema)
|
||||||
// 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()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure dht is correct
|
// Ensure dht is correct
|
||||||
if (config.config.EXPERIMENTAL && config.config.EXPERIMENTAL.dht) {
|
if (options.config.EXPERIMENTAL.dht) {
|
||||||
newSchema = newSchema.requiredKeys('modules.dht')
|
Joi.assert(options.modules.dht, ModuleSchema.required())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finish validation and return the updated config
|
return options
|
||||||
return Joi.attempt(config, newSchema)
|
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,8 @@ class Node extends EventEmitter {
|
|||||||
this.peerInfo = _options.peerInfo
|
this.peerInfo = _options.peerInfo
|
||||||
this.peerBook = _options.peerBook || new PeerBook()
|
this.peerBook = _options.peerBook || new PeerBook()
|
||||||
|
|
||||||
this._modules = _options.modules || {}
|
this._modules = _options.modules
|
||||||
this._config = _options.config || {}
|
this._config = _options.config
|
||||||
this._isStarted = false
|
this._isStarted = false
|
||||||
this._transport = [] // Transport instances/references
|
this._transport = [] // Transport instances/references
|
||||||
this._discovery = [] // Discovery service instances/references
|
this._discovery = [] // Discovery service instances/references
|
||||||
@ -76,7 +76,7 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dht provided components (peerRouting, contentRouting, dht)
|
// dht provided components (peerRouting, contentRouting, dht)
|
||||||
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.dht) {
|
if (this._config.EXPERIMENTAL.dht) {
|
||||||
const DHT = this._modules.dht
|
const DHT = this._modules.dht
|
||||||
this._dht = new DHT(this._switch, {
|
this._dht = new DHT(this._switch, {
|
||||||
kBucketSize: this._config.dht.kBucketSize || 20,
|
kBucketSize: this._config.dht.kBucketSize || 20,
|
||||||
@ -87,7 +87,7 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// enable/disable pubsub
|
// enable/disable pubsub
|
||||||
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.pubsub) {
|
if (this._config.EXPERIMENTAL.pubsub) {
|
||||||
this.pubsub = pubsub(this)
|
this.pubsub = pubsub(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,21 +113,4 @@ describe('configuration', () => {
|
|||||||
|
|
||||||
expect(() => validateConfig(options)).to.throw()
|
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