2018-06-28 10:06:25 +02:00
|
|
|
'use strict'
|
|
|
|
|
2019-08-21 16:44:30 +02:00
|
|
|
const mergeOptions = require('merge-options')
|
2019-02-21 16:07:35 +00:00
|
|
|
const { struct, superstruct } = require('superstruct')
|
|
|
|
const { optional, list } = struct
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2019-08-21 16:44:30 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:07:35 +00:00
|
|
|
// Define custom types
|
2019-08-20 15:01:40 +02:00
|
|
|
const s = superstruct({
|
|
|
|
types: {
|
|
|
|
transport: value => {
|
|
|
|
if (value.length === 0) return 'ERROR_EMPTY'
|
|
|
|
value.forEach(i => {
|
|
|
|
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
protector: value => {
|
|
|
|
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-11 12:44:58 +02:00
|
|
|
const modulesSchema = s({
|
|
|
|
connEncryption: optional(list([s('object|function')])),
|
|
|
|
// this is hacky to simulate optional because interface doesnt work correctly with it
|
|
|
|
// change to optional when fixed upstream
|
2019-08-20 15:01:40 +02:00
|
|
|
connProtector: s('undefined|protector'),
|
2019-04-11 12:44:58 +02:00
|
|
|
contentRouting: optional(list(['object'])),
|
|
|
|
dht: optional(s('null|function|object')),
|
2019-07-31 09:38:14 +02:00
|
|
|
pubsub: optional(s('null|function|object')),
|
2019-04-11 12:44:58 +02:00
|
|
|
peerDiscovery: optional(list([s('object|function')])),
|
|
|
|
peerRouting: optional(list(['object'])),
|
|
|
|
streamMuxer: optional(list([s('object|function')])),
|
2019-08-20 15:01:40 +02:00
|
|
|
transport: 'transport'
|
2019-04-11 12:44:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const configSchema = s({
|
2019-08-21 16:44:30 +02:00
|
|
|
peerDiscovery: 'object?',
|
|
|
|
relay: 'object?',
|
|
|
|
dht: 'object?',
|
|
|
|
pubsub: 'object?'
|
|
|
|
})
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2019-04-11 12:44:58 +02:00
|
|
|
const optionsSchema = s({
|
2019-04-12 11:10:09 +02:00
|
|
|
switch: 'object?',
|
2019-08-21 16:44:30 +02:00
|
|
|
connectionManager: 'object?',
|
2019-04-11 12:44:58 +02:00
|
|
|
datastore: 'object?',
|
|
|
|
peerInfo: 'object',
|
|
|
|
peerBook: 'object?',
|
|
|
|
modules: modulesSchema,
|
|
|
|
config: configSchema
|
|
|
|
})
|
2019-02-21 16:07:35 +00:00
|
|
|
|
|
|
|
module.exports.validate = (opts) => {
|
2019-08-21 16:44:30 +02:00
|
|
|
opts = mergeOptions(DefaultConfig, opts)
|
2019-02-21 16:07:35 +00:00
|
|
|
const [error, options] = optionsSchema.validate(opts)
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2019-02-21 16:07:35 +00:00
|
|
|
// Improve errors throwed, reduce stack by throwing here and add reason to the message
|
|
|
|
if (error) {
|
|
|
|
throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`)
|
|
|
|
} else {
|
|
|
|
// Throw when dht is enabled but no dht module provided
|
|
|
|
if (options.config.dht.enabled) {
|
|
|
|
s('function|object')(options.modules.dht)
|
|
|
|
}
|
2018-06-28 10:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 16:06:42 +01:00
|
|
|
return options
|
2018-06-28 10:06:25 +02:00
|
|
|
}
|