|
21 | 21 |
|
22 | 22 | // based on https://github.com/microsoft/vscode/blob/1.72.2/src/vs/base/common/uuid.ts
|
23 | 23 |
|
24 |
| -import { v5 } from 'uuid'; |
| 24 | +import { v4, v5 } from 'uuid'; |
25 | 25 |
|
26 | 26 | const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
27 | 27 |
|
28 | 28 | export function isUUID(value: string): boolean {
|
29 | 29 | return _UUIDPattern.test(value);
|
30 | 30 | }
|
31 | 31 |
|
32 |
| -declare const crypto: undefined | { |
33 |
| - // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#browser_compatibility |
34 |
| - getRandomValues?(data: Uint8Array): Uint8Array; |
35 |
| - // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID#browser_compatibility |
36 |
| - randomUUID?(): string; |
37 |
| -}; |
38 |
| - |
39 |
| -export const generateUuid = (function (): () => string { |
40 |
| - |
41 |
| - // use `randomUUID` if possible |
42 |
| - if (typeof crypto === 'object' && typeof crypto.randomUUID === 'function') { |
43 |
| - return crypto.randomUUID.bind(crypto); |
44 |
| - } |
45 |
| - |
46 |
| - // use `randomValues` if possible |
47 |
| - let getRandomValues: (bucket: Uint8Array) => Uint8Array; |
48 |
| - if (typeof crypto === 'object' && typeof crypto.getRandomValues === 'function') { |
49 |
| - getRandomValues = crypto.getRandomValues.bind(crypto); |
50 |
| - |
51 |
| - } else { |
52 |
| - getRandomValues = function (bucket: Uint8Array): Uint8Array { |
53 |
| - for (let i = 0; i < bucket.length; i++) { |
54 |
| - bucket[i] = Math.floor(Math.random() * 256); |
55 |
| - } |
56 |
| - return bucket; |
57 |
| - }; |
58 |
| - } |
59 |
| - |
60 |
| - // prep-work |
61 |
| - const _data = new Uint8Array(16); |
62 |
| - const _hex: string[] = []; |
63 |
| - for (let i = 0; i < 256; i++) { |
64 |
| - _hex.push(i.toString(16).padStart(2, '0')); |
65 |
| - } |
66 |
| - |
67 |
| - // eslint-disable-next-line @typescript-eslint/no-shadow |
68 |
| - return function generateUuid(): string { |
69 |
| - // get data |
70 |
| - getRandomValues(_data); |
71 |
| - |
72 |
| - // set version bits |
73 |
| - _data[6] = (_data[6] & 0x0f) | 0x40; |
74 |
| - _data[8] = (_data[8] & 0x3f) | 0x80; |
75 |
| - |
76 |
| - // print as string |
77 |
| - let i = 0; |
78 |
| - let result = ''; |
79 |
| - result += _hex[_data[i++]]; |
80 |
| - result += _hex[_data[i++]]; |
81 |
| - result += _hex[_data[i++]]; |
82 |
| - result += _hex[_data[i++]]; |
83 |
| - result += '-'; |
84 |
| - result += _hex[_data[i++]]; |
85 |
| - result += _hex[_data[i++]]; |
86 |
| - result += '-'; |
87 |
| - result += _hex[_data[i++]]; |
88 |
| - result += _hex[_data[i++]]; |
89 |
| - result += '-'; |
90 |
| - result += _hex[_data[i++]]; |
91 |
| - result += _hex[_data[i++]]; |
92 |
| - result += '-'; |
93 |
| - result += _hex[_data[i++]]; |
94 |
| - result += _hex[_data[i++]]; |
95 |
| - result += _hex[_data[i++]]; |
96 |
| - result += _hex[_data[i++]]; |
97 |
| - result += _hex[_data[i++]]; |
98 |
| - result += _hex[_data[i++]]; |
99 |
| - return result; |
100 |
| - }; |
101 |
| -})(); |
| 32 | +export function generateUuid(): string { |
| 33 | + return v4(); |
| 34 | +} |
102 | 35 |
|
103 | 36 | const NAMESPACE = '4c90ee4f-d952-44b1-83ca-f04121ab8e05';
|
104 | 37 | /**
|
|
0 commit comments