libp2p 0.46.21 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/README.md +51 -47
  2. package/dist/index.min.js +40 -28
  3. package/dist/src/address-manager/index.d.ts +4 -5
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +3 -3
  6. package/dist/src/address-manager/index.js.map +1 -1
  7. package/dist/src/components.d.ts +4 -15
  8. package/dist/src/components.d.ts.map +1 -1
  9. package/dist/src/components.js +2 -2
  10. package/dist/src/components.js.map +1 -1
  11. package/dist/src/config/connection-gater.browser.d.ts +1 -1
  12. package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
  13. package/dist/src/config/connection-gater.browser.js.map +1 -1
  14. package/dist/src/config/connection-gater.d.ts +1 -1
  15. package/dist/src/config/connection-gater.d.ts.map +1 -1
  16. package/dist/src/config.d.ts.map +1 -1
  17. package/dist/src/config.js +1 -2
  18. package/dist/src/config.js.map +1 -1
  19. package/dist/src/connection/index.d.ts +4 -6
  20. package/dist/src/connection/index.d.ts.map +1 -1
  21. package/dist/src/connection/index.js +11 -13
  22. package/dist/src/connection/index.js.map +1 -1
  23. package/dist/src/connection-manager/auto-dial.d.ts +3 -6
  24. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  25. package/dist/src/connection-manager/auto-dial.js +21 -21
  26. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  27. package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
  28. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  29. package/dist/src/connection-manager/connection-pruner.js +8 -8
  30. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  31. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  32. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  33. package/dist/src/connection-manager/constants.defaults.js +0 -4
  34. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  35. package/dist/src/connection-manager/dial-queue.d.ts +3 -10
  36. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  37. package/dist/src/connection-manager/dial-queue.js +30 -38
  38. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  39. package/dist/src/connection-manager/index.d.ts +3 -18
  40. package/dist/src/connection-manager/index.d.ts.map +1 -1
  41. package/dist/src/connection-manager/index.js +17 -18
  42. package/dist/src/connection-manager/index.js.map +1 -1
  43. package/dist/src/connection-manager/utils.js +1 -1
  44. package/dist/src/connection-manager/utils.js.map +1 -1
  45. package/dist/src/content-routing/index.d.ts +1 -5
  46. package/dist/src/content-routing/index.d.ts.map +1 -1
  47. package/dist/src/content-routing/index.js +2 -2
  48. package/dist/src/content-routing/index.js.map +1 -1
  49. package/dist/src/content-routing/utils.d.ts +1 -2
  50. package/dist/src/content-routing/utils.d.ts.map +1 -1
  51. package/dist/src/content-routing/utils.js +1 -1
  52. package/dist/src/content-routing/utils.js.map +1 -1
  53. package/dist/src/errors.d.ts +0 -2
  54. package/dist/src/errors.d.ts.map +1 -1
  55. package/dist/src/errors.js +0 -2
  56. package/dist/src/errors.js.map +1 -1
  57. package/dist/src/get-peer.d.ts +1 -1
  58. package/dist/src/get-peer.d.ts.map +1 -1
  59. package/dist/src/get-peer.js +1 -5
  60. package/dist/src/get-peer.js.map +1 -1
  61. package/dist/src/index.d.ts +5 -16
  62. package/dist/src/index.d.ts.map +1 -1
  63. package/dist/src/index.js.map +1 -1
  64. package/dist/src/libp2p.d.ts +4 -12
  65. package/dist/src/libp2p.d.ts.map +1 -1
  66. package/dist/src/libp2p.js +31 -65
  67. package/dist/src/libp2p.js.map +1 -1
  68. package/dist/src/peer-routing.d.ts +6 -6
  69. package/dist/src/peer-routing.d.ts.map +1 -1
  70. package/dist/src/peer-routing.js +12 -9
  71. package/dist/src/peer-routing.js.map +1 -1
  72. package/dist/src/registrar.d.ts +5 -7
  73. package/dist/src/registrar.d.ts.map +1 -1
  74. package/dist/src/registrar.js +4 -4
  75. package/dist/src/registrar.js.map +1 -1
  76. package/dist/src/transport-manager.d.ts +5 -9
  77. package/dist/src/transport-manager.d.ts.map +1 -1
  78. package/dist/src/transport-manager.js +12 -13
  79. package/dist/src/transport-manager.js.map +1 -1
  80. package/dist/src/upgrader.d.ts +8 -19
  81. package/dist/src/upgrader.d.ts.map +1 -1
  82. package/dist/src/upgrader.js +75 -38
  83. package/dist/src/upgrader.js.map +1 -1
  84. package/dist/src/version.d.ts +1 -1
  85. package/dist/src/version.d.ts.map +1 -1
  86. package/dist/src/version.js +1 -1
  87. package/dist/src/version.js.map +1 -1
  88. package/dist/typedoc-urls.json +8 -69
  89. package/package.json +37 -106
  90. package/src/address-manager/index.ts +6 -9
  91. package/src/components.ts +5 -16
  92. package/src/config/connection-gater.browser.ts +1 -1
  93. package/src/config/connection-gater.ts +1 -1
  94. package/src/config.ts +1 -2
  95. package/src/connection/index.ts +12 -17
  96. package/src/connection-manager/auto-dial.ts +23 -26
  97. package/src/connection-manager/connection-pruner.ts +10 -12
  98. package/src/connection-manager/constants.defaults.ts +0 -5
  99. package/src/connection-manager/dial-queue.ts +31 -46
  100. package/src/connection-manager/index.ts +19 -36
  101. package/src/connection-manager/utils.ts +1 -1
  102. package/src/content-routing/index.ts +3 -7
  103. package/src/content-routing/utils.ts +2 -3
  104. package/src/errors.ts +0 -2
  105. package/src/get-peer.ts +2 -7
  106. package/src/index.ts +6 -17
  107. package/src/libp2p.ts +32 -80
  108. package/src/peer-routing.ts +16 -15
  109. package/src/registrar.ts +8 -12
  110. package/src/transport-manager.ts +15 -22
  111. package/src/upgrader.ts +110 -64
  112. package/src/version.ts +1 -1
  113. package/dist/src/autonat/constants.d.ts +0 -18
  114. package/dist/src/autonat/constants.d.ts.map +0 -1
  115. package/dist/src/autonat/constants.js +0 -18
  116. package/dist/src/autonat/constants.js.map +0 -1
  117. package/dist/src/autonat/index.d.ts +0 -62
  118. package/dist/src/autonat/index.d.ts.map +0 -1
  119. package/dist/src/autonat/index.js +0 -447
  120. package/dist/src/autonat/index.js.map +0 -1
  121. package/dist/src/autonat/pb/index.d.ts +0 -57
  122. package/dist/src/autonat/pb/index.d.ts.map +0 -1
  123. package/dist/src/autonat/pb/index.js +0 -250
  124. package/dist/src/autonat/pb/index.js.map +0 -1
  125. package/dist/src/circuit-relay/constants.d.ts +0 -50
  126. package/dist/src/circuit-relay/constants.d.ts.map +0 -1
  127. package/dist/src/circuit-relay/constants.js +0 -56
  128. package/dist/src/circuit-relay/constants.js.map +0 -1
  129. package/dist/src/circuit-relay/index.d.ts +0 -55
  130. package/dist/src/circuit-relay/index.d.ts.map +0 -1
  131. package/dist/src/circuit-relay/index.js +0 -38
  132. package/dist/src/circuit-relay/index.js.map +0 -1
  133. package/dist/src/circuit-relay/pb/index.d.ts +0 -93
  134. package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
  135. package/dist/src/circuit-relay/pb/index.js +0 -425
  136. package/dist/src/circuit-relay/pb/index.js.map +0 -1
  137. package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
  138. package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
  139. package/dist/src/circuit-relay/server/advert-service.js +0 -73
  140. package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
  141. package/dist/src/circuit-relay/server/index.d.ts +0 -65
  142. package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
  143. package/dist/src/circuit-relay/server/index.js +0 -312
  144. package/dist/src/circuit-relay/server/index.js.map +0 -1
  145. package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
  146. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
  147. package/dist/src/circuit-relay/server/reservation-store.js +0 -65
  148. package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
  149. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
  150. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
  151. package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
  152. package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
  153. package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
  154. package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
  155. package/dist/src/circuit-relay/transport/discovery.js +0 -97
  156. package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
  157. package/dist/src/circuit-relay/transport/index.d.ts +0 -57
  158. package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
  159. package/dist/src/circuit-relay/transport/index.js +0 -276
  160. package/dist/src/circuit-relay/transport/index.js.map +0 -1
  161. package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
  162. package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
  163. package/dist/src/circuit-relay/transport/listener.js +0 -66
  164. package/dist/src/circuit-relay/transport/listener.js.map +0 -1
  165. package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
  166. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
  167. package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
  168. package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
  169. package/dist/src/circuit-relay/utils.d.ts +0 -13
  170. package/dist/src/circuit-relay/utils.d.ts.map +0 -1
  171. package/dist/src/circuit-relay/utils.js +0 -108
  172. package/dist/src/circuit-relay/utils.js.map +0 -1
  173. package/dist/src/dcutr/dcutr.d.ts +0 -43
  174. package/dist/src/dcutr/dcutr.d.ts.map +0 -1
  175. package/dist/src/dcutr/dcutr.js +0 -313
  176. package/dist/src/dcutr/dcutr.js.map +0 -1
  177. package/dist/src/dcutr/index.d.ts +0 -92
  178. package/dist/src/dcutr/index.d.ts.map +0 -1
  179. package/dist/src/dcutr/index.js +0 -63
  180. package/dist/src/dcutr/index.js.map +0 -1
  181. package/dist/src/dcutr/pb/message.d.ts +0 -20
  182. package/dist/src/dcutr/pb/message.d.ts.map +0 -1
  183. package/dist/src/dcutr/pb/message.js +0 -77
  184. package/dist/src/dcutr/pb/message.js.map +0 -1
  185. package/dist/src/dcutr/utils.d.ts +0 -8
  186. package/dist/src/dcutr/utils.d.ts.map +0 -1
  187. package/dist/src/dcutr/utils.js +0 -27
  188. package/dist/src/dcutr/utils.js.map +0 -1
  189. package/dist/src/fetch/constants.d.ts +0 -3
  190. package/dist/src/fetch/constants.d.ts.map +0 -1
  191. package/dist/src/fetch/constants.js +0 -4
  192. package/dist/src/fetch/constants.js.map +0 -1
  193. package/dist/src/fetch/index.d.ts +0 -61
  194. package/dist/src/fetch/index.d.ts.map +0 -1
  195. package/dist/src/fetch/index.js +0 -205
  196. package/dist/src/fetch/index.js.map +0 -1
  197. package/dist/src/fetch/pb/proto.d.ts +0 -28
  198. package/dist/src/fetch/pb/proto.d.ts.map +0 -1
  199. package/dist/src/fetch/pb/proto.js +0 -120
  200. package/dist/src/fetch/pb/proto.js.map +0 -1
  201. package/dist/src/identify/consts.d.ts +0 -10
  202. package/dist/src/identify/consts.d.ts.map +0 -1
  203. package/dist/src/identify/consts.js +0 -11
  204. package/dist/src/identify/consts.js.map +0 -1
  205. package/dist/src/identify/identify.d.ts +0 -54
  206. package/dist/src/identify/identify.d.ts.map +0 -1
  207. package/dist/src/identify/identify.js +0 -447
  208. package/dist/src/identify/identify.js.map +0 -1
  209. package/dist/src/identify/index.d.ts +0 -72
  210. package/dist/src/identify/index.d.ts.map +0 -1
  211. package/dist/src/identify/index.js +0 -15
  212. package/dist/src/identify/index.js.map +0 -1
  213. package/dist/src/identify/pb/message.d.ts +0 -17
  214. package/dist/src/identify/pb/message.d.ts.map +0 -1
  215. package/dist/src/identify/pb/message.js +0 -98
  216. package/dist/src/identify/pb/message.js.map +0 -1
  217. package/dist/src/insecure/index.d.ts +0 -26
  218. package/dist/src/insecure/index.d.ts.map +0 -1
  219. package/dist/src/insecure/index.js +0 -110
  220. package/dist/src/insecure/index.js.map +0 -1
  221. package/dist/src/insecure/pb/proto.d.ts +0 -30
  222. package/dist/src/insecure/pb/proto.d.ts.map +0 -1
  223. package/dist/src/insecure/pb/proto.js +0 -127
  224. package/dist/src/insecure/pb/proto.js.map +0 -1
  225. package/dist/src/ping/constants.d.ts +0 -9
  226. package/dist/src/ping/constants.d.ts.map +0 -1
  227. package/dist/src/ping/constants.js +0 -15
  228. package/dist/src/ping/constants.js.map +0 -1
  229. package/dist/src/ping/index.d.ts +0 -24
  230. package/dist/src/ping/index.d.ts.map +0 -1
  231. package/dist/src/ping/index.js +0 -115
  232. package/dist/src/ping/index.js.map +0 -1
  233. package/dist/src/pnet/crypto.d.ts +0 -18
  234. package/dist/src/pnet/crypto.d.ts.map +0 -1
  235. package/dist/src/pnet/crypto.js +0 -60
  236. package/dist/src/pnet/crypto.js.map +0 -1
  237. package/dist/src/pnet/errors.d.ts +0 -6
  238. package/dist/src/pnet/errors.d.ts.map +0 -1
  239. package/dist/src/pnet/errors.js +0 -6
  240. package/dist/src/pnet/errors.js.map +0 -1
  241. package/dist/src/pnet/index.d.ts +0 -31
  242. package/dist/src/pnet/index.d.ts.map +0 -1
  243. package/dist/src/pnet/index.js +0 -97
  244. package/dist/src/pnet/index.js.map +0 -1
  245. package/dist/src/pnet/key-generator.d.ts +0 -11
  246. package/dist/src/pnet/key-generator.d.ts.map +0 -1
  247. package/dist/src/pnet/key-generator.js +0 -22
  248. package/dist/src/pnet/key-generator.js.map +0 -1
  249. package/dist/src/upnp-nat/index.d.ts +0 -71
  250. package/dist/src/upnp-nat/index.d.ts.map +0 -1
  251. package/dist/src/upnp-nat/index.js +0 -136
  252. package/dist/src/upnp-nat/index.js.map +0 -1
  253. package/dist/src/utils/peer-job-queue.d.ts +0 -33
  254. package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
  255. package/dist/src/utils/peer-job-queue.js +0 -82
  256. package/dist/src/utils/peer-job-queue.js.map +0 -1
  257. package/src/autonat/constants.ts +0 -19
  258. package/src/autonat/index.ts +0 -597
  259. package/src/autonat/pb/index.proto +0 -35
  260. package/src/autonat/pb/index.ts +0 -320
  261. package/src/circuit-relay/constants.ts +0 -72
  262. package/src/circuit-relay/index.ts +0 -59
  263. package/src/circuit-relay/pb/index.proto +0 -67
  264. package/src/circuit-relay/pb/index.ts +0 -539
  265. package/src/circuit-relay/server/advert-service.ts +0 -108
  266. package/src/circuit-relay/server/index.ts +0 -443
  267. package/src/circuit-relay/server/reservation-store.ts +0 -116
  268. package/src/circuit-relay/server/reservation-voucher.ts +0 -51
  269. package/src/circuit-relay/transport/discovery.ts +0 -136
  270. package/src/circuit-relay/transport/index.ts +0 -396
  271. package/src/circuit-relay/transport/listener.ts +0 -97
  272. package/src/circuit-relay/transport/reservation-store.ts +0 -312
  273. package/src/circuit-relay/utils.ts +0 -136
  274. package/src/dcutr/dcutr.ts +0 -378
  275. package/src/dcutr/index.ts +0 -102
  276. package/src/dcutr/pb/message.proto +0 -12
  277. package/src/dcutr/pb/message.ts +0 -96
  278. package/src/dcutr/utils.ts +0 -33
  279. package/src/fetch/README.md +0 -41
  280. package/src/fetch/constants.ts +0 -3
  281. package/src/fetch/index.ts +0 -315
  282. package/src/fetch/pb/proto.proto +0 -15
  283. package/src/fetch/pb/proto.ts +0 -153
  284. package/src/identify/README.md +0 -13
  285. package/src/identify/consts.ts +0 -12
  286. package/src/identify/identify.ts +0 -561
  287. package/src/identify/index.ts +0 -90
  288. package/src/identify/pb/message.proto +0 -30
  289. package/src/identify/pb/message.ts +0 -126
  290. package/src/insecure/index.ts +0 -138
  291. package/src/insecure/pb/proto.proto +0 -18
  292. package/src/insecure/pb/proto.ts +0 -161
  293. package/src/ping/constants.ts +0 -15
  294. package/src/ping/index.ts +0 -170
  295. package/src/pnet/README.md +0 -68
  296. package/src/pnet/crypto.ts +0 -67
  297. package/src/pnet/errors.ts +0 -5
  298. package/src/pnet/index.ts +0 -126
  299. package/src/pnet/key-generator.ts +0 -23
  300. package/src/upnp-nat/index.ts +0 -212
  301. package/src/utils/peer-job-queue.ts +0 -119
