|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const promisify = require('promisify-es6') |
4 | 3 | const get = require('lodash/get') |
5 | 4 | const defaultsDeep = require('@nodeutils/defaults-deep') |
6 | 5 | const ipnsUtils = require('../ipns/routing/utils') |
7 | 6 |
|
8 | | -module.exports = function libp2p (self) { |
9 | | - return { |
10 | | - start: promisify((callback) => { |
11 | | - self.config.get(gotConfig) |
| 7 | +module.exports = function libp2p (self, config) { |
| 8 | + const options = self._options || {} |
| 9 | + config = config || {} |
12 | 10 |
|
13 | | - function gotConfig (err, config) { |
14 | | - if (err) { |
15 | | - return callback(err) |
16 | | - } |
| 11 | + // Always create libp2p via a bundle function |
| 12 | + const createBundle = typeof options.libp2p === 'function' |
| 13 | + ? options.libp2p |
| 14 | + : defaultBundle |
17 | 15 |
|
18 | | - const defaultBundle = (opts) => { |
19 | | - const libp2pDefaults = { |
20 | | - datastore: opts.datastore, |
21 | | - peerInfo: opts.peerInfo, |
22 | | - peerBook: opts.peerBook, |
23 | | - config: { |
24 | | - peerDiscovery: { |
25 | | - mdns: { |
26 | | - enabled: get(opts.options, 'config.Discovery.MDNS.Enabled', |
27 | | - get(opts.config, 'Discovery.MDNS.Enabled', true)) |
28 | | - }, |
29 | | - webRTCStar: { |
30 | | - enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled', |
31 | | - get(opts.config, 'Discovery.webRTCStar.Enabled', true)) |
32 | | - }, |
33 | | - bootstrap: { |
34 | | - list: get(opts.options, 'config.Bootstrap', |
35 | | - get(opts.config, 'Bootstrap', [])) |
36 | | - } |
37 | | - }, |
38 | | - relay: { |
39 | | - enabled: get(opts.options, 'relay.enabled', |
40 | | - get(opts.config, 'relay.enabled', false)), |
41 | | - hop: { |
42 | | - enabled: get(opts.options, 'relay.hop.enabled', |
43 | | - get(opts.config, 'relay.hop.enabled', false)), |
44 | | - active: get(opts.options, 'relay.hop.active', |
45 | | - get(opts.config, 'relay.hop.active', false)) |
46 | | - } |
47 | | - }, |
48 | | - dht: { |
49 | | - validators: { |
50 | | - ipns: ipnsUtils.validator |
51 | | - }, |
52 | | - selectors: { |
53 | | - ipns: ipnsUtils.selector |
54 | | - } |
55 | | - }, |
56 | | - EXPERIMENTAL: { |
57 | | - dht: get(opts.options, 'EXPERIMENTAL.dht', false), |
58 | | - pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false) |
59 | | - } |
60 | | - }, |
61 | | - connectionManager: get(opts.options, 'connectionManager', |
62 | | - get(opts.config, 'connectionManager', {})) |
63 | | - } |
| 16 | + const { datastore } = self._repo |
| 17 | + const peerInfo = self._peerInfo |
| 18 | + const peerBook = self._peerInfoBook |
| 19 | + const libp2p = createBundle({ options, config, datastore, peerInfo, peerBook }) |
| 20 | + let discoveredPeers = [] |
64 | 21 |
|
65 | | - const libp2pOptions = defaultsDeep( |
66 | | - get(self._options, 'libp2p', {}), |
67 | | - libp2pDefaults |
68 | | - ) |
| 22 | + const putAndDial = peerInfo => { |
| 23 | + peerBook.put(peerInfo) |
| 24 | + libp2p.dial(peerInfo, () => {}) |
| 25 | + } |
69 | 26 |
|
70 | | - // Required inline to reduce startup time |
71 | | - // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified |
72 | | - const Node = require('../runtime/libp2p-nodejs') |
73 | | - return new Node(libp2pOptions) |
74 | | - } |
| 27 | + libp2p.on('start', () => { |
| 28 | + peerInfo.multiaddrs.forEach((ma) => { |
| 29 | + self._print('Swarm listening on', ma.toString()) |
| 30 | + }) |
| 31 | + discoveredPeers.forEach(putAndDial) |
| 32 | + discoveredPeers = [] |
| 33 | + }) |
75 | 34 |
|
76 | | - // Always create libp2p via a bundle function |
77 | | - let libp2pBundle = get(self._options, 'libp2p', null) |
78 | | - if (typeof libp2pBundle !== 'function') { |
79 | | - libp2pBundle = defaultBundle |
80 | | - } |
| 35 | + libp2p.on('peer:discovery', (peerInfo) => { |
| 36 | + if (self.isOnline()) { |
| 37 | + putAndDial(peerInfo) |
| 38 | + } else { |
| 39 | + discoveredPeers.push(peerInfo) |
| 40 | + } |
| 41 | + }) |
81 | 42 |
|
82 | | - self._libp2pNode = libp2pBundle({ |
83 | | - options: self._options, |
84 | | - config: config, |
85 | | - datastore: self._repo.datastore, |
86 | | - peerInfo: self._peerInfo, |
87 | | - peerBook: self._peerInfoBook |
88 | | - }) |
| 43 | + libp2p.on('peer:connect', peerInfo => peerBook.put(peerInfo)) |
89 | 44 |
|
90 | | - let discoveredPeers = [] |
| 45 | + return libp2p |
| 46 | +} |
91 | 47 |
|
92 | | - const putAndDial = peerInfo => { |
93 | | - self._peerInfoBook.put(peerInfo) |
94 | | - self._libp2pNode.dial(peerInfo, () => {}) |
| 48 | +function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { |
| 49 | + const libp2pDefaults = { |
| 50 | + datastore, |
| 51 | + peerInfo, |
| 52 | + peerBook, |
| 53 | + config: { |
| 54 | + peerDiscovery: { |
| 55 | + mdns: { |
| 56 | + enabled: get(options, 'config.Discovery.MDNS.Enabled', |
| 57 | + get(config, 'Discovery.MDNS.Enabled', true)) |
| 58 | + }, |
| 59 | + webRTCStar: { |
| 60 | + enabled: get(options, 'config.Discovery.webRTCStar.Enabled', |
| 61 | + get(config, 'Discovery.webRTCStar.Enabled', true)) |
| 62 | + }, |
| 63 | + bootstrap: { |
| 64 | + list: get(options, 'config.Bootstrap', |
| 65 | + get(config, 'Bootstrap', [])) |
95 | 66 | } |
96 | | - |
97 | | - self._libp2pNode.on('start', () => { |
98 | | - discoveredPeers.forEach(putAndDial) |
99 | | - discoveredPeers = [] |
100 | | - }) |
101 | | - |
102 | | - self._libp2pNode.on('peer:discovery', (peerInfo) => { |
103 | | - if (self.isOnline()) { |
104 | | - putAndDial(peerInfo) |
105 | | - } else { |
106 | | - discoveredPeers.push(peerInfo) |
107 | | - } |
108 | | - }) |
109 | | - |
110 | | - self._libp2pNode.on('peer:connect', (peerInfo) => { |
111 | | - self._peerInfoBook.put(peerInfo) |
112 | | - }) |
113 | | - |
114 | | - self._libp2pNode.start((err) => { |
115 | | - if (err) { return callback(err) } |
116 | | - |
117 | | - self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => { |
118 | | - self._print('Swarm listening on', ma.toString()) |
119 | | - }) |
120 | | - |
121 | | - callback() |
122 | | - }) |
| 67 | + }, |
| 68 | + relay: { |
| 69 | + enabled: get(options, 'relay.enabled', |
| 70 | + get(config, 'relay.enabled', false)), |
| 71 | + hop: { |
| 72 | + enabled: get(options, 'relay.hop.enabled', |
| 73 | + get(config, 'relay.hop.enabled', false)), |
| 74 | + active: get(options, 'relay.hop.active', |
| 75 | + get(config, 'relay.hop.active', false)) |
| 76 | + } |
| 77 | + }, |
| 78 | + dht: { |
| 79 | + validators: { |
| 80 | + ipns: ipnsUtils.validator |
| 81 | + }, |
| 82 | + selectors: { |
| 83 | + ipns: ipnsUtils.selector |
| 84 | + } |
| 85 | + }, |
| 86 | + EXPERIMENTAL: { |
| 87 | + dht: get(options, 'EXPERIMENTAL.dht', false), |
| 88 | + pubsub: get(options, 'EXPERIMENTAL.pubsub', false) |
123 | 89 | } |
124 | | - }), |
125 | | - stop: promisify((callback) => { |
126 | | - self._libp2pNode.stop(callback) |
127 | | - }) |
| 90 | + }, |
| 91 | + connectionManager: get(options, 'connectionManager', |
| 92 | + get(config, 'connectionManager', {})) |
128 | 93 | } |
| 94 | + |
| 95 | + const libp2pOptions = defaultsDeep(get(options, 'libp2p', {}), libp2pDefaults) |
| 96 | + |
| 97 | + // Required inline to reduce startup time |
| 98 | + // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified |
| 99 | + const Node = require('../runtime/libp2p-nodejs') |
| 100 | + return new Node(libp2pOptions) |
129 | 101 | } |
0 commit comments