fix(sql): prevent oversized SQL editor loads

This commit is contained in:
zipg authored and GitHub committed 2026-07-28 23:59:34 +08:00
1 parent fc1232fd7f
commit f5c25510dc
16 files changed
+280 -34

No files matched your search

@@ -0,0 +1,34 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ExternalSqlFileTooLargeError } from "@/lib/sql/sqlFileOpen";
const mocks = vi.hoisted(() => ({
invoke: vi.fn(),
}));
vi.mock("@tauri-apps/api/core", () => ({
invoke: mocks.invoke,
}));
import { readExternalSqlFile } from "@/lib/backend/tauri";
describe("external SQL file API", () => {
beforeEach(() => {
mocks.invoke.mockReset();
});
it("returns editor content from the structured backend response", async () => {
mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;" });
await expect(readExternalSqlFile("/tmp/demo.sql")).resolves.toBe("select 1;");
expect(mocks.invoke).toHaveBeenCalledWith("read_external_sql_file", { path: "/tmp/demo.sql" });
});
it("maps oversized responses to a typed frontend error", async () => {
mocks.invoke.mockResolvedValue({ kind: "tooLarge", sizeBytes: 50 * 1024 ** 3, maxSizeBytes: 64 * 1024 ** 2 });
const error = await readExternalSqlFile("/tmp/backup.sql").catch((reason) => reason);
expect(error).toBeInstanceOf(ExternalSqlFileTooLargeError);
expect(error).toMatchObject({ sizeBytes: 50 * 1024 ** 3, maxSizeBytes: 64 * 1024 ** 2 });
});
});
+6 -1
View File
@@ -1,6 +1,7 @@
import { invoke } from "@tauri-apps/api/core";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { normalizeRustMongoCommand, type MongoCommand } from "@/lib/mongo/mongoShellCommand";
import { ExternalSqlFileTooLargeError } from "@/lib/sql/sqlFileOpen";
import type {
ConnectionConfig,
ConnectionTestResult,
@@ -670,7 +671,11 @@ export async function pendingOpenConnectionLinks(): Promise<string[]> {
}
export async function readExternalSqlFile(path: string): Promise<string> {
return invoke("read_external_sql_file", { path });
const result = await invoke<{ kind: "content"; content: string } | { kind: "tooLarge"; sizeBytes: number; maxSizeBytes: number }>("read_external_sql_file", { path });
if (result.kind === "tooLarge") {
throw new ExternalSqlFileTooLargeError(result.sizeBytes, result.maxSizeBytes);
}
return result.content;
}
export async function writeExternalSqlFile(path: string, content: string): Promise<void> {