From f2598ef8dfff0824037a02ec2fe53551569867d6 Mon Sep 17 00:00:00 2001 From: Nicholas Jitkoff Date: Thu, 28 Jul 2022 09:39:42 -0400 Subject: [PATCH] Handle errors in metadata --- netlify/edge-functions/metadata.js | 110 +++++++++++++++-------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/netlify/edge-functions/metadata.js b/netlify/edge-functions/metadata.js index 661a417..402d14c 100644 --- a/netlify/edge-functions/metadata.js +++ b/netlify/edge-functions/metadata.js @@ -18,64 +18,68 @@ function pathToMetadata(path) { } export default async (request, context) => { - const ua = request.headers.get("user-agent"); - let url = new URL(request.url); - let path = url.pathname; - let geo = context?.geo?.city + ", " + context?.geo?.subdivision?.code + ", " + context?.geo?.country?.code + try { + const ua = request.headers.get("user-agent"); + let url = new URL(request.url); + let path = url.pathname; + let geo = context?.geo?.city + ", " + context?.geo?.subdivision?.code + ", " + context?.geo?.country?.code - let uaArray = Deno.env.get("UA_ARRAY")?.split(",") || []; - let uaMatch = uaArray.some(a => ua.indexOf(a) != -1); + let uaArray = Deno.env.get("UA_ARRAY")?.split(",") || []; + let uaMatch = uaArray.some(a => ua.indexOf(a) != -1); - if (uaMatch) { - //console.log("Redirecting legacy client", ) - return new Response('', { status: 401 }); - } - - if (path != "/" ) { - // context.log(path, request.headers, geo); - let metadataBots = [ "Twitterbot", "curl", "facebookexternalhit", "Slackbot-LinkExpanding", "Discordbot", "snapchat"] - let isMetadataBot = metadataBots.some(bot => ua.indexOf(bot) != -1); + if (uaMatch) { + //console.log("Redirecting legacy client", ) + return new Response('', { status: 401 }); + } + + if (path != "/" ) { + // context.log(path, request.headers, geo); + let metadataBots = [ "Twitterbot", "curl", "facebookexternalhit", "Slackbot-LinkExpanding", "Discordbot", "snapchat"] + let isMetadataBot = metadataBots.some(bot => ua.indexOf(bot) != -1); - if (isMetadataBot && path.endsWith("/")) { - let info = pathToMetadata(path) + if (isMetadataBot && path.endsWith("/")) { + let info = pathToMetadata(path) - let content = ['']; - if (info.title) { content.push(`${info.title}`,``); } - if (info.s) { content.push(``); } - if (info.t) { content.push(``); } - if (info.d) { content.push(``,``); } - if (info.i) { - if (!info.i.startsWith("http")) info.i = atob(info.i.replace(/=/g,'')); - content.push(``); - content.push(``); - if (info.iw) content.push(``); - if (info.ih) content.push(``); - } - if (info.c) { content.push(``); } - if (info.v) { - if (!info.v.startsWith("http")) info.v = atob(info.v.replace(/=/g,'')); - content.push(``); - if (info.vw) content.push(``); - if (info.vh) content.push(``); - } - if (info.f) { - if (info.f.length > 9){ - if (!info.f.startsWith("http")) info.f = atob(info.f.replace(/=/g,'')); - content.push(``); - } else { - let codepoints = Array.from(info.f).map(c => c.codePointAt(0).toString(16)); - content.push(``); + let content = ['']; + if (info.title) { content.push(`${info.title}`,``); } + if (info.s) { content.push(``); } + if (info.t) { content.push(``); } + if (info.d) { content.push(``,``); } + if (info.i) { + if (!info.i.startsWith("http")) info.i = atob(info.i.replace(/=/g,'')); + content.push(``); + content.push(``); + if (info.iw) content.push(``); + if (info.ih) content.push(``); + } + if (info.c) { content.push(``); } + if (info.v) { + if (!info.v.startsWith("http")) info.v = atob(info.v.replace(/=/g,'')); + content.push(``); + if (info.vw) content.push(``); + if (info.vh) content.push(``); + } + if (info.f) { + if (info.f.length > 9){ + if (!info.f.startsWith("http")) info.f = atob(info.f.replace(/=/g,'')); + content.push(``); + } else { + let codepoints = Array.from(info.f).map(c => c.codePointAt(0).toString(16)); + content.push(``); + } } - } - content.push(``); - - console.log(["Metadata Request", JSON.stringify(info), geo, ua].join('\t')); + content.push(``); + + console.log(["Metadata Request", JSON.stringify(info), geo, ua].join('\t')); - return new Response(content.join("\n"), { - headers: { "content-type": "text/html" }, - }); - } - } else { - console.log(["Request", path, geo, request.headers.get("referer"), ua].join('\t')); + return new Response(content.join("\n"), { + headers: { "content-type": "text/html" }, + }); + } + } else { + console.log(["Request", path, geo, request.headers.get("referer"), ua].join('\t')); + } + } catch (e) { + console.log("Error:", request.url, e) } }