summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/utils/uuid.ts
blob: 7b964068c86f47a1122abbd8a43af41b7e737d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
const fallback = () => Math.random().toString(16).slice(2)

export function uuid() {
  const c = globalThis.crypto
  if (!c || typeof c.randomUUID !== "function") return fallback()
  if (typeof globalThis.isSecureContext === "boolean" && !globalThis.isSecureContext) return fallback()
  try {
    return c.randomUUID()
  } catch {
    return fallback()
  }
}