fix: improve config defaults (#409)

This removes defaults from superstruct and instead uses
mergeOptions to deeply set the defaults on configuration.
This ensures that defaults are properly set.

This is a step toward removing superstruct altogether, #406,
but it is still being used for basic type validation.
This commit is contained in:
Jacob Heun 2019-08-21 16:44:30 +02:00 committed by GitHub
parent b3deb356f1
commit 3eef695bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 50 deletions

View File

@ -56,6 +56,7 @@
"libp2p-crypto": "~0.16.1", "libp2p-crypto": "~0.16.1",
"libp2p-websockets": "^0.12.2", "libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7", "mafmt": "^6.0.7",
"merge-options": "^1.0.1",
"moving-average": "^1.0.0", "moving-average": "^1.0.0",
"multiaddr": "^6.1.0", "multiaddr": "^6.1.0",
"multistream-select": "~0.14.6", "multistream-select": "~0.14.6",
@ -99,7 +100,6 @@
"libp2p-websocket-star": "~0.10.2", "libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.4.1", "libp2p-websocket-star-rendezvous": "~0.4.1",
"lodash.times": "^4.3.2", "lodash.times": "^4.3.2",
"merge-options": "^1.0.1",
"nock": "^10.0.6", "nock": "^10.0.6",
"portfinder": "^1.0.20", "portfinder": "^1.0.20",
"pull-goodbye": "0.0.2", "pull-goodbye": "0.0.2",

View File

@ -1,8 +1,43 @@
'use strict' 'use strict'
const mergeOptions = require('merge-options')
const { struct, superstruct } = require('superstruct') const { struct, superstruct } = require('superstruct')
const { optional, list } = struct const { optional, list } = struct
const DefaultConfig = {
connectionManager: {
minPeers: 25
},
config: {
dht: {
enabled: false,
kBucketSize: 20,
randomWalk: {
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
queriesPerPeriod: 1,
interval: 300e3,
timeout: 10e3
}
},
peerDiscovery: {
autoDial: true
},
pubsub: {
enabled: true,
emitSelf: true,
signMessages: true,
strictSigning: true
},
relay: {
enabled: true,
hop: {
enabled: false,
active: false
}
}
}
}
// Define custom types // Define custom types
const s = superstruct({ const s = superstruct({
types: { types: {
@ -35,50 +70,15 @@ const modulesSchema = s({
}) })
const configSchema = s({ const configSchema = s({
peerDiscovery: s('object', { peerDiscovery: 'object?',
autoDial: true relay: 'object?',
}), dht: 'object?',
relay: s({ pubsub: 'object?'
enabled: 'boolean', })
hop: optional(s({
enabled: 'boolean',
active: 'boolean'
}, {
// HOP defaults
enabled: false,
active: false
}))
}, {
// Relay defaults
enabled: true
}),
// DHT config
dht: s('object?', {
// DHT defaults
enabled: false,
kBucketSize: 20,
randomWalk: {
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
queriesPerPeriod: 1,
interval: 300e3,
timeout: 10e3
}
}),
// Pubsub config
pubsub: s('object?', {
// Pubsub defaults
enabled: true,
emitSelf: true,
signMessages: true,
strictSigning: true
})
}, {})
const optionsSchema = s({ const optionsSchema = s({
switch: 'object?', switch: 'object?',
connectionManager: s('object', { connectionManager: 'object?',
minPeers: 25
}),
datastore: 'object?', datastore: 'object?',
peerInfo: 'object', peerInfo: 'object',
peerBook: 'object?', peerBook: 'object?',
@ -87,6 +87,7 @@ const optionsSchema = s({
}) })
module.exports.validate = (opts) => { module.exports.validate = (opts) => {
opts = mergeOptions(DefaultConfig, opts)
const [error, options] = optionsSchema.validate(opts) const [error, options] = optionsSchema.validate(opts)
// Improve errors throwed, reduce stack by throwing here and add reason to the message // Improve errors throwed, reduce stack by throwing here and add reason to the message
@ -99,9 +100,5 @@ module.exports.validate = (opts) => {
} }
} }
if (options.config.peerDiscovery.autoDial === undefined) {
options.config.peerDiscovery.autoDial = true
}
return options return options
} }

View File

@ -125,6 +125,15 @@ describe('configuration', () => {
interval: 1000, interval: 1000,
enabled: true enabled: true
} }
},
dht: {
enabled: false
},
relay: {
enabled: true
},
pubsub: {
enabled: true
} }
} }
} }
@ -292,6 +301,14 @@ describe('configuration', () => {
} }
}, },
dht: { dht: {
kBucketSize: 20,
enabled: false,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
},
selectors, selectors,
validators validators
} }

View File

@ -376,7 +376,7 @@ describe('.pubsub', () => {
constructor (node, config) { constructor (node, config) {
expect(config).to.be.eql({ expect(config).to.be.eql({
enabled: true, enabled: true,
selfEmit: false, emitSelf: false,
signMessages: false, signMessages: false,
strictSigning: false strictSigning: false
}).mark() }).mark()
@ -390,7 +390,7 @@ describe('.pubsub', () => {
config: { config: {
pubsub: { pubsub: {
enabled: true, enabled: true,
selfEmit: false, emitSelf: false,
signMessages: false, signMessages: false,
strictSigning: false strictSigning: false
} }
@ -408,7 +408,7 @@ describe('.pubsub', () => {
constructor (node, config) { constructor (node, config) {
expect(config).to.be.eql({ expect(config).to.be.eql({
enabled: true, enabled: true,
selfEmit: true, emitSelf: true,
signMessages: true, signMessages: true,
strictSigning: true strictSigning: true
}).mark() }).mark()
@ -422,7 +422,7 @@ describe('.pubsub', () => {
config: { config: {
pubsub: { pubsub: {
enabled: true, enabled: true,
selfEmit: true, emitSelf: true,
signMessages: true, signMessages: true,
strictSigning: true strictSigning: true
} }