libp2p 0.44.0-97b473ad → 0.44.0-e3deaa40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/dist/index.min.js +27 -27
  2. package/dist/src/address-manager/index.d.ts +4 -4
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +9 -6
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/autonat/index.d.ts.map +1 -1
  7. package/dist/src/autonat/index.js +15 -13
  8. package/dist/src/autonat/index.js.map +1 -1
  9. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  10. package/dist/src/circuit-relay/server/advert-service.js +2 -2
  11. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  12. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  13. package/dist/src/circuit-relay/server/index.js +7 -3
  14. package/dist/src/circuit-relay/server/index.js.map +1 -1
  15. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  16. package/dist/src/circuit-relay/transport/discovery.js +3 -1
  17. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  18. package/dist/src/circuit-relay/transport/index.d.ts +3 -0
  19. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  20. package/dist/src/circuit-relay/transport/index.js +6 -2
  21. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  22. package/dist/src/circuit-relay/transport/listener.d.ts +3 -0
  23. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  24. package/dist/src/circuit-relay/transport/listener.js +5 -4
  25. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  26. package/dist/src/circuit-relay/transport/reservation-store.d.ts +3 -0
  27. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  28. package/dist/src/circuit-relay/transport/reservation-store.js +11 -6
  29. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  30. package/dist/src/components.d.ts +6 -63
  31. package/dist/src/components.d.ts.map +1 -1
  32. package/dist/src/components.js +60 -164
  33. package/dist/src/components.js.map +1 -1
  34. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  35. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  36. package/dist/src/config/connection-gater.browser.js +27 -0
  37. package/dist/src/config/connection-gater.browser.js.map +1 -0
  38. package/dist/src/config/connection-gater.d.ts +6 -0
  39. package/dist/src/config/connection-gater.d.ts.map +1 -0
  40. package/dist/src/config/connection-gater.js +18 -0
  41. package/dist/src/config/connection-gater.js.map +1 -0
  42. package/dist/src/config.d.ts +2 -1
  43. package/dist/src/config.d.ts.map +1 -1
  44. package/dist/src/config.js +16 -23
  45. package/dist/src/config.js.map +1 -1
  46. package/dist/src/connection-manager/auto-dial.d.ts +3 -0
  47. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  48. package/dist/src/connection-manager/auto-dial.js +18 -4
  49. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  50. package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
  51. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  52. package/dist/src/connection-manager/connection-pruner.js +23 -8
  53. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  54. package/dist/src/connection-manager/dial-queue.d.ts +2 -1
  55. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  56. package/dist/src/connection-manager/dial-queue.js +18 -12
  57. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  58. package/dist/src/connection-manager/index.d.ts +8 -7
  59. package/dist/src/connection-manager/index.d.ts.map +1 -1
  60. package/dist/src/connection-manager/index.js +23 -34
  61. package/dist/src/connection-manager/index.js.map +1 -1
  62. package/dist/src/content-routing/index.d.ts +0 -10
  63. package/dist/src/content-routing/index.d.ts.map +1 -1
  64. package/dist/src/content-routing/index.js +7 -42
  65. package/dist/src/content-routing/index.js.map +1 -1
  66. package/dist/src/content-routing/utils.d.ts.map +1 -1
  67. package/dist/src/content-routing/utils.js +3 -1
  68. package/dist/src/content-routing/utils.js.map +1 -1
  69. package/dist/src/fetch/index.d.ts +13 -35
  70. package/dist/src/fetch/index.d.ts.map +1 -1
  71. package/dist/src/fetch/index.js +7 -9
  72. package/dist/src/fetch/index.js.map +1 -1
  73. package/dist/src/identify/index.d.ts +16 -48
  74. package/dist/src/identify/index.d.ts.map +1 -1
  75. package/dist/src/identify/index.js +200 -198
  76. package/dist/src/identify/index.js.map +1 -1
  77. package/dist/src/index.d.ts +11 -62
  78. package/dist/src/index.d.ts.map +1 -1
  79. package/dist/src/index.js.map +1 -1
  80. package/dist/src/insecure/index.d.ts.map +1 -1
  81. package/dist/src/insecure/index.js +0 -1
  82. package/dist/src/insecure/index.js.map +1 -1
  83. package/dist/src/libp2p.d.ts +11 -42
  84. package/dist/src/libp2p.d.ts.map +1 -1
  85. package/dist/src/libp2p.js +137 -195
  86. package/dist/src/libp2p.js.map +1 -1
  87. package/dist/src/peer-routing.d.ts.map +1 -1
  88. package/dist/src/peer-routing.js +7 -6
  89. package/dist/src/peer-routing.js.map +1 -1
  90. package/dist/src/ping/constants.d.ts +4 -0
  91. package/dist/src/ping/constants.d.ts.map +1 -1
  92. package/dist/src/ping/constants.js +10 -0
  93. package/dist/src/ping/constants.js.map +1 -1
  94. package/dist/src/ping/index.d.ts +10 -27
  95. package/dist/src/ping/index.d.ts.map +1 -1
  96. package/dist/src/ping/index.js +18 -21
  97. package/dist/src/ping/index.js.map +1 -1
  98. package/dist/src/pnet/index.d.ts.map +1 -1
  99. package/dist/src/pnet/index.js +0 -1
  100. package/dist/src/pnet/index.js.map +1 -1
  101. package/dist/src/registrar.d.ts +5 -2
  102. package/dist/src/registrar.d.ts.map +1 -1
  103. package/dist/src/registrar.js +27 -25
  104. package/dist/src/registrar.js.map +1 -1
  105. package/dist/src/transport-manager.d.ts +11 -4
  106. package/dist/src/transport-manager.d.ts.map +1 -1
  107. package/dist/src/transport-manager.js +27 -11
  108. package/dist/src/transport-manager.js.map +1 -1
  109. package/dist/src/upgrader.d.ts +15 -10
  110. package/dist/src/upgrader.d.ts.map +1 -1
  111. package/dist/src/upgrader.js +36 -55
  112. package/dist/src/upgrader.js.map +1 -1
  113. package/dist/src/upnp-nat/index.d.ts +42 -0
  114. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  115. package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
  116. package/dist/src/upnp-nat/index.js.map +1 -0
  117. package/package.json +59 -43
  118. package/src/address-manager/index.ts +12 -8
  119. package/src/autonat/index.ts +14 -13
  120. package/src/circuit-relay/server/advert-service.ts +2 -2
  121. package/src/circuit-relay/server/index.ts +7 -3
  122. package/src/circuit-relay/transport/discovery.ts +3 -1
  123. package/src/circuit-relay/transport/index.ts +10 -2
  124. package/src/circuit-relay/transport/listener.ts +8 -4
  125. package/src/circuit-relay/transport/reservation-store.ts +14 -6
  126. package/src/components.ts +71 -234
  127. package/src/config/connection-gater.browser.ts +31 -0
  128. package/src/config/connection-gater.ts +19 -0
  129. package/src/config.ts +8 -14
  130. package/src/connection-manager/auto-dial.ts +24 -5
  131. package/src/connection-manager/connection-pruner.ts +28 -8
  132. package/src/connection-manager/dial-queue.ts +21 -19
  133. package/src/connection-manager/index.ts +31 -43
  134. package/src/content-routing/index.ts +7 -55
  135. package/src/content-routing/utils.ts +3 -1
  136. package/src/fetch/index.ts +51 -14
  137. package/src/identify/index.ts +235 -225
  138. package/src/index.ts +12 -70
  139. package/src/insecure/index.ts +4 -5
  140. package/src/libp2p.ts +139 -259
  141. package/src/peer-routing.ts +10 -8
  142. package/src/ping/constants.ts +11 -0
  143. package/src/ping/index.ts +32 -28
  144. package/src/pnet/index.ts +0 -1
  145. package/src/registrar.ts +31 -27
  146. package/src/transport-manager.ts +36 -14
  147. package/src/upgrader.ts +59 -64
  148. package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
  149. package/src/version.ts +1 -1
  150. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  151. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  152. package/dist/src/dht/dht-content-routing.js +0 -32
  153. package/dist/src/dht/dht-content-routing.js.map +0 -1
  154. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  155. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  156. package/dist/src/dht/dht-peer-routing.js +0 -26
  157. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  158. package/dist/src/dht/dummy-dht.d.ts +0 -20
  159. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  160. package/dist/src/dht/dummy-dht.js +0 -46
  161. package/dist/src/dht/dummy-dht.js.map +0 -1
  162. package/dist/src/nat-manager.d.ts +0 -74
  163. package/dist/src/nat-manager.d.ts.map +0 -1
  164. package/dist/src/nat-manager.js.map +0 -1
  165. package/dist/src/peer-record-updater.d.ts +0 -24
  166. package/dist/src/peer-record-updater.d.ts.map +0 -1
  167. package/dist/src/peer-record-updater.js +0 -44
  168. package/dist/src/peer-record-updater.js.map +0 -1
  169. package/src/dht/dht-content-routing.ts +0 -44
  170. package/src/dht/dht-peer-routing.ts +0 -36
  171. package/src/dht/dummy-dht.ts +0 -60
  172. 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
