libp2p 0.46.20 → 0.46.21-0b4a2ee79

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 (278) hide show
  1. package/README.md +53 -40
  2. package/dist/index.min.js +40 -28
  3. package/dist/src/address-manager/index.d.ts +3 -1
  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 +3 -1
  8. package/dist/src/components.d.ts.map +1 -1
  9. package/dist/src/components.js.map +1 -1
  10. package/dist/src/connection/index.d.ts +2 -2
  11. package/dist/src/connection/index.d.ts.map +1 -1
  12. package/dist/src/connection/index.js +9 -9
  13. package/dist/src/connection/index.js.map +1 -1
  14. package/dist/src/connection-manager/auto-dial.d.ts +1 -1
  15. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  16. package/dist/src/connection-manager/auto-dial.js +21 -21
  17. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  18. package/dist/src/connection-manager/connection-pruner.d.ts +1 -1
  19. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  20. package/dist/src/connection-manager/connection-pruner.js +8 -8
  21. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  22. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  23. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  24. package/dist/src/connection-manager/constants.defaults.js +0 -4
  25. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  26. package/dist/src/connection-manager/dial-queue.d.ts +1 -3
  27. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  28. package/dist/src/connection-manager/dial-queue.js +30 -37
  29. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  30. package/dist/src/connection-manager/index.d.ts +1 -8
  31. package/dist/src/connection-manager/index.d.ts.map +1 -1
  32. package/dist/src/connection-manager/index.js +16 -17
  33. package/dist/src/connection-manager/index.js.map +1 -1
  34. package/dist/src/content-routing/index.js +1 -1
  35. package/dist/src/content-routing/index.js.map +1 -1
  36. package/dist/src/errors.d.ts +0 -2
  37. package/dist/src/errors.d.ts.map +1 -1
  38. package/dist/src/errors.js +0 -2
  39. package/dist/src/errors.js.map +1 -1
  40. package/dist/src/get-peer.d.ts.map +1 -1
  41. package/dist/src/get-peer.js +0 -3
  42. package/dist/src/get-peer.js.map +1 -1
  43. package/dist/src/index.d.ts +5 -6
  44. package/dist/src/index.d.ts.map +1 -1
  45. package/dist/src/index.js.map +1 -1
  46. package/dist/src/libp2p.d.ts +1 -2
  47. package/dist/src/libp2p.d.ts.map +1 -1
  48. package/dist/src/libp2p.js +23 -54
  49. package/dist/src/libp2p.js.map +1 -1
  50. package/dist/src/peer-routing.d.ts +5 -1
  51. package/dist/src/peer-routing.d.ts.map +1 -1
  52. package/dist/src/peer-routing.js +11 -8
  53. package/dist/src/peer-routing.js.map +1 -1
  54. package/dist/src/registrar.d.ts +3 -0
  55. package/dist/src/registrar.d.ts.map +1 -1
  56. package/dist/src/registrar.js +3 -3
  57. package/dist/src/registrar.js.map +1 -1
  58. package/dist/src/transport-manager.d.ts +3 -1
  59. package/dist/src/transport-manager.d.ts.map +1 -1
  60. package/dist/src/transport-manager.js +10 -10
  61. package/dist/src/transport-manager.js.map +1 -1
  62. package/dist/src/upgrader.d.ts +2 -2
  63. package/dist/src/upgrader.d.ts.map +1 -1
  64. package/dist/src/upgrader.js +75 -37
  65. package/dist/src/upgrader.js.map +1 -1
  66. package/dist/src/version.d.ts +1 -1
  67. package/dist/src/version.d.ts.map +1 -1
  68. package/dist/src/version.js +1 -1
  69. package/dist/src/version.js.map +1 -1
  70. package/package.json +34 -106
  71. package/src/address-manager/index.ts +5 -5
  72. package/src/components.ts +3 -1
  73. package/src/connection/index.ts +9 -10
  74. package/src/connection-manager/auto-dial.ts +21 -21
  75. package/src/connection-manager/connection-pruner.ts +8 -8
  76. package/src/connection-manager/constants.defaults.ts +0 -5
  77. package/src/connection-manager/dial-queue.ts +29 -38
  78. package/src/connection-manager/index.ts +16 -25
  79. package/src/content-routing/index.ts +1 -1
  80. package/src/errors.ts +0 -2
  81. package/src/get-peer.ts +0 -4
  82. package/src/index.ts +6 -7
  83. package/src/libp2p.ts +23 -61
  84. package/src/peer-routing.ts +15 -10
  85. package/src/registrar.ts +6 -5
  86. package/src/transport-manager.ts +12 -12
  87. package/src/upgrader.ts +100 -40
  88. package/src/version.ts +1 -1
  89. package/dist/src/autonat/constants.d.ts +0 -18
  90. package/dist/src/autonat/constants.d.ts.map +0 -1
  91. package/dist/src/autonat/constants.js +0 -18
  92. package/dist/src/autonat/constants.js.map +0 -1
  93. package/dist/src/autonat/index.d.ts +0 -62
  94. package/dist/src/autonat/index.d.ts.map +0 -1
  95. package/dist/src/autonat/index.js +0 -447
  96. package/dist/src/autonat/index.js.map +0 -1
  97. package/dist/src/autonat/pb/index.d.ts +0 -57
  98. package/dist/src/autonat/pb/index.d.ts.map +0 -1
  99. package/dist/src/autonat/pb/index.js +0 -250
  100. package/dist/src/autonat/pb/index.js.map +0 -1
  101. package/dist/src/circuit-relay/constants.d.ts +0 -50
  102. package/dist/src/circuit-relay/constants.d.ts.map +0 -1
  103. package/dist/src/circuit-relay/constants.js +0 -56
  104. package/dist/src/circuit-relay/constants.js.map +0 -1
  105. package/dist/src/circuit-relay/index.d.ts +0 -55
  106. package/dist/src/circuit-relay/index.d.ts.map +0 -1
  107. package/dist/src/circuit-relay/index.js +0 -38
  108. package/dist/src/circuit-relay/index.js.map +0 -1
  109. package/dist/src/circuit-relay/pb/index.d.ts +0 -93
  110. package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
  111. package/dist/src/circuit-relay/pb/index.js +0 -425
  112. package/dist/src/circuit-relay/pb/index.js.map +0 -1
  113. package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
  114. package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
  115. package/dist/src/circuit-relay/server/advert-service.js +0 -73
  116. package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
  117. package/dist/src/circuit-relay/server/index.d.ts +0 -65
  118. package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
  119. package/dist/src/circuit-relay/server/index.js +0 -312
  120. package/dist/src/circuit-relay/server/index.js.map +0 -1
  121. package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
  122. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
  123. package/dist/src/circuit-relay/server/reservation-store.js +0 -65
  124. package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
  125. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
  126. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
  127. package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
  128. package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
  129. package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
  130. package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
  131. package/dist/src/circuit-relay/transport/discovery.js +0 -97
  132. package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
  133. package/dist/src/circuit-relay/transport/index.d.ts +0 -57
  134. package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
  135. package/dist/src/circuit-relay/transport/index.js +0 -276
  136. package/dist/src/circuit-relay/transport/index.js.map +0 -1
  137. package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
  138. package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
  139. package/dist/src/circuit-relay/transport/listener.js +0 -66
  140. package/dist/src/circuit-relay/transport/listener.js.map +0 -1
  141. package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
  142. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
  143. package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
  144. package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
  145. package/dist/src/circuit-relay/utils.d.ts +0 -13
  146. package/dist/src/circuit-relay/utils.d.ts.map +0 -1
  147. package/dist/src/circuit-relay/utils.js +0 -108
  148. package/dist/src/circuit-relay/utils.js.map +0 -1
  149. package/dist/src/dcutr/dcutr.d.ts +0 -43
  150. package/dist/src/dcutr/dcutr.d.ts.map +0 -1
  151. package/dist/src/dcutr/dcutr.js +0 -313
  152. package/dist/src/dcutr/dcutr.js.map +0 -1
  153. package/dist/src/dcutr/index.d.ts +0 -92
  154. package/dist/src/dcutr/index.d.ts.map +0 -1
  155. package/dist/src/dcutr/index.js +0 -63
  156. package/dist/src/dcutr/index.js.map +0 -1
  157. package/dist/src/dcutr/pb/message.d.ts +0 -20
  158. package/dist/src/dcutr/pb/message.d.ts.map +0 -1
  159. package/dist/src/dcutr/pb/message.js +0 -77
  160. package/dist/src/dcutr/pb/message.js.map +0 -1
  161. package/dist/src/dcutr/utils.d.ts +0 -8
  162. package/dist/src/dcutr/utils.d.ts.map +0 -1
  163. package/dist/src/dcutr/utils.js +0 -27
  164. package/dist/src/dcutr/utils.js.map +0 -1
  165. package/dist/src/fetch/constants.d.ts +0 -3
  166. package/dist/src/fetch/constants.d.ts.map +0 -1
  167. package/dist/src/fetch/constants.js +0 -4
  168. package/dist/src/fetch/constants.js.map +0 -1
  169. package/dist/src/fetch/index.d.ts +0 -61
  170. package/dist/src/fetch/index.d.ts.map +0 -1
  171. package/dist/src/fetch/index.js +0 -205
  172. package/dist/src/fetch/index.js.map +0 -1
  173. package/dist/src/fetch/pb/proto.d.ts +0 -28
  174. package/dist/src/fetch/pb/proto.d.ts.map +0 -1
  175. package/dist/src/fetch/pb/proto.js +0 -120
  176. package/dist/src/fetch/pb/proto.js.map +0 -1
  177. package/dist/src/identify/consts.d.ts +0 -10
  178. package/dist/src/identify/consts.d.ts.map +0 -1
  179. package/dist/src/identify/consts.js +0 -11
  180. package/dist/src/identify/consts.js.map +0 -1
  181. package/dist/src/identify/identify.d.ts +0 -54
  182. package/dist/src/identify/identify.d.ts.map +0 -1
  183. package/dist/src/identify/identify.js +0 -447
  184. package/dist/src/identify/identify.js.map +0 -1
  185. package/dist/src/identify/index.d.ts +0 -72
  186. package/dist/src/identify/index.d.ts.map +0 -1
  187. package/dist/src/identify/index.js +0 -15
  188. package/dist/src/identify/index.js.map +0 -1
  189. package/dist/src/identify/pb/message.d.ts +0 -17
  190. package/dist/src/identify/pb/message.d.ts.map +0 -1
  191. package/dist/src/identify/pb/message.js +0 -98
  192. package/dist/src/identify/pb/message.js.map +0 -1
  193. package/dist/src/insecure/index.d.ts +0 -26
  194. package/dist/src/insecure/index.d.ts.map +0 -1
  195. package/dist/src/insecure/index.js +0 -110
  196. package/dist/src/insecure/index.js.map +0 -1
  197. package/dist/src/insecure/pb/proto.d.ts +0 -30
  198. package/dist/src/insecure/pb/proto.d.ts.map +0 -1
  199. package/dist/src/insecure/pb/proto.js +0 -127
  200. package/dist/src/insecure/pb/proto.js.map +0 -1
  201. package/dist/src/ping/constants.d.ts +0 -9
  202. package/dist/src/ping/constants.d.ts.map +0 -1
  203. package/dist/src/ping/constants.js +0 -15
  204. package/dist/src/ping/constants.js.map +0 -1
  205. package/dist/src/ping/index.d.ts +0 -24
  206. package/dist/src/ping/index.d.ts.map +0 -1
  207. package/dist/src/ping/index.js +0 -115
  208. package/dist/src/ping/index.js.map +0 -1
  209. package/dist/src/pnet/crypto.d.ts +0 -18
  210. package/dist/src/pnet/crypto.d.ts.map +0 -1
  211. package/dist/src/pnet/crypto.js +0 -60
  212. package/dist/src/pnet/crypto.js.map +0 -1
  213. package/dist/src/pnet/errors.d.ts +0 -6
  214. package/dist/src/pnet/errors.d.ts.map +0 -1
  215. package/dist/src/pnet/errors.js +0 -6
  216. package/dist/src/pnet/errors.js.map +0 -1
  217. package/dist/src/pnet/index.d.ts +0 -31
  218. package/dist/src/pnet/index.d.ts.map +0 -1
  219. package/dist/src/pnet/index.js +0 -97
  220. package/dist/src/pnet/index.js.map +0 -1
  221. package/dist/src/pnet/key-generator.d.ts +0 -11
  222. package/dist/src/pnet/key-generator.d.ts.map +0 -1
  223. package/dist/src/pnet/key-generator.js +0 -22
  224. package/dist/src/pnet/key-generator.js.map +0 -1
  225. package/dist/src/upnp-nat/index.d.ts +0 -71
  226. package/dist/src/upnp-nat/index.d.ts.map +0 -1
  227. package/dist/src/upnp-nat/index.js +0 -136
  228. package/dist/src/upnp-nat/index.js.map +0 -1
  229. package/dist/src/utils/peer-job-queue.d.ts +0 -33
  230. package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
  231. package/dist/src/utils/peer-job-queue.js +0 -82
  232. package/dist/src/utils/peer-job-queue.js.map +0 -1
  233. package/dist/typedoc-urls.json +0 -71
  234. package/src/autonat/constants.ts +0 -19
  235. package/src/autonat/index.ts +0 -597
  236. package/src/autonat/pb/index.proto +0 -35
  237. package/src/autonat/pb/index.ts +0 -320
  238. package/src/circuit-relay/constants.ts +0 -72
  239. package/src/circuit-relay/index.ts +0 -59
  240. package/src/circuit-relay/pb/index.proto +0 -67
  241. package/src/circuit-relay/pb/index.ts +0 -539
  242. package/src/circuit-relay/server/advert-service.ts +0 -108
  243. package/src/circuit-relay/server/index.ts +0 -443
  244. package/src/circuit-relay/server/reservation-store.ts +0 -116
  245. package/src/circuit-relay/server/reservation-voucher.ts +0 -51
  246. package/src/circuit-relay/transport/discovery.ts +0 -136
  247. package/src/circuit-relay/transport/index.ts +0 -396
  248. package/src/circuit-relay/transport/listener.ts +0 -97
  249. package/src/circuit-relay/transport/reservation-store.ts +0 -312
  250. package/src/circuit-relay/utils.ts +0 -136
  251. package/src/dcutr/dcutr.ts +0 -378
  252. package/src/dcutr/index.ts +0 -102
  253. package/src/dcutr/pb/message.proto +0 -12
  254. package/src/dcutr/pb/message.ts +0 -96
  255. package/src/dcutr/utils.ts +0 -33
  256. package/src/fetch/README.md +0 -41
  257. package/src/fetch/constants.ts +0 -3
  258. package/src/fetch/index.ts +0 -315
  259. package/src/fetch/pb/proto.proto +0 -15
  260. package/src/fetch/pb/proto.ts +0 -153
  261. package/src/identify/README.md +0 -13
  262. package/src/identify/consts.ts +0 -12
  263. package/src/identify/identify.ts +0 -561
  264. package/src/identify/index.ts +0 -90
  265. package/src/identify/pb/message.proto +0 -30
  266. package/src/identify/pb/message.ts +0 -126
  267. package/src/insecure/index.ts +0 -138
  268. package/src/insecure/pb/proto.proto +0 -18
  269. package/src/insecure/pb/proto.ts +0 -161
  270. package/src/ping/constants.ts +0 -15
  271. package/src/ping/index.ts +0 -170
  272. package/src/pnet/README.md +0 -68
  273. package/src/pnet/crypto.ts +0 -67
  274. package/src/pnet/errors.ts +0 -5
  275. package/src/pnet/index.ts +0 -126
  276. package/src/pnet/key-generator.ts +0 -23
  277. package/src/upnp-nat/index.ts +0 -212
  278. 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'