()
| 80 | } |
| 81 | |
| 82 | export const uuid = (): string => { |
| 83 | // best case, `crypto.randomUUID` is available |
| 84 | if (globalThis.crypto?.randomUUID) { |
| 85 | return globalThis.crypto.randomUUID() |
| 86 | } |
| 87 | |
| 88 | const bytes = new Uint8Array(16) |
| 89 | |
| 90 | if (globalThis.crypto?.getRandomValues) { |
| 91 | // `crypto.getRandomValues` is available even in non-secure contexts |
| 92 | globalThis.crypto.getRandomValues(bytes) |
| 93 | } else { |
| 94 | // fallback to Math.random, if the Crypto API is completely missing |
| 95 | for (let i = 0; i < bytes.length; i++) { |
| 96 | bytes[i] = Math.floor(Math.random() * 256) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | bytes[6] = (bytes[6] & 0x0f) | 0x40 // Set the 4 most significant bits to 0100 |
| 101 | bytes[8] = (bytes[8] & 0x3f) | 0x80 // Set the 2 most significant bits to 10 |
| 102 | |
| 103 | const hexValues: string[] = [] |
| 104 | bytes.forEach((byte) => { |
| 105 | hexValues.push(byte.toString(16).padStart(2, '0')) |
| 106 | }) |
| 107 | |
| 108 | return ( |
| 109 | hexValues.slice(0, 4).join('') + |
| 110 | '-' + |
| 111 | hexValues.slice(4, 6).join('') + |
| 112 | '-' + |
| 113 | hexValues.slice(6, 8).join('') + |
| 114 | '-' + |
| 115 | hexValues.slice(8, 10).join('') + |
| 116 | '-' + |
| 117 | hexValues.slice(10).join('') |
| 118 | ) |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Formats a query with parameters |
no test coverage detected