From 9bff7a5086b62a0f61d6ff2a84f891e591c18eb9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 13 May 2024 12:30:56 -0500 Subject: [PATCH] Fix some issues in pipeline and utils/api.ts --- deno.json | 2 +- src/pipeline.ts | 17 +++++++++-------- src/utils/api.ts | 6 ++---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/deno.json b/deno.json index 5d45588..0225510 100644 --- a/deno.json +++ b/deno.json @@ -20,7 +20,7 @@ "@db/sqlite": "jsr:@db/sqlite@^0.11.1", "@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.1", "@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0", - "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.17.1", + "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.19.0", "@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs", "@soapbox/kysely-deno-sqlite": "jsr:@soapbox/kysely-deno-sqlite@^2.1.0", "@soapbox/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0", diff --git a/src/pipeline.ts b/src/pipeline.ts index d05b09a..c086c92 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -35,8 +35,9 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise ${event.id}`); await hydrateEvent(event, signal); + await policyFilter(event); + await Promise.all([ - policyFilter(event), storeEvent(event, signal), parseMetadata(event, signal), processDeletions(event, signal), @@ -50,8 +51,8 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { - const UserPolicy = new MuteListPolicy(Conf.pubkey, Storages.admin); - const result = await UserPolicy.call(event); + const policy = new MuteListPolicy(Conf.pubkey, Storages.admin); + const result = await policy.call(event); debug(JSON.stringify(result)); @@ -59,11 +60,9 @@ async function policyFilter(event: NostrEvent): Promise { if (!ok) { const [prefix, ...rest] = reason.split(': '); if (['duplicate', 'pow', 'blocked', 'rate-limited', 'invalid'].includes(prefix)) { - const error = new RelayError(prefix as any, rest.join(': ')); - return Promise.reject(error); + throw new RelayError(prefix as RelayErrorPrefix, rest.join(': ')); } else { - const error = new RelayError('error', rest.join(': ')); - return Promise.reject(error); + throw new RelayError('error', rest.join(': ')); } } } @@ -272,9 +271,11 @@ async function streamOut(event: NostrEvent): Promise { } } +type RelayErrorPrefix = 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error'; + /** NIP-20 command line result. */ class RelayError extends Error { - constructor(prefix: 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error', message: string) { + constructor(prefix: RelayErrorPrefix, message: string) { super(`${prefix}: ${message}`); } } diff --git a/src/utils/api.ts b/src/utils/api.ts index cba7c66..8da87fb 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -103,10 +103,8 @@ async function updateAdminEvent( async function publishEvent(event: NostrEvent, c: AppContext): Promise { debug('EVENT', event); try { - await Promise.all([ - pipeline.handleEvent(event, c.req.raw.signal), - Storages.client.event(event), - ]); + await pipeline.handleEvent(event, c.req.raw.signal); + await Storages.client.event(event); } catch (e) { if (e instanceof pipeline.RelayError) { throw new HTTPException(422, {