Move ser/deser logic to JS

This commit is contained in:
Andras Schmelczer 2025-01-05 20:49:38 +00:00
parent 7e045caab1
commit e43a13648b
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 59 additions and 17 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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) {

View file

@ -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);
});
});

View file

@ -0,0 +1,3 @@
export function deserialize(data: string): Uint8Array {
return Buffer.from(data, "base64");
}

View file

@ -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);
});
});

View file

@ -0,0 +1,3 @@
export function serialize(data: Uint8Array): string {
return Buffer.from(data).toString("base64");
}