-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathindex.ts
61 lines (57 loc) · 1.63 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
import type { RoutingOptions } from '../index.js'
import type { PeerId } from '../peer-id/index.js'
import type { PeerInfo } from '../peer-info/index.js'
/**
* Any object that implements this Symbol as a property should return a
* PeerRouting instance as the property value, similar to how
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
*
* @example
*
* ```TypeScript
* import { peerRouting, PeerRouting } from '@libp2p/peer-routing'
*
* class MyPeerRouter implements PeerRouting {
* get [peerRouting] () {
* return this
* }
*
* // ...other methods
* }
* ```
*/
export const peerRoutingSymbol = Symbol.for('@libp2p/peer-routing')
/**
* Implementers of this interface can provide a PeerRouting implementation to
* interested callers.
*/
export interface PeerRoutingProvider {
[peerRoutingSymbol]: PeerRouting
}
export interface PeerRouting {
/**
* Searches the network for peer info corresponding to the passed peer id.
*
* @example
*
* ```TypeScript
* // ...
* 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
*
* ```TypeScript
* // 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>
}