libp2p 0.44.0 → 0.45.0-08d4c261

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 (238) hide show
  1. package/dist/index.min.js +32 -32
  2. package/dist/src/address-manager/index.d.ts +9 -5
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +44 -7
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/address-manager/utils.d.ts +2 -0
  7. package/dist/src/address-manager/utils.d.ts.map +1 -0
  8. package/dist/src/address-manager/utils.js +12 -0
  9. package/dist/src/address-manager/utils.js.map +1 -0
  10. package/dist/src/autonat/constants.d.ts +16 -2
  11. package/dist/src/autonat/constants.d.ts.map +1 -1
  12. package/dist/src/autonat/constants.js +16 -2
  13. package/dist/src/autonat/constants.js.map +1 -1
  14. package/dist/src/autonat/index.d.ts +12 -33
  15. package/dist/src/autonat/index.d.ts.map +1 -1
  16. package/dist/src/autonat/index.js +48 -33
  17. package/dist/src/autonat/index.js.map +1 -1
  18. package/dist/src/circuit-relay/index.d.ts +1 -1
  19. package/dist/src/circuit-relay/index.d.ts.map +1 -1
  20. package/dist/src/circuit-relay/server/advert-service.d.ts +2 -2
  21. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  22. package/dist/src/circuit-relay/server/advert-service.js +8 -4
  23. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  24. package/dist/src/circuit-relay/server/index.d.ts +8 -8
  25. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  26. package/dist/src/circuit-relay/server/index.js +32 -12
  27. package/dist/src/circuit-relay/server/index.js.map +1 -1
  28. package/dist/src/circuit-relay/server/reservation-store.d.ts +5 -5
  29. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -1
  30. package/dist/src/circuit-relay/server/reservation-store.js +10 -3
  31. package/dist/src/circuit-relay/server/reservation-store.js.map +1 -1
  32. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +1 -1
  33. package/dist/src/circuit-relay/server/reservation-voucher.js +5 -2
  34. package/dist/src/circuit-relay/server/reservation-voucher.js.map +1 -1
  35. package/dist/src/circuit-relay/transport/discovery.d.ts +4 -4
  36. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  37. package/dist/src/circuit-relay/transport/discovery.js +13 -5
  38. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  39. package/dist/src/circuit-relay/transport/index.d.ts +10 -8
  40. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  41. package/dist/src/circuit-relay/transport/index.js +26 -18
  42. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  43. package/dist/src/circuit-relay/transport/listener.d.ts +1 -1
  44. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  45. package/dist/src/circuit-relay/transport/listener.js +43 -43
  46. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  47. package/dist/src/circuit-relay/transport/reservation-store.d.ts +8 -7
  48. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  49. package/dist/src/circuit-relay/transport/reservation-store.js +65 -52
  50. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  51. package/dist/src/circuit-relay/utils.d.ts.map +1 -1
  52. package/dist/src/circuit-relay/utils.js +3 -3
  53. package/dist/src/circuit-relay/utils.js.map +1 -1
  54. package/dist/src/components.d.ts +10 -67
  55. package/dist/src/components.d.ts.map +1 -1
  56. package/dist/src/components.js +61 -165
  57. package/dist/src/components.js.map +1 -1
  58. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  59. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  60. package/dist/src/config/connection-gater.browser.js +27 -0
  61. package/dist/src/config/connection-gater.browser.js.map +1 -0
  62. package/dist/src/config/connection-gater.d.ts +6 -0
  63. package/dist/src/config/connection-gater.d.ts.map +1 -0
  64. package/dist/src/config/connection-gater.js +18 -0
  65. package/dist/src/config/connection-gater.js.map +1 -0
  66. package/dist/src/config.d.ts +2 -1
  67. package/dist/src/config.d.ts.map +1 -1
  68. package/dist/src/config.js +4 -66
  69. package/dist/src/config.js.map +1 -1
  70. package/dist/src/connection/index.d.ts +3 -3
  71. package/dist/src/connection/index.d.ts.map +1 -1
  72. package/dist/src/connection/index.js +38 -8
  73. package/dist/src/connection/index.js.map +1 -1
  74. package/dist/src/connection-manager/auto-dial.d.ts +4 -1
  75. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  76. package/dist/src/connection-manager/auto-dial.js +44 -13
  77. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  78. package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
  79. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  80. package/dist/src/connection-manager/connection-pruner.js +28 -8
  81. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  82. package/dist/src/connection-manager/dial-queue.d.ts +7 -6
  83. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  84. package/dist/src/connection-manager/dial-queue.js +59 -23
  85. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  86. package/dist/src/connection-manager/index.d.ts +16 -15
  87. package/dist/src/connection-manager/index.d.ts.map +1 -1
  88. package/dist/src/connection-manager/index.js +44 -41
  89. package/dist/src/connection-manager/index.js.map +1 -1
  90. package/dist/src/connection-manager/utils.d.ts +2 -3
  91. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  92. package/dist/src/connection-manager/utils.js +3 -3
  93. package/dist/src/connection-manager/utils.js.map +1 -1
  94. package/dist/src/content-routing/index.d.ts +2 -12
  95. package/dist/src/content-routing/index.d.ts.map +1 -1
  96. package/dist/src/content-routing/index.js +12 -44
  97. package/dist/src/content-routing/index.js.map +1 -1
  98. package/dist/src/content-routing/utils.d.ts +1 -1
  99. package/dist/src/content-routing/utils.d.ts.map +1 -1
  100. package/dist/src/content-routing/utils.js +3 -1
  101. package/dist/src/content-routing/utils.js.map +1 -1
  102. package/dist/src/fetch/index.d.ts +15 -37
  103. package/dist/src/fetch/index.d.ts.map +1 -1
  104. package/dist/src/fetch/index.js +19 -16
  105. package/dist/src/fetch/index.js.map +1 -1
  106. package/dist/src/get-peer.d.ts +1 -1
  107. package/dist/src/get-peer.d.ts.map +1 -1
  108. package/dist/src/get-peer.js +3 -3
  109. package/dist/src/get-peer.js.map +1 -1
  110. package/dist/src/identify/identify.d.ts +53 -0
  111. package/dist/src/identify/identify.d.ts.map +1 -0
  112. package/dist/src/identify/identify.js +417 -0
  113. package/dist/src/identify/identify.js.map +1 -0
  114. package/dist/src/identify/index.d.ts +14 -59
  115. package/dist/src/identify/index.d.ts.map +1 -1
  116. package/dist/src/identify/index.js +5 -388
  117. package/dist/src/identify/index.js.map +1 -1
  118. package/dist/src/index.d.ts +28 -79
  119. package/dist/src/index.d.ts.map +1 -1
  120. package/dist/src/index.js +2 -1
  121. package/dist/src/index.js.map +1 -1
  122. package/dist/src/insecure/index.js +6 -8
  123. package/dist/src/insecure/index.js.map +1 -1
  124. package/dist/src/libp2p.d.ts +16 -47
  125. package/dist/src/libp2p.d.ts.map +1 -1
  126. package/dist/src/libp2p.js +141 -201
  127. package/dist/src/libp2p.js.map +1 -1
  128. package/dist/src/peer-routing.d.ts +3 -40
  129. package/dist/src/peer-routing.d.ts.map +1 -1
  130. package/dist/src/peer-routing.js +8 -65
  131. package/dist/src/peer-routing.js.map +1 -1
  132. package/dist/src/ping/constants.d.ts +4 -0
  133. package/dist/src/ping/constants.d.ts.map +1 -1
  134. package/dist/src/ping/constants.js +10 -0
  135. package/dist/src/ping/constants.js.map +1 -1
  136. package/dist/src/ping/index.d.ts +11 -28
  137. package/dist/src/ping/index.d.ts.map +1 -1
  138. package/dist/src/ping/index.js +31 -28
  139. package/dist/src/ping/index.js.map +1 -1
  140. package/dist/src/pnet/crypto.js +3 -3
  141. package/dist/src/pnet/crypto.js.map +1 -1
  142. package/dist/src/pnet/index.d.ts.map +1 -1
  143. package/dist/src/pnet/index.js +9 -6
  144. package/dist/src/pnet/index.js.map +1 -1
  145. package/dist/src/pnet/key-generator.js +1 -1
  146. package/dist/src/pnet/key-generator.js.map +1 -1
  147. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  148. package/dist/src/pubsub/dummy-pubsub.js +2 -5
  149. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  150. package/dist/src/registrar.d.ts +6 -4
  151. package/dist/src/registrar.d.ts.map +1 -1
  152. package/dist/src/registrar.js +33 -28
  153. package/dist/src/registrar.js.map +1 -1
  154. package/dist/src/transport-manager.d.ts +14 -7
  155. package/dist/src/transport-manager.d.ts.map +1 -1
  156. package/dist/src/transport-manager.js +27 -13
  157. package/dist/src/transport-manager.js.map +1 -1
  158. package/dist/src/upgrader.d.ts +20 -15
  159. package/dist/src/upgrader.d.ts.map +1 -1
  160. package/dist/src/upgrader.js +49 -53
  161. package/dist/src/upgrader.js.map +1 -1
  162. package/dist/src/{nat-manager.d.ts → upnp-nat/index.d.ts} +12 -15
  163. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  164. package/dist/src/{nat-manager.js → upnp-nat/index.js} +23 -10
  165. package/dist/src/upnp-nat/index.js.map +1 -0
  166. package/dist/src/version.d.ts +1 -1
  167. package/dist/src/version.d.ts.map +1 -1
  168. package/dist/src/version.js +1 -1
  169. package/dist/src/version.js.map +1 -1
  170. package/package.json +62 -47
  171. package/src/address-manager/index.ts +52 -10
  172. package/src/address-manager/utils.ts +14 -0
  173. package/src/autonat/constants.ts +17 -2
  174. package/src/autonat/index.ts +65 -57
  175. package/src/circuit-relay/index.ts +1 -1
  176. package/src/circuit-relay/server/advert-service.ts +5 -5
  177. package/src/circuit-relay/server/index.ts +28 -20
  178. package/src/circuit-relay/server/reservation-store.ts +5 -5
  179. package/src/circuit-relay/server/reservation-voucher.ts +1 -1
  180. package/src/circuit-relay/transport/discovery.ts +12 -10
  181. package/src/circuit-relay/transport/index.ts +26 -26
  182. package/src/circuit-relay/transport/listener.ts +43 -33
  183. package/src/circuit-relay/transport/reservation-store.ts +42 -28
  184. package/src/circuit-relay/utils.ts +6 -6
  185. package/src/components.ts +75 -238
  186. package/src/config/connection-gater.browser.ts +31 -0
  187. package/src/config/connection-gater.ts +19 -0
  188. package/src/config.ts +9 -70
  189. package/src/connection/index.ts +6 -10
  190. package/src/connection-manager/auto-dial.ts +46 -17
  191. package/src/connection-manager/connection-pruner.ts +30 -10
  192. package/src/connection-manager/dial-queue.ts +56 -35
  193. package/src/connection-manager/index.ts +45 -57
  194. package/src/connection-manager/utils.ts +4 -5
  195. package/src/content-routing/index.ts +11 -59
  196. package/src/content-routing/utils.ts +4 -2
  197. package/src/fetch/index.ts +63 -26
  198. package/src/get-peer.ts +4 -4
  199. package/src/identify/identify.ts +519 -0
  200. package/src/identify/index.ts +19 -509
  201. package/src/index.ts +25 -88
  202. package/src/insecure/index.ts +10 -10
  203. package/src/libp2p.ts +158 -279
  204. package/src/peer-routing.ts +10 -110
  205. package/src/ping/constants.ts +11 -0
  206. package/src/ping/index.ts +43 -39
  207. package/src/pnet/crypto.ts +3 -3
  208. package/src/pnet/index.ts +6 -6
  209. package/src/pnet/key-generator.ts +1 -1
  210. package/src/pubsub/dummy-pubsub.ts +2 -2
  211. package/src/registrar.ts +34 -31
  212. package/src/transport-manager.ts +33 -19
  213. package/src/upgrader.ts +75 -71
  214. package/src/{nat-manager.ts → upnp-nat/index.ts} +23 -24
  215. package/src/version.ts +1 -1
  216. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  217. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  218. package/dist/src/dht/dht-content-routing.js +0 -32
  219. package/dist/src/dht/dht-content-routing.js.map +0 -1
  220. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  221. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  222. package/dist/src/dht/dht-peer-routing.js +0 -26
  223. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  224. package/dist/src/dht/dummy-dht.d.ts +0 -20
  225. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  226. package/dist/src/dht/dummy-dht.js +0 -46
  227. package/dist/src/dht/dummy-dht.js.map +0 -1
  228. package/dist/src/nat-manager.d.ts.map +0 -1
  229. package/dist/src/nat-manager.js.map +0 -1
  230. package/dist/src/peer-record-updater.d.ts +0 -24
  231. package/dist/src/peer-record-updater.d.ts.map +0 -1
  232. package/dist/src/peer-record-updater.js +0 -44
  233. package/dist/src/peer-record-updater.js.map +0 -1
  234. package/dist/typedoc-urls.json +0 -41
  235. package/src/dht/dht-content-routing.ts +0 -44
  236. package/src/dht/dht-peer-routing.ts +0 -36
  237. package/src/dht/dummy-dht.ts +0 -60
  238. package/src/peer-record-updater.ts +0 -65
