libp2p 0.46.21 → 1.0.0-551622a96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/README.md +51 -47
  2. package/dist/index.min.js +40 -28
  3. package/dist/src/address-manager/index.d.ts +4 -5
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +3 -3
  6. package/dist/src/address-manager/index.js.map +1 -1
  7. package/dist/src/components.d.ts +4 -15
  8. package/dist/src/components.d.ts.map +1 -1
  9. package/dist/src/components.js +2 -2
  10. package/dist/src/components.js.map +1 -1
  11. package/dist/src/config/connection-gater.browser.d.ts +1 -1
  12. package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
  13. package/dist/src/config/connection-gater.browser.js.map +1 -1
  14. package/dist/src/config/connection-gater.d.ts +1 -1
  15. package/dist/src/config/connection-gater.d.ts.map +1 -1
  16. package/dist/src/config.d.ts.map +1 -1
  17. package/dist/src/config.js +1 -2
  18. package/dist/src/config.js.map +1 -1
  19. package/dist/src/connection/index.d.ts +4 -6
  20. package/dist/src/connection/index.d.ts.map +1 -1
  21. package/dist/src/connection/index.js +11 -13
  22. package/dist/src/connection/index.js.map +1 -1
  23. package/dist/src/connection-manager/auto-dial.d.ts +3 -6
  24. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  25. package/dist/src/connection-manager/auto-dial.js +21 -21
  26. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  27. package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
  28. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  29. package/dist/src/connection-manager/connection-pruner.js +8 -8
  30. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  31. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  32. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  33. package/dist/src/connection-manager/constants.defaults.js +0 -4
  34. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  35. package/dist/src/connection-manager/dial-queue.d.ts +3 -10
  36. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  37. package/dist/src/connection-manager/dial-queue.js +30 -38
  38. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  39. package/dist/src/connection-manager/index.d.ts +3 -18
  40. package/dist/src/connection-manager/index.d.ts.map +1 -1
  41. package/dist/src/connection-manager/index.js +17 -18
  42. package/dist/src/connection-manager/index.js.map +1 -1
  43. package/dist/src/connection-manager/utils.js +1 -1
  44. package/dist/src/connection-manager/utils.js.map +1 -1
  45. package/dist/src/content-routing/index.d.ts +1 -5
  46. package/dist/src/content-routing/index.d.ts.map +1 -1
  47. package/dist/src/content-routing/index.js +2 -2
  48. package/dist/src/content-routing/index.js.map +1 -1
  49. package/dist/src/content-routing/utils.d.ts +1 -2
  50. package/dist/src/content-routing/utils.d.ts.map +1 -1
  51. package/dist/src/content-routing/utils.js +1 -1
  52. package/dist/src/content-routing/utils.js.map +1 -1
  53. package/dist/src/errors.d.ts +0 -2
  54. package/dist/src/errors.d.ts.map +1 -1
  55. package/dist/src/errors.js +0 -2
  56. package/dist/src/errors.js.map +1 -1
  57. package/dist/src/get-peer.d.ts +1 -1
  58. package/dist/src/get-peer.d.ts.map +1 -1
  59. package/dist/src/get-peer.js +1 -5
  60. package/dist/src/get-peer.js.map +1 -1
  61. package/dist/src/index.d.ts +5 -16
  62. package/dist/src/index.d.ts.map +1 -1
  63. package/dist/src/index.js.map +1 -1
  64. package/dist/src/libp2p.d.ts +4 -12
  65. package/dist/src/libp2p.d.ts.map +1 -1
  66. package/dist/src/libp2p.js +31 -65
  67. package/dist/src/libp2p.js.map +1 -1
  68. package/dist/src/peer-routing.d.ts +6 -6
  69. package/dist/src/peer-routing.d.ts.map +1 -1
  70. package/dist/src/peer-routing.js +12 -9
  71. package/dist/src/peer-routing.js.map +1 -1
  72. package/dist/src/registrar.d.ts +5 -7
  73. package/dist/src/registrar.d.ts.map +1 -1
  74. package/dist/src/registrar.js +4 -4
  75. package/dist/src/registrar.js.map +1 -1
  76. package/dist/src/transport-manager.d.ts +5 -9
  77. package/dist/src/transport-manager.d.ts.map +1 -1
  78. package/dist/src/transport-manager.js +12 -13
  79. package/dist/src/transport-manager.js.map +1 -1
  80. package/dist/src/upgrader.d.ts +8 -19
  81. package/dist/src/upgrader.d.ts.map +1 -1
  82. package/dist/src/upgrader.js +75 -38
  83. package/dist/src/upgrader.js.map +1 -1
  84. package/dist/src/version.d.ts +1 -1
  85. package/dist/src/version.d.ts.map +1 -1
  86. package/dist/src/version.js +1 -1
  87. package/dist/src/version.js.map +1 -1
  88. package/package.json +37 -106
  89. package/src/address-manager/index.ts +6 -9
  90. package/src/components.ts +5 -16
  91. package/src/config/connection-gater.browser.ts +1 -1
  92. package/src/config/connection-gater.ts +1 -1
  93. package/src/config.ts +1 -2
  94. package/src/connection/index.ts +12 -17
  95. package/src/connection-manager/auto-dial.ts +23 -26
  96. package/src/connection-manager/connection-pruner.ts +10 -12
  97. package/src/connection-manager/constants.defaults.ts +0 -5
  98. package/src/connection-manager/dial-queue.ts +31 -46
  99. package/src/connection-manager/index.ts +19 -36
  100. package/src/connection-manager/utils.ts +1 -1
  101. package/src/content-routing/index.ts +3 -7
  102. package/src/content-routing/utils.ts +2 -3
  103. package/src/errors.ts +0 -2
  104. package/src/get-peer.ts +2 -7
  105. package/src/index.ts +6 -17
  106. package/src/libp2p.ts +32 -80
  107. package/src/peer-routing.ts +16 -15
  108. package/src/registrar.ts +8 -12
  109. package/src/transport-manager.ts +15 -22
  110. package/src/upgrader.ts +110 -64
  111. package/src/version.ts +1 -1
  112. package/dist/src/autonat/constants.d.ts +0 -18
  113. package/dist/src/autonat/constants.d.ts.map +0 -1
  114. package/dist/src/autonat/constants.js +0 -18
  115. package/dist/src/autonat/constants.js.map +0 -1
  116. package/dist/src/autonat/index.d.ts +0 -62
  117. package/dist/src/autonat/index.d.ts.map +0 -1
  118. package/dist/src/autonat/index.js +0 -447
  119. package/dist/src/autonat/index.js.map +0 -1
  120. package/dist/src/autonat/pb/index.d.ts +0 -57
  121. package/dist/src/autonat/pb/index.d.ts.map +0 -1
  122. package/dist/src/autonat/pb/index.js +0 -250
  123. package/dist/src/autonat/pb/index.js.map +0 -1
  124. package/dist/src/circuit-relay/constants.d.ts +0 -50
  125. package/dist/src/circuit-relay/constants.d.ts.map +0 -1
  126. package/dist/src/circuit-relay/constants.js +0 -56
  127. package/dist/src/circuit-relay/constants.js.map +0 -1
  128. package/dist/src/circuit-relay/index.d.ts +0 -55
  129. package/dist/src/circuit-relay/index.d.ts.map +0 -1
  130. package/dist/src/circuit-relay/index.js +0 -38
  131. package/dist/src/circuit-relay/index.js.map +0 -1
  132. package/dist/src/circuit-relay/pb/index.d.ts +0 -93
  133. package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
  134. package/dist/src/circuit-relay/pb/index.js +0 -425
  135. package/dist/src/circuit-relay/pb/index.js.map +0 -1
  136. package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
  137. package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
  138. package/dist/src/circuit-relay/server/advert-service.js +0 -73
  139. package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
  140. package/dist/src/circuit-relay/server/index.d.ts +0 -65
  141. package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
  142. package/dist/src/circuit-relay/server/index.js +0 -312
  143. package/dist/src/circuit-relay/server/index.js.map +0 -1
  144. package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
  145. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
  146. package/dist/src/circuit-relay/server/reservation-store.js +0 -65
  147. package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
  148. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
  149. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
  150. package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
  151. package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
  152. package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
  153. package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
  154. package/dist/src/circuit-relay/transport/discovery.js +0 -97
  155. package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
  156. package/dist/src/circuit-relay/transport/index.d.ts +0 -57
  157. package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
  158. package/dist/src/circuit-relay/transport/index.js +0 -276
  159. package/dist/src/circuit-relay/transport/index.js.map +0 -1
  160. package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
  161. package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
  162. package/dist/src/circuit-relay/transport/listener.js +0 -66
  163. package/dist/src/circuit-relay/transport/listener.js.map +0 -1
  164. package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
  165. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
  166. package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
  167. package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
  168. package/dist/src/circuit-relay/utils.d.ts +0 -13
  169. package/dist/src/circuit-relay/utils.d.ts.map +0 -1
  170. package/dist/src/circuit-relay/utils.js +0 -108
  171. package/dist/src/circuit-relay/utils.js.map +0 -1
  172. package/dist/src/dcutr/dcutr.d.ts +0 -43
  173. package/dist/src/dcutr/dcutr.d.ts.map +0 -1
  174. package/dist/src/dcutr/dcutr.js +0 -313
  175. package/dist/src/dcutr/dcutr.js.map +0 -1
  176. package/dist/src/dcutr/index.d.ts +0 -92
  177. package/dist/src/dcutr/index.d.ts.map +0 -1
  178. package/dist/src/dcutr/index.js +0 -63
  179. package/dist/src/dcutr/index.js.map +0 -1
  180. package/dist/src/dcutr/pb/message.d.ts +0 -20
  181. package/dist/src/dcutr/pb/message.d.ts.map +0 -1
  182. package/dist/src/dcutr/pb/message.js +0 -77
  183. package/dist/src/dcutr/pb/message.js.map +0 -1
  184. package/dist/src/dcutr/utils.d.ts +0 -8
  185. package/dist/src/dcutr/utils.d.ts.map +0 -1
  186. package/dist/src/dcutr/utils.js +0 -27
  187. package/dist/src/dcutr/utils.js.map +0 -1
  188. package/dist/src/fetch/constants.d.ts +0 -3
  189. package/dist/src/fetch/constants.d.ts.map +0 -1
  190. package/dist/src/fetch/constants.js +0 -4
  191. package/dist/src/fetch/constants.js.map +0 -1
  192. package/dist/src/fetch/index.d.ts +0 -61
  193. package/dist/src/fetch/index.d.ts.map +0 -1
  194. package/dist/src/fetch/index.js +0 -205
  195. package/dist/src/fetch/index.js.map +0 -1
  196. package/dist/src/fetch/pb/proto.d.ts +0 -28
  197. package/dist/src/fetch/pb/proto.d.ts.map +0 -1
  198. package/dist/src/fetch/pb/proto.js +0 -120
  199. package/dist/src/fetch/pb/proto.js.map +0 -1
  200. package/dist/src/identify/consts.d.ts +0 -10
  201. package/dist/src/identify/consts.d.ts.map +0 -1
  202. package/dist/src/identify/consts.js +0 -11
  203. package/dist/src/identify/consts.js.map +0 -1
  204. package/dist/src/identify/identify.d.ts +0 -54
  205. package/dist/src/identify/identify.d.ts.map +0 -1
  206. package/dist/src/identify/identify.js +0 -447
  207. package/dist/src/identify/identify.js.map +0 -1
  208. package/dist/src/identify/index.d.ts +0 -72
  209. package/dist/src/identify/index.d.ts.map +0 -1
  210. package/dist/src/identify/index.js +0 -15
  211. package/dist/src/identify/index.js.map +0 -1
  212. package/dist/src/identify/pb/message.d.ts +0 -17
  213. package/dist/src/identify/pb/message.d.ts.map +0 -1
  214. package/dist/src/identify/pb/message.js +0 -98
  215. package/dist/src/identify/pb/message.js.map +0 -1
  216. package/dist/src/insecure/index.d.ts +0 -26
  217. package/dist/src/insecure/index.d.ts.map +0 -1
  218. package/dist/src/insecure/index.js +0 -110
  219. package/dist/src/insecure/index.js.map +0 -1
  220. package/dist/src/insecure/pb/proto.d.ts +0 -30
  221. package/dist/src/insecure/pb/proto.d.ts.map +0 -1
  222. package/dist/src/insecure/pb/proto.js +0 -127
  223. package/dist/src/insecure/pb/proto.js.map +0 -1
  224. package/dist/src/ping/constants.d.ts +0 -9
  225. package/dist/src/ping/constants.d.ts.map +0 -1
  226. package/dist/src/ping/constants.js +0 -15
  227. package/dist/src/ping/constants.js.map +0 -1
  228. package/dist/src/ping/index.d.ts +0 -24
  229. package/dist/src/ping/index.d.ts.map +0 -1
  230. package/dist/src/ping/index.js +0 -115
  231. package/dist/src/ping/index.js.map +0 -1
  232. package/dist/src/pnet/crypto.d.ts +0 -18
  233. package/dist/src/pnet/crypto.d.ts.map +0 -1
  234. package/dist/src/pnet/crypto.js +0 -60
  235. package/dist/src/pnet/crypto.js.map +0 -1
  236. package/dist/src/pnet/errors.d.ts +0 -6
  237. package/dist/src/pnet/errors.d.ts.map +0 -1
  238. package/dist/src/pnet/errors.js +0 -6
  239. package/dist/src/pnet/errors.js.map +0 -1
  240. package/dist/src/pnet/index.d.ts +0 -31
  241. package/dist/src/pnet/index.d.ts.map +0 -1
  242. package/dist/src/pnet/index.js +0 -97
  243. package/dist/src/pnet/index.js.map +0 -1
  244. package/dist/src/pnet/key-generator.d.ts +0 -11
  245. package/dist/src/pnet/key-generator.d.ts.map +0 -1
  246. package/dist/src/pnet/key-generator.js +0 -22
  247. package/dist/src/pnet/key-generator.js.map +0 -1
  248. package/dist/src/upnp-nat/index.d.ts +0 -71
  249. package/dist/src/upnp-nat/index.d.ts.map +0 -1
  250. package/dist/src/upnp-nat/index.js +0 -136
  251. package/dist/src/upnp-nat/index.js.map +0 -1
  252. package/dist/src/utils/peer-job-queue.d.ts +0 -33
  253. package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
  254. package/dist/src/utils/peer-job-queue.js +0 -82
  255. package/dist/src/utils/peer-job-queue.js.map +0 -1
  256. package/dist/typedoc-urls.json +0 -71
  257. package/src/autonat/constants.ts +0 -19
  258. package/src/autonat/index.ts +0 -597
  259. package/src/autonat/pb/index.proto +0 -35
  260. package/src/autonat/pb/index.ts +0 -320
  261. package/src/circuit-relay/constants.ts +0 -72
  262. package/src/circuit-relay/index.ts +0 -59
  263. package/src/circuit-relay/pb/index.proto +0 -67
  264. package/src/circuit-relay/pb/index.ts +0 -539
  265. package/src/circuit-relay/server/advert-service.ts +0 -108
  266. package/src/circuit-relay/server/index.ts +0 -443
  267. package/src/circuit-relay/server/reservation-store.ts +0 -116
  268. package/src/circuit-relay/server/reservation-voucher.ts +0 -51
  269. package/src/circuit-relay/transport/discovery.ts +0 -136
  270. package/src/circuit-relay/transport/index.ts +0 -396
  271. package/src/circuit-relay/transport/listener.ts +0 -97
  272. package/src/circuit-relay/transport/reservation-store.ts +0 -312
  273. package/src/circuit-relay/utils.ts +0 -136
  274. package/src/dcutr/dcutr.ts +0 -378
  275. package/src/dcutr/index.ts +0 -102
  276. package/src/dcutr/pb/message.proto +0 -12
  277. package/src/dcutr/pb/message.ts +0 -96
  278. package/src/dcutr/utils.ts +0 -33
  279. package/src/fetch/README.md +0 -41
  280. package/src/fetch/constants.ts +0 -3
  281. package/src/fetch/index.ts +0 -315
  282. package/src/fetch/pb/proto.proto +0 -15
  283. package/src/fetch/pb/proto.ts +0 -153
  284. package/src/identify/README.md +0 -13
  285. package/src/identify/consts.ts +0 -12
  286. package/src/identify/identify.ts +0 -561
  287. package/src/identify/index.ts +0 -90
  288. package/src/identify/pb/message.proto +0 -30
  289. package/src/identify/pb/message.ts +0 -126
  290. package/src/insecure/index.ts +0 -138
  291. package/src/insecure/pb/proto.proto +0 -18
  292. package/src/insecure/pb/proto.ts +0 -161
  293. package/src/ping/constants.ts +0 -15
  294. package/src/ping/index.ts +0 -170
  295. package/src/pnet/README.md +0 -68
  296. package/src/pnet/crypto.ts +0 -67
  297. package/src/pnet/errors.ts +0 -5
  298. package/src/pnet/index.ts +0 -126
  299. package/src/pnet/key-generator.ts +0 -23
  300. package/src/upnp-nat/index.ts +0 -212
  301. package/src/utils/peer-job-queue.ts +0 -119
