fix: replace crypto.randomUUID with polyfill for HTTP contexts

crypto.randomUUID() is only available in secure contexts (HTTPS/localhost).
Add uuid() utility with fallback for HTTP deployments.
This commit is contained in:
t8y2
2026-05-05 23:51:07 +08:00
parent 4e5a7cb40a
commit 0aa562f6ee
16 changed files with 48 additions and 23 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
import type { AiConfig } from "@/stores/settingsStore";
import { uuid } from "@/lib/utils";
import type {
ColumnInfo,
ConnectionConfig,
@@ -111,7 +112,7 @@ export async function runAiStream(
const messages: api.AiMessage[] = [...(history || []), { role: "user", content: userPrompt }];
const sid = sessionId || crypto.randomUUID();
const sid = sessionId || uuid();
const params = actionParams(input.action);
await api.aiStream(
+2 -1
View File
@@ -1,4 +1,5 @@
import type { ConnectionConfig, ConnectionGroup, SidebarLayout, SidebarOrderEntry, TreeNode } from "@/types/database";
import { uuid } from "@/lib/utils";
export function emptyLayout(): SidebarLayout {
return { groups: [], order: [] };
@@ -234,7 +235,7 @@ function reorderConnection(
}
export function createGroup(layout: SidebarLayout, name: string): { layout: SidebarLayout; groupId: string } {
const groupId = crypto.randomUUID();
const groupId = uuid();
const group: ConnectionGroup = { id: groupId, name, collapsed: false };
return {
groupId,
+10
View File
@@ -5,3 +5,13 @@ import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function uuid(): string {
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
return crypto.randomUUID();
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
});
}