libp2p 0.42.2 → 0.43.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 (295) hide show
  1. package/README.md +4 -4
  2. package/dist/index.min.js +29 -29
  3. package/dist/src/address-manager/index.d.ts +1 -10
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +46 -29
  6. package/dist/src/address-manager/index.js.map +1 -1
  7. package/dist/src/autonat/constants.d.ts +4 -0
  8. package/dist/src/autonat/constants.d.ts.map +1 -0
  9. package/dist/src/autonat/constants.js +4 -0
  10. package/dist/src/autonat/constants.js.map +1 -0
  11. package/dist/src/autonat/index.d.ts +64 -0
  12. package/dist/src/autonat/index.d.ts.map +1 -0
  13. package/dist/src/autonat/index.js +417 -0
  14. package/dist/src/autonat/index.js.map +1 -0
  15. package/dist/src/autonat/pb/index.d.ts +57 -0
  16. package/dist/src/autonat/pb/index.d.ts.map +1 -0
  17. package/dist/src/autonat/pb/index.js +250 -0
  18. package/dist/src/autonat/pb/index.js.map +1 -0
  19. package/dist/src/circuit/constants.d.ts +31 -6
  20. package/dist/src/circuit/constants.d.ts.map +1 -1
  21. package/dist/src/circuit/constants.js +37 -7
  22. package/dist/src/circuit/constants.js.map +1 -1
  23. package/dist/src/circuit/index.d.ts +16 -61
  24. package/dist/src/circuit/index.d.ts.map +1 -1
  25. package/dist/src/circuit/index.js +2 -68
  26. package/dist/src/circuit/index.js.map +1 -1
  27. package/dist/src/circuit/pb/index.d.ts +83 -43
  28. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  29. package/dist/src/circuit/pb/index.js +363 -127
  30. package/dist/src/circuit/pb/index.js.map +1 -1
  31. package/dist/src/circuit/server/advert-service.d.ts +43 -0
  32. package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
  33. package/dist/src/circuit/server/advert-service.js +69 -0
  34. package/dist/src/circuit/server/advert-service.js.map +1 -0
  35. package/dist/src/circuit/server/index.d.ts +60 -0
  36. package/dist/src/circuit/server/index.d.ts.map +1 -0
  37. package/dist/src/circuit/server/index.js +287 -0
  38. package/dist/src/circuit/server/index.js.map +1 -0
  39. package/dist/src/circuit/server/reservation-store.d.ts +49 -0
  40. package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
  41. package/dist/src/circuit/server/reservation-store.js +57 -0
  42. package/dist/src/circuit/server/reservation-store.js.map +1 -0
  43. package/dist/src/circuit/server/reservation-voucher.d.ts +18 -0
  44. package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
  45. package/dist/src/circuit/server/reservation-voucher.js +33 -0
  46. package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
  47. package/dist/src/circuit/transport/discovery.d.ts +45 -0
  48. package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
  49. package/dist/src/circuit/transport/discovery.js +82 -0
  50. package/dist/src/circuit/transport/discovery.js.map +1 -0
  51. package/dist/src/circuit/transport/index.d.ts +33 -0
  52. package/dist/src/circuit/transport/index.d.ts.map +1 -0
  53. package/dist/src/circuit/transport/index.js +225 -0
  54. package/dist/src/circuit/transport/index.js.map +1 -0
  55. package/dist/src/circuit/transport/listener.d.ts +9 -0
  56. package/dist/src/circuit/transport/listener.d.ts.map +1 -0
  57. package/dist/src/circuit/transport/listener.js +76 -0
  58. package/dist/src/circuit/transport/listener.js.map +1 -0
  59. package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
  60. package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
  61. package/dist/src/circuit/transport/reservation-store.js +165 -0
  62. package/dist/src/circuit/transport/reservation-store.js.map +1 -0
  63. package/dist/src/circuit/utils.d.ts +7 -0
  64. package/dist/src/circuit/utils.d.ts.map +1 -1
  65. package/dist/src/circuit/utils.js +87 -0
  66. package/dist/src/circuit/utils.js.map +1 -1
  67. package/dist/src/components.d.ts +2 -1
  68. package/dist/src/components.d.ts.map +1 -1
  69. package/dist/src/components.js +13 -13
  70. package/dist/src/components.js.map +1 -1
  71. package/dist/src/config.d.ts.map +1 -1
  72. package/dist/src/config.js +21 -25
  73. package/dist/src/config.js.map +1 -1
  74. package/dist/src/connection/index.d.ts +2 -2
  75. package/dist/src/connection/index.d.ts.map +1 -1
  76. package/dist/src/connection/index.js +4 -4
  77. package/dist/src/connection/index.js.map +1 -1
  78. package/dist/src/connection-manager/auto-dialler.d.ts +0 -4
  79. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  80. package/dist/src/connection-manager/auto-dialler.js +18 -18
  81. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  82. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  83. package/dist/src/connection-manager/dialer/dial-request.js +13 -6
  84. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
  85. package/dist/src/connection-manager/dialer/index.d.ts +10 -1
  86. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  87. package/dist/src/connection-manager/dialer/index.js +24 -9
  88. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  89. package/dist/src/connection-manager/index.d.ts +1 -4
  90. package/dist/src/connection-manager/index.d.ts.map +1 -1
  91. package/dist/src/connection-manager/index.js +8 -5
  92. package/dist/src/connection-manager/index.js.map +1 -1
  93. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  94. package/dist/src/connection-manager/latency-monitor.js +1 -1
  95. package/dist/src/connection-manager/latency-monitor.js.map +1 -1
  96. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  97. package/dist/src/content-routing/index.d.ts +6 -4
  98. package/dist/src/content-routing/index.d.ts.map +1 -1
  99. package/dist/src/content-routing/index.js +9 -9
  100. package/dist/src/content-routing/index.js.map +1 -1
  101. package/dist/src/content-routing/utils.d.ts +3 -3
  102. package/dist/src/content-routing/utils.d.ts.map +1 -1
  103. package/dist/src/content-routing/utils.js +2 -2
  104. package/dist/src/content-routing/utils.js.map +1 -1
  105. package/dist/src/dht/dht-content-routing.d.ts +2 -1
  106. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  107. package/dist/src/dht/dht-content-routing.js +2 -2
  108. package/dist/src/dht/dht-content-routing.js.map +1 -1
  109. package/dist/src/dht/dht-peer-routing.d.ts +3 -2
  110. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  111. package/dist/src/dht/dht-peer-routing.js +2 -2
  112. package/dist/src/dht/dht-peer-routing.js.map +1 -1
  113. package/dist/src/dht/dummy-dht.d.ts +1 -1
  114. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  115. package/dist/src/dht/dummy-dht.js +12 -12
  116. package/dist/src/dht/dummy-dht.js.map +1 -1
  117. package/dist/src/errors.d.ts +2 -1
  118. package/dist/src/errors.d.ts.map +1 -1
  119. package/dist/src/errors.js +1 -0
  120. package/dist/src/errors.js.map +1 -1
  121. package/dist/src/fetch/index.d.ts.map +1 -1
  122. package/dist/src/fetch/index.js +6 -6
  123. package/dist/src/fetch/index.js.map +1 -1
  124. package/dist/src/fetch/pb/proto.d.ts +3 -3
  125. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  126. package/dist/src/fetch/pb/proto.js +4 -3
  127. package/dist/src/fetch/pb/proto.js.map +1 -1
  128. package/dist/src/get-peer.js +2 -2
  129. package/dist/src/get-peer.js.map +1 -1
  130. package/dist/src/identify/index.d.ts +3 -1
  131. package/dist/src/identify/index.d.ts.map +1 -1
  132. package/dist/src/identify/index.js +19 -14
  133. package/dist/src/identify/index.js.map +1 -1
  134. package/dist/src/identify/pb/message.d.ts +2 -2
  135. package/dist/src/identify/pb/message.d.ts.map +1 -1
  136. package/dist/src/identify/pb/message.js +1 -0
  137. package/dist/src/identify/pb/message.js.map +1 -1
  138. package/dist/src/index.d.ts +14 -8
  139. package/dist/src/index.d.ts.map +1 -1
  140. package/dist/src/index.js.map +1 -1
  141. package/dist/src/insecure/index.d.ts.map +1 -1
  142. package/dist/src/insecure/index.js.map +1 -1
  143. package/dist/src/insecure/pb/proto.d.ts +3 -3
  144. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  145. package/dist/src/insecure/pb/proto.js +4 -5
  146. package/dist/src/insecure/pb/proto.js.map +1 -1
  147. package/dist/src/libp2p.d.ts +7 -3
  148. package/dist/src/libp2p.d.ts.map +1 -1
  149. package/dist/src/libp2p.js +35 -35
  150. package/dist/src/libp2p.js.map +1 -1
  151. package/dist/src/nat-manager.d.ts.map +1 -1
  152. package/dist/src/nat-manager.js +5 -3
  153. package/dist/src/nat-manager.js.map +1 -1
  154. package/dist/src/peer-record-updater.d.ts.map +1 -1
  155. package/dist/src/peer-routing.d.ts.map +1 -1
  156. package/dist/src/peer-routing.js +5 -5
  157. package/dist/src/peer-routing.js.map +1 -1
  158. package/dist/src/ping/index.d.ts.map +1 -1
  159. package/dist/src/ping/index.js +2 -2
  160. package/dist/src/ping/index.js.map +1 -1
  161. package/dist/src/pnet/crypto.d.ts +1 -1
  162. package/dist/src/pnet/crypto.d.ts.map +1 -1
  163. package/dist/src/pnet/crypto.js +1 -1
  164. package/dist/src/pnet/crypto.js.map +1 -1
  165. package/dist/src/pnet/index.js +3 -3
  166. package/dist/src/pnet/index.js.map +1 -1
  167. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  168. package/dist/src/pubsub/dummy-pubsub.js +9 -9
  169. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  170. package/dist/src/registrar.d.ts.map +1 -1
  171. package/dist/src/registrar.js +4 -4
  172. package/dist/src/registrar.js.map +1 -1
  173. package/dist/src/transport-manager.d.ts.map +1 -1
  174. package/dist/src/transport-manager.js +8 -9
  175. package/dist/src/transport-manager.js.map +1 -1
  176. package/dist/src/upgrader.d.ts +2 -1
  177. package/dist/src/upgrader.d.ts.map +1 -1
  178. package/dist/src/upgrader.js +38 -33
  179. package/dist/src/upgrader.js.map +1 -1
  180. package/dist/typedoc-urls.json +19 -6
  181. package/package.json +36 -33
  182. package/src/address-manager/index.ts +62 -35
  183. package/src/autonat/constants.ts +4 -0
  184. package/src/autonat/index.ts +567 -0
  185. package/src/autonat/pb/index.proto +35 -0
  186. package/src/autonat/pb/index.ts +320 -0
  187. package/src/circuit/constants.ts +48 -7
  188. package/src/circuit/index.ts +17 -129
  189. package/src/circuit/pb/index.proto +57 -32
  190. package/src/circuit/pb/index.ts +448 -138
  191. package/src/circuit/server/advert-service.ts +108 -0
  192. package/src/circuit/server/index.ts +421 -0
  193. package/src/circuit/server/reservation-store.ts +111 -0
  194. package/src/circuit/server/reservation-voucher.ts +51 -0
  195. package/src/circuit/transport/discovery.ts +127 -0
  196. package/src/circuit/transport/index.ts +325 -0
  197. package/src/circuit/transport/listener.ts +99 -0
  198. package/src/circuit/transport/reservation-store.ts +249 -0
  199. package/src/circuit/utils.ts +108 -0
  200. package/src/components.ts +22 -21
  201. package/src/config.ts +21 -25
  202. package/src/connection/index.ts +10 -10
  203. package/src/connection-manager/auto-dialler.ts +36 -40
  204. package/src/connection-manager/dialer/dial-request.ts +13 -6
  205. package/src/connection-manager/dialer/index.ts +34 -15
  206. package/src/connection-manager/index.ts +22 -23
  207. package/src/connection-manager/latency-monitor.ts +9 -9
  208. package/src/connection-manager/visibility-change-emitter.ts +4 -4
  209. package/src/content-routing/index.ts +18 -16
  210. package/src/content-routing/utils.ts +5 -5
  211. package/src/dht/dht-content-routing.ts +5 -4
  212. package/src/dht/dht-peer-routing.ts +5 -4
  213. package/src/dht/dummy-dht.ts +13 -13
  214. package/src/errors.ts +2 -1
  215. package/src/fetch/index.ts +13 -13
  216. package/src/fetch/pb/proto.ts +8 -7
  217. package/src/get-peer.ts +3 -3
  218. package/src/identify/index.ts +29 -24
  219. package/src/identify/pb/message.ts +3 -2
  220. package/src/index.ts +15 -8
  221. package/src/insecure/index.ts +2 -1
  222. package/src/insecure/pb/proto.ts +8 -9
  223. package/src/libp2p.ts +52 -46
  224. package/src/nat-manager.ts +11 -9
  225. package/src/peer-record-updater.ts +4 -4
  226. package/src/peer-routing.ts +9 -9
  227. package/src/ping/index.ts +6 -6
  228. package/src/pnet/crypto.ts +3 -3
  229. package/src/pnet/index.ts +3 -3
  230. package/src/pnet/key-generator.ts +1 -1
  231. package/src/pubsub/dummy-pubsub.ts +9 -9
  232. package/src/registrar.ts +12 -12
  233. package/src/transport-manager.ts +17 -18
  234. package/src/upgrader.ts +43 -42
  235. package/src/version.ts +1 -1
  236. package/dist/src/circuit/auto-relay.d.ts +0 -46
  237. package/dist/src/circuit/auto-relay.d.ts.map +0 -1
  238. package/dist/src/circuit/auto-relay.js +0 -220
  239. package/dist/src/circuit/auto-relay.js.map +0 -1
  240. package/dist/src/circuit/circuit/hop.d.ts +0 -42
  241. package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
  242. package/dist/src/circuit/circuit/hop.js +0 -142
  243. package/dist/src/circuit/circuit/hop.js.map +0 -1
  244. package/dist/src/circuit/circuit/stop.d.ts +0 -24
  245. package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
  246. package/dist/src/circuit/circuit/stop.js +0 -51
  247. package/dist/src/circuit/circuit/stop.js.map +0 -1
  248. package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
  249. package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
  250. package/dist/src/circuit/circuit/stream-handler.js +0 -59
  251. package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
  252. package/dist/src/circuit/circuit/utils.d.ts +0 -7
  253. package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
  254. package/dist/src/circuit/circuit/utils.js +0 -43
  255. package/dist/src/circuit/circuit/utils.js.map +0 -1
  256. package/dist/src/circuit/listener.d.ts +0 -9
  257. package/dist/src/circuit/listener.d.ts.map +0 -1
  258. package/dist/src/circuit/listener.js +0 -62
  259. package/dist/src/circuit/listener.js.map +0 -1
  260. package/dist/src/circuit/multicodec.d.ts +0 -2
  261. package/dist/src/circuit/multicodec.d.ts.map +0 -1
  262. package/dist/src/circuit/multicodec.js +0 -2
  263. package/dist/src/circuit/multicodec.js.map +0 -1
  264. package/dist/src/circuit/transport.d.ts +0 -51
  265. package/dist/src/circuit/transport.d.ts.map +0 -1
  266. package/dist/src/circuit/transport.js +0 -223
  267. package/dist/src/circuit/transport.js.map +0 -1
  268. package/dist/src/keychain/cms.d.ts +0 -33
  269. package/dist/src/keychain/cms.d.ts.map +0 -1
  270. package/dist/src/keychain/cms.js +0 -129
  271. package/dist/src/keychain/cms.js.map +0 -1
  272. package/dist/src/keychain/index.d.ts +0 -151
  273. package/dist/src/keychain/index.d.ts.map +0 -1
  274. package/dist/src/keychain/index.js +0 -519
  275. package/dist/src/keychain/index.js.map +0 -1
  276. package/dist/src/keychain/util.d.ts +0 -21
  277. package/dist/src/keychain/util.d.ts.map +0 -1
  278. package/dist/src/keychain/util.js +0 -79
  279. package/dist/src/keychain/util.js.map +0 -1
  280. package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
  281. package/src/circuit/README.md +0 -111
  282. package/src/circuit/auto-relay.ts +0 -292
  283. package/src/circuit/circuit/hop.ts +0 -220
  284. package/src/circuit/circuit/stop.ts +0 -83
  285. package/src/circuit/circuit/stream-handler.ts +0 -87
  286. package/src/circuit/circuit/utils.ts +0 -44
  287. package/src/circuit/listener.ts +0 -83
  288. package/src/circuit/multicodec.ts +0 -2
  289. package/src/circuit/transport.ts +0 -278
  290. package/src/keychain/README.md +0 -55
  291. package/src/keychain/cms.ts +0 -150
  292. package/src/keychain/doc/private-key.png +0 -0
  293. package/src/keychain/doc/private-key.xml +0 -1
  294. package/src/keychain/index.ts +0 -629
  295. package/src/keychain/util.ts +0 -82