package/src/components.ts CHANGED
@@ -1,22 +1,23 @@
1
1
  import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { isStartable, type Startable } from '@libp2p/interfaces/startable'
3
+ import type { AddressManager } from '@libp2p/interface-address-manager'
2
4
  import type { ConnectionProtector } from '@libp2p/interface-connection'
5
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
6
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
3
7
  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'
8
+ import type { Libp2pEvents } from '@libp2p/interface-libp2p'
6
9
  import type { Metrics } from '@libp2p/interface-metrics'
7
10
  import type { PeerId } from '@libp2p/interface-peer-id'
8
11
  import type { PeerRouting } from '@libp2p/interface-peer-routing'
9
12
  import type { PeerStore } from '@libp2p/interface-peer-store'
10
13
  import type { Registrar } from '@libp2p/interface-registrar'
11
14
  import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
15
+ import type { EventEmitter } from '@libp2p/interfaces/events'
12
16
  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 } from '@libp2p/interface-connection-manager'
16
- import type { ConnectionGater } from '@libp2p/interface-connection-gater'
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 isPrivate from 'private-ip'
2
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
3
+ import type { Multiaddr } from '@multiformats/multiaddr'
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,14 +1,13 @@
1
- import mergeOptions from 'merge-options'
2
- import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
3
- import { AGENT_VERSION } from './identify/consts.js'
4
- import { publicAddressesFirst } from '@libp2p/utils/address-sort'
5
1
  import { FaultTolerance } from '@libp2p/interface-transport'
