-
Notifications
You must be signed in to change notification settings - Fork 106
/
routing.ts
137 lines (127 loc) · 3.42 KB
/
routing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import type { AbortOptions, PeerId, PeerInfo } from '@libp2p/interface'
import type { CID } from 'multiformats/cid'
import type { ProgressOptions } from 'progress-events'
/**
* When a routing operation involves reading values, these options allow
* controlling where the values are read from. Some implementations support a
* local cache that may be used in preference over network calls, for example
* when a record has a TTL.
*/
export interface RoutingOptions extends AbortOptions, ProgressOptions {
/**
* Pass `false` to not use the network
*
* @default true
*/
useNetwork?: boolean
/**
* Pass `false` to not use cached values
*
* @default true
*/
useCache?: boolean
/**
* Pass `false` to not perform validation
*
* @default true
*/
validate?: boolean
}
/**
* A provider can supply the content for a CID
*/
export interface Provider extends PeerInfo {
/**
* If present these are the methods that the peer can supply the content via.
*
* If not present the caller should attempt to dial the remote peer and run
* the identify protocol to discover how to retrieve the content.
*
* Example values are (but not limited to):
*
* - transport-graphsync-filecoinv1
* - transport-ipfs-gateway-http
* - transport-bitswap
*/
protocols?: string[]
}
export interface Routing {
/**
* The implementation of this method should ensure that network peers know the
* caller can provide content that corresponds to the passed CID.
*
* @example
*
* ```js
* // ...
* await contentRouting.provide(cid)
* ```
*/
provide(cid: CID, options?: RoutingOptions): Promise<void>
/**
* Find the providers of the passed CID.
*
* @example
*
* ```js
* // Iterate over the providers found for the given cid
* for await (const provider of contentRouting.findProviders(cid)) {
* console.log(provider.id, provider.multiaddrs)
* }
* ```
*/
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<Provider>
/**
* Puts a value corresponding to the passed key in a way that can later be
* retrieved by another network peer using the get method.
*
* @example
*
* ```js
* // ...
* const key = '/key'
* const value = uint8ArrayFromString('oh hello there')
*
* await contentRouting.put(key, value)
* ```
*/
put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions): Promise<void>
/**
* Retrieves a value from the network corresponding to the passed key.
*
* @example
*
* ```js
* // ...
*
* const key = '/key'
* const value = await contentRouting.get(key)
* ```
*/
get(key: Uint8Array, options?: RoutingOptions): Promise<Uint8Array>
/**
* Searches the network for peer info corresponding to the passed peer id.
*
* @example
*
* ```js
* // ...
* const peer = await peerRouting.findPeer(peerId, options)
* ```
*/
findPeer(peerId: PeerId, options?: RoutingOptions): Promise<PeerInfo>
/**
* Search the network for peers that are closer to the passed key. Peer
* info should be yielded in ever-increasing closeness to the key.
*
* @example
*
* ```js
* // Iterate over the closest peers found for the given key
* for await (const peer of peerRouting.getClosestPeers(key)) {
* console.log(peer.id, peer.multiaddrs)
* }
* ```
*/
getClosestPeers(key: Uint8Array, options?: RoutingOptions): AsyncIterable<PeerInfo>
}