libp2p 0.36.2 → 0.37.0-b09eb8f

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 (431) hide show
  1. package/LICENSE +3 -21
  2. package/README.md +13 -10
  3. package/dist/src/address-manager/index.d.ts +34 -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 +216 -0
  10. package/dist/src/circuit/auto-relay.js.map +1 -0
  11. package/dist/src/circuit/circuit/hop.d.ts +26 -53
  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 +18 -10
  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 +19 -38
  20. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  21. package/dist/src/circuit/circuit/stream-handler.js +59 -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 +62 -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/pb/index.d.ts +52 -0
  44. package/dist/src/circuit/pb/index.d.ts.map +1 -0
  45. package/dist/src/circuit/pb/index.js +98 -0
  46. package/dist/src/circuit/pb/index.js.map +1 -0
  47. package/dist/src/circuit/transport.d.ts +18 -78
  48. package/dist/src/circuit/transport.d.ts.map +1 -1
  49. package/dist/src/circuit/transport.js +192 -0
  50. package/dist/src/circuit/transport.js.map +1 -0
  51. package/dist/src/circuit/utils.d.ts +5 -2
  52. package/dist/src/circuit/utils.d.ts.map +1 -1
  53. package/dist/src/circuit/utils.js +11 -0
  54. package/dist/src/circuit/utils.js.map +1 -0
  55. package/dist/src/config.d.ts +3 -85
  56. package/dist/src/config.d.ts.map +1 -1
  57. package/dist/src/config.js +91 -0
  58. package/dist/src/config.js.map +1 -0
  59. package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
  60. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  61. package/dist/src/connection-manager/auto-dialler.js +105 -0
  62. package/dist/src/connection-manager/auto-dialler.js.map +1 -0
  63. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +16 -0
  64. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
  65. package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
  66. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
  67. package/dist/src/connection-manager/dialer/dial-request.d.ts +27 -0
  68. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
  69. package/dist/src/connection-manager/dialer/dial-request.js +107 -0
  70. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
  71. package/dist/src/connection-manager/dialer/index.d.ts +104 -0
  72. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
  73. package/dist/src/connection-manager/dialer/index.js +266 -0
  74. package/dist/src/connection-manager/dialer/index.js.map +1 -0
  75. package/dist/src/connection-manager/index.d.ts +102 -136
  76. package/dist/src/connection-manager/index.d.ts.map +1 -1
  77. package/dist/src/connection-manager/index.js +339 -0
  78. package/dist/src/connection-manager/index.js.map +1 -0
  79. package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
  80. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  81. package/dist/src/connection-manager/latency-monitor.js +220 -0
  82. package/dist/src/connection-manager/latency-monitor.js.map +1 -0
  83. package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
  84. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  85. package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
  86. package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
  87. package/dist/src/constants.d.ts +22 -10
  88. package/dist/src/constants.d.ts.map +1 -1
  89. package/dist/src/constants.js +27 -0
  90. package/dist/src/constants.js.map +1 -0
  91. package/dist/src/content-routing/index.d.ts +27 -81
  92. package/dist/src/content-routing/index.d.ts.map +1 -1
  93. package/dist/src/content-routing/index.js +99 -0
  94. package/dist/src/content-routing/index.js.map +1 -0
  95. package/dist/src/content-routing/utils.d.ts +6 -49
  96. package/dist/src/content-routing/utils.d.ts.map +1 -1
  97. package/dist/src/content-routing/utils.js +42 -0
  98. package/dist/src/content-routing/utils.js.map +1 -0
  99. package/dist/src/dht/dht-content-routing.d.ts +10 -24
  100. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  101. package/dist/src/dht/dht-content-routing.js +32 -0
  102. package/dist/src/dht/dht-content-routing.js.map +1 -0
  103. package/dist/src/dht/dht-peer-routing.d.ts +9 -24
  104. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  105. package/dist/src/dht/dht-peer-routing.js +26 -0
  106. package/dist/src/dht/dht-peer-routing.js.map +1 -0
  107. package/dist/src/dht/dummy-dht.d.ts +20 -0
  108. package/dist/src/dht/dummy-dht.d.ts.map +1 -0
  109. package/dist/src/dht/dummy-dht.js +46 -0
  110. package/dist/src/dht/dummy-dht.js.map +1 -0
  111. package/dist/src/errors.d.ts +71 -63
  112. package/dist/src/errors.d.ts.map +1 -1
  113. package/dist/src/errors.js +76 -0
  114. package/dist/src/errors.js.map +1 -0
  115. package/dist/src/fetch/constants.d.ts +1 -1
  116. package/dist/src/fetch/constants.d.ts.map +1 -1
  117. package/dist/src/fetch/constants.js +3 -0
  118. package/dist/src/fetch/constants.js.map +1 -0
  119. package/dist/src/fetch/index.d.ts +30 -49
  120. package/dist/src/fetch/index.d.ts.map +1 -1
  121. package/dist/src/fetch/index.js +129 -0
  122. package/dist/src/fetch/index.js.map +1 -0
  123. package/dist/src/fetch/pb/proto.d.ts +27 -0
  124. package/dist/src/fetch/pb/proto.d.ts.map +1 -0
  125. package/dist/src/fetch/pb/proto.js +50 -0
  126. package/dist/src/fetch/pb/proto.js.map +1 -0
  127. package/dist/src/get-peer.d.ts +4 -11
  128. package/dist/src/get-peer.d.ts.map +1 -1
  129. package/dist/src/get-peer.js +43 -0
  130. package/dist/src/get-peer.js.map +1 -0
  131. package/dist/src/identify/consts.d.ts +9 -9
  132. package/dist/src/identify/consts.d.ts.map +1 -1
  133. package/dist/src/identify/consts.js +11 -0
  134. package/dist/src/identify/consts.js.map +1 -0
  135. package/dist/src/identify/index.d.ts +42 -96
  136. package/dist/src/identify/index.d.ts.map +1 -1
  137. package/dist/src/identify/index.js +322 -0
  138. package/dist/src/identify/index.js.map +1 -0
  139. package/dist/src/identify/pb/message.d.ts +16 -0
  140. package/dist/src/identify/pb/message.d.ts.map +1 -0
  141. package/dist/src/identify/pb/message.js +24 -0
  142. package/dist/src/identify/pb/message.js.map +1 -0
  143. package/dist/src/index.d.ts +150 -496
  144. package/dist/src/index.d.ts.map +1 -1
  145. package/dist/src/index.js +9 -0
  146. package/dist/src/index.js.map +1 -0
  147. package/dist/src/insecure/index.d.ts +9 -0
  148. package/dist/src/insecure/index.d.ts.map +1 -0
  149. package/dist/src/insecure/index.js +81 -0
  150. package/dist/src/insecure/index.js.map +1 -0
  151. package/dist/src/insecure/pb/proto.d.ts +29 -0
  152. package/dist/src/insecure/pb/proto.d.ts.map +1 -0
  153. package/dist/src/insecure/pb/proto.js +53 -0
  154. package/dist/src/insecure/pb/proto.js.map +1 -0
  155. package/dist/src/keychain/cms.d.ts +6 -14
  156. package/dist/src/keychain/cms.d.ts.map +1 -1
  157. package/dist/src/keychain/cms.js +129 -0
  158. package/dist/src/keychain/cms.js.map +1 -0
  159. package/dist/src/keychain/index.d.ts +60 -81
  160. package/dist/src/keychain/index.d.ts.map +1 -1
  161. package/dist/src/keychain/index.js +492 -0
  162. package/dist/src/keychain/index.js.map +1 -0
  163. package/dist/src/keychain/util.d.ts +3 -6
  164. package/dist/src/keychain/util.d.ts.map +1 -1
  165. package/dist/src/keychain/util.js +79 -0
  166. package/dist/src/keychain/util.js.map +1 -0
  167. package/dist/src/libp2p.d.ts +80 -0
  168. package/dist/src/libp2p.d.ts.map +1 -0
  169. package/dist/src/libp2p.js +397 -0
  170. package/dist/src/libp2p.js.map +1 -0
  171. package/dist/src/metrics/index.d.ts +47 -104
  172. package/dist/src/metrics/index.d.ts.map +1 -1
  173. package/dist/src/metrics/index.js +231 -0
  174. package/dist/src/metrics/index.js.map +1 -0
  175. package/dist/src/metrics/moving-average.d.ts +14 -0
  176. package/dist/src/metrics/moving-average.d.ts.map +1 -0
  177. package/dist/src/metrics/moving-average.js +40 -0
  178. package/dist/src/metrics/moving-average.js.map +1 -0
  179. package/dist/src/metrics/stats.d.ts +41 -93
  180. package/dist/src/metrics/stats.d.ts.map +1 -1
  181. package/dist/src/metrics/stats.js +183 -0
  182. package/dist/src/metrics/stats.js.map +1 -0
  183. package/dist/src/nat-manager.d.ts +45 -99
  184. package/dist/src/nat-manager.d.ts.map +1 -1
  185. package/dist/src/nat-manager.js +117 -0
  186. package/dist/src/nat-manager.js.map +1 -0
  187. package/dist/src/peer-record-updater.d.ts +15 -0
  188. package/dist/src/peer-record-updater.d.ts.map +1 -0
  189. package/dist/src/peer-record-updater.js +44 -0
  190. package/dist/src/peer-record-updater.js.map +1 -0
  191. package/dist/src/peer-routing.d.ts +40 -84
  192. package/dist/src/peer-routing.d.ts.map +1 -1
  193. package/dist/src/peer-routing.js +107 -0
  194. package/dist/src/peer-routing.js.map +1 -0
  195. package/dist/src/ping/constants.d.ts +4 -4
  196. package/dist/src/ping/constants.d.ts.map +1 -1
  197. package/dist/src/ping/constants.js +5 -0
  198. package/dist/src/ping/constants.js.map +1 -0
  199. package/dist/src/ping/index.d.ts +17 -31
  200. package/dist/src/ping/index.d.ts.map +1 -1
  201. package/dist/src/ping/index.js +57 -0
  202. package/dist/src/ping/index.js.map +1 -0
  203. package/dist/src/pnet/crypto.d.ts +15 -5
  204. package/dist/src/pnet/crypto.d.ts.map +1 -1
  205. package/dist/src/pnet/crypto.js +60 -0
  206. package/dist/src/pnet/crypto.js.map +1 -0
  207. package/dist/src/pnet/errors.d.ts +5 -5
  208. package/dist/src/pnet/errors.d.ts.map +1 -1
  209. package/dist/src/pnet/errors.js +6 -0
  210. package/dist/src/pnet/errors.js.map +1 -0
  211. package/dist/src/pnet/index.d.ts +12 -20
  212. package/dist/src/pnet/index.d.ts.map +1 -1
  213. package/dist/src/pnet/index.js +67 -0
  214. package/dist/src/pnet/index.js.map +1 -0
  215. package/dist/src/pnet/key-generator.d.ts +3 -7
  216. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  217. package/dist/src/pnet/key-generator.js +25 -0
  218. package/dist/src/pnet/key-generator.js.map +1 -0
  219. package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
  220. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
  221. package/dist/src/pubsub/dummy-pubsub.js +37 -0
  222. package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
  223. package/dist/src/registrar.d.ts +26 -65
  224. package/dist/src/registrar.d.ts.map +1 -1
  225. package/dist/src/registrar.js +144 -0
  226. package/dist/src/registrar.js.map +1 -0
  227. package/dist/src/transport-manager.d.ts +36 -82
  228. package/dist/src/transport-manager.d.ts.map +1 -1
  229. package/dist/src/transport-manager.js +223 -0
  230. package/dist/src/transport-manager.js.map +1 -0
  231. package/dist/src/upgrader.d.ts +49 -129
  232. package/dist/src/upgrader.d.ts.map +1 -1
  233. package/dist/src/upgrader.js +404 -0
  234. package/dist/src/upgrader.js.map +1 -0
  235. package/dist/src/version.d.ts +3 -0
  236. package/dist/src/version.d.ts.map +1 -0
  237. package/dist/src/version.js +3 -0
  238. package/dist/src/version.js.map +1 -0
  239. package/package.json +143 -203
  240. package/src/address-manager/index.ts +130 -0
  241. package/src/circuit/README.md +16 -11
  242. package/src/circuit/auto-relay.ts +286 -0
  243. package/src/circuit/circuit/hop.ts +211 -0
  244. package/src/circuit/circuit/stop.ts +78 -0
  245. package/src/circuit/circuit/stream-handler.ts +86 -0
  246. package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
  247. package/src/circuit/constants.ts +31 -0
  248. package/src/circuit/index.ts +120 -0
  249. package/src/circuit/listener.ts +82 -0
  250. package/src/circuit/multicodec.ts +2 -0
  251. package/src/circuit/{protocol → pb}/index.proto +1 -1
  252. package/src/circuit/pb/index.ts +117 -0
  253. package/src/circuit/transport.ts +219 -0
  254. package/src/circuit/utils.ts +12 -0
  255. package/src/config.ts +99 -0
  256. package/src/connection-manager/auto-dialler.ts +154 -0
  257. package/src/connection-manager/dialer/auto-dialer.ts +59 -0
  258. package/src/connection-manager/dialer/dial-request.ts +137 -0
  259. package/src/connection-manager/dialer/index.ts +381 -0
  260. package/src/connection-manager/index.ts +513 -0
  261. package/src/connection-manager/latency-monitor.ts +319 -0
  262. package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
  263. package/src/constants.ts +31 -0
  264. package/src/content-routing/index.ts +144 -0
  265. package/src/content-routing/utils.ts +54 -0
  266. package/src/dht/dht-content-routing.ts +43 -0
  267. package/src/dht/dht-peer-routing.ts +35 -0
  268. package/src/dht/dummy-dht.ts +60 -0
  269. package/src/errors.ts +74 -0
  270. package/src/fetch/README.md +1 -1
  271. package/src/fetch/constants.ts +3 -0
  272. package/src/fetch/index.ts +167 -0
  273. package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
  274. package/src/fetch/pb/proto.ts +65 -0
  275. package/src/get-peer.ts +57 -0
  276. package/src/identify/consts.ts +13 -0
  277. package/src/identify/index.ts +445 -0
  278. package/src/identify/{message.proto → pb/message.proto} +0 -0
  279. package/src/identify/pb/message.ts +37 -0
  280. package/src/index.ts +220 -0
  281. package/src/insecure/index.ts +101 -0
  282. package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
  283. package/src/insecure/pb/proto.ts +68 -0
  284. package/src/keychain/{cms.js → cms.ts} +62 -58
  285. package/src/keychain/index.ts +587 -0
  286. package/src/keychain/{util.js → util.ts} +7 -17
  287. package/src/libp2p.ts +538 -0
  288. package/src/metrics/index.ts +310 -0
  289. package/src/metrics/moving-average.ts +53 -0
  290. package/src/metrics/stats.ts +238 -0
  291. package/src/nat-manager.ts +198 -0
  292. package/src/peer-record-updater.ts +55 -0
  293. package/src/peer-routing.ts +185 -0
  294. package/src/ping/README.md +1 -1
  295. package/src/ping/constants.ts +5 -0
  296. package/src/ping/index.ts +84 -0
  297. package/src/pnet/README.md +2 -2
  298. package/src/pnet/crypto.ts +67 -0
  299. package/src/pnet/errors.ts +5 -0
  300. package/src/pnet/index.ts +97 -0
  301. package/src/pnet/key-generator.ts +28 -0
  302. package/src/pubsub/dummy-pubsub.ts +51 -0
  303. package/src/registrar.ts +189 -0
  304. package/src/transport-manager.ts +281 -0
  305. package/src/upgrader.ts +501 -0
  306. package/src/version.ts +3 -0
  307. package/dist/index.min.js +0 -55
  308. package/dist/src/circuit/protocol/index.d.ts +0 -173
  309. package/dist/src/dialer/dial-request.d.ts +0 -55
  310. package/dist/src/dialer/dial-request.d.ts.map +0 -1
  311. package/dist/src/dialer/index.d.ts +0 -182
  312. package/dist/src/dialer/index.d.ts.map +0 -1
  313. package/dist/src/fetch/proto.d.ts +0 -134
  314. package/dist/src/identify/message.d.ts +0 -95
  315. package/dist/src/insecure/plaintext.d.ts +0 -12
  316. package/dist/src/insecure/plaintext.d.ts.map +0 -1
  317. package/dist/src/insecure/proto.d.ts +0 -128
  318. package/dist/src/metrics/old-peers.d.ts +0 -3
  319. package/dist/src/metrics/old-peers.d.ts.map +0 -1
  320. package/dist/src/metrics/tracked-map.d.ts +0 -8
  321. package/dist/src/metrics/tracked-map.d.ts.map +0 -1
  322. package/dist/src/peer-store/address-book.d.ts +0 -67
  323. package/dist/src/peer-store/address-book.d.ts.map +0 -1
  324. package/dist/src/peer-store/index.d.ts +0 -58
  325. package/dist/src/peer-store/index.d.ts.map +0 -1
  326. package/dist/src/peer-store/key-book.d.ts +0 -40
  327. package/dist/src/peer-store/key-book.d.ts.map +0 -1
  328. package/dist/src/peer-store/metadata-book.d.ts +0 -58
  329. package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
  330. package/dist/src/peer-store/pb/peer.d.ts +0 -222
  331. package/dist/src/peer-store/proto-book.d.ts +0 -43
  332. package/dist/src/peer-store/proto-book.d.ts.map +0 -1
  333. package/dist/src/peer-store/store.d.ts +0 -73
  334. package/dist/src/peer-store/store.d.ts.map +0 -1
  335. package/dist/src/peer-store/types.d.ts +0 -202
  336. package/dist/src/peer-store/types.d.ts.map +0 -1
  337. package/dist/src/ping/util.d.ts +0 -5
  338. package/dist/src/ping/util.d.ts.map +0 -1
  339. package/dist/src/pubsub-adapter.d.ts +0 -22
  340. package/dist/src/pubsub-adapter.d.ts.map +0 -1
  341. package/dist/src/record/envelope/envelope.d.ts +0 -77
  342. package/dist/src/record/envelope/index.d.ts +0 -80
  343. package/dist/src/record/envelope/index.d.ts.map +0 -1
  344. package/dist/src/record/peer-record/consts.d.ts +0 -4
  345. package/dist/src/record/peer-record/consts.d.ts.map +0 -1
  346. package/dist/src/record/peer-record/index.d.ts +0 -60
  347. package/dist/src/record/peer-record/index.d.ts.map +0 -1
  348. package/dist/src/record/peer-record/peer-record.d.ts +0 -133
  349. package/dist/src/record/utils.d.ts +0 -12
  350. package/dist/src/record/utils.d.ts.map +0 -1
  351. package/dist/src/types.d.ts +0 -90
  352. package/dist/src/types.d.ts.map +0 -1
  353. package/src/address-manager/index.js +0 -96
  354. package/src/circuit/auto-relay.js +0 -302
  355. package/src/circuit/circuit/hop.js +0 -205
  356. package/src/circuit/circuit/stop.js +0 -81
  357. package/src/circuit/circuit/stream-handler.js +0 -94
  358. package/src/circuit/constants.js +0 -12
  359. package/src/circuit/index.js +0 -102
  360. package/src/circuit/listener.js +0 -75
  361. package/src/circuit/multicodec.js +0 -5
  362. package/src/circuit/protocol/index.d.ts +0 -173
  363. package/src/circuit/protocol/index.js +0 -530
  364. package/src/circuit/transport.js +0 -229
  365. package/src/circuit/utils.js +0 -17
  366. package/src/config.js +0 -114
  367. package/src/connection-manager/auto-dialler.js +0 -132
  368. package/src/connection-manager/index.js +0 -374
  369. package/src/connection-manager/latency-monitor.js +0 -264
  370. package/src/constants.js +0 -18
  371. package/src/content-routing/index.js +0 -163
  372. package/src/content-routing/utils.js +0 -89
  373. package/src/dht/dht-content-routing.js +0 -44
  374. package/src/dht/dht-peer-routing.js +0 -51
  375. package/src/dialer/dial-request.js +0 -103
  376. package/src/dialer/index.js +0 -376
  377. package/src/errors.js +0 -66
  378. package/src/fetch/constants.js +0 -6
  379. package/src/fetch/index.js +0 -159
  380. package/src/fetch/proto.d.ts +0 -134
  381. package/src/fetch/proto.js +0 -333
  382. package/src/get-peer.js +0 -49
  383. package/src/identify/consts.js +0 -15
  384. package/src/identify/index.js +0 -384
  385. package/src/identify/message.d.ts +0 -95
  386. package/src/identify/message.js +0 -328
  387. package/src/index.js +0 -813
  388. package/src/insecure/plaintext.js +0 -95
  389. package/src/insecure/proto.d.ts +0 -128
  390. package/src/insecure/proto.js +0 -371
  391. package/src/keychain/index.js +0 -561
  392. package/src/metrics/index.js +0 -290
  393. package/src/metrics/old-peers.js +0 -16
  394. package/src/metrics/stats.js +0 -270
  395. package/src/metrics/tracked-map.js +0 -94
  396. package/src/nat-manager.js +0 -197
  397. package/src/peer-routing.js +0 -176
  398. package/src/peer-store/README.md +0 -145
  399. package/src/peer-store/address-book.js +0 -382
  400. package/src/peer-store/index.js +0 -121
  401. package/src/peer-store/key-book.js +0 -141
  402. package/src/peer-store/metadata-book.js +0 -250
  403. package/src/peer-store/pb/peer.d.ts +0 -222
  404. package/src/peer-store/pb/peer.js +0 -643
  405. package/src/peer-store/pb/peer.proto +0 -31
  406. package/src/peer-store/proto-book.js +0 -237
  407. package/src/peer-store/store.js +0 -263
  408. package/src/peer-store/types.ts +0 -245
  409. package/src/ping/constants.js +0 -8
  410. package/src/ping/index.js +0 -84
  411. package/src/ping/util.js +0 -18
  412. package/src/pnet/crypto.js +0 -84
  413. package/src/pnet/errors.js +0 -7
  414. package/src/pnet/index.js +0 -86
  415. package/src/pnet/key-generator.js +0 -33
  416. package/src/pubsub-adapter.js +0 -61
  417. package/src/record/README.md +0 -130
  418. package/src/record/envelope/envelope.d.ts +0 -77
  419. package/src/record/envelope/envelope.js +0 -243
  420. package/src/record/envelope/envelope.proto +0 -19
  421. package/src/record/envelope/index.js +0 -183
  422. package/src/record/peer-record/consts.js +0 -14
  423. package/src/record/peer-record/index.js +0 -113
  424. package/src/record/peer-record/peer-record.d.ts +0 -133
  425. package/src/record/peer-record/peer-record.js +0 -367
  426. package/src/record/peer-record/peer-record.proto +0 -18
  427. package/src/record/utils.js +0 -25
  428. package/src/registrar.js +0 -127
  429. package/src/transport-manager.js +0 -269
  430. package/src/types.ts +0 -98
  431. package/src/upgrader.js +0 -492
