uuid.ts 359 B

123456789101112
  1. const fallback = () => Math.random().toString(16).slice(2)
  2. export function uuid() {
  3. const c = globalThis.crypto
  4. if (!c || typeof c.randomUUID !== "function") return fallback()
  5. if (typeof globalThis.isSecureContext === "boolean" && !globalThis.isSecureContext) return fallback()
  6. try {
  7. return c.randomUUID()
  8. } catch {
  9. return fallback()
  10. }
  11. }