From 9e1460b9e1fb5939b75ebf6ab6313bc9b75e84ab Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Mon, 12 Aug 2024 21:20:13 -0700 Subject: [PATCH] Remove UUID library in favor of Crypto (#221) This PR swaps usage of the `uuid` library for the built-in `crypto.randomUUID` function, which is available in both NodeJS and the browser. The built-in function is around 12x faster than the `uuid` library. The strings produced by the built-in function are fully compatible with the UUIDv4 standard, so it's an easy switch. --- frontend/app/store/contextmenu.ts | 3 +-- frontend/app/store/wos.ts | 5 ++--- frontend/app/view/waveai/waveai.tsx | 8 +++----- frontend/layout/lib/layoutNode.ts | 3 --- frontend/util/util.ts | 13 ------------- package.json | 1 - yarn.lock | 10 ---------- 7 files changed, 6 insertions(+), 37 deletions(-) diff --git a/frontend/app/store/contextmenu.ts b/frontend/app/store/contextmenu.ts index 71ddf96f1..f1f23c110 100644 --- a/frontend/app/store/contextmenu.ts +++ b/frontend/app/store/contextmenu.ts @@ -1,7 +1,6 @@ // Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 -import { v4 as uuidv4 } from "uuid"; import { getApi } from "./global"; class ContextMenuModelType { @@ -25,7 +24,7 @@ class ContextMenuModelType { role: item.role, type: item.type, label: item.label, - id: uuidv4(), + id: crypto.randomUUID(), }; if (item.click) { this.handlers.set(electronItem.id, item.click); diff --git a/frontend/app/store/wos.ts b/frontend/app/store/wos.ts index 2620ffd94..19febf6a3 100644 --- a/frontend/app/store/wos.ts +++ b/frontend/app/store/wos.ts @@ -7,7 +7,6 @@ import { sendRpcCommand } from "@/app/store/wshrpc"; import { getWebServerEndpoint } from "@/util/endpoints"; import * as jotai from "jotai"; import * as React from "react"; -import { v4 as uuidv4 } from "uuid"; import { atoms, globalStore } from "./global"; import * as services from "./services"; @@ -120,7 +119,7 @@ function wshServerRpcHelper_responsestream( const msg: RpcMessage = { command: command, data: data, - reqid: uuidv4(), + reqid: crypto.randomUUID(), }; if (opts?.timeout) { msg.timeout = opts.timeout; @@ -135,7 +134,7 @@ function wshServerRpcHelper_call(command: string, data: any, opts: WshRpcCommand data: data, }; if (!opts?.noresponse) { - msg.reqid = uuidv4(); + msg.reqid = crypto.randomUUID(); } if (opts?.timeout) { msg.timeout = opts.timeout; diff --git a/frontend/app/view/waveai/waveai.tsx b/frontend/app/view/waveai/waveai.tsx index 3742e2e74..fea856f2e 100644 --- a/frontend/app/view/waveai/waveai.tsx +++ b/frontend/app/view/waveai/waveai.tsx @@ -11,8 +11,6 @@ import type { OverlayScrollbars } from "overlayscrollbars"; import { OverlayScrollbarsComponent, OverlayScrollbarsComponentRef } from "overlayscrollbars-react"; import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react"; import tinycolor from "tinycolor2"; -import { v4 as uuidv4 } from "uuid"; - import "./waveai.less"; interface ChatMessageType { @@ -33,7 +31,7 @@ interface ChatItemProps { function promptToMsg(prompt: OpenAIPromptMessageType): ChatMessageType { return { - id: uuidv4(), + id: crypto.randomUUID(), user: prompt.role, text: prompt.content, isAssistant: prompt.role == "assistant", @@ -78,7 +76,7 @@ export class WaveAiModel implements ViewModel { }); this.simulateAssistantResponseAtom = jotai.atom(null, async (get, set, userMessage: ChatMessageType) => { const typingMessage: ChatMessageType = { - id: uuidv4(), + id: crypto.randomUUID(), user: "assistant", text: "", isAssistant: true, @@ -145,7 +143,7 @@ export class WaveAiModel implements ViewModel { const sendMessage = (text: string, user: string = "user") => { const newMessage: ChatMessageType = { - id: uuidv4(), + id: crypto.randomUUID(), user, text, isAssistant: false, diff --git a/frontend/layout/lib/layoutNode.ts b/frontend/layout/lib/layoutNode.ts index 5eae76152..693e1652b 100644 --- a/frontend/layout/lib/layoutNode.ts +++ b/frontend/layout/lib/layoutNode.ts @@ -1,12 +1,9 @@ // Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 -import { getCrypto } from "@/util/util"; import { DefaultNodeSize, LayoutNode } from "./model"; import { FlexDirection, reverseFlexDirection } from "./utils"; -const crypto = getCrypto(); - /** * Creates a new node. * @param flexDirection The flex direction for the new node. diff --git a/frontend/util/util.ts b/frontend/util/util.ts index fbbe2280a..3c267df25 100644 --- a/frontend/util/util.ts +++ b/frontend/util/util.ts @@ -185,18 +185,6 @@ const lazy = any>(callback: T) => { }; }; -/** - * Workaround for NodeJS compatibility. Will attempt to resolve the Crypto API from the browser and fallback to NodeJS if it isn't present. - * @returns The Crypto API. - */ -function getCrypto() { - try { - return window.crypto; - } catch { - return crypto; - } -} - /** * Generates an external link by appending the given URL to the "https://extern?" endpoint. * @@ -212,7 +200,6 @@ export { base64ToString, boundNumber, fireAndForget, - getCrypto, getPromiseState, getPromiseValue, isBlank, diff --git a/package.json b/package.json index 2533c1a57..db71a8622 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,6 @@ "throttle-debounce": "^5.0.2", "tinycolor2": "^1.6.0", "use-device-pixel-ratio": "^1.1.2", - "uuid": "^10.0.0", "winston": "^3.14.1" }, "packageManager": "yarn@4.4.0" diff --git a/yarn.lock b/yarn.lock index 53fdf3e0a..040ecae84 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12670,7 +12670,6 @@ __metadata: typescript: "npm:^5.5.4" typescript-eslint: "npm:^8.0.1" use-device-pixel-ratio: "npm:^1.1.2" - uuid: "npm:^10.0.0" vite: "npm:^5.4.0" vite-plugin-image-optimizer: "npm:^1.1.8" vite-plugin-static-copy: "npm:^1.0.6" @@ -13309,15 +13308,6 @@ __metadata: languageName: node linkType: hard -"uuid@npm:^10.0.0": - version: 10.0.0 - resolution: "uuid@npm:10.0.0" - bin: - uuid: dist/bin/uuid - checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe - languageName: node - linkType: hard - "uuid@npm:^9.0.0": version: 9.0.1 resolution: "uuid@npm:9.0.1"