package/src/upgrader.js DELETED
@@ -1,492 +0,0 @@
1
- 'use strict'
2
-
3
- const debug = require('debug')
4
- const log = Object.assign(debug('libp2p:upgrader'), {
5
- error: debug('libp2p:upgrader:err')
6
- })
7
- const errCode = require('err-code')
8
- const Multistream = require('multistream-select')
9
- const { Connection } = require('libp2p-interfaces/src/connection')
10
- const PeerId = require('peer-id')
11
- const { pipe } = require('it-pipe')
12
- // @ts-ignore mutable-proxy does not export types
13
- const mutableProxy = require('mutable-proxy')
14
-
15
- const { codes } = require('./errors')
16
-
17
- /**
18
- * @typedef {import('libp2p-interfaces/src/transport/types').MultiaddrConnection} MultiaddrConnection
19
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory
20
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').Muxer} Muxer
21
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
22
- * @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
23
- * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
24
- * @typedef {import('multiaddr').Multiaddr} Multiaddr
25
- * @typedef {import('./types').ConnectionGater} ConnectionGater
26
- */
27
-
28
- /**
29
- * @typedef CryptoResult
30
- * @property {MultiaddrConnection} conn A duplex iterable
31
- * @property {PeerId} remotePeer
32
- * @property {string} protocol
33
- */
34
-
35
- class Upgrader {
36
- /**
37
- * @param {object} options
38
- * @param {PeerId} options.localPeer
39
- * @param {ConnectionGater} options.connectionGater
40
- *
41
- * @param {import('./metrics')} [options.metrics]
42
- * @param {Map<string, Crypto>} [options.cryptos]
43
- * @param {Map<string, MuxerFactory>} [options.muxers]
44
- * @param {(connection: Connection) => void} options.onConnection - Called when a connection is upgraded
45
- * @param {(connection: Connection) => void} options.onConnectionEnd
46
- */
47
- constructor ({
48
- localPeer,
49
- metrics,
50
- connectionGater,
51
- cryptos = new Map(),
52
- muxers = new Map(),
53
- onConnectionEnd = () => {},
54
- onConnection = () => {}
55
- }) {
56
- this.connectionGater = connectionGater
57
- this.localPeer = localPeer
58
- this.metrics = metrics
59
- this.cryptos = cryptos
60
- this.muxers = muxers
61
- /** @type {import("./pnet") | null} */
62
- this.protector = null
63
- this.protocols = new Map()
64
- this.onConnection = onConnection
65
- this.onConnectionEnd = onConnectionEnd
66
- }
67
-
68
- /**
69
- * Upgrades an inbound connection
70
- *
71
- * @async
72
- * @param {MultiaddrConnection} maConn
73
- * @returns {Promise<Connection>}
74
- */
75
- async upgradeInbound (maConn) {
76
- let encryptedConn
77
- let remotePeer
78
- let upgradedConn
79
- let Muxer
80
- let cryptoProtocol
81
- let setPeer
82
- let proxyPeer
83
-
84
- if (await this.connectionGater.denyInboundConnection(maConn)) {
85
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
86
- }
87
-
88
- if (this.metrics) {
89
- ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
90
- const idString = (Math.random() * 1e9).toString(36) + Date.now()
91
- setPeer({ toB58String: () => idString })
92
- maConn = this.metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
93
- }
94
-
95
- log('Starting the inbound connection upgrade')
96
-
97
- // Protect
98
- let protectedConn = maConn
99
- if (this.protector) {
100
- protectedConn = await this.protector.protect(maConn)
101
- }
102
-
103
- try {
104
- // Encrypt the connection
105
- ({
106
- conn: encryptedConn,
107
- remotePeer,
108
- protocol: cryptoProtocol
109
- } = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))
110
-
111
- if (await this.connectionGater.denyInboundEncryptedConnection(remotePeer, encryptedConn)) {
112
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
113
- }
114
-
115
- // Multiplex the connection
116
- if (this.muxers.size) {
117
- ({ stream: upgradedConn, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
118
- } else {
119
- upgradedConn = encryptedConn
120
- }
121
- } catch (/** @type {any} */ err) {
122
- log.error('Failed to upgrade inbound connection', err)
123
- await maConn.close(err)
124
- throw err
125
- }
126
-
127
- if (await this.connectionGater.denyInboundUpgradedConnection(remotePeer, encryptedConn)) {
128
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
129
- }
130
-
131
- if (this.metrics) {
132
- this.metrics.updatePlaceholder(proxyPeer, remotePeer)
133
- setPeer(remotePeer)
134
- }
135
-
136
- log('Successfully upgraded inbound connection')
137
-
138
- return this._createConnection({
139
- cryptoProtocol,
140
- direction: 'inbound',
141
- maConn,
142
- upgradedConn,
143
- Muxer,
144
- remotePeer
145
- })
146
- }
147
-
148
- /**
149
- * Upgrades an outbound connection
150
- *
151
- * @async
152
- * @param {MultiaddrConnection} maConn
153
- * @returns {Promise<Connection>}
154
- */
155
- async upgradeOutbound (maConn) {
156
- const idStr = maConn.remoteAddr.getPeerId()
157
- if (!idStr) {
158
- throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR)
159
- }
160
-
161
- const remotePeerId = PeerId.createFromB58String(idStr)
162
-
163
- if (await this.connectionGater.denyOutboundConnection(remotePeerId, maConn)) {
164
- throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
165
- }
166
-
167
- let encryptedConn
168
- let remotePeer
169
- let upgradedConn
170
- let cryptoProtocol
171
- let Muxer
172
- let setPeer
173
- let proxyPeer
174
-
175
- if (this.metrics) {
176
- ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
177
- const idString = (Math.random() * 1e9).toString(36) + Date.now()
178
- setPeer({ toB58String: () => idString })
179
- maConn = this.metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
180
- }
181
-
182
- log('Starting the outbound connection upgrade')
183
-
184
- // Protect
185
- let protectedConn = maConn
186
- if (this.protector) {
187
- protectedConn = await this.protector.protect(maConn)
188
- }
189
-
190
- try {
191
- // Encrypt the connection
192
- ({
193
- conn: encryptedConn,
194
- remotePeer,
195
- protocol: cryptoProtocol
196
- } = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))
197
-
198
- if (await this.connectionGater.denyOutboundEncryptedConnection(remotePeer, encryptedConn)) {
199
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
200
- }
201
-
202
- // Multiplex the connection
203
- if (this.muxers.size) {
204
- ({ stream: upgradedConn, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
205
- } else {
206
- upgradedConn = encryptedConn
207
- }
208
- } catch (/** @type {any} */ err) {
209
- log.error('Failed to upgrade outbound connection', err)
210
- await maConn.close(err)
211
- throw err
212
- }
213
-
214
- if (await this.connectionGater.denyOutboundUpgradedConnection(remotePeer, encryptedConn)) {
215
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
216
- }
217
-
218
- if (this.metrics) {
219
- this.metrics.updatePlaceholder(proxyPeer, remotePeer)
220
- setPeer(remotePeer)
221
- }
222
-
223
- log('Successfully upgraded outbound connection')
224
-
225
- return this._createConnection({
226
- cryptoProtocol,
227
- direction: 'outbound',
228
- maConn,
229
- upgradedConn,
230
- Muxer,
231
- remotePeer
232
- })
233
- }
234
-
235
- /**
236
- * A convenience method for generating a new `Connection`
237
- *
238
- * @private
239
- * @param {object} options
240
- * @param {string} options.cryptoProtocol - The crypto protocol that was negotiated
241
- * @param {'inbound' | 'outbound'} options.direction - One of ['inbound', 'outbound']
242
- * @param {MultiaddrConnection} options.maConn - The transport layer connection
243
- * @param {MuxedStream | MultiaddrConnection} options.upgradedConn - A duplex connection returned from multiplexer and/or crypto selection
244
- * @param {MuxerFactory} [options.Muxer] - The muxer to be used for muxing
245
- * @param {PeerId} options.remotePeer - The peer the connection is with
246
- * @returns {Connection}
247
- */
248
- _createConnection ({
249
- cryptoProtocol,
250
- direction,
251
- maConn,
252
- upgradedConn,
253
- Muxer,
254
- remotePeer
255
- }) {
256
- /** @type {import("libp2p-interfaces/src/stream-muxer/types").Muxer} */
257
- let muxer
258
- /** @type {import("libp2p-interfaces/src/connection/connection").CreatedMuxedStream | undefined} */
259
- let newStream
260
- /** @type {Connection} */
261
- let connection // eslint-disable-line prefer-const
262
-
263
- if (Muxer) {
264
- // Create the muxer
265
- muxer = new Muxer({
266
- // Run anytime a remote stream is created
267
- onStream: async muxedStream => {
268
- if (!connection) return
269
- const mss = new Multistream.Listener(muxedStream)
270
- try {
271
- const { stream, protocol } = await mss.handle(Array.from(this.protocols.keys()))
272
- log('%s: incoming stream opened on %s', direction, protocol)
273
- if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol })
274
- connection.addStream(muxedStream, { protocol })
275
- this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
276
- } catch (/** @type {any} */ err) {
277
- log.error(err)
278
- }
279
- },
280
- // Run anytime a stream closes
281
- onStreamEnd: muxedStream => {
282
- connection.removeStream(muxedStream.id)
283
- }
284
- })
285
-
286
- newStream = async (protocols) => {
287
- log('%s: starting new stream on %s', direction, protocols)
288
- const muxedStream = muxer.newStream()
289
- const mss = new Multistream.Dialer(muxedStream)
290
- try {
291
- const { stream, protocol } = await mss.select(protocols)
292
- if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol })
293
- return { stream: { ...muxedStream, ...stream }, protocol }
294
- } catch (/** @type {any} */ err) {
295
- log.error('could not create new stream', err)
296
- throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
297
- }
298
- }
299
-
300
- // Pipe all data through the muxer
301
- pipe(upgradedConn, muxer, upgradedConn).catch(log.error)
302
- }
303
-
304
- const _timeline = maConn.timeline
305
- maConn.timeline = new Proxy(_timeline, {
306
- set: (...args) => {
307
- if (connection && args[1] === 'close' && args[2] && !_timeline.close) {
308
- // Wait for close to finish before notifying of the closure
309
- (async () => {
310
- try {
311
- if (connection.stat.status === 'open') {
312
- await connection.close()
313
- }
314
- } catch (/** @type {any} */ err) {
315
- log.error(err)
316
- } finally {
317
- this.onConnectionEnd(connection)
318
- }
319
- })().catch(err => {
320
- log.error(err)
321
- })
322
- }
323
-
324
- return Reflect.set(...args)
325
- }
326
- })
327
- maConn.timeline.upgraded = Date.now()
328
-
329
- const errConnectionNotMultiplexed = () => {
330
- throw errCode(new Error('connection is not multiplexed'), codes.ERR_CONNECTION_NOT_MULTIPLEXED)
331
- }
332
-
333
- // Create the connection
334
- connection = new Connection({
335
- localAddr: maConn.localAddr,
336
- remoteAddr: maConn.remoteAddr,
337
- localPeer: this.localPeer,
338
- remotePeer: remotePeer,
339
- stat: {
340
- direction,
341
- // @ts-ignore
342
- timeline: maConn.timeline,
343
- multiplexer: Muxer && Muxer.multicodec,
344
- encryption: cryptoProtocol
345
- },
346
- newStream: newStream || errConnectionNotMultiplexed,
347
- getStreams: () => muxer ? muxer.streams : errConnectionNotMultiplexed(),
348
- close: async () => {
349
- await maConn.close()
350
- // Ensure remaining streams are aborted
351
- if (muxer) {
352
- muxer.streams.map(stream => stream.abort())
353
- }
354
- }
355
- })
356
-
357
- this.onConnection(connection)
358
-
359
- return connection
360
- }
361
-
362
- /**
363
- * Routes incoming streams to the correct handler
364
- *
365
- * @private
366
- * @param {object} options
367
- * @param {Connection} options.connection - The connection the stream belongs to
368
- * @param {MuxedStream} options.stream
369
- * @param {string} options.protocol
370
- */
371
- _onStream ({ connection, stream, protocol }) {
372
- const handler = this.protocols.get(protocol)
373
- handler({ connection, stream, protocol })
374
- }
375
-
376
- /**
377
- * Attempts to encrypt the incoming `connection` with the provided `cryptos`.
378
- *
379
- * @private
380
- * @async
381
- * @param {PeerId} localPeer - The initiators PeerId
382
- * @param {*} connection
383
- * @param {Map<string, Crypto>} cryptos
384
- * @returns {Promise<CryptoResult>} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
385
- */
386
- async _encryptInbound (localPeer, connection, cryptos) {
387
- const mss = new Multistream.Listener(connection)
388
- const protocols = Array.from(cryptos.keys())
389
- log('handling inbound crypto protocol selection', protocols)
390
-
391
- try {
392
- const { stream, protocol } = await mss.handle(protocols)
393
- const crypto = cryptos.get(protocol)
394
- log('encrypting inbound connection...')
395
-
396
- if (!crypto) {
397
- throw new Error(`no crypto module found for ${protocol}`)
398
- }
399
-
400
- return {
401
- ...await crypto.secureInbound(localPeer, stream),
402
- protocol
403
- }
404
- } catch (/** @type {any} */ err) {
405
- throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
406
- }
407
- }
408
-
409
- /**
410
- * Attempts to encrypt the given `connection` with the provided `cryptos`.
411
- * The first `Crypto` module to succeed will be used
412
- *
413
- * @private
414
- * @async
415
- * @param {PeerId} localPeer - The initiators PeerId
416
- * @param {MultiaddrConnection} connection
417
- * @param {PeerId} remotePeerId
418
- * @param {Map<string, Crypto>} cryptos
419
- * @returns {Promise<CryptoResult>} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
420
- */
421
- async _encryptOutbound (localPeer, connection, remotePeerId, cryptos) {
422
- const mss = new Multistream.Dialer(connection)
423
- const protocols = Array.from(cryptos.keys())
424
- log('selecting outbound crypto protocol', protocols)
425
-
426
- try {
427
- const { stream, protocol } = await mss.select(protocols)
428
- const crypto = cryptos.get(protocol)
429
- log('encrypting outbound connection to %j', remotePeerId)
430
-
431
- if (!crypto) {
432
- throw new Error(`no crypto module found for ${protocol}`)
433
- }
434
-
435
- return {
436
- ...await crypto.secureOutbound(localPeer, stream, remotePeerId),
437
- protocol
438
- }
439
- } catch (/** @type {any} */ err) {
440
- throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
441
- }
442
- }
443
-
444
- /**
445
- * Selects one of the given muxers via multistream-select. That
446
- * muxer will be used for all future streams on the connection.
447
- *
448
- * @private
449
- * @async
450
- * @param {MultiaddrConnection} connection - A basic duplex connection to multiplex
451
- * @param {Map<string, MuxerFactory>} muxers - The muxers to attempt multiplexing with
452
- * @returns {Promise<{ stream: MuxedStream, Muxer?: MuxerFactory}>} A muxed connection
453
- */
454
- async _multiplexOutbound (connection, muxers) {
455
- const dialer = new Multistream.Dialer(connection)
456
- const protocols = Array.from(muxers.keys())
457
- log('outbound selecting muxer %s', protocols)
458
- try {
459
- const { stream, protocol } = await dialer.select(protocols)
460
- log('%s selected as muxer protocol', protocol)
461
- const Muxer = muxers.get(protocol)
462
- return { stream, Muxer }
463
- } catch (/** @type {any} */ err) {
464
- throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
465
- }
466
- }
467
-
468
- /**
469
- * Registers support for one of the given muxers via multistream-select. The
470
- * selected muxer will be used for all future streams on the connection.
471
- *
472
- * @private
473
- * @async
474
- * @param {MultiaddrConnection} connection - A basic duplex connection to multiplex
475
- * @param {Map<string, MuxerFactory>} muxers - The muxers to attempt multiplexing with
476
- * @returns {Promise<{ stream: MuxedStream, Muxer?: MuxerFactory}>} A muxed connection
477
- */
478
- async _multiplexInbound (connection, muxers) {
479
- const listener = new Multistream.Listener(connection)
480
- const protocols = Array.from(muxers.keys())
481
- log('inbound handling muxers %s', protocols)
482
- try {
483
- const { stream, protocol } = await listener.handle(protocols)
484
- const Muxer = muxers.get(protocol)
485
- return { stream, Muxer }
486
- } catch (/** @type {any} */ err) {
487
- throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
488
- }
489
- }
490
- }
491
-
492
- module.exports = Upgrader