Skip to content

Commit 50bb4fc

Browse files
authoredFeb 20, 2024··
Use common uuid generator everywhere (#13255)
1 parent f6c8fe5 commit 50bb4fc

33 files changed

+64
-142
lines changed
 

‎package.json

-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"@types/node": "18",
1818
"@types/sinon": "^10.0.6",
1919
"@types/temp": "^0.9.1",
20-
"@types/uuid": "^7.0.3",
2120
"@typescript-eslint/eslint-plugin": "^4.8.1",
2221
"@typescript-eslint/eslint-plugin-tslint": "^4.8.1",
2322
"@typescript-eslint/parser": "^4.8.1",
@@ -56,7 +55,6 @@
5655
"typedoc": "^0.22.11",
5756
"typedoc-plugin-external-module-map": "1.3.2",
5857
"typescript": "~4.5.5",
59-
"uuid": "^8.0.0",
6058
"yargs": "^15.3.1"
6159
},
6260
"scripts": {

‎packages/core/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@types/react-dom": "^18.0.6",
3131
"@types/route-parser": "^0.1.1",
3232
"@types/safer-buffer": "^2.1.0",
33+
"@types/uuid": "^9.0.8",
3334
"@types/ws": "^8.5.5",
3435
"@types/yargs": "^15",
3536
"@vscode/codicons": "*",
@@ -68,7 +69,7 @@
6869
"safer-buffer": "^2.1.2",
6970
"socket.io": "^4.5.3",
7071
"socket.io-client": "^4.5.3",
71-
"uuid": "^8.3.2",
72+
"uuid": "^9.0.1",
7273
"vscode-languageserver-protocol": "^3.17.2",
7374
"vscode-uri": "^2.1.1",
7475
"ws": "^8.14.1",

‎packages/core/src/browser/tooltip-service.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as React from 'react';
1919
import ReactTooltip from 'react-tooltip';
2020
import { ReactRenderer, RendererHost } from './widgets/react-renderer';
2121
import { CorePreferences } from './core-preferences';
22-
import { v4 } from 'uuid';
22+
import { generateUuid } from '../common/uuid';
2323

2424
export const TooltipService = Symbol('TooltipService');
2525

@@ -59,7 +59,7 @@ export class TooltipServiceImpl extends ReactRenderer implements TooltipService
5959
@inject(RendererHost) @optional() host?: RendererHost
6060
) {
6161
super(host);
62-
this.tooltipId = v4();
62+
this.tooltipId = generateUuid();
6363
}
6464

6565
@postConstruct()

‎packages/core/src/common/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ export * from './strings';
4646
export * from './telemetry';
4747
export * from './types';
4848
export { default as URI } from './uri';
49+
export * from './uuid';
4950
export * from './view-column';
5051
export * from './version';

‎packages/core/src/common/uuid.ts

+4-71
Original file line numberDiff line numberDiff line change
@@ -21,84 +21,17 @@
2121

2222
// based on https://github.com/microsoft/vscode/blob/1.72.2/src/vs/base/common/uuid.ts
2323

24-
import { v5 } from 'uuid';
24+
import { v4, v5 } from 'uuid';
2525

2626
const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
2727

2828
export function isUUID(value: string): boolean {
2929
return _UUIDPattern.test(value);
3030
}
3131

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+
}
10235

