import { monotonicFactory } from "ulid"; const ULID_REGEX = /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/; // Monotonic factory: guarantees that successive calls produce ULIDs that sort // in creation order even within the same millisecond. This is important for // tiebreakers that rely on ULID ordering (e.g. reverse-chronological lists). const monotonicUlid = monotonicFactory(); /** Generate a monotonic ULID. */ export function createUlid(): string { return monotonicUlid(); } /** Validate a ULID string. */ export function isUlid(value: string): boolean { return ULID_REGEX.test(value); } /** * Assert a value is a ULID, throwing a clear error otherwise. * Used by the server to reject malformed IDs at the boundary. */ export function assertUlid(value: string): void { if (!isUlid(value)) { throw new Error(`Invalid ULID: ${value}`); } }