libp2p 0.39.5 → 0.40.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/README.md +5 -0
- package/dist/src/address-manager/index.d.ts +18 -2
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +18 -5
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts +2 -2
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +12 -12
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/index.d.ts +13 -3
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +29 -34
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +22 -7
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +26 -17
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/components.d.ts +110 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +199 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +6 -6
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +12 -5
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +9 -9
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -9
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +82 -39
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +7 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +4 -4
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +7 -3
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +24 -35
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +41 -41
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +23 -34
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +10 -9
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +2 -8
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -3
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +24 -29
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -2
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +19 -19
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +48 -71
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts +9 -2
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +3 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +11 -2
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-record-updater.js +10 -10
- package/dist/src/peer-record-updater.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -2
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +4 -4
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +7 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +3 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +2 -17
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +4 -1
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +4 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +9 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +4 -4
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +17 -5
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +55 -43
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +37 -37
- package/src/address-manager/index.ts +31 -8
- package/src/circuit/auto-relay.ts +15 -15
- package/src/circuit/index.ts +16 -5
- package/src/circuit/pb/index.ts +29 -35
- package/src/circuit/transport.ts +49 -20
- package/src/components.ts +332 -0
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +23 -15
- package/src/connection-manager/index.ts +139 -47
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +12 -7
- package/src/fetch/pb/proto.ts +24 -35
- package/src/identify/index.ts +57 -53
- package/src/identify/pb/message.ts +23 -35
- package/src/index.ts +10 -9
- package/src/insecure/index.ts +6 -3
- package/src/insecure/pb/proto.ts +24 -29
- package/src/keychain/index.ts +27 -22
- package/src/libp2p.ts +49 -77
- package/src/nat-manager.ts +14 -6
- package/src/peer-record-updater.ts +23 -13
- package/src/peer-routing.ts +12 -7
- package/src/ping/index.ts +12 -7
- package/src/pnet/index.ts +5 -1
- package/src/pubsub/dummy-pubsub.ts +3 -1
- package/src/registrar.ts +17 -10
- package/src/transport-manager.ts +15 -8
- package/src/upgrader.ts +75 -49
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import errCode from 'err-code'
|
|
2
|
+
import type { ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection'
|
|
3
|
+
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
4
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
5
|
+
import { isStartable, Startable } from '@libp2p/interfaces/startable'
|
|
6
|
+
import type { Metrics } from '@libp2p/interface-metrics'
|
|
7
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
+
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
9
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
10
|
+
import type { Registrar } from '@libp2p/interface-registrar'
|
|
11
|
+
import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
|
|
12
|
+
import type { Datastore } from 'interface-datastore'
|
|
13
|
+
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
14
|
+
import type { DualDHT } from '@libp2p/interface-dht'
|
|
15
|
+
import type { ConnectionManager, Dialer } from '@libp2p/interface-connection-manager'
|
|
16
|
+
|
|
17
|
+
export interface Components {
|
|
18
|
+
peerId: PeerId
|
|
19
|
+
addressManager: AddressManager
|
|
20
|
+
peerStore: PeerStore
|
|
21
|
+
upgrader: Upgrader
|
|
22
|
+
registrar: Registrar
|
|
23
|
+
connectionManager: ConnectionManager
|
|
24
|
+
transportManager: TransportManager
|
|
25
|
+
connectionGater: ConnectionGater
|
|
26
|
+
contentRouting: ContentRouting
|
|
27
|
+
peerRouting: PeerRouting
|
|
28
|
+
datastore: Datastore
|
|
29
|
+
connectionProtector?: ConnectionProtector
|
|
30
|
+
dialer: Dialer
|
|
31
|
+
metrics?: Metrics
|
|
32
|
+
dht?: DualDHT
|
|
33
|
+
pubsub?: PubSub
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ComponentsInit {
|
|
37
|
+
peerId?: PeerId
|
|
38
|
+
addressManager?: AddressManager
|
|
39
|
+
peerStore?: PeerStore
|
|
40
|
+
upgrader?: Upgrader
|
|
41
|
+
metrics?: Metrics
|
|
42
|
+
registrar?: Registrar
|
|
43
|
+
connectionManager?: ConnectionManager
|
|
44
|
+
transportManager?: TransportManager
|
|
45
|
+
connectionGater?: ConnectionGater
|
|
46
|
+
contentRouting?: ContentRouting
|
|
47
|
+
peerRouting?: PeerRouting
|
|
48
|
+
datastore?: Datastore
|
|
49
|
+
connectionProtector?: ConnectionProtector
|
|
50
|
+
dht?: DualDHT
|
|
51
|
+
pubsub?: PubSub
|
|
52
|
+
dialer?: Dialer
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class DefaultComponents implements Components, Startable {
|
|
56
|
+
private _peerId?: PeerId
|
|
57
|
+
private _addressManager?: AddressManager
|
|
58
|
+
private _peerStore?: PeerStore
|
|
59
|
+
private _upgrader?: Upgrader
|
|
60
|
+
private _metrics?: Metrics
|
|
61
|
+
private _registrar?: Registrar
|
|
62
|
+
private _connectionManager?: ConnectionManager
|
|
63
|
+
private _transportManager?: TransportManager
|
|
64
|
+
private _connectionGater?: ConnectionGater
|
|
65
|
+
private _contentRouting?: ContentRouting
|
|
66
|
+
private _peerRouting?: PeerRouting
|
|
67
|
+
private _datastore?: Datastore
|
|
68
|
+
private _connectionProtector?: ConnectionProtector
|
|
69
|
+
private _dht?: DualDHT
|
|
70
|
+
private _pubsub?: PubSub
|
|
71
|
+
private _dialer?: Dialer
|
|
72
|
+
private _started = false
|
|
73
|
+
|
|
74
|
+
constructor (init: ComponentsInit = {}) {
|
|
75
|
+
this._peerId = init.peerId
|
|
76
|
+
this._addressManager = init.addressManager
|
|
77
|
+
this._peerStore = init.peerStore
|
|
78
|
+
this._upgrader = init.upgrader
|
|
79
|
+
this._metrics = init.metrics
|
|
80
|
+
this._registrar = init.registrar
|
|
81
|
+
this._connectionManager = init.connectionManager
|
|
82
|
+
this._transportManager = init.transportManager
|
|
83
|
+
this._connectionGater = init.connectionGater
|
|
84
|
+
this._contentRouting = init.contentRouting
|
|
85
|
+
this._peerRouting = init.peerRouting
|
|
86
|
+
this._datastore = init.datastore
|
|
87
|
+
this._connectionProtector = init.connectionProtector
|
|
88
|
+
this._dht = init.dht
|
|
89
|
+
this._pubsub = init.pubsub
|
|
90
|
+
this._dialer = init.dialer
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
isStarted () {
|
|
94
|
+
return this._started
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async beforeStart () {
|
|
98
|
+
await Promise.all(
|
|
99
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
100
|
+
if (startable.beforeStart != null) {
|
|
101
|
+
await startable.beforeStart()
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async start () {
|
|
108
|
+
await Promise.all(
|
|
109
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
110
|
+
await startable.start()
|
|
111
|
+
})
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
this._started = true
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async afterStart () {
|
|
118
|
+
await Promise.all(
|
|
119
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
120
|
+
if (startable.afterStart != null) {
|
|
121
|
+
await startable.afterStart()
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async beforeStop () {
|
|
128
|
+
await Promise.all(
|
|
129
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
130
|
+
if (startable.beforeStop != null) {
|
|
131
|
+
await startable.beforeStop()
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async stop () {
|
|
138
|
+
await Promise.all(
|
|
139
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
140
|
+
await startable.stop()
|
|
141
|
+
})
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
this._started = false
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async afterStop () {
|
|
148
|
+
await Promise.all(
|
|
149
|
+
Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
|
|
150
|
+
if (startable.afterStop != null) {
|
|
151
|
+
await startable.afterStop()
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
get peerId (): PeerId {
|
|
158
|
+
if (this._peerId == null) {
|
|
159
|
+
throw errCode(new Error('peerId not set'), 'ERR_SERVICE_MISSING')
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return this._peerId
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
set peerId (peerId: PeerId) {
|
|
166
|
+
this._peerId = peerId
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get addressManager (): AddressManager {
|
|
170
|
+
if (this._addressManager == null) {
|
|
171
|
+
throw errCode(new Error('addressManager not set'), 'ERR_SERVICE_MISSING')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return this._addressManager
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
set addressManager (addressManager: AddressManager) {
|
|
178
|
+
this._addressManager = addressManager
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
get peerStore (): PeerStore {
|
|
182
|
+
if (this._peerStore == null) {
|
|
183
|
+
throw errCode(new Error('peerStore not set'), 'ERR_SERVICE_MISSING')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return this._peerStore
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
set peerStore (peerStore: PeerStore) {
|
|
190
|
+
this._peerStore = peerStore
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get upgrader (): Upgrader {
|
|
194
|
+
if (this._upgrader == null) {
|
|
195
|
+
throw errCode(new Error('upgrader not set'), 'ERR_SERVICE_MISSING')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return this._upgrader
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
set upgrader (upgrader: Upgrader) {
|
|
202
|
+
this._upgrader = upgrader
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
get registrar (): Registrar {
|
|
206
|
+
if (this._registrar == null) {
|
|
207
|
+
throw errCode(new Error('registrar not set'), 'ERR_SERVICE_MISSING')
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return this._registrar
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
set registrar (registrar: Registrar) {
|
|
214
|
+
this._registrar = registrar
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get connectionManager (): ConnectionManager {
|
|
218
|
+
if (this._connectionManager == null) {
|
|
219
|
+
throw errCode(new Error('connectionManager not set'), 'ERR_SERVICE_MISSING')
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return this._connectionManager
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
set connectionManager (connectionManager: ConnectionManager) {
|
|
226
|
+
this._connectionManager = connectionManager
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
get transportManager (): TransportManager {
|
|
230
|
+
if (this._transportManager == null) {
|
|
231
|
+
throw errCode(new Error('transportManager not set'), 'ERR_SERVICE_MISSING')
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return this._transportManager
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
set transportManager (transportManager: TransportManager) {
|
|
238
|
+
this._transportManager = transportManager
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
get connectionGater (): ConnectionGater {
|
|
242
|
+
if (this._connectionGater == null) {
|
|
243
|
+
throw errCode(new Error('connectionGater not set'), 'ERR_SERVICE_MISSING')
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return this._connectionGater
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
set connectionGater (connectionGater: ConnectionGater) {
|
|
250
|
+
this._connectionGater = connectionGater
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
get contentRouting (): ContentRouting {
|
|
254
|
+
if (this._contentRouting == null) {
|
|
255
|
+
throw errCode(new Error('contentRouting not set'), 'ERR_SERVICE_MISSING')
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return this._contentRouting
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
set contentRouting (contentRouting: ContentRouting) {
|
|
262
|
+
this._contentRouting = contentRouting
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
get peerRouting (): PeerRouting {
|
|
266
|
+
if (this._peerRouting == null) {
|
|
267
|
+
throw errCode(new Error('peerRouting not set'), 'ERR_SERVICE_MISSING')
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return this._peerRouting
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
set peerRouting (peerRouting: PeerRouting) {
|
|
274
|
+
this._peerRouting = peerRouting
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
get datastore (): Datastore {
|
|
278
|
+
if (this._datastore == null) {
|
|
279
|
+
throw errCode(new Error('datastore not set'), 'ERR_SERVICE_MISSING')
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return this._datastore
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
set datastore (datastore: Datastore) {
|
|
286
|
+
this._datastore = datastore
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
get connectionProtector (): ConnectionProtector | undefined {
|
|
290
|
+
return this._connectionProtector
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
set connectionProtector (connectionProtector: ConnectionProtector | undefined) {
|
|
294
|
+
this._connectionProtector = connectionProtector
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
get dialer (): Dialer {
|
|
298
|
+
if (this._dialer == null) {
|
|
299
|
+
throw errCode(new Error('dialer not set'), 'ERR_SERVICE_MISSING')
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return this._dialer
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
set dialer (dialer: Dialer) {
|
|
306
|
+
this._dialer = dialer
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
get metrics (): Metrics | undefined {
|
|
310
|
+
return this._metrics
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
set metrics (metrics: Metrics | undefined) {
|
|
314
|
+
this._metrics = metrics
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
get dht (): DualDHT | undefined {
|
|
318
|
+
return this._dht
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
set dht (dht: DualDHT | undefined) {
|
|
322
|
+
this._dht = dht
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
get pubsub (): PubSub | undefined {
|
|
326
|
+
return this._pubsub
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
set pubsub (pubsub: PubSub | undefined) {
|
|
330
|
+
this._pubsub = pubsub
|
|
331
|
+
}
|
|
332
|
+
}
|
|
@@ -7,7 +7,9 @@ import { pipe } from 'it-pipe'
|
|
|
7
7
|
import filter from 'it-filter'
|
|
8
8
|
import sort from 'it-sort'
|
|
9
9
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
10
|
-
import type {
|
|
10
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
11
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
12
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
11
13
|
|
|
12
14
|
const log = logger('libp2p:connection-manager:auto-dialler')
|
|
13
15
|
|
|
@@ -28,6 +30,12 @@ export interface AutoDiallerInit {
|
|
|
28
30
|
autoDialInterval?: number
|
|
29
31
|
}
|
|
30
32
|
|
|
33
|
+
export interface AutoDiallerComponents {
|
|
34
|
+
peerId: PeerId
|
|
35
|
+
connectionManager: ConnectionManager
|
|
36
|
+
peerStore: PeerStore
|
|
37
|
+
}
|
|
38
|
+
|
|
31
39
|
const defaultOptions: Partial<AutoDiallerInit> = {
|
|
32
40
|
enabled: true,
|
|
33
41
|
minConnections: 0,
|
|
@@ -35,7 +43,7 @@ const defaultOptions: Partial<AutoDiallerInit> = {
|
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
export class AutoDialler implements Startable {
|
|
38
|
-
private readonly components:
|
|
46
|
+
private readonly components: AutoDiallerComponents
|
|
39
47
|
private readonly options: Required<AutoDiallerInit>
|
|
40
48
|
private running: boolean
|
|
41
49
|
private autoDialTimeout?: ReturnType<retimer>
|
|
@@ -45,7 +53,7 @@ export class AutoDialler implements Startable {
|
|
|
45
53
|
* It will keep the number of connections below the upper limit and sort
|
|
46
54
|
* the peers to connect based on wether we know their keys and protocols.
|
|
47
55
|
*/
|
|
48
|
-
constructor (components:
|
|
56
|
+
constructor (components: AutoDiallerComponents, init: AutoDiallerInit) {
|
|
49
57
|
this.components = components
|
|
50
58
|
this.options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
51
59
|
this.running = false
|
|
@@ -102,19 +110,19 @@ export class AutoDialler implements Startable {
|
|
|
102
110
|
const minConnections = this.options.minConnections
|
|
103
111
|
|
|
104
112
|
// Already has enough connections
|
|
105
|
-
if (this.components.
|
|
113
|
+
if (this.components.connectionManager.getConnections().length >= minConnections) {
|
|
106
114
|
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
115
|
|
|
108
116
|
return
|
|
109
117
|
}
|
|
110
118
|
|
|
111
119
|
// Sort peers on whether we know protocols or public keys for them
|
|
112
|
-
const allPeers = await this.components.
|
|
120
|
+
const allPeers = await this.components.peerStore.all()
|
|
113
121
|
|
|
114
122
|
const peers = await pipe(
|
|
115
123
|
// shuffle the peers
|
|
116
124
|
allPeers.sort(() => Math.random() > 0.5 ? 1 : -1),
|
|
117
|
-
(source) => filter(source, (peer) => !peer.id.equals(this.components.
|
|
125
|
+
(source) => filter(source, (peer) => !peer.id.equals(this.components.peerId)),
|
|
118
126
|
(source) => sort(source, (a, b) => {
|
|
119
127
|
if (b.protocols.length > a.protocols.length) {
|
|
120
128
|
return 1
|
|
@@ -126,7 +134,7 @@ export class AutoDialler implements Startable {
|
|
|
126
134
|
async (source) => await all(source)
|
|
127
135
|
)
|
|
128
136
|
|
|
129
|
-
for (let i = 0; this.running && i < peers.length && this.components.
|
|
137
|
+
for (let i = 0; this.running && i < peers.length && this.components.connectionManager.getConnections().length < minConnections; i++) {
|
|
130
138
|
// Connection Manager was stopped during async dial
|
|
131
139
|
if (!this.running) {
|
|
132
140
|
return
|
|
@@ -134,10 +142,10 @@ export class AutoDialler implements Startable {
|
|
|
134
142
|
|
|
135
143
|
const peer = peers[i]
|
|
136
144
|
|
|
137
|
-
if (this.components.
|
|
145
|
+
if (this.components.connectionManager.getConnections(peer.id).length === 0) {
|
|
138
146
|
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
147
|
try {
|
|
140
|
-
await this.components.
|
|
148
|
+
await this.components.connectionManager.openConnection(peer.id)
|
|
141
149
|
} catch (err: any) {
|
|
142
150
|
log.error('could not connect to peerStore stored peer', err)
|
|
143
151
|
}
|
|
@@ -19,17 +19,17 @@ import {
|
|
|
19
19
|
MAX_PER_PEER_DIALS,
|
|
20
20
|
MAX_ADDRS_TO_DIAL
|
|
21
21
|
} from '../../constants.js'
|
|
22
|
-
import type { Connection } from '@libp2p/interface-connection'
|
|
22
|
+
import type { Connection, ConnectionGater } from '@libp2p/interface-connection'
|
|
23
23
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
24
24
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
25
25
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
26
26
|
import { getPeer } from '../../get-peer.js'
|
|
27
27
|
import sort from 'it-sort'
|
|
28
|
-
import type { Components } from '@libp2p/components'
|
|
29
28
|
import map from 'it-map'
|
|
30
|
-
import type { AddressSorter } from '@libp2p/interface-peer-store'
|
|
31
|
-
import type { ComponentMetricsTracker } from '@libp2p/interface-metrics'
|
|
29
|
+
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
|
|
30
|
+
import type { ComponentMetricsTracker, Metrics } from '@libp2p/interface-metrics'
|
|
32
31
|
import type { Dialer } from '@libp2p/interface-connection-manager'
|
|
32
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
33
33
|
|
|
34
34
|
const log = logger('libp2p:dialer')
|
|
35
35
|
|
|
@@ -87,8 +87,16 @@ export interface DialerInit {
|
|
|
87
87
|
metrics?: ComponentMetricsTracker
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
export interface DefaultDialerComponents {
|
|
91
|
+
peerId: PeerId
|
|
92
|
+
metrics?: Metrics
|
|
93
|
+
peerStore: PeerStore
|
|
94
|
+
transportManager: TransportManager
|
|
95
|
+
connectionGater: ConnectionGater
|
|
96
|
+
}
|
|
97
|
+
|
|
90
98
|
export class DefaultDialer implements Startable, Dialer {
|
|
91
|
-
private readonly components:
|
|
99
|
+
private readonly components: DefaultDialerComponents
|
|
92
100
|
private readonly addressSorter: AddressSorter
|
|
93
101
|
private readonly maxAddrsToDial: number
|
|
94
102
|
private readonly timeout: number
|
|
@@ -98,7 +106,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
98
106
|
public pendingDialTargets: Map<string, PendingDialTarget>
|
|
99
107
|
private started: boolean
|
|
100
108
|
|
|
101
|
-
constructor (components:
|
|
109
|
+
constructor (components: DefaultDialerComponents, init: DialerInit = {}) {
|
|
102
110
|
this.started = false
|
|
103
111
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
104
112
|
this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL
|
|
@@ -114,7 +122,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
114
122
|
this.pendingDialTargets = trackedMap({
|
|
115
123
|
component: METRICS_COMPONENT,
|
|
116
124
|
metric: METRICS_PENDING_DIAL_TARGETS,
|
|
117
|
-
metrics: components.
|
|
125
|
+
metrics: components.metrics
|
|
118
126
|
})
|
|
119
127
|
|
|
120
128
|
for (const [key, value] of Object.entries(init.resolvers ?? {})) {
|
|
@@ -159,7 +167,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
159
167
|
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
160
168
|
const { id, multiaddrs } = getPeer(peer)
|
|
161
169
|
|
|
162
|
-
if (this.components.
|
|
170
|
+
if (this.components.peerId.equals(id)) {
|
|
163
171
|
throw errCode(new Error('Tried to dial self'), codes.ERR_DIALED_SELF)
|
|
164
172
|
}
|
|
165
173
|
|
|
@@ -167,10 +175,10 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
167
175
|
|
|
168
176
|
if (multiaddrs != null && multiaddrs.length > 0) {
|
|
169
177
|
log('storing multiaddrs %p', id, multiaddrs)
|
|
170
|
-
await this.components.
|
|
178
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
171
179
|
}
|
|
172
180
|
|
|
173
|
-
if (await this.components.
|
|
181
|
+
if (await this.components.connectionGater.denyDialPeer(id)) {
|
|
174
182
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
175
183
|
}
|
|
176
184
|
|
|
@@ -235,9 +243,9 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
235
243
|
const _resolve = this._resolve.bind(this)
|
|
236
244
|
|
|
237
245
|
const addrs = await pipe(
|
|
238
|
-
await this.components.
|
|
246
|
+
await this.components.peerStore.addressBook.get(peer),
|
|
239
247
|
(source) => filter(source, async (address) => {
|
|
240
|
-
return !(await this.components.
|
|
248
|
+
return !(await this.components.connectionGater.denyDialMultiaddr(peer, address.multiaddr))
|
|
241
249
|
}),
|
|
242
250
|
// Sort addresses so, for example, we try certified public address first
|
|
243
251
|
(source) => sort(source, this.addressSorter),
|
|
@@ -247,7 +255,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
247
255
|
}
|
|
248
256
|
},
|
|
249
257
|
// Multiaddrs not supported by the available transports will be filtered out.
|
|
250
|
-
(source) => filter(source, (ma) => Boolean(this.components.
|
|
258
|
+
(source) => filter(source, (ma) => Boolean(this.components.transportManager.transportForMultiaddr(ma))),
|
|
251
259
|
(source) => map(source, (ma) => {
|
|
252
260
|
if (peer.toString() === ma.getPeerId()) {
|
|
253
261
|
return ma
|
|
@@ -259,7 +267,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
259
267
|
)
|
|
260
268
|
|
|
261
269
|
if (addrs.length > this.maxAddrsToDial) {
|
|
262
|
-
await this.components.
|
|
270
|
+
await this.components.peerStore.delete(peer)
|
|
263
271
|
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
|
|
264
272
|
}
|
|
265
273
|
|
|
@@ -282,7 +290,7 @@ export class DefaultDialer implements Startable, Dialer {
|
|
|
282
290
|
throw errCode(new Error('already aborted'), codes.ERR_ALREADY_ABORTED)
|
|
283
291
|
}
|
|
284
292
|
|
|
285
|
-
return await this.components.
|
|
293
|
+
return await this.components.transportManager.dial(addr, options).catch(err => {
|
|
286
294
|
log.error('dial to %s failed', addr, err)
|
|
287
295
|
throw err
|
|
288
296
|
})
|