/** * UUIDv7: 48-bit unix-ms timestamp + random. Time-ordered so ids sort roughly * by creation across devices — the property the sync design (02-ARCHITECTURE §3) * relies on for cheap cursor pagination. Client-generated, never server-assigned. * Web Crypto keeps this package loadable in both Node and the POS renderer. */ export function uuidv7(now: number = Date.now()): string { const b = new Uint8Array(16) globalThis.crypto.getRandomValues(b) b[0] = (now / 2 ** 40) & 0xff b[1] = (now / 2 ** 32) & 0xff b[2] = (now / 2 ** 24) & 0xff b[3] = (now / 2 ** 16) & 0xff b[4] = (now / 2 ** 8) & 0xff b[5] = now & 0xff b[6] = 0x70 | (b[6]! & 0x0f) b[8] = 0x80 | (b[8]! & 0x3f) const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('') return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}` } export function timestampOfUuidv7(id: string): number { const hex = id.replaceAll('-', '').slice(0, 12) return Number.parseInt(hex, 16) }