Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

1.9 KB
Raw Blame
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";

/**
 * `crypto.randomUUID` exists only in secure contexts, so every bare call in the
 * desktop/web bundle crashes under the plain-HTTP Docker deployment (issue #9306).
 * The guard-compatible forms — `crypto?.randomUUID?.()`, `globalThis.crypto?.randomUUID?.()`
 * — are allowed, as is the shared helper that owns the fallback.
 */
const SOURCE_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
const HELPER_PATH = join("lib", "common", "utils.ts");
const BARE_CALL = /(?<!\?)crypto\s*\.\s*randomUUID\s*\(/;

function sourceFiles(directory: string): string[] {
  return readdirSync(directory).flatMap((entry) => {
    const path = join(directory, entry);
    if (statSync(path).isDirectory()) return entry === "__tests__" ? [] : sourceFiles(path);
    return entry.endsWith(".ts") || entry.endsWith(".vue") ? [path] : [];
  });
}

describe("crypto.randomUUID call sites", () => {
  it("recognizes direct calls but not guard-compatible ones", () => {
    expect(BARE_CALL.test("const id = crypto.randomUUID();")).toBe(true);
    expect(BARE_CALL.test("globalThis.crypto.randomUUID()")).toBe(true);
    expect(BARE_CALL.test("crypto?.randomUUID?.() || fallback")).toBe(false);
    expect(BARE_CALL.test("globalThis.crypto?.randomUUID?.() ?? fallback")).toBe(false);
  });

  it("routes every UUID through the shared helper instead of calling crypto.randomUUID directly", () => {
    const offenders = sourceFiles(SOURCE_ROOT)
      .map((path) => ({ path: relative(SOURCE_ROOT, path).split(sep).join("/"), text: readFileSync(path, "utf8") }))
      .filter(({ path }) => path !== HELPER_PATH.split(sep).join("/"))
      .filter(({ text }) => BARE_CALL.test(text))
      .map(({ path }) => path);

    expect(offenders).toEqual([]);
  });
});