Skip to content

Commit df03fe2

Browse files
committed
fix: replace crypto.randomUUID with randomBrowserUUID for consistent UUID generation
1 parent 70bff85 commit df03fe2

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

adminforth/spa/src/stores/toast.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ref, watch, type Ref } from 'vue'
22
import { defineStore } from 'pinia'
33
import { useRoute } from 'vue-router';
4+
import { randomBrowserUUID } from '@/utils/browserUuid';
45

56

67

@@ -22,7 +23,7 @@ export const useToastStore = defineStore('toast', () => {
2223
buttons?: { value: any; label: string }[];
2324
onResolve?: (value?: any) => void;
2425
}): string => {
25-
const toastId = crypto.randomUUID();
26+
const toastId = randomBrowserUUID();
2627
toasts.value.push({
2728
...toast,
2829
id: toastId,
@@ -45,4 +46,4 @@ export const useToastStore = defineStore('toast', () => {
4546
};
4647

4748
return { toasts, addToast, removeToast, resolveToast };
48-
});
49+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const UUID_BYTE_LENGTH = 16;
2+
3+
const UUID_HEX = Array.from(
4+
{ length: 256 },
5+
(_, index) => index.toString(16).padStart(2, '0'),
6+
);
7+
8+
export function randomBrowserUUID(): string {
9+
if (typeof crypto.randomUUID === 'function') {
10+
return crypto.randomUUID();
11+
}
12+
13+
const bytes = crypto.getRandomValues(
14+
new Uint8Array(UUID_BYTE_LENGTH),
15+
);
16+
17+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
18+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
19+
20+
return [
21+
UUID_HEX[bytes[0]],
22+
UUID_HEX[bytes[1]],
23+
UUID_HEX[bytes[2]],
24+
UUID_HEX[bytes[3]],
25+
'-',
26+
UUID_HEX[bytes[4]],
27+
UUID_HEX[bytes[5]],
28+
'-',
29+
UUID_HEX[bytes[6]],
30+
UUID_HEX[bytes[7]],
31+
'-',
32+
UUID_HEX[bytes[8]],
33+
UUID_HEX[bytes[9]],
34+
'-',
35+
UUID_HEX[bytes[10]],
36+
UUID_HEX[bytes[11]],
37+
UUID_HEX[bytes[12]],
38+
UUID_HEX[bytes[13]],
39+
UUID_HEX[bytes[14]],
40+
UUID_HEX[bytes[15]],
41+
].join('');
42+
}

adminforth/spa/src/utils/clientId.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { randomBrowserUUID } from './browserUuid';
2+
13
const ADMINFORTH_CLIENT_ID_STORAGE_KEY = 'adminforthClientId';
24

35
export const ADMINFORTH_CLIENT_ID_HEADER = 'x-adminforth-client-id';
@@ -8,7 +10,7 @@ export function getAdminForthClientId(): string {
810
return existingClientId;
911
}
1012

11-
const clientId = crypto.randomUUID();
13+
const clientId = randomBrowserUUID();
1214
sessionStorage.setItem(ADMINFORTH_CLIENT_ID_STORAGE_KEY, clientId);
1315
return clientId;
1416
}

0 commit comments

Comments
 (0)