fix(uuid): place version nibble in byte 6 (RFC 9562) — was random, making uuidv7 ids non-compliant

This commit is contained in:
Tiel Coder
2026-11-18 18:35:53 +00:00
parent e1895079ff
commit e47ef839a7
+3 -3
View File
@@ -19,9 +19,9 @@ export function uuidv7(): string {
bytes[4] = (ms >> 8) & 0xff; bytes[4] = (ms >> 8) & 0xff;
bytes[5] = ms & 0xff; bytes[5] = ms & 0xff;
// byte 6: 8 random bits; byte 7: 4 random bits + version 0111 // byte 6: version 0111 + 4 random bits; byte 7: 4 random bits (RFC 9562 §6.6)
bytes[6] = getRandomValues(new Uint8Array(1))[0]; bytes[6] = 0x70 | (getRandomValues(new Uint8Array(1))[0] & 0x0f);
bytes[7] = 0x70 | (getRandomValues(new Uint8Array(1))[0] & 0x0f); bytes[7] = getRandomValues(new Uint8Array(1))[0];
// byte 8: variant 10xx + 6 random bits; bytes 9-15: remaining random // byte 8: variant 10xx + 6 random bits; bytes 9-15: remaining random
bytes[8] = 0x80 | (getRandomValues(new Uint8Array(1))[0] & 0x3f); bytes[8] = 0x80 | (getRandomValues(new Uint8Array(1))[0] & 0x3f);