(crypto = getCrypto())
| 34 | * @returns string Generated UUID4. |
| 35 | */ |
| 36 | export function uuid4(crypto = getCrypto()): string { |
| 37 | try { |
| 38 | if (crypto?.randomUUID) { |
| 39 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 40 | return withRandomSafeContext(() => crypto.randomUUID!()).replace(/-/g, ''); |
| 41 | } |
| 42 | } catch { |
| 43 | // some runtimes can crash invoking crypto |
| 44 | // https://github.com/getsentry/sentry-javascript/issues/8935 |
| 45 | } |
| 46 | |
| 47 | if (!emptyUuid) { |
| 48 | // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523 |
| 49 | // Concatenating the following numbers as strings results in '10000000100040008000100000000000' |
| 50 | emptyUuid = ([1e7] as unknown as string) + 1e3 + 4e3 + 8e3 + 1e11; |
| 51 | } |
| 52 | |
| 53 | return emptyUuid.replace(/[018]/g, c => |
| 54 | // eslint-disable-next-line no-bitwise |
| 55 | ((c as unknown as number) ^ ((getRandomByte() & 15) >> ((c as unknown as number) / 4))).toString(16), |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | function getFirstException(event: Event): Exception | undefined { |
| 60 | return event.exception?.values?.[0]; |
no test coverage detected