package/src/upgrader.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import errCode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import * as mss from '@libp2p/multistream-select'
4
4
  import { codes } from './errors.js'
5
5
  import { createConnection } from './connection/index.js'
6
6
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
7
7
  import { peerIdFromString } from '@libp2p/peer-id'
8
- import type { MultiaddrConnection, Connection, Stream, ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection'
8
+ import type { MultiaddrConnection, Connection, Stream, ConnectionProtector } from '@libp2p/interface-connection'
9
9
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
10
10
  import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
11
11
  import type { PeerId } from '@libp2p/interface-peer-id'
@@ -20,6 +20,7 @@ import { setMaxListeners } from 'events'
20
20
  import type { Metrics } from '@libp2p/interface-metrics'
21
21
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
22
22
  import type { PeerStore } from '@libp2p/interface-peer-store'
23
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
23
24
 
24
25
  const log = logger('libp2p:upgrader')
25
26
 
@@ -53,7 +54,7 @@ export interface UpgraderInit {
53
54
  inboundUpgradeTimeout: number
54
55
  }
55
56
 
56
- function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
57
+ function findIncomingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
57
58
  try {
58
59
  const { options } = registrar.getHandler(protocol)
59
60
 
@@ -67,7 +68,7 @@ function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
67
68
  return DEFAULT_MAX_INBOUND_STREAMS
68
69
  }
69
70
 
70
- function findOutgoingStreamLimit (protocol: string, registrar: Registrar) {
71
+ function findOutgoingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
71
72
  try {
72
73
  const { options } = registrar.getHandler(protocol)
73
74
 
@@ -81,7 +82,7 @@ function findOutgoingStreamLimit (protocol: string, registrar: Registrar) {
81
82
  return DEFAULT_MAX_OUTBOUND_STREAMS
82
83
  }
83
84
 
84
- function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection) {
85
+ function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection): number {
85
86
  let streamCount = 0
86
87
 
87
88
  connection.streams.forEach(stream => {
@@ -135,7 +136,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
135
136
  const accept = await this.components.connectionManager.acceptIncomingConnection(maConn)
136
137
 
137
138
  if (!accept) {
138
- throw errCode(new Error('connection denied'), codes.ERR_CONNECTION_DENIED)
139
+ throw new CodeError('connection denied', codes.ERR_CONNECTION_DENIED)
139
140
  }
140
141
 
141
142
  let encryptedConn
@@ -149,15 +150,15 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
149
150
  try {
150
151
  // fails on node < 15.4
151
152
  setMaxListeners?.(Infinity, timeoutController.signal)
152
- } catch {}
153
+ } catch { }
153
154
 
154
155
  try {
155
156
  const abortableStream = abortableDuplex(maConn, timeoutController.signal)
156
157
  maConn.source = abortableStream.source
157
158
  maConn.sink = abortableStream.sink
158
159
 
159
- if (await this.components.connectionGater.denyInboundConnection(maConn)) {
160
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
160
+ if ((await this.components.connectionGater.denyInboundConnection?.(maConn)) === true) {
161
+ throw new CodeError('The multiaddr connection is blocked by gater.acceptConnection', codes.ERR_CONNECTION_INTERCEPTED)
161
162
  }
162
163
 
163
164
  this.components.metrics?.trackMultiaddrConnection(maConn)
@@ -186,17 +187,17 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
186
187
  protocol: cryptoProtocol
187
188
  } = await this._encryptInbound(protectedConn))
188
189
 
189
- if (await this.components.connectionGater.denyInboundEncryptedConnection(remotePeer, {
190
+ if ((await this.components.connectionGater.denyInboundEncryptedConnection?.(remotePeer, {
190
191
  ...protectedConn,
191
192
  ...encryptedConn
192
- })) {
193
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
193
+ })) === true) {
194
+ throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
194
195
  }
195
196
  } else {
196
197
  const idStr = maConn.remoteAddr.getPeerId()
197
198
 
198
199
  if (idStr == null) {
199
- throw errCode(new Error('inbound connection that skipped encryption must have a peer id'), codes.ERR_INVALID_MULTIADDR)
200
+ throw new CodeError('inbound connection that skipped encryption must have a peer id', codes.ERR_INVALID_MULTIADDR)
200
201
  }
201
202
 
202
203
  const remotePeerId = peerIdFromString(idStr)
@@ -222,11 +223,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
222
223
  throw err
223
224
  }
224
225
 
225
- if (await this.components.connectionGater.denyInboundUpgradedConnection(remotePeer, {
226
+ if ((await this.components.connectionGater.denyInboundUpgradedConnection?.(remotePeer, {
226
227
  ...protectedConn,
227
228
  ...encryptedConn
228
- })) {
229
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
229
+ })) === true) {
230
+ throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
230
231
  }
231
232
 
232
233
  log('Successfully upgraded inbound connection')
@@ -255,8 +256,8 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
255
256
  if (idStr != null) {
256
257
  remotePeerId = peerIdFromString(idStr)
257
258
 
258
- if (await this.components.connectionGater.denyOutboundConnection(remotePeerId, maConn)) {
259
- throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
259
+ if ((await this.components.connectionGater.denyOutboundConnection?.(remotePeerId, maConn)) === true) {
260
+ throw new CodeError('The multiaddr connection is blocked by connectionGater.denyOutboundConnection', codes.ERR_CONNECTION_INTERCEPTED)
260
261
  }
261
262
  }
262
263
 
@@ -293,15 +294,15 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
293
294
  protocol: cryptoProtocol
294
295
  } = await this._encryptOutbound(protectedConn, remotePeerId))
295
296
 
296
- if (await this.components.connectionGater.denyOutboundEncryptedConnection(remotePeer, {
297
+ if ((await this.components.connectionGater.denyOutboundEncryptedConnection?.(remotePeer, {
297
298
  ...protectedConn,
298
299
  ...encryptedConn
299
- })) {
300
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
300
+ })) === true) {
301
+ throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
301
302
  }
302
303
  } else {
303
304
  if (remotePeerId == null) {
304
- throw errCode(new Error('Encryption was skipped but no peer id was passed'), codes.ERR_INVALID_PEER)
305
+ throw new CodeError('Encryption was skipped but no peer id was passed', codes.ERR_INVALID_PEER)
305
306
  }
306
307
 
307
308
  cryptoProtocol = 'native'
@@ -326,11 +327,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
326
327
  throw err
327
328
  }
328
329
 
329
- if (await this.components.connectionGater.denyOutboundUpgradedConnection(remotePeer, {
330
+ if ((await this.components.connectionGater.denyOutboundUpgradedConnection?.(remotePeer, {
330
331
  ...protectedConn,
331
332
  ...encryptedConn
332
- })) {
333
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
333
+ })) === true) {
334
+ throw new CodeError('The multiaddr connection is blocked by gater.acceptEncryptedConnection', codes.ERR_CONNECTION_INTERCEPTED)
334
335
  }
335
336
 
336
337
  log('Successfully upgraded outbound connection')
@@ -386,7 +387,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
386
387
  const streamCount = countStreams(protocol, 'inbound', connection)
387
388
 
388
389
  if (streamCount === incomingLimit) {
389
- muxedStream.abort(errCode(new Error(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`), codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS))
390
+ muxedStream.abort(new CodeError(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`, codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS))
390
391
 
391
392
  return
392
393
  }
@@ -399,7 +400,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
399
400
 
400
401
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
401
402
  // the peerstore should ensure that the peer is registered with that protocol
402
- this.components.peerStore.protoBook.add(remotePeer, [protocol]).catch(err => log.error(err))
403
+ this.components.peerStore.protoBook.add(remotePeer, [protocol]).catch(err => { log.error(err) })
403
404
 
404
405
  connection.addStream(muxedStream)
405
406
  this.components.metrics?.trackProtocolStream(muxedStream, connection)
@@ -422,7 +423,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
422
423
 
423
424
  newStream = async (protocols: string[], options: AbortOptions = {}): Promise<Stream> => {
424
425
  if (muxer == null) {
425
- throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
426
+ throw new CodeError('Stream is not multiplexed', codes.ERR_MUXER_UNAVAILABLE)
426
427
  }
427
428
 
428
429
  log('%s: starting new stream on %s', direction, protocols)
@@ -439,7 +440,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
439
440
  try {
440
441
  // fails on node < 15.4
441
442
  setMaxListeners?.(Infinity, controller.signal)
442
- } catch {}
443
+ } catch { }
443
444
  }
444
445
 
445
446
  const { stream, protocol } = await mss.select(muxedStream, protocols, options)
@@ -448,7 +449,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
448
449
  const streamCount = countStreams(protocol, 'outbound', connection)
449
450
 
450
451
  if (streamCount === outgoingLimit) {
451
- const err = errCode(new Error(`Too many outbound protocol streams for protocol "${protocol}" - limit ${outgoingLimit}`), codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
452
+ const err = new CodeError(`Too many outbound protocol streams for protocol "${protocol}" - limit ${outgoingLimit}`, codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
452
453
  muxedStream.abort(err)
453
454
 
454
455
  throw err
@@ -456,7 +457,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
456
457
 
457
458
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
458
459
  // the peerstore should ensure that the peer is registered with that protocol
459
- this.components.peerStore.protoBook.add(remotePeer, [protocol]).catch(err => log.error(err))
460
+ this.components.peerStore.protoBook.add(remotePeer, [protocol]).catch(err => { log.error(err) })
460
461
 
461
462
  // after the handshake the returned stream can have early data so override
462
463
  // the souce/sink
@@ -478,7 +479,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
478
479
  throw err
479
480
  }
480
481
 
481
- throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
482
+ throw new CodeError(String(err), codes.ERR_UNSUPPORTED_PROTOCOL)
482
483
  } finally {
483
484
  if (controller != null) {
484
485
  controller.clear()
@@ -522,14 +523,14 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
522
523
  })
523
524
  maConn.timeline.upgraded = Date.now()
524
525
 
525
- const errConnectionNotMultiplexed = () => {
526
- throw errCode(new Error('connection is not multiplexed'), codes.ERR_CONNECTION_NOT_MULTIPLEXED)
526
+ const errConnectionNotMultiplexed = (): any => {
527
+ throw new CodeError('connection is not multiplexed', codes.ERR_CONNECTION_NOT_MULTIPLEXED)
527
528
  }
528
529
 
529
530
  // Create the connection
530
531
  connection = createConnection({
531
532
  remoteAddr: maConn.remoteAddr,
532
- remotePeer: remotePeer,
533
+ remotePeer,
533
534
  stat: {
534
535
  status: 'OPEN',
535
536
  direction,
@@ -538,7 +539,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
538
539
  encryption: cryptoProtocol
539
540
  },
540
541
  newStream: newStream ?? errConnectionNotMultiplexed,
541
- getStreams: () => muxer != null ? muxer.streams : errConnectionNotMultiplexed(),
542
+ getStreams: () => { if (muxer != null) { return muxer.streams } else { return errConnectionNotMultiplexed() } },
542
543
  close: async () => {
543
544
  await maConn.close()
544
545
  // Ensure remaining streams are closed
@@ -589,7 +590,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
589
590
  protocol
590
591
  }
591
592
  } catch (err: any) {
592
- throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
593
+ throw new CodeError(String(err), codes.ERR_ENCRYPTION_FAILED)
593
594
  }
594
595
  }
595
596
 
@@ -618,7 +619,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
618
619
  protocol
619
620
  }
620
621
  } catch (err: any) {
621
- throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
622
+ throw new CodeError(String(err), codes.ERR_ENCRYPTION_FAILED)
622
623
  }
623
624
  }
624
625
 
@@ -626,7 +627,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
626
627
  * Selects one of the given muxers via multistream-select. That
627
628
  * muxer will be used for all future streams on the connection.
628
629
  */
629
- async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
630
+ async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
630
631
  const protocols = Array.from(muxers.keys())
631
632
  log('outbound selecting muxer %s', protocols)
632
633
  try {
@@ -638,7 +639,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
638
639
  return { stream, muxerFactory }
639
640
  } catch (err: any) {
640
641
  log.error('error multiplexing outbound stream', err)
641
- throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
642
+ throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
642
643
  }
643
644
  }
644
645
 
@@ -646,7 +647,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
646
647
  * Registers support for one of the given muxers via multistream-select. The
647
648
  * selected muxer will be used for all future streams on the connection.
648
649
  */
649
- async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory}> {
650
+ async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
650
651
  const protocols = Array.from(muxers.keys())
651
652
  log('inbound handling muxers %s', protocols)
652
653
  try {
@@ -657,7 +658,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
657
658
  return { stream, muxerFactory }
658
659
  } catch (err: any) {
659
660
  log.error('error multiplexing inbound stream', err)
660
- throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
661
+ throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
661
662
  }
662
663
  }
663
664
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.42.2'
1
+ export const version = '0.43.0'
2
2
  export const name = 'libp2p'
@@ -1,46 +0,0 @@
1
- import type { PeerId } from '@libp2p/interface-peer-id';
2
- import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interface-peer-store';
3
- import type { Connection } from '@libp2p/interface-connection';
4
- import type { RelayComponents } from './index.js';
5
- export interface AutoRelayInit {
6
- addressSorter?: AddressSorter;
7
- maxListeners?: number;
8
- onError?: (error: Error, msg?: string) => void;
9
- }
10
- export declare class AutoRelay {
11
- private readonly components;
12
- private readonly addressSorter;
13
- private readonly maxListeners;
14
- private readonly listenRelays;
15
- private readonly onError;
16
- constructor(components: RelayComponents, init: AutoRelayInit);
17
- /**
18
- * Check if a peer supports the relay protocol.
19
- * If the protocol is not supported, check if it was supported before and remove it as a listen relay.
20
- * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
21
- * inside the threshold.
22
- */
23
- _onProtocolChange(evt: CustomEvent<PeerProtocolsChangeData>): Promise<void>;
24
- /**
25
- * Peer disconnects
26
- */
27
- _onPeerDisconnected(evt: CustomEvent<Connection>): void;
28
- /**
29
- * Attempt to listen on the given relay connection
30
- */
31
- _addListenRelay(connection: Connection, id: string): Promise<void>;
32
- /**
33
- * Remove listen relay
34
- */
35
- _removeListenRelay(id: string): Promise<void>;
36
- /**
37
- * Try to listen on available hop relay connections.
38
- * The following order will happen while we do not have enough relays.
39
- * 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
40
- * 2. Dial and try to listen on the peers we know that support hop but are not connected.
41
- * 3. Search the network.
42
- */
43
- _listenOnAvailableHopRelays(peersToIgnore?: string[]): Promise<void>;
44
- _tryToListenOnRelay(peerId: PeerId): Promise<void>;
45
- }
46
- //# sourceMappingURL=auto-relay.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auto-relay.d.ts","sourceRoot":"","sources":["../../../src/circuit/auto-relay.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAA;AAC1F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAK9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAMjD,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CAC/C;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAQ;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAC1C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;gBAEjD,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE,aAAa;IAkB7D;;;;;OAKG;IACG,iBAAiB,CAAE,GAAG,EAAE,WAAW,CAAC,uBAAuB,CAAC;IAkDlE;;OAEG;IACH,mBAAmB,CAAE,GAAG,EAAE,WAAW,CAAC,UAAU,CAAC;IAejD;;OAEG;IACG,eAAe,CAAE,UAAU,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+CzE;;OAEG;IACG,kBAAkB,CAAE,EAAE,EAAE,MAAM;IAOpC;;;;;;OAMG;IACG,2BAA2B,CAAE,aAAa,GAAE,MAAM,EAAO;IAqFzD,mBAAmB,CAAE,MAAM,EAAE,MAAM;CAS1C"}
@@ -1,220 +0,0 @@
1
- import { logger } from '@libp2p/logger';
2
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
3
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
4
- import { RELAY_CODEC } from './multicodec.js';
5
- import { canHop } from './circuit/hop.js';
6
- import { namespaceToCid } from './utils.js';
7
- import { CIRCUIT_PROTO_CODE, HOP_METADATA_KEY, HOP_METADATA_VALUE, RELAY_RENDEZVOUS_NS } from './constants.js';
8
- import sort from 'it-sort';
9
- import all from 'it-all';
10
- import { pipe } from 'it-pipe';
11
- import { publicAddressesFirst } from '@libp2p/utils/address-sort';
12
- const log = logger('libp2p:auto-relay');
13
- const noop = () => { };
14
- export class AutoRelay {
15
- constructor(components, init) {
16
- this.components = components;
17
- this.addressSorter = init.addressSorter ?? publicAddressesFirst;
18
- this.maxListeners = init.maxListeners ?? 1;
19
- this.listenRelays = new Set();
20
- this.onError = init.onError ?? noop;
21
- this._onProtocolChange = this._onProtocolChange.bind(this);
22
- this._onPeerDisconnected = this._onPeerDisconnected.bind(this);
23
- this.components.peerStore.addEventListener('change:protocols', (evt) => {
24
- void this._onProtocolChange(evt).catch(err => {
25
- log.error(err);
26
- });
27
- });
28
- this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected);
29
- }
30
- /**
31
- * Check if a peer supports the relay protocol.
32
- * If the protocol is not supported, check if it was supported before and remove it as a listen relay.
33
- * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
34
- * inside the threshold.
35
- */
36
- async _onProtocolChange(evt) {
37
- const { peerId, protocols } = evt.detail;
38
- const id = peerId.toString();
39
- // Check if it has the protocol
40
- const hasProtocol = protocols.find(protocol => protocol === RELAY_CODEC);
41
- // If no protocol, check if we were keeping the peer before as a listenRelay
42
- if (hasProtocol == null) {
43
- if (this.listenRelays.has(id)) {
44
- await this._removeListenRelay(id);
45
- }
46
- return;
47
- }
48
- if (this.listenRelays.has(id)) {
49
- return;
50
- }
51
- // If protocol, check if can hop, store info in the metadataBook and listen on it
52
- try {
53
- const connections = this.components.connectionManager.getConnections(peerId);
54
- if (connections.length === 0) {
55
- return;
56
- }
57
- const connection = connections[0];
58
- // Do not hop on a relayed connection
59
- if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
60
- log(`relayed connection to ${id} will not be used to hop on`);
61
- return;
62
- }
63
- const supportsHop = await canHop({ connection });
64
- if (supportsHop) {
65
- await this.components.peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE));
66
- await this._addListenRelay(connection, id);
67
- }
68
- }
69
- catch (err) {
70
- this.onError(err);
71
- }
72
- }
73
- /**
74
- * Peer disconnects
75
- */
76
- _onPeerDisconnected(evt) {
77
- const connection = evt.detail;
78
- const peerId = connection.remotePeer;
79
- const id = peerId.toString();
80
- // Not listening on this relay
81
- if (!this.listenRelays.has(id)) {
82
- return;
83
- }
84
- this._removeListenRelay(id).catch(err => {
85
- log.error(err);
86
- });
87
- }
88
- /**
89
- * Attempt to listen on the given relay connection
90
- */
91
- async _addListenRelay(connection, id) {
92
- try {
93
- // Check if already listening on enough relays
94
- if (this.listenRelays.size >= this.maxListeners) {
95
- return;
96
- }
97
- // Get peer known addresses and sort them with public addresses first
98
- const remoteAddrs = await pipe(await this.components.peerStore.addressBook.get(connection.remotePeer), (source) => sort(source, this.addressSorter), async (source) => await all(source));
99
- // Attempt to listen on relay
100
- const result = await Promise.all(remoteAddrs.map(async (addr) => {
101
- try {
102
- let multiaddr = addr.multiaddr;
103
- if (multiaddr.getPeerId() == null) {
104
- multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`);
105
- }
106
- multiaddr = multiaddr.encapsulate('/p2p-circuit');
107
- // Announce multiaddrs will update on listen success by TransportManager event being triggered
108
- await this.components.transportManager.listen([multiaddr]);
109
- return true;
110
- }
111
- catch (err) {
112
- log.error('error listening on circuit address', err);
113
- this.onError(err);
114
- }
115
- return false;
116
- }));
117
- if (result.includes(true)) {
118
- this.listenRelays.add(id);
119
- }
120
- }
121
- catch (err) {
122
- this.onError(err);
123
- this.listenRelays.delete(id);
124
- }
125
- }
126
- /**
127
- * Remove listen relay
128
- */
129
- async _removeListenRelay(id) {
130
- if (this.listenRelays.delete(id)) {
131
- // TODO: this should be responsibility of the connMgr
132
- await this._listenOnAvailableHopRelays([id]);
133
- }
134
- }
135
- /**
136
- * Try to listen on available hop relay connections.
137
- * The following order will happen while we do not have enough relays.
138
- * 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
139
- * 2. Dial and try to listen on the peers we know that support hop but are not connected.
140
- * 3. Search the network.
141
- */
142
- async _listenOnAvailableHopRelays(peersToIgnore = []) {
143
- // TODO: The peer redial issue on disconnect should be handled by connection gating
144
- // Check if already listening on enough relays
145
- if (this.listenRelays.size >= this.maxListeners) {
146
- return;
147
- }
148
- const knownHopsToDial = [];
149
- const peers = await this.components.peerStore.all();
150
- // Check if we have known hop peers to use and attempt to listen on the already connected
151
- for (const { id, metadata } of peers) {
152
- const idStr = id.toString();
153
- // Continue to next if listening on this or peer to ignore
154
- if (this.listenRelays.has(idStr)) {
155
- continue;
156
- }
157
- if (peersToIgnore.includes(idStr)) {
158
- continue;
159
- }
160
- const supportsHop = metadata.get(HOP_METADATA_KEY);
161
- // Continue to next if it does not support Hop
162
- if ((supportsHop == null) || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
163
- continue;
164
- }
165
- const connections = this.components.connectionManager.getConnections(id);
166
- // If not connected, store for possible later use.
167
- if (connections.length === 0) {
168
- knownHopsToDial.push(id);
169
- continue;
170
- }
171
- await this._addListenRelay(connections[0], idStr);
172
- // Check if already listening on enough relays
173
- if (this.listenRelays.size >= this.maxListeners) {
174
- return;
175
- }
176
- }
177
- // Try to listen on known peers that are not connected
178
- for (const peerId of knownHopsToDial) {
179
- await this._tryToListenOnRelay(peerId);
180
- // Check if already listening on enough relays
181
- if (this.listenRelays.size >= this.maxListeners) {
182
- return;
183
- }
184
- }
185
- // Try to find relays to hop on the network
186
- try {
187
- const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS);
188
- for await (const provider of this.components.contentRouting.findProviders(cid)) {
189
- if (provider.multiaddrs.length === 0) {
190
- continue;
191
- }
192
- const peerId = provider.id;
193
- if (peerId.equals(this.components.peerId)) {
194
- // Skip the provider if it's us as dialing will fail
195
- continue;
196
- }
197
- await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs);
198
- await this._tryToListenOnRelay(peerId);
199
- // Check if already listening on enough relays
200
- if (this.listenRelays.size >= this.maxListeners) {
201
- return;
202
- }
203
- }
204
- }
205
- catch (err) {
206
- this.onError(err);
207
- }
208
- }
209
- async _tryToListenOnRelay(peerId) {
210
- try {
211
- const connection = await this.components.connectionManager.openConnection(peerId);
212
- await this._addListenRelay(connection, peerId.toString());
213
- }
214
- catch (err) {
215
- log.error('Could not use %p as relay', peerId, err);
216
- this.onError(err, `could not connect and listen on known hop relay ${peerId.toString()}`);
217
- }
218
- }
219
- }
220
- //# sourceMappingURL=auto-relay.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auto-relay.js","sourceRoot":"","sources":["../../../src/circuit/auto-relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gB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IAAI,CAAC,YAAY,EAAE;oBAC/C,OAAM;iBACP;aACF;SACF;QAAC,OAAO,GAAQ,EAAE;YACjB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;SAClB;IACH,CAAC;IAED,KAAK,CAAC,mBAAmB,CAAE,MAAc;QACvC,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,cAAc,CAAC,MAAM,CAAC,CAAA;YACjF,MAAM,IAAI,CAAC,eAAe,CAAC,UAAU,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;SAC1D;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,MAAM,EAAE,GAAG,CAAC,CAAA;YACnD,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,mDAAmD,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;SAC1F;IACH,CAAC;CACF"}
@@ -1,42 +0,0 @@
1
- import { StreamHandler } from './stream-handler.js';
2
- import { CircuitRelay as CircuitPB } from '../pb/index.js';
3
- import type { Connection } from '@libp2p/interface-connection';
4
- import type { Duplex } from 'it-stream-types';
5
- import type { Circuit } from '../transport.js';
6
- import type { ConnectionManager } from '@libp2p/interface-connection-manager';
7
- import type { AbortOptions } from '@libp2p/interfaces';
8
- import type { Uint8ArrayList } from 'uint8arraylist';
9
- export interface HopRequest {
10
- connection: Connection;
11
- request: CircuitPB;
12
- streamHandler: StreamHandler;
13
- circuit: Circuit;
14
- connectionManager: ConnectionManager;
15
- }
16
- export declare function handleHop(hopRequest: HopRequest): Promise<void>;
17
- export interface HopConfig extends AbortOptions {
18
- connection: Connection;
19
- request: CircuitPB;
20
- }
21
- /**
22
- * Performs a HOP request to a relay peer, to request a connection to another
23
- * peer. A new, virtual, connection will be created between the two via the relay.
24
- */
25
- export declare function hop(options: HopConfig): Promise<Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>>;
26
- export interface CanHopOptions extends AbortOptions {
27
- connection: Connection;
28
- }
29
- /**
30
- * Performs a CAN_HOP request to a relay peer, in order to understand its capabilities
31
- */
32
- export declare function canHop(options: CanHopOptions): Promise<boolean>;
33
- export interface HandleCanHopOptions {
34
- connection: Connection;
35
- streamHandler: StreamHandler;
36
- circuit: Circuit;
37
- }
38
- /**
39
- * Creates an unencoded CAN_HOP response based on the Circuits configuration
40
- */
41
- export declare function handleCanHop(options: HandleCanHopOptions): void;
42
- //# sourceMappingURL=hop.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hop.d.ts","sourceRoot":"","sources":["../../../../src/circuit/circuit/hop.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,YAAY,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAK1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAE9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIpD,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;IAClB,aAAa,EAAE,aAAa,CAAA;IAC5B,OAAO,EAAE,OAAO,CAAA;IAChB,iBAAiB,EAAE,iBAAiB,CAAA;CACrC;AAED,wBAAsB,SAAS,CAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CA6FtE;AAED,MAAM,WAAW,SAAU,SAAQ,YAAY;IAC7C,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,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