Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

prettier

Steven Tey6ee8732d8c7c
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
index fd1eef2df4..6b14028c60 100644
--- a/pages/api/edge/metatags.ts
+++ b/pages/api/edge/metatags.ts
@@ -1,135 +1,135 @@
import { NextFetchEvent, NextRequest } from "next/server";
import { parse } from "node-html-parser";
-import { getDomainWithoutWWW, isValidUrl } from "@/lib/utils";
+import { isValidUrl } from "@/lib/utils";
import { ratelimit, recordMetatags } from "@/lib/upstash";
import { getToken } from "next-auth/jwt";
export const config = {
- runtime: "experimental-edge",
+ runtime: "experimental-edge",
};
export default async function handler(req: NextRequest, ev: NextFetchEvent) {
- if (req.method === "GET") {
- let url = req.nextUrl.searchParams.get("url");
- if (!isValidUrl(url)) {
- return new Response("Invalid URL", { status: 400 });
- }
-
- // Rate limit if user is not logged in
- const session = await getToken({
- req,
- secret: process.env.NEXTAUTH_SECRET,
- });
- if (!session?.email) {
- const { success } = await ratelimit.limit("metatags");
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
- }
-
- const metatags = await getMetaTags(url, ev);
- return new Response(JSON.stringify(metatags), {
- status: 200,
- headers: {
- "Content-Type": "application/json",
- },
- });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
+ if (req.method === "GET") {
+ let url = req.nextUrl.searchParams.get("url");
+ if (!isValidUrl(url)) {
+ return new Response("Invalid URL", { status: 400 });
+ }
+
+ // Rate limit if user is not logged in
+ const session = await getToken({
+ req,
+ secret: process.env.NEXTAUTH_SECRET,
+ });
+ if (!session?.email) {
+ const { success } = await ratelimit.limit("metatags");
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+ }
+
+ const metatags = await getMetaTags(url, ev);
+ return new Response(JSON.stringify(metatags), {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+ } else {
+ return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
+ }
}
const getHtml = async (url: string) => {
- const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 5000); // timeout if it takes longer than 5 seconds
- return await fetch(url, {
- signal: controller.signal,
- headers: {
- "User-Agent": "dub-bot/1.0",
- },
- }).then((res) => {
- clearTimeout(timeoutId);
- return res.text();
- });
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 5000); // timeout if it takes longer than 5 seconds
+ return await fetch(url, {
+ signal: controller.signal,
+ headers: {
+ "User-Agent": "dub-bot/1.0",
+ },
+ }).then((res) => {
+ clearTimeout(timeoutId);
+ return res.text();
+ });
};
const getAst = (html: string) => {
- const ast = parse(html);
- return ast;
+ const ast = parse(html);
+ return ast;
};
const getHeadChildNodes = (ast: ReturnType<typeof getAst>) => {
- const metaTags = ast.querySelectorAll("meta").map(({ attributes }) => {
- const property = attributes.property || attributes.name || attributes.href;
- return {
- property,
- content: attributes.content,
- };
- });
- const title = ast.querySelector("title").innerText;
- const linkTags = ast.querySelectorAll("link").map(({ attributes }) => {
- const { rel, href } = attributes;
- return {
- rel,
- href,
- };
- });
-
- return { metaTags, title, linkTags };
+ const metaTags = ast.querySelectorAll("meta").map(({ attributes }) => {
+ const property = attributes.property || attributes.name || attributes.href;
+ return {
+ property,
+ content: attributes.content,
+ };
+ });
+ const title = ast.querySelector("title").innerText;
+ const linkTags = ast.querySelectorAll("link").map(({ attributes }) => {
+ const { rel, href } = attributes;
+ return {
+ rel,
+ href,
+ };
+ });
+
+ return { metaTags, title, linkTags };
};
const getRelativeUrl = (url: string, imageUrl: string) => {
- if (!imageUrl) {
- return null;
- }
- if (isValidUrl(imageUrl)) {
- return imageUrl;
- }
- const { protocol, host } = new URL(url);
- const baseURL = `${protocol}//${host}`;
- return new URL(imageUrl, baseURL).toString();
+ if (!imageUrl) {
+ return null;
+ }
+ if (isValidUrl(imageUrl)) {
+ return imageUrl;
+ }
+ const { protocol, host } = new URL(url);
+ const baseURL = `${protocol}//${host}`;
+ return new URL(imageUrl, baseURL).toString();
};
export const getMetaTags = async (url: string, ev: NextFetchEvent) => {
- const html = await getHtml(url);
- const headAst = getAst(html);
- const { metaTags, title: pageTitle, linkTags } = getHeadChildNodes(headAst);
+ const html = await getHtml(url);
+ const headAst = getAst(html);
+ const { metaTags, title: pageTitle, linkTags } = getHeadChildNodes(headAst);
- let object = {};
+ let object = {};
- for (let k in metaTags) {
- let { property, content } = metaTags[k];
+ for (let k in metaTags) {
+ let { property, content } = metaTags[k];
- property && (object[property] = content);
- }
+ property && (object[property] = content);
+ }
- for (let m in linkTags) {
- let { rel, href } = linkTags[m];
+ for (let m in linkTags) {
+ let { rel, href } = linkTags[m];
- rel && (object[rel] = href);
- }
+ rel && (object[rel] = href);
+ }
- const title = object["og:title"] || object["twitter:title"] || pageTitle;
+ const title = object["og:title"] || object["twitter:title"] || pageTitle;
- const description =
- object["description"] ||
- object["og:description"] ||
- object["twitter:description"];
+ const description =
+ object["description"] ||
+ object["og:description"] ||
+ object["twitter:description"];
- const image =
- object["og:image"] ||
- object["twitter:image"] ||
- object["image_src"] ||
- object["icon"] ||
- object["shortcut icon"];
+ const image =
+ object["og:image"] ||
+ object["twitter:image"] ||
+ object["image_src"] ||
+ object["icon"] ||
+ object["shortcut icon"];
- ev.waitUntil(
- recordMetatags(url, title && description && image ? false : true),
- );
+ ev.waitUntil(
+ recordMetatags(url, title && description && image ? false : true),
+ );
- return {
- title,
- description,
- image: getRelativeUrl(url, image),
- };
+ return {
+ title,
+ description,
+ image: getRelativeUrl(url, image),
+ };
};