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.
This commit is contained in:
Evan Simkowitz 2024-08-12 21:20:13 -07:00 committed by GitHub
parent 02801532c0
commit 9e1460b9e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 6 additions and 37 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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,

View File

@ -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.

View File

@ -185,18 +185,6 @@ const lazy = <T extends (...args: any[]) => 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,

View File

@ -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"

View File

@ -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"