The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add create-id
Kiran K82f16b3b69e1
diff --git a/apps/web/lib/api/create-id.ts b/apps/web/lib/api/create-id.tsnew file mode 100644index 0000000000..0797acd940--- /dev/null+++ b/apps/web/lib/api/create-id.ts@@ -0,0 +1,56 @@+import baseX from "base-x";+import crypto from "crypto";++const prefixes = [+ "user_",+ "link_",+ "tag_",+ "dom_",+ "po_",+ "dash_",+ "int_",+ "app_",+ "cus_",+ "utm_",+ "wh_",+ "pgi_",+ "pge_",+ "pn_",+ "cm_",+ "pga_",+ "dub_embed_",+ "inv_",+ "rw_",+ "fold_",+] as const;++const base58 = baseX(+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",+);++// 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);++ return `${prefix || ""}${id}`;+};diff --git a/apps/web/package.json b/apps/web/package.jsonindex 20ce50e6d3..61e7af9a1a 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -42,6 +42,7 @@"@stripe/stripe-js": "^1.44.1","@tanstack/react-table": "^8.17.3","@team-plain/typescript-sdk": "^4.5.0",+ "@types/base-x": "^3.0.10","@types/bcryptjs": "^2.4.6","@upstash/qstash": "^2.3.0","@upstash/ratelimit": "^2.0.5",@@ -60,6 +61,7 @@"@visx/text": "^3.3.0","ai": "^4.0.30","aws4fetch": "^1.0.19",+ "base-x": "^5.0.0","bcryptjs": "^2.4.3","bottleneck": "^2.19.5","cmdk": "^0.2.0",diff --git a/apps/web/tests/misc/create-id.test.ts b/apps/web/tests/misc/create-id.test.tsnew file mode 100644index 0000000000..dc4d80ed7a--- /dev/null+++ b/apps/web/tests/misc/create-id.test.ts@@ -0,0 +1,38 @@+import { createId } from "@/lib/api/create-id";+import { describe, expect, it } from "vitest";++describe("createId", () => {+ it("should create ids in lexicographical order", () => {+ const ids: string[] = [];++ for (let i = 0; i < 10; i++) {+ ids.push(createId({}));++ const now = Date.now();+ while (Date.now() - now < 10) {} // busy wait for 10ms+ }++ // Create a copy and sort it lexicographically+ const sortedIds = [...ids].sort();+ expect(ids).toEqual(sortedIds);+ });++ it("should maintain order with prefixes", () => {+ const ids: string[] = [];++ for (let i = 0; i < 5; i++) {+ ids.push(createId({ prefix: "link_" }));++ const now = Date.now();+ while (Date.now() - now < 10) {} // busy wait for 10ms+ }++ const sortedIds = [...ids].sort();+ expect(ids).toEqual(sortedIds);++ console.log("Generated IDs in order:", {+ ids,+ sortedIds,+ });+ });+});