10336
const NAMESPACE = '4c90ee4f-d952-44b1-83ca-f04121ab8e05';
10437
/**

‎packages/core/src/electron-main/electron-main-application-module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// *****************************************************************************
1616

1717
import { ContainerModule } from 'inversify';
18-
import { v4 } from 'uuid';
18+
import { generateUuid } from '../common/uuid';
1919
import { bindContributionProvider } from '../common/contribution-provider';
2020
import { RpcConnectionHandler } from '../common/messaging/proxy-factory';
2121
import { ElectronSecurityToken } from '../electron-common/electron-token';
@@ -29,7 +29,7 @@ import { ElectronSecurityTokenService } from './electron-security-token-service'
2929
import { ElectronMessagingService } from './messaging/electron-messaging-service';
3030
import { ElectronConnectionHandler } from './messaging/electron-connection-handler';
3131

32-
const electronSecurityToken: ElectronSecurityToken = { value: v4() };
32+
const electronSecurityToken: ElectronSecurityToken = { value: generateUuid() };
3333
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3434
(global as any)[ElectronSecurityToken] = electronSecurityToken;
3535

‎packages/filesystem/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"@types/multer": "^1.4.7",
99
"@types/rimraf": "^2.0.2",
1010
"@types/tar-fs": "^1.16.1",
11-
"@types/uuid": "^7.0.3",
1211
"async-mutex": "^0.3.1",
1312
"body-parser": "^1.18.3",
1413
"browserfs": "^1.4.3",
@@ -19,7 +18,6 @@
1918
"stat-mode": "^1.0.0",
2019
"tar-fs": "^1.16.2",
2120
"trash": "^7.2.0",
22-
"uuid": "^8.0.0",
2321
"vscode-languageserver-textdocument": "^1.0.1"
2422
},
2523
"publishConfig": {

‎packages/filesystem/src/node/disk-file-system-provider.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { equal, fail } from 'assert';
2525
import { promises as fs } from 'fs';
2626
import { join } from 'path';
2727
import * as temp from 'temp';
28-
import { v4 } from 'uuid';
28+
import { generateUuid } from '@theia/core/lib/common/uuid';
2929
import { FilePermission, FileSystemProviderCapabilities, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files';
3030
import { DiskFileSystemProvider } from './disk-file-system-provider';
3131
import { bindFileSystemWatcherServer } from './filesystem-backend-module';
@@ -53,7 +53,7 @@ describe('disk-file-system-provider', () => {
5353
describe('stat', () => {
5454
it("should omit the 'permissions' property of the stat if the file can be both read and write", async () => {
5555
const tempDirPath = tracked.mkdirSync();
56-
const tempFilePath = join(tempDirPath, `${v4()}.txt`);
56+
const tempFilePath = join(tempDirPath, `${generateUuid()}.txt`);
5757
await fs.writeFile(tempFilePath, 'some content', { encoding: 'utf8' });
5858

5959
let content = await fs.readFile(tempFilePath, { encoding: 'utf8' });
@@ -70,7 +70,7 @@ describe('disk-file-system-provider', () => {
7070

7171
it("should set the 'permissions' property to `Readonly` if the file can be read but not write", async () => {
7272
const tempDirPath = tracked.mkdirSync();
73-
const tempFilePath = join(tempDirPath, `${v4()}.txt`);
73+
const tempFilePath = join(tempDirPath, `${generateUuid()}.txt`);
7474
await fs.writeFile(tempFilePath, 'readonly content', {
7575
encoding: 'utf8',
7676
});

‎packages/filesystem/src/node/disk-file-system-provider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
2626
import { basename, dirname, normalize, join } from 'path';
27-
import { v4 } from 'uuid';
27+
import { generateUuid } from '@theia/core/lib/common/uuid';
2828
import * as os from 'os';
2929
import * as fs from 'fs';
3030
import {
@@ -530,7 +530,7 @@ export class DiskFileSystemProvider implements Disposable,
530530

531531
protected async rimrafMove(path: string): Promise<void> {
532532
try {
533-
const pathInTemp = join(os.tmpdir(), v4());
533+
const pathInTemp = join(os.tmpdir(), generateUuid());
534534
try {
535535
await promisify(rename)(path, pathInTemp);
536536
} catch (error) {

‎packages/filesystem/src/node/download/file-download-handler.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import * as os from 'os';
1818
import * as fs from '@theia/core/shared/fs-extra';
1919
import * as path from 'path';
20-
import { v4 } from 'uuid';
20+
import { generateUuid } from '@theia/core/lib/common/uuid';
2121
import { Request, Response } from '@theia/core/shared/express';
2222
import { inject, injectable } from '@theia/core/shared/inversify';
2323
import { OK, BAD_REQUEST, METHOD_NOT_ALLOWED, NOT_FOUND, INTERNAL_SERVER_ERROR, REQUESTED_RANGE_NOT_SATISFIABLE, PARTIAL_CONTENT } from 'http-status-codes';
@@ -135,12 +135,12 @@ export abstract class FileDownloadHandler {
135135
end: (isNaN(end) || end > statSize - 1) ? (statSize - 1) : end
136136
};
137137
}
138-
protected async archive(inputPath: string, outputPath: string = path.join(os.tmpdir(), v4()), entries?: string[]): Promise<string> {
138+
protected async archive(inputPath: string, outputPath: string = path.join(os.tmpdir(), generateUuid()), entries?: string[]): Promise<string> {
139139
await this.directoryArchiver.archive(inputPath, outputPath, entries);
140140
return outputPath;
141141
}
142142

143-
protected async createTempDir(downloadId: string = v4()): Promise<string> {
143+
protected async createTempDir(downloadId: string = generateUuid()): Promise<string> {
144144
const outputPath = path.join(os.tmpdir(), downloadId);
145145
await fs.mkdir(outputPath);
146146
return outputPath;
@@ -221,7 +221,7 @@ export class SingleFileDownloadHandler extends FileDownloadHandler {
221221
return;
222222
}
223223
try {
224-
const downloadId = v4();
224+
const downloadId = generateUuid();
225225
const options: PrepareDownloadOptions = { filePath, downloadId, remove: false };
226226
if (!stat.isDirectory()) {
227227
await this.prepareDownload(request, response, options);
@@ -271,7 +271,7 @@ export class MultiFileDownloadHandler extends FileDownloadHandler {
271271
}
272272
}
273273
try {
274-
const downloadId = v4();
274+
const downloadId = generateUuid();
275275
const outputRootPath = await this.createTempDir(downloadId);
276276
const distinctUris = Array.from(new Set(body.uris.map(uri => new URI(uri))));
277277
const tarPaths = [];

‎packages/mini-browser/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"@types/mime-types": "^2.1.0",
99
"mime-types": "^2.1.18",
1010
"pdfobject": "^2.0.201604172",
11-
"uuid": "^8.0.0",
1211
"vhost": "^3.0.2"
1312
},
1413
"publishConfig": {

‎packages/mini-browser/src/browser/environment/mini-browser-environment.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Endpoint, FrontendApplicationContribution } from '@theia/core/lib/brows
1818
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
1919
import { environment } from '@theia/core/shared/@theia/application-package/lib/environment';
2020
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
21-
import { v4 } from 'uuid';
21+
import { generateUuid } from '@theia/core/lib/common/uuid';
2222
import { MiniBrowserEndpoint } from '../../common/mini-browser-endpoint';
2323

2424
/**
@@ -71,7 +71,7 @@ export class MiniBrowserEnvironment implements FrontendApplicationContribution {
7171
* Throws if `hostPatternPromise` is not yet resolved.
7272
*/
7373
getRandomEndpoint(): Endpoint {
74-
return this.getEndpoint(v4());
74+
return this.getEndpoint(generateUuid());
7575
}
7676

