libp2p 0.43.4 → 0.44.0-0d46c78f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +29 -29
- package/dist/src/address-manager/index.d.ts +4 -4
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +9 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +19 -19
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.js +2 -2
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +7 -3
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +3 -1
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +3 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +6 -2
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +3 -0
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +5 -4
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +4 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +11 -6
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/circuit-relay/utils.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +7 -69
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +60 -174
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +8 -0
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
- package/dist/src/config/connection-gater.browser.js +27 -0
- package/dist/src/config/connection-gater.browser.js.map +1 -0
- package/dist/src/config/connection-gater.d.ts +6 -0
- package/dist/src/config/connection-gater.d.ts.map +1 -0
- package/dist/src/config/connection-gater.js +18 -0
- package/dist/src/config/connection-gater.js.map +1 -0
- package/dist/src/config.d.ts +2 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -71
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +39 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +136 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +32 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +111 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +78 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +372 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +70 -73
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +118 -247
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +0 -10
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +7 -42
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +3 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/fetch/index.d.ts +13 -35
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +10 -12
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +16 -48
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +203 -201
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -67
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +3 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +12 -35
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +141 -206
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -38
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +0 -59
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/constants.d.ts +4 -0
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +10 -0
- package/dist/src/ping/constants.js.map +1 -1
- package/dist/src/ping/index.d.ts +10 -27
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +18 -21
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -2
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +27 -25
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +15 -4
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +37 -12
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +16 -11
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +38 -47
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +42 -0
- package/dist/src/upnp-nat/index.d.ts.map +1 -0
- package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
- package/dist/src/upnp-nat/index.js.map +1 -0
- package/package.json +70 -54
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +20 -22
- package/src/circuit-relay/server/advert-service.ts +2 -2
- package/src/circuit-relay/server/index.ts +7 -3
- package/src/circuit-relay/transport/discovery.ts +3 -1
- package/src/circuit-relay/transport/index.ts +10 -2
- package/src/circuit-relay/transport/listener.ts +8 -4
- package/src/circuit-relay/transport/reservation-store.ts +15 -7
- package/src/circuit-relay/utils.ts +3 -1
- package/src/components.ts +72 -251
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +3 -73
- package/src/connection-manager/auto-dial.ts +189 -0
- package/src/connection-manager/connection-pruner.ts +157 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +506 -0
- package/src/connection-manager/index.ts +194 -339
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/index.ts +7 -55
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +55 -18
- package/src/identify/index.ts +238 -228
- package/src/index.ts +12 -76
- package/src/insecure/index.ts +9 -5
- package/src/libp2p.ts +146 -265
- package/src/peer-routing.ts +1 -101
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +32 -28
- package/src/registrar.ts +31 -27
- package/src/transport-manager.ts +49 -15
- package/src/upgrader.ts +62 -57
- package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -121
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -324
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -1
- package/dist/src/dht/dht-content-routing.d.ts +0 -17
- package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-content-routing.js +0 -32
- package/dist/src/dht/dht-content-routing.js.map +0 -1
- package/dist/src/dht/dht-peer-routing.d.ts +0 -15
- package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-peer-routing.js +0 -26
- package/dist/src/dht/dht-peer-routing.js.map +0 -1
- package/dist/src/dht/dummy-dht.d.ts +0 -20
- package/dist/src/dht/dummy-dht.d.ts.map +0 -1
- package/dist/src/dht/dummy-dht.js +0 -46
- package/dist/src/dht/dummy-dht.js.map +0 -1
- package/dist/src/nat-manager.d.ts +0 -74
- package/dist/src/nat-manager.d.ts.map +0 -1
- package/dist/src/nat-manager.js.map +0 -1
- package/dist/src/peer-record-updater.d.ts +0 -24
- package/dist/src/peer-record-updater.d.ts.map +0 -1
- package/dist/src/peer-record-updater.js +0 -44
- package/dist/src/peer-record-updater.js.map +0 -1
- package/dist/typedoc-urls.json +0 -41
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -461
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
- package/src/dht/dht-content-routing.ts +0 -44
- package/src/dht/dht-peer-routing.ts +0 -36
- package/src/dht/dummy-dht.ts +0 -60
- package/src/peer-record-updater.ts +0 -65
package/src/libp2p.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
3
3
|
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
4
|
-
import { Startable, isStartable } from '@libp2p/interfaces/startable'
|
|
5
4
|
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
|
|
6
5
|
import { MemoryDatastore } from 'datastore-core/memory'
|
|
7
6
|
import { DefaultPeerRouting } from './peer-routing.js'
|
|
@@ -9,256 +8,199 @@ import { CompoundContentRouting } from './content-routing/index.js'
|
|
|
9
8
|
import { codes } from './errors.js'
|
|
10
9
|
import { DefaultAddressManager } from './address-manager/index.js'
|
|
11
10
|
import { DefaultConnectionManager } from './connection-manager/index.js'
|
|
12
|
-
import { AutoDialler } from './connection-manager/auto-dialler.js'
|
|
13
11
|
import { DefaultKeyChain } from '@libp2p/keychain'
|
|
14
12
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
15
13
|
import { DefaultUpgrader } from './upgrader.js'
|
|
16
14
|
import { DefaultRegistrar } from './registrar.js'
|
|
17
|
-
import { IdentifyService } from './identify/index.js'
|
|
18
|
-
import { FetchService } from './fetch/index.js'
|
|
19
|
-
import { PingService } from './ping/index.js'
|
|
20
|
-
import { NatManager } from './nat-manager.js'
|
|
21
|
-
import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
22
|
-
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
23
15
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
24
|
-
import {
|
|
25
|
-
import { AutonatService } from './autonat/index.js'
|
|
26
|
-
import { DefaultComponents } from './components.js'
|
|
16
|
+
import { defaultComponents } from './components.js'
|
|
27
17
|
import type { Components } from './components.js'
|
|
28
18
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
29
19
|
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
30
|
-
import type {
|
|
31
|
-
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
32
|
-
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
33
|
-
import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
|
|
34
|
-
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
20
|
+
import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
|
|
35
21
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
36
|
-
import type { Libp2p,
|
|
37
|
-
import { validateConfig } from './config.js'
|
|
38
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
22
|
+
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
|
|
39
23
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
40
|
-
import type { DualDHT } from '@libp2p/interface-dht'
|
|
41
24
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
42
25
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
43
26
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
44
|
-
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
45
27
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
46
|
-
import { DummyDHT } from './dht/dummy-dht.js'
|
|
47
|
-
import { DummyPubSub } from './pubsub/dummy-pubsub.js'
|
|
48
28
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
49
|
-
import {
|
|
29
|
+
import type { KeyChain } from '@libp2p/interface-keychain'
|
|
30
|
+
import type { Libp2pEvents, PendingDial, ServiceMap } from '@libp2p/interface-libp2p'
|
|
31
|
+
import { setMaxListeners } from 'events'
|
|
32
|
+
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
50
33
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
51
34
|
import type { Datastore } from 'interface-datastore'
|
|
52
|
-
import type { KeyChain } from '@libp2p/interface-keychain'
|
|
53
35
|
import mergeOptions from 'merge-options'
|
|
54
|
-
import
|
|
36
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
37
|
+
import { validateConfig } from './config.js'
|
|
38
|
+
import { ContentRouting, contentRouting } from '@libp2p/interface-content-routing'
|
|
39
|
+
import { PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
|
|
40
|
+
import { peerDiscovery } from '@libp2p/interface-peer-discovery'
|
|
41
|
+
import { connectionGater } from './config/connection-gater.js'
|
|
55
42
|
|
|
56
43
|
const log = logger('libp2p')
|
|
57
44
|
|
|
58
|
-
export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
45
|
+
export class Libp2pNode<T extends ServiceMap = {}> extends EventEmitter<Libp2pEvents> implements Libp2p<T> {
|
|
59
46
|
public peerId: PeerId
|
|
60
|
-
public dht: DualDHT
|
|
61
|
-
public pubsub: PubSub
|
|
62
|
-
public identifyService: IdentifyService
|
|
63
|
-
public circuitService?: CircuitRelayService
|
|
64
|
-
public fetchService: FetchService
|
|
65
|
-
public pingService: PingService
|
|
66
|
-
public autonatService: AutonatService
|
|
67
|
-
public components: Components
|
|
68
47
|
public peerStore: PeerStore
|
|
69
48
|
public contentRouting: ContentRouting
|
|
70
49
|
public peerRouting: PeerRouting
|
|
71
50
|
public keychain: KeyChain
|
|
72
|
-
public connectionManager: ConnectionManager
|
|
73
|
-
public registrar: Registrar
|
|
74
51
|
public metrics?: Metrics
|
|
52
|
+
public services: T
|
|
75
53
|
|
|
76
|
-
|
|
77
|
-
|
|
54
|
+
public components: Components
|
|
55
|
+
#started: boolean
|
|
78
56
|
|
|
79
|
-
constructor (init: Libp2pInit) {
|
|
57
|
+
constructor (init: Libp2pInit<T>) {
|
|
80
58
|
super()
|
|
81
59
|
|
|
82
|
-
this
|
|
60
|
+
// event bus - components can listen to this emitter to be notified of system events
|
|
61
|
+
// and also cause them to be emitted
|
|
62
|
+
const events = new EventEmitter<Libp2pEvents>()
|
|
63
|
+
const originalDispatch = events.dispatchEvent.bind(events)
|
|
64
|
+
events.dispatchEvent = (evt: any) => {
|
|
65
|
+
const internalResult = originalDispatch(evt)
|
|
66
|
+
const externalResult = this.dispatchEvent(
|
|
67
|
+
new CustomEvent(evt.type, { detail: evt.detail })
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return internalResult || externalResult
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
// This emitter gets listened to a lot
|
|
75
|
+
setMaxListeners?.(Infinity, events)
|
|
76
|
+
} catch {}
|
|
77
|
+
|
|
78
|
+
this.#started = false
|
|
83
79
|
this.peerId = init.peerId
|
|
84
|
-
|
|
80
|
+
// @ts-expect-error {} may not be of type T
|
|
81
|
+
this.services = {}
|
|
82
|
+
const components = this.components = defaultComponents({
|
|
85
83
|
peerId: init.peerId,
|
|
84
|
+
events,
|
|
86
85
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
87
|
-
connectionGater:
|
|
88
|
-
denyDialPeer: async () => await Promise.resolve(false),
|
|
89
|
-
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
90
|
-
denyInboundConnection: async () => await Promise.resolve(false),
|
|
91
|
-
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
92
|
-
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
93
|
-
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
94
|
-
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
95
|
-
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
96
|
-
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
97
|
-
...init.connectionGater
|
|
98
|
-
}
|
|
86
|
+
connectionGater: connectionGater(init.connectionGater)
|
|
99
87
|
})
|
|
100
|
-
|
|
88
|
+
|
|
89
|
+
this.peerStore = this.configureComponent('peerStore', new PersistentPeerStore(components, {
|
|
101
90
|
addressFilter: this.components.connectionGater.filterMultiaddrForPeer,
|
|
102
91
|
...init.peerStore
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
this.services = [
|
|
106
|
-
components
|
|
107
|
-
]
|
|
92
|
+
}))
|
|
108
93
|
|
|
109
94
|
// Create Metrics
|
|
110
95
|
if (init.metrics != null) {
|
|
111
|
-
this.metrics = this.
|
|
96
|
+
this.metrics = this.configureComponent('metrics', init.metrics(this.components))
|
|
112
97
|
}
|
|
113
98
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peerData }))
|
|
99
|
+
components.events.addEventListener('peer:update', evt => {
|
|
100
|
+
// if there was no peer previously in the peer store this is a new peer
|
|
101
|
+
if (evt.detail.previous == null) {
|
|
102
|
+
this.safeDispatchEvent('peer:discovery', { detail: evt.detail.peer })
|
|
103
|
+
}
|
|
120
104
|
})
|
|
121
105
|
|
|
122
106
|
// Set up connection protector if configured
|
|
123
107
|
if (init.connectionProtector != null) {
|
|
124
|
-
this.
|
|
108
|
+
this.configureComponent('connectionProtector', init.connectionProtector(components))
|
|
125
109
|
}
|
|
126
110
|
|
|
127
111
|
// Set up the Upgrader
|
|
128
112
|
this.components.upgrader = new DefaultUpgrader(this.components, {
|
|
129
|
-
connectionEncryption: (init.connectionEncryption ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
130
|
-
muxers: (init.streamMuxers ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
113
|
+
connectionEncryption: (init.connectionEncryption ?? []).map((fn, index) => this.configureComponent(`connection-encryption-${index}`, fn(this.components))),
|
|
114
|
+
muxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
|
|
131
115
|
inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
|
|
132
116
|
})
|
|
133
117
|
|
|
134
|
-
//
|
|
135
|
-
this.
|
|
118
|
+
// Setup the transport manager
|
|
119
|
+
this.configureComponent('transportManager', new DefaultTransportManager(this.components, init.transportManager))
|
|
136
120
|
|
|
137
121
|
// Create the Connection Manager
|
|
138
|
-
this.connectionManager
|
|
139
|
-
|
|
140
|
-
// forward connection manager events
|
|
141
|
-
this.components.connectionManager.addEventListener('peer:disconnect', (event) => {
|
|
142
|
-
this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: event.detail }))
|
|
143
|
-
})
|
|
144
|
-
this.components.connectionManager.addEventListener('peer:connect', (event) => {
|
|
145
|
-
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: event.detail }))
|
|
146
|
-
})
|
|
122
|
+
this.configureComponent('connectionManager', new DefaultConnectionManager(this.components, init.connectionManager))
|
|
147
123
|
|
|
148
124
|
// Create the Registrar
|
|
149
|
-
this.registrar
|
|
150
|
-
|
|
151
|
-
// Setup the transport manager
|
|
152
|
-
this.components.transportManager = new DefaultTransportManager(this.components, init.transportManager)
|
|
125
|
+
this.configureComponent('registrar', new DefaultRegistrar(this.components))
|
|
153
126
|
|
|
154
127
|
// Addresses {listen, announce, noAnnounce}
|
|
155
|
-
this.
|
|
156
|
-
|
|
157
|
-
// update our peer record when addresses change
|
|
158
|
-
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
128
|
+
this.configureComponent('addressManager', new DefaultAddressManager(this.components, init.addresses))
|
|
159
129
|
|
|
160
130
|
// Create keychain
|
|
161
131
|
const keychainOpts = DefaultKeyChain.generateOptions()
|
|
162
|
-
this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
|
|
132
|
+
this.keychain = this.configureComponent('keyChain', new DefaultKeyChain(this.components, {
|
|
163
133
|
...keychainOpts,
|
|
164
134
|
...init.keychain
|
|
165
135
|
}))
|
|
166
136
|
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// Add the identify service
|
|
171
|
-
this.identifyService = new IdentifyService(this.components, {
|
|
172
|
-
...init.identify
|
|
173
|
-
})
|
|
174
|
-
this.configureComponent(this.identifyService)
|
|
175
|
-
|
|
176
|
-
// dht provided components (peerRouting, contentRouting, dht)
|
|
177
|
-
if (init.dht != null) {
|
|
178
|
-
this.dht = this.components.dht = init.dht(this.components)
|
|
179
|
-
} else {
|
|
180
|
-
this.dht = new DummyDHT()
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Create pubsub if provided
|
|
184
|
-
if (init.pubsub != null) {
|
|
185
|
-
this.pubsub = this.components.pubsub = init.pubsub(this.components)
|
|
186
|
-
} else {
|
|
187
|
-
this.pubsub = new DummyPubSub()
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Attach remaining APIs
|
|
191
|
-
// peer and content routing will automatically get modules from _modules and _dht
|
|
192
|
-
|
|
193
|
-
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
194
|
-
|
|
195
|
-
if (init.dht != null) {
|
|
196
|
-
// add dht to routers
|
|
197
|
-
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))
|
|
198
|
-
|
|
199
|
-
// use dht for peer discovery
|
|
200
|
-
this.dht.addEventListener('peer', (evt) => {
|
|
201
|
-
this.onDiscoveryPeer(evt)
|
|
202
|
-
})
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
this.peerRouting = this.components.peerRouting = this.configureComponent(new DefaultPeerRouting(this.components, {
|
|
206
|
-
...init.peerRouting,
|
|
137
|
+
// Peer routers
|
|
138
|
+
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map((fn, index) => this.configureComponent(`peer-router-${index}`, fn(this.components)))
|
|
139
|
+
this.peerRouting = this.components.peerRouting = this.configureComponent('peerRouting', new DefaultPeerRouting(this.components, {
|
|
207
140
|
routers: peerRouters
|
|
208
141
|
}))
|
|
209
142
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
// add dht to routers
|
|
214
|
-
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
this.contentRouting = this.components.contentRouting = this.configureComponent(new CompoundContentRouting(this.components, {
|
|
143
|
+
// Content routers
|
|
144
|
+
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map((fn, index) => this.configureComponent(`content-router-${index}`, fn(this.components)))
|
|
145
|
+
this.contentRouting = this.components.contentRouting = this.configureComponent('contentRouting', new CompoundContentRouting(this.components, {
|
|
218
146
|
routers: contentRouters
|
|
219
147
|
}))
|
|
220
148
|
|
|
221
|
-
this.fetchService = this.configureComponent(new FetchService(this.components, {
|
|
222
|
-
...init.fetch
|
|
223
|
-
}))
|
|
224
|
-
|
|
225
|
-
this.pingService = this.configureComponent(new PingService(this.components, {
|
|
226
|
-
...init.ping
|
|
227
|
-
}))
|
|
228
|
-
|
|
229
|
-
this.autonatService = this.configureComponent(new AutonatService(this.components, {
|
|
230
|
-
...init.autonat
|
|
231
|
-
}))
|
|
232
|
-
|
|
233
|
-
this.configureComponent(new AutoDialler(this.components, {
|
|
234
|
-
minConnections: init.connectionManager.minConnections,
|
|
235
|
-
autoDialInterval: init.connectionManager.autoDialInterval
|
|
236
|
-
}))
|
|
237
|
-
|
|
238
|
-
if (init.relay != null) {
|
|
239
|
-
this.circuitService = this.configureComponent(init.relay(this.components))
|
|
240
|
-
}
|
|
241
|
-
|
|
242
149
|
// Discovery modules
|
|
243
|
-
|
|
244
|
-
const service = this.configureComponent(fn(this.components))
|
|
150
|
+
;(init.peerDiscovery ?? []).forEach((fn, index) => {
|
|
151
|
+
const service = this.configureComponent(`peer-discovery-${index}`, fn(this.components))
|
|
245
152
|
|
|
246
153
|
service.addEventListener('peer', (evt) => {
|
|
247
|
-
this
|
|
154
|
+
this.#onDiscoveryPeer(evt)
|
|
248
155
|
})
|
|
249
|
-
}
|
|
156
|
+
})
|
|
250
157
|
|
|
251
158
|
// Transport modules
|
|
252
|
-
init.transports.forEach((fn) => {
|
|
253
|
-
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
159
|
+
init.transports.forEach((fn, index) => {
|
|
160
|
+
this.components.transportManager.add(this.configureComponent(`transport-${index}`, fn(this.components)))
|
|
254
161
|
})
|
|
162
|
+
|
|
163
|
+
// User defined modules
|
|
164
|
+
if (init.services != null) {
|
|
165
|
+
for (const name of Object.keys(init.services)) {
|
|
166
|
+
const createService = init.services[name]
|
|
167
|
+
const service: any = createService(this.components)
|
|
168
|
+
|
|
169
|
+
if (service == null) {
|
|
170
|
+
log.error('service factory %s returned null or undefined instance', name)
|
|
171
|
+
continue
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this.services[name as keyof T] = service
|
|
175
|
+
this.configureComponent(name, service)
|
|
176
|
+
|
|
177
|
+
if (service[contentRouting] != null) {
|
|
178
|
+
log('registering service %s for content routing', name)
|
|
179
|
+
contentRouters.push(service[contentRouting])
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (service[peerRouting] != null) {
|
|
183
|
+
log('registering service %s for peer routing', name)
|
|
184
|
+
peerRouters.push(service[peerRouting])
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (service[peerDiscovery] != null) {
|
|
188
|
+
log('registering service %s for peer discovery', name)
|
|
189
|
+
service[peerDiscovery].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
|
|
190
|
+
this.#onDiscoveryPeer(evt)
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
255
195
|
}
|
|
256
196
|
|
|
257
|
-
private configureComponent <T> (component: T): T {
|
|
258
|
-
if (
|
|
259
|
-
|
|
197
|
+
private configureComponent <T> (name: string, component: T): T {
|
|
198
|
+
if (component == null) {
|
|
199
|
+
log.error('component %s was null or undefined', name)
|
|
260
200
|
}
|
|
261
201
|
|
|
202
|
+
this.components[name] = component
|
|
203
|
+
|
|
262
204
|
return component
|
|
263
205
|
}
|
|
264
206
|
|
|
@@ -266,11 +208,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
266
208
|
* Starts the libp2p node and all its subsystems
|
|
267
209
|
*/
|
|
268
210
|
async start (): Promise<void> {
|
|
269
|
-
if (this
|
|
211
|
+
if (this.#started) {
|
|
270
212
|
return
|
|
271
213
|
}
|
|
272
214
|
|
|
273
|
-
this
|
|
215
|
+
this.#started = true
|
|
274
216
|
|
|
275
217
|
log('libp2p is starting')
|
|
276
218
|
|
|
@@ -282,27 +224,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
282
224
|
}
|
|
283
225
|
|
|
284
226
|
try {
|
|
285
|
-
await
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
await service.beforeStart()
|
|
289
|
-
}
|
|
290
|
-
})
|
|
291
|
-
)
|
|
292
|
-
|
|
293
|
-
// start any startables
|
|
294
|
-
await Promise.all(
|
|
295
|
-
this.services.map(async service => { await service.start() })
|
|
296
|
-
)
|
|
297
|
-
|
|
298
|
-
await Promise.all(
|
|
299
|
-
this.services.map(async service => {
|
|
300
|
-
if (service.afterStart != null) {
|
|
301
|
-
await service.afterStart()
|
|
302
|
-
}
|
|
303
|
-
})
|
|
304
|
-
)
|
|
305
|
-
|
|
227
|
+
await this.components.beforeStart?.()
|
|
228
|
+
await this.components.start()
|
|
229
|
+
await this.components.afterStart?.()
|
|
306
230
|
log('libp2p has started')
|
|
307
231
|
} catch (err: any) {
|
|
308
232
|
log.error('An error occurred starting libp2p', err)
|
|
@@ -315,45 +239,33 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
315
239
|
* Stop the libp2p node by closing its listeners and open connections
|
|
316
240
|
*/
|
|
317
241
|
async stop (): Promise<void> {
|
|
318
|
-
if (!this
|
|
242
|
+
if (!this.#started) {
|
|
319
243
|
return
|
|
320
244
|
}
|
|
321
245
|
|
|
322
246
|
log('libp2p is stopping')
|
|
323
247
|
|
|
324
|
-
this
|
|
325
|
-
|
|
326
|
-
await Promise.all(
|
|
327
|
-
this.services.map(async service => {
|
|
328
|
-
if (service.beforeStop != null) {
|
|
329
|
-
await service.beforeStop()
|
|
330
|
-
}
|
|
331
|
-
})
|
|
332
|
-
)
|
|
333
|
-
|
|
334
|
-
await Promise.all(
|
|
335
|
-
this.services.map(async service => { await service.stop() })
|
|
336
|
-
)
|
|
248
|
+
this.#started = false
|
|
337
249
|
|
|
338
|
-
await
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
await service.afterStop()
|
|
342
|
-
}
|
|
343
|
-
})
|
|
344
|
-
)
|
|
250
|
+
await this.components.beforeStop?.()
|
|
251
|
+
await this.components.stop()
|
|
252
|
+
await this.components.afterStop?.()
|
|
345
253
|
|
|
346
254
|
log('libp2p has stopped')
|
|
347
255
|
}
|
|
348
256
|
|
|
349
257
|
isStarted (): boolean {
|
|
350
|
-
return this
|
|
258
|
+
return this.#started
|
|
351
259
|
}
|
|
352
260
|
|
|
353
261
|
getConnections (peerId?: PeerId): Connection[] {
|
|
354
262
|
return this.components.connectionManager.getConnections(peerId)
|
|
355
263
|
}
|
|
356
264
|
|
|
265
|
+
getDialQueue (): PendingDial[] {
|
|
266
|
+
return this.components.connectionManager.getDialQueue()
|
|
267
|
+
}
|
|
268
|
+
|
|
357
269
|
getPeers (): PeerId[] {
|
|
358
270
|
const peerSet = new PeerSet()
|
|
359
271
|
|
|
@@ -365,7 +277,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
365
277
|
}
|
|
366
278
|
|
|
367
279
|
async dial (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<Connection> {
|
|
368
|
-
// @ts-expect-error
|
|
369
280
|
return await this.components.connectionManager.openConnection(peer, options)
|
|
370
281
|
}
|
|
371
282
|
|
|
@@ -413,12 +324,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
413
324
|
|
|
414
325
|
const peerInfo = await this.peerStore.get(peer)
|
|
415
326
|
|
|
416
|
-
if (peerInfo.
|
|
417
|
-
return peerInfo.
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (this.dht == null) {
|
|
421
|
-
throw new CodeError('Public key was not in the peer store and the DHT is not enabled', codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
327
|
+
if (peerInfo.id.publicKey != null) {
|
|
328
|
+
return peerInfo.id.publicKey
|
|
422
329
|
}
|
|
423
330
|
|
|
424
331
|
const peerKey = uint8ArrayConcat([
|
|
@@ -426,38 +333,16 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
426
333
|
peer.multihash.digest
|
|
427
334
|
])
|
|
428
335
|
|
|
429
|
-
// search
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
await this.peerStore.keyBook.set(peer, event.value)
|
|
435
|
-
|
|
436
|
-
return key.bytes
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
throw new CodeError(`Node not responding with its public key: ${peer.toString()}`, codes.ERR_INVALID_RECORD)
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
async fetch (peer: PeerId | Multiaddr, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
|
|
444
|
-
if (isMultiaddr(peer)) {
|
|
445
|
-
const peerId = peerIdFromString(peer.getPeerId() ?? '')
|
|
446
|
-
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
447
|
-
peer = peerId
|
|
448
|
-
}
|
|
336
|
+
// search any available content routing methods
|
|
337
|
+
const bytes = await this.contentRouting.get(peerKey, options)
|
|
338
|
+
// ensure the returned key is valid
|
|
339
|
+
unmarshalPublicKey(bytes)
|
|
449
340
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
async ping (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<number> {
|
|
454
|
-
if (isMultiaddr(peer)) {
|
|
455
|
-
const peerId = peerIdFromString(peer.getPeerId() ?? '')
|
|
456
|
-
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
457
|
-
peer = peerId
|
|
458
|
-
}
|
|
341
|
+
await this.peerStore.patch(peer, {
|
|
342
|
+
publicKey: bytes
|
|
343
|
+
})
|
|
459
344
|
|
|
460
|
-
return
|
|
345
|
+
return bytes
|
|
461
346
|
}
|
|
462
347
|
|
|
463
348
|
async handle (protocols: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void> {
|
|
@@ -485,18 +370,18 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
485
370
|
}
|
|
486
371
|
|
|
487
372
|
async register (protocol: string, topology: Topology): Promise<string> {
|
|
488
|
-
return await this.registrar.register(protocol, topology)
|
|
373
|
+
return await this.components.registrar.register(protocol, topology)
|
|
489
374
|
}
|
|
490
375
|
|
|
491
376
|
unregister (id: string): void {
|
|
492
|
-
this.registrar.unregister(id)
|
|
377
|
+
this.components.registrar.unregister(id)
|
|
493
378
|
}
|
|
494
379
|
|
|
495
380
|
/**
|
|
496
|
-
* Called whenever peer discovery services emit `peer` events
|
|
497
|
-
*
|
|
381
|
+
* Called whenever peer discovery services emit `peer` events and adds peers
|
|
382
|
+
* to the peer store.
|
|
498
383
|
*/
|
|
499
|
-
onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
|
|
384
|
+
#onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
|
|
500
385
|
const { detail: peer } = evt
|
|
501
386
|
|
|
502
387
|
if (peer.id.toString() === this.peerId.toString()) {
|
|
@@ -504,15 +389,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
504
389
|
return
|
|
505
390
|
}
|
|
506
391
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => { log.error(err) })
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
|
|
392
|
+
void this.components.peerStore.merge(peer.id, {
|
|
393
|
+
multiaddrs: peer.multiaddrs,
|
|
394
|
+
protocols: peer.protocols
|
|
395
|
+
})
|
|
396
|
+
.catch(err => { log.error(err) })
|
|
516
397
|
}
|
|
517
398
|
}
|
|
518
399
|
|
|
@@ -520,7 +401,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
520
401
|
* Returns a new Libp2pNode instance - this exposes more of the internals than the
|
|
521
402
|
* libp2p interface and is useful for testing and debugging.
|
|
522
403
|
*/
|
|
523
|
-
export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pNode
|
|
404
|
+
export async function createLibp2pNode <T extends ServiceMap = {}> (options: Libp2pOptions<T>): Promise<Libp2pNode<T>> {
|
|
524
405
|
if (options.peerId == null) {
|
|
525
406
|
const datastore = options.datastore as Datastore | undefined
|
|
526
407
|
|