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.

switch to ULID instead

Steven Tey8910cf2a837b
diff --git a/apps/web/lib/api/create-id.ts b/apps/web/lib/api/create-id.ts
index 0797acd940..5c84182fe8 100644
--- a/apps/web/lib/api/create-id.ts
+++ b/apps/web/lib/api/create-id.ts
@@ -24,33 +24,36 @@ const prefixes = [
"fold_",
] as const;
-const base58 = baseX(
- "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
-);
+// ULID uses base32 encoding
+const base32 = baseX("0123456789ABCDEFGHJKMNPQRSTVWXYZ");
+
+// Creates a ULID-compatible buffer (48 bits timestamp + 80 bits randomness)
+function createULIDBuffer(): Uint8Array {
+ const buf = new Uint8Array(16); // 128 bits total
+
+ // Timestamp (48 bits = 6 bytes)
+ const timestamp = BigInt(Date.now());
+ buf[0] = Number((timestamp >> BigInt(40)) & BigInt(255));
+ buf[1] = Number((timestamp >> BigInt(32)) & BigInt(255));
+ buf[2] = Number((timestamp >> BigInt(24)) & BigInt(255));
+ buf[3] = Number((timestamp >> BigInt(16)) & BigInt(255));
+ buf[4] = Number((timestamp >> BigInt(8)) & BigInt(255));
+ buf[5] = Number(timestamp & BigInt(255));
+
+ // Randomness (80 bits = 10 bytes)
+ crypto.getRandomValues(buf.subarray(6));
+
+ return buf;
+}
// Creates a unique, time-sortable ID with an optional prefix
export const createId = ({
prefix,
- length = 24,
}: {
prefix?: (typeof prefixes)[number];
- length?: number;
}) => {
- // Start epoch from 2024-01-01 to save space in the ID
- const EPOCH_TIMESTAMP = 1704067200000;
- const t = Date.now() - EPOCH_TIMESTAMP;
-
- // A buffer with 4 bytes for timestamp and remaining for randomness
- const buf = new Uint8Array(Math.max(12, Math.ceil(length * 0.75)));
-
- buf[0] = (t >>> 24) & 255;
- buf[1] = (t >>> 16) & 255;
- buf[2] = (t >>> 8) & 255;
- buf[3] = t & 255;
-
- crypto.getRandomValues(buf.subarray(4));
-
- const id = base58.encode(buf);
+ const buf = createULIDBuffer();
+ const id = base32.encode(buf);
return `${prefix || ""}${id}`;
};
diff --git a/apps/web/lib/embed/constants.ts b/apps/web/lib/embed/constants.ts
index 9d2afc93db..5bb8f4c89e 100644
--- a/apps/web/lib/embed/constants.ts
+++ b/apps/web/lib/embed/constants.ts
@@ -1,6 +1,5 @@
// Embed public token
export const EMBED_PUBLIC_TOKEN_PREFIX = "dub_embed_";
-export const EMBED_PUBLIC_TOKEN_LENGTH = 36;
export const EMBED_PUBLIC_TOKEN_EXPIRY = 24 * 60 * 60; // 24 hours
// Referrals embed token
diff --git a/apps/web/lib/embed/referrals/token-class.ts b/apps/web/lib/embed/referrals/token-class.ts
index 9944e6b05a..f9af53060f 100644
--- a/apps/web/lib/embed/referrals/token-class.ts
+++ b/apps/web/lib/embed/referrals/token-class.ts
@@ -3,7 +3,6 @@ import { redis } from "@/lib/upstash";
import { createId } from "@/lib/api/create-id";
import {
EMBED_PUBLIC_TOKEN_EXPIRY,
- EMBED_PUBLIC_TOKEN_LENGTH,
EMBED_PUBLIC_TOKEN_PREFIX,
} from "../constants";
@@ -16,7 +15,6 @@ class ReferralsEmbedToken {
async create(props: ReferralsEmbedTokenProps) {
const publicToken = createId({
prefix: EMBED_PUBLIC_TOKEN_PREFIX,
- length: EMBED_PUBLIC_TOKEN_LENGTH,
});
await redis.set(publicToken, JSON.stringify(props), {