@@ -1,4 +1,4 @@
1
- import { CodeError } from '@libp2p/interface/errors'
1
+ import { CodeError } from '@libp2p/interface'
2
2
  import { KEEP_ALIVE } from '@libp2p/interface/peer-store/tags'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
4
  import { defaultAddressSort } from '@libp2p/utils/address-sort'
@@ -9,18 +9,10 @@ import { codes } from '../errors.js'
9
9
  import { getPeerAddress } from '../get-peer.js'
10
10
  import { AutoDial } from './auto-dial.js'
11
11
  import { ConnectionPruner } from './connection-pruner.js'
12
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
12
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
13
13
  import { DialQueue } from './dial-queue.js'
14
- import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
15
- import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection'
16
- import type { ConnectionGater } from '@libp2p/interface/connection-gater'
17
- import type { TypedEventTarget } from '@libp2p/interface/events'
18
- import type { Metrics } from '@libp2p/interface/metrics'
19
- import type { PeerId } from '@libp2p/interface/peer-id'
20
- import type { Peer, PeerStore } from '@libp2p/interface/peer-store'
21
- import type { Startable } from '@libp2p/interface/startable'
22
- import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
23
- import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
14
+ import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions, ComponentLogger, Logger, Connection, MultiaddrConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, Peer, PeerStore, Startable } from '@libp2p/interface'
15
+ import type { ConnectionManager, OpenConnectionOptions, TransportManager } from '@libp2p/interface-internal'
24
16
 