@@ -1,315 +0,0 @@
1
- import { CodeError } from '@libp2p/interface/errors'
2
- import { setMaxListeners } from '@libp2p/interface/events'
3
- import { logger } from '@libp2p/logger'
4
- import first from 'it-first'
5
- import * as lp from 'it-length-prefixed'
6
- import { pipe } from 'it-pipe'
7
- import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
8
- import { toString as uint8arrayToString } from 'uint8arrays/to-string'
9
- import { codes } from '../errors.js'
10
- import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
11
- import { FetchRequest, FetchResponse } from './pb/proto.js'
12
- import type { AbortOptions } from '@libp2p/interface'
13
- import type { Stream } from '@libp2p/interface/connection'
14
- import type { PeerId } from '@libp2p/interface/peer-id'
15
- import type { Startable } from '@libp2p/interface/startable'
16
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
17
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
18
-
19
- const log = logger('libp2p:fetch')
20
-
21
- const DEFAULT_TIMEOUT = 10000
22
-
23
- export interface FetchServiceInit {
24
- protocolPrefix?: string
25
- maxInboundStreams?: number
26
- maxOutboundStreams?: number
27
-
28
- /**
29
- * How long we should wait for a remote peer to send any data
30
- */
31
- timeout?: number
32
- }
33
-
34
- export interface HandleMessageOptions {
35
- stream: Stream
36
- protocol: string
37
- }
38
-
39
- export interface LookupFunction {
40
- (key: string): Promise<Uint8Array | null>
41
- }
42
-
43
- export interface FetchServiceComponents {
44
- registrar: Registrar
45
- connectionManager: ConnectionManager
46
- }
47
-
48
- export interface FetchService {
49
- /**
50
- * The protocol name used by this fetch service
51
- */
52
- protocol: string
53
-
54
- /**
55
- * Sends a request to fetch the value associated with the given key from the given peer
56
- */
57
- fetch(peer: PeerId, key: string, options?: AbortOptions): Promise<Uint8Array | null>
58
-
59
- /**
60
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
61
- * share the same prefix
62
- *
63
- * @example
64
- *
65
- * ```js
66
- * // ...
67
- * libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
68
- * ```
69
- */
70
- registerLookupFunction(prefix: string, lookup: LookupFunction): void
71
-
72
- /**
73
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
74
- * share the same prefix.
75
- *
76
- * @example
77
- *
78
- * ```js
79
- * // ...
80
- * libp2p.fetchService.unregisterLookupFunction('/prefix')
81
- * ```
82
- */
83
- unregisterLookupFunction(prefix: string, lookup?: LookupFunction): void
84
- }
85
-
86
- /**
87
- * A simple libp2p protocol for requesting a value corresponding to a key from a peer.
88
- * Developers can register one or more lookup function for retrieving the value corresponding to
89
- * a given key. Each lookup function must act on a distinct part of the overall key space, defined
90
- * by a fixed prefix that all keys that should be routed to that lookup function will start with.
91
- */
92
- class DefaultFetchService implements Startable, FetchService {
93
- public readonly protocol: string
94
- private readonly components: FetchServiceComponents
95
- private readonly lookupFunctions: Map<string, LookupFunction>
96
- private started: boolean
97
- private readonly init: FetchServiceInit
98
-
99
- constructor (components: FetchServiceComponents, init: FetchServiceInit) {
100
- this.started = false
101
- this.components = components
102
- this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
103
- this.lookupFunctions = new Map() // Maps key prefix to value lookup function
104
- this.handleMessage = this.handleMessage.bind(this)
105
- this.init = init
106
- }
107
-
108
- async start (): Promise<void> {
109
- await this.components.registrar.handle(this.protocol, (data) => {
110
- void this.handleMessage(data)
111
- .then(async () => {
112
- await data.stream.close()
113
- })
114
- .catch(err => {
115
- log.error(err)
116
- })
117
- }, {
118
- maxInboundStreams: this.init.maxInboundStreams,
119
- maxOutboundStreams: this.init.maxOutboundStreams
120
- })
121
- this.started = true
122
- }
123
-
124
- async stop (): Promise<void> {
125
- await this.components.registrar.unhandle(this.protocol)
126
- this.started = false
127
- }
128
-
129
- isStarted (): boolean {
130
- return this.started
131
- }
132
-
133
- /**
134
- * Sends a request to fetch the value associated with the given key from the given peer
135
- */
136
- async fetch (peer: PeerId, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
137
- log('dialing %s to %p', this.protocol, peer)
138
-
139
- const connection = await this.components.connectionManager.openConnection(peer, options)
140
- let signal = options.signal
141
- let stream: Stream | undefined
142
- let onAbort = (): void => {}
143
-
144
- // create a timeout if no abort signal passed
145
- if (signal == null) {
146
- log('using default timeout of %d ms', this.init.timeout)
147
- signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
148
-
149
- setMaxListeners(Infinity, signal)
150
- }
151
-
152
- try {
153
- stream = await connection.newStream(this.protocol, {
154
- signal
155
- })
156
-
157
- onAbort = () => {
158
- stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT))
159
- }
160
-
161
- // make stream abortable
162
- signal.addEventListener('abort', onAbort, { once: true })
163
-
164
- log('fetch %s', key)
165
-
166
- const result = await pipe(
167
- [FetchRequest.encode({ identifier: key })],
168
- (source) => lp.encode(source),
169
- stream,
170
- (source) => lp.decode(source),
171
- async function (source) {
172
- const buf = await first(source)
173
-
174
- if (buf == null) {
175
- throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
176
- }
177
-
178
- const response = FetchResponse.decode(buf)
179
-
180
- switch (response.status) {
181
- case (FetchResponse.StatusCode.OK): {
182
- log('received status for %s ok', key)
183
- return response.data
184
- }
185
- case (FetchResponse.StatusCode.NOT_FOUND): {
186
- log('received status for %s not found', key)
187
- return null
188
- }
189
- case (FetchResponse.StatusCode.ERROR): {
190
- log('received status for %s error', key)
191
- const errmsg = uint8arrayToString(response.data)
192
- throw new CodeError('Error in fetch protocol response: ' + errmsg, codes.ERR_INVALID_PARAMETERS)
193
- }
194
- default: {
195
- log('received status for %s unknown', key)
196
- throw new CodeError('Unknown response status', codes.ERR_INVALID_MESSAGE)
197
- }
198
- }
199
- }
200
- )
201
-
202
- return result ?? null
203
- } finally {
204
- signal.removeEventListener('abort', onAbort)
205
- if (stream != null) {
206
- await stream.close()
207
- }
208
- }
209
- }
210
-
211
- /**
212
- * Invoked when a fetch request is received. Reads the request message off the given stream and
213
- * responds based on looking up the key in the request via the lookup callback that corresponds
214
- * to the key's prefix.
215
- */
216
- async handleMessage (data: IncomingStreamData): Promise<void> {
217
- const { stream } = data
218
- const self = this
219
-
220
- await pipe(
221
- stream,
222
- (source) => lp.decode(source),
223
- async function * (source) {
224
- const buf = await first(source)
225
-
226
- if (buf == null) {
227
- throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
228
- }
229
-
230
- // for await (const buf of source) {
231
- const request = FetchRequest.decode(buf)
232
-
233
- let response: FetchResponse
234
- const lookup = self._getLookupFunction(request.identifier)
235
- if (lookup != null) {
236
- log('look up data with identifier %s', request.identifier)
237
- const data = await lookup(request.identifier)
238
- if (data != null) {
239
- log('sending status for %s ok', request.identifier)
240
- response = { status: FetchResponse.StatusCode.OK, data }
241
- } else {
242
- log('sending status for %s not found', request.identifier)
243
- response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
244
- }
245
- } else {
246
- log('sending status for %s error', request.identifier)
247
- const errmsg = uint8arrayFromString(`No lookup function registered for key: ${request.identifier}`)
248
- response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }
249
- }
250
-
251
- yield FetchResponse.encode(response)
252
- },
253
- (source) => lp.encode(source),
254
- stream
255
- )
256
- }
257
-
258
- /**
259
- * Given a key, finds the appropriate function for looking up its corresponding value, based on
260
- * the key's prefix.
261
- */
262
- _getLookupFunction (key: string): LookupFunction | undefined {
263
- for (const prefix of this.lookupFunctions.keys()) {
264
- if (key.startsWith(prefix)) {
265
- return this.lookupFunctions.get(prefix)
266
- }
267
- }
268
- }
269
-
270
- /**
271
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
272
- * share the same prefix
273
- *
274
- * @example
275
- *
276
- * ```js
277
- * // ...
278
- * libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
279
- * ```
280
- */
281
- registerLookupFunction (prefix: string, lookup: LookupFunction): void {
282
- if (this.lookupFunctions.has(prefix)) {
283
- throw new CodeError(`Fetch protocol handler for key prefix '${prefix}' already registered`, codes.ERR_KEY_ALREADY_EXISTS)
284
- }
285
-
286
- this.lookupFunctions.set(prefix, lookup)
287
- }
288
-
289
- /**
290
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
291
- * share the same prefix.
292
- *
293
- * @example
294
- *
295
- * ```js
296
- * // ...
297
- * libp2p.fetchService.unregisterLookupFunction('/prefix')
298
- * ```
299
- */
300
- unregisterLookupFunction (prefix: string, lookup?: LookupFunction): void {
301
- if (lookup != null) {
302
- const existingLookup = this.lookupFunctions.get(prefix)
303
-
304
- if (existingLookup !== lookup) {
305
- return
306
- }
307
- }
308
-
309
- this.lookupFunctions.delete(prefix)
310
- }
311
- }
312
-
313
- export function fetchService (init: FetchServiceInit = {}): (components: FetchServiceComponents) => FetchService {
314
- return (components) => new DefaultFetchService(components, init)
315
- }
@@ -1,15 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message FetchRequest {
4
- string identifier = 1;
5
- }
6
-
7
- message FetchResponse {
8
- StatusCode status = 1;
9
- enum StatusCode {
10
- OK = 0;
11
- NOT_FOUND = 1;
12
- ERROR = 2;
13
- }
14
- bytes data = 2;
15
- }
@@ -1,153 +0,0 @@
1
- /* eslint-disable import/export */
2
- /* eslint-disable complexity */
3
- /* eslint-disable @typescript-eslint/no-namespace */
4
- /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
- /* eslint-disable @typescript-eslint/no-empty-interface */
6
-
7
- import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
8
- import type { Codec } from 'protons-runtime'
9
- import type { Uint8ArrayList } from 'uint8arraylist'
10
-
11
- export interface FetchRequest {
12
- identifier: string
13
- }
14
-
15
- export namespace FetchRequest {
16
- let _codec: Codec<FetchRequest>
17
-
18
- export const codec = (): Codec<FetchRequest> => {
19
- if (_codec == null) {
20
- _codec = message<FetchRequest>((obj, w, opts = {}) => {
21
- if (opts.lengthDelimited !== false) {
22
- w.fork()
23
- }
24
-
25
- if ((obj.identifier != null && obj.identifier !== '')) {
26
- w.uint32(10)
27
- w.string(obj.identifier)
28
- }
29
-
30
- if (opts.lengthDelimited !== false) {
31
- w.ldelim()
32
- }
33
- }, (reader, length) => {
34
- const obj: any = {
35
- identifier: ''
36
- }
37
-
38
- const end = length == null ? reader.len : reader.pos + length
39
-
40
- while (reader.pos < end) {
41
- const tag = reader.uint32()
42
-
43
- switch (tag >>> 3) {
44
- case 1:
45
- obj.identifier = reader.string()
46
- break
47
- default:
48
- reader.skipType(tag & 7)
49
- break
50
- }
51
- }
52
-
53
- return obj
54
- })
55
- }
56
-
57
- return _codec
58
- }
59
-
60
- export const encode = (obj: Partial<FetchRequest>): Uint8Array => {
61
- return encodeMessage(obj, FetchRequest.codec())
62
- }
63
-
64
- export const decode = (buf: Uint8Array | Uint8ArrayList): FetchRequest => {
65
- return decodeMessage(buf, FetchRequest.codec())
66
- }
67
- }
68
-
69
- export interface FetchResponse {
70
- status: FetchResponse.StatusCode
71
- data: Uint8Array
72
- }
73
-
74
- export namespace FetchResponse {
75
- export enum StatusCode {
76
- OK = 'OK',
77
- NOT_FOUND = 'NOT_FOUND',
78
- ERROR = 'ERROR'
79
- }
80
-
81
- enum __StatusCodeValues {
82
- OK = 0,
83
- NOT_FOUND = 1,
84
- ERROR = 2
85
- }
86
-
87
- export namespace StatusCode {
88
- export const codec = (): Codec<StatusCode> => {
89
- return enumeration<StatusCode>(__StatusCodeValues)
90
- }
91
- }
92
-
93
- let _codec: Codec<FetchResponse>
94
-
95
- export const codec = (): Codec<FetchResponse> => {
96
- if (_codec == null) {
97
- _codec = message<FetchResponse>((obj, w, opts = {}) => {
98
- if (opts.lengthDelimited !== false) {
99
- w.fork()
100
- }
101
-
102
- if (obj.status != null && __StatusCodeValues[obj.status] !== 0) {
103
- w.uint32(8)
104
- FetchResponse.StatusCode.codec().encode(obj.status, w)
105
- }
106
-
107
- if ((obj.data != null && obj.data.byteLength > 0)) {
108
- w.uint32(18)
109
- w.bytes(obj.data)
110
- }
111
-
112
- if (opts.lengthDelimited !== false) {
113
- w.ldelim()
114
- }
115
- }, (reader, length) => {
116
- const obj: any = {
117
- status: StatusCode.OK,
118
- data: new Uint8Array(0)
119
- }
120
-
121
- const end = length == null ? reader.len : reader.pos + length
122
-
123
- while (reader.pos < end) {
124
- const tag = reader.uint32()
125
-
126
- switch (tag >>> 3) {
127
- case 1:
128
- obj.status = FetchResponse.StatusCode.codec().decode(reader)
129
- break
130
- case 2:
131
- obj.data = reader.bytes()
132
- break
133
- default:
134
- reader.skipType(tag & 7)
135
- break
136
- }
137
- }
138
-
139
- return obj
140
- })
141
- }
142
-
143
- return _codec
144
- }
145
-
146
- export const encode = (obj: Partial<FetchResponse>): Uint8Array => {
147
- return encodeMessage(obj, FetchResponse.codec())
148
- }
149
-
150
- export const decode = (buf: Uint8Array | Uint8ArrayList): FetchResponse => {
151
- return decodeMessage(buf, FetchResponse.codec())
152
- }
153
- }
@@ -1,13 +0,0 @@
1
- # js-libp2p-identify
2
-
3
- > libp2p Identify Protocol
4
-
5
- **Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-identify.
6
-
7
- ## Description
8
-
9
- Identify is a STUN protocol, used by libp2p in order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
10
-
11
- ## How does it work
12
-
13
- The spec for Identify and Identify Push is at [libp2p/specs](https://github.com/libp2p/specs/tree/master/identify).
@@ -1,12 +0,0 @@
1
- import { version } from '../version.js'
2
-
3
- export const PROTOCOL_VERSION = 'ipfs/0.1.0' // deprecated
4
- export const AGENT_VERSION = `js-libp2p/${version}`
5
- export const MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0' // deprecated
6
- export const MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0' // deprecated
7
-
8
- export const IDENTIFY_PROTOCOL_VERSION = '0.1.0'
9
- export const MULTICODEC_IDENTIFY_PROTOCOL_NAME = 'id'
10
- export const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME = 'id/push'
11
- export const MULTICODEC_IDENTIFY_PROTOCOL_VERSION = '1.0.0'
12
- export const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION = '1.0.0'