-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
104 lines (95 loc) · 3.07 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
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @packageDocumentation
*
* An implementation of the Fetch protocol as described here: https://github.com/libp2p/specs/tree/master/fetch
*
* The fetch protocol is a simple protocol for requesting a value corresponding to a key from a peer.
*
* @example
*
* ```typescript
* import { createLibp2p } from 'libp2p'
* import { fetch } from '@libp2p/fetch'
* import { peerIdFromString } from '@libp2p/peer-id'
*
* const libp2p = await createLibp2p({
* services: {
* fetch: fetch()
* }
* })
*
* // Given a key (as a string) returns a value (as a Uint8Array), or undefined
* // if the key isn't found.
* // All keys must be prefixed by the same prefix, which will be used to find
* // the appropriate key lookup function.
* async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> {
* // app specific callback to lookup key-value pairs.
* return Uint8Array.from([0, 1, 2, 3, 4])
* }
*
* // Enable this peer to respond to fetch requests for keys that begin with
* // '/my_subsystem_key_prefix/'
* libp2p.services.fetch.registerLookupFunction('/my_subsystem_key_prefix/', my_subsystem_key_lookup)
*
* const key = '/my_subsystem_key_prefix/{...}'
* const peerDst = peerIdFromString('Qmfoo...')
*
* // Load the value from the remote peer, timing out after 10s
* const value = await libp2p.services.fetch.fetch(peerDst, key, {
* signal: AbortSignal.timeout(10_000)
* })
* ```
*/
import { Fetch as FetchClass } from './fetch.js'
import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface'
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
export interface FetchInit {
protocolPrefix?: string
maxInboundStreams?: number
maxOutboundStreams?: number
/**
* How long we should wait for a remote peer to send any data
*/
timeout?: number
}
export interface LookupFunction {
(key: string): Promise<Uint8Array | undefined>
}
export interface FetchComponents {
registrar: Registrar
connectionManager: ConnectionManager
logger: ComponentLogger
}
export interface Fetch {
/**
* Sends a request to fetch the value associated with the given key from the given peer
*/
fetch(peer: PeerId, key: string, options?: AbortOptions): Promise<Uint8Array | undefined>
/**
* Registers a new lookup callback that can map keys to values, for a given set of keys that
* share the same prefix
*
* @example
*
* ```TypeScript
* // ...
* libp2p.services.fetch.registerLookupFunction('/prefix', (key) => { ... })
* ```
*/
registerLookupFunction(prefix: string, lookup: LookupFunction): void
/**
* Registers a new lookup callback that can map keys to values, for a given set of keys that
* share the same prefix.
*
* @example
*
* ```TypeScript
* // ...
* libp2p.services.fetch.unregisterLookupFunction('/prefix')
* ```
*/
unregisterLookupFunction(prefix: string, lookup?: LookupFunction): void
}
export function fetch (init: FetchInit = {}): (components: FetchComponents) => Fetch {
return (components) => new FetchClass(components, init)
}