From d91c4f50b4e71adac3a810bf4938c1beb6235f6f Mon Sep 17 00:00:00 2001 From: vorotamoroz Date: Fri, 29 Sep 2023 18:55:46 +0900 Subject: [PATCH] - Improved: - A New binary file handling implemented - A new encrypted format has been implemented - Now the chunk sizes will be adjusted for efficient sync - Fixed: - levels of exception in some logs have been fixed - Tidied: - Some Lint warnings have been suppressed. --- src/CmdConfigSync.ts | 6 ++--- src/CmdHiddenFileSync.ts | 10 ++++----- src/CmdSetupLiveSync.ts | 4 ++-- src/DocumentHistoryModal.ts | 8 +++---- src/GlobalHistory.svelte | 4 ++-- src/JsonResolvePane.svelte | 4 ++-- src/ObsidianLiveSyncSettingTab.ts | 13 +++++++++-- src/dialogs.ts | 1 - src/lib | 2 +- src/main.ts | 37 ++++++++++++++++++++++--------- 10 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/CmdConfigSync.ts b/src/CmdConfigSync.ts index 7e2af1f..09e4189 100644 --- a/src/CmdConfigSync.ts +++ b/src/CmdConfigSync.ts @@ -7,7 +7,7 @@ import { ICXHeader, PERIODIC_PLUGIN_SWEEP, } from "./types"; import { delay, getDocData } from "./lib/src/utils"; import { Logger } from "./lib/src/logger"; import { WrappedNotice } from "./lib/src/wrapper"; -import { base64ToArrayBuffer, arrayBufferToBase64, readString, crc32CKHash } from "./lib/src/strbin"; +import { readString, crc32CKHash, decodeBinary, encodeBinary } from "./lib/src/strbin"; import { serialized } from "./lib/src/lock"; import { LiveSyncCommands } from "./LiveSyncCommands"; import { stripAllPrefixes } from "./lib/src/path"; @@ -328,7 +328,7 @@ export class ConfigSync extends LiveSyncCommands { const path = `${baseDir}/${f.filename}`; await this.ensureDirectoryEx(path); if (!content) { - const dt = base64ToArrayBuffer(f.data); + const dt = decodeBinary(f.data); await this.app.vault.adapter.writeBinary(path, dt); } else { await this.app.vault.adapter.write(path, content); @@ -460,7 +460,7 @@ export class ConfigSync extends LiveSyncCommands { const contentBin = await this.app.vault.adapter.readBinary(path); let content: string[]; try { - content = await arrayBufferToBase64(contentBin); + content = await encodeBinary(contentBin, this.settings.useV1); if (path.toLowerCase().endsWith("/manifest.json")) { const v = readString(new Uint8Array(contentBin)); try { diff --git a/src/CmdHiddenFileSync.ts b/src/CmdHiddenFileSync.ts index 87c14d7..6fcfaef 100644 --- a/src/CmdHiddenFileSync.ts +++ b/src/CmdHiddenFileSync.ts @@ -6,7 +6,7 @@ import { Logger } from "./lib/src/logger"; import { PouchDB } from "./lib/src/pouchdb-browser.js"; import { scheduleTask, isInternalMetadata, PeriodicProcessor } from "./utils"; import { WrappedNotice } from "./lib/src/wrapper"; -import { base64ToArrayBuffer, arrayBufferToBase64 } from "./lib/src/strbin"; +import { decodeBinary, encodeBinary } from "./lib/src/strbin"; import { serialized } from "./lib/src/lock"; import { JsonResolveModal } from "./JsonResolveModal"; import { LiveSyncCommands } from "./LiveSyncCommands"; @@ -411,7 +411,7 @@ export class HiddenFileSync extends LiveSyncCommands { const contentBin = await this.app.vault.adapter.readBinary(file.path); let content: string[]; try { - content = await arrayBufferToBase64(contentBin); + content = await encodeBinary(contentBin, this.settings.useV1); } catch (ex) { Logger(`The file ${file.path} could not be encoded`); Logger(ex, LOG_LEVEL_VERBOSE); @@ -547,7 +547,7 @@ export class HiddenFileSync extends LiveSyncCommands { } if (!isExists) { await this.ensureDirectoryEx(filename); - await this.app.vault.adapter.writeBinary(filename, base64ToArrayBuffer(fileOnDB.data), { mtime: fileOnDB.mtime, ctime: fileOnDB.ctime }); + await this.app.vault.adapter.writeBinary(filename, decodeBinary(fileOnDB.data), { mtime: fileOnDB.mtime, ctime: fileOnDB.ctime }); try { //@ts-ignore internalAPI await this.app.vault.adapter.reconcileInternalFile(filename); @@ -559,12 +559,12 @@ export class HiddenFileSync extends LiveSyncCommands { return true; } else { const contentBin = await this.app.vault.adapter.readBinary(filename); - const content = await arrayBufferToBase64(contentBin); + const content = await encodeBinary(contentBin, this.settings.useV1); if (isDocContentSame(content, fileOnDB.data) && !force) { // Logger(`STORAGE <-- DB:${filename}: skipped (hidden) Not changed`, LOG_LEVEL_VERBOSE); return true; } - await this.app.vault.adapter.writeBinary(filename, base64ToArrayBuffer(fileOnDB.data), { mtime: fileOnDB.mtime, ctime: fileOnDB.ctime }); + await this.app.vault.adapter.writeBinary(filename, decodeBinary(fileOnDB.data), { mtime: fileOnDB.mtime, ctime: fileOnDB.ctime }); try { //@ts-ignore internalAPI await this.app.vault.adapter.reconcileInternalFile(filename); diff --git a/src/CmdSetupLiveSync.ts b/src/CmdSetupLiveSync.ts index 857b83d..91680be 100644 --- a/src/CmdSetupLiveSync.ts +++ b/src/CmdSetupLiveSync.ts @@ -60,7 +60,7 @@ export class SetupLiveSync extends LiveSyncCommands { delete setting[k]; } } - const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false)); + const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false, false)); const uri = `${configURIBase}${encryptedSetting}`; await navigator.clipboard.writeText(uri); Logger("Setup URI copied to clipboard", LOG_LEVEL_NOTICE); @@ -70,7 +70,7 @@ export class SetupLiveSync extends LiveSyncCommands { if (encryptingPassphrase === false) return; const setting = { ...this.settings, configPassphraseStore: "", encryptedCouchDBConnection: "", encryptedPassphrase: "" }; - const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false)); + const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false, false)); const uri = `${configURIBase}${encryptedSetting}`; await navigator.clipboard.writeText(uri); Logger("Setup URI copied to clipboard", LOG_LEVEL_NOTICE); diff --git a/src/DocumentHistoryModal.ts b/src/DocumentHistoryModal.ts index 7721caf..350b0e5 100644 --- a/src/DocumentHistoryModal.ts +++ b/src/DocumentHistoryModal.ts @@ -1,6 +1,6 @@ import { TFile, Modal, App, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, diff_match_patch } from "./deps"; import { getPathFromTFile, isValidPath } from "./utils"; -import { base64ToArrayBuffer, base64ToString, escapeStringToHTML } from "./lib/src/strbin"; +import { decodeBinary, escapeStringToHTML, readString } from "./lib/src/strbin"; import ObsidianLiveSyncPlugin from "./main"; import { type DocumentID, type FilePathWithPrefix, type LoadedEntry, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "./lib/src/types"; import { Logger } from "./lib/src/logger"; @@ -89,7 +89,7 @@ export class DocumentHistoryModal extends Modal { this.currentDoc = w; this.info.innerHTML = `Modified:${new Date(w.mtime).toLocaleString()}`; let result = ""; - const w1data = w.datatype == "plain" ? getDocData(w.data) : base64ToString(w.data); + const w1data = w.datatype == "plain" ? getDocData(w.data) : readString(new Uint8Array(decodeBinary(w.data))); this.currentDeleted = !!w.deleted; this.currentText = w1data; if (this.showDiff) { @@ -99,7 +99,7 @@ export class DocumentHistoryModal extends Modal { const w2 = await db.getDBEntry(this.file, { rev: oldRev }, false, false, true); if (w2 != false) { const dmp = new diff_match_patch(); - const w2data = w2.datatype == "plain" ? getDocData(w2.data) : base64ToString(w2.data); + const w2data = w2.datatype == "plain" ? getDocData(w2.data) : readString(new Uint8Array(decodeBinary(w.data))); const diff = dmp.diff_main(w2data, w1data); dmp.diff_cleanupSemantic(diff); for (const v of diff) { @@ -204,7 +204,7 @@ export class DocumentHistoryModal extends Modal { await focusFile(pathToWrite); this.close(); } else if (this.currentDoc?.datatype == "newnote") { - await this.app.vault.adapter.writeBinary(pathToWrite, base64ToArrayBuffer(this.currentDoc.data)); + await this.app.vault.adapter.writeBinary(pathToWrite, decodeBinary(this.currentDoc.data)); await focusFile(pathToWrite); this.close(); } else { diff --git a/src/GlobalHistory.svelte b/src/GlobalHistory.svelte index 33a51e8..9463383 100644 --- a/src/GlobalHistory.svelte +++ b/src/GlobalHistory.svelte @@ -7,7 +7,7 @@ import { DocumentHistoryModal } from "./DocumentHistoryModal"; import { isPlainText, stripAllPrefixes } from "./lib/src/path"; import { TFile } from "./deps"; - import { arrayBufferToBase64 } from "./lib/src/strbin"; + import { encodeBinary } from "./lib/src/strbin"; export let plugin: ObsidianLiveSyncPlugin; let showDiffInfo = false; @@ -116,7 +116,7 @@ result = isDocContentSame(data, doc.data); } else { const data = await plugin.app.vault.readBinary(abs); - const dataEEncoded = await arrayBufferToBase64(data); + const dataEEncoded = await encodeBinary(data, plugin.settings.useV1); result = isDocContentSame(dataEEncoded, doc.data); } if (result) { diff --git a/src/JsonResolvePane.svelte b/src/JsonResolvePane.svelte index e814966..f3269cc 100644 --- a/src/JsonResolvePane.svelte +++ b/src/JsonResolvePane.svelte @@ -1,7 +1,7 @@