spb/datacenterindex
Public
HTML 53.9%
TypeScript 44.5%
JavaScript 0.6%
SQL 0.5%
1import { randomBytes, createHash } from "node:crypto";23const ALPHABET = "0123456789abcdefghijkmnpqrstuvwxyz"; // no l/o to avoid confusion45export const ID_PREFIXES = {6 facility: "fac",7 campus: "cmp",8 operator: "op",9 cloud_region: "cr",10 metro: "met",11 ixp: "ix",12 project: "prj",13 event: "evt",14 document: "doc",15 source: "src",16 news: "nws",17 run: "run",18 match: "mtc",19 provenance: "prv",20 ranking: "rnk",21 claim: "clm",22 flag: "flg",23 constraint: "grd",24 watch: "wtc",25 cluster: "cls",26} as const;27export type IdKind = keyof typeof ID_PREFIXES;2829export function newId(kind: IdKind, len = 12): string {30 const bytes = randomBytes(len);31 let out = "";32 for (let i = 0; i < len; i++) out += ALPHABET[bytes[i]! % ALPHABET.length];33 return `${ID_PREFIXES[kind]}_${out}`;34}3536/** Deterministic id from a stable key (used for documents/provenance so re-runs are idempotent). */37export function stableId(kind: IdKind, key: string, len = 16): string {38 const h = createHash("sha256").update(key).digest("hex").slice(0, len);39 return `${ID_PREFIXES[kind]}_${h}`;40}4142export function kindOfId(id: string): IdKind | null {43 const p = id.split("_")[0];44 for (const [k, v] of Object.entries(ID_PREFIXES)) if (v === p) return k as IdKind;45 return null;46}47