From 05534d532bd48f159f4e0e6cd1d4099e99a5dfda Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 25 Apr 2024 18:28:19 -0500 Subject: [PATCH] APISigner: refactor with InternalRelay --- src/signers/APISigner.ts | 36 +++++++++++++++++------------------ src/storages.ts | 13 +++++++++++++ src/storages/InternalRelay.ts | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/signers/APISigner.ts b/src/signers/APISigner.ts index 54280434..43221421 100644 --- a/src/signers/APISigner.ts +++ b/src/signers/APISigner.ts @@ -6,7 +6,7 @@ import { Stickynotes } from '@/deps.ts'; import { connectResponseSchema } from '@/schemas/nostr.ts'; import { jsonSchema } from '@/schema.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts'; -import { Sub } from '@/subs.ts'; +import { Storages } from '@/storages.ts'; import { eventMatchesTemplate } from '@/utils.ts'; import { createAdminEvent } from '@/utils/api.ts'; @@ -78,27 +78,25 @@ export class APISigner implements NostrSigner { messageId: string, template: Omit, ): Promise { - const sub = Sub.sub(messageId, '1', [{ kinds: [24133], authors: [pubkey], '#p': [Conf.pubkey] }]); + const sub = Storages.pubsub.req( + [{ kinds: [24133], authors: [pubkey], '#p': [Conf.pubkey] }], + { signal: this.#c.req.raw.signal }, + ); - const close = (): void => { - Sub.close(messageId); - this.#c.req.raw.signal.removeEventListener('abort', close); - }; + for await (const msg of sub) { + if (msg[0] === 'EVENT') { + const event = msg[2]; + const decrypted = await new AdminSigner().nip04.decrypt(event.pubkey, event.content); - this.#c.req.raw.signal.addEventListener('abort', close); + const result = jsonSchema + .pipe(connectResponseSchema) + .refine((msg) => msg.id === messageId, 'Message ID mismatch') + .refine((msg) => eventMatchesTemplate(msg.result, template), 'Event template mismatch') + .safeParse(decrypted); - for await (const event of sub) { - const decrypted = await new AdminSigner().nip04.decrypt(event.pubkey, event.content); - - const result = jsonSchema - .pipe(connectResponseSchema) - .refine((msg) => msg.id === messageId, 'Message ID mismatch') - .refine((msg) => eventMatchesTemplate(msg.result, template), 'Event template mismatch') - .safeParse(decrypted); - - if (result.success) { - close(); - return result.data.result; + if (result.success) { + return result.data.result; + } } } diff --git a/src/storages.ts b/src/storages.ts index 107aa170..32d8dd2a 100644 --- a/src/storages.ts +++ b/src/storages.ts @@ -8,6 +8,7 @@ import { Optimizer } from '@/storages/optimizer.ts'; import { PoolStore } from '@/storages/pool-store.ts'; import { Reqmeister } from '@/storages/reqmeister.ts'; import { SearchStore } from '@/storages/search-store.ts'; +import { InternalRelay } from '@/storages/InternalRelay.ts'; import { Time } from '@/utils/time.ts'; /** Relay pool storage. */ @@ -43,4 +44,16 @@ const searchStore = new SearchStore({ fallback: optimizer, }); +export class Storages { + private static _subsub: InternalRelay | undefined; + + static get pubsub(): InternalRelay { + if (!this._subsub) { + this._subsub = new InternalRelay(); + } + + return this._subsub; + } +} + export { cache, client, eventsDB, optimizer, reqmeister, searchStore }; diff --git a/src/storages/InternalRelay.ts b/src/storages/InternalRelay.ts index 70aba29e..e2f4fadf 100644 --- a/src/storages/InternalRelay.ts +++ b/src/storages/InternalRelay.ts @@ -23,7 +23,7 @@ export class InternalRelay implements NRelay { async *req( filters: NostrFilter[], - opts: { signal?: AbortSignal }, + opts?: { signal?: AbortSignal }, ): AsyncGenerator { const id = crypto.randomUUID(); const machina = new Machina(opts?.signal);