From 721edb65273509c0a88e63667fc05db5487df86b Mon Sep 17 00:00:00 2001 From: Kieran Date: Wed, 10 Jan 2024 14:40:11 +0000 Subject: [PATCH] fix: only run function inject for nostr entity --- functions/[id].ts | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/functions/[id].ts b/functions/[id].ts index c41e229..acbf450 100644 --- a/functions/[id].ts +++ b/functions/[id].ts @@ -1,34 +1,37 @@ interface Env {} export const onRequest: PagesFunction = async context => { - const id = context.params.id as string; + const id = context.params.id as string | undefined; + const prefixes = ["npub1", "nprofile1", "naddr1", "nevent1", "note1"]; const next = await context.next(); - try { - const rsp = await fetch( - `http://nostr.api.v0l.io/api/v1/opengraph/${id}?canonical=${encodeURIComponent("https://zap.stream/%s")}`, - { - method: "POST", - body: await next.arrayBuffer(), - headers: { - "user-agent": "zap.stream/1.0 (https://zap.stream)", - "content-type": "text/html", - accept: "text/html", - }, - } - ); - if (rsp.ok) { - const body = await rsp.text(); - if (body.length > 0) { - return new Response(body, { + if (id && prefixes.some(a => id.startsWith(a))) { + try { + const rsp = await fetch( + `http://nostr.api.v0l.io/api/v1/opengraph/${id}?canonical=${encodeURIComponent("https://zap.stream/%s")}`, + { + method: "POST", + body: await next.arrayBuffer(), headers: { + "user-agent": "zap.stream/1.0 (https://zap.stream)", "content-type": "text/html", + accept: "text/html", }, - }); + } + ); + if (rsp.ok) { + const body = await rsp.text(); + if (body.length > 0) { + return new Response(body, { + headers: { + "content-type": "text/html", + }, + }); + } } + } catch { + // ignore } - } catch { - // ignore } return next; };