-
Notifications
You must be signed in to change notification settings - Fork 446
/
index.ts
104 lines (89 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
import { CodeError } from '@libp2p/interface/errors'
import merge from 'it-merge'
import { pipe } from 'it-pipe'
import { messages, codes } from '../errors.js'
import {
storeAddresses,
uniquePeers,
requirePeers
} from './utils.js'
import type { AbortOptions } from '@libp2p/interface'
import type { ContentRouting } from '@libp2p/interface/content-routing'
import type { PeerInfo } from '@libp2p/interface/peer-info'
import type { PeerStore } from '@libp2p/interface/peer-store'
import type { Startable } from '@libp2p/interface/startable'
import type { CID } from 'multiformats/cid'
export interface CompoundContentRoutingInit {
routers: ContentRouting[]
}
export interface CompoundContentRoutingComponents {
peerStore: PeerStore
}
export class CompoundContentRouting implements ContentRouting, Startable {
private readonly routers: ContentRouting[]
private started: boolean
private readonly components: CompoundContentRoutingComponents
constructor (components: CompoundContentRoutingComponents, init: CompoundContentRoutingInit) {
this.routers = init.routers ?? []
this.started = false
this.components = components
}
isStarted (): boolean {
return this.started
}
async start (): Promise<void> {
this.started = true
}
async stop (): Promise<void> {
this.started = false
}
/**
* Iterates over all content routers in parallel to find providers of the given key
*/
async * findProviders (key: CID, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
if (this.routers.length === 0) {
throw new CodeError('No content routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
}
yield * pipe(
merge(
...this.routers.map(router => router.findProviders(key, options))
),
(source) => storeAddresses(source, this.components.peerStore),
(source) => uniquePeers(source),
(source) => requirePeers(source)
)
}
/**
* Iterates over all content routers in parallel to notify it is
* a provider of the given key
*/
async provide (key: CID, options: AbortOptions = {}): Promise<void> {
if (this.routers.length === 0) {
throw new CodeError('No content routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
}
await Promise.all(this.routers.map(async (router) => { await router.provide(key, options) }))
}
/**
* Store the given key/value pair in the available content routings
*/
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> {
if (!this.isStarted()) {
throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
}
await Promise.all(this.routers.map(async (router) => {
await router.put(key, value, options)
}))
}
/**
* Get the value to the given key.
* Times out after 1 minute by default.
*/
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> {
if (!this.isStarted()) {
throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
}
return Promise.any(this.routers.map(async (router) => {
return router.get(key, options)
}))
}
}