Skip to content

Build your personal knowledge base with Trilium Notes

perf(import): detect UTF-16 by BOM instead of chardet

Elian Doran6c8f36a7b55e
diff --git a/packages/trilium-core/src/services/utils/binary.spec.ts b/packages/trilium-core/src/services/utils/binary.spec.ts
index 9d4880abaa..0996baf110 100644
--- a/packages/trilium-core/src/services/utils/binary.spec.ts
+++ b/packages/trilium-core/src/services/utils/binary.spec.ts
@@ -212,7 +212,12 @@ describe("binary utils", () => {
expect(processStringOrBuffer(buffer)).toBe("Hello UTF-8 world with enough text to detect");
});
- it("decodes a UTF-16LE buffer and strips the BOM", () => {
+ it("decodes multi-byte (CJK) UTF-8 content", () => {
+ const buffer = encodeUtf8("中文测试内容");
+ expect(processStringOrBuffer(buffer)).toBe("中文测试内容");
+ });
+
+ it("decodes a UTF-16LE buffer with a BOM and strips the BOM", () => {
// BOM (0xFF 0xFE) followed by "Hi" in little-endian UTF-16
const buffer = new Uint8Array([0xff, 0xfe, 0x48, 0x00, 0x69, 0x00]);
const result = processStringOrBuffer(buffer);
@@ -221,8 +226,14 @@ describe("binary utils", () => {
expect(result.charCodeAt(0)).not.toBe(0xfeff);
});
- it("falls back to UTF-8 decoding for undetected encodings", () => {
- // a short ASCII buffer that chardet may not classify as UTF-16
+ it("treats UTF-16 without a BOM as UTF-8 (matching the previous chardet behaviour)", () => {
+ // "Hi" in little-endian UTF-16 but with no BOM: chardet never classified this as UTF-16LE
+ // either, so it was — and still is — decoded byte-wise as UTF-8 (null high bytes preserved).
+ const buffer = new Uint8Array([0x48, 0x00, 0x69, 0x00]);
+ expect([...processStringOrBuffer(buffer)].map((c) => c.charCodeAt(0))).toEqual([0x48, 0x00, 0x69, 0x00]);
+ });
+
+ it("decodes a plain ASCII buffer as UTF-8", () => {
const buffer = new Uint8Array([0x61, 0x62, 0x63]); // "abc"
expect(processStringOrBuffer(buffer)).toBe("abc");
});
diff --git a/packages/trilium-core/src/services/utils/binary.ts b/packages/trilium-core/src/services/utils/binary.ts
index a2b8ddd589..8a1b115f93 100644
--- a/packages/trilium-core/src/services/utils/binary.ts
+++ b/packages/trilium-core/src/services/utils/binary.ts
@@ -1,5 +1,3 @@
-import chardet from "chardet";
-
const utf8Decoder = new TextDecoder("utf-8");
const utf8Encoder = new TextEncoder();
@@ -111,13 +109,13 @@ export function processStringOrBuffer(data: string | Uint8Array | null) {
return data;
}
- const detectedEncoding = chardet.detect(data);
- switch (detectedEncoding) {
- case "UTF-16LE":
- return stripBom(new TextDecoder("utf-16le").decode(data));
- /* v8 ignore next -- empty fall-through label; coverage is attributed to the shared `default` body */
- case "UTF-8":
- default:
- return utf8Decoder.decode(data);
+ // The only non-UTF-8 encoding we decode is UTF-16LE. Detection previously used chardet, but chardet
+ // only ever resolved to UTF-16LE when the FF FE byte-order mark was present — BOM-less UTF-16 was
+ // detected as a single-byte encoding and decoded as UTF-8 anyway. A cheap BOM check reproduces that
+ // behaviour exactly, without running statistical charset detection on every imported note's content.
+ if (data.length >= 2 && data[0] === 0xFF && data[1] === 0xFE) {
+ return stripBom(new TextDecoder("utf-16le").decode(data));
}
+
+ return utf8Decoder.decode(data);
}