fix: only run function inject for nostr entity

This commit is contained in:
2024-01-10 14:40:11 +00:00
parent 576c9a2b9e
commit 721edb6527

View File

@ -1,34 +1,37 @@
interface Env {} interface Env {}
export const onRequest: PagesFunction<Env> = async context => { export const onRequest: PagesFunction<Env> = 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(); const next = await context.next();
try { if (id && prefixes.some(a => id.startsWith(a))) {
const rsp = await fetch( try {
`http://nostr.api.v0l.io/api/v1/opengraph/${id}?canonical=${encodeURIComponent("https://zap.stream/%s")}`, 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(), method: "POST",
headers: { body: await next.arrayBuffer(),
"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: { headers: {
"user-agent": "zap.stream/1.0 (https://zap.stream)",
"content-type": "text/html", "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; return next;
}; };