From bccf33f80ae8f1eefca3655225e429777511ea2e Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Wed, 17 Jul 2024 11:42:13 -0300 Subject: [PATCH] feat(storages): remove PoolStore, use NPool --- src/storages.ts | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/storages.ts b/src/storages.ts index 418bdd5e..e0039973 100644 --- a/src/storages.ts +++ b/src/storages.ts @@ -1,18 +1,17 @@ // deno-lint-ignore-file require-await -import { RelayPoolWorker } from 'nostr-relaypool'; - import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; import { AdminStore } from '@/storages/AdminStore.ts'; import { EventsDB } from '@/storages/EventsDB.ts'; -import { PoolStore } from '@/storages/pool-store.ts'; import { SearchStore } from '@/storages/search-store.ts'; import { InternalRelay } from '@/storages/InternalRelay.ts'; +import { NPool, NRelay1 } from '@nostrify/nostrify'; +import { getRelays } from '@/utils/outbox.ts'; export class Storages { private static _db: Promise | undefined; private static _admin: Promise | undefined; - private static _client: Promise | undefined; + private static _client: Promise | undefined; private static _pubsub: Promise | undefined; private static _search: Promise | undefined; @@ -44,7 +43,7 @@ export class Storages { } /** Relay pool storage. */ - public static async client(): Promise { + public static async client(): Promise { if (!this._client) { this._client = (async () => { const db = await this.db(); @@ -64,22 +63,22 @@ export class Storages { console.log(`pool: connecting to ${activeRelays.length} relays.`); - const worker = new Worker('https://unpkg.com/nostr-relaypool2@0.6.34/lib/nostr-relaypool.worker.js', { - type: 'module', - }); + return new NPool({ + open(url) { + return new NRelay1(url); + }, + reqRouter: async (filters) => { + return new Map(activeRelays.map((relay) => { + return [relay, filters]; + })); + }, + eventRouter: async (event) => { + const relaySet = await getRelays(await Storages.db(), event.pubkey); + relaySet.delete(Conf.relay); - // @ts-ignore Wrong types. - const pool = new RelayPoolWorker(worker, activeRelays, { - autoReconnect: true, - // The pipeline verifies events. - skipVerification: true, - // The logging feature overwhelms the CPU and creates too many logs. - logErrorsAndNotices: false, - }); - - return new PoolStore({ - pool, - relays: activeRelays, + const relays = [...relaySet].slice(0, 4); + return relays; + }, }); })(); }