This repository has been 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.3k
/
Copy pathstart.js
160 lines (144 loc) · 3.84 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
const Bitswap = require('ipfs-bitswap')
const PeerBook = require('peer-book')
const IPNS = require('../ipns')
const routingConfig = require('../ipns/routing/config')
const defer = require('p-defer')
const { AlreadyInitializedError } = require('../errors')
const Components = require('./')
module.exports = ({
apiManager,
options: constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
keychain,
peerInfo,
pinManager,
preload,
print,
repo
}) => async function start () {
const startPromise = defer()
const { cancel } = apiManager.update({ start: () => startPromise.promise })
try {
// The repo may be closed if previously stopped
if (repo.closed) {
await repo.open()
}
const config = await repo.config.get()
const peerBook = new PeerBook()
const libp2p = Components.legacy.libp2p({
_options: constructorOptions,
_repo: repo,
_peerInfo: peerInfo,
_peerInfoBook: peerBook,
_print: print
}, config)
await libp2p.start()
const ipnsRouting = routingConfig({
_options: constructorOptions,
libp2p,
_repo: repo,
_peerInfo: peerInfo
})
const ipns = new IPNS(ipnsRouting, repo.datastore, peerInfo, keychain, { pass: initOptions.pass })
const bitswap = new Bitswap(libp2p, repo.blocks, { statsEnabled: true })
await bitswap.start()
blockService.setExchange(bitswap)
await preload.start()
await ipns.republisher.start()
// TODO: start mfs preload here
const api = createApi({
apiManager,
bitswap,
constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
peerInfo,
pinManager,
preload,
print,
repo
})
apiManager.update(api, () => undefined)
} catch (err) {
cancel()
startPromise.reject(err)
throw err
}
startPromise.resolve(apiManager.api)
return apiManager.api
}
function createApi ({
apiManager,
bitswap,
constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
peerInfo,
pinManager,
preload,
print,
repo
}) {
const dag = Components.legacy.dag({ _ipld: ipld, _preload: preload })
const object = Components.legacy.object({ _ipld: ipld, _preload: preload, dag, _gcLock: gcLock })
const pin = Components.legacy.pin({ _ipld: ipld, _preload: preload, object, _repo: repo, _pinManager: pinManager })
const add = Components.add({ ipld, dag, preload, pin, gcLock, options: constructorOptions })
const isOnline = Components.isOnline({ libp2p })
const dns = Components.dns()
const name = {
pubsub: {
cancel: Components.name.pubsub.cancel({ ipns, options: constructorOptions }),
state: Components.name.pubsub.state({ ipns, options: constructorOptions }),
subs: Components.name.pubsub.subs({ ipns, options: constructorOptions })
},
publish: Components.name.publish({ ipns, dag, peerInfo, isOnline, keychain, options: constructorOptions }),
resolve: Components.name.resolve({ dns, ipns, peerInfo, isOnline, options: constructorOptions })
}
const resolve = Components.resolve({ name, ipld })
const refs = Components.refs({ ipld, resolve, preload })
refs.local = Components.refs.local({ repo })
const api = {
add,
cat: Components.cat({ ipld, preload }),
config: Components.config({ repo }),
dns,
get: Components.get({ ipld, preload }),
init: () => { throw new AlreadyInitializedError() },
ls: Components.ls({ ipld, preload }),
name,
refs,
resolve,
start: () => apiManager.api,
stop: Components.stop({
apiManager,
bitswap,
options: constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
peerInfo,
preload,
print,
repo
})
}
return api
}