mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
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:
parent
b3deb356f1
commit
3eef695bc0
@ -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",
|
||||
@ -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",
|
||||
|
@ -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: {
|
||||
@ -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?',
|
||||
@ -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
|
||||
@ -99,9 +100,5 @@ module.exports.validate = (opts) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (options.config.peerDiscovery.autoDial === undefined) {
|
||||
options.config.peerDiscovery.autoDial = true
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
@ -125,6 +125,15 @@ describe('configuration', () => {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
relay: {
|
||||
enabled: true
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -292,6 +301,14 @@ describe('configuration', () => {
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabled: false,
|
||||
randomWalk: {
|
||||
enabled: false,
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300000,
|
||||
timeout: 10000
|
||||
},
|
||||
selectors,
|
||||
validators
|
||||
}
|
||||
|
@ -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()
|
||||
@ -390,7 +390,7 @@ describe('.pubsub', () => {
|
||||
config: {
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
selfEmit: false,
|
||||
emitSelf: false,
|
||||
signMessages: false,
|
||||
strictSigning: false
|
||||
}
|
||||
@ -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()
|
||||
@ -422,7 +422,7 @@ describe('.pubsub', () => {
|
||||
config: {
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
selfEmit: true,
|
||||
emitSelf: true,
|
||||
signMessages: true,
|
||||
strictSigning: true
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user