MCPcopy Index your code
hub / github.com/TanStack/db / safeRandomUUID

Function safeRandomUUID

packages/db/src/utils/uuid.ts:11–45  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9 * See https://github.com/TanStack/db/issues/1541.
10 */
11export function safeRandomUUID(): string {
12 const c: Crypto | undefined =
13 typeof globalThis !== `undefined` ? (globalThis as any).crypto : undefined
14
15 if (c && typeof c.randomUUID === `function`) {
16 return c.randomUUID()
17 }
18
19 if (c && typeof c.getRandomValues === `function`) {
20 const bytes = c.getRandomValues(new Uint8Array(16))
21 // Per RFC 4122 §4.4: set version (4) and variant (10xx) bits.
22 bytes[6] = (bytes[6]! & 0x0f) | 0x40
23 bytes[8] = (bytes[8]! & 0x3f) | 0x80
24
25 const hex: Array<string> = []
26 for (let i = 0; i < 16; i++) {
27 hex.push(bytes[i]!.toString(16).padStart(2, `0`))
28 }
29 return (
30 hex.slice(0, 4).join(``) +
31 `-` +
32 hex.slice(4, 6).join(``) +
33 `-` +
34 hex.slice(6, 8).join(``) +
35 `-` +
36 hex.slice(8, 10).join(``) +
37 `-` +
38 hex.slice(10, 16).join(``)
39 )
40 }
41
42 throw new Error(
43 `No secure random number generator available: neither crypto.randomUUID nor crypto.getRandomValues is defined in this environment.`,
44 )
45}

Callers 15

constructorMethod · 0.90
pullSinceMethod · 0.90
constructorMethod · 0.90
generateUuidFunction · 0.90
constructorMethod · 0.90
updateMethod · 0.90

Calls 2

toStringMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected