File tree Expand file tree Collapse file tree 2 files changed +23
-16
lines changed Expand file tree Collapse file tree 2 files changed +23
-16
lines changed Original file line number Diff line number Diff line change @@ -147,13 +147,15 @@ export interface ITask<T> {
147
147
/**
148
148
* Copied from src/vs/base/common/uuid.ts
149
149
*/
150
- export function generateUuid ( ) {
151
- // use `randomValues` if possible
152
- function getRandomValues ( bucket : Uint8Array ) : Uint8Array {
153
- for ( let i = 0 ; i < bucket . length ; i ++ ) {
154
- bucket [ i ] = Math . floor ( Math . random ( ) * 256 ) ;
155
- }
156
- return bucket ;
150
+ export function generateUuid ( ) : string {
151
+ // use `randomUUID` if possible
152
+ if ( typeof crypto . randomUUID === 'function' ) {
153
+ // see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
154
+ // > Although crypto is available on all windows, the returned Crypto object only has one
155
+ // > usable feature in insecure contexts: the getRandomValues() method.
156
+ // > In general, you should use this API only in secure contexts.
157
+
158
+ return crypto . randomUUID . bind ( crypto ) . toString ( ) ;
157
159
}
158
160
159
161
// prep-work
@@ -164,7 +166,7 @@ export function generateUuid() {
164
166
}
165
167
166
168
// get data
167
- getRandomValues ( _data ) ;
169
+ crypto . getRandomValues ( _data ) ;
168
170
169
171
// set version bits
170
172
_data [ 6 ] = ( _data [ 6 ] & 0x0f ) | 0x40 ;
Original file line number Diff line number Diff line change @@ -55,13 +55,18 @@ export class Profiler {
55
55
}
56
56
}
57
57
58
- function generateUuid ( ) {
59
- // use `randomValues` if possible
60
- function getRandomValues ( bucket : Uint8Array ) : Uint8Array {
61
- for ( let i = 0 ; i < bucket . length ; i ++ ) {
62
- bucket [ i ] = Math . floor ( Math . random ( ) * 256 ) ;
63
- }
64
- return bucket ;
58
+ /**
59
+ * Copied from src/vs/base/common/uuid.ts
60
+ */
61
+ export function generateUuid ( ) : string {
62
+ // use `randomUUID` if possible
63
+ if ( typeof crypto . randomUUID === 'function' ) {
64
+ // see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
65
+ // > Although crypto is available on all windows, the returned Crypto object only has one
66
+ // > usable feature in insecure contexts: the getRandomValues() method.
67
+ // > In general, you should use this API only in secure contexts.
68
+
69
+ return crypto . randomUUID . bind ( crypto ) . toString ( ) ;
65
70
}
66
71
67
72
// prep-work
@@ -72,7 +77,7 @@ function generateUuid() {
72
77
}
73
78
74
79
// get data
75
- getRandomValues ( _data ) ;
80
+ crypto . getRandomValues ( _data ) ;
76
81
77
82
// set version bits
78
83
_data [ 6 ] = ( _data [ 6 ] & 0x0f ) | 0x40 ;
You can’t perform that action at this time.
0 commit comments