libp2p 0.44.0 → 0.45.0-08d4c261

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 (238) hide show
  1. package/dist/index.min.js +32 -32
  2. package/dist/src/address-manager/index.d.ts +9 -5
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +44 -7
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/address-manager/utils.d.ts +2 -0
  7. package/dist/src/address-manager/utils.d.ts.map +1 -0
  8. package/dist/src/address-manager/utils.js +12 -0
  9. package/dist/src/address-manager/utils.js.map +1 -0
  10. package/dist/src/autonat/constants.d.ts +16 -2
  11. package/dist/src/autonat/constants.d.ts.map +1 -1
  12. package/dist/src/autonat/constants.js +16 -2
  13. package/dist/src/autonat/constants.js.map +1 -1
  14. package/dist/src/autonat/index.d.ts +12 -33
  15. package/dist/src/autonat/index.d.ts.map +1 -1
  16. package/dist/src/autonat/index.js +48 -33
  17. package/dist/src/autonat/index.js.map +1 -1
  18. package/dist/src/circuit-relay/index.d.ts +1 -1
  19. package/dist/src/circuit-relay/index.d.ts.map +1 -1
  20. package/dist/src/circuit-relay/server/advert-service.d.ts +2 -2
  21. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  22. package/dist/src/circuit-relay/server/advert-service.js +8 -4
  23. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  24. package/dist/src/circuit-relay/server/index.d.ts +8 -8
  25. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  26. package/dist/src/circuit-relay/server/index.js +32 -12
  27. package/dist/src/circuit-relay/server/index.js.map +1 -1
  28. package/dist/src/circuit-relay/server/reservation-store.d.ts +5 -5
  29. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -1
  30. package/dist/src/circuit-relay/server/reservation-store.js +10 -3
  31. package/dist/src/circuit-relay/server/reservation-store.js.map +1 -1
  32. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +1 -1
  33. package/dist/src/circuit-relay/server/reservation-voucher.js +5 -2
  34. package/dist/src/circuit-relay/server/reservation-voucher.js.map +1 -1
  35. package/dist/src/circuit-relay/transport/discovery.d.ts +4 -4
  36. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  37. package/dist/src/circuit-relay/transport/discovery.js +13 -5
  38. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  39. package/dist/src/circuit-relay/transport/index.d.ts +10 -8
  40. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  41. package/dist/src/circuit-relay/transport/index.js +26 -18
  42. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  43. package/dist/src/circuit-relay/transport/listener.d.ts +1 -1
  44. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  45. package/dist/src/circuit-relay/transport/listener.js +43 -43
  46. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  47. package/dist/src/circuit-relay/transport/reservation-store.d.ts +8 -7
  48. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  49. package/dist/src/circuit-relay/transport/reservation-store.js +65 -52
  50. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  51. package/dist/src/circuit-relay/utils.d.ts.map +1 -1
  52. package/dist/src/circuit-relay/utils.js +3 -3
  53. package/dist/src/circuit-relay/utils.js.map +1 -1
  54. package/dist/src/components.d.ts +10 -67
  55. package/dist/src/components.d.ts.map +1 -1
  56. package/dist/src/components.js +61 -165
  57. package/dist/src/components.js.map +1 -1
  58. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  59. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  60. package/dist/src/config/connection-gater.browser.js +27 -0
  61. package/dist/src/config/connection-gater.browser.js.map +1 -0
  62. package/dist/src/config/connection-gater.d.ts +6 -0
  63. package/dist/src/config/connection-gater.d.ts.map +1 -0
  64. package/dist/src/config/connection-gater.js +18 -0
  65. package/dist/src/config/connection-gater.js.map +1 -0
  66. package/dist/src/config.d.ts +2 -1
  67. package/dist/src/config.d.ts.map +1 -1
  68. package/dist/src/config.js +4 -66
  69. package/dist/src/config.js.map +1 -1
  70. package/dist/src/connection/index.d.ts +3 -3
  71. package/dist/src/connection/index.d.ts.map +1 -1
  72. package/dist/src/connection/index.js +38 -8
  73. package/dist/src/connection/index.js.map +1 -1
  74. package/dist/src/connection-manager/auto-dial.d.ts +4 -1
  75. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  76. package/dist/src/connection-manager/auto-dial.js +44 -13
  77. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  78. package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
  79. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  80. package/dist/src/connection-manager/connection-pruner.js +28 -8
  81. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  82. package/dist/src/connection-manager/dial-queue.d.ts +7 -6
  83. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  84. package/dist/src/connection-manager/dial-queue.js +59 -23
  85. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  86. package/dist/src/connection-manager/index.d.ts +16 -15
  87. package/dist/src/connection-manager/index.d.ts.map +1 -1
  88. package/dist/src/connection-manager/index.js +44 -41
  89. package/dist/src/connection-manager/index.js.map +1 -1
  90. package/dist/src/connection-manager/utils.d.ts +2 -3
  91. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  92. package/dist/src/connection-manager/utils.js +3 -3
  93. package/dist/src/connection-manager/utils.js.map +1 -1
  94. package/dist/src/content-routing/index.d.ts +2 -12
  95. package/dist/src/content-routing/index.d.ts.map +1 -1
  96. package/dist/src/content-routing/index.js +12 -44
  97. package/dist/src/content-routing/index.js.map +1 -1
  98. package/dist/src/content-routing/utils.d.ts +1 -1
  99. package/dist/src/content-routing/utils.d.ts.map +1 -1
  100. package/dist/src/content-routing/utils.js +3 -1
  101. package/dist/src/content-routing/utils.js.map +1 -1
  102. package/dist/src/fetch/index.d.ts +15 -37
  103. package/dist/src/fetch/index.d.ts.map +1 -1
  104. package/dist/src/fetch/index.js +19 -16
  105. package/dist/src/fetch/index.js.map +1 -1
  106. package/dist/src/get-peer.d.ts +1 -1
  107. package/dist/src/get-peer.d.ts.map +1 -1
  108. package/dist/src/get-peer.js +3 -3
  109. package/dist/src/get-peer.js.map +1 -1
  110. package/dist/src/identify/identify.d.ts +53 -0
  111. package/dist/src/identify/identify.d.ts.map +1 -0
  112. package/dist/src/identify/identify.js +417 -0
  113. package/dist/src/identify/identify.js.map +1 -0
  114. package/dist/src/identify/index.d.ts +14 -59
  115. package/dist/src/identify/index.d.ts.map +1 -1
  116. package/dist/src/identify/index.js +5 -388
  117. package/dist/src/identify/index.js.map +1 -1
  118. package/dist/src/index.d.ts +28 -79
  119. package/dist/src/index.d.ts.map +1 -1
  120. package/dist/src/index.js +2 -1
  121. package/dist/src/index.js.map +1 -1
  122. package/dist/src/insecure/index.js +6 -8
  123. package/dist/src/insecure/index.js.map +1 -1
  124. package/dist/src/libp2p.d.ts +16 -47
  125. package/dist/src/libp2p.d.ts.map +1 -1
  126. package/dist/src/libp2p.js +141 -201
  127. package/dist/src/libp2p.js.map +1 -1
  128. package/dist/src/peer-routing.d.ts +3 -40
  129. package/dist/src/peer-routing.d.ts.map +1 -1
  130. package/dist/src/peer-routing.js +8 -65
  131. package/dist/src/peer-routing.js.map +1 -1
  132. package/dist/src/ping/constants.d.ts +4 -0
  133. package/dist/src/ping/constants.d.ts.map +1 -1
  134. package/dist/src/ping/constants.js +10 -0
  135. package/dist/src/ping/constants.js.map +1 -1
  136. package/dist/src/ping/index.d.ts +11 -28
  137. package/dist/src/ping/index.d.ts.map +1 -1
  138. package/dist/src/ping/index.js +31 -28
  139. package/dist/src/ping/index.js.map +1 -1
  140. package/dist/src/pnet/crypto.js +3 -3
  141. package/dist/src/pnet/crypto.js.map +1 -1
  142. package/dist/src/pnet/index.d.ts.map +1 -1
  143. package/dist/src/pnet/index.js +9 -6
  144. package/dist/src/pnet/index.js.map +1 -1
  145. package/dist/src/pnet/key-generator.js +1 -1
  146. package/dist/src/pnet/key-generator.js.map +1 -1
  147. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  148. package/dist/src/pubsub/dummy-pubsub.js +2 -5
  149. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  150. package/dist/src/registrar.d.ts +6 -4
  151. package/dist/src/registrar.d.ts.map +1 -1
  152. package/dist/src/registrar.js +33 -28
  153. package/dist/src/registrar.js.map +1 -1
  154. package/dist/src/transport-manager.d.ts +14 -7
  155. package/dist/src/transport-manager.d.ts.map +1 -1
  156. package/dist/src/transport-manager.js +27 -13
  157. package/dist/src/transport-manager.js.map +1 -1
  158. package/dist/src/upgrader.d.ts +20 -15
  159. package/dist/src/upgrader.d.ts.map +1 -1
  160. package/dist/src/upgrader.js +49 -53
  161. package/dist/src/upgrader.js.map +1 -1
  162. package/dist/src/{nat-manager.d.ts → upnp-nat/index.d.ts} +12 -15
  163. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  164. package/dist/src/{nat-manager.js → upnp-nat/index.js} +23 -10
  165. package/dist/src/upnp-nat/index.js.map +1 -0
  166. package/dist/src/version.d.ts +1 -1
  167. package/dist/src/version.d.ts.map +1 -1
  168. package/dist/src/version.js +1 -1
  169. package/dist/src/version.js.map +1 -1
  170. package/package.json +62 -47
  171. package/src/address-manager/index.ts +52 -10
  172. package/src/address-manager/utils.ts +14 -0
  173. package/src/autonat/constants.ts +17 -2
  174. package/src/autonat/index.ts +65 -57
  175. package/src/circuit-relay/index.ts +1 -1
  176. package/src/circuit-relay/server/advert-service.ts +5 -5
  177. package/src/circuit-relay/server/index.ts +28 -20
  178. package/src/circuit-relay/server/reservation-store.ts +5 -5
  179. package/src/circuit-relay/server/reservation-voucher.ts +1 -1
  180. package/src/circuit-relay/transport/discovery.ts +12 -10
  181. package/src/circuit-relay/transport/index.ts +26 -26
  182. package/src/circuit-relay/transport/listener.ts +43 -33
  183. package/src/circuit-relay/transport/reservation-store.ts +42 -28
  184. package/src/circuit-relay/utils.ts +6 -6
  185. package/src/components.ts +75 -238
  186. package/src/config/connection-gater.browser.ts +31 -0
  187. package/src/config/connection-gater.ts +19 -0
  188. package/src/config.ts +9 -70
  189. package/src/connection/index.ts +6 -10
  190. package/src/connection-manager/auto-dial.ts +46 -17
  191. package/src/connection-manager/connection-pruner.ts +30 -10
  192. package/src/connection-manager/dial-queue.ts +56 -35
  193. package/src/connection-manager/index.ts +45 -57
  194. package/src/connection-manager/utils.ts +4 -5
  195. package/src/content-routing/index.ts +11 -59
  196. package/src/content-routing/utils.ts +4 -2
  197. package/src/fetch/index.ts +63 -26
  198. package/src/get-peer.ts +4 -4
  199. package/src/identify/identify.ts +519 -0
  200. package/src/identify/index.ts +19 -509
  201. package/src/index.ts +25 -88
  202. package/src/insecure/index.ts +10 -10
  203. package/src/libp2p.ts +158 -279
  204. package/src/peer-routing.ts +10 -110
  205. package/src/ping/constants.ts +11 -0
  206. package/src/ping/index.ts +43 -39
  207. package/src/pnet/crypto.ts +3 -3
  208. package/src/pnet/index.ts +6 -6
  209. package/src/pnet/key-generator.ts +1 -1
  210. package/src/pubsub/dummy-pubsub.ts +2 -2
  211. package/src/registrar.ts +34 -31
  212. package/src/transport-manager.ts +33 -19
  213. package/src/upgrader.ts +75 -71
  214. package/src/{nat-manager.ts → upnp-nat/index.ts} +23 -24
  215. package/src/version.ts +1 -1
  216. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  217. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  218. package/dist/src/dht/dht-content-routing.js +0 -32
  219. package/dist/src/dht/dht-content-routing.js.map +0 -1
  220. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  221. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  222. package/dist/src/dht/dht-peer-routing.js +0 -26
  223. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  224. package/dist/src/dht/dummy-dht.d.ts +0 -20
  225. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  226. package/dist/src/dht/dummy-dht.js +0 -46
  227. package/dist/src/dht/dummy-dht.js.map +0 -1
  228. package/dist/src/nat-manager.d.ts.map +0 -1
  229. package/dist/src/nat-manager.js.map +0 -1
  230. package/dist/src/peer-record-updater.d.ts +0 -24
  231. package/dist/src/peer-record-updater.d.ts.map +0 -1
  232. package/dist/src/peer-record-updater.js +0 -44
  233. package/dist/src/peer-record-updater.js.map +0 -1
  234. package/dist/typedoc-urls.json +0 -41
  235. package/src/dht/dht-content-routing.ts +0 -44
  236. package/src/dht/dht-peer-routing.ts +0 -36
  237. package/src/dht/dummy-dht.ts +0 -60
  238. package/src/peer-record-updater.ts +0 -65
