fix: opengraph middleware
This commit is contained in:
parent
8cca297d6d
commit
326ce2ba68
@ -28,19 +28,16 @@ export const onRequest: PagesFunction<Env> = async context => {
|
|||||||
const u = new URL(context.request.url);
|
const u = new URL(context.request.url);
|
||||||
|
|
||||||
const prefixes = ["npub1", "nprofile1", "naddr1", "nevent1", "note1"];
|
const prefixes = ["npub1", "nprofile1", "naddr1", "nevent1", "note1"];
|
||||||
const isEntityPath = () => {
|
const isEntityPath = prefixes.some(
|
||||||
return prefixes.some(
|
a => u.pathname.startsWith(`/${a}`) || u.pathname.startsWith(`/e/${a}`) || u.pathname.startsWith(`/p/${a}`),
|
||||||
a => u.pathname.startsWith(`/${a}`) || u.pathname.startsWith(`/e/${a}`) || u.pathname.startsWith(`/p/${a}`),
|
);
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const nostrAddress = u.pathname.match(/^\/([a-zA-Z0-9_]+)$/i);
|
const nostrAddress = u.pathname.match(/^\/([a-zA-Z0-9_]+)$/i);
|
||||||
|
|
||||||
const next = await context.next();
|
const next = await context.next();
|
||||||
if (u.pathname != "/" && (isEntityPath() || nostrAddress)) {
|
if (u.pathname != "/" && (isEntityPath || nostrAddress)) {
|
||||||
|
//console.log("Handeling path: ", u.pathname, isEntityPath, nostrAddress[1]);
|
||||||
try {
|
try {
|
||||||
let id = nostrAddress ? nostrAddress[1] : u.pathname.split("/").at(-1);
|
let id = u.pathname.split("/").at(-1);
|
||||||
if (nostrAddress) {
|
if (!isEntityPath && nostrAddress) {
|
||||||
const pubkey = await fetchNostrAddress(id, HOST);
|
const pubkey = await fetchNostrAddress(id, HOST);
|
||||||
if (pubkey) {
|
if (pubkey) {
|
||||||
id = bech32.encode("npub", bech32.toWords(fromHex(pubkey)));
|
id = bech32.encode("npub", bech32.toWords(fromHex(pubkey)));
|
||||||
@ -48,18 +45,19 @@ export const onRequest: PagesFunction<Env> = async context => {
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const rsp = await fetch(
|
const fetchApi = `http://nostr.api.v0l.io/api/v1/opengraph/${id}?canonical=${encodeURIComponent(
|
||||||
`http://nostr.api.v0l.io/api/v1/opengraph/${id}?canonical=${encodeURIComponent(`https://${HOST}/%s`)}`,
|
`https://${HOST}/%s`,
|
||||||
{
|
)}`;
|
||||||
method: "POST",
|
console.log("Fetching tags from: ", fetchApi);
|
||||||
body: await next.arrayBuffer(),
|
const rsp = await fetch(fetchApi, {
|
||||||
headers: {
|
method: "POST",
|
||||||
"user-agent": `SnortFunctions/1.0 (https://${HOST})`,
|
body: await next.arrayBuffer(),
|
||||||
"content-type": "text/html",
|
headers: {
|
||||||
accept: "text/html",
|
"user-agent": `SnortFunctions/1.0 (https://${HOST})`,
|
||||||
},
|
"content-type": "text/html",
|
||||||
|
accept: "text/html",
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
if (rsp.ok) {
|
if (rsp.ok) {
|
||||||
const body = await rsp.text();
|
const body = await rsp.text();
|
||||||
if (body.length > 0) {
|
if (body.length > 0) {
|
||||||
@ -70,8 +68,8 @@ export const onRequest: PagesFunction<Env> = async context => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e) {
|
||||||
// ignore
|
console.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return next;
|
return next;
|
||||||
|
Loading…
Reference in New Issue
Block a user