polyfill.ts 1.1 KB

123456789101112131415161718192021222324252627
  1. // @ts-nocheck
  2. if (Crypto && typeof Crypto.prototype.randomUUID !== 'function') {
  3. if (typeof Crypto.prototype.getRandomValues === 'function' && typeof Uint8Array === 'function') {
  4. Crypto.prototype.randomUUID = function () {
  5. return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) {
  6. const num = Number(c);
  7. return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16);
  8. });
  9. };
  10. } else {
  11. Crypto.prototype.randomUUID = function () {
  12. let timestamp = new Date().getTime();
  13. let per = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;
  14. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  15. let random = Math.random() * 16;
  16. if (timestamp > 0) {
  17. random = (timestamp + random) % 16 | 0;
  18. timestamp = Math.floor(timestamp / 16);
  19. } else {
  20. random = (per + random) % 16 | 0;
  21. per = Math.floor(per / 16);
  22. }
  23. return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
  24. });
  25. };
  26. }
  27. }