Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
backup: refactor
Adorian Dorana83dff3683f4
diff --git a/apps/client/src/setup_restore.spec.tsx b/apps/client/src/setup_restore.spec.tsxindex 246a91d993..df95b57fe8 100644--- a/apps/client/src/setup_restore.spec.tsx+++ b/apps/client/src/setup_restore.spec.tsx@@ -697,7 +697,7 @@ describe("what the restore calls the stage it is at", () => {}});- it("says what happens next once the restore is done, rather than naming a finished step", () => {+ it("says what happens next once it is done, rather than naming a finished step", () => {// The frame between the last step and the reload, and the one the user watches hardest.expect(stageLabel("done")).toBe("setup.redirecting");// Nothing is said about a failure here: the screen replacing this one says it properly.diff --git a/apps/server/src/backup_provider.ts b/apps/server/src/backup_provider.tsindex 1fca6dd1b9..f2259d71e0 100644--- a/apps/server/src/backup_provider.ts+++ b/apps/server/src/backup_provider.ts@@ -1,6 +1,6 @@import {FIXED_HEADER_BYTES,- peekBackupContainer,+ getInfo,writeBackupContainer} from "@triliumnext/backup-container";import type {@@ -325,16 +325,16 @@ function describeContainer(filePath: string, fileName: string): Partial<Database}}- const info = peekBackupContainer(head);- if (!info) {+ const info = getInfo(head);+ if (!info.isValid || !info.isSupported) {return {};}return {- compressed: info.compressed,- encrypted: info.encrypted,+ compressed: info.isCompressed,+ encrypted: info.isEncrypted,// Recorded as 0 when the writer did not know it, which reads the same as "not stated".- plaintextSize: info.plaintextSize > 0 ? info.plaintextSize : undefined+ plaintextSize: info.size > 0 ? info.size : undefined};}diff --git a/apps/server/src/services/database_restore.ts b/apps/server/src/services/database_restore.tsindex b2cceeba87..c5f7a52fc2 100644--- a/apps/server/src/services/database_restore.ts+++ b/apps/server/src/services/database_restore.ts@@ -1,8 +1,8 @@import {type BackupContainerErrorReason,FIXED_HEADER_BYTES,+ getInfo,isBackupContainerError,- peekBackupContainer,type ProgressCallback,readBackupContainer} from "@triliumnext/backup-container";@@ -416,9 +416,12 @@ export function readBackupFormat(filePath: string): BackupFormat | null {}}- const container = peekBackupContainer(head);+ const container = getInfo(head);+ // A version this build cannot open is left to the reader to refuse, which it does by name; here+ // it is only ever a file this path has no way to unwrap.+ const readable = container.isValid && container.isSupported;- return { container: !!container, encrypted: container?.encrypted ?? false };+ return { container: readable, encrypted: readable && container.isEncrypted };}/**diff --git a/apps/standalone/src/lightweight/database_restore.ts b/apps/standalone/src/lightweight/database_restore.tsindex cdc4fe7741..558acec68e 100644--- a/apps/standalone/src/lightweight/database_restore.ts+++ b/apps/standalone/src/lightweight/database_restore.ts@@ -1,11 +1,11 @@import type { SAHPoolUtil } from "@sqlite.org/sqlite-wasm";import {- type BackupContainerInfo,FIXED_HEADER_BYTES,+ getInfo,isBackupContainerError,- peekBackupContainer,type ProgressCallback,readBackupContainer,+ type SupportedBackupContainer,containerSize} from "@triliumnext/backup-container/web";import {@@ -399,12 +399,12 @@ export async function writeCurrentDatabaseName(dbName: string): Promise<void> {*/async function readBackupFormat(backup: Blob): Promise<{ container: boolean; encrypted: boolean } | null> {const head = new Uint8Array(await backup.slice(0, FIXED_HEADER_BYTES).arrayBuffer());- const container = peekBackupContainer(head);+ const container = getInfo(head);- if (container) {+ if (container.isValid && container.isSupported) {requireWholeContainer(container, backup.size);- return { container: true, encrypted: container.encrypted };+ return { container: true, encrypted: container.isEncrypted };}return looksLikeSqlite(head) ? { container: false, encrypted: false } : null;@@ -417,12 +417,12 @@ async function readBackupFormat(backup: Blob): Promise<{ container: boolean; enc* header records. Compression breaks the derivation, since the payload is then shorter than the* plaintext by a ratio nothing states in advance.*/-function requireWholeContainer(container: BackupContainerInfo, size: number): void {- if (container.compressed || container.plaintextSize <= 0) {+function requireWholeContainer(container: SupportedBackupContainer, size: number): void {+ if (container.isCompressed || container.size <= 0) {return;}- const expected = containerSize(container.plaintextSize, container.encrypted);+ const expected = containerSize(container.size, container.isEncrypted);if (size < expected) {throw incompleteBackup(size, expected);}