Skip to content

Build your personal knowledge base with Trilium Notes

chore(core): fix usage of Buffer

Elian Doran55f09fe21a61
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index 30aec07ada..0aa29ab568 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -29,7 +29,7 @@ interface ImportZipOpts {
preserveIds?: boolean;
}
-async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Buffer, importRootNote: BNote, opts?: ImportZipOpts): Promise<BNote> {
+async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote, opts?: ImportZipOpts): Promise<BNote> {
/** maps from original noteId (in ZIP file) to newly generated noteId */
const noteIdMap: Record<string, string> = {};
/** type maps from original attachmentId (in ZIP file) to newly generated attachmentId */
@@ -655,9 +655,9 @@ export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise
});
}
-export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise<void>) {
+export function readZipFile(buffer: Uint8Array, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise<void>) {
return new Promise<void>((res, rej) => {
- yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, (err, zipfile) => {
+ yauzl.fromBuffer(Buffer.from(buffer), { lazyEntries: true, validateEntrySizes: false }, (err, zipfile) => {
if (err) rej(err);
if (!zipfile) throw new Error("Unable to read zip file.");
diff --git a/packages/trilium-core/src/services/utils/binary.ts b/packages/trilium-core/src/services/utils/binary.ts
index dc31b46af0..721119080f 100644
--- a/packages/trilium-core/src/services/utils/binary.ts
+++ b/packages/trilium-core/src/services/utils/binary.ts
@@ -73,21 +73,21 @@ export function wrapStringOrBuffer(stringOrBuffer: string | Uint8Array) {
* @param data the string or buffer to process.
* @returns the string representation of the buffer, or the same string is it's a string.
*/
-export function processStringOrBuffer(data: string | Buffer | null) {
+export function processStringOrBuffer(data: string | Uint8Array | null) {
if (!data) {
return "";
}
- if (!Buffer.isBuffer(data)) {
+ if (typeof data === "string") {
return data;
}
const detectedEncoding = chardet.detect(data);
switch (detectedEncoding) {
case "UTF-16LE":
- return stripBom(data.toString("utf-16le"));
+ return stripBom(new TextDecoder("utf-16le").decode(data));
case "UTF-8":
default:
- return data.toString("utf-8");
+ return utf8Decoder.decode(data);
}
}