libp2p 0.36.0 → 0.36.2-199395d

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 (398) hide show
  1. package/LICENSE +3 -21
  2. package/README.md +13 -10
  3. package/dist/src/address-manager/index.d.ts +33 -52
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +81 -0
  6. package/dist/src/address-manager/index.js.map +1 -0
  7. package/dist/src/circuit/auto-relay.d.ts +24 -85
  8. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  9. package/dist/src/circuit/auto-relay.js +215 -0
  10. package/dist/src/circuit/auto-relay.js.map +1 -0
  11. package/dist/src/circuit/circuit/hop.d.ts +25 -52
  12. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  13. package/dist/src/circuit/circuit/hop.js +138 -0
  14. package/dist/src/circuit/circuit/hop.js.map +1 -0
  15. package/dist/src/circuit/circuit/stop.d.ts +16 -8
  16. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  17. package/dist/src/circuit/circuit/stop.js +49 -0
  18. package/dist/src/circuit/circuit/stop.js.map +1 -0
  19. package/dist/src/circuit/circuit/stream-handler.d.ts +16 -35
  20. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  21. package/dist/src/circuit/circuit/stream-handler.js +60 -0
  22. package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
  23. package/dist/src/circuit/circuit/utils.d.ts +3 -6
  24. package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
  25. package/dist/src/circuit/circuit/utils.js +43 -0
  26. package/dist/src/circuit/circuit/utils.js.map +1 -0
  27. package/dist/src/circuit/constants.d.ts +24 -6
  28. package/dist/src/circuit/constants.d.ts.map +1 -1
  29. package/dist/src/circuit/constants.js +26 -0
  30. package/dist/src/circuit/constants.js.map +1 -0
  31. package/dist/src/circuit/index.d.ts +41 -74
  32. package/dist/src/circuit/index.d.ts.map +1 -1
  33. package/dist/src/circuit/index.js +69 -0
  34. package/dist/src/circuit/index.js.map +1 -0
  35. package/dist/src/circuit/listener.d.ts +8 -3
  36. package/dist/src/circuit/listener.d.ts.map +1 -1
  37. package/dist/src/circuit/listener.js +54 -0
  38. package/dist/src/circuit/listener.js.map +1 -0
  39. package/dist/src/circuit/multicodec.d.ts +1 -1
  40. package/dist/src/circuit/multicodec.d.ts.map +1 -1
  41. package/dist/src/circuit/multicodec.js +2 -0
  42. package/dist/src/circuit/multicodec.js.map +1 -0
  43. package/dist/src/circuit/{protocol → pb}/index.d.ts +0 -0
  44. package/{src/circuit/protocol → dist/src/circuit/pb}/index.js +7 -9
  45. package/dist/src/circuit/transport.d.ts +18 -78
  46. package/dist/src/circuit/transport.d.ts.map +1 -1
  47. package/dist/src/circuit/transport.js +190 -0
  48. package/dist/src/circuit/transport.js.map +1 -0
  49. package/dist/src/circuit/utils.d.ts +5 -2
  50. package/dist/src/circuit/utils.d.ts.map +1 -1
  51. package/dist/src/circuit/utils.js +11 -0
  52. package/dist/src/circuit/utils.js.map +1 -0
  53. package/dist/src/config.d.ts +3 -85
  54. package/dist/src/config.d.ts.map +1 -1
  55. package/dist/src/config.js +93 -0
  56. package/dist/src/config.js.map +1 -0
  57. package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
  58. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  59. package/dist/src/connection-manager/auto-dialler.js +105 -0
  60. package/dist/src/connection-manager/auto-dialler.js.map +1 -0
  61. package/dist/src/connection-manager/index.d.ts +79 -144
  62. package/dist/src/connection-manager/index.d.ts.map +1 -1
  63. package/dist/src/connection-manager/index.js +295 -0
  64. package/dist/src/connection-manager/index.js.map +1 -0
  65. package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
  66. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  67. package/dist/src/connection-manager/latency-monitor.js +220 -0
  68. package/dist/src/connection-manager/latency-monitor.js.map +1 -0
  69. package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
  70. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  71. package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
  72. package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
  73. package/dist/src/constants.d.ts +22 -10
  74. package/dist/src/constants.d.ts.map +1 -1
  75. package/dist/src/constants.js +27 -0
  76. package/dist/src/constants.js.map +1 -0
  77. package/dist/src/content-routing/index.d.ts +26 -81
  78. package/dist/src/content-routing/index.d.ts.map +1 -1
  79. package/dist/src/content-routing/index.js +99 -0
  80. package/dist/src/content-routing/index.js.map +1 -0
  81. package/dist/src/content-routing/utils.d.ts +6 -49
  82. package/dist/src/content-routing/utils.d.ts.map +1 -1
  83. package/dist/src/content-routing/utils.js +42 -0
  84. package/dist/src/content-routing/utils.js.map +1 -0
  85. package/dist/src/dht/dht-content-routing.d.ts +10 -24
  86. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  87. package/dist/src/dht/dht-content-routing.js +32 -0
  88. package/dist/src/dht/dht-content-routing.js.map +1 -0
  89. package/dist/src/dht/dht-peer-routing.d.ts +9 -24
  90. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  91. package/dist/src/dht/dht-peer-routing.js +26 -0
  92. package/dist/src/dht/dht-peer-routing.js.map +1 -0
  93. package/dist/src/dialer/auto-dialer.d.ts +14 -0
  94. package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
  95. package/dist/src/dialer/auto-dialer.js +24 -0
  96. package/dist/src/dialer/auto-dialer.js.map +1 -0
  97. package/dist/src/dialer/dial-request.d.ts +18 -46
  98. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  99. package/dist/src/dialer/dial-request.js +83 -0
  100. package/dist/src/dialer/dial-request.js.map +1 -0
  101. package/dist/src/dialer/index.d.ts +41 -148
  102. package/dist/src/dialer/index.d.ts.map +1 -1
  103. package/dist/src/dialer/index.js +281 -0
  104. package/dist/src/dialer/index.js.map +1 -0
  105. package/dist/src/errors.d.ts +68 -63
  106. package/dist/src/errors.d.ts.map +1 -1
  107. package/dist/src/errors.js +73 -0
  108. package/dist/src/errors.js.map +1 -0
  109. package/dist/src/fetch/constants.d.ts +1 -1
  110. package/dist/src/fetch/constants.d.ts.map +1 -1
  111. package/dist/src/fetch/constants.js +3 -0
  112. package/dist/src/fetch/constants.js.map +1 -0
  113. package/dist/src/fetch/index.d.ts +30 -49
  114. package/dist/src/fetch/index.d.ts.map +1 -1
  115. package/dist/src/fetch/index.js +130 -0
  116. package/dist/src/fetch/index.js.map +1 -0
  117. package/dist/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
  118. package/{src/fetch → dist/src/fetch/pb}/proto.js +7 -9
  119. package/dist/src/get-peer.d.ts +4 -11
  120. package/dist/src/get-peer.d.ts.map +1 -1
  121. package/dist/src/get-peer.js +43 -0
  122. package/dist/src/get-peer.js.map +1 -0
  123. package/dist/src/identify/consts.d.ts +9 -9
  124. package/dist/src/identify/consts.d.ts.map +1 -1
  125. package/dist/src/identify/consts.js +11 -0
  126. package/dist/src/identify/consts.js.map +1 -0
  127. package/dist/src/identify/index.d.ts +40 -96
  128. package/dist/src/identify/index.d.ts.map +1 -1
  129. package/dist/src/identify/index.js +322 -0
  130. package/dist/src/identify/index.js.map +1 -0
  131. package/{src/identify → dist/src/identify/pb}/message.d.ts +20 -5
  132. package/{src/identify → dist/src/identify/pb}/message.js +83 -42
  133. package/dist/src/index.d.ts +157 -583
  134. package/dist/src/index.d.ts.map +1 -1
  135. package/dist/src/index.js +9 -0
  136. package/dist/src/index.js.map +1 -0
  137. package/dist/src/insecure/index.d.ts +9 -0
  138. package/dist/src/insecure/index.d.ts.map +1 -0
  139. package/dist/src/insecure/index.js +78 -0
  140. package/dist/src/insecure/index.js.map +1 -0
  141. package/dist/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
  142. package/{src/insecure → dist/src/insecure/pb}/proto.js +39 -22
  143. package/dist/src/keychain/cms.d.ts +6 -14
  144. package/dist/src/keychain/cms.d.ts.map +1 -1
  145. package/dist/src/keychain/cms.js +129 -0
  146. package/dist/src/keychain/cms.js.map +1 -0
  147. package/dist/src/keychain/index.d.ts +62 -82
  148. package/dist/src/keychain/index.d.ts.map +1 -1
  149. package/dist/src/keychain/index.js +493 -0
  150. package/dist/src/keychain/index.js.map +1 -0
  151. package/dist/src/keychain/util.d.ts +3 -6
  152. package/dist/src/keychain/util.d.ts.map +1 -1
  153. package/dist/src/keychain/util.js +79 -0
  154. package/dist/src/keychain/util.js.map +1 -0
  155. package/dist/src/libp2p.d.ts +75 -0
  156. package/dist/src/libp2p.d.ts.map +1 -0
  157. package/dist/src/libp2p.js +368 -0
  158. package/dist/src/libp2p.js.map +1 -0
  159. package/dist/src/metrics/index.d.ts +47 -104
  160. package/dist/src/metrics/index.d.ts.map +1 -1
  161. package/dist/src/metrics/index.js +231 -0
  162. package/dist/src/metrics/index.js.map +1 -0
  163. package/dist/src/metrics/moving-average.d.ts +14 -0
  164. package/dist/src/metrics/moving-average.d.ts.map +1 -0
  165. package/dist/src/metrics/moving-average.js +40 -0
  166. package/dist/src/metrics/moving-average.js.map +1 -0
  167. package/dist/src/metrics/stats.d.ts +45 -93
  168. package/dist/src/metrics/stats.d.ts.map +1 -1
  169. package/dist/src/metrics/stats.js +183 -0
  170. package/dist/src/metrics/stats.js.map +1 -0
  171. package/dist/src/nat-manager.d.ts +42 -99
  172. package/dist/src/nat-manager.d.ts.map +1 -1
  173. package/dist/src/nat-manager.js +114 -0
  174. package/dist/src/nat-manager.js.map +1 -0
  175. package/dist/src/peer-record-updater.d.ts +15 -0
  176. package/dist/src/peer-record-updater.d.ts.map +1 -0
  177. package/dist/src/peer-record-updater.js +44 -0
  178. package/dist/src/peer-record-updater.js.map +1 -0
  179. package/dist/src/peer-routing.d.ts +39 -84
  180. package/dist/src/peer-routing.d.ts.map +1 -1
  181. package/dist/src/peer-routing.js +108 -0
  182. package/dist/src/peer-routing.js.map +1 -0
  183. package/dist/src/ping/constants.d.ts +4 -4
  184. package/dist/src/ping/constants.d.ts.map +1 -1
  185. package/dist/src/ping/constants.js +5 -0
  186. package/dist/src/ping/constants.js.map +1 -0
  187. package/dist/src/ping/index.d.ts +26 -33
  188. package/dist/src/ping/index.d.ts.map +1 -1
  189. package/dist/src/ping/index.js +56 -0
  190. package/dist/src/ping/index.js.map +1 -0
  191. package/dist/src/pnet/crypto.d.ts +15 -5
  192. package/dist/src/pnet/crypto.d.ts.map +1 -1
  193. package/dist/src/pnet/crypto.js +60 -0
  194. package/dist/src/pnet/crypto.js.map +1 -0
  195. package/dist/src/pnet/errors.d.ts +5 -5
  196. package/dist/src/pnet/errors.d.ts.map +1 -1
  197. package/dist/src/pnet/errors.js +6 -0
  198. package/dist/src/pnet/errors.js.map +1 -0
  199. package/dist/src/pnet/index.d.ts +11 -20
  200. package/dist/src/pnet/index.d.ts.map +1 -1
  201. package/dist/src/pnet/index.js +66 -0
  202. package/dist/src/pnet/index.js.map +1 -0
  203. package/dist/src/pnet/key-generator.d.ts +3 -7
  204. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  205. package/dist/src/pnet/key-generator.js +25 -0
  206. package/dist/src/pnet/key-generator.js.map +1 -0
  207. package/dist/src/registrar.d.ts +27 -65
  208. package/dist/src/registrar.d.ts.map +1 -1
  209. package/dist/src/registrar.js +159 -0
  210. package/dist/src/registrar.js.map +1 -0
  211. package/dist/src/transport-manager.d.ts +34 -82
  212. package/dist/src/transport-manager.d.ts.map +1 -1
  213. package/dist/src/transport-manager.js +223 -0
  214. package/dist/src/transport-manager.js.map +1 -0
  215. package/dist/src/upgrader.d.ts +49 -129
  216. package/dist/src/upgrader.d.ts.map +1 -1
  217. package/dist/src/upgrader.js +402 -0
  218. package/dist/src/upgrader.js.map +1 -0
  219. package/dist/src/version.d.ts +3 -0
  220. package/dist/src/version.d.ts.map +1 -0
  221. package/dist/src/version.js +3 -0
  222. package/dist/src/version.js.map +1 -0
  223. package/package.json +145 -197
  224. package/src/address-manager/index.ts +129 -0
  225. package/src/circuit/README.md +16 -11
  226. package/src/circuit/auto-relay.ts +284 -0
  227. package/src/circuit/circuit/hop.ts +211 -0
  228. package/src/circuit/circuit/stop.ts +78 -0
  229. package/src/circuit/circuit/stream-handler.ts +87 -0
  230. package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
  231. package/src/circuit/constants.ts +31 -0
  232. package/src/circuit/index.ts +120 -0
  233. package/src/circuit/{listener.js → listener.ts} +21 -27
  234. package/src/circuit/multicodec.ts +2 -0
  235. package/src/circuit/{protocol → pb}/index.d.ts +0 -0
  236. package/src/circuit/pb/index.js +528 -0
  237. package/src/circuit/{protocol → pb}/index.proto +0 -0
  238. package/src/circuit/transport.ts +216 -0
  239. package/src/circuit/utils.ts +12 -0
  240. package/src/config.ts +101 -0
  241. package/src/connection-manager/auto-dialler.ts +154 -0
  242. package/src/connection-manager/index.ts +422 -0
  243. package/src/connection-manager/latency-monitor.ts +319 -0
  244. package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
  245. package/src/constants.ts +31 -0
  246. package/src/content-routing/index.ts +143 -0
  247. package/src/content-routing/utils.ts +54 -0
  248. package/src/dht/dht-content-routing.ts +43 -0
  249. package/src/dht/dht-peer-routing.ts +35 -0
  250. package/src/dialer/auto-dialer.ts +40 -0
  251. package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
  252. package/src/dialer/index.ts +374 -0
  253. package/src/errors.ts +71 -0
  254. package/src/fetch/README.md +1 -1
  255. package/src/fetch/constants.ts +3 -0
  256. package/src/fetch/{index.js → index.ts} +79 -70
  257. package/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
  258. package/src/fetch/pb/proto.js +331 -0
  259. package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
  260. package/src/get-peer.ts +57 -0
  261. package/src/identify/consts.ts +13 -0
  262. package/src/identify/index.ts +445 -0
  263. package/{dist/src/identify → src/identify/pb}/message.d.ts +20 -5
  264. package/src/identify/pb/message.js +369 -0
  265. package/src/identify/{message.proto → pb/message.proto} +0 -0
  266. package/src/index.ts +234 -0
  267. package/src/insecure/index.ts +97 -0
  268. package/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
  269. package/src/insecure/pb/proto.js +388 -0
  270. package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
  271. package/src/keychain/{cms.js → cms.ts} +62 -58
  272. package/src/keychain/index.ts +588 -0
  273. package/src/keychain/{util.js → util.ts} +7 -17
  274. package/src/libp2p.ts +501 -0
  275. package/src/metrics/index.ts +310 -0
  276. package/src/metrics/moving-average.ts +53 -0
  277. package/src/metrics/stats.ts +243 -0
  278. package/src/nat-manager.ts +194 -0
  279. package/src/peer-record-updater.ts +55 -0
  280. package/src/peer-routing.ts +185 -0
  281. package/src/ping/README.md +1 -1
  282. package/src/ping/constants.ts +5 -0
  283. package/src/ping/index.ts +83 -0
  284. package/src/pnet/README.md +2 -2
  285. package/src/pnet/crypto.ts +67 -0
  286. package/src/pnet/errors.ts +5 -0
  287. package/src/pnet/index.ts +95 -0
  288. package/src/pnet/key-generator.ts +28 -0
  289. package/src/registrar.ts +205 -0
  290. package/src/transport-manager.ts +279 -0
  291. package/src/upgrader.ts +499 -0
  292. package/src/version.ts +3 -0
  293. package/dist/index.min.js +0 -55
  294. package/dist/src/insecure/plaintext.d.ts +0 -12
  295. package/dist/src/insecure/plaintext.d.ts.map +0 -1
  296. package/dist/src/metrics/old-peers.d.ts +0 -3
  297. package/dist/src/metrics/old-peers.d.ts.map +0 -1
  298. package/dist/src/metrics/tracked-map.d.ts +0 -8
  299. package/dist/src/metrics/tracked-map.d.ts.map +0 -1
  300. package/dist/src/peer-store/address-book.d.ts +0 -67
  301. package/dist/src/peer-store/address-book.d.ts.map +0 -1
  302. package/dist/src/peer-store/index.d.ts +0 -58
  303. package/dist/src/peer-store/index.d.ts.map +0 -1
  304. package/dist/src/peer-store/key-book.d.ts +0 -40
  305. package/dist/src/peer-store/key-book.d.ts.map +0 -1
  306. package/dist/src/peer-store/metadata-book.d.ts +0 -58
  307. package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
  308. package/dist/src/peer-store/pb/peer.d.ts +0 -222
  309. package/dist/src/peer-store/proto-book.d.ts +0 -43
  310. package/dist/src/peer-store/proto-book.d.ts.map +0 -1
  311. package/dist/src/peer-store/store.d.ts +0 -73
  312. package/dist/src/peer-store/store.d.ts.map +0 -1
  313. package/dist/src/peer-store/types.d.ts +0 -202
  314. package/dist/src/peer-store/types.d.ts.map +0 -1
  315. package/dist/src/ping/util.d.ts +0 -5
  316. package/dist/src/ping/util.d.ts.map +0 -1
  317. package/dist/src/pubsub-adapter.d.ts +0 -22
  318. package/dist/src/pubsub-adapter.d.ts.map +0 -1
  319. package/dist/src/record/envelope/envelope.d.ts +0 -77
  320. package/dist/src/record/envelope/index.d.ts +0 -80
  321. package/dist/src/record/envelope/index.d.ts.map +0 -1
  322. package/dist/src/record/peer-record/consts.d.ts +0 -4
  323. package/dist/src/record/peer-record/consts.d.ts.map +0 -1
  324. package/dist/src/record/peer-record/index.d.ts +0 -60
  325. package/dist/src/record/peer-record/index.d.ts.map +0 -1
  326. package/dist/src/record/peer-record/peer-record.d.ts +0 -133
  327. package/dist/src/record/utils.d.ts +0 -12
  328. package/dist/src/record/utils.d.ts.map +0 -1
  329. package/dist/src/types.d.ts +0 -90
  330. package/dist/src/types.d.ts.map +0 -1
  331. package/src/address-manager/index.js +0 -96
  332. package/src/circuit/auto-relay.js +0 -302
  333. package/src/circuit/circuit/hop.js +0 -205
  334. package/src/circuit/circuit/stop.js +0 -81
  335. package/src/circuit/circuit/stream-handler.js +0 -94
  336. package/src/circuit/constants.js +0 -12
  337. package/src/circuit/index.js +0 -102
  338. package/src/circuit/multicodec.js +0 -5
  339. package/src/circuit/transport.js +0 -229
  340. package/src/circuit/utils.js +0 -17
  341. package/src/config.js +0 -114
  342. package/src/connection-manager/auto-dialler.js +0 -132
  343. package/src/connection-manager/index.js +0 -363
  344. package/src/connection-manager/latency-monitor.js +0 -264
  345. package/src/constants.js +0 -18
  346. package/src/content-routing/index.js +0 -163
  347. package/src/content-routing/utils.js +0 -89
  348. package/src/dht/dht-content-routing.js +0 -44
  349. package/src/dht/dht-peer-routing.js +0 -51
  350. package/src/dialer/index.js +0 -376
  351. package/src/errors.js +0 -66
  352. package/src/fetch/constants.js +0 -6
  353. package/src/get-peer.js +0 -49
  354. package/src/identify/consts.js +0 -15
  355. package/src/identify/index.js +0 -384
  356. package/src/index.js +0 -811
  357. package/src/insecure/plaintext.js +0 -95
  358. package/src/keychain/index.js +0 -561
  359. package/src/metrics/index.js +0 -290
  360. package/src/metrics/old-peers.js +0 -16
  361. package/src/metrics/stats.js +0 -270
  362. package/src/metrics/tracked-map.js +0 -94
  363. package/src/nat-manager.js +0 -197
  364. package/src/peer-routing.js +0 -176
  365. package/src/peer-store/README.md +0 -145
  366. package/src/peer-store/address-book.js +0 -382
  367. package/src/peer-store/index.js +0 -121
  368. package/src/peer-store/key-book.js +0 -141
  369. package/src/peer-store/metadata-book.js +0 -250
  370. package/src/peer-store/pb/peer.d.ts +0 -222
  371. package/src/peer-store/pb/peer.js +0 -643
  372. package/src/peer-store/pb/peer.proto +0 -31
  373. package/src/peer-store/proto-book.js +0 -237
  374. package/src/peer-store/store.js +0 -263
  375. package/src/peer-store/types.ts +0 -245
  376. package/src/ping/constants.js +0 -8
  377. package/src/ping/index.js +0 -85
  378. package/src/ping/util.js +0 -18
  379. package/src/pnet/crypto.js +0 -84
  380. package/src/pnet/errors.js +0 -7
  381. package/src/pnet/index.js +0 -86
  382. package/src/pnet/key-generator.js +0 -33
  383. package/src/pubsub-adapter.js +0 -61
  384. package/src/record/README.md +0 -130
  385. package/src/record/envelope/envelope.d.ts +0 -77
  386. package/src/record/envelope/envelope.js +0 -243
  387. package/src/record/envelope/envelope.proto +0 -19
  388. package/src/record/envelope/index.js +0 -183
  389. package/src/record/peer-record/consts.js +0 -14
  390. package/src/record/peer-record/index.js +0 -113
  391. package/src/record/peer-record/peer-record.d.ts +0 -133
  392. package/src/record/peer-record/peer-record.js +0 -367
  393. package/src/record/peer-record/peer-record.proto +0 -18
  394. package/src/record/utils.js +0 -25
  395. package/src/registrar.js +0 -127
  396. package/src/transport-manager.js +0 -269
  397. package/src/types.ts +0 -98
  398. package/src/upgrader.js +0 -492
