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 pathstart.js
81 lines (67 loc) · 2.51 KB
/
start.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
'use strict'
const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const get = require('dlv')
const setImmediate = require('async/setImmediate')
const promisify = require('promisify-es6')
const { TieredDatastore } = require('datastore-core')
const IPNS = require('../ipns')
const PubsubDatastore = require('../ipns/routing/pubsub-datastore')
const OfflineDatastore = require('../ipns/routing/offline-datastore')
module.exports = (self) => {
return promisify((callback) => {
const done = (err) => {
if (err) {
setImmediate(() => self.emit('error', err))
return callback(err)
}
self.state.started()
setImmediate(() => self.emit('start'))
callback()
}
if (self.state.state() !== 'stopped') {
return done(new Error(`Not able to start from state: ${self.state.state()}`))
}
self.log('starting')
self.state.start()
series([
(cb) => {
// The repo may be closed if previously stopped
self._repo.closed
? self._repo.open(cb)
: cb()
},
(cb) => self.libp2p.start(cb),
(cb) => {
// Setup online routing for IPNS with a tiered routing composed by a DHT and a Pubsub router (if properly enabled)
const ipnsStores = []
// Add IPNS pubsub if enabled
let pubsubDs
if (get(self._options, 'EXPERIMENTAL.ipnsPubsub', false)) {
const pubsub = self._libp2pNode.pubsub
const localDatastore = self._repo.datastore
const peerId = self._peerInfo.id
pubsubDs = new PubsubDatastore(pubsub, localDatastore, peerId)
ipnsStores.push(pubsubDs)
}
// NOTE: IPNS routing is being replaced by the local repo datastore while the IPNS over DHT is not ready
// When DHT is added, if local option enabled, should receive offlineDatastore as well
const offlineDatastore = new OfflineDatastore(self._repo)
ipnsStores.push(offlineDatastore)
// Create ipns routing with a set of datastores
const routing = new TieredDatastore(ipnsStores)
self._ipns = new IPNS(routing, self._repo, self._peerInfo, self._keychain, self._options)
self._bitswap = new Bitswap(
self._libp2pNode,
self._repo.blocks,
{ statsEnabled: true }
)
self._bitswap.start()
self._blockService.setExchange(self._bitswap)
self._preload.start()
self._ipns.republisher.start()
self._mfsPreload.start(cb)
}
], done)
})
}