libp2p 0.39.5 → 0.40.0-030dbc8

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.
Files changed (170) hide show
  1. package/README.md +14 -2
  2. package/dist/src/address-manager/index.d.ts +18 -2
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +19 -6
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/circuit/auto-relay.d.ts +2 -2
  7. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  8. package/dist/src/circuit/auto-relay.js +12 -12
  9. package/dist/src/circuit/auto-relay.js.map +1 -1
  10. package/dist/src/circuit/index.d.ts +13 -3
  11. package/dist/src/circuit/index.d.ts.map +1 -1
  12. package/dist/src/circuit/index.js +1 -1
  13. package/dist/src/circuit/index.js.map +1 -1
  14. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  15. package/dist/src/circuit/pb/index.js +29 -34
  16. package/dist/src/circuit/pb/index.js.map +1 -1
  17. package/dist/src/circuit/transport.d.ts +22 -7
  18. package/dist/src/circuit/transport.d.ts.map +1 -1
  19. package/dist/src/circuit/transport.js +26 -17
  20. package/dist/src/circuit/transport.js.map +1 -1
  21. package/dist/src/components.d.ts +110 -0
  22. package/dist/src/components.d.ts.map +1 -0
  23. package/dist/src/components.js +199 -0
  24. package/dist/src/components.js.map +1 -0
  25. package/dist/src/config.d.ts.map +1 -1
  26. package/dist/src/config.js +0 -11
  27. package/dist/src/config.js.map +1 -1
  28. package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
  29. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  30. package/dist/src/connection-manager/auto-dialler.js +6 -6
  31. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  32. package/dist/src/connection-manager/dialer/index.d.ts +12 -6
  33. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  34. package/dist/src/connection-manager/dialer/index.js +12 -17
  35. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  36. package/dist/src/connection-manager/index.d.ts +41 -17
  37. package/dist/src/connection-manager/index.d.ts.map +1 -1
  38. package/dist/src/connection-manager/index.js +80 -81
  39. package/dist/src/connection-manager/index.js.map +1 -1
  40. package/dist/src/content-routing/index.d.ts +7 -2
  41. package/dist/src/content-routing/index.d.ts.map +1 -1
  42. package/dist/src/content-routing/index.js +4 -4
  43. package/dist/src/content-routing/index.js.map +1 -1
  44. package/dist/src/content-routing/utils.d.ts +1 -1
  45. package/dist/src/content-routing/utils.d.ts.map +1 -1
  46. package/dist/src/errors.d.ts +2 -1
  47. package/dist/src/errors.d.ts.map +1 -1
  48. package/dist/src/errors.js +1 -0
  49. package/dist/src/errors.js.map +1 -1
  50. package/dist/src/fetch/index.d.ts +7 -3
  51. package/dist/src/fetch/index.d.ts.map +1 -1
  52. package/dist/src/fetch/index.js +18 -6
  53. package/dist/src/fetch/index.js.map +1 -1
  54. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  55. package/dist/src/fetch/pb/proto.js +24 -35
  56. package/dist/src/fetch/pb/proto.js.map +1 -1
  57. package/dist/src/identify/index.d.ts +13 -3
  58. package/dist/src/identify/index.d.ts.map +1 -1
  59. package/dist/src/identify/index.js +41 -41
  60. package/dist/src/identify/index.js.map +1 -1
  61. package/dist/src/identify/pb/message.d.ts.map +1 -1
  62. package/dist/src/identify/pb/message.js +23 -34
  63. package/dist/src/identify/pb/message.js.map +1 -1
  64. package/dist/src/index.d.ts +13 -12
  65. package/dist/src/index.d.ts.map +1 -1
  66. package/dist/src/index.js.map +1 -1
  67. package/dist/src/insecure/index.d.ts +2 -8
  68. package/dist/src/insecure/index.d.ts.map +1 -1
  69. package/dist/src/insecure/index.js +5 -3
  70. package/dist/src/insecure/index.js.map +1 -1
  71. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  72. package/dist/src/insecure/pb/proto.js +24 -29
  73. package/dist/src/insecure/pb/proto.js.map +1 -1
  74. package/dist/src/keychain/index.d.ts +6 -2
  75. package/dist/src/keychain/index.d.ts.map +1 -1
  76. package/dist/src/keychain/index.js +19 -19
  77. package/dist/src/keychain/index.js.map +1 -1
  78. package/dist/src/libp2p.d.ts +1 -2
  79. package/dist/src/libp2p.d.ts.map +1 -1
  80. package/dist/src/libp2p.js +49 -73
  81. package/dist/src/libp2p.js.map +1 -1
  82. package/dist/src/nat-manager.d.ts +9 -2
  83. package/dist/src/nat-manager.d.ts.map +1 -1
  84. package/dist/src/nat-manager.js +3 -3
  85. package/dist/src/nat-manager.js.map +1 -1
  86. package/dist/src/peer-record-updater.d.ts +11 -2
  87. package/dist/src/peer-record-updater.d.ts.map +1 -1
  88. package/dist/src/peer-record-updater.js +10 -10
  89. package/dist/src/peer-record-updater.js.map +1 -1
  90. package/dist/src/peer-routing.d.ts +6 -2
  91. package/dist/src/peer-routing.d.ts.map +1 -1
  92. package/dist/src/peer-routing.js +4 -4
  93. package/dist/src/peer-routing.js.map +1 -1
  94. package/dist/src/ping/index.d.ts +7 -3
  95. package/dist/src/ping/index.d.ts.map +1 -1
  96. package/dist/src/ping/index.js +3 -3
  97. package/dist/src/ping/index.js.map +1 -1
  98. package/dist/src/pnet/index.d.ts +2 -17
  99. package/dist/src/pnet/index.d.ts.map +1 -1
  100. package/dist/src/pnet/index.js +4 -1
  101. package/dist/src/pnet/index.js.map +1 -1
  102. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
  103. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  104. package/dist/src/pubsub/dummy-pubsub.js +4 -0
  105. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  106. package/dist/src/registrar.d.ts +9 -3
  107. package/dist/src/registrar.d.ts.map +1 -1
  108. package/dist/src/registrar.js +6 -6
  109. package/dist/src/registrar.js.map +1 -1
  110. package/dist/src/transport-manager.d.ts +9 -3
  111. package/dist/src/transport-manager.d.ts.map +1 -1
  112. package/dist/src/transport-manager.js +5 -6
  113. package/dist/src/transport-manager.js.map +1 -1
  114. package/dist/src/upgrader.d.ts +17 -5
  115. package/dist/src/upgrader.d.ts.map +1 -1
  116. package/dist/src/upgrader.js +55 -75
  117. package/dist/src/upgrader.js.map +1 -1
  118. package/package.json +39 -40
  119. package/src/address-manager/index.ts +34 -10
  120. package/src/circuit/README.md +6 -6
  121. package/src/circuit/auto-relay.ts +15 -15
  122. package/src/circuit/index.ts +16 -5
  123. package/src/circuit/pb/index.ts +29 -35
  124. package/src/circuit/transport.ts +49 -20
  125. package/src/components.ts +332 -0
  126. package/src/config.ts +0 -11
  127. package/src/connection-manager/auto-dialler.ts +17 -9
  128. package/src/connection-manager/dialer/index.ts +26 -25
  129. package/src/connection-manager/index.ts +136 -93
  130. package/src/content-routing/index.ts +13 -7
  131. package/src/errors.ts +2 -1
  132. package/src/fetch/index.ts +28 -10
  133. package/src/fetch/pb/proto.ts +24 -35
  134. package/src/identify/index.ts +57 -53
  135. package/src/identify/pb/message.ts +23 -35
  136. package/src/index.ts +12 -11
  137. package/src/insecure/index.ts +6 -3
  138. package/src/insecure/pb/proto.ts +24 -29
  139. package/src/keychain/index.ts +27 -22
  140. package/src/libp2p.ts +50 -79
  141. package/src/nat-manager.ts +14 -6
  142. package/src/peer-record-updater.ts +23 -13
  143. package/src/peer-routing.ts +12 -7
  144. package/src/ping/index.ts +12 -7
  145. package/src/pnet/index.ts +5 -1
  146. package/src/pubsub/dummy-pubsub.ts +3 -1
  147. package/src/registrar.ts +17 -10
  148. package/src/transport-manager.ts +16 -10
  149. package/src/upgrader.ts +78 -87
  150. package/src/version.ts +1 -1
  151. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
  152. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
  153. package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
  154. package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
  155. package/dist/src/metrics/index.d.ts +0 -93
  156. package/dist/src/metrics/index.d.ts.map +0 -1
  157. package/dist/src/metrics/index.js +0 -234
  158. package/dist/src/metrics/index.js.map +0 -1
  159. package/dist/src/metrics/moving-average.d.ts +0 -14
  160. package/dist/src/metrics/moving-average.d.ts.map +0 -1
  161. package/dist/src/metrics/moving-average.js +0 -40
  162. package/dist/src/metrics/moving-average.js.map +0 -1
  163. package/dist/src/metrics/stats.d.ts +0 -87
  164. package/dist/src/metrics/stats.d.ts.map +0 -1
  165. package/dist/src/metrics/stats.js +0 -183
  166. package/dist/src/metrics/stats.js.map +0 -1
  167. package/src/connection-manager/dialer/auto-dialer.ts +0 -65
  168. package/src/metrics/index.ts +0 -311
  169. package/src/metrics/moving-average.ts +0 -53
  170. package/src/metrics/stats.ts +0 -238