@@ -1,21 +1,18 @@
1
1
  import { CodeError } from '@libp2p/interfaces/errors'
2
+ import merge from 'it-merge'
3
+ import { pipe } from 'it-pipe'
2
4
  import { messages, codes } from '../errors.js'
3
5
  import {
4
6
  storeAddresses,
5
7
  uniquePeers,
6
8
  requirePeers
7
9
  } from './utils.js'
8
- import drain from 'it-drain'
9
- import merge from 'it-merge'
10
- import { pipe } from 'it-pipe'
11
10
  import type { ContentRouting } from '@libp2p/interface-content-routing'
11
+ import type { PeerInfo } from '@libp2p/interface-peer-info'
12
+ import type { PeerStore } from '@libp2p/interface-peer-store'
12
13
  import type { AbortOptions } from '@libp2p/interfaces'
13
14
  import type { Startable } from '@libp2p/interfaces/startable'
14
15
  import type { CID } from 'multiformats/cid'
15
- import type { PeerStore } from '@libp2p/interface-peer-store'
16
- import type { DualDHT } from '@libp2p/interface-dht'
17
- import type { PeerInfo } from '@libp2p/interface-peer-info'
18
- import type { PeerId } from '@libp2p/interface-peer-id'
19
16
 
20
17
  export interface CompoundContentRoutingInit {
21
18
  routers: ContentRouting[]
@@ -23,7 +20,6 @@ export interface CompoundContentRoutingInit {
23
20
 
24
21
  export interface CompoundContentRoutingComponents {
25
22
  peerStore: PeerStore
26
- dht?: DualDHT
27
23
  }
28
24
 
29
25
  export class CompoundContentRouting implements ContentRouting, Startable {
@@ -54,7 +50,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
54
50
  */
55
51
  async * findProviders (key: CID, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
56
52
  if (this.routers.length === 0) {
57
- throw new CodeError('No content this.routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
53
+ throw new CodeError('No content routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
58
54
  }
59
55
 
60
56
  yield * pipe(
@@ -87,11 +83,9 @@ export class CompoundContentRouting implements ContentRouting, Startable {
87
83
  throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
88
84
  }
89
85
 
90
- const dht = this.components.dht
91
-
92
- if (dht != null) {
93
- await drain(dht.put(key, value, options))
94
- }
86
+ await Promise.all(this.routers.map(async (router) => {
87
+ await router.put(key, value, options)
88
+ }))
95
89
  }
96
90
 
97
91
  /**
@@ -103,50 +97,8 @@ export class CompoundContentRouting implements ContentRouting, Startable {
103
97
  throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
104
98
  }
105
99
 
106
- const dht = this.components.dht
107
-
108
- if (dht != null) {
109
- for await (const event of dht.get(key, options)) {
110
- if (event.name === 'VALUE') {
111
- return event.value
112
- }
113
- }
114
- }
115
-
116
- throw new CodeError(messages.NOT_FOUND, codes.ERR_NOT_FOUND)
117
- }
118
-
119
- /**
120
- * Get the `n` values to the given key without sorting
121
- */
122
- async * getMany (key: Uint8Array, nVals: number, options: AbortOptions): AsyncIterable<{ from: PeerId, val: Uint8Array }> {
123
- if (!this.isStarted()) {
124
- throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
125
- }
126
-
127
- if (nVals == null || nVals === 0) {
128
- return
129
- }
130
-
131
- let gotValues = 0
132
- const dht = this.components.dht
133
-
134
- if (dht != null) {
135
- for await (const event of dht.get(key, options)) {
136
- if (event.name === 'VALUE') {
137
- yield { from: event.from, val: event.value }
138
-
139
- gotValues++
140
-
141
- if (gotValues === nVals) {
142
- break
143
- }
144
- }
145
- }
146
- }
147
-
148
- if (gotValues === 0) {
149
- throw new CodeError(messages.NOT_FOUND, codes.ERR_NOT_FOUND)
150
- }
100
+ return Promise.any(this.routers.map(async (router) => {
101
+ return router.get(key, options)
102
+ }))
151
103
  }
152
104
  }
@@ -1,9 +1,9 @@
1
1
  import { CodeError } from '@libp2p/interfaces/errors'
2
2
  import filter from 'it-filter'
3
3
  import map from 'it-map'
4
- import type { Source } from 'it-stream-types'
5
4
  import type { PeerInfo } from '@libp2p/interface-peer-info'
6
5
  import type { PeerStore } from '@libp2p/interface-peer-store'
6
+ import type { Source } from 'it-stream-types'
7
7
 
8
8
  /**
9
9
  * Store the multiaddrs from every peer in the passed peer store
@@ -11,7 +11,9 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
11
11
  export async function * storeAddresses (source: Source<PeerInfo>, peerStore: PeerStore): AsyncIterable<PeerInfo> {
12
12
  yield * map(source, async (peer) => {
13
13
  // ensure we have the addresses for a given peer
14
- await peerStore.addressBook.add(peer.id, peer.multiaddrs)
14
+ await peerStore.merge(peer.id, {
15
+ multiaddrs: peer.multiaddrs
16
+ })
15
17
 
16
18
  return peer
17
19
  })
@@ -1,34 +1,35 @@
1
- import { logger } from '@libp2p/logger'
1
+ import { setMaxListeners } from 'events'
2
2
  import { CodeError } from '@libp2p/interfaces/errors'
3
- import { codes } from '../errors.js'
4
- import * as lp from 'it-length-prefixed'
5
- import { FetchRequest, FetchResponse } from './pb/proto.js'
6
- import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
7
- import type { PeerId } from '@libp2p/interface-peer-id'
8
- import type { Startable } from '@libp2p/interfaces/startable'
9
- import type { Stream } from '@libp2p/interface-connection'
10
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
11
- import type { AbortOptions } from '@libp2p/interfaces'
3
+ import { logger } from '@libp2p/logger'
12
4
  import { abortableDuplex } from 'abortable-iterator'
13
- import { pipe } from 'it-pipe'
14
5
  import first from 'it-first'
15
- import { TimeoutController } from 'timeout-abort-controller'
16
- import { setMaxListeners } from 'events'
6
+ import * as lp from 'it-length-prefixed'
7
+ import { pipe } from 'it-pipe'
17
8
  import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
18
9
  import { toString as uint8arrayToString } from 'uint8arrays/to-string'
10
+ import { codes } from '../errors.js'
11
+ import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
12
+ import { FetchRequest, FetchResponse } from './pb/proto.js'
13
+ import type { Stream } from '@libp2p/interface-connection'
19
14
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
15
+ import type { PeerId } from '@libp2p/interface-peer-id'
16
+ import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
17
+ import type { AbortOptions } from '@libp2p/interfaces'
18
+ import type { Startable } from '@libp2p/interfaces/startable'
20
19
 
21
20
  const log = logger('libp2p:fetch')
22
21
 
22
+ const DEFAULT_TIMEOUT = 10000
23
+
23
24
  export interface FetchServiceInit {
24
- protocolPrefix: string
25
- maxInboundStreams: number
26
- maxOutboundStreams: number
25
+ protocolPrefix?: string
26
+ maxInboundStreams?: number
27
+ maxOutboundStreams?: number
27
28
 
28
29
  /**
29
30
  * How long we should wait for a remote peer to send any data
30
31
  */
31
- timeout: number
32
+ timeout?: number
32
33
  }
33
34
 
34
35
  export interface HandleMessageOptions {
@@ -45,13 +46,51 @@ export interface FetchServiceComponents {
45
46
  connectionManager: ConnectionManager
46
47
  }
47
48
 
49
+ export interface FetchService {
50
+ /**
51
+ * The protocol name used by this fetch service
52
+ */
53
+ protocol: string
54
+
55
+ /**
56
+ * Sends a request to fetch the value associated with the given key from the given peer
57
+ */
58
+ fetch: (peer: PeerId, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
59
+
60
+ /**
61
+ * Registers a new lookup callback that can map keys to values, for a given set of keys that
62
+ * share the same prefix
63
+ *
64
+ * @example
65
+ *
66
+ * ```js
67
+ * // ...
68
+ * libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
69
+ * ```
70
+ */
71
+ registerLookupFunction: (prefix: string, lookup: LookupFunction) => void
72
+
73
+ /**
74
+ * Registers a new lookup callback that can map keys to values, for a given set of keys that
75
+ * share the same prefix.
76
+ *
77
+ * @example
78
+ *
79
+ * ```js
80
+ * // ...
81
+ * libp2p.fetchService.unregisterLookupFunction('/prefix')
82
+ * ```
83
+ */
84
+ unregisterLookupFunction: (prefix: string, lookup?: LookupFunction) => void
85
+ }
86
+
48
87
  /**
49
88
  * A simple libp2p protocol for requesting a value corresponding to a key from a peer.
50
89
  * Developers can register one or more lookup function for retrieving the value corresponding to
51
90
  * a given key. Each lookup function must act on a distinct part of the overall key space, defined
52
91
  * by a fixed prefix that all keys that should be routed to that lookup function will start with.
53
92
  */
54
- export class FetchService implements Startable {
93
+ class DefaultFetchService implements Startable, FetchService {
55
94
  public readonly protocol: string
56
95
  private readonly components: FetchServiceComponents
57
96
  private readonly lookupFunctions: Map<string, LookupFunction>
@@ -99,19 +138,17 @@ export class FetchService implements Startable {
99
138
  log('dialing %s to %p', this.protocol, peer)
100
139
 
101
140
  const connection = await this.components.connectionManager.openConnection(peer, options)
102
- let timeoutController
103
141
  let signal = options.signal
104
142
  let stream: Stream | undefined
105
143
 
106
144
  // create a timeout if no abort signal passed
107
145
  if (signal == null) {
108
146
  log('using default timeout of %d ms', this.init.timeout)
109
- timeoutController = new TimeoutController(this.init.timeout)
110
- signal = timeoutController.signal
147
+ signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
111
148
 
112
149
  try {
113
150
  // fails on node < 15.4
114
- setMaxListeners?.(Infinity, timeoutController.signal)
151
+ setMaxListeners?.(Infinity, signal)
115
152
  } catch {}
116
153
  }
117
154
 
@@ -163,10 +200,6 @@ export class FetchService implements Startable {
163
200
 
164
201
  return result ?? null
165
202
  } finally {
166
- if (timeoutController != null) {
167
- timeoutController.clear()
168
- }
169
-
170
203
  if (stream != null) {
171
204
  stream.close()
172
205
  }
@@ -274,3 +307,7 @@ export class FetchService implements Startable {
274
307
  this.lookupFunctions.delete(prefix)
275
308
  }
276
309
  }
310
+
311
+ export function fetchService (init: FetchServiceInit = {}): (components: FetchServiceComponents) => FetchService {
312
+ return (components) => new DefaultFetchService(components, init)
313
+ }
package/src/get-peer.ts CHANGED
@@ -1,11 +1,11 @@
1
+ import { isPeerId } from '@libp2p/interface-peer-id'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
+ import { logger } from '@libp2p/logger'
1
4
  import { peerIdFromString } from '@libp2p/peer-id'
2
- import type { Multiaddr } from '@multiformats/multiaddr'
3
5
  import { isMultiaddr } from '@multiformats/multiaddr'
4
- import { CodeError } from '@libp2p/interfaces/errors'
5
6
  import { codes } from './errors.js'
6
- import { isPeerId } from '@libp2p/interface-peer-id'
7
7
  import type { PeerId } from '@libp2p/interface-peer-id'
8
- import { logger } from '@libp2p/logger'
8
+ import type { Multiaddr } from '@multiformats/multiaddr'
9
9
 
10
10
  const log = logger('libp2p:get-peer')
11
11