11import type { AbortOptions , PeerId , PeerInfo , TraceOptions } from '@libp2p/interface'
22import type { CID } from 'multiformats/cid'
3- import type { ProgressOptions } from 'progress-events'
3+ import type { ProgressEvent , ProgressOptions } from 'progress-events'
44
55/**
66 * When a routing operation involves reading values, these options allow
77 * controlling where the values are read from. Some implementations support a
88 * local cache that may be used in preference over network calls, for example
99 * when a record has a TTL.
1010 */
11- export interface RoutingOptions extends AbortOptions , ProgressOptions , TraceOptions {
11+ export interface RoutingOptions < Event extends ProgressEvent = any > extends AbortOptions , ProgressOptions < Event > , TraceOptions {
1212 /**
1313 * Pass `false` to not use the network
1414 *
@@ -55,7 +55,140 @@ export interface Provider extends PeerInfo {
5555 routing : string
5656}
5757
58+ export interface RoutingFindProvidersStartEvent {
59+ routing : string
60+ cid : CID
61+ }
62+
63+ export interface RoutingFindProvidersEndEvent {
64+ routing : string
65+ cid : CID
66+ found : number
67+ }
68+
69+ export interface RoutingFindProvidersHttpGatewayProvider {
70+ routing : 'http-gateway-router'
71+ cid : CID
72+ provider : PeerInfo & {
73+ protocols : [ 'transport-ipfs-gateway-http' ]
74+ }
75+ }
76+
77+ export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
78+ routing : 'delegated-http-router'
79+ cid : CID
80+ provider : PeerInfo & {
81+ routing : 'delegated-http-routing'
82+ protocols : string [ ]
83+ }
84+ }
85+
86+ export interface RoutingFindProvidersLibp2pProvider {
87+ routing : 'libp2p-router'
88+ cid : CID
89+ provider : PeerInfo
90+ }
91+
92+ export type RoutingFindProvidersProviderEvent = RoutingFindProvidersHttpGatewayProvider | RoutingFindProvidersDelegatedHttpRoutingProvider | RoutingFindProvidersLibp2pProvider
93+
94+ export type RoutingFindProvidersProgressEvents =
95+ ProgressEvent < 'helia:routing:find-providers:start' , RoutingFindProvidersStartEvent > |
96+ ProgressEvent < 'helia:routing:find-providers:provider' , RoutingFindProvidersProviderEvent > |
97+ ProgressEvent < 'helia:routing:find-providers:end' , RoutingFindProvidersEndEvent > |
98+ RoutingFindPeerProgressEvents
99+
100+ export interface RoutingProvideStartEvent {
101+ routing : string
102+ cid : CID
103+ }
104+
105+ export interface RoutingProvideEndEvent {
106+ routing : string
107+ cid : CID
108+ }
109+
110+ export type RoutingProvideProgressEvents =
111+ ProgressEvent < 'helia:routing:provide:start' , RoutingProvideStartEvent > |
112+ ProgressEvent < 'helia:routing:provide:end' , RoutingProvideEndEvent >
113+
114+ export interface RoutingCancelReprovideStartEvent {
115+ routing : string
116+ cid : CID
117+ }
118+
119+ export interface RoutingCancelReprovideEndEvent {
120+ routing : string
121+ cid : CID
122+ }
123+
124+ export type RoutingCancelReprovideProgressEvents =
125+ ProgressEvent < 'helia:routing:cancel-reprovide:start' , RoutingCancelReprovideStartEvent > |
126+ ProgressEvent < 'helia:routing:cancel-reprovide:end' , RoutingCancelReprovideEndEvent >
127+
128+ export interface RoutingPutStartEvent {
129+ routing : string
130+ key : Uint8Array
131+ value : Uint8Array
132+ }
133+
134+ export interface RoutingPutEndEvent {
135+ routing : string
136+ key : Uint8Array
137+ value : Uint8Array
138+ }
139+
140+ export type RoutingPutProgressEvents =
141+ ProgressEvent < 'helia:routing:put:start' , RoutingPutStartEvent > |
142+ ProgressEvent < 'helia:routing:put:end' , RoutingPutEndEvent >
143+
144+ export interface RoutingGetStartEvent {
145+ routing : string
146+ key : Uint8Array
147+ }
148+
149+ export interface RoutingGetEndEvent {
150+ routing : string
151+ key : Uint8Array
152+ }
153+
154+ export type RoutingGetProgressEvents =
155+ ProgressEvent < 'helia:routing:get:start' , RoutingGetStartEvent > |
156+ ProgressEvent < 'helia:routing:get:end' , RoutingGetEndEvent >
157+
158+ export interface RoutingFindPeerStartEvent {
159+ routing : string
160+ peerId : PeerId
161+ }
162+
163+ export interface RoutingFindPeerEndEvent {
164+ routing : string
165+ peerId : PeerId
166+ }
167+
168+ export type RoutingFindPeerProgressEvents =
169+ ProgressEvent < 'helia:routing:find-peer:start' , RoutingFindPeerStartEvent > |
170+ ProgressEvent < 'helia:routing:find-peer:end' , RoutingFindPeerEndEvent >
171+
172+ export interface RoutingGetClosestPeersStartEvent {
173+ routing : string
174+ key : Uint8Array
175+ }
176+
177+ export interface RoutingGetClosestPeersEndEvent {
178+ routing : string
179+ key : Uint8Array
180+ }
181+
182+ export type RoutingGetClosestPeersProgressEvents =
183+ ProgressEvent < 'helia:routing:get-closest-peers:start' , RoutingGetClosestPeersStartEvent > |
184+ ProgressEvent < 'helia:routing:get-closest-peers:end' , RoutingGetClosestPeersEndEvent >
185+
58186export interface Routing {
187+ /**
188+ * The name of this routing implementation
189+ */
190+ name : string
191+
59192 /**
60193 * The implementation of this method should ensure that network peers know the
61194 * caller can provide content that corresponds to the passed CID.
@@ -67,7 +200,7 @@ export interface Routing {
67200 * await contentRouting.provide(cid)
68201 * ```
69202 */
70- provide ( cid : CID , options ?: RoutingOptions ) : Promise < void >
203+ provide ( cid : CID , options ?: RoutingOptions < RoutingProvideProgressEvents > ) : Promise < void >
71204
72205 /**
73206 * Helia will periodically re-provide every previously provided CID. Use this
@@ -94,7 +227,7 @@ export interface Routing {
94227 * }
95228 * ```
96229 */
97- findProviders ( cid : CID , options ?: RoutingOptions ) : AsyncIterable < Provider >
230+ findProviders ( cid : CID , options ?: RoutingOptions < RoutingFindProvidersProgressEvents > ) : AsyncIterable < Provider >
98231
99232 /**
100233 * Puts a value corresponding to the passed key in a way that can later be
@@ -110,7 +243,7 @@ export interface Routing {
110243 * await contentRouting.put(key, value)
111244 * ```
112245 */
113- put ( key : Uint8Array , value : Uint8Array , options ?: RoutingOptions ) : Promise < void >
246+ put ( key : Uint8Array , value : Uint8Array , options ?: RoutingOptions < RoutingPutProgressEvents > ) : Promise < void >
114247
115248 /**
116249 * Retrieves a value from the network corresponding to the passed key.
@@ -124,7 +257,7 @@ export interface Routing {
124257 * const value = await contentRouting.get(key)
125258 * ```
126259 */
127- get ( key : Uint8Array , options ?: RoutingOptions ) : Promise < Uint8Array >
260+ get ( key : Uint8Array , options ?: RoutingOptions < RoutingGetProgressEvents > ) : Promise < Uint8Array >
128261
129262 /**
130263 * Searches the network for peer info corresponding to the passed peer id.
@@ -136,7 +269,7 @@ export interface Routing {
136269 * const peer = await peerRouting.findPeer(peerId, options)
137270 * ```
138271 */
139- findPeer ( peerId : PeerId , options ?: RoutingOptions ) : Promise < PeerInfo >
272+ findPeer ( peerId : PeerId , options ?: RoutingOptions < RoutingFindPeerProgressEvents > ) : Promise < PeerInfo >
140273
141274 /**
142275 * Search the network for peers that are closer to the passed key. Peer
@@ -151,5 +284,5 @@ export interface Routing {
151284 * }
152285 * ```
153286 */
154- getClosestPeers ( key : Uint8Array , options ?: RoutingOptions ) : AsyncIterable < PeerInfo >
287+ getClosestPeers ( key : Uint8Array , options ?: RoutingOptions < RoutingGetClosestPeersProgressEvents > ) : AsyncIterable < PeerInfo >
155288}
0 commit comments