فهرست منبع

feat: use crypto.randomUUID

代强 3 سال پیش
والد
کامیت
f1a3b69067
1فایلهای تغییر یافته به همراه3 افزوده شده و 1 حذف شده
  1. 3 1
      packages/semi-foundation/utils/uuid.ts

+ 3 - 1
packages/semi-foundation/utils/uuid.ts

@@ -5,7 +5,9 @@ export default function getUuid(prefix: string) {
 // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
 function getUuidv4() {
     try {
-        return crypto.randomUUID();
+        return crypto?.randomUUID?.() ?? (String(1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
+            (Number(c) ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (Number(c) / 4)))).toString(16)
+        ));
     } catch (err) {
         return getUuid('semi');
     }