APISigner: refactor with InternalRelay
This commit is contained in:
parent
7aa931a69e
commit
05534d532b
|
@ -6,7 +6,7 @@ import { Stickynotes } from '@/deps.ts';
|
||||||
import { connectResponseSchema } from '@/schemas/nostr.ts';
|
import { connectResponseSchema } from '@/schemas/nostr.ts';
|
||||||
import { jsonSchema } from '@/schema.ts';
|
import { jsonSchema } from '@/schema.ts';
|
||||||
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
||||||
import { Sub } from '@/subs.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
import { eventMatchesTemplate } from '@/utils.ts';
|
import { eventMatchesTemplate } from '@/utils.ts';
|
||||||
import { createAdminEvent } from '@/utils/api.ts';
|
import { createAdminEvent } from '@/utils/api.ts';
|
||||||
|
|
||||||
|
@ -78,16 +78,14 @@ export class APISigner implements NostrSigner {
|
||||||
messageId: string,
|
messageId: string,
|
||||||
template: Omit<NostrEvent, 'id' | 'pubkey' | 'sig'>,
|
template: Omit<NostrEvent, 'id' | 'pubkey' | 'sig'>,
|
||||||
): Promise<NostrEvent> {
|
): Promise<NostrEvent> {
|
||||||
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 => {
|
for await (const msg of sub) {
|
||||||
Sub.close(messageId);
|
if (msg[0] === 'EVENT') {
|
||||||
this.#c.req.raw.signal.removeEventListener('abort', close);
|
const event = msg[2];
|
||||||
};
|
|
||||||
|
|
||||||
this.#c.req.raw.signal.addEventListener('abort', close);
|
|
||||||
|
|
||||||
for await (const event of sub) {
|
|
||||||
const decrypted = await new AdminSigner().nip04.decrypt(event.pubkey, event.content);
|
const decrypted = await new AdminSigner().nip04.decrypt(event.pubkey, event.content);
|
||||||
|
|
||||||
const result = jsonSchema
|
const result = jsonSchema
|
||||||
|
@ -97,10 +95,10 @@ export class APISigner implements NostrSigner {
|
||||||
.safeParse(decrypted);
|
.safeParse(decrypted);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
close();
|
|
||||||
return result.data.result;
|
return result.data.result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
throw new HTTPException(408, {
|
throw new HTTPException(408, {
|
||||||
res: this.#c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
res: this.#c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
||||||
|
|
|
@ -8,6 +8,7 @@ import { Optimizer } from '@/storages/optimizer.ts';
|
||||||
import { PoolStore } from '@/storages/pool-store.ts';
|
import { PoolStore } from '@/storages/pool-store.ts';
|
||||||
import { Reqmeister } from '@/storages/reqmeister.ts';
|
import { Reqmeister } from '@/storages/reqmeister.ts';
|
||||||
import { SearchStore } from '@/storages/search-store.ts';
|
import { SearchStore } from '@/storages/search-store.ts';
|
||||||
|
import { InternalRelay } from '@/storages/InternalRelay.ts';
|
||||||
import { Time } from '@/utils/time.ts';
|
import { Time } from '@/utils/time.ts';
|
||||||
|
|
||||||
/** Relay pool storage. */
|
/** Relay pool storage. */
|
||||||
|
@ -43,4 +44,16 @@ const searchStore = new SearchStore({
|
||||||
fallback: optimizer,
|
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 };
|
export { cache, client, eventsDB, optimizer, reqmeister, searchStore };
|
||||||
|
|
|
@ -23,7 +23,7 @@ export class InternalRelay implements NRelay {
|
||||||
|
|
||||||
async *req(
|
async *req(
|
||||||
filters: NostrFilter[],
|
filters: NostrFilter[],
|
||||||
opts: { signal?: AbortSignal },
|
opts?: { signal?: AbortSignal },
|
||||||
): AsyncGenerator<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED> {
|
): AsyncGenerator<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED> {
|
||||||
const id = crypto.randomUUID();
|
const id = crypto.randomUUID();
|
||||||
const machina = new Machina<NostrEvent>(opts?.signal);
|
const machina = new Machina<NostrEvent>(opts?.signal);
|
||||||
|
|
Loading…
Reference in New Issue