import { randomBytes, createHash } from "node:crypto"; const ALPHABET = "0123456789abcdefghijkmnpqrstuvwxyz"; // no l/o to avoid confusion export const ID_PREFIXES = { facility: "fac", campus: "cmp", operator: "op", cloud_region: "cr", metro: "met", ixp: "ix", project: "prj", event: "evt", document: "doc", source: "src", news: "nws", run: "run", match: "mtc", provenance: "prv", ranking: "rnk", claim: "clm", flag: "flg", constraint: "grd", watch: "wtc", cluster: "cls", } as const; export type IdKind = keyof typeof ID_PREFIXES; export function newId(kind: IdKind, len = 12): string { const bytes = randomBytes(len); let out = ""; for (let i = 0; i < len; i++) out += ALPHABET[bytes[i]! % ALPHABET.length]; return `${ID_PREFIXES[kind]}_${out}`; } /** Deterministic id from a stable key (used for documents/provenance so re-runs are idempotent). */ export function stableId(kind: IdKind, key: string, len = 16): string { const h = createHash("sha256").update(key).digest("hex").slice(0, len); return `${ID_PREFIXES[kind]}_${h}`; } export function kindOfId(id: string): IdKind | null { const p = id.split("_")[0]; for (const [k, v] of Object.entries(ID_PREFIXES)) if (v === p) return k as IdKind; return null; }