1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) Microsoft Corporation. All rights reserved.
3+ * Licensed under the MIT License. See License.txt in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
5+
6+ /**
7+ * Copied from vscode/src/vs/base/common/uuid.ts
8+ */
9+ export function generateUuid ( ) : string {
10+ // use `randomUUID` if possible
11+ if ( typeof crypto . randomUUID === 'function' ) {
12+ // see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
13+ // > Although crypto is available on all windows, the returned Crypto object only has one
14+ // > usable feature in insecure contexts: the getRandomValues() method.
15+ // > In general, you should use this API only in secure contexts.
16+
17+ return crypto . randomUUID . bind ( crypto ) ( ) ;
18+ }
19+
20+ // prep-work
21+ const _data = new Uint8Array ( 16 ) ;
22+ const _hex : string [ ] = [ ] ;
23+ for ( let i = 0 ; i < 256 ; i ++ ) {
24+ _hex . push ( i . toString ( 16 ) . padStart ( 2 , '0' ) ) ;
25+ }
26+
27+ // get data
28+ crypto . getRandomValues ( _data ) ;
29+
30+ // set version bits
31+ _data [ 6 ] = ( _data [ 6 ] & 0x0f ) | 0x40 ;
32+ _data [ 8 ] = ( _data [ 8 ] & 0x3f ) | 0x80 ;
33+
34+ // print as string
35+ let i = 0 ;
36+ let result = '' ;
37+ result += _hex [ _data [ i ++ ] ] ;
38+ result += _hex [ _data [ i ++ ] ] ;
39+ result += _hex [ _data [ i ++ ] ] ;
40+ result += _hex [ _data [ i ++ ] ] ;
41+ result += '-' ;
42+ result += _hex [ _data [ i ++ ] ] ;
43+ result += _hex [ _data [ i ++ ] ] ;
44+ result += '-' ;
45+ result += _hex [ _data [ i ++ ] ] ;
46+ result += _hex [ _data [ i ++ ] ] ;
47+ result += '-' ;
48+ result += _hex [ _data [ i ++ ] ] ;
49+ result += _hex [ _data [ i ++ ] ] ;
50+ result += '-' ;
51+ result += _hex [ _data [ i ++ ] ] ;
52+ result += _hex [ _data [ i ++ ] ] ;
53+ result += _hex [ _data [ i ++ ] ] ;
54+ result += _hex [ _data [ i ++ ] ] ;
55+ result += _hex [ _data [ i ++ ] ] ;
56+ result += _hex [ _data [ i ++ ] ] ;
57+ return result ;
58+ }
0 commit comments