@@ -1,90 +0,0 @@
1
- import type PeerId from 'peer-id';
2
- import type { Multiaddr } from 'multiaddr';
3
- import type { MultiaddrConnection } from 'libp2p-interfaces/src/transport/types';
4
- export interface ConnectionGater {
5
- /**
6
- * denyDialMultiaddr tests whether we're permitted to Dial the
7
- * specified peer.
8
- *
9
- * This is called by the dialer.connectToPeer implementation before
10
- * dialling a peer.
11
- *
12
- * Return true to prevent dialing the passed peer.
13
- */
14
- denyDialPeer: (peerId: PeerId) => Promise<boolean>;
15
- /**
16
- * denyDialMultiaddr tests whether we're permitted to dial the specified
17
- * multiaddr for the given peer.
18
- *
19
- * This is called by the dialer.connectToPeer implementation after it has
20
- * resolved the peer's addrs, and prior to dialling each.
21
- *
22
- * Return true to prevent dialing the passed peer on the passed multiaddr.
23
- */
24
- denyDialMultiaddr: (peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
25
- /**
26
- * denyInboundConnection tests whether an incipient inbound connection is allowed.
27
- *
28
- * This is called by the upgrader, or by the transport directly (e.g. QUIC,
29
- * Bluetooth), straight after it has accepted a connection from its socket.
30
- *
31
- * Return true to deny the incoming passed connection.
32
- */
33
- denyInboundConnection: (maConn: MultiaddrConnection) => Promise<boolean>;
34
- /**
35
- * denyOutboundConnection tests whether an incipient outbound connection is allowed.
36
- *
37
- * This is called by the upgrader, or by the transport directly (e.g. QUIC,
38
- * Bluetooth), straight after it has created a connection with its socket.
39
- *
40
- * Return true to deny the incoming passed connection.
41
- */
42
- denyOutboundConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
43
- /**
44
- * denyInboundEncryptedConnection tests whether a given connection, now encrypted,
45
- * is allowed.
46
- *
47
- * This is called by the upgrader, after it has performed the security
48
- * handshake, and before it negotiates the muxer, or by the directly by the
49
- * transport, at the exact same checkpoint.
50
- *
51
- * Return true to deny the passed secured connection.
52
- */
53
- denyInboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
54
- /**
55
- * denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
56
- * is allowed.
57
- *
58
- * This is called by the upgrader, after it has performed the security
59
- * handshake, and before it negotiates the muxer, or by the directly by the
60
- * transport, at the exact same checkpoint.
61
- *
62
- * Return true to deny the passed secured connection.
63
- */
64
- denyOutboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
65
- /**
66
- * denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
67
- *
68
- * This is called after encryption has been negotiated and the connection has been
69
- * multiplexed, if a multiplexer is configured.
70
- *
71
- * Return true to deny the passed upgraded connection.
72
- */
73
- denyInboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
74
- /**
75
- * denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
76
- *
77
- * This is called after encryption has been negotiated and the connection has been
78
- * multiplexed, if a multiplexer is configured.
79
- *
80
- * Return true to deny the passed upgraded connection.
81
- */
82
- denyOutboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
83
- /**
84
- * Used by the address book to filter passed addresses.
85
- *
86
- * Return true to allow storing the passed multiaddr for the passed peer.
87
- */
88
- filterMultiaddrForPeer: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
89
- }
90
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,SAAS,CAAA;AACjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAA;AAEhF,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElD;;;;;;;;OAQG;IACH,iBAAiB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAE7E;;;;;;;OAOG;IACH,qBAAqB,EAAE,CAAC,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAExE;;;;;;;OAOG;IACH,sBAAsB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEzF;;;;;;;;;OASG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;;;;;;OASG;IACH,+BAA+B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElG;;;;;;;OAOG;IACH,6BAA6B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEhG;;;;;;;OAOG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;OAIG;IACH,sBAAsB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACjF"}
@@ -1,96 +0,0 @@
1
- 'use strict'
2
-
3
- const { EventEmitter } = require('events')
4
- const { Multiaddr } = require('multiaddr')
5
- const PeerId = require('peer-id')
6
-
7
- /**
8
- * @typedef {Object} AddressManagerOptions
9
- * @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
10
- * @property {string[]} [announce = []] - list of multiaddrs string representation to announce.
11
- */
12
-
13
- /**
14
- * @fires AddressManager#change:addresses Emitted when a addresses change.
15
- */
16
- class AddressManager extends EventEmitter {
17
- /**
18
- * Responsible for managing the peer addresses.
19
- * Peers can specify their listen and announce addresses.
20
- * The listen addresses will be used by the libp2p transports to listen for new connections,
21
- * while the announce addresses will be used for the peer addresses' to other peers in the network.
22
- *
23
- * @class
24
- * @param {PeerId} peerId - The Peer ID of the node
25
- * @param {object} [options]
26
- * @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
27
- * @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
28
- */
29
- constructor (peerId, { listen = [], announce = [] } = {}) {
30
- super()
31
-
32
- this.peerId = peerId
33
- this.listen = new Set(listen.map(ma => ma.toString()))
34
- this.announce = new Set(announce.map(ma => ma.toString()))
35
- this.observed = new Set()
36
- }
37
-
38
- /**
39
- * Get peer listen multiaddrs.
40
- *
41
- * @returns {Multiaddr[]}
42
- */
43
- getListenAddrs () {
44
- return Array.from(this.listen).map((a) => new Multiaddr(a))
45
- }
46
-
47
- /**
48
- * Get peer announcing multiaddrs.
49
- *
50
- * @returns {Multiaddr[]}
51
- */
52
- getAnnounceAddrs () {
53
- return Array.from(this.announce).map((a) => new Multiaddr(a))
54
- }
55
-
56
- /**
57
- * Get observed multiaddrs.
58
- *
59
- * @returns {Array<Multiaddr>}
60
- */
61
- getObservedAddrs () {
62
- return Array.from(this.observed).map((a) => new Multiaddr(a))
63
- }
64
-
65
- /**
66
- * Add peer observed addresses
67
- *
68
- * @param {string | Multiaddr} addr
69
- */
70
- addObservedAddr (addr) {
71
- let ma = new Multiaddr(addr)
72
- const remotePeer = ma.getPeerId()
73
-
74
- // strip our peer id if it has been passed
75
- if (remotePeer) {
76
- const remotePeerId = PeerId.createFromB58String(remotePeer)
77
-
78
- // use same encoding for comparison
79
- if (remotePeerId.equals(this.peerId)) {
80
- ma = ma.decapsulate(new Multiaddr(`/p2p/${this.peerId}`))
81
- }
82
- }
83
-
84
- const addrString = ma.toString()
85
-
86
- // do not trigger the change:addresses event if we already know about this address
87
- if (this.observed.has(addrString)) {
88
- return
89
- }
90
-
91
- this.observed.add(addrString)
92
- this.emit('change:addresses')
93
- }
94
- }
95
-
96
- module.exports = AddressManager
@@ -1,302 +0,0 @@
1
- 'use strict'
2
-
3
- const debug = require('debug')
4
- const log = Object.assign(debug('libp2p:auto-relay'), {
5
- error: debug('libp2p:auto-relay:err')
6
- })
7
-
8
- const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
9
- const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
10
- const { Multiaddr } = require('multiaddr')
11
- const all = require('it-all')
12
-
13
- const { relay: multicodec } = require('./multicodec')
14
- const { canHop } = require('./circuit/hop')
15
- const { namespaceToCid } = require('./utils')
16
- const {
17
- CIRCUIT_PROTO_CODE,
18
- HOP_METADATA_KEY,
19
- HOP_METADATA_VALUE,
20
- RELAY_RENDEZVOUS_NS
21
- } = require('./constants')
22
-
23
- /**
24
- * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
25
- * @typedef {import('../peer-store/types').Address} Address
26
- * @typedef {import('peer-id')} PeerId
27
- */
28
-
29
- /**
30
- * @typedef {Object} AutoRelayProperties
31
- * @property {import('../')} libp2p
32
- *
33
- * @typedef {Object} AutoRelayOptions
34
- * @property {number} [maxListeners = 1] - maximum number of relays to listen.
35
- * @property {(error: Error, msg?: string) => {}} [onError]
36
- */
37
-
38
- class AutoRelay {
39
- /**
40
- * Creates an instance of AutoRelay.
41
- *
42
- * @class
43
- * @param {AutoRelayProperties & AutoRelayOptions} props
44
- */
45
- constructor ({ libp2p, maxListeners = 1, onError }) {
46
- this._libp2p = libp2p
47
- this._peerId = libp2p.peerId
48
- this._peerStore = libp2p.peerStore
49
- this._connectionManager = libp2p.connectionManager
50
- this._transportManager = libp2p.transportManager
51
- this._addressSorter = libp2p.dialer.addressSorter
52
-
53
- this.maxListeners = maxListeners
54
-
55
- /**
56
- * @type {Set<string>}
57
- */
58
- this._listenRelays = new Set()
59
-
60
- this._onProtocolChange = this._onProtocolChange.bind(this)
61
- this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
62
-
63
- this._peerStore.on('change:protocols', this._onProtocolChange)
64
- this._connectionManager.on('peer:disconnect', this._onPeerDisconnected)
65
-
66
- /**
67
- * @param {Error} error
68
- * @param {string} [msg]
69
- */
70
- this._onError = (error, msg) => {
71
- log.error(msg || error)
72
- onError && onError(error, msg)
73
- }
74
- }
75
-
76
- /**
77
- * Check if a peer supports the relay protocol.
78
- * If the protocol is not supported, check if it was supported before and remove it as a listen relay.
79
- * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
80
- * inside the threshold.
81
- *
82
- * @param {Object} props
83
- * @param {PeerId} props.peerId
84
- * @param {string[]} props.protocols
85
- * @returns {Promise<void>}
86
- */
87
- async _onProtocolChange ({ peerId, protocols }) {
88
- const id = peerId.toB58String()
89
-
90
- // Check if it has the protocol
91
- const hasProtocol = protocols.find(protocol => protocol === multicodec)
92
-
93
- // If no protocol, check if we were keeping the peer before as a listenRelay
94
- if (!hasProtocol && this._listenRelays.has(id)) {
95
- await this._removeListenRelay(id)
96
- return
97
- } else if (!hasProtocol || this._listenRelays.has(id)) {
98
- return
99
- }
100
-
101
- // If protocol, check if can hop, store info in the metadataBook and listen on it
102
- try {
103
- const connection = this._connectionManager.get(peerId)
104
- if (!connection) {
105
- return
106
- }
107
-
108
- // Do not hop on a relayed connection
109
- if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
110
- log(`relayed connection to ${id} will not be used to hop on`)
111
- return
112
- }
113
-
114
- const supportsHop = await canHop({ connection })
115
-
116
- if (supportsHop) {
117
- await this._peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
118
- await this._addListenRelay(connection, id)
119
- }
120
- } catch (/** @type {any} */ err) {
121
- this._onError(err)
122
- }
123
- }
124
-
125
- /**
126
- * Peer disconnects.
127
- *
128
- * @param {Connection} connection - connection to the peer
129
- */
130
- _onPeerDisconnected (connection) {
131
- const peerId = connection.remotePeer
132
- const id = peerId.toB58String()
133
-
134
- // Not listening on this relay
135
- if (!this._listenRelays.has(id)) {
136
- return
137
- }
138
-
139
- this._removeListenRelay(id).catch(err => {
140
- log.error(err)
141
- })
142
- }
143
-
144
- /**
145
- * Attempt to listen on the given relay connection.
146
- *
147
- * @private
148
- * @param {Connection} connection - connection to the peer
149
- * @param {string} id - peer identifier string
150
- * @returns {Promise<void>}
151
- */
152
- async _addListenRelay (connection, id) {
153
- try {
154
- // Check if already listening on enough relays
155
- if (this._listenRelays.size >= this.maxListeners) {
156
- return
157
- }
158
-
159
- // Get peer known addresses and sort them per public addresses first
160
- const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
161
- connection.remotePeer, this._addressSorter
162
- )
163
-
164
- // Attempt to listen on relay
165
- const result = await Promise.all(
166
- remoteAddrs.map(async addr => {
167
- try {
168
- // Announce multiaddrs will update on listen success by TransportManager event being triggered
169
- await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
170
- return true
171
- } catch (/** @type {any} */ err) {
172
- this._onError(err)
173
- }
174
-
175
- return false
176
- })
177
- )
178
-
179
- if (result.includes(true)) {
180
- this._listenRelays.add(id)
181
- }
182
- } catch (/** @type {any} */ err) {
183
- this._onError(err)
184
- this._listenRelays.delete(id)
185
- }
186
- }
187
-
188
- /**
189
- * Remove listen relay.
190
- *
191
- * @private
192
- * @param {string} id - peer identifier string.
193
- */
194
- async _removeListenRelay (id) {
195
- if (this._listenRelays.delete(id)) {
196
- // TODO: this should be responsibility of the connMgr
197
- await this._listenOnAvailableHopRelays([id])
198
- }
199
- }
200
-
201
- /**
202
- * Try to listen on available hop relay connections.
203
- * The following order will happen while we do not have enough relays.
204
- * 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
205
- * 2. Dial and try to listen on the peers we know that support hop but are not connected.
206
- * 3. Search the network.
207
- *
208
- * @param {string[]} [peersToIgnore]
209
- */
210
- async _listenOnAvailableHopRelays (peersToIgnore = []) {
211
- // TODO: The peer redial issue on disconnect should be handled by connection gating
212
- // Check if already listening on enough relays
213
- if (this._listenRelays.size >= this.maxListeners) {
214
- return
215
- }
216
-
217
- const knownHopsToDial = []
218
- const peers = await all(this._peerStore.getPeers())
219
-
220
- // Check if we have known hop peers to use and attempt to listen on the already connected
221
- for await (const { id, metadata } of peers) {
222
- const idStr = id.toB58String()
223
-
224
- // Continue to next if listening on this or peer to ignore
225
- if (this._listenRelays.has(idStr)) {
226
- continue
227
- }
228
-
229
- if (peersToIgnore.includes(idStr)) {
230
- continue
231
- }
232
-
233
- const supportsHop = metadata.get(HOP_METADATA_KEY)
234
-
235
- // Continue to next if it does not support Hop
236
- if (!supportsHop || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
237
- continue
238
- }
239
-
240
- const connection = this._connectionManager.get(id)
241
-
242
- // If not connected, store for possible later use.
243
- if (!connection) {
244
- knownHopsToDial.push(id)
245
- continue
246
- }
247
-
248
- await this._addListenRelay(connection, idStr)
249
-
250
- // Check if already listening on enough relays
251
- if (this._listenRelays.size >= this.maxListeners) {
252
- return
253
- }
254
- }
255
-
256
- // Try to listen on known peers that are not connected
257
- for (const peerId of knownHopsToDial) {
258
- await this._tryToListenOnRelay(peerId)
259
-
260
- // Check if already listening on enough relays
261
- if (this._listenRelays.size >= this.maxListeners) {
262
- return
263
- }
264
- }
265
-
266
- // Try to find relays to hop on the network
267
- try {
268
- const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
269
- for await (const provider of this._libp2p.contentRouting.findProviders(cid)) {
270
- if (!provider.multiaddrs.length) {
271
- continue
272
- }
273
-
274
- const peerId = provider.id
275
- await this._peerStore.addressBook.add(peerId, provider.multiaddrs)
276
-
277
- await this._tryToListenOnRelay(peerId)
278
-
279
- // Check if already listening on enough relays
280
- if (this._listenRelays.size >= this.maxListeners) {
281
- return
282
- }
283
- }
284
- } catch (/** @type {any} */ err) {
285
- this._onError(err)
286
- }
287
- }
288
-
289
- /**
290
- * @param {PeerId} peerId
291
- */
292
- async _tryToListenOnRelay (peerId) {
293
- try {
294
- const connection = await this._libp2p.dial(peerId)
295
- await this._addListenRelay(connection, peerId.toB58String())
296
- } catch (/** @type {any} */ err) {
297
- this._onError(err, `could not connect and listen on known hop relay ${peerId.toB58String()}`)
298
- }
299
- }
300
- }
301
-
302
- module.exports = AutoRelay
@@ -1,205 +0,0 @@
1
- 'use strict'
2
-
3
- const debug = require('debug')
4
- const log = Object.assign(debug('libp2p:circuit:hop'), {
5
- error: debug('libp2p:circuit:hop:err')
6
- })
7
- const errCode = require('err-code')
8
-
9
- const PeerId = require('peer-id')
10
- const { validateAddrs } = require('./utils')
11
- const StreamHandler = require('./stream-handler')
12
- const { CircuitRelay: CircuitPB } = require('../protocol')
13
- const { pipe } = require('it-pipe')
14
- const { codes: Errors } = require('../../errors')
15
-
16
- const { stop } = require('./stop')
17
-
18
- const multicodec = require('./../multicodec')
19
-
20
- /**
21
- * @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
22
- * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
23
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
24
- * @typedef {import('../transport')} Transport
25
- */
26
-
27
- /**
28
- * @typedef {Object} HopRequest
29
- * @property {Connection} connection
30
- * @property {ICircuitRelay} request
31
- * @property {StreamHandler} streamHandler
32
- * @property {Transport} circuit
33
- */
34
-
35
- /**
36
- * @param {HopRequest} options
37
- * @returns {Promise<void>}
38
- */
39
- async function handleHop ({
40
- connection,
41
- request,
42
- streamHandler,
43
- circuit
44
- }) {
45
- // Ensure hop is enabled
46
- if (!circuit._options.hop.enabled) {
47
- log('HOP request received but we are not acting as a relay')
48
- return streamHandler.end({
49
- type: CircuitPB.Type.STATUS,
50
- code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY
51
- })
52
- }
53
-
54
- // Validate the HOP request has the required input
55
- try {
56
- validateAddrs(request, streamHandler)
57
- } catch (/** @type {any} */ err) {
58
- return log.error('invalid hop request via peer %s', connection.remotePeer.toB58String(), err)
59
- }
60
-
61
- if (!request.dstPeer) {
62
- log('HOP request received but we do not receive a dstPeer')
63
- return
64
- }
65
-
66
- // Get the connection to the destination (stop) peer
67
- const destinationPeer = new PeerId(request.dstPeer.id)
68
-
69
- const destinationConnection = circuit._connectionManager.get(destinationPeer)
70
- if (!destinationConnection && !circuit._options.hop.active) {
71
- log('HOP request received but we are not connected to the destination peer')
72
- return streamHandler.end({
73
- type: CircuitPB.Type.STATUS,
74
- code: CircuitPB.Status.HOP_NO_CONN_TO_DST
75
- })
76
- }
77
-
78
- // TODO: Handle being an active relay
79
- if (!destinationConnection) {
80
- return
81
- }
82
-
83
- // Handle the incoming HOP request by performing a STOP request
84
- const stopRequest = {
85
- type: CircuitPB.Type.STOP,
86
- dstPeer: request.dstPeer,
87
- srcPeer: request.srcPeer
88
- }
89
-
90
- let destinationStream
91
- try {
92
- destinationStream = await stop({
93
- connection: destinationConnection,
94
- request: stopRequest
95
- })
96
- } catch (/** @type {any} */ err) {
97
- return log.error(err)
98
- }
99
-
100
- log('hop request from %s is valid', connection.remotePeer.toB58String())
101
- streamHandler.write({
102
- type: CircuitPB.Type.STATUS,
103
- code: CircuitPB.Status.SUCCESS
104
- })
105
- const sourceStream = streamHandler.rest()
106
-
107
- // Short circuit the two streams to create the relayed connection
108
- return pipe(
109
- sourceStream,
110
- destinationStream,
111
- sourceStream
112
- )
113
- }
114
-
115
- /**
116
- * Performs a HOP request to a relay peer, to request a connection to another
117
- * peer. A new, virtual, connection will be created between the two via the relay.
118
- *
119
- * @param {object} options
120
- * @param {Connection} options.connection - Connection to the relay
121
- * @param {ICircuitRelay} options.request
122
- * @returns {Promise<MuxedStream>}
123
- */
124
- async function hop ({
125
- connection,
126
- request
127
- }) {
128
- // Create a new stream to the relay
129
- const { stream } = await connection.newStream([multicodec.relay])
130
- // Send the HOP request
131
- const streamHandler = new StreamHandler({ stream })
132
- streamHandler.write(request)
133
-
134
- const response = await streamHandler.read()
135
-
136
- if (!response) {
137
- throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED)
138
- }
139
-
140
- if (response.code === CircuitPB.Status.SUCCESS) {
141
- log('hop request was successful')
142
- return streamHandler.rest()
143
- }
144
-
145
- log('hop request failed with code %d, closing stream', response.code)
146
- streamHandler.close()
147
- throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
148
- }
149
-
150
- /**
151
- * Performs a CAN_HOP request to a relay peer, in order to understand its capabilities.
152
- *
153
- * @param {object} options
154
- * @param {Connection} options.connection - Connection to the relay
155
- * @returns {Promise<boolean>}
156
- */
157
- async function canHop ({
158
- connection
159
- }) {
160
- // Create a new stream to the relay
161
- const { stream } = await connection.newStream([multicodec.relay])
162
- // Send the HOP request
163
- const streamHandler = new StreamHandler({ stream })
164
- streamHandler.write({
165
- type: CircuitPB.Type.CAN_HOP
166
- })
167
-
168
- const response = await streamHandler.read()
169
- await streamHandler.close()
170
-
171
- if (!response || response.code !== CircuitPB.Status.SUCCESS) {
172
- return false
173
- }
174
-
175
- return true
176
- }
177
-
178
- /**
179
- * Creates an unencoded CAN_HOP response based on the Circuits configuration
180
- *
181
- * @param {Object} options
182
- * @param {Connection} options.connection
183
- * @param {StreamHandler} options.streamHandler
184
- * @param {Transport} options.circuit
185
- * @private
186
- */
187
- function handleCanHop ({
188
- connection,
189
- streamHandler,
190
- circuit
191
- }) {
192
- const canHop = circuit._options.hop.enabled
193
- log('can hop (%s) request from %s', canHop, connection.remotePeer.toB58String())
194
- streamHandler.end({
195
- type: CircuitPB.Type.STATUS,
196
- code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
197
- })
198
- }
199
-
200
- module.exports = {
201
- handleHop,
202
- hop,
203
- canHop,
204
- handleCanHop
205
- }