diff --git a/plugin/src/file-operations/obsidian-file-operations.ts b/plugin/src/file-operations/obsidian-file-operations.ts index c578018..6a82006 100644 --- a/plugin/src/file-operations/obsidian-file-operations.ts +++ b/plugin/src/file-operations/obsidian-file-operations.ts @@ -49,7 +49,7 @@ export class ObsidianFileOperations implements FileOperations { return new Uint8Array(0); } - if (isBinary(expectedContent) || !path.endsWith(".md")) { + if (isBinary(expectedContent)) { await this.vault.adapter.writeBinary( normalizePath(path), newContent diff --git a/plugin/src/services/sync-service.ts b/plugin/src/services/sync-service.ts index 32f09d8..d9c3410 100644 --- a/plugin/src/services/sync-service.ts +++ b/plugin/src/services/sync-service.ts @@ -10,7 +10,7 @@ import type { } from "src/database/document-metadata"; import { Logger } from "src/tracing/logger"; import { retriedFetch } from "src/utils/retried-fetch"; -import { bytesToBase64 } from "sync_lib"; +import { serialize } from "src/utils/serialize"; export interface CheckConnectionResult { isSuccessful: boolean; @@ -83,7 +83,7 @@ export class SyncService { } }, body: { - contentBase64: bytesToBase64(contentBytes), + contentBase64: serialize(contentBytes), createdDate: createdDate.toISOString(), relativePath } @@ -132,7 +132,7 @@ export class SyncService { }, body: { parentVersionId, - contentBase64: bytesToBase64(contentBytes), + contentBase64: serialize(contentBytes), createdDate: createdDate.toISOString(), relativePath } diff --git a/plugin/src/sync-operations/syncer.ts b/plugin/src/sync-operations/syncer.ts index 5621f7c..bbf4360 100644 --- a/plugin/src/sync-operations/syncer.ts +++ b/plugin/src/sync-operations/syncer.ts @@ -12,7 +12,7 @@ import { unlockDocument, waitForDocumentLock } from "./document-lock"; import PQueue from "p-queue"; import { EMPTY_HASH, hash } from "src/utils/hash"; import type { components } from "src/services/types"; -import { base64ToBytes } from "sync_lib"; +import { deserialize } from "src/utils/deserialize"; export class Syncer { private readonly remainingOperationsListeners: (( @@ -238,7 +238,7 @@ export class Syncer { }); if (response.type === "MergingUpdate") { - const responseBytes = base64ToBytes(response.contentBase64); + const responseBytes = deserialize(response.contentBase64); contentHash = hash(responseBytes); await this.operations.write( @@ -364,7 +364,7 @@ export class Syncer { } if (response.type === "MergingUpdate") { - const responseBytes = base64ToBytes( + const responseBytes = deserialize( response.contentBase64 ); contentHash = hash(responseBytes); @@ -373,15 +373,15 @@ export class Syncer { contentBytes, responseBytes ); - } - this.history.addHistoryEntry({ - status: SyncStatus.SUCCESS, - source: SyncSource.PULL, - relativePath, - message: `The file we updated had been updated remotely, so we downloaded the merged version`, - type: SyncType.UPDATE - }); + this.history.addHistoryEntry({ + status: SyncStatus.SUCCESS, + source: SyncSource.PULL, + relativePath, + message: `The file we updated had been updated remotely, so we downloaded the merged version`, + type: SyncType.UPDATE + }); + } await this.database.moveDocument({ documentId: localMetadata.documentId, @@ -470,7 +470,7 @@ export class Syncer { documentId: remoteVersion.documentId }) ).contentBase64; - const contentBytes = base64ToBytes(content); + const contentBytes = deserialize(content); await this.operations.create( remoteVersion.relativePath, @@ -532,7 +532,7 @@ export class Syncer { documentId: remoteVersion.documentId }) ).contentBase64; - const contentBytes = base64ToBytes(content); + const contentBytes = deserialize(content); const contentHash = hash(contentBytes); if (relativePath !== remoteVersion.relativePath) { diff --git a/plugin/src/utils/deserialize.test.ts b/plugin/src/utils/deserialize.test.ts new file mode 100644 index 0000000..fa50a2c --- /dev/null +++ b/plugin/src/utils/deserialize.test.ts @@ -0,0 +1,18 @@ +import init, { base64ToBytes } from "sync_lib"; +import fs from "fs"; + +describe("deserialize", () => { + it("should serialize a Uint8Array to a base64 string", async () => { + const wasmBin = fs.readFileSync( + "../backend/sync_lib/pkg/sync_lib_bg.wasm" + ); + await init({ module_or_path: wasmBin }); + + const base64 = "SGVsbG8="; + const jsResult = base64ToBytes(base64); + const expected = new Uint8Array([72, 101, 108, 108, 111]); + expect(jsResult).toEqual(expected); + const rustResult = base64ToBytes(base64); + expect(jsResult).toEqual(rustResult); + }); +}); diff --git a/plugin/src/utils/deserialize.ts b/plugin/src/utils/deserialize.ts new file mode 100644 index 0000000..c02e18f --- /dev/null +++ b/plugin/src/utils/deserialize.ts @@ -0,0 +1,3 @@ +export function deserialize(data: string): Uint8Array { + return Buffer.from(data, "base64"); +} diff --git a/plugin/src/utils/serialize.test.ts b/plugin/src/utils/serialize.test.ts new file mode 100644 index 0000000..ae2016e --- /dev/null +++ b/plugin/src/utils/serialize.test.ts @@ -0,0 +1,18 @@ +import { serialize } from "./serialize"; +import init, { bytesToBase64 } from "sync_lib"; +import fs from "fs"; + +describe("serialize", () => { + it("should serialize a Uint8Array to a base64 string", async () => { + const wasmBin = fs.readFileSync( + "../backend/sync_lib/pkg/sync_lib_bg.wasm" + ); + await init({ module_or_path: wasmBin }); + + const data = new Uint8Array([72, 101, 108, 108, 111]); + const jsResult = serialize(data); + const rustResult = bytesToBase64(data); + expect(rustResult).toBe("SGVsbG8="); + expect(jsResult).toBe(rustResult); + }); +}); diff --git a/plugin/src/utils/serialize.ts b/plugin/src/utils/serialize.ts new file mode 100644 index 0000000..be9a9ab --- /dev/null +++ b/plugin/src/utils/serialize.ts @@ -0,0 +1,3 @@ +export function serialize(data: Uint8Array): string { + return Buffer.from(data).toString("base64"); +}