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
404 lines (368 loc) · 12.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'use strict'
const log = require('debug')('ipfs:components:start')
const Bitswap = require('ipfs-bitswap')
const multiaddr = require('multiaddr')
const get = require('dlv')
const defer = require('p-defer')
const errCode = require('err-code')
const IPNS = require('../ipns')
const routingConfig = require('../ipns/routing/config')
const { AlreadyInitializedError, NotEnabledError } = require('../errors')
const Components = require('./')
const createMfsPreload = require('../mfs-preload')
const { withTimeoutOption } = require('../utils')
const WEBSOCKET_STAR_PROTO_CODE = 479
/**
* @param {Object} config
* @param {APIManager} config.apiManager
* @param {StartOptions} config.options
* @param {IPFSBlockService} config.blockService
* @param {GCLock} config.gcLock
* @param {InitOptions} config.initOptions
* @param {IPLD} config.ipld
* @param {Keychain} config.keychain
* @param {PeerId} config.peerId
* @param {PinManager} config.pinManager
* @param {Preload} config.preload
* @param {Print} config.print
* @param {IPFSRepo} config.repo
*/
module.exports = ({
apiManager,
options: constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
keychain,
peerId,
pinManager,
preload,
print,
repo
}) => {
async function start () {
const startPromise = defer()
startPromise.promise.catch((err) => log(err))
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.getAll()
const addrs = []
if (config.Addresses && config.Addresses.Swarm) {
config.Addresses.Swarm.forEach(addr => {
let ma = multiaddr(addr)
// Temporary error for users migrating using websocket-star multiaddrs for listenning on libp2p
// websocket-star support was removed from ipfs and libp2p
if (ma.protoCodes().includes(WEBSOCKET_STAR_PROTO_CODE)) {
throw errCode(new Error('websocket-star swarm addresses are not supported. See https://github.com/ipfs/js-ipfs/issues/2779'), 'ERR_WEBSOCKET_STAR_SWARM_ADDR_NOT_SUPPORTED')
}
// multiaddrs that go via a signalling server or other intermediary (e.g. stardust,
// webrtc-star) can have the intermediary's peer ID in the address, so append our
// peer ID to the end of it
const maId = ma.getPeerId()
if (maId && maId !== peerId.toB58String()) {
ma = ma.encapsulate(`/p2p/${peerId.toB58String()}`)
}
addrs.push(ma)
})
}
const libp2p = Components.libp2p({
options: constructorOptions,
repo,
peerId: peerId,
multiaddrs: addrs,
config
})
libp2p.keychain && await libp2p.loadKeychain()
await libp2p.start()
libp2p.transportManager.getAddrs().forEach(ma => print(`Swarm listening on ${ma}/p2p/${peerId.toB58String()}`))
const ipnsRouting = routingConfig({ libp2p, repo, peerId, options: constructorOptions })
const ipns = new IPNS(ipnsRouting, repo.datastore, peerId, keychain, { pass: initOptions.pass })
const bitswap = new Bitswap(libp2p, repo.blocks, { statsEnabled: true })
await bitswap.start()
blockService.setExchange(bitswap)
const dag = {
get: Components.dag.get({ ipld, preload }),
resolve: Components.dag.resolve({ ipld, preload }),
tree: Components.dag.tree({ ipld, preload }),
// FIXME: resolve this circular dependency
get put () {
const put = Components.dag.put({ ipld, pin, gcLock, preload })
Object.defineProperty(this, 'put', { value: put })
return put
}
}
const pinAddAll = Components.pin.addAll({ pinManager, gcLock, dag })
const pinRmAll = Components.pin.rmAll({ pinManager, gcLock, dag })
const pin = {
add: Components.pin.add({ addAll: pinAddAll }),
addAll: pinAddAll,
ls: Components.pin.ls({ pinManager, dag }),
rm: Components.pin.rm({ rmAll: pinRmAll }),
rmAll: pinRmAll
}
const block = {
get: Components.block.get({ blockService, preload }),
put: Components.block.put({ blockService, pin, gcLock, preload }),
rm: Components.block.rm({ blockService, gcLock, pinManager }),
stat: Components.block.stat({ blockService, preload })
}
const files = Components.files({ ipld, block, blockService, repo, preload, options: constructorOptions })
const mfsPreload = createMfsPreload({ files, preload, options: constructorOptions.preload })
await Promise.all([
ipns.republisher.start(),
preload.start(),
mfsPreload.start()
])
const api = createApi({
apiManager,
bitswap,
block,
blockService,
config,
constructorOptions,
dag,
files,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
mfsPreload,
peerId,
pin,
preload,
print,
repo
})
const { api: startedApi } = apiManager.update(api, () => undefined)
startPromise.resolve(startedApi)
return startedApi
} catch (err) {
cancel()
startPromise.reject(err)
throw err
}
}
return withTimeoutOption(start)
}
/**
* @param {CreateAPIConfig} config
*/
function createApi ({
apiManager,
bitswap,
block,
blockService,
config,
constructorOptions,
dag,
files,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
mfsPreload,
peerId,
pin,
preload,
print,
repo
}) {
const object = {
data: Components.object.data({ ipld, preload }),
get: Components.object.get({ ipld, preload }),
links: Components.object.links({ dag }),
new: Components.object.new({ ipld, preload }),
patch: {
addLink: Components.object.patch.addLink({ ipld, gcLock, preload }),
appendData: Components.object.patch.appendData({ ipld, gcLock, preload }),
rmLink: Components.object.patch.rmLink({ ipld, gcLock, preload }),
setData: Components.object.patch.setData({ ipld, gcLock, preload })
},
put: Components.object.put({ ipld, gcLock, preload }),
stat: Components.object.stat({ ipld, preload })
}
const addAll = Components.addAll({ block, preload, pin, gcLock, options: constructorOptions })
const isOnline = Components.isOnline({ libp2p })
const dhtNotEnabled = async () => { // eslint-disable-line require-await
throw new NotEnabledError('dht not enabled')
}
const dhtNotEnabledIterator = async function * () { // eslint-disable-line require-await,require-yield
throw new NotEnabledError('dht not enabled')
}
const dht = get(libp2p, '_config.dht.enabled', false) ? Components.dht({ libp2p, repo }) : {
get: dhtNotEnabled,
put: dhtNotEnabled,
findProvs: dhtNotEnabledIterator,
findPeer: dhtNotEnabled,
provide: dhtNotEnabledIterator,
query: dhtNotEnabledIterator
}
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, peerId, isOnline, keychain }),
resolve: Components.name.resolve({ dns, ipns, peerId, isOnline, options: constructorOptions })
}
const resolve = Components.resolve({ name, ipld })
const refs = Object.assign(
Components.refs({ ipld, resolve, preload }),
{ local: Components.refs.local({ repo }) }
)
const pubsubNotEnabled = async () => { // eslint-disable-line require-await
throw new NotEnabledError('pubsub not enabled')
}
const pubsub = get(constructorOptions, 'config.Pubsub.Enabled', get(config, 'Pubsub.Enabled', true))
? Components.pubsub({ libp2p })
: {
subscribe: pubsubNotEnabled,
unsubscribe: pubsubNotEnabled,
publish: pubsubNotEnabled,
ls: pubsubNotEnabled,
peers: pubsubNotEnabled
}
const api = {
add: Components.add({ addAll }),
addAll,
bitswap: {
stat: Components.bitswap.stat({ bitswap }),
unwant: Components.bitswap.unwant({ bitswap }),
wantlist: Components.bitswap.wantlist({ bitswap }),
wantlistForPeer: Components.bitswap.wantlistForPeer({ bitswap })
},
block,
bootstrap: {
add: Components.bootstrap.add({ repo }),
clear: Components.bootstrap.clear({ repo }),
list: Components.bootstrap.list({ repo }),
reset: Components.bootstrap.reset({ repo }),
rm: Components.bootstrap.rm({ repo })
},
cat: Components.cat({ ipld, preload }),
config: Components.config({ repo }),
dag,
dht,
dns,
files,
get: Components.get({ ipld, preload }),
id: Components.id({ peerId, libp2p }),
init: async () => { throw new AlreadyInitializedError() }, // eslint-disable-line require-await
isOnline,
ipld,
key: {
export: Components.key.export({ keychain }),
gen: Components.key.gen({ keychain }),
import: Components.key.import({ keychain }),
info: Components.key.info({ keychain }),
list: Components.key.list({ keychain }),
rename: Components.key.rename({ keychain }),
rm: Components.key.rm({ keychain })
},
libp2p,
ls: Components.ls({ ipld, preload }),
name,
object,
pin,
ping: Components.ping({ libp2p }),
pubsub,
refs,
repo: {
gc: Components.repo.gc({ gcLock, pin, refs, repo }),
stat: Components.repo.stat({ repo }),
version: Components.repo.version({ repo })
},
resolve,
start: () => apiManager.api,
stats: {
bitswap: Components.bitswap.stat({ bitswap }),
bw: libp2p.metrics
? Components.stats.bw({ libp2p })
: async () => { // eslint-disable-line require-await
throw new NotEnabledError('libp2p metrics not enabled')
},
repo: Components.repo.stat({ repo })
},
stop: Components.stop({
apiManager,
bitswap,
options: constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
mfsPreload,
peerId,
preload,
print,
repo
}),
swarm: {
addrs: Components.swarm.addrs({ libp2p }),
connect: Components.swarm.connect({ libp2p }),
disconnect: Components.swarm.disconnect({ libp2p }),
localAddrs: Components.swarm.localAddrs({ multiaddrs: libp2p.multiaddrs }),
peers: Components.swarm.peers({ libp2p })
},
version: Components.version({ repo })
}
return api
}
/**
* @typedef {Object} CreateAPIConfig
* @property {APIManager} apiManager
* @property {Bitswap} [bitswap]
* @property {Block} block
* @property {IPFSBlockService} blockService
* @property {Config} config
* @property {StartOptions} constructorOptions
* @property {DAG} dag
* @property {Files} [files]
* @property {GCLock} gcLock
* @property {InitOptions} initOptions
* @property {IPLD} ipld
* @property {import('../ipns')} ipns
* @property {Keychain} keychain
* @property {LibP2P} libp2p
* @property {MFSPreload} mfsPreload
* @property {PeerId} peerId
* @property {Pin} pin
* @property {Preload} preload
* @property {Print} print
* @property {IPFSRepo} repo
*
* @typedef {(...args:any[]) => void} Print
*
* @typedef {import('./init').InitOptions} InitOptions
* @typedef {import('./init').ConstructorOptions<boolean | InitOptions, true>} StartOptions
* @typedef {import('./init').Keychain} Keychain
* @typedef {import('../api-manager')} APIManager
* @typedef {import('./pin/pin-manager')} PinManager
* @typedef {import('../mfs-preload').MFSPreload} MFSPreload
* @typedef {import('.').IPFSBlockService} IPFSBlockService
* @typedef {import('.').GCLock} GCLock
* @typedef {import('.')} IPLD
* @typedef {import('.').PeerId} PeerId
* @typedef {import('.').Preload} Preload
* @typedef {import('.').IPFSRepo} IPFSRepo
* @typedef {import('.').LibP2P} LibP2P
* @typedef {import('.').Pin} Pin
* @typedef {import('.').Files} Files
* @typedef {import('.').DAG} DAG
* @typedef {import('.').Config} Config
* @typedef {import('.').Block} Block
*/