@@ -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
+ }
package/src/config.ts CHANGED
@@ -37,17 +37,6 @@ const DefaultConfig: Partial<Libp2pInit> = {
37
37
  transportManager: {
38
38
  faultTolerance: FaultTolerance.FATAL_ALL
39
39
  },
40
- metrics: {
41
- enabled: false,
42
- computeThrottleMaxQueueSize: 1000,
43
- computeThrottleTimeout: 2000,
44
- movingAverageIntervals: [
45
- 60 * 1000, // 1 minute
46
- 5 * 60 * 1000, // 5 minutes
47
- 15 * 60 * 1000 // 15 minutes
48
- ],
49
- maxOldPeersRetention: 50
50
- },
51
40
  peerRouting: {
52
41
  refreshManager: {
53
42
  enabled: true,
@@ -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 { Components } from '@libp2p/components'
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: 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: Components, init: AutoDiallerInit) {
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.getConnectionManager().getConnections().length >= minConnections) {
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.getPeerStore().all()
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.getPeerId())),
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.getConnectionManager().getConnections().length < minConnections; i++) {
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.getConnectionManager().getConnections(peer.id).length === 0) {
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.getConnectionManager().openConnection(peer.id)
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,24 +19,20 @@ 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 { 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
 
36
- const METRICS_COMPONENT = 'dialler'
37
- const METRICS_PENDING_DIALS = 'pending-dials'
38
- const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
39
-
40
36
  export interface DialTarget {
41
37
  id: string
42
38
  addrs: Multiaddr[]
@@ -84,11 +80,18 @@ export interface DialerInit {
84
80
  * Multiaddr resolvers to use when dialing
85
81
  */
86
82
  resolvers?: Record<string, Resolver>
87
- metrics?: ComponentMetricsTracker
83
+ }
84
+
85
+ export interface DefaultDialerComponents {
86
+ peerId: PeerId
87
+ metrics?: Metrics
88
+ peerStore: PeerStore
89
+ transportManager: TransportManager
90
+ connectionGater: ConnectionGater
88
91
  }
89
92
 
90
93
  export class DefaultDialer implements Startable, Dialer {
91
- private readonly components: Components
94
+ private readonly components: DefaultDialerComponents
92
95
  private readonly addressSorter: AddressSorter
93
96
  private readonly maxAddrsToDial: number
94
97
  private readonly timeout: number
@@ -98,7 +101,7 @@ export class DefaultDialer implements Startable, Dialer {
98
101
  public pendingDialTargets: Map<string, PendingDialTarget>
99
102
  private started: boolean
100
103
 
101
- constructor (components: Components, init: DialerInit = {}) {
104
+ constructor (components: DefaultDialerComponents, init: DialerInit = {}) {
102
105
  this.started = false
103
106
  this.addressSorter = init.addressSorter ?? publicAddressesFirst
104
107
  this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL
@@ -107,14 +110,12 @@ export class DefaultDialer implements Startable, Dialer {
107
110
  this.tokens = [...new Array(init.maxParallelDials ?? MAX_PARALLEL_DIALS)].map((_, index) => index)
108
111
  this.components = components
109
112
  this.pendingDials = trackedMap({
110
- component: METRICS_COMPONENT,
111
- metric: METRICS_PENDING_DIALS,
112
- metrics: init.metrics
113
+ name: 'libp2p_dialler_pending_dials',
114
+ metrics: components.metrics
113
115
  })
114
116
  this.pendingDialTargets = trackedMap({
115
- component: METRICS_COMPONENT,
116
- metric: METRICS_PENDING_DIAL_TARGETS,
117
- metrics: components.getMetrics()
117
+ name: 'libp2p_dialler_pending_dial_targets',
118
+ metrics: components.metrics
118
119
  })
119
120
 
120
121
  for (const [key, value] of Object.entries(init.resolvers ?? {})) {
@@ -159,7 +160,7 @@ export class DefaultDialer implements Startable, Dialer {
159
160
  async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
160
161
  const { id, multiaddrs } = getPeer(peer)
161
162
 
162
- if (this.components.getPeerId().equals(id)) {
163
+ if (this.components.peerId.equals(id)) {
163
164
  throw errCode(new Error('Tried to dial self'), codes.ERR_DIALED_SELF)
164
165
  }
165
166
 
@@ -167,10 +168,10 @@ export class DefaultDialer implements Startable, Dialer {
167
168
 
168
169
  if (multiaddrs != null && multiaddrs.length > 0) {
169
170
  log('storing multiaddrs %p', id, multiaddrs)
170
- await this.components.getPeerStore().addressBook.add(id, multiaddrs)
171
+ await this.components.peerStore.addressBook.add(id, multiaddrs)
171
172
  }
172
173
 
173
- if (await this.components.getConnectionGater().denyDialPeer(id)) {
174
+ if (await this.components.connectionGater.denyDialPeer(id)) {
174
175
  throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
175
176
  }
176
177
 
@@ -235,9 +236,9 @@ export class DefaultDialer implements Startable, Dialer {
235
236
  const _resolve = this._resolve.bind(this)
236
237
 
237
238
  const addrs = await pipe(
238
- await this.components.getPeerStore().addressBook.get(peer),
239
+ await this.components.peerStore.addressBook.get(peer),
239
240
  (source) => filter(source, async (address) => {
240
- return !(await this.components.getConnectionGater().denyDialMultiaddr(peer, address.multiaddr))
241
+ return !(await this.components.connectionGater.denyDialMultiaddr(peer, address.multiaddr))
241
242
  }),
242
243
  // Sort addresses so, for example, we try certified public address first
243
244
  (source) => sort(source, this.addressSorter),
@@ -247,7 +248,7 @@ export class DefaultDialer implements Startable, Dialer {
247
248
  }
248
249
  },
249
250
  // Multiaddrs not supported by the available transports will be filtered out.
250
- (source) => filter(source, (ma) => Boolean(this.components.getTransportManager().transportForMultiaddr(ma))),
251
+ (source) => filter(source, (ma) => Boolean(this.components.transportManager.transportForMultiaddr(ma))),
251
252
  (source) => map(source, (ma) => {
252
253
  if (peer.toString() === ma.getPeerId()) {
253
254
  return ma
@@ -259,7 +260,7 @@ export class DefaultDialer implements Startable, Dialer {
259
260
  )
260
261
 
261
262
  if (addrs.length > this.maxAddrsToDial) {
262
- await this.components.getPeerStore().delete(peer)
263
+ await this.components.peerStore.delete(peer)
263
264
  throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
264
265
  }
265
266
 
@@ -282,7 +283,7 @@ export class DefaultDialer implements Startable, Dialer {
282
283
  throw errCode(new Error('already aborted'), codes.ERR_ALREADY_ABORTED)
283
284
  }
284
285
 
285
- return await this.components.getTransportManager().dial(addr, options).catch(err => {
286
+ return await this.components.transportManager.dial(addr, options).catch(err => {
286
287
  log.error('dial to %s failed', addr, err)
287
288
  throw err
288
289
  })