libp2p 0.42.2-45fc415b → 0.42.2-5e63ee2d

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 (251) hide show
  1. package/dist/index.min.js +28 -28
  2. package/dist/src/address-manager/index.d.ts +1 -10
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +46 -29
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/autonat/constants.d.ts +4 -0
  7. package/dist/src/autonat/constants.d.ts.map +1 -0
  8. package/dist/src/autonat/constants.js +4 -0
  9. package/dist/src/autonat/constants.js.map +1 -0
  10. package/dist/src/autonat/index.d.ts +64 -0
  11. package/dist/src/autonat/index.d.ts.map +1 -0
  12. package/dist/src/autonat/index.js +417 -0
  13. package/dist/src/autonat/index.js.map +1 -0
  14. package/dist/src/autonat/pb/index.d.ts +57 -0
  15. package/dist/src/autonat/pb/index.d.ts.map +1 -0
  16. package/dist/src/autonat/pb/index.js +250 -0
  17. package/dist/src/autonat/pb/index.js.map +1 -0
  18. package/dist/src/circuit/constants.d.ts +31 -6
  19. package/dist/src/circuit/constants.d.ts.map +1 -1
  20. package/dist/src/circuit/constants.js +37 -7
  21. package/dist/src/circuit/constants.js.map +1 -1
  22. package/dist/src/circuit/index.d.ts +16 -61
  23. package/dist/src/circuit/index.d.ts.map +1 -1
  24. package/dist/src/circuit/index.js +2 -68
  25. package/dist/src/circuit/index.js.map +1 -1
  26. package/dist/src/circuit/pb/index.d.ts +83 -43
  27. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  28. package/dist/src/circuit/pb/index.js +363 -127
  29. package/dist/src/circuit/pb/index.js.map +1 -1
  30. package/dist/src/circuit/server/advert-service.d.ts +43 -0
  31. package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
  32. package/dist/src/circuit/server/advert-service.js +69 -0
  33. package/dist/src/circuit/server/advert-service.js.map +1 -0
  34. package/dist/src/circuit/server/index.d.ts +60 -0
  35. package/dist/src/circuit/server/index.d.ts.map +1 -0
  36. package/dist/src/circuit/server/index.js +287 -0
  37. package/dist/src/circuit/server/index.js.map +1 -0
  38. package/dist/src/circuit/server/reservation-store.d.ts +49 -0
  39. package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
  40. package/dist/src/circuit/server/reservation-store.js +57 -0
  41. package/dist/src/circuit/server/reservation-store.js.map +1 -0
  42. package/dist/src/circuit/server/reservation-voucher.d.ts +18 -0
  43. package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
  44. package/dist/src/circuit/server/reservation-voucher.js +33 -0
  45. package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
  46. package/dist/src/circuit/transport/discovery.d.ts +45 -0
  47. package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
  48. package/dist/src/circuit/transport/discovery.js +82 -0
  49. package/dist/src/circuit/transport/discovery.js.map +1 -0
  50. package/dist/src/circuit/transport/index.d.ts +33 -0
  51. package/dist/src/circuit/transport/index.d.ts.map +1 -0
  52. package/dist/src/circuit/transport/index.js +225 -0
  53. package/dist/src/circuit/transport/index.js.map +1 -0
  54. package/dist/src/circuit/transport/listener.d.ts +9 -0
  55. package/dist/src/circuit/transport/listener.d.ts.map +1 -0
  56. package/dist/src/circuit/transport/listener.js +76 -0
  57. package/dist/src/circuit/transport/listener.js.map +1 -0
  58. package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
  59. package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
  60. package/dist/src/circuit/transport/reservation-store.js +165 -0
  61. package/dist/src/circuit/transport/reservation-store.js.map +1 -0
  62. package/dist/src/circuit/utils.d.ts +7 -0
  63. package/dist/src/circuit/utils.d.ts.map +1 -1
  64. package/dist/src/circuit/utils.js +87 -0
  65. package/dist/src/circuit/utils.js.map +1 -1
  66. package/dist/src/components.d.ts +2 -1
  67. package/dist/src/components.d.ts.map +1 -1
  68. package/dist/src/components.js.map +1 -1
  69. package/dist/src/config.d.ts.map +1 -1
  70. package/dist/src/config.js +17 -21
  71. package/dist/src/config.js.map +1 -1
  72. package/dist/src/connection/index.d.ts +2 -2
  73. package/dist/src/connection/index.d.ts.map +1 -1
  74. package/dist/src/connection/index.js +1 -1
  75. package/dist/src/connection/index.js.map +1 -1
  76. package/dist/src/connection-manager/auto-dialler.d.ts +0 -4
  77. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  78. package/dist/src/connection-manager/auto-dialler.js +0 -9
  79. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  80. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  81. package/dist/src/connection-manager/dialer/dial-request.js +8 -1
  82. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
  83. package/dist/src/connection-manager/dialer/index.d.ts +2 -1
  84. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  85. package/dist/src/connection-manager/dialer/index.js +3 -3
  86. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  87. package/dist/src/connection-manager/index.d.ts +0 -4
  88. package/dist/src/connection-manager/index.d.ts.map +1 -1
  89. package/dist/src/connection-manager/index.js +1 -1
  90. package/dist/src/connection-manager/index.js.map +1 -1
  91. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  92. package/dist/src/connection-manager/latency-monitor.js +1 -1
  93. package/dist/src/connection-manager/latency-monitor.js.map +1 -1
  94. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  95. package/dist/src/content-routing/index.d.ts +6 -4
  96. package/dist/src/content-routing/index.d.ts.map +1 -1
  97. package/dist/src/content-routing/index.js +1 -1
  98. package/dist/src/content-routing/index.js.map +1 -1
  99. package/dist/src/content-routing/utils.d.ts +3 -3
  100. package/dist/src/content-routing/utils.d.ts.map +1 -1
  101. package/dist/src/dht/dht-content-routing.d.ts +2 -1
  102. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  103. package/dist/src/dht/dht-content-routing.js.map +1 -1
  104. package/dist/src/dht/dht-peer-routing.d.ts +3 -2
  105. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  106. package/dist/src/dht/dht-peer-routing.js.map +1 -1
  107. package/dist/src/dht/dummy-dht.d.ts +1 -1
  108. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  109. package/dist/src/errors.d.ts +2 -1
  110. package/dist/src/errors.d.ts.map +1 -1
  111. package/dist/src/errors.js +1 -0
  112. package/dist/src/errors.js.map +1 -1
  113. package/dist/src/fetch/index.d.ts.map +1 -1
  114. package/dist/src/fetch/pb/proto.d.ts +3 -3
  115. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  116. package/dist/src/fetch/pb/proto.js +4 -3
  117. package/dist/src/fetch/pb/proto.js.map +1 -1
  118. package/dist/src/identify/index.d.ts +3 -1
  119. package/dist/src/identify/index.d.ts.map +1 -1
  120. package/dist/src/identify/index.js +12 -7
  121. package/dist/src/identify/index.js.map +1 -1
  122. package/dist/src/identify/pb/message.d.ts +2 -2
  123. package/dist/src/identify/pb/message.d.ts.map +1 -1
  124. package/dist/src/identify/pb/message.js +1 -0
  125. package/dist/src/identify/pb/message.js.map +1 -1
  126. package/dist/src/index.d.ts +13 -7
  127. package/dist/src/index.d.ts.map +1 -1
  128. package/dist/src/index.js.map +1 -1
  129. package/dist/src/insecure/index.d.ts.map +1 -1
  130. package/dist/src/insecure/index.js.map +1 -1
  131. package/dist/src/insecure/pb/proto.d.ts +3 -3
  132. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  133. package/dist/src/insecure/pb/proto.js +4 -5
  134. package/dist/src/insecure/pb/proto.js.map +1 -1
  135. package/dist/src/libp2p.d.ts +6 -2
  136. package/dist/src/libp2p.d.ts.map +1 -1
  137. package/dist/src/libp2p.js +19 -21
  138. package/dist/src/libp2p.js.map +1 -1
  139. package/dist/src/nat-manager.d.ts.map +1 -1
  140. package/dist/src/nat-manager.js +3 -1
  141. package/dist/src/nat-manager.js.map +1 -1
  142. package/dist/src/peer-record-updater.d.ts.map +1 -1
  143. package/dist/src/peer-routing.d.ts.map +1 -1
  144. package/dist/src/ping/index.d.ts.map +1 -1
  145. package/dist/src/pnet/crypto.d.ts +1 -1
  146. package/dist/src/pnet/crypto.d.ts.map +1 -1
  147. package/dist/src/pnet/crypto.js +1 -1
  148. package/dist/src/pnet/crypto.js.map +1 -1
  149. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  150. package/dist/src/registrar.d.ts.map +1 -1
  151. package/dist/src/transport-manager.d.ts.map +1 -1
  152. package/dist/src/transport-manager.js +3 -4
  153. package/dist/src/transport-manager.js.map +1 -1
  154. package/dist/src/upgrader.d.ts +2 -1
  155. package/dist/src/upgrader.d.ts.map +1 -1
  156. package/dist/src/upgrader.js +19 -14
  157. package/dist/src/upgrader.js.map +1 -1
  158. package/package.json +32 -26
  159. package/src/address-manager/index.ts +62 -35
  160. package/src/autonat/constants.ts +4 -0
  161. package/src/autonat/index.ts +567 -0
  162. package/src/autonat/pb/index.proto +35 -0
  163. package/src/autonat/pb/index.ts +320 -0
  164. package/src/circuit/constants.ts +48 -7
  165. package/src/circuit/index.ts +17 -129
  166. package/src/circuit/pb/index.proto +57 -32
  167. package/src/circuit/pb/index.ts +448 -138
  168. package/src/circuit/server/advert-service.ts +108 -0
  169. package/src/circuit/server/index.ts +421 -0
  170. package/src/circuit/server/reservation-store.ts +111 -0
  171. package/src/circuit/server/reservation-voucher.ts +51 -0
  172. package/src/circuit/transport/discovery.ts +127 -0
  173. package/src/circuit/transport/index.ts +325 -0
  174. package/src/circuit/transport/listener.ts +99 -0
  175. package/src/circuit/transport/reservation-store.ts +249 -0
  176. package/src/circuit/utils.ts +108 -0
  177. package/src/components.ts +9 -8
  178. package/src/config.ts +17 -21
  179. package/src/connection/index.ts +7 -7
  180. package/src/connection-manager/auto-dialler.ts +4 -20
  181. package/src/connection-manager/dialer/dial-request.ts +8 -1
  182. package/src/connection-manager/dialer/index.ts +10 -9
  183. package/src/connection-manager/index.ts +14 -19
  184. package/src/connection-manager/latency-monitor.ts +9 -9
  185. package/src/connection-manager/visibility-change-emitter.ts +4 -4
  186. package/src/content-routing/index.ts +10 -8
  187. package/src/content-routing/utils.ts +3 -3
  188. package/src/dht/dht-content-routing.ts +3 -2
  189. package/src/dht/dht-peer-routing.ts +3 -2
  190. package/src/dht/dummy-dht.ts +1 -1
  191. package/src/errors.ts +2 -1
  192. package/src/fetch/index.ts +7 -7
  193. package/src/fetch/pb/proto.ts +8 -7
  194. package/src/identify/index.ts +22 -17
  195. package/src/identify/pb/message.ts +3 -2
  196. package/src/index.ts +14 -7
  197. package/src/insecure/index.ts +2 -1
  198. package/src/insecure/pb/proto.ts +8 -9
  199. package/src/libp2p.ts +33 -32
  200. package/src/nat-manager.ts +9 -7
  201. package/src/peer-record-updater.ts +4 -4
  202. package/src/peer-routing.ts +4 -4
  203. package/src/ping/index.ts +4 -4
  204. package/src/pnet/crypto.ts +3 -3
  205. package/src/pnet/key-generator.ts +1 -1
  206. package/src/registrar.ts +8 -8
  207. package/src/transport-manager.ts +12 -13
  208. package/src/upgrader.ts +24 -23
  209. package/src/version.ts +1 -1
  210. package/dist/src/circuit/auto-relay.d.ts +0 -46
  211. package/dist/src/circuit/auto-relay.d.ts.map +0 -1
  212. package/dist/src/circuit/auto-relay.js +0 -220
  213. package/dist/src/circuit/auto-relay.js.map +0 -1
  214. package/dist/src/circuit/circuit/hop.d.ts +0 -42
  215. package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
  216. package/dist/src/circuit/circuit/hop.js +0 -142
  217. package/dist/src/circuit/circuit/hop.js.map +0 -1
  218. package/dist/src/circuit/circuit/stop.d.ts +0 -24
  219. package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
  220. package/dist/src/circuit/circuit/stop.js +0 -51
  221. package/dist/src/circuit/circuit/stop.js.map +0 -1
  222. package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
  223. package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
  224. package/dist/src/circuit/circuit/stream-handler.js +0 -59
  225. package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
  226. package/dist/src/circuit/circuit/utils.d.ts +0 -7
  227. package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
  228. package/dist/src/circuit/circuit/utils.js +0 -43
  229. package/dist/src/circuit/circuit/utils.js.map +0 -1
  230. package/dist/src/circuit/listener.d.ts +0 -9
  231. package/dist/src/circuit/listener.d.ts.map +0 -1
  232. package/dist/src/circuit/listener.js +0 -62
  233. package/dist/src/circuit/listener.js.map +0 -1
  234. package/dist/src/circuit/multicodec.d.ts +0 -2
  235. package/dist/src/circuit/multicodec.d.ts.map +0 -1
  236. package/dist/src/circuit/multicodec.js +0 -2
  237. package/dist/src/circuit/multicodec.js.map +0 -1
  238. package/dist/src/circuit/transport.d.ts +0 -51
  239. package/dist/src/circuit/transport.d.ts.map +0 -1
  240. package/dist/src/circuit/transport.js +0 -223
  241. package/dist/src/circuit/transport.js.map +0 -1
  242. package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
  243. package/src/circuit/README.md +0 -111
  244. package/src/circuit/auto-relay.ts +0 -292
  245. package/src/circuit/circuit/hop.ts +0 -220
  246. package/src/circuit/circuit/stop.ts +0 -83
  247. package/src/circuit/circuit/stream-handler.ts +0 -87
  248. package/src/circuit/circuit/utils.ts +0 -44
  249. package/src/circuit/listener.ts +0 -83
  250. package/src/circuit/multicodec.ts +0 -2
  251. package/src/circuit/transport.ts +0 -278
