Skip to content

Commit

Permalink
fix: improve config defaults (#409)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jacobheun committed Aug 21, 2019
1 parent b3deb35 commit 3eef695
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -56,6 +56,7 @@
"libp2p-crypto": "~0.16.1",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
"merge-options": "^1.0.1",
"moving-average": "^1.0.0",
"multiaddr": "^6.1.0",
"multistream-select": "~0.14.6",
Expand Down Expand Up @@ -99,7 +100,6 @@
"libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.4.1",
"lodash.times": "^4.3.2",
"merge-options": "^1.0.1",
"nock": "^10.0.6",
"portfinder": "^1.0.20",
"pull-goodbye": "0.0.2",
Expand Down
87 changes: 42 additions & 45 deletions src/config.js
@@ -1,8 +1,43 @@
'use strict'

const mergeOptions = require('merge-options')
const { struct, superstruct } = require('superstruct')
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
const s = superstruct({
types: {
Expand Down Expand Up @@ -35,50 +70,15 @@ const modulesSchema = s({
})

const configSchema = s({
peerDiscovery: s('object', {
autoDial: true
}),
relay: s({
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
})
}, {})
peerDiscovery: 'object?',
relay: 'object?',
dht: 'object?',
pubsub: 'object?'
})

const optionsSchema = s({
switch: 'object?',
connectionManager: s('object', {
minPeers: 25
}),
connectionManager: 'object?',
datastore: 'object?',
peerInfo: 'object',
peerBook: 'object?',
Expand All @@ -87,6 +87,7 @@ const optionsSchema = s({
})

module.exports.validate = (opts) => {
opts = mergeOptions(DefaultConfig, opts)
const [error, options] = optionsSchema.validate(opts)

// Improve errors throwed, reduce stack by throwing here and add reason to the message
Expand All @@ -99,9 +100,5 @@ module.exports.validate = (opts) => {
}
}

if (options.config.peerDiscovery.autoDial === undefined) {
options.config.peerDiscovery.autoDial = true
}

return options
}
17 changes: 17 additions & 0 deletions test/config.spec.js
Expand Up @@ -125,6 +125,15 @@ describe('configuration', () => {
interval: 1000,
enabled: true
}
},
dht: {
enabled: false
},
relay: {
enabled: true
},
pubsub: {
enabled: true
}
}
}
Expand Down Expand Up @@ -292,6 +301,14 @@ describe('configuration', () => {
}
},
dht: {
kBucketSize: 20,
enabled: false,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
},
selectors,
validators
}
Expand Down
8 changes: 4 additions & 4 deletions test/pubsub.node.js
Expand Up @@ -376,7 +376,7 @@ describe('.pubsub', () => {
constructor (node, config) {
expect(config).to.be.eql({
enabled: true,
selfEmit: false,
emitSelf: false,
signMessages: false,
strictSigning: false
}).mark()
Expand All @@ -390,7 +390,7 @@ describe('.pubsub', () => {
config: {
pubsub: {
enabled: true,
selfEmit: false,
emitSelf: false,
signMessages: false,
strictSigning: false
}
Expand All @@ -408,7 +408,7 @@ describe('.pubsub', () => {
constructor (node, config) {
expect(config).to.be.eql({
enabled: true,
selfEmit: true,
emitSelf: true,
signMessages: true,
strictSigning: true
}).mark()
Expand All @@ -422,7 +422,7 @@ describe('.pubsub', () => {
config: {
pubsub: {
enabled: true,
selfEmit: true,
emitSelf: true,
signMessages: true,
strictSigning: true
}
Expand Down

0 comments on commit 3eef695

Please sign in to comment.