From 4ad70efb000a48f3cad060834f0142b3828aa05c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 28 Jun 2018 16:06:42 +0100 Subject: [PATCH] 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 --- src/config.js | 60 +++++++++------------------------------------ src/index.js | 8 +++--- test/config.spec.js | 17 ------------- 3 files changed, 16 insertions(+), 69 deletions(-) diff --git a/src/config.js b/src/config.js index 7f57e50c..03072a57 100644 --- a/src/config.js +++ b/src/config.js @@ -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 } diff --git a/src/index.js b/src/index.js index c0656482..b368853d 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } diff --git a/test/config.spec.js b/test/config.spec.js index d99d9118..787319d8 100644 --- a/test/config.spec.js +++ b/test/config.spec.js @@ -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() - }) })