Skip to content

Commit

Permalink
fix: remove peer discovery module config checks
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
alanshaw authored and jacobheun committed Jun 29, 2018
1 parent f538663 commit 8c83e07
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 69 deletions.
60 changes: 12 additions & 48 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down
17 changes: 0 additions & 17 deletions test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 8c83e07

Please sign in to comment.