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,561 +0,0 @@
1
- import { CodeError } from '@libp2p/interface/errors'
2
- import { setMaxListeners } from '@libp2p/interface/events'
3
- import { peerIdFromKeys } from '@libp2p/peer-id'
4
- import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
5
- import { type Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
6
- import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher'
7
- import { pbStream } from 'it-protobuf-stream'
8
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10
- import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
11
- import { codes } from '../errors.js'
12
- import {
13
- AGENT_VERSION,
14
- IDENTIFY_PROTOCOL_VERSION,
15
- MULTICODEC_IDENTIFY_PROTOCOL_NAME,
16
- MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
17
- MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
18
- MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
19
- } from './consts.js'
20
- import { Identify } from './pb/message.js'
21
- import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
22
- import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions, Logger } from '@libp2p/interface'
23
- import type { Connection, Stream } from '@libp2p/interface/connection'
24
- import type { TypedEventTarget } from '@libp2p/interface/events'
25
- import type { PeerId } from '@libp2p/interface/peer-id'
26
- import type { Peer, PeerData, PeerStore } from '@libp2p/interface/peer-store'
27
- import type { Startable } from '@libp2p/interface/startable'
28
- import type { AddressManager } from '@libp2p/interface-internal/address-manager'
29
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
30
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
31
-
32
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
33
- const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8
34
-
35
- const defaultValues = {
36
- protocolPrefix: 'ipfs',
37
- agentVersion: AGENT_VERSION,
38
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
39
- timeout: 60000,
40
- maxInboundStreams: 1,
41
- maxOutboundStreams: 1,
42
- maxPushIncomingStreams: 1,
43
- maxPushOutgoingStreams: 1,
44
- maxObservedAddresses: 10,
45
- maxIdentifyMessageSize: 8192,
46
- runOnConnectionOpen: true,
47
- runOnTransientConnection: true
48
- }
49
-
50
- export class DefaultIdentifyService implements Startable, IdentifyService {
51
- private readonly identifyProtocolStr: string
52
- private readonly identifyPushProtocolStr: string
53
- public readonly host: {
54
- protocolVersion: string
55
- agentVersion: string
56
- }
57
-
58
- private started: boolean
59
- private readonly timeout: number
60
- private readonly peerId: PeerId
61
- private readonly peerStore: PeerStore
62
- private readonly registrar: Registrar
63
- private readonly connectionManager: ConnectionManager
64
- private readonly addressManager: AddressManager
65
- private readonly maxInboundStreams: number
66
- private readonly maxOutboundStreams: number
67
- private readonly maxPushIncomingStreams: number
68
- private readonly maxPushOutgoingStreams: number
69
- private readonly maxIdentifyMessageSize: number
70
- private readonly maxObservedAddresses: number
71
- private readonly events: TypedEventTarget<Libp2pEvents>
72
- private readonly runOnTransientConnection: boolean
73
- readonly #log: Logger
74
-
75
- constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) {
76
- this.started = false
77
- this.peerId = components.peerId
78
- this.peerStore = components.peerStore
79
- this.registrar = components.registrar
80
- this.addressManager = components.addressManager
81
- this.connectionManager = components.connectionManager
82
- this.events = components.events
83
- this.#log = components.logger.forComponent('libp2p:identify')
84
-
85
- this.identifyProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
86
- this.identifyPushProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
87
- this.timeout = init.timeout ?? defaultValues.timeout
88
- this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams
89
- this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams
90
- this.maxPushIncomingStreams = init.maxPushIncomingStreams ?? defaultValues.maxPushIncomingStreams
91
- this.maxPushOutgoingStreams = init.maxPushOutgoingStreams ?? defaultValues.maxPushOutgoingStreams
92
- this.maxIdentifyMessageSize = init.maxIdentifyMessageSize ?? defaultValues.maxIdentifyMessageSize
93
- this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses
94
- this.runOnTransientConnection = init.runOnTransientConnection ?? defaultValues.runOnTransientConnection
95
-
96
- // Store self host metadata
97
- this.host = {
98
- protocolVersion: `${init.protocolPrefix ?? defaultValues.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
99
- agentVersion: init.agentVersion ?? defaultValues.agentVersion
100
- }
101
-
102
- if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) {
103
- // When a new connection happens, trigger identify
104
- components.events.addEventListener('connection:open', (evt) => {
105
- const connection = evt.detail
106
- this.identify(connection).catch(err => { this.#log.error('error during identify trigged by connection:open', err) })
107
- })
108
- }
109
-
110
- // When self peer record changes, trigger identify-push
111
- components.events.addEventListener('self:peer:update', (evt) => {
112
- void this.push().catch(err => { this.#log.error(err) })
113
- })
114
-
115
- // Append user agent version to default AGENT_VERSION depending on the environment
116
- if (this.host.agentVersion === AGENT_VERSION) {
117
- if (isNode || isElectronMain) {
118
- this.host.agentVersion += ` UserAgent=${globalThis.process.version}`
119
- } else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
120
- this.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
121
- }
122
- }
123
- }
124
-
125
- isStarted (): boolean {
126
- return this.started
127
- }
128
-
129
- async start (): Promise<void> {
130
- if (this.started) {
131
- return
132
- }
133
-
134
- await this.peerStore.merge(this.peerId, {
135
- metadata: {
136
- AgentVersion: uint8ArrayFromString(this.host.agentVersion),
137
- ProtocolVersion: uint8ArrayFromString(this.host.protocolVersion)
138
- }
139
- })
140
-
141
- await this.registrar.handle(this.identifyProtocolStr, (data) => {
142
- void this._handleIdentify(data).catch(err => {
143
- this.#log.error(err)
144
- })
145
- }, {
146
- maxInboundStreams: this.maxInboundStreams,
147
- maxOutboundStreams: this.maxOutboundStreams,
148
- runOnTransientConnection: this.runOnTransientConnection
149
- })
150
- await this.registrar.handle(this.identifyPushProtocolStr, (data) => {
151
- void this._handlePush(data).catch(err => {
152
- this.#log.error(err)
153
- })
154
- }, {
155
- maxInboundStreams: this.maxPushIncomingStreams,
156
- maxOutboundStreams: this.maxPushOutgoingStreams,
157
- runOnTransientConnection: this.runOnTransientConnection
158
- })
159
-
160
- this.started = true
161
- }
162
-
163
- async stop (): Promise<void> {
164
- await this.registrar.unhandle(this.identifyProtocolStr)
165
- await this.registrar.unhandle(this.identifyPushProtocolStr)
166
-
167
- this.started = false
168
- }
169
-
170
- /**
171
- * Send an Identify Push update to the list of connections
172
- */
173
- async pushToConnections (connections: Connection[]): Promise<void> {
174
- const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
175
- const peerRecord = new PeerRecord({
176
- peerId: this.peerId,
177
- multiaddrs: listenAddresses
178
- })
179
- const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId)
180
- const supportedProtocols = this.registrar.getProtocols()
181
- const peer = await this.peerStore.get(this.peerId)
182
- const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion))
183
- const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion))
184
-
185
- const pushes = connections.map(async connection => {
186
- let stream: Stream | undefined
187
-
188
- const signal = AbortSignal.timeout(this.timeout)
189
-
190
- setMaxListeners(Infinity, signal)
191
-
192
- try {
193
- stream = await connection.newStream([this.identifyPushProtocolStr], {
194
- signal,
195
- runOnTransientConnection: this.runOnTransientConnection
196
- })
197
-
198
- const pb = pbStream(stream, {
199
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
200
- }).pb(Identify)
201
-
202
- await pb.write({
203
- listenAddrs: listenAddresses.map(ma => ma.bytes),
204
- signedPeerRecord: signedPeerRecord.marshal(),
205
- protocols: supportedProtocols,
206
- agentVersion,
207
- protocolVersion
208
- }, {
209
- signal
210
- })
211
-
212
- await stream.close({
213
- signal
214
- })
215
- } catch (err: any) {
216
- // Just log errors
217
- this.#log.error('could not push identify update to peer', err)
218
- stream?.abort(err)
219
- }
220
- })
221
-
222
- await Promise.all(pushes)
223
- }
224
-
225
- /**
226
- * Calls `push` on all peer connections
227
- */
228
- async push (): Promise<void> {
229
- // Do not try to push if we are not running
230
- if (!this.isStarted()) {
231
- return
232
- }
233
-
234
- const connections: Connection[] = []
235
-
236
- await Promise.all(
237
- this.connectionManager.getConnections().map(async conn => {
238
- try {
239
- const peer = await this.peerStore.get(conn.remotePeer)
240
-
241
- if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
242
- return
243
- }
244
-
245
- connections.push(conn)
246
- } catch (err: any) {
247
- if (err.code !== codes.ERR_NOT_FOUND) {
248
- throw err
249
- }
250
- }
251
- })
252
- )
253
-
254
- await this.pushToConnections(connections)
255
- }
256
-
257
- async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
258
- let stream: Stream | undefined
259
-
260
- if (options.signal == null) {
261
- const signal = AbortSignal.timeout(this.timeout)
262
- setMaxListeners(Infinity, signal)
263
-
264
- options = {
265
- ...options,
266
- signal
267
- }
268
- }
269
-
270
- try {
271
- stream = await connection.newStream([this.identifyProtocolStr], {
272
- ...options,
273
- runOnTransientConnection: this.runOnTransientConnection
274
- })
275
-
276
- const pb = pbStream(stream, {
277
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
278
- }).pb(Identify)
279
-
280
- const message = await pb.read(options)
281
-
282
- await stream.close(options)
283
-
284
- return message
285
- } catch (err: any) {
286
- this.#log.error('error while reading identify message', err)
287
- stream?.abort(err)
288
- throw err
289
- }
290
- }
291
-
292
- async identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyResult> {
293
- const message = await this._identify(connection, options)
294
- const {
295
- publicKey,
296
- protocols,
297
- observedAddr
298
- } = message
299
-
300
- if (publicKey == null) {
301
- throw new CodeError('public key was missing from identify message', codes.ERR_MISSING_PUBLIC_KEY)
302
- }
303
-
304
- const id = await peerIdFromKeys(publicKey)
305
-
306
- if (!connection.remotePeer.equals(id)) {
307
- throw new CodeError('identified peer does not match the expected peer', codes.ERR_INVALID_PEER)
308
- }
309
-
310
- if (this.peerId.equals(id)) {
311
- throw new CodeError('identified peer is our own peer id?', codes.ERR_INVALID_PEER)
312
- }
313
-
314
- // Get the observedAddr if there is one
315
- const cleanObservedAddr = getCleanMultiaddr(observedAddr)
316
-
317
- this.#log('identify completed for peer %p and protocols %o', id, protocols)
318
- this.#log('our observed address is %a', cleanObservedAddr)
319
-
320
- if (cleanObservedAddr != null &&
321
- this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) {
322
- this.#log('storing our observed address %a', cleanObservedAddr)
323
- this.addressManager.addObservedAddr(cleanObservedAddr)
324
- }
325
-
326
- return this.#consumeIdentifyMessage(connection, message)
327
- }
328
-
329
- /**
330
- * Sends the `Identify` response with the Signed Peer Record
331
- * to the requesting peer over the given `connection`
332
- */
333
- async _handleIdentify (data: IncomingStreamData): Promise<void> {
334
- const { connection, stream } = data
335
-
336
- const signal = AbortSignal.timeout(this.timeout)
337
-
338
- setMaxListeners(Infinity, signal)
339
-
340
- try {
341
- const publicKey = this.peerId.publicKey ?? new Uint8Array(0)
342
- const peerData = await this.peerStore.get(this.peerId)
343
- const multiaddrs = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
344
- let signedPeerRecord = peerData.peerRecordEnvelope
345
-
346
- if (multiaddrs.length > 0 && signedPeerRecord == null) {
347
- const peerRecord = new PeerRecord({
348
- peerId: this.peerId,
349
- multiaddrs
350
- })
351
-
352
- const envelope = await RecordEnvelope.seal(peerRecord, this.peerId)
353
- signedPeerRecord = envelope.marshal().subarray()
354
- }
355
-
356
- let observedAddr: Uint8Array | undefined = connection.remoteAddr.bytes
357
-
358
- if (!IP_OR_DOMAIN.matches(connection.remoteAddr)) {
359
- observedAddr = undefined
360
- }
361
-
362
- const pb = pbStream(stream).pb(Identify)
363
-
364
- await pb.write({
365
- protocolVersion: this.host.protocolVersion,
366
- agentVersion: this.host.agentVersion,
367
- publicKey,
368
- listenAddrs: multiaddrs.map(addr => addr.bytes),
369
- signedPeerRecord,
370
- observedAddr,
371
- protocols: peerData.protocols
372
- }, {
373
- signal
374
- })
375
-
376
- await stream.close({
377
- signal
378
- })
379
- } catch (err: any) {
380
- this.#log.error('could not respond to identify request', err)
381
- stream.abort(err)
382
- }
383
- }
384
-
385
- /**
386
- * Reads the Identify Push message from the given `connection`
387
- */
388
- async _handlePush (data: IncomingStreamData): Promise<void> {
389
- const { connection, stream } = data
390
-
391
- try {
392
- if (this.peerId.equals(connection.remotePeer)) {
393
- throw new Error('received push from ourselves?')
394
- }
395
-
396
- const options = {
397
- signal: AbortSignal.timeout(this.timeout)
398
- }
399
-
400
- const pb = pbStream(stream, {
401
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
402
- }).pb(Identify)
403
-
404
- const message = await pb.read(options)
405
- await stream.close(options)
406
-
407
- await this.#consumeIdentifyMessage(connection, message)
408
- } catch (err: any) {
409
- this.#log.error('received invalid message', err)
410
- stream.abort(err)
411
- return
412
- }
413
-
414
- this.#log('handled push from %p', connection.remotePeer)
415
- }
416
-
417
- async #consumeIdentifyMessage (connection: Connection, message: Identify): Promise<IdentifyResult> {
418
- this.#log('received identify from %p', connection.remotePeer)
419
-
420
- if (message == null) {
421
- throw new CodeError('message was null or undefined', 'ERR_INVALID_MESSAGE')
422
- }
423
-
424
- const peer: PeerData = {}
425
-
426
- if (message.listenAddrs.length > 0) {
427
- peer.addresses = message.listenAddrs.map(buf => ({
428
- isCertified: false,
429
- multiaddr: multiaddr(buf)
430
- }))
431
- }
432
-
433
- if (message.protocols.length > 0) {
434
- peer.protocols = message.protocols
435
- }
436
-
437
- if (message.publicKey != null) {
438
- peer.publicKey = message.publicKey
439
-
440
- const peerId = await peerIdFromKeys(message.publicKey)
441
-
442
- if (!peerId.equals(connection.remotePeer)) {
443
- throw new CodeError('public key did not match remote PeerId', 'ERR_INVALID_PUBLIC_KEY')
444
- }
445
- }
446
-
447
- let output: SignedPeerRecord | undefined
448
-
449
- // if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer
450
- if (message.signedPeerRecord != null) {
451
- this.#log('received signedPeerRecord in push from %p', connection.remotePeer)
452
-
453
- let peerRecordEnvelope = message.signedPeerRecord
454
- const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN)
455
- let peerRecord = PeerRecord.createFromProtobuf(envelope.payload)
456
-
457
- // Verify peerId
458
- if (!peerRecord.peerId.equals(envelope.peerId)) {
459
- throw new CodeError('signing key does not match PeerId in the PeerRecord', 'ERR_INVALID_SIGNING_KEY')
460
- }
461
-
462
- // Make sure remote peer is the one sending the record
463
- if (!connection.remotePeer.equals(peerRecord.peerId)) {
464
- throw new CodeError('signing key does not match remote PeerId', 'ERR_INVALID_PEER_RECORD_KEY')
465
- }
466
-
467
- let existingPeer: Peer | undefined
468
-
469
- try {
470
- existingPeer = await this.peerStore.get(peerRecord.peerId)
471
- } catch (err: any) {
472
- if (err.code !== 'ERR_NOT_FOUND') {
473
- throw err
474
- }
475
- }
476
-
477
- if (existingPeer != null) {
478
- // don't lose any existing metadata
479
- peer.metadata = existingPeer.metadata
480
-
481
- // if we have previously received a signed record for this peer, compare it to the incoming one
482
- if (existingPeer.peerRecordEnvelope != null) {
483
- const storedEnvelope = await RecordEnvelope.createFromProtobuf(existingPeer.peerRecordEnvelope)
484
- const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
485
-
486
- // ensure seq is greater than, or equal to, the last received
487
- if (storedRecord.seqNumber >= peerRecord.seqNumber) {
488
- this.#log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
489
- peerRecord = storedRecord
490
- peerRecordEnvelope = existingPeer.peerRecordEnvelope
491
- }
492
- }
493
- }
494
-
495
- // store the signed record for next time
496
- peer.peerRecordEnvelope = peerRecordEnvelope
497
-
498
- // override the stored addresses with the signed multiaddrs
499
- peer.addresses = peerRecord.multiaddrs.map(multiaddr => ({
500
- isCertified: true,
501
- multiaddr
502
- }))
503
-
504
- output = {
505
- seq: peerRecord.seqNumber,
506
- addresses: peerRecord.multiaddrs
507
- }
508
- } else {
509
- this.#log('%p did not send a signed peer record', connection.remotePeer)
510
- }
511
-
512
- this.#log('patching %p with', connection.remotePeer, peer)
513
- await this.peerStore.patch(connection.remotePeer, peer)
514
-
515
- if (message.agentVersion != null || message.protocolVersion != null) {
516
- const metadata: Record<string, Uint8Array> = {}
517
-
518
- if (message.agentVersion != null) {
519
- metadata.AgentVersion = uint8ArrayFromString(message.agentVersion)
520
- }
521
-
522
- if (message.protocolVersion != null) {
523
- metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion)
524
- }
525
-
526
- this.#log('merging %p metadata', connection.remotePeer, metadata)
527
- await this.peerStore.merge(connection.remotePeer, {
528
- metadata
529
- })
530
- }
531
-
532
- const result: IdentifyResult = {
533
- peerId: connection.remotePeer,
534
- protocolVersion: message.protocolVersion,
535
- agentVersion: message.agentVersion,
536
- publicKey: message.publicKey,
537
- listenAddrs: message.listenAddrs.map(buf => multiaddr(buf)),
538
- observedAddr: message.observedAddr == null ? undefined : multiaddr(message.observedAddr),
539
- protocols: message.protocols,
540
- signedPeerRecord: output,
541
- connection
542
- }
543
-
544
- this.events.safeDispatchEvent('peer:identify', { detail: result })
545
-
546
- return result
547
- }
548
- }
549
-
550
- /**
551
- * Takes the `addr` and converts it to a Multiaddr if possible
552
- */
553
- function getCleanMultiaddr (addr: Uint8Array | string | null | undefined): Multiaddr | undefined {
554
- if (addr != null && addr.length > 0) {
555
- try {
556
- return multiaddr(addr)
557
- } catch {
558
-
559
- }
560
- }
561
- }
@@ -1,90 +0,0 @@
1
- import {
2
- MULTICODEC_IDENTIFY,
3
- MULTICODEC_IDENTIFY_PUSH
4
- } from './consts.js'
5
- import { DefaultIdentifyService } from './identify.js'
6
- import { Identify } from './pb/message.js'
7
- import type { AbortOptions, IdentifyResult, Libp2pEvents, ComponentLogger } from '@libp2p/interface'
8
- import type { TypedEventTarget } from '@libp2p/interface/events'
9
- import type { PeerId } from '@libp2p/interface/peer-id'
10
- import type { PeerStore } from '@libp2p/interface/peer-store'
11
- import type { Connection } from '@libp2p/interface/src/connection/index.js'
12
- import type { AddressManager } from '@libp2p/interface-internal/address-manager'
13
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
14
- import type { Registrar } from '@libp2p/interface-internal/registrar'
15
-
16
- export interface IdentifyServiceInit {
17
- /**
18
- * The prefix to use for the protocol (default: 'ipfs')
19
- */
20
- protocolPrefix?: string
21
-
22
- /**
23
- * What details we should send as part of an identify message
24
- */
25
- agentVersion?: string
26
-
27
- /**
28
- * How long we should wait for a remote peer to send their identify response
29
- */
30
- timeout?: number
31
-
32
- /**
33
- * Identify responses larger than this in bytes will be rejected (default: 8192)
34
- */
35
- maxIdentifyMessageSize?: number
36
-
37
- maxInboundStreams?: number
38
- maxOutboundStreams?: number
39
-
40
- maxPushIncomingStreams?: number
41
- maxPushOutgoingStreams?: number
42
- maxObservedAddresses?: number
43
-
44
- /**
45
- * Whether to automatically dial identify on newly opened connections (default: true)
46
- */
47
- runOnConnectionOpen?: boolean
48
-
49
- /**
50
- * Whether to run on connections with data or duration limits (default: true)
51
- */
52
- runOnTransientConnection?: boolean
53
- }
54
-
55
- export interface IdentifyServiceComponents {
56
- peerId: PeerId
57
- peerStore: PeerStore
58
- connectionManager: ConnectionManager
59
- registrar: Registrar
60
- addressManager: AddressManager
61
- events: TypedEventTarget<Libp2pEvents>
62
- logger: ComponentLogger
63
- }
64
-
65
- /**
66
- * The protocols the IdentifyService supports
67
- */
68
- export const multicodecs = {
69
- IDENTIFY: MULTICODEC_IDENTIFY,
70
- IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
71
- }
72
-
73
- export const Message = { Identify }
74
-
75
- export interface IdentifyService {
76
- /**
77
- * due to the default limits on inbound/outbound streams for this protocol,
78
- * invoking this method when runOnConnectionOpen is true can lead to unpredictable results
79
- * as streams may be closed by the local or the remote node.
80
- * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol,
81
- * you may be better off configuring a topology to be notified instead.
82
- */
83
- identify(connection: Connection, options?: AbortOptions): Promise<IdentifyResult>
84
-
85
- push(): Promise<void>
86
- }
87
-
88
- export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService {
89
- return (components) => new DefaultIdentifyService(components, init)
90
- }
@@ -1,30 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message Identify {
4
- // protocolVersion determines compatibility between peers
5
- optional string protocolVersion = 5; // e.g. ipfs/1.0.0
6
-
7
- // agentVersion is like a UserAgent string in browsers, or client version in bittorrent
8
- // includes the client name and client.
9
- optional string agentVersion = 6; // e.g. go-ipfs/0.1.0
10
-
11
- // publicKey is this node's public key (which also gives its node.ID)
12
- // - may not need to be sent, as secure channel implies it has been sent.
13
- // - then again, if we change / disable secure channel, may still want it.
14
- optional bytes publicKey = 1;
15
-
16
- // listenAddrs are the multiaddrs the sender node listens for open connections on
17
- repeated bytes listenAddrs = 2;
18
-
19
- // oservedAddr is the multiaddr of the remote endpoint that the sender node perceives
20
- // this is useful information to convey to the other side, as it helps the remote endpoint
21
- // determine whether its connection to the local peer goes through NAT.
22
- optional bytes observedAddr = 4;
23
-
24
- repeated string protocols = 3;
25
-
26
- // signedPeerRecord contains a serialized SignedEnvelope containing a PeerRecord,
27
- // signed by the sending node. It contains the same addresses as the listenAddrs field, but
28
- // in a form that lets us share authenticated addrs with other peers.
29
- optional bytes signedPeerRecord = 8;
30
- }