Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 54b12e5

Browse files
authoredMar 3, 2025
Copy UUID impl (microsoft#231347)
1 parent 12f73d5 commit 54b12e5

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed
 

‎extensions/ipynb/src/helper.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ export interface ITask<T> {
147147
/**
148148
* Copied from src/vs/base/common/uuid.ts
149149
*/
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();
157159
}
158160

159161
// prep-work
@@ -164,7 +166,7 @@ export function generateUuid() {
164166
}
165167

166168
// get data
167-
getRandomValues(_data);
169+
crypto.getRandomValues(_data);
168170

169171
// set version bits
170172
_data[6] = (_data[6] & 0x0f) | 0x40;

‎test/automation/src/profiler.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,18 @@ export class Profiler {
5555
}
5656
}
5757

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();
6570
}
6671

6772
// prep-work
@@ -72,7 +77,7 @@ function generateUuid() {
7277
}
7378

7479
// get data
75-
getRandomValues(_data);
80+
crypto.getRandomValues(_data);
7681

7782
// set version bits
7883
_data[6] = (_data[6] & 0x0f) | 0x40;

0 commit comments

Comments
 (0)
Please sign in to comment.