Move ser/deser logic to JS
This commit is contained in:
parent
7e045caab1
commit
e43a13648b
7 changed files with 59 additions and 17 deletions
|
|
@ -49,7 +49,7 @@ export class ObsidianFileOperations implements FileOperations {
|
||||||
return new Uint8Array(0);
|
return new Uint8Array(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isBinary(expectedContent) || !path.endsWith(".md")) {
|
if (isBinary(expectedContent)) {
|
||||||
await this.vault.adapter.writeBinary(
|
await this.vault.adapter.writeBinary(
|
||||||
normalizePath(path),
|
normalizePath(path),
|
||||||
newContent
|
newContent
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import type {
|
||||||
} from "src/database/document-metadata";
|
} from "src/database/document-metadata";
|
||||||
import { Logger } from "src/tracing/logger";
|
import { Logger } from "src/tracing/logger";
|
||||||
import { retriedFetch } from "src/utils/retried-fetch";
|
import { retriedFetch } from "src/utils/retried-fetch";
|
||||||
import { bytesToBase64 } from "sync_lib";
|
import { serialize } from "src/utils/serialize";
|
||||||
|
|
||||||
export interface CheckConnectionResult {
|
export interface CheckConnectionResult {
|
||||||
isSuccessful: boolean;
|
isSuccessful: boolean;
|
||||||
|
|
@ -83,7 +83,7 @@ export class SyncService {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
contentBase64: bytesToBase64(contentBytes),
|
contentBase64: serialize(contentBytes),
|
||||||
createdDate: createdDate.toISOString(),
|
createdDate: createdDate.toISOString(),
|
||||||
relativePath
|
relativePath
|
||||||
}
|
}
|
||||||
|
|
@ -132,7 +132,7 @@ export class SyncService {
|
||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
parentVersionId,
|
parentVersionId,
|
||||||
contentBase64: bytesToBase64(contentBytes),
|
contentBase64: serialize(contentBytes),
|
||||||
createdDate: createdDate.toISOString(),
|
createdDate: createdDate.toISOString(),
|
||||||
relativePath
|
relativePath
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import { unlockDocument, waitForDocumentLock } from "./document-lock";
|
||||||
import PQueue from "p-queue";
|
import PQueue from "p-queue";
|
||||||
import { EMPTY_HASH, hash } from "src/utils/hash";
|
import { EMPTY_HASH, hash } from "src/utils/hash";
|
||||||
import type { components } from "src/services/types";
|
import type { components } from "src/services/types";
|
||||||
import { base64ToBytes } from "sync_lib";
|
import { deserialize } from "src/utils/deserialize";
|
||||||
|
|
||||||
export class Syncer {
|
export class Syncer {
|
||||||
private readonly remainingOperationsListeners: ((
|
private readonly remainingOperationsListeners: ((
|
||||||
|
|
@ -238,7 +238,7 @@ export class Syncer {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.type === "MergingUpdate") {
|
if (response.type === "MergingUpdate") {
|
||||||
const responseBytes = base64ToBytes(response.contentBase64);
|
const responseBytes = deserialize(response.contentBase64);
|
||||||
contentHash = hash(responseBytes);
|
contentHash = hash(responseBytes);
|
||||||
|
|
||||||
await this.operations.write(
|
await this.operations.write(
|
||||||
|
|
@ -364,7 +364,7 @@ export class Syncer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.type === "MergingUpdate") {
|
if (response.type === "MergingUpdate") {
|
||||||
const responseBytes = base64ToBytes(
|
const responseBytes = deserialize(
|
||||||
response.contentBase64
|
response.contentBase64
|
||||||
);
|
);
|
||||||
contentHash = hash(responseBytes);
|
contentHash = hash(responseBytes);
|
||||||
|
|
@ -373,15 +373,15 @@ export class Syncer {
|
||||||
contentBytes,
|
contentBytes,
|
||||||
responseBytes
|
responseBytes
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.SUCCESS,
|
status: SyncStatus.SUCCESS,
|
||||||
source: SyncSource.PULL,
|
source: SyncSource.PULL,
|
||||||
relativePath,
|
relativePath,
|
||||||
message: `The file we updated had been updated remotely, so we downloaded the merged version`,
|
message: `The file we updated had been updated remotely, so we downloaded the merged version`,
|
||||||
type: SyncType.UPDATE
|
type: SyncType.UPDATE
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await this.database.moveDocument({
|
await this.database.moveDocument({
|
||||||
documentId: localMetadata.documentId,
|
documentId: localMetadata.documentId,
|
||||||
|
|
@ -470,7 +470,7 @@ export class Syncer {
|
||||||
documentId: remoteVersion.documentId
|
documentId: remoteVersion.documentId
|
||||||
})
|
})
|
||||||
).contentBase64;
|
).contentBase64;
|
||||||
const contentBytes = base64ToBytes(content);
|
const contentBytes = deserialize(content);
|
||||||
|
|
||||||
await this.operations.create(
|
await this.operations.create(
|
||||||
remoteVersion.relativePath,
|
remoteVersion.relativePath,
|
||||||
|
|
@ -532,7 +532,7 @@ export class Syncer {
|
||||||
documentId: remoteVersion.documentId
|
documentId: remoteVersion.documentId
|
||||||
})
|
})
|
||||||
).contentBase64;
|
).contentBase64;
|
||||||
const contentBytes = base64ToBytes(content);
|
const contentBytes = deserialize(content);
|
||||||
const contentHash = hash(contentBytes);
|
const contentHash = hash(contentBytes);
|
||||||
|
|
||||||
if (relativePath !== remoteVersion.relativePath) {
|
if (relativePath !== remoteVersion.relativePath) {
|
||||||
|
|
|
||||||
18
plugin/src/utils/deserialize.test.ts
Normal file
18
plugin/src/utils/deserialize.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
3
plugin/src/utils/deserialize.ts
Normal file
3
plugin/src/utils/deserialize.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export function deserialize(data: string): Uint8Array {
|
||||||
|
return Buffer.from(data, "base64");
|
||||||
|
}
|
||||||
18
plugin/src/utils/serialize.test.ts
Normal file
18
plugin/src/utils/serialize.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
3
plugin/src/utils/serialize.ts
Normal file
3
plugin/src/utils/serialize.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export function serialize(data: Uint8Array): string {
|
||||||
|
return Buffer.from(data).toString("base64");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue