9 lines
461 B
TypeScript
9 lines
461 B
TypeScript
/** getRandomValues also works on HTTP intranet pages where randomUUID is unavailable. */
|
|||
|
|
export function newRequestId(): string {
|
||
|
|
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
|
||
|
|
bytes[6] = (bytes[6] & 15) | 64;
|
||
|
|
bytes[8] = (bytes[8] & 63) | 128;
|
||
|
|
const hex = Array.from(bytes, b => b.toString(16).padStart(2, "0")).join("");
|
||
|
|
return `${hex.slice(0,8)}-${hex.slice(8,12)}-${hex.slice(12,16)}-${hex.slice(16,20)}-${hex.slice(20)}`;
|
||
|
|
}
|