forked from adamritter/nostr-relaypool-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata-cache.ts
61 lines (56 loc) · 1.51 KB
/
metadata-cache.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 {Event} from "nostr-tools";
export class MetadataCache {
data: Map<string, Event>;
promises: Map<string, Promise<Event>>;
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<Event> {
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(fetchMetadata(server, pubkey));
}
const r = firstGoodPromise(rs);
r.then((x) => {
this.data.set(pubkey, x);
this.promises.delete(pubkey);
});
this.promises.set(pubkey, r);
return r;
}
}
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);
}
});
});
});
}
function fetchMetadata(server: string, pubkey: string) {
const url = `${server}/${pubkey}/metadata.json`;
return fetchJSON(url);
}