This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathconfig.js
87 lines (78 loc) · 2.02 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
const Multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const { struct, superstruct } = require('superstruct')
const { optional, union } = struct
const s = superstruct({
types: {
multiaddr: v => {
if (v === null) {
return `multiaddr invalid, value must be a string, Buffer, or another Multiaddr got ${v}`
}
try {
Multiaddr(v)
} catch (err) {
return `multiaddr invalid, ${err.message}`
}
return true
},
'multiaddr-ipfs': v => mafmt.IPFS.matches(v) ? true : `multiaddr IPFS invalid`
}
})
const configSchema = s({
repo: optional(s('object|string')),
repoOwner: 'boolean?',
preload: s({
enabled: 'boolean?',
addresses: optional(s(['multiaddr'])),
interval: 'number?'
}, { enabled: true, interval: 30 * 1000 }),
init: optional(union(['boolean', s({
bits: 'number?',
emptyRepo: 'boolean?',
privateKey: optional(s('object|string')), // object should be a custom type for PeerId using 'kind-of'
pass: 'string?'
})])),
start: 'boolean?',
offline: 'boolean?',
pass: 'string?',
silent: 'boolean?',
relay: 'object?', // relay validates in libp2p
EXPERIMENTAL: optional(s({
pubsub: 'boolean?',
ipnsPubsub: 'boolean?',
sharding: 'boolean?',
dht: 'boolean?'
})),
connectionManager: 'object?',
config: optional(s({
API: 'object?',
Addresses: optional(s({
Swarm: optional(s(['multiaddr'])),
API: 'multiaddr?',
Gateway: 'multiaddr'
})),
Discovery: optional(s({
MDNS: optional(s({
Enabled: 'boolean?',
Interval: 'number?'
})),
webRTCStar: optional(s({
Enabled: 'boolean?'
}))
})),
Bootstrap: optional(s(['multiaddr-ipfs']))
})),
ipld: 'object?',
libp2p: optional(union(['function', 'object'])) // libp2p validates this
}, {
repoOwner: true
})
const validate = (opts) => {
const [err, options] = configSchema.validate(opts)
if (err) {
throw err
}
return options
}
module.exports = { validate }