6
- import type { Multiaddr } from '@multiformats/multiaddr'
7
- import type { Libp2pInit } from './index.js'
8
- import { codes, messages } from './errors.js'
9
2
  import { CodeError } from '@libp2p/interfaces/errors'
3
+ import { publicAddressesFirst } from '@libp2p/utils/address-sort'
4
+ import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
5
+ import mergeOptions from 'merge-options'
6
+ import { codes, messages } from './errors.js'
7
+ import type { Libp2pInit } from './index.js'
8
+ import type { ServiceMap } from '@libp2p/interface-libp2p'
10
9
  import type { RecursivePartial } from '@libp2p/interfaces'
11
- import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
10
+ import type { Multiaddr } from '@multiformats/multiaddr'
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>): Libp2pInit {
83
- const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
30
+ export function validateConfig <T extends ServiceMap = Record<string, unknown>> (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,11 +1,11 @@
1
- import type { Multiaddr } from '@multiformats/multiaddr'
2
- import { CodeError } from '@libp2p/interfaces/errors'
3
- import { OPEN, CLOSING, CLOSED } from '@libp2p/interface-connection/status'
4
1
  import { symbol } from '@libp2p/interface-connection'
2
+ import { OPEN, CLOSING, CLOSED } from '@libp2p/interface-connection/status'
3
+ import { CodeError } from '@libp2p/interfaces/errors'
4
+ import { logger } from '@libp2p/logger'
5
5
  import type { Connection, ConnectionStat, Stream } from '@libp2p/interface-connection'
6
6
  import type { PeerId } from '@libp2p/interface-peer-id'
7
- import { logger } from '@libp2p/logger'
8
7
  import type { AbortOptions } from '@libp2p/interfaces'
8
+ import type { Multiaddr } from '@multiformats/multiaddr'
9
9
 
10
10
  const log = logger('libp2p:connection')
11
11
 
@@ -87,13 +87,9 @@ export class ConnectionImpl implements Connection {
87
87
  this._closing = false
88
88
  }
89
89
 
90
- get [Symbol.toStringTag] (): 'Connection' {
91
- return 'Connection'
92
- }
90
+ readonly [Symbol.toStringTag] = 'Connection'
93
91
 
94
- get [symbol] (): true {
95
- return true
96
- }
92
+ readonly [symbol] = true
97
93
 
98
94
  /**
99
95
  * Get all the streams of the muxer