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 pathlibp2p.js
108 lines (96 loc) · 3.09 KB
/
libp2p.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const get = require('lodash/get')
const defaultsDeep = require('@nodeutils/defaults-deep')
const ipnsUtils = require('../ipns/routing/utils')
module.exports = function libp2p (self, config) {
const options = self._options || {}
config = config || {}
// Always create libp2p via a bundle function
const createBundle = typeof options.libp2p === 'function'
? options.libp2p
: defaultBundle
const { datastore } = self._repo
const peerInfo = self._peerInfo
const peerBook = self._peerInfoBook
const libp2p = createBundle({ options, config, datastore, peerInfo, peerBook })
let discoveredPeers = []
const putAndDial = peerInfo => {
peerBook.put(peerInfo)
libp2p.dial(peerInfo, () => {})
}
libp2p.on('start', () => {
peerInfo.multiaddrs.forEach((ma) => {
self._print('Swarm listening on', ma.toString())
})
discoveredPeers.forEach(putAndDial)
discoveredPeers = []
})
libp2p.on('peer:discovery', (peerInfo) => {
if (self.isOnline()) {
putAndDial(peerInfo)
} else {
discoveredPeers.push(peerInfo)
}
})
libp2p.on('peer:connect', peerInfo => peerBook.put(peerInfo))
return libp2p
}
function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
const libp2pDefaults = {
datastore,
peerInfo,
peerBook,
config: {
peerDiscovery: {
mdns: {
enabled: get(options, 'config.Discovery.MDNS.Enabled',
get(config, 'Discovery.MDNS.Enabled', true))
},
webRTCStar: {
enabled: get(options, 'config.Discovery.webRTCStar.Enabled',
get(config, 'Discovery.webRTCStar.Enabled', true))
},
bootstrap: {
list: get(options, 'config.Bootstrap',
get(config, 'Bootstrap', []))
}
},
relay: {
enabled: get(options, 'relay.enabled',
get(config, 'relay.enabled', false)),
hop: {
enabled: get(options, 'relay.hop.enabled',
get(config, 'relay.hop.enabled', false)),
active: get(options, 'relay.hop.active',
get(config, 'relay.hop.active', false))
}
},
dht: {
kBucketSize: get(options, 'dht.kBucketSize', 20),
enabled: get(options, 'dht.enabled', true) && !(get(options, 'offline', false)),
randomWalk: {
enabled: get(options, 'dht.randomWalk.enabled', true)
},
validators: {
ipns: ipnsUtils.validator
},
selectors: {
ipns: ipnsUtils.selector
}
},
EXPERIMENTAL: {
pubsub: get(options, 'EXPERIMENTAL.pubsub', false)
}
},
connectionManager: get(options, 'connectionManager',
{
maxPeers: get(config, 'Swarm.ConnMgr.HighWater'),
minPeers: get(config, 'Swarm.ConnMgr.LowWater')
})
}
const libp2pOptions = defaultsDeep(get(options, 'libp2p', {}), libp2pDefaults)
// Required inline to reduce startup time
// Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
const Node = require('../runtime/libp2p-nodejs')
return new Node(libp2pOptions)
}