@helia/interface 6.2.1-6f8165b5 → 6.2.1-9114743f

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.
package/src/routing.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { AbortOptions, PeerId, PeerInfo, TraceOptions } from '@libp2p/interface'
1
+ import type { Multiaddr } from '@multiformats/multiaddr'
2
+ import type { AbortOptions } from 'abort-error'
2
3
  import type { CID } from 'multiformats/cid'
3
4
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
4
5
 
@@ -8,7 +9,7 @@ import type { ProgressEvent, ProgressOptions } from 'progress-events'
8
9
  * local cache that may be used in preference over network calls, for example
9
10
  * when a record has a TTL.
10
11
  */
11
- export interface RoutingOptions<Event extends ProgressEvent = any> extends AbortOptions, ProgressOptions<Event>, TraceOptions {
12
+ export interface RoutingOptions<Event extends ProgressEvent = any> extends AbortOptions, ProgressOptions<Event> {
12
13
  /**
13
14
  * Pass `false` to not use the network
14
15
  *
@@ -29,12 +30,42 @@ export interface RoutingOptions<Event extends ProgressEvent = any> extends Abort
29
30
  * @default true
30
31
  */
31
32
  validate?: boolean
33
+
34
+ /**
35
+ * Where tracing is used, this value carries tracing context
36
+ */
37
+ trace?: any
38
+ }
39
+
40
+ /**
41
+ * A Peer is another node on the network
42
+ */
43
+ export interface Peer {
44
+ /**
45
+ * The identifier of the remote peer
46
+ */
47
+ id: CID
48
+
49
+ /**
50
+ * The multiaddrs a peer is listening on
51
+ */
52
+ multiaddrs: Multiaddr[]
32
53
  }
33
54
 
34
55
  /**
35
56
  * A provider can supply the content for a CID
36
57
  */
37
- export interface Provider extends PeerInfo {
58
+ export interface Provider {
59
+ /**
60
+ * The identifier of the remote peer, a CID
61
+ */
62
+ id: CID
63
+
64
+ /**
65
+ * The multiaddrs a peer is listening on
66
+ */
67
+ multiaddrs: Multiaddr[]
68
+
38
69
  /**
39
70
  * If present these are the methods that the peer can supply the content via.
40
71
  *
@@ -69,7 +100,7 @@ export interface RoutingFindProvidersEndEvent {
69
100
  export interface RoutingFindProvidersHttpGatewayProvider {
70
101
  routing: 'http-gateway-router'
71
102
  cid: CID
72
- provider: PeerInfo & {
103
+ provider: Peer & {
73
104
  protocols: ['transport-ipfs-gateway-http']
74
105
  }
75
106
  }
@@ -77,7 +108,7 @@ export interface RoutingFindProvidersHttpGatewayProvider {
77
108
  export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
78
109
  routing: 'delegated-http-router'
79
110
  cid: CID
80
- provider: PeerInfo & {
111
+ provider: Peer & {
81
112
  routing: 'delegated-http-routing'
82
113
  protocols: string[]
83
114
  }
@@ -86,7 +117,7 @@ export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
86
117
  export interface RoutingFindProvidersLibp2pProvider {
87
118
  routing: 'libp2p-router'
88
119
  cid: CID
89
- provider: PeerInfo
120
+ provider: Peer
90
121
  }
91
122
 
92
123
  export type RoutingFindProvidersProviderEvent = RoutingFindProvidersHttpGatewayProvider | RoutingFindProvidersDelegatedHttpRoutingProvider | RoutingFindProvidersLibp2pProvider
@@ -157,12 +188,12 @@ export type RoutingGetProgressEvents =
157
188
 
158
189
  export interface RoutingFindPeerStartEvent {
159
190
  routing: string
160
- peerId: PeerId
191
+ peerId: CID
161
192
  }
162
193
 
163
194
  export interface RoutingFindPeerEndEvent {
164
195
  routing: string
165
- peerId: PeerId
196
+ peerId: CID
166
197
  }
167
198
 
168
199
  export type RoutingFindPeerProgressEvents =
@@ -183,7 +214,7 @@ export type RoutingGetClosestPeersProgressEvents =
183
214
  ProgressEvent<'helia:routing:get-closest-peers:start', RoutingGetClosestPeersStartEvent> |
184
215
  ProgressEvent<'helia:routing:get-closest-peers:end', RoutingGetClosestPeersEndEvent>
185
216
 
186
- export interface Routing {
217
+ export interface Router {
187
218
  /**
188
219
  * The name of this routing implementation
189
220
  */
@@ -197,10 +228,12 @@ export interface Routing {
197
228
  *
198
229
  * ```js
199
230
  * // ...
200
- * await contentRouting.provide(cid)
231
+ * await contentRouting.provide(cid, {
232
+ * signal: AbortSignal.timeout(5_000)
233
+ * })
201
234
  * ```
202
235
  */
203
- provide(cid: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>
236
+ provide?(cid: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>
204
237
 
205
238
  /**
206
239
  * Helia will periodically re-provide every previously provided CID. Use this
@@ -210,10 +243,12 @@ export interface Routing {
210
243
  *
211
244
  * ```js
212
245
  * // ...
213
- * await contentRouting.cancelReprovide(cid)
246
+ * await contentRouting.cancelReprovide(cid, {
247
+ * signal: AbortSignal.timeout(5_000)
248
+ * })
214
249
  * ```
215
250
  */
216
- cancelReprovide(key: CID, options?: AbortOptions): Promise<void>
251
+ cancelReprovide?(key: CID, options?: AbortOptions): Promise<void>
217
252
 
218
253
  /**
219
254
  * Find the providers of the passed CID.
@@ -222,12 +257,14 @@ export interface Routing {
222
257
  *
223
258
  * ```js
224
259
  * // Iterate over the providers found for the given cid
225
- * for await (const provider of contentRouting.findProviders(cid)) {
260
+ * for await (const provider of contentRouting.findProviders(cid, {
261
+ * signal: AbortSignal.timeout(5_000)
262
+ * })) {
226
263
  * console.log(provider.id, provider.multiaddrs)
227
264
  * }
228
265
  * ```
229
266
  */
230
- findProviders(cid: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>
267
+ findProviders?(cid: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>
231
268
 
232
269
  /**
233
270
  * Puts a value corresponding to the passed key in a way that can later be
@@ -237,13 +274,15 @@ export interface Routing {
237
274
  *
238
275
  * ```js
239
276
  * // ...
240
- * const key = '/key'
277
+ * const key = uint8ArrayFromString('/key')
241
278
  * const value = uint8ArrayFromString('oh hello there')
242
279
  *
243
- * await contentRouting.put(key, value)
280
+ * await contentRouting.put(key, value, {
281
+ * signal: AbortSignal.timeout(5_000)
282
+ * })
244
283
  * ```
245
284
  */
246
- put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>
285
+ put?(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>
247
286
 
248
287
  /**
249
288
  * Retrieves a value from the network corresponding to the passed key.
@@ -253,11 +292,13 @@ export interface Routing {
253
292
  * ```js
254
293
  * // ...
255
294
  *
256
- * const key = '/key'
257
- * const value = await contentRouting.get(key)
295
+ * const key = uint8ArrayFromString('/key')
296
+ * const value = await contentRouting.get(key, {
297
+ * signal: AbortSignal.timeout(5_000)
298
+ * })
258
299
  * ```
259
300
  */
260
- get(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>
301
+ get?(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>
261
302
 
262
303
  /**
263
304
  * Searches the network for peer info corresponding to the passed peer id.
@@ -266,10 +307,12 @@ export interface Routing {
266
307
  *
267
308
  * ```js
268
309
  * // ...
269
- * const peer = await peerRouting.findPeer(peerId, options)
310
+ * const peer = await peerRouting.findPeer(peerId, {
311
+ * signal: AbortSignal.timeout(5_000)
312
+ * })
270
313
  * ```
271
314
  */
272
- findPeer(peerId: PeerId, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<PeerInfo>
315
+ findPeer?(peer: CID, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<Peer>
273
316
 
274
317
  /**
275
318
  * Search the network for peers that are closer to the passed key. Peer
@@ -279,10 +322,14 @@ export interface Routing {
279
322
  *
280
323
  * ```js
281
324
  * // Iterate over the closest peers found for the given key
282
- * for await (const peer of peerRouting.getClosestPeers(key)) {
325
+ * for await (const peer of peerRouting.getClosestPeers(key, {
326
+ * signal: AbortSignal.timeout(5_000)
327
+ * })) {
283
328
  * console.log(peer.id, peer.multiaddrs)
284
329
  * }
285
330
  * ```
286
331
  */
287
- getClosestPeers(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<PeerInfo>
332
+ getClosestPeers?(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<Peer>
288
333
  }
334
+
335
+ export type Routing = Required<Omit<Router, 'name'>>