25
17
  const DEFAULT_DIAL_PRIORITY = 50
26
18
 
@@ -89,14 +81,6 @@ export interface ConnectionManagerInit {
89
81
  */
90
82
  maxParallelDials?: number
91
83
 
92
- /**
93
- * To prevent individual peers with large amounts of multiaddrs swamping the
94
- * dial queue, this value controls how many addresses to dial in parallel per
95
- * peer. So for example if two peers have 10 addresses and this value is set
96
- * at 5, we will dial 5 addresses from each at a time. (default: 1)
97
- */
98
- maxParallelDialsPerPeer?: number
99
-
100
84
  /**
101
85
  * Maximum number of addresses allowed for a given peer - if a peer has more
102
86
  * addresses than this then the dial will fail. (default: 25)
@@ -186,7 +170,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
186
170
  private readonly peerStore: PeerStore
187
171
  private readonly metrics?: Metrics
188
172
  private readonly events: TypedEventTarget<Libp2pEvents>
189
- readonly #log: Logger
173
+ private readonly log: Logger
190
174
 
191
175
  constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit = {}) {
192
176
  this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
@@ -205,7 +189,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
205
189
  this.peerStore = components.peerStore
206
190
  this.metrics = components.metrics
207
191
  this.events = components.events
208
- this.#log = components.logger.forComponent('libp2p:connection-manager')
192
+ this.log = components.logger.forComponent('libp2p:connection-manager')
209
193
 
210
194
  this.onConnect = this.onConnect.bind(this)
211
195
  this.onDisconnect = this.onDisconnect.bind(this)
@@ -260,7 +244,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
260
244
  addressSorter: init.addressSorter ?? defaultAddressSort,
261
245
  maxParallelDials: init.maxParallelDials ?? MAX_PARALLEL_DIALS,
262
246
  maxPeerAddrsToDial: init.maxPeerAddrsToDial ?? MAX_PEER_ADDRS_TO_DIAL,
263
- maxParallelDialsPerPeer: init.maxParallelDialsPerPeer ?? MAX_PARALLEL_DIALS_PER_PEER,
264
247
  dialTimeout: init.dialTimeout ?? DIAL_TIMEOUT,
265
248
  resolvers: init.resolvers ?? {
266
249
  dnsaddr: dnsaddrResolver
@@ -359,7 +342,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
359
342
  this.autoDial.start()
360
343
 
361
344
  this.started = true
362
- this.#log('started')
345
+ this.log('started')
363
346
  }
364
347
 
365
348
  async afterStart (): Promise<void> {
@@ -376,13 +359,13 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
376
359
  keepAlivePeers.map(async peer => {
377
360
  await this.openConnection(peer.id)
378
361
  .catch(err => {
379
- this.#log.error(err)
362
+ this.log.error(err)
380
363
  })
381
364
  })
382
365
  )
383
366
  })
384
367
  .catch(err => {
385
- this.#log.error(err)
368
+ this.log.error(err)
386
369
  })
387
370
 
388
371
  this.autoDial.afterStart()
@@ -403,22 +386,22 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
403
386
  try {
404
387
  await connection.close()
405
388
  } catch (err) {
406
- this.#log.error(err)
389
+ this.log.error(err)
407
390
  }
408
391
  })())
409
392
  }
410
393
  }
411
394
 
412
- this.#log('closing %d connections', tasks.length)
395
+ this.log('closing %d connections', tasks.length)
413
396
  await Promise.all(tasks)
414
397
  this.connections.clear()
415
398
 
416
- this.#log('stopped')
399
+ this.log('stopped')
417
400
  }
418
401
 
419
402
  onConnect (evt: CustomEvent<Connection>): void {
420
403
  void this._onConnect(evt).catch(err => {
421
- this.#log.error(err)
404
+ this.log.error(err)
422
405
  })
423
406
  }
424
407
 
@@ -508,12 +491,12 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
508
491
  const { peerId } = getPeerAddress(peerIdOrMultiaddr)
509
492
 
510
493
  if (peerId != null && options.force !== true) {
511
- this.#log('dial %p', peerId)
494
+ this.log('dial %p', peerId)
512
495
  const existingConnection = this.getConnections(peerId)
513
496
  .find(conn => !conn.transient)
514
497
 
515
498
  if (existingConnection != null) {
516
- this.#log('had an existing non-transient connection to %p', peerId)
499
+ this.log('had an existing non-transient connection to %p', peerId)
517
500
 
518
501
  return existingConnection
519
502
  }
@@ -569,7 +552,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
569
552
  })
570
553
 
571
554
  if (denyConnection) {
572
- this.#log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
555
+ this.log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
573
556
  return false
574
557
  }
575
558
 
@@ -586,7 +569,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
586
569
 
587
570
  // check pending connections
588
571
  if (this.incomingPendingConnections === this.maxIncomingPendingConnections) {
589
- this.#log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
572
+ this.log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
590
573
  return false
591
574
  }
592
575
 
@@ -596,7 +579,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
596
579
  try {
597
580
  await this.inboundConnectionRateLimiter.consume(host, 1)
598
581
  } catch {
599
- this.#log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
582
+ this.log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
600
583
  return false
601
584
  }
602
585
  }
@@ -607,7 +590,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
607
590
  return true
608
591
  }
609
592
 
610
- this.#log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
593
+ this.log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
611
594
  return false
612
595
  }
613
596
 
@@ -1,4 +1,4 @@
1
- import { setMaxListeners } from '@libp2p/interface/events'
1
+ import { setMaxListeners } from '@libp2p/interface'
2
2
  import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/multiaddr'
3
3
  import { type ClearableSignal, anySignal } from 'any-signal'
4
4
  import type { LoggerOptions } from '@libp2p/interface'
@@ -1,17 +1,13 @@
1
- import { CodeError } from '@libp2p/interface/errors'
1
+ import { CodeError } from '@libp2p/interface'
2
2
  import merge from 'it-merge'
3
3
  import { pipe } from 'it-pipe'
4
- import { messages, codes } from '../errors.js'
4
+ import { codes, messages } from '../errors.js'
5
5
  import {
6
6
  storeAddresses,
7
7
  uniquePeers,
8
8
  requirePeers
9
9
  } from './utils.js'
10
- import type { AbortOptions } from '@libp2p/interface'
11
- import type { ContentRouting } from '@libp2p/interface/content-routing'
12
- import type { PeerInfo } from '@libp2p/interface/peer-info'
13
- import type { PeerStore } from '@libp2p/interface/peer-store'
14
- import type { Startable } from '@libp2p/interface/startable'
10
+ import type { AbortOptions, ContentRouting, PeerInfo, PeerStore, Startable } from '@libp2p/interface'
15
11
  import type { CID } from 'multiformats/cid'
16
12
 
17
13
  export interface CompoundContentRoutingInit {
@@ -1,8 +1,7 @@
1
- import { CodeError } from '@libp2p/interface/errors'
1
+ import { CodeError } from '@libp2p/interface'
2
2
  import filter from 'it-filter'
3
3
  import map from 'it-map'
4
- import type { PeerInfo } from '@libp2p/interface/peer-info'
5
- import type { PeerStore } from '@libp2p/interface/peer-store'
4
+ import type { PeerInfo, PeerStore } from '@libp2p/interface'
6
5
  import type { Source } from 'it-stream-types'
7
6
 
8
7
  /**
package/src/errors.ts CHANGED
@@ -37,7 +37,6 @@ export enum codes {
37
37
  ERR_INVALID_PEER = 'ERR_INVALID_PEER',
38
38
  ERR_MUXER_UNAVAILABLE = 'ERR_MUXER_UNAVAILABLE',
39
39
  ERR_NOT_FOUND = 'ERR_NOT_FOUND',
40
- ERR_TIMEOUT = 'ERR_TIMEOUT',
41
40
  ERR_TRANSPORT_UNAVAILABLE = 'ERR_TRANSPORT_UNAVAILABLE',
42
41
  ERR_TRANSPORT_DIAL_FAILED = 'ERR_TRANSPORT_DIAL_FAILED',
43
42
  ERR_UNSUPPORTED_PROTOCOL = 'ERR_UNSUPPORTED_PROTOCOL',
@@ -48,7 +47,6 @@ export enum codes {
48
47
  ERR_NO_ROUTERS_AVAILABLE = 'ERR_NO_ROUTERS_AVAILABLE',
49
48
  ERR_CONNECTION_NOT_MULTIPLEXED = 'ERR_CONNECTION_NOT_MULTIPLEXED',
50
49
  ERR_NO_DIAL_TOKENS = 'ERR_NO_DIAL_TOKENS',
51
- ERR_KEYCHAIN_REQUIRED = 'ERR_KEYCHAIN_REQUIRED',
52
50
  ERR_INVALID_CMS = 'ERR_INVALID_CMS',
53
51
  ERR_MISSING_KEYS = 'ERR_MISSING_KEYS',
54
52
  ERR_NO_KEY = 'ERR_NO_KEY',
package/src/get-peer.ts CHANGED
@@ -1,14 +1,10 @@
1
- import { CodeError } from '@libp2p/interface/errors'
2
- import { isPeerId } from '@libp2p/interface/peer-id'
3
- import { logger } from '@libp2p/logger'
1
+ import { CodeError, isPeerId } from '@libp2p/interface'
4
2
  import { peerIdFromString } from '@libp2p/peer-id'
5
3
  import { isMultiaddr } from '@multiformats/multiaddr'
6
4
  import { codes } from './errors.js'
7
- import type { PeerId } from '@libp2p/interface/peer-id'
5
+ import type { PeerId } from '@libp2p/interface'
8
6
  import type { Multiaddr } from '@multiformats/multiaddr'
9
7
 
10
- const log = logger('libp2p:get-peer')
11
-
12
8
  export interface PeerAddress {
13
9
  peerId?: PeerId
14
10
  multiaddrs: Multiaddr[]
@@ -35,7 +31,6 @@ export function getPeerAddress (peer: PeerId | Multiaddr | Multiaddr[]): PeerAdd
35
31
  // ensure PeerId is either not set or is consistent
36
32
  peer.forEach(ma => {
37
33
  if (!isMultiaddr(ma)) {
38
- log.error('multiaddr %s was invalid', ma)
39
34
  throw new CodeError('Invalid Multiaddr', codes.ERR_INVALID_MULTIADDR)
40
35
  }
41
36
 
package/src/index.ts CHANGED
@@ -19,18 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js'
19
19
  import type { Components } from './components.js'
20
20
  import type { ConnectionManagerInit } from './connection-manager/index.js'
21
21
  import type { TransportManagerInit } from './transport-manager.js'
22
- import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger } from '@libp2p/interface'
23
- import type { ConnectionProtector } from '@libp2p/interface/connection'
24
- import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter'
25
- import type { ConnectionGater } from '@libp2p/interface/connection-gater'
26
- import type { ContentRouting } from '@libp2p/interface/content-routing'
27
- import type { Metrics } from '@libp2p/interface/metrics'
28
- import type { PeerDiscovery } from '@libp2p/interface/peer-discovery'
29
- import type { PeerId } from '@libp2p/interface/peer-id'
30
- import type { PeerRouting } from '@libp2p/interface/peer-routing'
31
- import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
32
- import type { Transport } from '@libp2p/interface/transport'
33
- import type { KeyChainInit } from '@libp2p/keychain'
22
+ import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport } from '@libp2p/interface'
34
23
  import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
35
24
  import type { Datastore } from 'interface-datastore'
36
25
 
@@ -47,6 +36,11 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
47
36
  */
48
37
  peerId: PeerId
49
38
 
39
+ /**
40
+ * Metadata about the node - implementation name, version number, etc
41
+ */
42
+ nodeInfo: NodeInfo
43
+
50
44
  /**
51
45
  * Addresses for transport listening and to advertise to the network
52
46
  */
@@ -79,11 +73,6 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
79
73
  */
80
74
  peerStore: PersistentPeerStoreInit
81
75
 
82
- /**
83
- * keychain configuration
84
- */
85
- keychain: KeyChainInit
86
-
87
76
  /**
88
77
  * An array that must include at least 1 compliant transport
89
78
  */
package/src/libp2p.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  import { unmarshalPublicKey } from '@libp2p/crypto/keys'
2
- import { type ContentRouting, contentRouting } from '@libp2p/interface/content-routing'
3
- import { CodeError } from '@libp2p/interface/errors'
4
- import { TypedEventEmitter, CustomEvent, setMaxListeners } from '@libp2p/interface/events'
5
- import { peerDiscovery } from '@libp2p/interface/peer-discovery'
6
- import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing'
7
- import { DefaultKeyChain } from '@libp2p/keychain'
2
+ import { type ContentRouting, contentRoutingSymbol, type PeerRouting, peerRoutingSymbol, type Libp2pEvents, type PendingDial, type ServiceMap, type AbortOptions, type ComponentLogger, type Logger, type Connection, type NewStreamOptions, type Stream, type Metrics, type PeerId, type PeerInfo, type PeerStore, type Topology } from '@libp2p/interface'
3
+ import { CodeError, TypedEventEmitter, CustomEvent, setMaxListeners, peerDiscoverySymbol } from '@libp2p/interface'
8
4
  import { defaultLogger } from '@libp2p/logger'
9
5
  import { PeerSet } from '@libp2p/peer-collections'
10
6
  import { peerIdFromString } from '@libp2p/peer-id'
@@ -12,7 +8,6 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
12
8
  import { PersistentPeerStore } from '@libp2p/peer-store'
13
9
  import { isMultiaddr, type Multiaddr } from '@multiformats/multiaddr'
14
10
  import { MemoryDatastore } from 'datastore-core/memory'
15
- import mergeOptions from 'merge-options'
16
11
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
17
12
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
18
13
  import { DefaultAddressManager } from './address-manager/index.js'
@@ -26,32 +21,23 @@ import { DefaultPeerRouting } from './peer-routing.js'
26
21
  import { DefaultRegistrar } from './registrar.js'
27
22
  import { DefaultTransportManager } from './transport-manager.js'
28
23
  import { DefaultUpgrader } from './upgrader.js'
24
+ import * as pkg from './version.js'
29
25
  import type { Components } from './components.js'
30
26
  import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
31
- import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
32
- import type { Connection, NewStreamOptions, Stream } from '@libp2p/interface/connection'
33
- import type { KeyChain } from '@libp2p/interface/keychain'
34
- import type { Metrics } from '@libp2p/interface/metrics'
35
- import type { PeerId } from '@libp2p/interface/peer-id'
36
- import type { PeerInfo } from '@libp2p/interface/peer-info'
37
- import type { PeerStore } from '@libp2p/interface/peer-store'
38
- import type { Topology } from '@libp2p/interface/topology'
39
- import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal/registrar'
40
- import type { Datastore } from 'interface-datastore'
27
+ import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal'
41
28
 
42
29
  export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
43
30
  public peerId: PeerId
44
31
  public peerStore: PeerStore
45
32
  public contentRouting: ContentRouting
46
33
  public peerRouting: PeerRouting
47
- public keychain: KeyChain
48
34
  public metrics?: Metrics
49
35
  public services: T
50
36
  public logger: ComponentLogger
51
37
 
52
38
  public components: Components
53
39
  #started: boolean
54
- readonly #log: Logger
40
+ private readonly log: Logger
55
41
 
56
42
  constructor (init: Libp2pInit<T>) {
57
43
  super()
@@ -75,11 +61,15 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
75
61
  this.#started = false
76
62
  this.peerId = init.peerId
77
63
  this.logger = init.logger ?? defaultLogger()
78
- this.#log = this.logger.forComponent('libp2p')
64
+ this.log = this.logger.forComponent('libp2p')
79
65
  // @ts-expect-error {} may not be of type T
80
66
  this.services = {}
81
67
  const components = this.components = defaultComponents({
82
68
  peerId: init.peerId,
69
+ nodeInfo: init.nodeInfo ?? {
70
+ name: pkg.name,
71
+ version: pkg.version
72
+ },
83
73
  logger: this.logger,
84
74
  events,
85
75
  datastore: init.datastore ?? new MemoryDatastore(),
@@ -101,8 +91,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
101
91
  if (evt.detail.previous == null) {
102
92
  const peerInfo: PeerInfo = {
103
93
  id: evt.detail.peer.id,
104
- multiaddrs: evt.detail.peer.addresses.map(a => a.multiaddr),
105
- protocols: evt.detail.peer.protocols
94
+ multiaddrs: evt.detail.peer.addresses.map(a => a.multiaddr)
106
95
  }
107
96
 
108
97
  components.events.safeDispatchEvent('peer:discovery', { detail: peerInfo })
@@ -133,13 +122,6 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
133
122
  // Addresses {listen, announce, noAnnounce}
134
123
  this.configureComponent('addressManager', new DefaultAddressManager(this.components, init.addresses))
135
124
 
136
- // Create keychain
137
- const keychainOpts = DefaultKeyChain.generateOptions()
138
- this.keychain = this.configureComponent('keyChain', new DefaultKeyChain(this.components, {
139
- ...keychainOpts,
140
- ...init.keychain
141
- }))
142
-
143
125
  // Peer routers
144
126
  const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map((fn, index) => this.configureComponent(`peer-router-${index}`, fn(this.components)))
145
127
  this.peerRouting = this.components.peerRouting = this.configureComponent('peerRouting', new DefaultPeerRouting(this.components, {
@@ -173,26 +155,26 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
173
155
  const service: any = createService(this.components)
174
156
 
175
157
  if (service == null) {
176
- this.#log.error('service factory %s returned null or undefined instance', name)
158
+ this.log.error('service factory %s returned null or undefined instance', name)
177
159
  continue
178
160
  }
179
161
 
180
162
  this.services[name as keyof T] = service
181
163
  this.configureComponent(name, service)
182
164
 
183
- if (service[contentRouting] != null) {
184
- this.#log('registering service %s for content routing', name)
185
- contentRouters.push(service[contentRouting])
165
+ if (service[contentRoutingSymbol] != null) {
166
+ this.log('registering service %s for content routing', name)
167
+ contentRouters.push(service[contentRoutingSymbol])
186
168
  }
187
169
 
188
- if (service[peerRouting] != null) {
189
- this.#log('registering service %s for peer routing', name)
190
- peerRouters.push(service[peerRouting])
170
+ if (service[peerRoutingSymbol] != null) {
171
+ this.log('registering service %s for peer routing', name)
172
+ peerRouters.push(service[peerRoutingSymbol])
191
173
  }
192
174
 
193
- if (service[peerDiscovery] != null) {
194
- this.#log('registering service %s for peer discovery', name)
195
- service[peerDiscovery].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
175
+ if (service[peerDiscoverySymbol] != null) {
176
+ this.log('registering service %s for peer discovery', name)
177
+ service[peerDiscoverySymbol].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
196
178
  this.#onDiscoveryPeer(evt)
197
179
  })
198
180
  }
@@ -202,7 +184,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
202
184
 
203
185
  private configureComponent <T> (name: string, component: T): T {
204
186
  if (component == null) {
205
- this.#log.error('component %s was null or undefined', name)
187
+ this.log.error('component %s was null or undefined', name)
206
188
  }
207
189
 
208
190
  this.components[name] = component
@@ -220,14 +202,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
220
202
 
221
203
  this.#started = true
222
204
 
223
- this.#log('libp2p is starting')
224
-
225
- const keys = await this.keychain.listKeys()
226
-
227
- if (keys.find(key => key.name === 'self') == null) {
228
- this.#log('importing self key into keychain')
229
- await this.keychain.importPeer('self', this.components.peerId)
230
- }
205
+ this.log('libp2p is starting')
231
206
 
232
207
  try {
233
208
  await this.components.beforeStart?.()
@@ -235,9 +210,9 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
235
210
  await this.components.afterStart?.()
236
211
 
237
212
  this.safeDispatchEvent('start', { detail: this })
238
- this.#log('libp2p has started')
213
+ this.log('libp2p has started')
239
214
  } catch (err: any) {
240
- this.#log.error('An error occurred starting libp2p', err)
215
+ this.log.error('An error occurred starting libp2p', err)
241
216
  await this.stop()
242
217
  throw err
243
218
  }
@@ -251,7 +226,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
251
226
  return
252
227
  }
253
228
 
254
- this.#log('libp2p is stopping')
229
+ this.log('libp2p is stopping')
255
230
 
256
231
  this.#started = false
257
232
 
@@ -260,7 +235,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
260
235
  await this.components.afterStop?.()
261
236
 
262
237
  this.safeDispatchEvent('stop', { detail: this })
263
- this.#log('libp2p has stopped')
238
+ this.log('libp2p has stopped')
264
239
  }
265
240
 
266
241
  isStarted (): boolean {
@@ -325,7 +300,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
325
300
  * Get the public key for the given peer id
326
301
  */
327
302
  async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
328
- this.#log('getPublicKey %p', peer)
303
+ this.log('getPublicKey %p', peer)
329
304
 
330
305
  if (peer.publicKey != null) {
331
306
  return peer.publicKey
@@ -394,15 +369,14 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
394
369
  const { detail: peer } = evt
395
370
 
396
371
  if (peer.id.toString() === this.peerId.toString()) {
397
- this.#log.error(new Error(codes.ERR_DISCOVERED_SELF))
372
+ this.log.error(new Error(codes.ERR_DISCOVERED_SELF))
398
373
  return
399
374
  }
400
375
 
401
376
  void this.components.peerStore.merge(peer.id, {
402
- multiaddrs: peer.multiaddrs,
403
- protocols: peer.protocols
377
+ multiaddrs: peer.multiaddrs
404
378
  })
405
- .catch(err => { this.#log.error(err) })
379
+ .catch(err => { this.log.error(err) })
406
380
  }
407
381
  }
408
382
 
@@ -411,29 +385,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
411
385
  * libp2p interface and is useful for testing and debugging.
412
386
  */
413
387
  export async function createLibp2pNode <T extends ServiceMap = Record<string, unknown>> (options: Libp2pOptions<T>): Promise<Libp2pNode<T>> {
414
- if (options.peerId == null) {
415
- const datastore = options.datastore as Datastore | undefined
416
-
417
- if (datastore != null) {
418
- try {
419
- // try load the peer id from the keychain
420
- const keyChain = new DefaultKeyChain({
421
- datastore
422
- }, mergeOptions(DefaultKeyChain.generateOptions(), options.keychain))
423
-
424
- options.peerId = await keyChain.exportPeerId('self')
425
- } catch (err: any) {
426
- if (err.code !== 'ERR_NOT_FOUND') {
427
- throw err
428
- }
429
- }
430
- }
431
- }
432
-
433
- if (options.peerId == null) {
434
- // no peer id in the keychain, create a new peer id
435
- options.peerId = await createEd25519PeerId()
436
- }
388
+ options.peerId ??= await createEd25519PeerId()
437
389
 
438
390
  return new Libp2pNode(validateConfig(options))
439
391
  }
@@ -1,5 +1,4 @@
1
- import { CodeError } from '@libp2p/interface/errors'
2
- import { logger } from '@libp2p/logger'
1
+ import { CodeError } from '@libp2p/interface'
3
2
  import filter from 'it-filter'
4
3
  import first from 'it-first'
5
4
  import merge from 'it-merge'
@@ -10,13 +9,8 @@ import {
10
9
  requirePeers
11
10
  } from './content-routing/utils.js'
12
11
  import { codes, messages } from './errors.js'
13
- import type { AbortOptions } from '@libp2p/interface'
14
- import type { PeerId } from '@libp2p/interface/peer-id'
15
- import type { PeerInfo } from '@libp2p/interface/peer-info'
16
- import type { PeerRouting } from '@libp2p/interface/peer-routing'
17
- import type { PeerStore } from '@libp2p/interface/peer-store'
18
-
19
- const log = logger('libp2p:peer-routing')
12
+ import type { AbortOptions, Logger, PeerId, PeerInfo, PeerRouting, PeerStore } from '@libp2p/interface'
13
+ import type { ComponentLogger } from '@libp2p/logger'
20
14
 
21
15
  export interface PeerRoutingInit {
22
16
  routers?: PeerRouting[]
@@ -25,14 +19,19 @@ export interface PeerRoutingInit {
25
19
  export interface DefaultPeerRoutingComponents {
26
20
  peerId: PeerId
27
21
  peerStore: PeerStore
22
+ logger: ComponentLogger
28
23
  }
29
24
 
30
25
  export class DefaultPeerRouting implements PeerRouting {
31
- private readonly components: DefaultPeerRoutingComponents
26
+ private readonly log: Logger
27
+ private readonly peerId: PeerId
28
+ private readonly peerStore: PeerStore
32
29
  private readonly routers: PeerRouting[]
33
30
 
34
31
  constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
35
- this.components = components
32
+ this.log = components.logger.forComponent('libp2p:peer-routing')
33
+ this.peerId = components.peerId
34
+ this.peerStore = components.peerStore
36
35
  this.routers = init.routers ?? []
37
36
  }
38
37
 
@@ -44,22 +43,24 @@ export class DefaultPeerRouting implements PeerRouting {
44
43
  throw new CodeError('No peer routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
45
44
  }
46
45
 
47
- if (id.toString() === this.components.peerId.toString()) {
46
+ if (id.toString() === this.peerId.toString()) {
48
47
  throw new CodeError('Should not try to find self', codes.ERR_FIND_SELF)
49
48
  }
50
49
 
50
+ const self = this
51
+
51
52
  const output = await pipe(
52
53
  merge(
53
54
  ...this.routers.map(router => (async function * () {
54
55
  try {
55
56
  yield await router.findPeer(id, options)
56
57
  } catch (err) {
57
- log.error(err)
58
+ self.log.error(err)
58
59
  }
59
60
  })())
60
61
  ),
61
62
  (source) => filter(source, Boolean),
62
- (source) => storeAddresses(source, this.components.peerStore),
63
+ (source) => storeAddresses(source, this.peerStore),
63
64
  async (source) => first(source)
64
65
  )
65
66
 
@@ -82,7 +83,7 @@ export class DefaultPeerRouting implements PeerRouting {
82
83
  merge(
83
84
  ...this.routers.map(router => router.getClosestPeers(key, options))
84
85
  ),
85
- (source) => storeAddresses(source, this.components.peerStore),
86
+ (source) => storeAddresses(source, this.peerStore),
86
87
  (source) => uniquePeers(source),
87
88
  (source) => requirePeers(source)
88
89
  )