UNPKG

nostr-web-components

Version:

collection of web components that provide quick access to basic nostr things

55 lines (54 loc) 1.44 kB
import LRUCache from "mnemonist/lru-cache"; import { SimplePool } from "nostr-tools"; import { normalizeURL } from "nostr-tools/utils"; const pool = window.nostrSharedPool || new SimplePool(); const relayListsCache = window.nostrMetadataCache || new LRUCache(1e3); const fallbackRelays = [ "wss://relay.nostr.band", "wss://relay.damus.io", "wss://relay.primal.net", "wss://nos.lol", "wss://cache2.primal.net/v1", "wss://relay.nostr.bg" ].map(normalizeURL); function getOutboxRelaysFor(pubkey) { let rl = relayListsCache.get(pubkey); if (rl) return rl; let promise = new Promise( (resolve) => pool.subscribeManyEose( ["wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"], [ { kinds: [10002], authors: [pubkey] } ], { onevent(evt) { try { resolve( evt.tags.filter((tag) => tag[0] === "r" && (tag[2] === void 0 || tag[2] === "write")).map((tag) => normalizeURL(tag[1])) ); } catch (err) { } }, onclose() { resolve([]); } } ) ); relayListsCache.set(pubkey, promise); return promise; } function appendFallback(relays) { let next = fallbackRelays[Math.floor(Math.random() * fallbackRelays.length)]; if (relays.includes(next)) return; relays.push(next); } export { appendFallback, getOutboxRelaysFor };