libp2p 0.44.0 → 0.45.0
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 +30 -30
- package/dist/src/address-manager/index.d.ts +8 -4
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +37 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/address-manager/utils.d.ts +2 -0
- package/dist/src/address-manager/utils.d.ts.map +1 -0
- package/dist/src/address-manager/utils.js +12 -0
- package/dist/src/address-manager/utils.js.map +1 -0
- package/dist/src/autonat/constants.d.ts +16 -2
- package/dist/src/autonat/constants.d.ts.map +1 -1
- package/dist/src/autonat/constants.js +16 -2
- package/dist/src/autonat/constants.js.map +1 -1
- package/dist/src/autonat/index.d.ts +12 -33
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +36 -30
- 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 +10 -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 +4 -2
- 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 -4
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +30 -26
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -4
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +37 -30
- 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 +0 -62
- 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 +36 -13
- 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 +36 -14
- 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/identify.d.ts +53 -0
- package/dist/src/identify/identify.d.ts.map +1 -0
- package/dist/src/identify/identify.js +405 -0
- package/dist/src/identify/identify.js.map +1 -0
- package/dist/src/identify/index.d.ts +12 -58
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +5 -388
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +13 -68
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -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 -196
- 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 +11 -4
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +19 -10
- 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 +39 -48
- 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/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +25 -10
- package/package.json +61 -45
- package/src/address-manager/index.ts +50 -8
- package/src/address-manager/utils.ts +14 -0
- package/src/autonat/constants.ts +17 -2
- package/src/autonat/index.ts +53 -45
- package/src/circuit-relay/server/advert-service.ts +2 -2
- package/src/circuit-relay/server/index.ts +11 -3
- package/src/circuit-relay/transport/discovery.ts +5 -3
- package/src/circuit-relay/transport/index.ts +9 -4
- package/src/circuit-relay/transport/listener.ts +38 -28
- package/src/circuit-relay/transport/reservation-store.ts +34 -20
- 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 +3 -64
- package/src/connection-manager/auto-dial.ts +44 -15
- package/src/connection-manager/connection-pruner.ts +28 -8
- package/src/connection-manager/dial-queue.ts +41 -21
- 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/identify.ts +518 -0
- package/src/identify/index.ts +17 -507
- package/src/index.ts +14 -77
- package/src/insecure/index.ts +4 -4
- package/src/libp2p.ts +139 -260
- 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 +27 -13
- package/src/upgrader.ts +62 -58
- package/src/{nat-manager.ts → upnp-nat/index.ts} +16 -17
- 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/components.ts
CHANGED
|
@@ -10,13 +10,14 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
|
10
10
|
import type { Registrar } from '@libp2p/interface-registrar'
|
|
11
11
|
import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
|
|
12
12
|
import type { Datastore } from 'interface-datastore'
|
|
13
|
-
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
14
|
-
import type { DualDHT } from '@libp2p/interface-dht'
|
|
15
13
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
16
14
|
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
15
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
16
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
17
17
|
|
|
18
|
-
export interface Components {
|
|
18
|
+
export interface Components extends Record<string, any>, Startable {
|
|
19
19
|
peerId: PeerId
|
|
20
|
+
events: EventEmitter<Libp2pEvents>
|
|
20
21
|
addressManager: AddressManager
|
|
21
22
|
peerStore: PeerStore
|
|
22
23
|
upgrader: Upgrader
|
|
@@ -29,12 +30,11 @@ export interface Components {
|
|
|
29
30
|
datastore: Datastore
|
|
30
31
|
connectionProtector?: ConnectionProtector
|
|
31
32
|
metrics?: Metrics
|
|
32
|
-
dht?: DualDHT
|
|
33
|
-
pubsub?: PubSub
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
export interface ComponentsInit {
|
|
37
36
|
peerId?: PeerId
|
|
37
|
+
events?: EventEmitter<Libp2pEvents>
|
|
38
38
|
addressManager?: AddressManager
|
|
39
39
|
peerStore?: PeerStore
|
|
40
40
|
upgrader?: Upgrader
|
|
@@ -47,271 +47,108 @@ export interface ComponentsInit {
|
|
|
47
47
|
peerRouting?: PeerRouting
|
|
48
48
|
datastore?: Datastore
|
|
49
49
|
connectionProtector?: ConnectionProtector
|
|
50
|
-
dht?: DualDHT
|
|
51
|
-
pubsub?: PubSub
|
|
52
50
|
}
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
private _addressManager?: AddressManager
|
|
57
|
-
private _peerStore?: PeerStore
|
|
58
|
-
private _upgrader?: Upgrader
|
|
59
|
-
private _metrics?: Metrics
|
|
60
|
-
private _registrar?: Registrar
|
|
61
|
-
private _connectionManager?: ConnectionManager
|
|
62
|
-
private _transportManager?: TransportManager
|
|
63
|
-
private _connectionGater?: ConnectionGater
|
|
64
|
-
private _contentRouting?: ContentRouting
|
|
65
|
-
private _peerRouting?: PeerRouting
|
|
66
|
-
private _datastore?: Datastore
|
|
67
|
-
private _connectionProtector?: ConnectionProtector
|
|
68
|
-
private _dht?: DualDHT
|
|
69
|
-
private _pubsub?: PubSub
|
|
52
|
+
class DefaultComponents implements Startable {
|
|
53
|
+
public components: Record<string, any> = {}
|
|
70
54
|
private _started = false
|
|
71
55
|
|
|
72
56
|
constructor (init: ComponentsInit = {}) {
|
|
73
|
-
this.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
this._registrar = init.registrar
|
|
79
|
-
this._connectionManager = init.connectionManager
|
|
80
|
-
this._transportManager = init.transportManager
|
|
81
|
-
this._connectionGater = init.connectionGater
|
|
82
|
-
this._contentRouting = init.contentRouting
|
|
83
|
-
this._peerRouting = init.peerRouting
|
|
84
|
-
this._datastore = init.datastore
|
|
85
|
-
this._connectionProtector = init.connectionProtector
|
|
86
|
-
this._dht = init.dht
|
|
87
|
-
this._pubsub = init.pubsub
|
|
57
|
+
this.components = {}
|
|
58
|
+
|
|
59
|
+
for (const [key, value] of Object.entries(init)) {
|
|
60
|
+
this.components[key] = value
|
|
61
|
+
}
|
|
88
62
|
}
|
|
89
63
|
|
|
90
64
|
isStarted (): boolean {
|
|
91
65
|
return this._started
|
|
92
66
|
}
|
|
93
67
|
|
|
94
|
-
async beforeStart
|
|
68
|
+
private async _invokeStartableMethod (methodName: 'beforeStart' | 'start' | 'afterStart' | 'beforeStop' | 'stop' | 'afterStop'): Promise<void> {
|
|
95
69
|
await Promise.all(
|
|
96
|
-
Object.values(this
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
70
|
+
Object.values(this.components)
|
|
71
|
+
.filter(obj => isStartable(obj))
|
|
72
|
+
.map(async (startable: Startable) => {
|
|
73
|
+
await startable[methodName]?.()
|
|
74
|
+
})
|
|
101
75
|
)
|
|
102
76
|
}
|
|
103
77
|
|
|
104
|
-
async
|
|
105
|
-
await
|
|
106
|
-
|
|
107
|
-
await startable.start()
|
|
108
|
-
})
|
|
109
|
-
)
|
|
78
|
+
async beforeStart (): Promise<void> {
|
|
79
|
+
await this._invokeStartableMethod('beforeStart')
|
|
80
|
+
}
|
|
110
81
|
|
|
82
|
+
async start (): Promise<void> {
|
|
83
|
+
await this._invokeStartableMethod('start')
|
|
111
84
|
this._started = true
|
|
112
85
|
}
|
|
113
86
|
|
|
114
87
|
async afterStart (): Promise<void> {
|
|
115
|
-
await
|
|
116
|
-
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
117
|
-
if (startable.afterStart != null) {
|
|
118
|
-
await startable.afterStart()
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
)
|
|
88
|
+
await this._invokeStartableMethod('afterStart')
|
|
122
89
|
}
|
|
123
90
|
|
|
124
91
|
async beforeStop (): Promise<void> {
|
|
125
|
-
await
|
|
126
|
-
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
127
|
-
if (startable.beforeStop != null) {
|
|
128
|
-
await startable.beforeStop()
|
|
129
|
-
}
|
|
130
|
-
})
|
|
131
|
-
)
|
|
92
|
+
await this._invokeStartableMethod('beforeStop')
|
|
132
93
|
}
|
|
133
94
|
|
|
134
95
|
async stop (): Promise<void> {
|
|
135
|
-
await
|
|
136
|
-
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
137
|
-
await startable.stop()
|
|
138
|
-
})
|
|
139
|
-
)
|
|
140
|
-
|
|
96
|
+
await this._invokeStartableMethod('stop')
|
|
141
97
|
this._started = false
|
|
142
98
|
}
|
|
143
99
|
|
|
144
100
|
async afterStop (): Promise<void> {
|
|
145
|
-
await
|
|
146
|
-
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
147
|
-
if (startable.afterStop != null) {
|
|
148
|
-
await startable.afterStop()
|
|
149
|
-
}
|
|
150
|
-
})
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
get peerId (): PeerId {
|
|
155
|
-
if (this._peerId == null) {
|
|
156
|
-
throw new CodeError('peerId not set', 'ERR_SERVICE_MISSING')
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return this._peerId
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
set peerId (peerId: PeerId) {
|
|
163
|
-
this._peerId = peerId
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
get addressManager (): AddressManager {
|
|
167
|
-
if (this._addressManager == null) {
|
|
168
|
-
throw new CodeError('addressManager not set', 'ERR_SERVICE_MISSING')
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return this._addressManager
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
set addressManager (addressManager: AddressManager) {
|
|
175
|
-
this._addressManager = addressManager
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
get peerStore (): PeerStore {
|
|
179
|
-
if (this._peerStore == null) {
|
|
180
|
-
throw new CodeError('peerStore not set', 'ERR_SERVICE_MISSING')
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return this._peerStore
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
set peerStore (peerStore: PeerStore) {
|
|
187
|
-
this._peerStore = peerStore
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
get upgrader (): Upgrader {
|
|
191
|
-
if (this._upgrader == null) {
|
|
192
|
-
throw new CodeError('upgrader not set', 'ERR_SERVICE_MISSING')
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return this._upgrader
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
set upgrader (upgrader: Upgrader) {
|
|
199
|
-
this._upgrader = upgrader
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
get registrar (): Registrar {
|
|
203
|
-
if (this._registrar == null) {
|
|
204
|
-
throw new CodeError('registrar not set', 'ERR_SERVICE_MISSING')
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return this._registrar
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
set registrar (registrar: Registrar) {
|
|
211
|
-
this._registrar = registrar
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
get connectionManager (): ConnectionManager {
|
|
215
|
-
if (this._connectionManager == null) {
|
|
216
|
-
throw new CodeError('connectionManager not set', 'ERR_SERVICE_MISSING')
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return this._connectionManager
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
set connectionManager (connectionManager: ConnectionManager) {
|
|
223
|
-
this._connectionManager = connectionManager
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
get transportManager (): TransportManager {
|
|
227
|
-
if (this._transportManager == null) {
|
|
228
|
-
throw new CodeError('transportManager not set', 'ERR_SERVICE_MISSING')
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return this._transportManager
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
set transportManager (transportManager: TransportManager) {
|
|
235
|
-
this._transportManager = transportManager
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
get connectionGater (): ConnectionGater {
|
|
239
|
-
if (this._connectionGater == null) {
|
|
240
|
-
throw new CodeError('connectionGater not set', 'ERR_SERVICE_MISSING')
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return this._connectionGater
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
set connectionGater (connectionGater: ConnectionGater) {
|
|
247
|
-
this._connectionGater = connectionGater
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
get contentRouting (): ContentRouting {
|
|
251
|
-
if (this._contentRouting == null) {
|
|
252
|
-
throw new CodeError('contentRouting not set', 'ERR_SERVICE_MISSING')
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return this._contentRouting
|
|
101
|
+
await this._invokeStartableMethod('afterStop')
|
|
256
102
|
}
|
|
103
|
+
}
|
|
257
104
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
105
|
+
const OPTIONAL_SERVICES = [
|
|
106
|
+
'metrics',
|
|
107
|
+
'connectionProtector'
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
const NON_SERVICE_PROPERTIES = [
|
|
111
|
+
'components',
|
|
112
|
+
'isStarted',
|
|
113
|
+
'beforeStart',
|
|
114
|
+
'start',
|
|
115
|
+
'afterStart',
|
|
116
|
+
'beforeStop',
|
|
117
|
+
'stop',
|
|
118
|
+
'afterStop',
|
|
119
|
+
'then',
|
|
120
|
+
'_invokeStartableMethod'
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
export function defaultComponents (init: ComponentsInit = {}): Components {
|
|
124
|
+
const components = new DefaultComponents(init)
|
|
125
|
+
|
|
126
|
+
const proxy = new Proxy(components, {
|
|
127
|
+
get (target, prop, receiver) {
|
|
128
|
+
if (typeof prop === 'string' && !NON_SERVICE_PROPERTIES.includes(prop)) {
|
|
129
|
+
const service = components.components[prop]
|
|
130
|
+
|
|
131
|
+
if (service == null && !OPTIONAL_SERVICES.includes(prop)) {
|
|
132
|
+
throw new CodeError(`${prop} not set`, 'ERR_SERVICE_MISSING')
|
|
133
|
+
}
|
|
261
134
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
throw new CodeError('peerRouting not set', 'ERR_SERVICE_MISSING')
|
|
265
|
-
}
|
|
135
|
+
return service
|
|
136
|
+
}
|
|
266
137
|
|
|
267
|
-
|
|
268
|
-
|
|
138
|
+
return Reflect.get(target, prop, receiver)
|
|
139
|
+
},
|
|
269
140
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
141
|
+
set (target, prop, value) {
|
|
142
|
+
if (typeof prop === 'string') {
|
|
143
|
+
components.components[prop] = value
|
|
144
|
+
} else {
|
|
145
|
+
Reflect.set(target, prop, value)
|
|
146
|
+
}
|
|
273
147
|
|
|
274
|
-
|
|
275
|
-
if (this._datastore == null) {
|
|
276
|
-
throw new CodeError('datastore not set', 'ERR_SERVICE_MISSING')
|
|
148
|
+
return true
|
|
277
149
|
}
|
|
150
|
+
})
|
|
278
151
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
set datastore (datastore: Datastore) {
|
|
283
|
-
this._datastore = datastore
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
get connectionProtector (): ConnectionProtector | undefined {
|
|
287
|
-
return this._connectionProtector
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
set connectionProtector (connectionProtector: ConnectionProtector | undefined) {
|
|
291
|
-
this._connectionProtector = connectionProtector
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
get metrics (): Metrics | undefined {
|
|
295
|
-
return this._metrics
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
set metrics (metrics: Metrics | undefined) {
|
|
299
|
-
this._metrics = metrics
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
get dht (): DualDHT | undefined {
|
|
303
|
-
return this._dht
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
set dht (dht: DualDHT | undefined) {
|
|
307
|
-
this._dht = dht
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
get pubsub (): PubSub | undefined {
|
|
311
|
-
return this._pubsub
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
set pubsub (pubsub: PubSub | undefined) {
|
|
315
|
-
this._pubsub = pubsub
|
|
316
|
-
}
|
|
152
|
+
// @ts-expect-error component keys are proxied
|
|
153
|
+
return proxy
|
|
317
154
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
2
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
import isPrivate from 'private-ip'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a connection gater that disallows dialling private addresses by
|
|
7
|
+
* default. Browsers are severely limited in their resource usage so don't
|
|
8
|
+
* waste time trying to dial undiallable addresses.
|
|
9
|
+
*/
|
|
10
|
+
export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
|
|
11
|
+
return {
|
|
12
|
+
denyDialPeer: async () => false,
|
|
13
|
+
denyDialMultiaddr: async (multiaddr: Multiaddr) => {
|
|
14
|
+
const tuples = multiaddr.stringTuples()
|
|
15
|
+
|
|
16
|
+
if (tuples[0][0] === 4 || tuples[0][0] === 41) {
|
|
17
|
+
return Boolean(isPrivate(`${tuples[0][1]}`))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return false
|
|
21
|
+
},
|
|
22
|
+
denyInboundConnection: async () => false,
|
|
23
|
+
denyOutboundConnection: async () => false,
|
|
24
|
+
denyInboundEncryptedConnection: async () => false,
|
|
25
|
+
denyOutboundEncryptedConnection: async () => false,
|
|
26
|
+
denyInboundUpgradedConnection: async () => false,
|
|
27
|
+
denyOutboundUpgradedConnection: async () => false,
|
|
28
|
+
filterMultiaddrForPeer: async () => true,
|
|
29
|
+
...gater
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a default connection gater implementation that allows everything
|
|
5
|
+
*/
|
|
6
|
+
export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
|
|
7
|
+
return {
|
|
8
|
+
denyDialPeer: async () => false,
|
|
9
|
+
denyDialMultiaddr: async () => false,
|
|
10
|
+
denyInboundConnection: async () => false,
|
|
11
|
+
denyOutboundConnection: async () => false,
|
|
12
|
+
denyInboundEncryptedConnection: async () => false,
|
|
13
|
+
denyOutboundEncryptedConnection: async () => false,
|
|
14
|
+
denyInboundUpgradedConnection: async () => false,
|
|
15
|
+
denyOutboundUpgradedConnection: async () => false,
|
|
16
|
+
filterMultiaddrForPeer: async () => true,
|
|
17
|
+
...gater
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mergeOptions from 'merge-options'
|
|
2
2
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
3
|
-
import { AGENT_VERSION } from './identify/consts.js'
|
|
4
3
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
5
4
|
import { FaultTolerance } from '@libp2p/interface-transport'
|
|
6
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -8,7 +7,7 @@ import type { Libp2pInit } from './index.js'
|
|
|
8
7
|
import { codes, messages } from './errors.js'
|
|
9
8
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
10
9
|
import type { RecursivePartial } from '@libp2p/interfaces'
|
|
11
|
-
import {
|
|
10
|
+
import type { ServiceMap } from '@libp2p/interface-libp2p'
|
|
12
11
|
|
|
13
12
|
const DefaultConfig: Partial<Libp2pInit> = {
|
|
14
13
|
addresses: {
|
|
@@ -25,62 +24,11 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
25
24
|
},
|
|
26
25
|
transportManager: {
|
|
27
26
|
faultTolerance: FaultTolerance.FATAL_ALL
|
|
28
|
-
},
|
|
29
|
-
peerRouting: {
|
|
30
|
-
refreshManager: {
|
|
31
|
-
enabled: true,
|
|
32
|
-
interval: 6e5,
|
|
33
|
-
bootDelay: 10e3
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
nat: {
|
|
37
|
-
enabled: true,
|
|
38
|
-
ttl: 7200,
|
|
39
|
-
keepAlive: true
|
|
40
|
-
},
|
|
41
|
-
identify: {
|
|
42
|
-
protocolPrefix: 'ipfs',
|
|
43
|
-
host: {
|
|
44
|
-
agentVersion: AGENT_VERSION
|
|
45
|
-
},
|
|
46
|
-
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
|
|
47
|
-
timeout: 60000,
|
|
48
|
-
maxInboundStreams: 1,
|
|
49
|
-
maxOutboundStreams: 1,
|
|
50
|
-
maxPushIncomingStreams: 1,
|
|
51
|
-
maxPushOutgoingStreams: 1,
|
|
52
|
-
maxObservedAddresses: 10
|
|
53
|
-
},
|
|
54
|
-
ping: {
|
|
55
|
-
protocolPrefix: 'ipfs',
|
|
56
|
-
// See https://github.com/libp2p/specs/blob/d4b5fb0152a6bb86cfd9ea/ping/ping.md?plain=1#L38-L43
|
|
57
|
-
// The dialing peer MUST NOT keep more than one outbound stream for the ping protocol per peer.
|
|
58
|
-
// The listening peer SHOULD accept at most two streams per peer since cross-stream behavior is
|
|
59
|
-
// non-linear and stream writes occur asynchronously. The listening peer may perceive the
|
|
60
|
-
// dialing peer closing and opening the wrong streams (for instance, closing stream B and
|
|
61
|
-
// opening stream A even though the dialing peer is opening stream B and closing stream A).
|
|
62
|
-
maxInboundStreams: 2,
|
|
63
|
-
maxOutboundStreams: 1,
|
|
64
|
-
timeout: 10000
|
|
65
|
-
},
|
|
66
|
-
fetch: {
|
|
67
|
-
protocolPrefix: 'libp2p',
|
|
68
|
-
maxInboundStreams: 1,
|
|
69
|
-
maxOutboundStreams: 1,
|
|
70
|
-
timeout: 10000
|
|
71
|
-
},
|
|
72
|
-
autonat: {
|
|
73
|
-
protocolPrefix: 'libp2p',
|
|
74
|
-
maxInboundStreams: 1,
|
|
75
|
-
maxOutboundStreams: 1,
|
|
76
|
-
timeout: 30000,
|
|
77
|
-
startupDelay: 5000,
|
|
78
|
-
refreshInterval: 60000
|
|
79
27
|
}
|
|
80
28
|
}
|
|
81
29
|
|
|
82
|
-
export function validateConfig (opts: RecursivePartial<Libp2pInit
|
|
83
|
-
const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
|
|
30
|
+
export function validateConfig <T extends ServiceMap = {}> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
|
|
31
|
+
const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
|
|
84
32
|
|
|
85
33
|
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
|
|
86
34
|
throw new CodeError(messages.ERR_TRANSPORTS_REQUIRED, codes.ERR_TRANSPORTS_REQUIRED)
|
|
@@ -94,14 +42,5 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
|
|
|
94
42
|
throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED)
|
|
95
43
|
}
|
|
96
44
|
|
|
97
|
-
// Append user agent version to default AGENT_VERSION depending on the environment
|
|
98
|
-
if (resultingOptions.identify.host.agentVersion === AGENT_VERSION) {
|
|
99
|
-
if (isNode || isElectronMain) {
|
|
100
|
-
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.process.version}`
|
|
101
|
-
} else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
|
|
102
|
-
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
45
|
return resultingOptions
|
|
107
46
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
3
3
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
4
|
-
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
|
+
import { PeerMap, PeerSet } from '@libp2p/peer-collections'
|
|
5
5
|
import PQueue from 'p-queue'
|
|
6
6
|
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
|
|
7
7
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
8
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
9
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
8
10
|
|
|
9
11
|
const log = logger('libp2p:connection-manager:auto-dial')
|
|
10
12
|
|
|
@@ -18,6 +20,7 @@ interface AutoDialInit {
|
|
|
18
20
|
interface AutoDialComponents {
|
|
19
21
|
connectionManager: ConnectionManager
|
|
20
22
|
peerStore: PeerStore
|
|
23
|
+
events: EventEmitter<Libp2pEvents>
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
const defaultOptions = {
|
|
@@ -55,6 +58,14 @@ export class AutoDial implements Startable {
|
|
|
55
58
|
this.queue.addListener('error', (err) => {
|
|
56
59
|
log.error('error during auto-dial', err)
|
|
57
60
|
})
|
|
61
|
+
|
|
62
|
+
// check the min connection limit whenever a peer disconnects
|
|
63
|
+
components.events.addEventListener('connection:close', () => {
|
|
64
|
+
this.autoDial()
|
|
65
|
+
.catch(err => {
|
|
66
|
+
log.error(err)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
isStarted (): boolean {
|
|
@@ -86,7 +97,18 @@ export class AutoDial implements Startable {
|
|
|
86
97
|
}
|
|
87
98
|
|
|
88
99
|
async autoDial (): Promise<void> {
|
|
89
|
-
|
|
100
|
+
if (!this.started) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const connections = this.connectionManager.getConnectionsMap()
|
|
105
|
+
const numConnections = connections.size
|
|
106
|
+
const dialQueue = new PeerSet(
|
|
107
|
+
// @ts-expect-error boolean filter removes falsy peer IDs
|
|
108
|
+
this.connectionManager.getDialQueue()
|
|
109
|
+
.map(queue => queue.peerId)
|
|
110
|
+
.filter(Boolean)
|
|
111
|
+
)
|
|
90
112
|
|
|
91
113
|
// Already has enough connections
|
|
92
114
|
if (numConnections >= this.minConnections) {
|
|
@@ -97,40 +119,47 @@ export class AutoDial implements Startable {
|
|
|
97
119
|
log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
|
|
98
120
|
|
|
99
121
|
// Sort peers on whether we know protocols or public keys for them
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
log('loaded %d peers from the peer store', peers.length)
|
|
122
|
+
const peers = await this.peerStore.all()
|
|
103
123
|
|
|
104
124
|
// Remove some peers
|
|
105
|
-
|
|
125
|
+
const filteredPeers = peers.filter((peer) => {
|
|
106
126
|
// Remove peers without addresses
|
|
107
127
|
if (peer.addresses.length === 0) {
|
|
108
128
|
return false
|
|
109
129
|
}
|
|
110
130
|
|
|
131
|
+
// remove peers we are already connected to
|
|
132
|
+
if (connections.has(peer.id)) {
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// remove peers we are already dialling
|
|
137
|
+
if (dialQueue.has(peer.id)) {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
|
|
111
141
|
return true
|
|
112
142
|
})
|
|
113
143
|
|
|
114
|
-
// shuffle the peers
|
|
115
|
-
|
|
144
|
+
// shuffle the peers - this is so peers with the same tag values will be
|
|
145
|
+
// dialled in a different order each time
|
|
146
|
+
const shuffledPeers = filteredPeers.sort(() => Math.random() > 0.5 ? 1 : -1)
|
|
116
147
|
|
|
117
148
|
// Sort shuffled peers by tag value
|
|
118
149
|
const peerValues = new PeerMap<number>()
|
|
119
|
-
for (const peer of
|
|
150
|
+
for (const peer of shuffledPeers) {
|
|
120
151
|
if (peerValues.has(peer.id)) {
|
|
121
152
|
continue
|
|
122
153
|
}
|
|
123
154
|
|
|
124
|
-
const tags = await this.peerStore.getTags(peer.id)
|
|
125
|
-
|
|
126
155
|
// sum all tag values
|
|
127
|
-
peerValues.set(peer.id, (
|
|
156
|
+
peerValues.set(peer.id, [...peer.tags.values()].reduce((acc, curr) => {
|
|
128
157
|
return acc + curr.value
|
|
129
158
|
}, 0))
|
|
130
159
|
}
|
|
131
160
|
|
|
132
161
|
// sort by value, highest to lowest
|
|
133
|
-
const sortedPeers =
|
|
162
|
+
const sortedPeers = shuffledPeers.sort((a, b) => {
|
|
134
163
|
const peerAValue = peerValues.get(a.id) ?? 0
|
|
135
164
|
const peerBValue = peerValues.get(b.id) ?? 0
|
|
136
165
|
|
|
@@ -145,11 +174,11 @@ export class AutoDial implements Startable {
|
|
|
145
174
|
return 0
|
|
146
175
|
})
|
|
147
176
|
|
|
148
|
-
log('selected %d peers to dial', sortedPeers.length)
|
|
177
|
+
log('selected %d/%d peers to dial', sortedPeers.length, peers.length)
|
|
149
178
|
|
|
150
179
|
for (const peer of sortedPeers) {
|
|
151
180
|
this.queue.add(async () => {
|
|
152
|
-
const numConnections = this.connectionManager.
|
|
181
|
+
const numConnections = this.connectionManager.getConnectionsMap().size
|
|
153
182
|
|
|
154
183
|
// Check to see if we still need to auto dial
|
|
155
184
|
if (numConnections >= this.minConnections) {
|