File
Blob: src/worker/common/ids.ts
| 1 | // Generate a prefixed identifier with 16 random bytes encoded as hex. |
| 2 | // Used for D1 row primary keys: `user_<32 hex>`, `ns_<32 hex>`, |
| 3 | // `repo_<32 hex>`, `pat_<32 hex>`. The hex tail is ~128 bits, enough |
| 4 | // entropy to make collisions practically impossible without depending |
| 5 | // on D1 retry semantics for unique-key conflicts. |
| 6 | export function newPrefixedId(prefix: string): string { |
| 7 | const bytes = new Uint8Array(16); |
| 8 | crypto.getRandomValues(bytes); |
| 9 | let hex = ""; |
| 10 | for (let i = 0; i < bytes.length; i += 1) { |
| 11 | hex += bytes[i]!.toString(16).padStart(2, "0"); |
| 12 | } |
| 13 | return `${prefix}_${hex}`; |
| 14 | } |