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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(import): decode connection import files beyond UTF-8
Johanb32ef45da1ce
diff --git a/apps/desktop/src/lib/common/__tests__/decodeText.spec.ts b/apps/desktop/src/lib/common/__tests__/decodeText.spec.tsnew file mode 100644index 0000000000..d1c70df846--- /dev/null+++ b/apps/desktop/src/lib/common/__tests__/decodeText.spec.ts@@ -0,0 +1,58 @@+import { describe, expect, it } from "vitest";+import { decodeImportFileText } from "@/lib/common/decodeText";++function utf16Bytes(text: string, littleEndian: boolean, bom: boolean): ArrayBuffer {+ const out: number[] = [];+ if (bom) out.push(...(littleEndian ? [0xff, 0xfe] : [0xfe, 0xff]));+ for (const char of text) {+ const code = char.charCodeAt(0);+ if (littleEndian) out.push(code & 0xff, (code >> 8) & 0xff);+ else out.push((code >> 8) & 0xff, code & 0xff);+ }+ return new Uint8Array(out).buffer as ArrayBuffer;+}++function utf8Bytes(text: string, bom: boolean): ArrayBuffer {+ const encoded = new TextEncoder().encode(text);+ if (!bom) return encoded.buffer as ArrayBuffer;+ const out = new Uint8Array(encoded.length + 3);+ out.set([0xef, 0xbb, 0xbf], 0);+ out.set(encoded, 3);+ return out.buffer as ArrayBuffer;+}++const xml = `<Connections><Connection ConnType="MYSQL" Name="local" Host="127.0.0.1" Port="3306" /></Connections>`;++describe("decodeImportFileText", () => {+ it("decodes plain UTF-8 unchanged", () => {+ expect(decodeImportFileText(utf8Bytes(xml, false))).toBe(xml);+ });++ it("strips a UTF-8 BOM", () => {+ expect(decodeImportFileText(utf8Bytes(xml, true))).toBe(xml);+ });++ it("decodes UTF-16LE with BOM (#10666 Navicat 17 macOS export)", () => {+ expect(decodeImportFileText(utf16Bytes(xml, true, true))).toBe(xml);+ });++ it("decodes UTF-16BE with BOM", () => {+ expect(decodeImportFileText(utf16Bytes(xml, false, true))).toBe(xml);+ });++ it("decodes BOM-less UTF-16LE via the null-byte heuristic", () => {+ expect(decodeImportFileText(utf16Bytes(xml, true, false))).toBe(xml);+ });++ it("decodes BOM-less UTF-16BE via the null-byte heuristic", () => {+ expect(decodeImportFileText(utf16Bytes(xml, false, false))).toBe(xml);+ });++ it("decodes an empty file to an empty string", () => {+ expect(decodeImportFileText(new ArrayBuffer(0))).toBe("");+ });++ it("accepts Uint8Array input (Tauri readFile shape)", () => {+ expect(decodeImportFileText(new Uint8Array(utf8Bytes(xml, false)))).toBe(xml);+ });+});diff --git a/apps/desktop/src/lib/common/decodeText.ts b/apps/desktop/src/lib/common/decodeText.tsnew file mode 100644index 0000000000..9c22dc575e--- /dev/null+++ b/apps/desktop/src/lib/common/decodeText.ts@@ -0,0 +1,44 @@+/**+ * Decode an imported connection file's bytes to text.+ *+ * Navicat 17 exports on macOS can arrive as UTF-16 (with or without BOM)+ * while the import path historically decoded everything as UTF-8, which+ * turned the XML into mojibake and surfaced as "Invalid Navicat connection+ * file" (#10666). Sniff the BOM first, then fall back to a null-byte+ * heuristic for BOM-less UTF-16; plain UTF-8 (including JSON payloads)+ * decodes exactly as before.+ */+export function decodeImportFileText(buffer: ArrayBuffer | Uint8Array): string {+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);+ if (bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) {+ return stripBom(new TextDecoder("utf-8").decode(bytes.subarray(3)));+ }+ if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xfe) {+ return stripBom(new TextDecoder("utf-16le").decode(bytes.subarray(2)));+ }+ if (bytes.length >= 2 && bytes[0] === 0xfe && bytes[1] === 0xff) {+ return stripBom(new TextDecoder("utf-16be").decode(bytes.subarray(2)));+ }+ // BOM-less UTF-16: ASCII-range text leaves a null in every other byte.+ // `<C` is `<\0C\0` in LE (nulls on odd indices) and `\0<\0C` in BE.+ const sample = bytes.subarray(0, 64);+ let evenNulls = 0;+ let oddNulls = 0;+ for (let i = 0; i < sample.length; i++) {+ if (sample[i] === 0) {+ if (i % 2 === 0) evenNulls++;+ else oddNulls++;+ }+ }+ if (oddNulls > 0 && oddNulls >= evenNulls) {+ return stripBom(new TextDecoder("utf-16le").decode(bytes));+ }+ if (evenNulls > 0 && evenNulls > oddNulls) {+ return stripBom(new TextDecoder("utf-16be").decode(bytes));+ }+ return stripBom(new TextDecoder("utf-8").decode(bytes));+}++function stripBom(text: string): string {+ return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;+}diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.tsindex 59a4587c87..badaee1a9a 100644--- a/apps/desktop/src/stores/connectionStore.ts+++ b/apps/desktop/src/stores/connectionStore.ts@@ -9870,14 +9870,18 @@ export const useConnectionStore = defineStore("connection", () => {if (isTauriRuntime()) {const { open } = await import("@tauri-apps/plugin-dialog");- const { readTextFile } = await import("@tauri-apps/plugin-fs");+ const { readFile } = await import("@tauri-apps/plugin-fs");+ const { decodeImportFileText } = await import("@/lib/common/decodeText");const path = await open({filters: source === "navicat" ? [{ name: "Navicat Connection Export", extensions: ["ncx", "xml"] }] : [{ name: "DBX JSON", extensions: ["json"] }],multiple: false,});if (!path) return null;- content = await readTextFile(path as string);+ // Raw bytes: Navicat 17 macOS exports can be UTF-16 (#10666), which+ // readTextFile would reject as invalid UTF-8.+ content = decodeImportFileText(await readFile(path as string));} else {+ const { decodeImportFileText } = await import("@/lib/common/decodeText");content = await new Promise<string>((resolve, reject) => {const input = document.createElement("input");input.type = "file";@@ -9888,10 +9892,10 @@ export const useConnectionStore = defineStore("connection", () => {reject(new Error("No file selected"));return;}- const reader = new FileReader();- reader.onload = () => resolve(reader.result as string);- reader.onerror = () => reject(reader.error);- reader.readAsText(file);+ file+ .arrayBuffer()+ .then((buffer) => resolve(decodeImportFileText(buffer)))+ .catch(reject);};input.click();});