libp2p 0.44.0-97b473ad → 0.44.0-e3deaa40
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 +27 -27
- 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.map +1 -1
- package/dist/src/autonat/index.js +15 -13
- 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 +3 -0
- 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/components.d.ts +6 -63
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +60 -164
- 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 +16 -23
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +18 -4
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +23 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +2 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +18 -12
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +8 -7
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +23 -34
- package/dist/src/connection-manager/index.js.map +1 -1
- 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 +7 -9
- 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 +200 -198
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -62
- 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 +0 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +11 -42
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +137 -195
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +7 -6
- 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/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +0 -1
- package/dist/src/pnet/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 +11 -4
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +27 -11
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +15 -10
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +36 -55
- 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 +59 -43
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +14 -13
- 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 +14 -6
- package/src/components.ts +71 -234
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +8 -14
- package/src/connection-manager/auto-dial.ts +24 -5
- package/src/connection-manager/connection-pruner.ts +28 -8
- package/src/connection-manager/dial-queue.ts +21 -19
- package/src/connection-manager/index.ts +31 -43
- package/src/content-routing/index.ts +7 -55
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +51 -14
- package/src/identify/index.ts +235 -225
- package/src/index.ts +12 -70
- package/src/insecure/index.ts +4 -5
- package/src/libp2p.ts +139 -259
- package/src/peer-routing.ts +10 -8
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +32 -28
- package/src/pnet/index.ts +0 -1
- package/src/registrar.ts +31 -27
- package/src/transport-manager.ts +36 -14
- package/src/upgrader.ts +59 -64
- package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
- package/src/version.ts +1 -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/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'
|
|
@@ -13,251 +12,196 @@ import { DefaultKeyChain } from '@libp2p/keychain'
|
|
|
13
12
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
14
13
|
import { DefaultUpgrader } from './upgrader.js'
|
|
15
14
|
import { DefaultRegistrar } from './registrar.js'
|
|
16
|
-
import { IdentifyService } from './identify/index.js'
|
|
17
|
-
import { FetchService } from './fetch/index.js'
|
|
18
|
-
import { PingService } from './ping/index.js'
|
|
19
|
-
import { NatManager } from './nat-manager.js'
|
|
20
|
-
import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
21
|
-
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
22
15
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
23
|
-
import {
|
|
24
|
-
import { AutonatService } from './autonat/index.js'
|
|
25
|
-
import { DefaultComponents } from './components.js'
|
|
16
|
+
import { defaultComponents } from './components.js'
|
|
26
17
|
import type { Components } from './components.js'
|
|
27
18
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
28
19
|
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
29
|
-
import type {
|
|
30
|
-
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
31
|
-
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
32
|
-
import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
|
|
33
|
-
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
20
|
+
import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
|
|
34
21
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
35
|
-
import type { Libp2p,
|
|
36
|
-
import { validateConfig } from './config.js'
|
|
37
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
22
|
+
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
|
|
38
23
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
39
|
-
import type { DualDHT } from '@libp2p/interface-dht'
|
|
40
24
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
41
25
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
42
26
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
43
|
-
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
44
27
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
45
|
-
import { DummyDHT } from './dht/dummy-dht.js'
|
|
46
|
-
import { DummyPubSub } from './pubsub/dummy-pubsub.js'
|
|
47
28
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
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'
|
|
48
33
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
49
34
|
import type { Datastore } from 'interface-datastore'
|
|
50
|
-
import type { KeyChain } from '@libp2p/interface-keychain'
|
|
51
35
|
import mergeOptions from 'merge-options'
|
|
52
|
-
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'
|
|
53
42
|
|
|
54
43
|
const log = logger('libp2p')
|
|
55
44
|
|
|
56
|
-
export
|
|
57
|
-
|
|
58
|
-
export interface PendingDial {
|
|
59
|
-
id: string
|
|
60
|
-
status: PendingDialStatus
|
|
61
|
-
peerId?: PeerId
|
|
62
|
-
multiaddrs: Multiaddr[]
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
45
|
+
export class Libp2pNode<T extends ServiceMap = {}> extends EventEmitter<Libp2pEvents> implements Libp2p<T> {
|
|
66
46
|
public peerId: PeerId
|
|
67
|
-
public dht: DualDHT
|
|
68
|
-
public pubsub: PubSub
|
|
69
|
-
public identifyService: IdentifyService
|
|
70
|
-
public circuitService?: CircuitRelayService
|
|
71
|
-
public fetchService: FetchService
|
|
72
|
-
public pingService: PingService
|
|
73
|
-
public autonatService: AutonatService
|
|
74
|
-
public components: Components
|
|
75
47
|
public peerStore: PeerStore
|
|
76
48
|
public contentRouting: ContentRouting
|
|
77
49
|
public peerRouting: PeerRouting
|
|
78
50
|
public keychain: KeyChain
|
|
79
|
-
public connectionManager: ConnectionManager
|
|
80
|
-
public registrar: Registrar
|
|
81
51
|
public metrics?: Metrics
|
|
52
|
+
public services: T
|
|
82
53
|
|
|
83
|
-
|
|
84
|
-
|
|
54
|
+
public components: Components
|
|
55
|
+
#started: boolean
|
|
85
56
|
|
|
86
|
-
constructor (init: Libp2pInit) {
|
|
57
|
+
constructor (init: Libp2pInit<T>) {
|
|
87
58
|
super()
|
|
88
59
|
|
|
89
|
-
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
|
|
90
79
|
this.peerId = init.peerId
|
|
91
|
-
|
|
80
|
+
// @ts-expect-error {} may not be of type T
|
|
81
|
+
this.services = {}
|
|
82
|
+
const components = this.components = defaultComponents({
|
|
92
83
|
peerId: init.peerId,
|
|
84
|
+
events,
|
|
93
85
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
94
|
-
connectionGater:
|
|
95
|
-
denyDialPeer: async () => await Promise.resolve(false),
|
|
96
|
-
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
97
|
-
denyInboundConnection: async () => await Promise.resolve(false),
|
|
98
|
-
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
99
|
-
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
100
|
-
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
101
|
-
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
102
|
-
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
103
|
-
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
104
|
-
...init.connectionGater
|
|
105
|
-
}
|
|
86
|
+
connectionGater: connectionGater(init.connectionGater)
|
|
106
87
|
})
|
|
107
|
-
|
|
88
|
+
|
|
89
|
+
this.peerStore = this.configureComponent('peerStore', new PersistentPeerStore(components, {
|
|
108
90
|
addressFilter: this.components.connectionGater.filterMultiaddrForPeer,
|
|
109
91
|
...init.peerStore
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
this.services = [
|
|
113
|
-
components
|
|
114
|
-
]
|
|
92
|
+
}))
|
|
115
93
|
|
|
116
94
|
// Create Metrics
|
|
117
95
|
if (init.metrics != null) {
|
|
118
|
-
this.metrics = this.
|
|
96
|
+
this.metrics = this.configureComponent('metrics', init.metrics(this.components))
|
|
119
97
|
}
|
|
120
98
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
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
|
+
}
|
|
127
104
|
})
|
|
128
105
|
|
|
129
106
|
// Set up connection protector if configured
|
|
130
107
|
if (init.connectionProtector != null) {
|
|
131
|
-
this.
|
|
108
|
+
this.configureComponent('connectionProtector', init.connectionProtector(components))
|
|
132
109
|
}
|
|
133
110
|
|
|
134
111
|
// Set up the Upgrader
|
|
135
112
|
this.components.upgrader = new DefaultUpgrader(this.components, {
|
|
136
|
-
connectionEncryption: (init.connectionEncryption ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
137
|
-
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))),
|
|
138
115
|
inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
|
|
139
116
|
})
|
|
140
117
|
|
|
141
118
|
// Setup the transport manager
|
|
142
|
-
this.
|
|
119
|
+
this.configureComponent('transportManager', new DefaultTransportManager(this.components, init.transportManager))
|
|
143
120
|
|
|
144
121
|
// Create the Connection Manager
|
|
145
|
-
this.connectionManager
|
|
146
|
-
|
|
147
|
-
// forward connection manager events
|
|
148
|
-
this.components.connectionManager.addEventListener('peer:disconnect', (event) => {
|
|
149
|
-
this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: event.detail }))
|
|
150
|
-
})
|
|
151
|
-
this.components.connectionManager.addEventListener('peer:connect', (event) => {
|
|
152
|
-
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: event.detail }))
|
|
153
|
-
})
|
|
122
|
+
this.configureComponent('connectionManager', new DefaultConnectionManager(this.components, init.connectionManager))
|
|
154
123
|
|
|
155
124
|
// Create the Registrar
|
|
156
|
-
this.registrar
|
|
125
|
+
this.configureComponent('registrar', new DefaultRegistrar(this.components))
|
|
157
126
|
|
|
158
127
|
// Addresses {listen, announce, noAnnounce}
|
|
159
|
-
this.
|
|
160
|
-
|
|
161
|
-
// update our peer record when addresses change
|
|
162
|
-
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
128
|
+
this.configureComponent('addressManager', new DefaultAddressManager(this.components, init.addresses))
|
|
163
129
|
|
|
164
130
|
// Create keychain
|
|
165
131
|
const keychainOpts = DefaultKeyChain.generateOptions()
|
|
166
|
-
this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
|
|
132
|
+
this.keychain = this.configureComponent('keyChain', new DefaultKeyChain(this.components, {
|
|
167
133
|
...keychainOpts,
|
|
168
134
|
...init.keychain
|
|
169
135
|
}))
|
|
170
136
|
|
|
171
|
-
//
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
// Add the identify service
|
|
175
|
-
this.identifyService = new IdentifyService(this.components, {
|
|
176
|
-
...init.identify
|
|
177
|
-
})
|
|
178
|
-
this.configureComponent(this.identifyService)
|
|
179
|
-
|
|
180
|
-
// dht provided components (peerRouting, contentRouting, dht)
|
|
181
|
-
if (init.dht != null) {
|
|
182
|
-
this.dht = this.components.dht = init.dht(this.components)
|
|
183
|
-
} else {
|
|
184
|
-
this.dht = new DummyDHT()
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Create pubsub if provided
|
|
188
|
-
if (init.pubsub != null) {
|
|
189
|
-
this.pubsub = this.components.pubsub = init.pubsub(this.components)
|
|
190
|
-
} else {
|
|
191
|
-
this.pubsub = new DummyPubSub()
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Attach remaining APIs
|
|
195
|
-
// peer and content routing will automatically get modules from _modules and _dht
|
|
196
|
-
|
|
197
|
-
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
198
|
-
|
|
199
|
-
if (init.dht != null) {
|
|
200
|
-
// add dht to routers
|
|
201
|
-
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))
|
|
202
|
-
|
|
203
|
-
// use dht for peer discovery
|
|
204
|
-
this.dht.addEventListener('peer', (evt) => {
|
|
205
|
-
this.onDiscoveryPeer(evt)
|
|
206
|
-
})
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
this.peerRouting = this.components.peerRouting = this.configureComponent(new DefaultPeerRouting(this.components, {
|
|
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, {
|
|
210
140
|
...init.peerRouting,
|
|
211
141
|
routers: peerRouters
|
|
212
142
|
}))
|
|
213
143
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
// add dht to routers
|
|
218
|
-
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
this.contentRouting = this.components.contentRouting = this.configureComponent(new CompoundContentRouting(this.components, {
|
|
144
|
+
// Content routers
|
|
145
|
+
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map((fn, index) => this.configureComponent(`content-router-${index}`, fn(this.components)))
|
|
146
|
+
this.contentRouting = this.components.contentRouting = this.configureComponent('contentRouting', new CompoundContentRouting(this.components, {
|
|
222
147
|
routers: contentRouters
|
|
223
148
|
}))
|
|
224
149
|
|
|
225
|
-
this.fetchService = this.configureComponent(new FetchService(this.components, {
|
|
226
|
-
...init.fetch
|
|
227
|
-
}))
|
|
228
|
-
|
|
229
|
-
this.pingService = this.configureComponent(new PingService(this.components, {
|
|
230
|
-
...init.ping
|
|
231
|
-
}))
|
|
232
|
-
|
|
233
|
-
this.autonatService = this.configureComponent(new AutonatService(this.components, {
|
|
234
|
-
...init.autonat
|
|
235
|
-
}))
|
|
236
|
-
|
|
237
|
-
if (init.relay != null) {
|
|
238
|
-
this.circuitService = this.configureComponent(init.relay(this.components))
|
|
239
|
-
}
|
|
240
|
-
|
|
241
150
|
// Discovery modules
|
|
242
|
-
|
|
243
|
-
const service = this.configureComponent(fn(this.components))
|
|
151
|
+
;(init.peerDiscovery ?? []).forEach((fn, index) => {
|
|
152
|
+
const service = this.configureComponent(`peer-discovery-${index}`, fn(this.components))
|
|
244
153
|
|
|
245
154
|
service.addEventListener('peer', (evt) => {
|
|
246
|
-
this
|
|
155
|
+
this.#onDiscoveryPeer(evt)
|
|
247
156
|
})
|
|
248
|
-
}
|
|
157
|
+
})
|
|
249
158
|
|
|
250
159
|
// Transport modules
|
|
251
|
-
init.transports.forEach((fn) => {
|
|
252
|
-
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
160
|
+
init.transports.forEach((fn, index) => {
|
|
161
|
+
this.components.transportManager.add(this.configureComponent(`transport-${index}`, fn(this.components)))
|
|
253
162
|
})
|
|
163
|
+
|
|
164
|
+
// User defined modules
|
|
165
|
+
if (init.services != null) {
|
|
166
|
+
for (const name of Object.keys(init.services)) {
|
|
167
|
+
const createService = init.services[name]
|
|
168
|
+
const service: any = createService(this.components)
|
|
169
|
+
|
|
170
|
+
if (service == null) {
|
|
171
|
+
log.error('service factory %s returned null or undefined instance', name)
|
|
172
|
+
continue
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
this.services[name as keyof T] = service
|
|
176
|
+
this.configureComponent(name, service)
|
|
177
|
+
|
|
178
|
+
if (service[contentRouting] != null) {
|
|
179
|
+
log('registering service %s for content routing', name)
|
|
180
|
+
contentRouters.push(service[contentRouting])
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (service[peerRouting] != null) {
|
|
184
|
+
log('registering service %s for peer routing', name)
|
|
185
|
+
peerRouters.push(service[peerRouting])
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (service[peerDiscovery] != null) {
|
|
189
|
+
log('registering service %s for peer discovery', name)
|
|
190
|
+
service[peerDiscovery].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
|
|
191
|
+
this.#onDiscoveryPeer(evt)
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
254
196
|
}
|
|
255
197
|
|
|
256
|
-
private configureComponent <T> (component: T): T {
|
|
257
|
-
if (
|
|
258
|
-
|
|
198
|
+
private configureComponent <T> (name: string, component: T): T {
|
|
199
|
+
if (component == null) {
|
|
200
|
+
log.error('component %s was null or undefined', name)
|
|
259
201
|
}
|
|
260
202
|
|
|
203
|
+
this.components[name] = component
|
|
204
|
+
|
|
261
205
|
return component
|
|
262
206
|
}
|
|
263
207
|
|
|
@@ -265,11 +209,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
265
209
|
* Starts the libp2p node and all its subsystems
|
|
266
210
|
*/
|
|
267
211
|
async start (): Promise<void> {
|
|
268
|
-
if (this
|
|
212
|
+
if (this.#started) {
|
|
269
213
|
return
|
|
270
214
|
}
|
|
271
215
|
|
|
272
|
-
this
|
|
216
|
+
this.#started = true
|
|
273
217
|
|
|
274
218
|
log('libp2p is starting')
|
|
275
219
|
|
|
@@ -281,27 +225,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
281
225
|
}
|
|
282
226
|
|
|
283
227
|
try {
|
|
284
|
-
await
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
await service.beforeStart()
|
|
288
|
-
}
|
|
289
|
-
})
|
|
290
|
-
)
|
|
291
|
-
|
|
292
|
-
// start any startables
|
|
293
|
-
await Promise.all(
|
|
294
|
-
this.services.map(async service => { await service.start() })
|
|
295
|
-
)
|
|
296
|
-
|
|
297
|
-
await Promise.all(
|
|
298
|
-
this.services.map(async service => {
|
|
299
|
-
if (service.afterStart != null) {
|
|
300
|
-
await service.afterStart()
|
|
301
|
-
}
|
|
302
|
-
})
|
|
303
|
-
)
|
|
304
|
-
|
|
228
|
+
await this.components.beforeStart?.()
|
|
229
|
+
await this.components.start()
|
|
230
|
+
await this.components.afterStart?.()
|
|
305
231
|
log('libp2p has started')
|
|
306
232
|
} catch (err: any) {
|
|
307
233
|
log.error('An error occurred starting libp2p', err)
|
|
@@ -314,39 +240,23 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
314
240
|
* Stop the libp2p node by closing its listeners and open connections
|
|
315
241
|
*/
|
|
316
242
|
async stop (): Promise<void> {
|
|
317
|
-
if (!this
|
|
243
|
+
if (!this.#started) {
|
|
318
244
|
return
|
|
319
245
|
}
|
|
320
246
|
|
|
321
247
|
log('libp2p is stopping')
|
|
322
248
|
|
|
323
|
-
this
|
|
324
|
-
|
|
325
|
-
await Promise.all(
|
|
326
|
-
this.services.map(async service => {
|
|
327
|
-
if (service.beforeStop != null) {
|
|
328
|
-
await service.beforeStop()
|
|
329
|
-
}
|
|
330
|
-
})
|
|
331
|
-
)
|
|
332
|
-
|
|
333
|
-
await Promise.all(
|
|
334
|
-
this.services.map(async service => { await service.stop() })
|
|
335
|
-
)
|
|
249
|
+
this.#started = false
|
|
336
250
|
|
|
337
|
-
await
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
await service.afterStop()
|
|
341
|
-
}
|
|
342
|
-
})
|
|
343
|
-
)
|
|
251
|
+
await this.components.beforeStop?.()
|
|
252
|
+
await this.components.stop()
|
|
253
|
+
await this.components.afterStop?.()
|
|
344
254
|
|
|
345
255
|
log('libp2p has stopped')
|
|
346
256
|
}
|
|
347
257
|
|
|
348
258
|
isStarted (): boolean {
|
|
349
|
-
return this
|
|
259
|
+
return this.#started
|
|
350
260
|
}
|
|
351
261
|
|
|
352
262
|
getConnections (peerId?: PeerId): Connection[] {
|
|
@@ -415,12 +325,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
415
325
|
|
|
416
326
|
const peerInfo = await this.peerStore.get(peer)
|
|
417
327
|
|
|
418
|
-
if (peerInfo.
|
|
419
|
-
return peerInfo.
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (this.dht == null) {
|
|
423
|
-
throw new CodeError('Public key was not in the peer store and the DHT is not enabled', codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
328
|
+
if (peerInfo.id.publicKey != null) {
|
|
329
|
+
return peerInfo.id.publicKey
|
|
424
330
|
}
|
|
425
331
|
|
|
426
332
|
const peerKey = uint8ArrayConcat([
|
|
@@ -428,38 +334,16 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
428
334
|
peer.multihash.digest
|
|
429
335
|
])
|
|
430
336
|
|
|
431
|
-
// search
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
await this.peerStore.keyBook.set(peer, event.value)
|
|
437
|
-
|
|
438
|
-
return key.bytes
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
throw new CodeError(`Node not responding with its public key: ${peer.toString()}`, codes.ERR_INVALID_RECORD)
|
|
443
|
-
}
|
|
337
|
+
// search any available content routing methods
|
|
338
|
+
const bytes = await this.contentRouting.get(peerKey, options)
|
|
339
|
+
// ensure the returned key is valid
|
|
340
|
+
unmarshalPublicKey(bytes)
|
|
444
341
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
449
|
-
peer = peerId
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
return await this.fetchService.fetch(peer, key, options)
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
async ping (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<number> {
|
|
456
|
-
if (isMultiaddr(peer)) {
|
|
457
|
-
const peerId = peerIdFromString(peer.getPeerId() ?? '')
|
|
458
|
-
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
459
|
-
peer = peerId
|
|
460
|
-
}
|
|
342
|
+
await this.peerStore.patch(peer, {
|
|
343
|
+
publicKey: bytes
|
|
344
|
+
})
|
|
461
345
|
|
|
462
|
-
return
|
|
346
|
+
return bytes
|
|
463
347
|
}
|
|
464
348
|
|
|
465
349
|
async handle (protocols: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void> {
|
|
@@ -487,18 +371,18 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
487
371
|
}
|
|
488
372
|
|
|
489
373
|
async register (protocol: string, topology: Topology): Promise<string> {
|
|
490
|
-
return await this.registrar.register(protocol, topology)
|
|
374
|
+
return await this.components.registrar.register(protocol, topology)
|
|
491
375
|
}
|
|
492
376
|
|
|
493
377
|
unregister (id: string): void {
|
|
494
|
-
this.registrar.unregister(id)
|
|
378
|
+
this.components.registrar.unregister(id)
|
|
495
379
|
}
|
|
496
380
|
|
|
497
381
|
/**
|
|
498
382
|
* Called whenever peer discovery services emit `peer` events and adds peers
|
|
499
383
|
* to the peer store.
|
|
500
384
|
*/
|
|
501
|
-
onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
|
|
385
|
+
#onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
|
|
502
386
|
const { detail: peer } = evt
|
|
503
387
|
|
|
504
388
|
if (peer.id.toString() === this.peerId.toString()) {
|
|
@@ -506,15 +390,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
506
390
|
return
|
|
507
391
|
}
|
|
508
392
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => { log.error(err) })
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
|
|
393
|
+
void this.components.peerStore.merge(peer.id, {
|
|
394
|
+
multiaddrs: peer.multiaddrs,
|
|
395
|
+
protocols: peer.protocols
|
|
396
|
+
})
|
|
397
|
+
.catch(err => { log.error(err) })
|
|
518
398
|
}
|
|
519
399
|
}
|
|
520
400
|
|
|
@@ -522,7 +402,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
522
402
|
* Returns a new Libp2pNode instance - this exposes more of the internals than the
|
|
523
403
|
* libp2p interface and is useful for testing and debugging.
|
|
524
404
|
*/
|
|
525
|
-
export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pNode
|
|
405
|
+
export async function createLibp2pNode <T extends ServiceMap = {}> (options: Libp2pOptions<T>): Promise<Libp2pNode<T>> {
|
|
526
406
|
if (options.peerId == null) {
|
|
527
407
|
const datastore = options.datastore as Datastore | undefined
|
|
528
408
|
|
package/src/peer-routing.ts
CHANGED
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
uniquePeers,
|
|
7
7
|
requirePeers
|
|
8
8
|
} from './content-routing/utils.js'
|
|
9
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
10
9
|
import merge from 'it-merge'
|
|
11
10
|
import { pipe } from 'it-pipe'
|
|
12
11
|
import first from 'it-first'
|
|
@@ -24,6 +23,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
24
23
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
25
24
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
26
25
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
26
|
+
import { anySignal } from 'any-signal'
|
|
27
27
|
|
|
28
28
|
const log = logger('libp2p:peer-routing')
|
|
29
29
|
|
|
@@ -65,7 +65,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
65
65
|
private readonly refreshManagerInit: RefreshManagerInit
|
|
66
66
|
private timeoutId?: ReturnType<typeof setTimeout>
|
|
67
67
|
private started: boolean
|
|
68
|
-
private abortController?:
|
|
68
|
+
private abortController?: AbortController
|
|
69
69
|
|
|
70
70
|
constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
|
|
71
71
|
this.components = components
|
|
@@ -104,23 +104,25 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
104
104
|
return
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
this.abortController = new AbortController()
|
|
108
|
+
|
|
109
|
+
const signal = anySignal([this.abortController.signal, AbortSignal.timeout(this.refreshManagerInit.timeout ?? 10e3)])
|
|
109
110
|
|
|
111
|
+
try {
|
|
110
112
|
// this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
|
|
111
113
|
// appearing in the console
|
|
112
114
|
try {
|
|
113
|
-
|
|
114
|
-
setMaxListeners?.(Infinity, this.abortController.signal)
|
|
115
|
+
setMaxListeners?.(Infinity, signal)
|
|
115
116
|
} catch {}
|
|
116
117
|
|
|
117
118
|
// nb getClosestPeers adds the addresses to the address book
|
|
118
|
-
await drain(this.getClosestPeers(this.components.peerId.toBytes(), { signal
|
|
119
|
+
await drain(this.getClosestPeers(this.components.peerId.toBytes(), { signal }))
|
|
119
120
|
} catch (err: any) {
|
|
120
121
|
log.error(err)
|
|
121
122
|
} finally {
|
|
122
|
-
this.abortController?.
|
|
123
|
+
this.abortController?.abort()
|
|
123
124
|
this.abortController = undefined
|
|
125
|
+
signal.clear()
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
|
package/src/ping/constants.ts
CHANGED
|
@@ -3,3 +3,14 @@ export const PROTOCOL = '/ipfs/ping/1.0.0'
|
|
|
3
3
|
export const PING_LENGTH = 32
|
|
4
4
|
export const PROTOCOL_VERSION = '1.0.0'
|
|
5
5
|
export const PROTOCOL_NAME = 'ping'
|
|
6
|
+
export const PROTOCOL_PREFIX = 'ipfs'
|
|
7
|
+
export const TIMEOUT = 10000
|
|
8
|
+
|
|
9
|
+
// See https://github.com/libp2p/specs/blob/d4b5fb0152a6bb86cfd9ea/ping/ping.md?plain=1#L38-L43
|
|
10
|
+
// The dialing peer MUST NOT keep more than one outbound stream for the ping protocol per peer.
|
|
11
|
+
// The listening peer SHOULD accept at most two streams per peer since cross-stream behavior is
|
|
12
|
+
// non-linear and stream writes occur asynchronously. The listening peer may perceive the
|
|
13
|
+
// dialing peer closing and opening the wrong streams (for instance, closing stream B and
|
|
14
|
+
// opening stream A even though the dialing peer is opening stream B and closing stream A).
|
|
15
|
+
export const MAX_INBOUND_STREAMS = 2
|
|
16
|
+
export const MAX_OUTBOUND_STREAMS = 1
|