- export class DefaultComponents implements Components, Startable {
55
- private _peerId?: PeerId
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._peerId = init.peerId
74
- this._addressManager = init.addressManager
75
- this._peerStore = init.peerStore
76
- this._upgrader = init.upgrader
77
- this._metrics = init.metrics
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 (): Promise<void> {
68
+ private async _invokeStartableMethod (methodName: 'beforeStart' | 'start' | 'afterStart' | 'beforeStop' | 'stop' | 'afterStop'): Promise<void> {
95
69
  await Promise.all(
96
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
97
- if (startable.beforeStart != null) {
98
- await startable.beforeStart()
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 start (): Promise<void> {
105
- await Promise.all(
106
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
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 Promise.all(
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 Promise.all(
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 Promise.all(
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 Promise.all(
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
- set contentRouting (contentRouting: ContentRouting) {
259
- this._contentRouting = contentRouting
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
- get peerRouting (): PeerRouting {
263
- if (this._peerRouting == null) {
264
- throw new CodeError('peerRouting not set', 'ERR_SERVICE_MISSING')
265
- }
135
+ return service
136
+ }
266
137
 
267
- return this._peerRouting
268
- }
138
+ return Reflect.get(target, prop, receiver)
139
+ },
269
140
 
270
- set peerRouting (peerRouting: PeerRouting) {
271
- this._peerRouting = peerRouting
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
- get datastore (): Datastore {
275
- if (this._datastore == null) {
276
- throw new CodeError('datastore not set', 'ERR_SERVICE_MISSING')
148
+ return true
277
149
  }
150
+ })
278
151
 
279
- return this._datastore
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 { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
10
+ import type { ServiceMap } from '@libp2p/interface-libp2p'
12
11
 
13
12
  const DefaultConfig: Partial<Libp2pInit> = {
14
13
  addresses: {
@@ -32,7 +31,9 @@ const DefaultConfig: Partial<Libp2pInit> = {
32
31
  interval: 6e5,
33
32
  bootDelay: 10e3
34
33
  }
35
- },
34
+ }
35
+ /*
36
+
36
37
  nat: {
37
38
  enabled: true,
38
39
  ttl: 7200,
@@ -77,10 +78,12 @@ const DefaultConfig: Partial<Libp2pInit> = {
77
78
  startupDelay: 5000,
78
79
  refreshInterval: 60000
79
80
  }
81
+
82
+ */
80
83
  }
81
84
 
82
- export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit {
83
- const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
85
+ export function validateConfig <T extends ServiceMap = {}> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
86
+ const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
84
87
 
85
88
  if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
86
89
  throw new CodeError(messages.ERR_TRANSPORTS_REQUIRED, codes.ERR_TRANSPORTS_REQUIRED)
@@ -94,14 +97,5 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
94
97
  throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED)
95
98
  }
96
99
 
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
100
  return resultingOptions
107
101
  }
@@ -5,6 +5,8 @@ import { PeerMap } 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,12 @@ export class AutoDial implements Startable {
86
97
  }
87
98
 
88
99
  async autoDial (): Promise<void> {
89
- const numConnections = this.connectionManager.getConnections().length
100
+ if (!this.started) {
101
+ return
102
+ }
103
+
104
+ const connections = this.connectionManager.getConnectionsMap()
105
+ const numConnections = connections.size
90
106
 
91
107
  // Already has enough connections
92
108
  if (numConnections >= this.minConnections) {
@@ -106,6 +122,11 @@ export class AutoDial implements Startable {
106
122
  return false
107
123
  }
108
124
 
125
+ // remove peers we are already connected to
126
+ if (connections.has(peer.id)) {
127
+ return false
128
+ }
129
+
109
130
  return true
110
131
  })
111
132
 
@@ -120,10 +141,8 @@ export class AutoDial implements Startable {
120
141
  continue
121
142
  }
122
143
 
123
- const tags = await this.peerStore.getTags(peer.id)
124
-
125
144
  // sum all tag values
126
- peerValues.set(peer.id, (tags ?? []).reduce((acc, curr) => {
145
+ peerValues.set(peer.id, [...peer.tags.values()].reduce((acc, curr) => {
127
146
  return acc + curr.value
128
147
  }, 0))
129
148
  }
@@ -148,7 +167,7 @@ export class AutoDial implements Startable {
148
167
 
149
168
  for (const peer of sortedPeers) {
150
169
  this.queue.add(async () => {
151
- const numConnections = this.connectionManager.getConnections().length
170
+ const numConnections = this.connectionManager.getConnectionsMap().size
152
171
 
153
172
  // Check to see if we still need to auto dial
154
173
  if (numConnections >= this.minConnections) {
@@ -4,7 +4,8 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
4
4
  import { PeerMap } from '@libp2p/peer-collections'
5
5
  import type { Multiaddr } from '@multiformats/multiaddr'
6
6
  import { MAX_CONNECTIONS } from './constants.js'
7
- import { CustomEvent } from '@libp2p/interfaces/events'
7
+ import type { EventEmitter } from '@libp2p/interfaces/events'
8
+ import type { Libp2pEvents } from '@libp2p/interface-libp2p'
8
9
 
9
10
  const log = logger('libp2p:connection-manager:connection-pruner')
10
11
 
@@ -16,6 +17,7 @@ interface ConnectionPrunerInit {
16
17
  interface ConnectionPrunerComponents {
17
18
  connectionManager: ConnectionManager
18
19
  peerStore: PeerStore
20
+ events: EventEmitter<Libp2pEvents>
19
21
  }
20
22
 
21
23
  const defaultOptions = {
@@ -31,12 +33,22 @@ export class ConnectionPruner {
31
33
  private readonly connectionManager: ConnectionManager
32
34
  private readonly peerStore: PeerStore
33
35
  private readonly allow: Multiaddr[]
36
+ private readonly events: EventEmitter<Libp2pEvents>
34
37
 
35
38
  constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) {
36
39
  this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
37
40
  this.allow = init.allow ?? defaultOptions.allow
38
41
  this.connectionManager = components.connectionManager
39
42
  this.peerStore = components.peerStore
43
+ this.events = components.events
44
+
45
+ // check the max connection limit whenever a peer connects
46
+ components.events.addEventListener('connection:open', () => {
47
+ this.maybePruneConnections()
48
+ .catch(err => {
49
+ log.error(err)
50
+ })
51
+ })
40
52
  }
41
53
 
42
54
  /**
@@ -48,7 +60,7 @@ export class ConnectionPruner {
48
60
  const numConnections = connections.length
49
61
  const toPrune = Math.max(numConnections - this.maxConnections, 0)
50
62
 
51
- log.trace('checking max connections limit %d/%d', numConnections, this.maxConnections)
63
+ log('checking max connections limit %d/%d', numConnections, this.maxConnections)
52
64
  if (numConnections <= this.maxConnections) {
53
65
  return
54
66
  }
@@ -64,12 +76,20 @@ export class ConnectionPruner {
64
76
  continue
65
77
  }
66
78
 
67
- const tags = await this.peerStore.getTags(remotePeer)
79
+ peerValues.set(remotePeer, 0)
68
80
 
69
- // sum all tag values
70
- peerValues.set(remotePeer, tags.reduce((acc, curr) => {
71
- return acc + curr.value
72
- }, 0))
81
+ try {
82
+ const peer = await this.peerStore.get(remotePeer)
83
+
84
+ // sum all tag values
85
+ peerValues.set(remotePeer, [...peer.tags.values()].reduce((acc, curr) => {
86
+ return acc + curr.value
87
+ }, 0))
88
+ } catch (err: any) {
89
+ if (err.code !== 'ERR_NOT_FOUND') {
90
+ log.error('error loading peer tags', err)
91
+ }
92
+ }
73
93
  }
74
94
 
75
95
  // sort by value, lowest to highest
@@ -132,6 +152,6 @@ export class ConnectionPruner {
132
152
  )
133
153
 
134
154
  // despatch prune event
135
- this.connectionManager.dispatchEvent(new CustomEvent('peer:prune', { detail: toClose.map(conn => conn.remotePeer) }))
155
+ this.events.safeDispatchEvent('connection:prune', { detail: toClose })
136
156
  }
137
157
  }