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.

wip

Kiran K84a1bf66507c
diff --git a/apps/web/lib/api/case-sensitive-short-links.ts b/apps/web/lib/api/case-sensitive-short-links.ts
new file mode 100644
index 0000000000..7d0ee8dbb0
--- /dev/null
+++ b/apps/web/lib/api/case-sensitive-short-links.ts
@@ -0,0 +1,33 @@
+import { LinkProps } from "../types";
+
+export const CASE_SENSITIVE_DOMAINS = ["buff.ly", "dub.sh"];
+
+export const encodeKey = (text: string) => {
+ return Buffer.from(text).toString("base64");
+};
+
+export const decodeKey = (encoded: string) => {
+ return Buffer.from(encoded, "base64").toString();
+};
+
+export const encodeKeyIfCaseSensitive = ({
+ domain,
+ key,
+}: Pick<LinkProps, "domain" | "key">) => {
+ const caseSensitive = CASE_SENSITIVE_DOMAINS.includes(domain);
+
+ return caseSensitive ? encodeKey(key) : key;
+};
+
+export const decodeKeyIfCaseSensitive = ({
+ domain,
+ key,
+}: Pick<LinkProps, "domain" | "key">) => {
+ const caseSensitive = CASE_SENSITIVE_DOMAINS.includes(domain);
+
+ return caseSensitive ? decodeKey(key) : key;
+};
+
+export const isCaseSensitiveDomain = (domain: string) => {
+ return CASE_SENSITIVE_DOMAINS.includes(domain);
+};
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 88f2a2a2eb..bf5c2da1eb 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -1,6 +1,9 @@
import { LinkProps, RedisLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
-import { CASE_SENSITIVE_DOMAINS, decodeKey } from "./constants";
+import {
+ decodeKeyIfCaseSensitive,
+ isCaseSensitiveDomain,
+} from "../case-sensitive-short-links";
import { ExpandedLink } from "./utils/transform-link";
/*
@@ -44,25 +47,20 @@ class LinkCache {
const redisLink = formatRedisLink(link);
const hasWebhooks = redisLink.webhookIds && redisLink.webhookIds.length > 0;
- const caseSensitive = CASE_SENSITIVE_DOMAINS.includes(link.domain);
-
- if (caseSensitive) {
- link.key = decodeKey(link.key);
- }
+ link.key = decodeKeyIfCaseSensitive({
+ domain: link.domain,
+ key: link.key,
+ });
return await redis.set(
- this._createKey({ domain: link.domain, key: link.key, caseSensitive }),
+ this._createKey({ domain: link.domain, key: link.key }),
redisLink,
hasWebhooks ? undefined : { ex: CACHE_EXPIRATION },
);
}
async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
- const caseSensitive = CASE_SENSITIVE_DOMAINS.includes(domain);
-
- return await redis.get<RedisLinkProps>(
- this._createKey({ domain, key, caseSensitive }),
- );
+ return await redis.get<RedisLinkProps>(this._createKey({ domain, key }));
}
async delete({ domain, key }: Pick<LinkProps, "domain" | "key">) {
@@ -121,14 +119,10 @@ class LinkCache {
return await pipeline.exec();
}
- _createKey({
- domain,
- key,
- caseSensitive = false,
- }: Pick<LinkProps, "domain" | "key"> & { caseSensitive?: boolean }) {
+ _createKey({ domain, key }: Pick<LinkProps, "domain" | "key">) {
const cacheKey = `linkcache:${domain}:${key}`;
- return caseSensitive ? cacheKey : cacheKey.toLowerCase();
+ return isCaseSensitiveDomain(domain) ? cacheKey : cacheKey.toLowerCase();
}
}
diff --git a/apps/web/lib/api/links/constants.ts b/apps/web/lib/api/links/constants.ts
deleted file mode 100644
index 85c694b814..0000000000
--- a/apps/web/lib/api/links/constants.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-// List of domains that support case sensitive short links
-export const CASE_SENSITIVE_DOMAINS = ["buff.ly"];
-
-// TODO:
-// Need a better hash function (+ move to a new file)
-
-const SECRET_KEY = "your-secret-key-here";
-
-// Simple XOR cipher with base64 encoding
-export const hashKey = (text: string): string => {
- const xored = text
- .split("")
- .map((char, i) =>
- String.fromCharCode(
- char.charCodeAt(0) ^ SECRET_KEY.charCodeAt(i % SECRET_KEY.length),
- ),
- )
- .join("");
-
- return Buffer.from(xored).toString("base64");
-};
-
-export const decodeKey = (hash: string): string => {
- const xored = Buffer.from(hash, "base64").toString();
-
- return xored
- .split("")
- .map((char, i) =>
- String.fromCharCode(
- char.charCodeAt(0) ^ SECRET_KEY.charCodeAt(i % SECRET_KEY.length),
- ),
- )
- .join("");
-};