The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
make caseSensitive dependency free
Steven Tey9fca4bf03088
diff --git a/apps/web/lib/api/case-sensitive-short-links.ts b/apps/web/lib/api/case-sensitive-short-links.tsindex 7c795ef76b..32f1201c28 100644--- a/apps/web/lib/api/case-sensitive-short-links.ts+++ b/apps/web/lib/api/case-sensitive-short-links.ts@@ -1,11 +1,11 @@-import { LinkProps } from "../types";--export const CASE_SENSITIVE_DOMAINS = ["buff.ly", "dub-internal-test.com"];-// This is not actually a secret key, it's just a string that we XOR with the key to make it case sensitiveconst SECRET_KEY = "58ff90c0dc372ded858cbf8fb2306066";+export const CASE_SENSITIVE_DOMAINS = ["buff.ly", "dub-internal-test.com"];+export const encodeKey = (text: string): string => {+ if (!text) return "";+const xored = text.split("").map((char, i) =>@@ -19,6 +19,8 @@ export const encodeKey = (text: string): string => {};export const decodeKey = (hash: string): string => {+ if (!hash) return "";+const xored = Buffer.from(hash, "base64").toString();return xored@@ -40,7 +42,10 @@ export const isCaseSensitiveDomain = (domain: string) => {export const encodeKeyIfCaseSensitive = ({domain,key,-}: Pick<LinkProps, "domain" | "key">) => {+}: {+ domain: string;+ key: string;+}) => {return isCaseSensitiveDomain(domain) ? encodeKey(key) : key;};@@ -48,7 +53,10 @@ export const encodeKeyIfCaseSensitive = ({export const decodeKeyIfCaseSensitive = ({domain,key,-}: Pick<LinkProps, "domain" | "key">) => {+}: {+ domain: string;+ key: string;+}) => {return isCaseSensitiveDomain(domain) ? decodeKey(key) : key;};@@ -58,7 +66,7 @@ export const decodeLinkIfCaseSensitive = (link: any) => {const originalKey = decodeKey(link.key);if (link.shortLink) {- link.shortLink = link.shortLink.replace(link.key, originalKey);+ link.shortLink = `https://${link.domain}${originalKey === "_root" ? "" : `/${originalKey}`}`;}link.key = originalKey;diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex 82fd65df7f..62ae0d26cf 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -83,6 +83,7 @@ export default async function LinkMiddleware(});if (!linkData) {+ // special case for buff.lyif (domain === "buff.ly") {return NextResponse.rewrite(new URL(@@ -118,7 +119,7 @@ export default async function LinkMiddleware(// format link to fit the RedisLinkProps interfacecachedLink = formatRedisLink(linkData as any);-+ // cache in Redisev.waitUntil(linkCache.set(linkData as any));}@@ -166,7 +167,7 @@ export default async function LinkMiddleware(// - no `pw` param is provided// - the `pw` param is incorrect// this will also ensure that no clicks are tracked unless the password is correct- if (!pw || (await getLinkViaEdge({ domain, key })).password !== pw) {+ if (!pw || (await getLinkViaEdge({ domain, key }))?.password !== pw) {return NextResponse.rewrite(new URL(`/password/${linkId}`, req.url), {headers: {...DUB_HEADERS,diff --git a/apps/web/lib/planetscale/get-link-via-edge.ts b/apps/web/lib/planetscale/get-link-via-edge.tsindex c4750b8798..3adda3058a 100644--- a/apps/web/lib/planetscale/get-link-via-edge.ts+++ b/apps/web/lib/planetscale/get-link-via-edge.ts@@ -1,6 +1,6 @@import { punyEncode } from "@dub/utils";import {- decodeLinkIfCaseSensitive,+ decodeKeyIfCaseSensitive,encodeKey,isCaseSensitiveDomain,} from "../api/case-sensitive-short-links";@@ -14,7 +14,8 @@ export const getLinkViaEdge = async ({domain: string;key: string;}) => {- const keyToQuery = isCaseSensitiveDomain(domain)+ const isCaseSensitive = isCaseSensitiveDomain(domain);+ const keyToQuery = isCaseSensitive? // for case sensitive domains, we need to encode the keyencodeKey(key): // for non-case sensitive domains, we need to make sure that the key is always URI-decoded + punycode-encoded@@ -32,5 +33,7 @@ export const getLinkViaEdge = async ({? (rows[0] as EdgeLinkProps): null;- return link ? decodeLinkIfCaseSensitive(link) : null;+ return link+ ? { ...link, key: decodeKeyIfCaseSensitive({ domain, key }) }+ : null;};