From 03eacbe58030b56149284730a0af0367a01383cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 15 Oct 2022 15:57:10 +0200 Subject: [PATCH] Use node crypto built-in uuid v4 generator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package-lock.json | 27 ------------------- package.json | 2 -- .../WorkerBroadcastChannel.ts | 4 +-- .../ui-server/UIWebSocketServer.ts | 3 +-- src/ui/web/package-lock.json | 27 ------------------- src/ui/web/package.json | 2 -- src/ui/web/src/composables/UIClient.ts | 3 +-- src/utils/Utils.ts | 8 +++--- 8 files changed, 8 insertions(+), 68 deletions(-) diff --git a/package-lock.json b/package-lock.json index 523423ee..e61d1e69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,6 @@ "source-map-support": "^0.5.21", "tar": "^6.1.11", "tslib": "^2.4.0", - "uuid": "^9.0.0", "winston": "^3.8.2", "winston-daily-rotate-file": "^4.7.1", "ws": "^8.9.0" @@ -41,7 +40,6 @@ "@types/node": "^18.11.0", "@types/proper-lockfile": "^4.1.2", "@types/tar": "^6.1.3", - "@types/uuid": "^8.3.4", "@types/ws": "^8.5.3", "@typescript-eslint/eslint-plugin": "^5.40.0", "@typescript-eslint/parser": "^5.40.0", @@ -1938,12 +1936,6 @@ "integrity": "sha512-N1rW+njavs70y2cApeIw1vLMYXRwfBy+7trgavGuuTfOd7j1Yh7QTRc/yqsPl6ncokt72ZXuxEU0PiCp9bSwNQ==", "dev": true }, - "node_modules/@types/uuid": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz", - "integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==", - "dev": true - }, "node_modules/@types/webgl-ext": { "version": "0.0.30", "resolved": "https://registry.npmjs.org/@types/webgl-ext/-/webgl-ext-0.0.30.tgz", @@ -16580,14 +16572,6 @@ "integrity": "sha512-mLs5zAK+ctllYBj+iAQvlDCwoxU/WDOUaJkcFudeiAX6OajC6BKXJUa9a+tbtkC11dz2Ufb7h0lyvIOVn4LADA==", "dev": true }, - "node_modules/uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/uuid-parse": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/uuid-parse/-/uuid-parse-1.1.0.tgz", @@ -18753,12 +18737,6 @@ "integrity": "sha512-N1rW+njavs70y2cApeIw1vLMYXRwfBy+7trgavGuuTfOd7j1Yh7QTRc/yqsPl6ncokt72ZXuxEU0PiCp9bSwNQ==", "dev": true }, - "@types/uuid": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz", - "integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==", - "dev": true - }, "@types/webgl-ext": { "version": "0.0.30", "resolved": "https://registry.npmjs.org/@types/webgl-ext/-/webgl-ext-0.0.30.tgz", @@ -30089,11 +30067,6 @@ "integrity": "sha512-mLs5zAK+ctllYBj+iAQvlDCwoxU/WDOUaJkcFudeiAX6OajC6BKXJUa9a+tbtkC11dz2Ufb7h0lyvIOVn4LADA==", "dev": true }, - "uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==" - }, "uuid-parse": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/uuid-parse/-/uuid-parse-1.1.0.tgz", diff --git a/package.json b/package.json index 19dd533e..364403de 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ "source-map-support": "^0.5.21", "tar": "^6.1.11", "tslib": "^2.4.0", - "uuid": "^9.0.0", "winston": "^3.8.2", "winston-daily-rotate-file": "^4.7.1", "ws": "^8.9.0" @@ -110,7 +109,6 @@ "@types/node": "^18.11.0", "@types/proper-lockfile": "^4.1.2", "@types/tar": "^6.1.3", - "@types/uuid": "^8.3.4", "@types/ws": "^8.5.3", "@typescript-eslint/eslint-plugin": "^5.40.0", "@typescript-eslint/parser": "^5.40.0", diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/WorkerBroadcastChannel.ts index c1df1afd..652a27e1 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/WorkerBroadcastChannel.ts @@ -1,7 +1,5 @@ import { BroadcastChannel } from 'worker_threads'; -import * as uuid from 'uuid'; - import type { JsonType } from '../types/JsonType'; import type { BroadcastChannelRequest, @@ -42,7 +40,7 @@ export default abstract class WorkerBroadcastChannel extends BroadcastChannel { ); return false; } - if (uuid.validate(messageEvent.data[0]) === false) { + if (Utils.validateUUID(messageEvent.data[0]) === false) { logger.error( this.logPrefix(moduleName, 'validateMessageEvent') + ' Worker broadcast channel protocol message event data UUID field is invalid' diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 1b060783..60a4e3c1 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -2,7 +2,6 @@ import type { IncomingMessage } from 'http'; import type internal from 'stream'; import { StatusCodes } from 'http-status-codes'; -import * as uuid from 'uuid'; import WebSocket, { type RawData, WebSocketServer } from 'ws'; import type { UIServerConfiguration } from '../../types/ConfigurationData'; @@ -175,7 +174,7 @@ export default class UIWebSocketServer extends AbstractUIServer { return false; } - if (uuid.validate(request[0]) === false) { + if (Utils.validateUUID(request[0]) === false) { logger.error( `${this.logPrefix( moduleName, diff --git a/src/ui/web/package-lock.json b/src/ui/web/package-lock.json index 188410e1..51b5e527 100644 --- a/src/ui/web/package-lock.json +++ b/src/ui/web/package-lock.json @@ -11,7 +11,6 @@ "core-js": "^3.25.5", "finalhandler": "^1.2.0", "serve-static": "^1.15.0", - "uuid": "^9.0.0", "vue": "^3.2.41", "vue-class-component": "^8.0.0-rc.1", "vue-router": "^4.1.5" @@ -20,7 +19,6 @@ "@types/chai": "^4.3.3", "@types/mocha": "^10.0.0", "@types/node": "^18.11.0", - "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^5.40.0", "@typescript-eslint/parser": "^5.40.0", "@vue/cli": "~5.0.8", @@ -2938,12 +2936,6 @@ "@types/node": "*" } }, - "node_modules/@types/uuid": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz", - "integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==", - "dev": true - }, "node_modules/@types/webpack-env": { "version": "1.17.0", "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.17.0.tgz", @@ -17898,14 +17890,6 @@ "node": ">= 0.4.0" } }, - "node_modules/uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/validate-npm-package-license": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", @@ -21528,12 +21512,6 @@ "@types/node": "*" } }, - "@types/uuid": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.4.tgz", - "integrity": "sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==", - "dev": true - }, "@types/webpack-env": { "version": "1.17.0", "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.17.0.tgz", @@ -32776,11 +32754,6 @@ "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=", "dev": true }, - "uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==" - }, "validate-npm-package-license": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", diff --git a/src/ui/web/package.json b/src/ui/web/package.json index 4aa12956..43102903 100644 --- a/src/ui/web/package.json +++ b/src/ui/web/package.json @@ -14,7 +14,6 @@ "core-js": "^3.25.5", "finalhandler": "^1.2.0", "serve-static": "^1.15.0", - "uuid": "^9.0.0", "vue": "^3.2.41", "vue-class-component": "^8.0.0-rc.1", "vue-router": "^4.1.5" @@ -23,7 +22,6 @@ "@types/chai": "^4.3.3", "@types/mocha": "^10.0.0", "@types/node": "^18.11.0", - "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^5.40.0", "@typescript-eslint/parser": "^5.40.0", "@vue/cli": "~5.0.8", diff --git a/src/ui/web/src/composables/UIClient.ts b/src/ui/web/src/composables/UIClient.ts index e920545a..cf989400 100644 --- a/src/ui/web/src/composables/UIClient.ts +++ b/src/ui/web/src/composables/UIClient.ts @@ -3,7 +3,6 @@ import type { ProtocolResponse, ResponsePayload } from '@/types/UIProtocol'; import Utils from './Utils'; import config from '@/assets/config'; -import { v4 as uuidv4 } from 'uuid'; type ResponseHandler = { procedureName: ProcedureName; @@ -145,7 +144,7 @@ export default class UIClient { let uuid: string; return Utils.promiseWithTimeout( new Promise((resolve, reject) => { - uuid = uuidv4(); + uuid = crypto.randomUUID(); const msg = JSON.stringify([uuid, command, data]); if (this._ws.readyState !== WebSocket.OPEN) { diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index f297f228..18d56497 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,7 +1,5 @@ import crypto from 'crypto'; -import { v4 as uuid } from 'uuid'; - import { WebSocketCloseEventStatusString } from '../types/WebSocket'; export default class Utils { @@ -14,7 +12,11 @@ export default class Utils { } public static generateUUID(): string { - return uuid(); + return crypto.randomUUID(); + } + + public static validateUUID(uuid: string): boolean { + return /\/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$\/i/.test(uuid); } public static async sleep(milliSeconds: number): Promise { -- 2.34.1