|
|
|
@ -1,12 +1,8 @@ |
|
|
|
import { MONITOR, MONITOR_RELAYS } from '@/constants' |
|
|
|
import { simplifyUrl } from '@/lib/url' |
|
|
|
import { tagNameEquals } from '@/lib/tag' |
|
|
|
import indexDb from '@/services/indexed-db.service' |
|
|
|
import { isWebsocketUrl, simplifyUrl } from '@/lib/url' |
|
|
|
import { TAwesomeRelayCollection, TRelayInfo } from '@/types' |
|
|
|
import { TNip66RelayInfo, TRelayInfo } from '@/types' |
|
|
|
|
|
|
|
import DataLoader from 'dataloader' |
|
|
|
import DataLoader from 'dataloader' |
|
|
|
import FlexSearch from 'flexsearch' |
|
|
|
import FlexSearch from 'flexsearch' |
|
|
|
import { Event } from 'nostr-tools' |
|
|
|
|
|
|
|
import client from './client.service' |
|
|
|
|
|
|
|
import indexedDb from './indexed-db.service' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RelayInfoService { |
|
|
|
class RelayInfoService { |
|
|
|
static instance: RelayInfoService |
|
|
|
static instance: RelayInfoService |
|
|
|
@ -14,14 +10,13 @@ class RelayInfoService { |
|
|
|
public static getInstance(): RelayInfoService { |
|
|
|
public static getInstance(): RelayInfoService { |
|
|
|
if (!RelayInfoService.instance) { |
|
|
|
if (!RelayInfoService.instance) { |
|
|
|
RelayInfoService.instance = new RelayInfoService() |
|
|
|
RelayInfoService.instance = new RelayInfoService() |
|
|
|
RelayInfoService.instance.init() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return RelayInfoService.instance |
|
|
|
return RelayInfoService.instance |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private initPromise: Promise<void> | null = null |
|
|
|
private initPromise: Promise<void> | null = null |
|
|
|
|
|
|
|
private awesomeRelayCollections: Promise<TAwesomeRelayCollection[]> | null = null |
|
|
|
private relayInfoMap = new Map<string, TNip66RelayInfo>() |
|
|
|
private relayInfoMap = new Map<string, TRelayInfo>() |
|
|
|
private relayInfoIndex = new FlexSearch.Index({ |
|
|
|
private relayInfoIndex = new FlexSearch.Index({ |
|
|
|
tokenize: 'forward', |
|
|
|
tokenize: 'forward', |
|
|
|
encode: (str) => |
|
|
|
encode: (str) => |
|
|
|
@ -32,19 +27,15 @@ class RelayInfoService { |
|
|
|
.toLocaleLowerCase() |
|
|
|
.toLocaleLowerCase() |
|
|
|
.split(/\s+/) |
|
|
|
.split(/\s+/) |
|
|
|
}) |
|
|
|
}) |
|
|
|
private fetchDataloader = new DataLoader<string, TNip66RelayInfo | undefined>( |
|
|
|
private fetchDataloader = new DataLoader<string, TRelayInfo | undefined>( |
|
|
|
(urls) => Promise.all(urls.map((url) => this._getRelayInfo(url))), |
|
|
|
async (urls) => { |
|
|
|
|
|
|
|
const results = await Promise.allSettled(urls.map((url) => this._getRelayInfo(url))) |
|
|
|
|
|
|
|
return results.map((res) => (res.status === 'fulfilled' ? res.value : undefined)) |
|
|
|
|
|
|
|
}, |
|
|
|
{ maxBatchSize: 1 } |
|
|
|
{ maxBatchSize: 1 } |
|
|
|
) |
|
|
|
) |
|
|
|
private relayUrlsForRandom: string[] = [] |
|
|
|
private relayUrlsForRandom: string[] = [] |
|
|
|
|
|
|
|
|
|
|
|
async init() { |
|
|
|
|
|
|
|
if (!this.initPromise) { |
|
|
|
|
|
|
|
this.initPromise = this.loadRelayInfos() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
await this.initPromise |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async search(query: string) { |
|
|
|
async search(query: string) { |
|
|
|
if (this.initPromise) { |
|
|
|
if (this.initPromise) { |
|
|
|
await this.initPromise |
|
|
|
await this.initPromise |
|
|
|
@ -60,9 +51,7 @@ class RelayInfoService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const result = await this.relayInfoIndex.searchAsync(query) |
|
|
|
const result = await this.relayInfoIndex.searchAsync(query) |
|
|
|
return result |
|
|
|
return result.map((url) => this.relayInfoMap.get(url as string)).filter(Boolean) as TRelayInfo[] |
|
|
|
.map((url) => this.relayInfoMap.get(url as string)) |
|
|
|
|
|
|
|
.filter(Boolean) as TNip66RelayInfo[] |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getRelayInfos(urls: string[]) { |
|
|
|
async getRelayInfos(urls: string[]) { |
|
|
|
@ -82,7 +71,7 @@ class RelayInfoService { |
|
|
|
await this.initPromise |
|
|
|
await this.initPromise |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const relayInfos: TNip66RelayInfo[] = [] |
|
|
|
const relayInfos: TRelayInfo[] = [] |
|
|
|
while (relayInfos.length < count) { |
|
|
|
while (relayInfos.length < count) { |
|
|
|
const randomIndex = Math.floor(Math.random() * this.relayUrlsForRandom.length) |
|
|
|
const randomIndex = Math.floor(Math.random() * this.relayUrlsForRandom.length) |
|
|
|
const url = this.relayUrlsForRandom[randomIndex] |
|
|
|
const url = this.relayUrlsForRandom[randomIndex] |
|
|
|
@ -99,146 +88,81 @@ class RelayInfoService { |
|
|
|
return relayInfos |
|
|
|
return relayInfos |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getAwesomeRelayCollections() { |
|
|
|
|
|
|
|
if (this.awesomeRelayCollections) return this.awesomeRelayCollections |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.awesomeRelayCollections = (async () => { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const res = await fetch( |
|
|
|
|
|
|
|
'https://raw.githubusercontent.com/CodyTseng/awesome-nostr-relays/master/dist/collections.json' |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
if (!res.ok) { |
|
|
|
|
|
|
|
throw new Error('Failed to fetch awesome relay collections') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const data = (await res.json()) as { collections: TAwesomeRelayCollection[] } |
|
|
|
|
|
|
|
return data.collections |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
|
|
console.error('Error fetching awesome relay collections:', error) |
|
|
|
|
|
|
|
return [] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
})() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.awesomeRelayCollections |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async _getRelayInfo(url: string) { |
|
|
|
private async _getRelayInfo(url: string) { |
|
|
|
const exist = this.relayInfoMap.get(url) |
|
|
|
const exist = this.relayInfoMap.get(url) |
|
|
|
if (exist && (exist.hasNip11 || exist.triedNip11)) { |
|
|
|
if (exist) { |
|
|
|
return exist |
|
|
|
return exist |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const nip11 = await this.fetchRelayInfoByNip11(url) |
|
|
|
const storedRelayInfo = await indexDb.getRelayInfo(url) |
|
|
|
const relayInfo = nip11 |
|
|
|
if (storedRelayInfo) { |
|
|
|
? { |
|
|
|
return await this.addRelayInfo(storedRelayInfo) |
|
|
|
...nip11, |
|
|
|
} |
|
|
|
url, |
|
|
|
|
|
|
|
shortUrl: simplifyUrl(url), |
|
|
|
const nip11 = await this.fetchRelayNip11(url) |
|
|
|
hasNip11: Object.keys(nip11).length > 0, |
|
|
|
const relayInfo = { |
|
|
|
triedNip11: true |
|
|
|
...(nip11 ?? {}), |
|
|
|
} |
|
|
|
url, |
|
|
|
: { |
|
|
|
shortUrl: simplifyUrl(url) |
|
|
|
url, |
|
|
|
} |
|
|
|
shortUrl: simplifyUrl(url), |
|
|
|
|
|
|
|
hasNip11: false, |
|
|
|
|
|
|
|
triedNip11: true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return await this.addRelayInfo(relayInfo) |
|
|
|
return await this.addRelayInfo(relayInfo) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async fetchRelayInfoByNip11(url: string) { |
|
|
|
private async fetchRelayNip11(url: string) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
console.log('Fetching NIP-11 for', url) |
|
|
|
const res = await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), { |
|
|
|
const res = await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), { |
|
|
|
headers: { Accept: 'application/nostr+json' } |
|
|
|
headers: { Accept: 'application/nostr+json' } |
|
|
|
}) |
|
|
|
}) |
|
|
|
return res.json() as TRelayInfo |
|
|
|
return res.json() as Omit<TRelayInfo, 'url' | 'shortUrl'> |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
return undefined |
|
|
|
return undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async loadRelayInfos() { |
|
|
|
private async addRelayInfo(relayInfo: TRelayInfo) { |
|
|
|
const localRelayInfos = await indexedDb.getAllRelayInfoEvents() |
|
|
|
if (!Array.isArray(relayInfo.supported_nips)) { |
|
|
|
const relayInfos = formatRelayInfoEvents(localRelayInfos) |
|
|
|
relayInfo.supported_nips = [] |
|
|
|
relayInfos.forEach((relayInfo) => this.addRelayInfo(relayInfo)) |
|
|
|
|
|
|
|
this.relayUrlsForRandom = Array.from(this.relayInfoMap.keys()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const loadFromInternet = async (slowFetch: boolean = true) => { |
|
|
|
|
|
|
|
let until: number = Math.round(Date.now() / 1000) |
|
|
|
|
|
|
|
const since = until - 60 * 60 * 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (until) { |
|
|
|
|
|
|
|
const relayInfoEvents = await client.fetchEvents(MONITOR_RELAYS, { |
|
|
|
|
|
|
|
authors: [MONITOR], |
|
|
|
|
|
|
|
kinds: [30166], |
|
|
|
|
|
|
|
since, |
|
|
|
|
|
|
|
until, |
|
|
|
|
|
|
|
limit: slowFetch ? 100 : 1000 |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
const events = relayInfoEvents.sort((a, b) => b.created_at - a.created_at) |
|
|
|
|
|
|
|
if (events.length === 0) { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (const event of events) { |
|
|
|
|
|
|
|
await indexedDb.putRelayInfoEvent(event) |
|
|
|
|
|
|
|
const relayInfo = formatRelayInfoEvents([event])[0] |
|
|
|
|
|
|
|
await this.addRelayInfo(relayInfo) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
until = events[events.length - 1].created_at - 1 |
|
|
|
|
|
|
|
if (slowFetch) { |
|
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.relayUrlsForRandom = Array.from(this.relayInfoMap.keys()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (localRelayInfos.length === 0) { |
|
|
|
|
|
|
|
await loadFromInternet(false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
setTimeout(loadFromInternet, 1000 * 20) // 20 seconds
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async addRelayInfo(relayInfo: TNip66RelayInfo) { |
|
|
|
|
|
|
|
const oldRelayInfo = this.relayInfoMap.get(relayInfo.url) |
|
|
|
|
|
|
|
const newRelayInfo = oldRelayInfo |
|
|
|
|
|
|
|
? { |
|
|
|
|
|
|
|
...oldRelayInfo, |
|
|
|
|
|
|
|
...relayInfo, |
|
|
|
|
|
|
|
hasNip11: oldRelayInfo.hasNip11 || relayInfo.hasNip11, |
|
|
|
|
|
|
|
triedNip11: oldRelayInfo.triedNip11 || relayInfo.triedNip11 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
: relayInfo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(newRelayInfo.supported_nips)) { |
|
|
|
|
|
|
|
newRelayInfo.supported_nips = [] |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.relayInfoMap.set(newRelayInfo.url, newRelayInfo) |
|
|
|
this.relayInfoMap.set(relayInfo.url, relayInfo) |
|
|
|
await this.relayInfoIndex.addAsync( |
|
|
|
await Promise.allSettled([ |
|
|
|
newRelayInfo.url, |
|
|
|
this.relayInfoIndex.addAsync( |
|
|
|
[ |
|
|
|
relayInfo.url, |
|
|
|
newRelayInfo.shortUrl, |
|
|
|
[ |
|
|
|
...newRelayInfo.shortUrl.split('.'), |
|
|
|
relayInfo.shortUrl, |
|
|
|
newRelayInfo.name ?? '', |
|
|
|
...relayInfo.shortUrl.split('.'), |
|
|
|
newRelayInfo.description ?? '' |
|
|
|
relayInfo.name ?? '', |
|
|
|
].join(' ') |
|
|
|
relayInfo.description ?? '' |
|
|
|
) |
|
|
|
].join(' ') |
|
|
|
return newRelayInfo |
|
|
|
), |
|
|
|
|
|
|
|
indexDb.putRelayInfo(relayInfo) |
|
|
|
|
|
|
|
]) |
|
|
|
|
|
|
|
return relayInfo |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const instance = RelayInfoService.getInstance() |
|
|
|
const instance = RelayInfoService.getInstance() |
|
|
|
export default instance |
|
|
|
export default instance |
|
|
|
|
|
|
|
|
|
|
|
function formatRelayInfoEvents(relayInfoEvents: Event[]) { |
|
|
|
|
|
|
|
const urlSet = new Set<string>() |
|
|
|
|
|
|
|
const relayInfos: TNip66RelayInfo[] = [] |
|
|
|
|
|
|
|
relayInfoEvents.forEach((event) => { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const url = event.tags.find(tagNameEquals('d'))?.[1] |
|
|
|
|
|
|
|
if (!url || urlSet.has(url) || !isWebsocketUrl(url)) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
urlSet.add(url) |
|
|
|
|
|
|
|
const basicInfo = event.content ? (JSON.parse(event.content) as TRelayInfo) : {} |
|
|
|
|
|
|
|
const tagInfo: Omit<TNip66RelayInfo, 'url' | 'shortUrl'> = { |
|
|
|
|
|
|
|
hasNip11: Object.keys(basicInfo).length > 0, |
|
|
|
|
|
|
|
triedNip11: false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
event.tags.forEach((tag) => { |
|
|
|
|
|
|
|
if (tag[0] === 'T') { |
|
|
|
|
|
|
|
tagInfo.relayType = tag[1] |
|
|
|
|
|
|
|
} else if (tag[0] === 'g' && tag[2] === 'countryCode') { |
|
|
|
|
|
|
|
tagInfo.countryCode = tag[1] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
relayInfos.push({ |
|
|
|
|
|
|
|
...basicInfo, |
|
|
|
|
|
|
|
...tagInfo, |
|
|
|
|
|
|
|
url, |
|
|
|
|
|
|
|
shortUrl: simplifyUrl(url) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
|
|
console.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return relayInfos |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|