7777
protected async getHostPattern(): Promise<string> {

‎packages/notebook/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"@theia/editor": "1.46.0",
88
"@theia/filesystem": "1.46.0",
99
"@theia/monaco": "1.46.0",
10-
"react-perfect-scrollbar": "^1.5.8",
11-
"uuid": "^8.3.2"
10+
"react-perfect-scrollbar": "^1.5.8"
1211
},
1312
"publishConfig": {
1413
"access": "public"

‎packages/notebook/src/browser/service/notebook-execution-state-service.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Licensed under the MIT License. See License.txt in the project root for license information.
1919
*--------------------------------------------------------------------------------------------*/
2020

21-
import { Disposable, DisposableCollection, Emitter, URI } from '@theia/core';
21+
import { Disposable, DisposableCollection, Emitter, URI, generateUuid } from '@theia/core';
2222
import { inject, injectable } from '@theia/core/shared/inversify';
2323
import { NotebookService } from './notebook-service';
2424
import {
@@ -27,7 +27,6 @@ import {
2727
} from '../../common';
2828
import { CellPartialInternalMetadataEditByHandle, CellEditOperation } from '../notebook-types';
2929
import { NotebookModel } from '../view-model/notebook-model';
30-
import { v4 } from 'uuid';
3130

3231
export type CellExecuteUpdate = CellExecuteOutputEdit | CellExecuteOutputItemEdit | CellExecutionStateUpdate;
3332

@@ -178,7 +177,7 @@ export class CellExecution implements Disposable {
178177
editType: CellEditType.PartialInternalMetadata,
179178
handle: this.cellHandle,
180179
internalMetadata: {
181-
executionId: v4(),
180+
executionId: generateUuid(),
182181
runStartTime: undefined,
183182
runEndTime: undefined,
184183
lastRunSuccess: undefined,

‎packages/plugin-ext/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"mime": "^2.4.4",
4747
"ps-tree": "^1.2.0",
4848
"semver": "^7.5.4",
49-
"uuid": "^8.0.0",
5049
"vhost": "^3.0.2",
5150
"vscode-textmate": "^9.0.0"
5251
},

‎packages/plugin-ext/src/main/browser/comments/comments-main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { URI } from '@theia/core/shared/vscode-uri';
3838
import { CancellationToken } from '@theia/core/lib/common';
3939
import { RPCProtocol } from '../../../common/rpc-protocol';
4040
import { interfaces } from '@theia/core/shared/inversify';
41-
import { v4 as uuidv4 } from 'uuid';
41+
import { generateUuid } from '@theia/core/lib/common/uuid';
4242
import { CommentsContribution } from './comments-contribution';
4343

4444
/*---------------------------------------------------------------------------------------------
@@ -392,7 +392,7 @@ export class CommentsMainImp implements CommentsMain {
392392
}
393393

394394
$registerCommentController(handle: number, id: string, label: string): void {
395-
const providerId = uuidv4();
395+
const providerId = generateUuid();
396396
this.handlers.set(handle, providerId);
397397

398398
const provider = new CommentController(this.proxy, this.commentService, handle, providerId, id, label, {});

‎packages/plugin-ext/src/main/browser/custom-editors/custom-editor-opener.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import URI from '@theia/core/lib/common/uri';
1919
import { ApplicationShell, OpenHandler, Widget, WidgetManager, WidgetOpenerOptions } from '@theia/core/lib/browser';
2020
import { CustomEditor, CustomEditorPriority, CustomEditorSelector } from '../../../common';
2121
import { CustomEditorWidget } from './custom-editor-widget';
22-
import { v4 } from 'uuid';
22+
import { generateUuid } from '@theia/core/lib/common/uuid';
2323
import { Emitter } from '@theia/core';
2424
import { match } from '@theia/core/lib/common/glob';
2525

@@ -77,7 +77,7 @@ export class CustomEditorOpener implements OpenHandler {
7777
const uriString = uri.toString();
7878
let widgetPromise = this.pendingWidgetPromises.get(uriString);
7979
if (!widgetPromise) {
80-
const id = v4();
80+
const id = generateUuid();
8181
widgetPromise = this.widgetManager.getOrCreateWidget<CustomEditorWidget>(CustomEditorWidget.FACTORY_ID, { id });
8282
this.pendingWidgetPromises.set(uriString, widgetPromise);
8383
widget = await widgetPromise;

‎packages/plugin-ext/src/main/browser/notebooks/renderers/cell-output-webview.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import * as React from '@theia/core/shared/react';
2222
import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
2323
import { NotebookRendererMessagingService, CellOutputWebview, NotebookRendererRegistry, NotebookEditorWidgetService, NotebookCellOutputsSplice } from '@theia/notebook/lib/browser';
24-
import { v4 } from 'uuid';
24+
import { generateUuid } from '@theia/core/lib/common/uuid';
2525
import { NotebookCellModel } from '@theia/notebook/lib/browser/view-model/notebook-cell-model';
2626
import { WebviewWidget } from '../../webview/webview';
2727
import { Message, WidgetManager } from '@theia/core/lib/browser';
@@ -65,7 +65,7 @@ export class CellOutputWebviewImpl implements CellOutputWebview, Disposable {
6565
@inject(QuickPickService)
6666
protected readonly quickPickService: QuickPickService;
6767

68-
readonly id = v4();
68+
readonly id = generateUuid();
6969

7070
protected readonly elementRef = React.createRef<HTMLDivElement>();
7171
protected outputPresentationListeners: DisposableCollection = new DisposableCollection();

0 commit comments

Comments
 (0)
Please sign in to comment.