@@ -8,7 +8,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
8
  * @param {Uint8Array | NodeJS.WriteStream} bytes - An object to write the psk into
9
9
  * @returns {void}
10
10
  */
11
- export function generateKey (bytes: Uint8Array | NodeJS.WriteStream) {
11
+ export function generateKey (bytes: Uint8Array | NodeJS.WriteStream): void {
12
12
  const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
13
13
  const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
14
14
 
package/src/registrar.ts CHANGED
@@ -44,14 +44,14 @@ export class DefaultRegistrar implements Registrar {
44
44
  this.components.peerStore.addEventListener('change:protocols', this._onProtocolChange)
45
45
  }
46
46
 
47
- getProtocols () {
47
+ getProtocols (): string[] {
48
48
  return Array.from(new Set<string>([
49
49
  ...this.topologies.keys(),
50
50
  ...this.handlers.keys()
51
51
  ])).sort()
52
52
  }
53
53
 
54
- getHandler (protocol: string) {
54
+ getHandler (protocol: string): StreamHandlerRecord {
55
55
  const handler = this.handlers.get(protocol)
56
56
 
57
57
  if (handler == null) {
@@ -61,7 +61,7 @@ export class DefaultRegistrar implements Registrar {
61
61
  return handler
62
62
  }
63
63
 
64
- getTopologies (protocol: string) {
64
+ getTopologies (protocol: string): Topology[] {
65
65
  const topologies = this.topologies.get(protocol)
66
66
 
67
67
  if (topologies == null) {
@@ -99,7 +99,7 @@ export class DefaultRegistrar implements Registrar {
99
99
  * Removes the handler for each protocol. The protocol
100
100
  * will no longer be supported on streams.
101
101
  */
102
- async unhandle (protocols: string | string[]) {
102
+ async unhandle (protocols: string | string[]): Promise<void> {
103
103
  const protocolList = Array.isArray(protocols) ? protocols : [protocols]
104
104
 
105
105
  protocolList.forEach(protocol => {
@@ -140,7 +140,7 @@ export class DefaultRegistrar implements Registrar {
140
140
  /**
141
141
  * Unregister topology
142
142
  */
143
- unregister (id: string) {
143
+ unregister (id: string): void {
144
144
  for (const [protocol, topologies] of this.topologies.entries()) {
145
145
  if (topologies.has(id)) {
146
146
  topologies.delete(id)
@@ -155,7 +155,7 @@ export class DefaultRegistrar implements Registrar {
155
155
  /**
156
156
  * Remove a disconnected peer from the record
157
157
  */
158
- _onDisconnect (evt: CustomEvent<Connection>) {
158
+ _onDisconnect (evt: CustomEvent<Connection>): void {
159
159
  const connection = evt.detail
160
160
 
161
161
  void this.components.peerStore.protoBook.get(connection.remotePeer)
@@ -182,7 +182,7 @@ export class DefaultRegistrar implements Registrar {
182
182
  * On peer connected if we already have their protocols. Usually used for reconnects
183
183
  * as change:protocols event won't be emitted due to identical protocols.
184
184
  */
185
- _onConnect (evt: CustomEvent<Connection>) {
185
+ _onConnect (evt: CustomEvent<Connection>): void {
186
186
  const connection = evt.detail
187
187
 
188
188
  void this.components.peerStore.protoBook.get(connection.remotePeer)
@@ -208,7 +208,7 @@ export class DefaultRegistrar implements Registrar {
208
208
  /**
209
209
  * Check if a new peer support the multicodecs for this topology
210
210
  */
211
- _onProtocolChange (evt: CustomEvent<PeerProtocolsChangeData>) {
211
+ _onProtocolChange (evt: CustomEvent<PeerProtocolsChangeData>): void {
212
212
  const { peerId, protocols, oldProtocols } = evt.detail
213
213
  const removed = oldProtocols.filter(protocol => !protocols.includes(protocol))
214
214
  const added = protocols.filter(protocol => !oldProtocols.includes(protocol))
@@ -1,5 +1,4 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import pSettle from 'p-settle'
3
2
  import { codes } from './errors.js'
4
3
  import errCode from 'err-code'
5
4
  import { FaultTolerance } from '@libp2p/interface-transport'
@@ -48,7 +47,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
48
47
  /**
49
48
  * Adds a `Transport` to the manager
50
49
  */
51
- add (transport: Transport) {
50
+ add (transport: Transport): void {
52
51
  const tag = transport[Symbol.toStringTag]
53
52
 
54
53
  if (tag == null) {
@@ -56,7 +55,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
56
55
  }
57
56
 
58
57
  if (this.transports.has(tag)) {
59
- throw errCode(new Error('There is already a transport with this tag'), codes.ERR_DUPLICATE_TRANSPORT)
58
+ throw errCode(new Error(`There is already a transport with the tag ${tag}`), codes.ERR_DUPLICATE_TRANSPORT)
60
59
  }
61
60
 
62
61
  log('adding transport %s', tag)
@@ -68,11 +67,11 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
68
67
  }
69
68
  }
70
69
 
71
- isStarted () {
70
+ isStarted (): boolean {
72
71
  return this.started
73
72
  }
74
73
 
75
- async start () {
74
+ async start (): Promise<void> {
76
75
  // Listen on the provided transports for the provided addresses
77
76
  const addrs = this.components.addressManager.getListenAddrs()
78
77
 
@@ -84,7 +83,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
84
83
  /**
85
84
  * Stops all listeners
86
85
  */
87
- async stop () {
86
+ async stop (): Promise<void> {
88
87
  const tasks = []
89
88
  for (const [key, listeners] of this.listeners) {
90
89
  log('closing listeners for %s', key)
@@ -148,14 +147,14 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
148
147
  /**
149
148
  * Returns all the transports instances
150
149
  */
151
- getTransports () {
150
+ getTransports (): Transport[] {
152
151
  return Array.of(...this.transports.values())
153
152
  }
154
153
 
155
154
  /**
156
155
  * Finds a transport that matches the given Multiaddr
157
156
  */
158
- transportForMultiaddr (ma: Multiaddr) {
157
+ transportForMultiaddr (ma: Multiaddr): Transport | undefined {
159
158
  for (const transport of this.transports.values()) {
160
159
  const addrs = transport.filter([ma])
161
160
 
@@ -168,7 +167,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
168
167
  /**
169
168
  * Starts listeners for each listen Multiaddr
170
169
  */
171
- async listen (addrs: Multiaddr[]) {
170
+ async listen (addrs: Multiaddr[]): Promise<void> {
172
171
  if (addrs == null || addrs.length === 0) {
173
172
  log('no addresses were provided for listening, this node is dial only')
174
173
  return
@@ -218,12 +217,12 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
218
217
  continue
219
218
  }
220
219
 
221
- const results = await pSettle(tasks)
220
+ const results = await Promise.allSettled(tasks)
222
221
  // If we are listening on at least 1 address, succeed.
223
222
  // TODO: we should look at adding a retry (`p-retry`) here to better support
224
223
  // listening on remote addresses as they may be offline. We could then potentially
225
224
  // just wait for any (`p-any`) listener to succeed on each transport before returning
226
- const isListening = results.find(r => r.isFulfilled)
225
+ const isListening = results.find(r => r.status === 'fulfilled')
227
226
  if ((isListening == null) && this.faultTolerance !== FaultTolerance.NO_FATAL) {
228
227
  throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
229
228
  }
@@ -244,7 +243,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
244
243
  * Removes the given transport from the manager.
245
244
  * If a transport has any running listeners, they will be closed.
246
245
  */
247
- async remove (key: string) {
246
+ async remove (key: string): Promise<void> {
248
247
  log('removing %s', key)
249
248
 
250
249
  // Close any running listeners
@@ -262,7 +261,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
262
261
  *
263
262
  * @async
264
263
  */
265
- async removeAll () {
264
+ async removeAll (): Promise<void> {
266
265
  const tasks = []
267
266
  for (const key of this.transports.keys()) {
268
267
  tasks.push(this.remove(key))
package/src/upgrader.ts CHANGED
@@ -5,7 +5,7 @@ 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 => {
@@ -149,14 +150,14 @@ 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
+ if ((await this.components.connectionGater.denyInboundConnection?.(maConn)) === true) {
160
161
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
161
162
  }
162
163
 
@@ -186,10 +187,10 @@ 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
+ })) === true) {
193
194
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
194
195
  }
195
196
  } else {
@@ -222,10 +223,10 @@ 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
+ })) === true) {
229
230
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
230
231
  }
231
232
 
@@ -255,7 +256,7 @@ 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
+ if ((await this.components.connectionGater.denyOutboundConnection?.(remotePeerId, maConn)) === true) {
259
260
  throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
260
261
  }
261
262
  }
@@ -293,10 +294,10 @@ 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
+ })) === true) {
300
301
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
301
302
  }
302
303
  } else {
@@ -326,10 +327,10 @@ 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
+ })) === true) {
333
334
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
334
335
  }
335
336
 
@@ -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)
@@ -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)
@@ -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
@@ -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
+ const errConnectionNotMultiplexed = (): any => {
526
527
  throw errCode(new Error('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
@@ -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 {
@@ -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 {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.42.2-45fc415b'
1
+ export const version = '0.42.2-5e63ee2d'
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,gBAAgB,CAAA;AACvC,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB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