-
Notifications
You must be signed in to change notification settings - Fork 17
/
write-relays.ts
59 lines (55 loc) · 1.54 KB
/
write-relays.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
export class WriteRelaysPerPubkey {
data: Map<string, string[]>;
promises: Map<string, Promise<string[]>>;
servers: string[];
constructor(servers?: string[]) {
this.data = new Map();
this.promises = new Map();
this.servers = servers || ["https://us.rbr.bio", "https://eu.rbr.bio"];
}
async get(pubkey: string): Promise<string[]> {
let value = this.data.get(pubkey);
if (value) {
return Promise.resolve(value);
}
const promise = this.promises.get(pubkey);
if (promise) {
return promise;
}
const rs = [];
for (let server of this.servers) {
rs.push(fetchWriteRelays(server, pubkey));
}
const r: Promise<string[]> = firstGoodPromise(rs);
r.then((x: string[]) => {
this.data.set(pubkey, x);
this.promises.delete(pubkey);
});
this.promises.set(pubkey, r);
return r;
}
}
function fetchWriteRelays(server: string, pubkey: string): Promise<string[]> {
const url = `${server}/${pubkey}/writerelays.json`;
return fetchJSON(url);
}
async function fetchJSON(url: string) {
return fetch(url)
.then((response) => response.json())
.catch((e) => {
throw new Error("error fetching " + url + " " + e);
});
}
function firstGoodPromise<T>(promises: Promise<T>[]): Promise<T> {
return new Promise((resolve, reject) => {
let rejects: any[] = [];
promises.forEach((p) => {
p.then(resolve).catch((rej) => {
rejects.push(rej);
if (rejects.length === promises.length) {
reject(rejects);
}
});
});
});
}