This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
532 lines (410 loc) · 11.7 KB
/
index.ts
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import errCode from 'err-code'
import type { ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection'
import type { ContentRouting } from '@libp2p/interface-content-routing'
import type { AddressManager } from '@libp2p/interface-address-manager'
import { isStartable, Startable } from '@libp2p/interfaces/startable'
import type { Metrics } from '@libp2p/interface-metrics'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerRouting } from '@libp2p/interface-peer-routing'
import type { PeerStore } from '@libp2p/interface-peer-store'
import type { Registrar } from '@libp2p/interface-registrar'
import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
import type { Datastore } from 'interface-datastore'
import type { PubSub } from '@libp2p/interface-pubsub'
import type { DualDHT } from '@libp2p/interface-dht'
import type { ConnectionManager, Dialer } from '@libp2p/interface-connection-manager'
export interface Initializable {
init: (components: Components) => void
}
export function isInitializable (obj: any): obj is Initializable {
return obj != null && typeof obj.init === 'function'
}
export interface ComponentsInit {
peerId?: PeerId
addressManager?: AddressManager
peerStore?: PeerStore
upgrader?: Upgrader
metrics?: Metrics
registrar?: Registrar
connectionManager?: ConnectionManager
transportManager?: TransportManager
connectionGater?: ConnectionGater
contentRouting?: ContentRouting
peerRouting?: PeerRouting
datastore?: Datastore
connectionProtector?: ConnectionProtector
dht?: DualDHT
pubsub?: PubSub
dialer?: Dialer
}
export class Components implements Startable {
private _peerId?: PeerId
private _addressManager?: AddressManager
private _peerStore?: PeerStore
private _upgrader?: Upgrader
private _metrics?: Metrics
private _registrar?: Registrar
private _connectionManager?: ConnectionManager
private _transportManager?: TransportManager
private _connectionGater?: ConnectionGater
private _contentRouting?: ContentRouting
private _peerRouting?: PeerRouting
private _datastore?: Datastore
private _connectionProtector?: ConnectionProtector
private _dht?: DualDHT
private _pubSub?: PubSub
private _dialer?: Dialer
private _started = false
constructor (init: ComponentsInit = {}) {
if (init.peerId != null) {
this.setPeerId(init.peerId)
}
if (init.addressManager != null) {
this.setAddressManager(init.addressManager)
}
if (init.peerStore != null) {
this.setPeerStore(init.peerStore)
}
if (init.upgrader != null) {
this.setUpgrader(init.upgrader)
}
if (init.metrics != null) {
this.setMetrics(init.metrics)
}
if (init.registrar != null) {
this.setRegistrar(init.registrar)
}
if (init.connectionManager != null) {
this.setConnectionManager(init.connectionManager)
}
if (init.transportManager != null) {
this.setTransportManager(init.transportManager)
}
if (init.connectionGater != null) {
this.setConnectionGater(init.connectionGater)
}
if (init.contentRouting != null) {
this.setContentRouting(init.contentRouting)
}
if (init.peerRouting != null) {
this.setPeerRouting(init.peerRouting)
}
if (init.datastore != null) {
this.setDatastore(init.datastore)
}
if (init.connectionProtector != null) {
this.setConnectionProtector(init.connectionProtector)
}
if (init.dht != null) {
this.setDHT(init.dht)
}
if (init.pubsub != null) {
this.setPubSub(init.pubsub)
}
if (init.dialer != null) {
this.setDialer(init.dialer)
}
}
isStarted () {
return this._started
}
async beforeStart () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
if (startable.beforeStart != null) {
await startable.beforeStart()
}
})
)
}
async start () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
await startable.start()
})
)
this._started = true
}
async afterStart () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
if (startable.afterStart != null) {
await startable.afterStart()
}
})
)
}
async beforeStop () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
if (startable.beforeStop != null) {
await startable.beforeStop()
}
})
)
}
async stop () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
await startable.stop()
})
)
this._started = false
}
async afterStop () {
await Promise.all(
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
if (startable.afterStop != null) {
await startable.afterStop()
}
})
)
}
get peerId (): PeerId {
if (this._peerId == null) {
throw errCode(new Error('peerId not set'), 'ERR_SERVICE_MISSING')
}
return this._peerId
}
get metrics (): Metrics | undefined {
return this._metrics
}
get addressManager (): AddressManager {
if (this._addressManager == null) {
throw errCode(new Error('addressManager not set'), 'ERR_SERVICE_MISSING')
}
return this._addressManager
}
get peerStore (): PeerStore {
if (this._peerStore == null) {
throw errCode(new Error('peerStore not set'), 'ERR_SERVICE_MISSING')
}
return this._peerStore
}
get upgrader (): Upgrader {
if (this._upgrader == null) {
throw errCode(new Error('upgrader not set'), 'ERR_SERVICE_MISSING')
}
return this._upgrader
}
get registrar (): Registrar {
if (this._registrar == null) {
throw errCode(new Error('registrar not set'), 'ERR_SERVICE_MISSING')
}
return this._registrar
}
get connectionManager (): ConnectionManager {
if (this._connectionManager == null) {
throw errCode(new Error('connectionManager not set'), 'ERR_SERVICE_MISSING')
}
return this._connectionManager
}
get transportManager (): TransportManager {
if (this._transportManager == null) {
throw errCode(new Error('transportManager not set'), 'ERR_SERVICE_MISSING')
}
return this._transportManager
}
get connectionGater (): ConnectionGater {
if (this._connectionGater == null) {
throw errCode(new Error('connectionGater not set'), 'ERR_SERVICE_MISSING')
}
return this._connectionGater
}
get contentRouting (): ContentRouting {
if (this._contentRouting == null) {
throw errCode(new Error('contentRouting not set'), 'ERR_SERVICE_MISSING')
}
return this._contentRouting
}
get peerRouting (): PeerRouting {
if (this._peerRouting == null) {
throw errCode(new Error('peerRouting not set'), 'ERR_SERVICE_MISSING')
}
return this._peerRouting
}
get datastore (): Datastore {
if (this._datastore == null) {
throw errCode(new Error('datastore not set'), 'ERR_SERVICE_MISSING')
}
return this._datastore
}
get connectionProtector (): ConnectionProtector | undefined {
return this._connectionProtector
}
get dht (): DualDHT {
if (this._dht == null) {
throw errCode(new Error('dht not set'), 'ERR_SERVICE_MISSING')
}
return this._dht
}
get pubSub (): PubSub {
if (this._pubSub == null) {
throw errCode(new Error('pubsub not set'), 'ERR_SERVICE_MISSING')
}
return this._pubSub
}
get dialer (): Dialer {
if (this._dialer == null) {
throw errCode(new Error('dialer not set'), 'ERR_SERVICE_MISSING')
}
return this._dialer
}
setPeerId (peerId: PeerId) {
this._peerId = peerId
return peerId
}
getPeerId (): PeerId {
return this.peerId
}
setMetrics (metrics: Metrics) {
this._metrics = metrics
if (isInitializable(metrics)) {
metrics.init(this)
}
return metrics
}
getMetrics (): Metrics | undefined {
return this.metrics
}
setAddressManager (addressManager: AddressManager) {
this._addressManager = addressManager
if (isInitializable(addressManager)) {
addressManager.init(this)
}
return addressManager
}
getAddressManager (): AddressManager {
return this.addressManager
}
setPeerStore (peerStore: PeerStore) {
this._peerStore = peerStore
if (isInitializable(peerStore)) {
peerStore.init(this)
}
return peerStore
}
getPeerStore (): PeerStore {
return this.peerStore
}
setUpgrader (upgrader: Upgrader) {
this._upgrader = upgrader
if (isInitializable(upgrader)) {
upgrader.init(this)
}
return upgrader
}
getUpgrader (): Upgrader {
return this.upgrader
}
setRegistrar (registrar: Registrar) {
this._registrar = registrar
if (isInitializable(registrar)) {
registrar.init(this)
}
return registrar
}
getRegistrar (): Registrar {
return this.registrar
}
setConnectionManager (connectionManager: ConnectionManager) {
this._connectionManager = connectionManager
if (isInitializable(connectionManager)) {
connectionManager.init(this)
}
return connectionManager
}
getConnectionManager (): ConnectionManager {
return this.connectionManager
}
setTransportManager (transportManager: TransportManager) {
this._transportManager = transportManager
if (isInitializable(transportManager)) {
transportManager.init(this)
}
return transportManager
}
getTransportManager (): TransportManager {
return this.transportManager
}
setConnectionGater (connectionGater: ConnectionGater) {
this._connectionGater = connectionGater
if (isInitializable(connectionGater)) {
connectionGater.init(this)
}
return connectionGater
}
getConnectionGater (): ConnectionGater {
return this.connectionGater
}
setContentRouting (contentRouting: ContentRouting) {
this._contentRouting = contentRouting
if (isInitializable(contentRouting)) {
contentRouting.init(this)
}
return contentRouting
}
getContentRouting (): ContentRouting {
return this.contentRouting
}
setPeerRouting (peerRouting: PeerRouting) {
this._peerRouting = peerRouting
if (isInitializable(peerRouting)) {
peerRouting.init(this)
}
return peerRouting
}
getPeerRouting (): PeerRouting {
return this.peerRouting
}
setDatastore (datastore: Datastore) {
this._datastore = datastore
if (isInitializable(datastore)) {
datastore.init(this)
}
return datastore
}
getDatastore (): Datastore {
return this.datastore
}
setConnectionProtector (connectionProtector: ConnectionProtector) {
this._connectionProtector = connectionProtector
if (isInitializable(connectionProtector)) {
connectionProtector.init(this)
}
return connectionProtector
}
getConnectionProtector (): ConnectionProtector | undefined {
return this.connectionProtector
}
setDHT (dht: DualDHT) {
this._dht = dht
if (isInitializable(dht)) {
dht.init(this)
}
return dht
}
getDHT (): DualDHT {
return this.dht
}
setPubSub (pubsub: PubSub) {
this._pubSub = pubsub
if (isInitializable(pubsub)) {
pubsub.init(this)
}
return pubsub
}
getPubSub (): PubSub {
return this.pubSub
}
setDialer (dialer: Dialer) {
this._dialer = dialer
if (isInitializable(dialer)) {
dialer.init(this)
}
return dialer
}
getDialer (): Dialer {
return this.dialer
}
}