-
Notifications
You must be signed in to change notification settings - Fork 436
/
index.ts
91 lines (85 loc) · 2.32 KB
/
index.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
import type { RoutingOptions } from '../index.js'
import type { PeerInfo } from '../peer-info/index.js'
import type { CID } from 'multiformats/cid'
/**
* Any object that implements this Symbol as a property should return a
* ContentRouting instance as the property value, similar to how
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
*
* @example
*
* ```TypeScript
* import { contentRoutingSymbol, ContentRouting } from '@libp2p/content-routing'
*
* class MyContentRouter implements ContentRouting {
* get [contentRoutingSymbol] () {
* return this
* }
*
* // ...other methods
* }
* ```
*/
export const contentRoutingSymbol = Symbol.for('@libp2p/content-routing')
/**
* Implementers of this interface can provide a ContentRouting implementation to
* interested callers.
*/
export interface ContentRoutingProvider {
[contentRoutingSymbol]: ContentRouting
}
export interface ContentRouting {
/**
* The implementation of this method should ensure that network peers know the
* caller can provide content that corresponds to the passed CID.
*
* @example
*
* ```TypeScript
* // ...
* await contentRouting.provide(cid)
* ```
*/
provide(cid: CID, options?: RoutingOptions): Promise<void>
/**
* Find the providers of the passed CID.
*
* @example
*
* ```TypeScript
* // 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<PeerInfo>
/**
* 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
*
* ```TypeScript
* // ...
* 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
*
* ```TypeScript
* // ...
*
* const key = '/key'
* const value = await contentRouting.get(key)
* ```
*/
get(key: Uint8Array, options?: RoutingOptions): Promise<Uint8Array>
}