From 4f0ff8909a8262b3cecb471c197f48058a2230e8 Mon Sep 17 00:00:00 2001 From: pagoru Date: Fri, 22 Aug 2025 18:21:04 +0200 Subject: [PATCH] fix: updater + db have incorrect module paths - fix #113 --- src/modules/db/crypto.ts | 19 +++++++++++++------ src/modules/db/main.ts | 39 ++++++++++++++++++--------------------- src/utils/path.utils.ts | 2 +- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/modules/db/crypto.ts b/src/modules/db/crypto.ts index 76fcfc0..6268ff0 100644 --- a/src/modules/db/crypto.ts +++ b/src/modules/db/crypto.ts @@ -10,21 +10,28 @@ import { encryptSHA256, encryptToken, } from "../../utils/encryption.utils.ts"; +import { getModulePath } from "../../utils/path.utils.ts"; export const crypto = ({ pathname }: DbProps): DbCryptoMutable => { const load = async () => { //generate pep (pepper) try { - await Deno.stat(pathname + DATABASE_PEPPER_FILE); + await Deno.stat(getModulePath(pathname) + DATABASE_PEPPER_FILE); } catch (e) { - await Deno.writeTextFile(pathname + DATABASE_PEPPER_FILE, ulid()); + await Deno.writeTextFile( + getModulePath(pathname) + DATABASE_PEPPER_FILE, + ulid(), + ); } //generate key (hash) try { - await Deno.stat(pathname + DATABASE_KEY_FILE); + await Deno.stat(getModulePath(pathname) + DATABASE_KEY_FILE); } catch (e) { - await Deno.writeTextFile(pathname + DATABASE_KEY_FILE, ulid()); + await Deno.writeTextFile( + getModulePath(pathname) + DATABASE_KEY_FILE, + ulid(), + ); } }; @@ -32,7 +39,7 @@ export const crypto = ({ pathname }: DbProps): DbCryptoMutable => { ///////ENCRYPT/DECRYPT/////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// const getSecretKey = (): Promise => - Deno.readTextFile(pathname + DATABASE_KEY_FILE); + Deno.readTextFile(getModulePath(pathname) + DATABASE_KEY_FILE); const $encryptSHA256 = async (text: string): Promise => { return await encryptSHA256(text, await getSecretKey()); @@ -46,7 +53,7 @@ export const crypto = ({ pathname }: DbProps): DbCryptoMutable => { ///////BCRYPT/WITH/PEPPER//////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// const getPepper = (): Promise => - Deno.readTextFile(pathname + DATABASE_PEPPER_FILE); + Deno.readTextFile(getModulePath(pathname) + DATABASE_PEPPER_FILE); const pepperPassword = async (password: string): Promise => (await getPepper()) + ":" + password; diff --git a/src/modules/db/main.ts b/src/modules/db/main.ts index 26afe6a..2e5ecd8 100644 --- a/src/modules/db/main.ts +++ b/src/modules/db/main.ts @@ -38,27 +38,27 @@ export const getDb = (props: DbProps = {}): DbMutable => { }, } = props; const parsedProps = { - pathname: getModulePath(pathname), + pathname, backups: { onMigration, - pathname: getModulePath(backupsPathname), + pathname: backupsPathname, }, }; let db: Deno.Kv; const load = async () => { try { - await Deno.stat(parsedProps.backups.pathname!); + await Deno.stat(getModulePath(backupsPathname)!); } catch (e) { - await Deno.mkdir(parsedProps.backups.pathname!); + await Deno.mkdir(getModulePath(backupsPathname)!); } await $crypto.load(); - db = await Deno.openKv(parsedProps.pathname); + db = await Deno.openKv(getModulePath(pathname)); }; const $checkDbNull = () => { - if (!db) console.error(`Db '${parsedProps.pathname}' is closed!`); + if (!db) console.error(`Db '${pathname}' is closed!`); return Boolean(db); }; @@ -176,23 +176,20 @@ export const getDb = (props: DbProps = {}): DbMutable => { }; const backup = async (name?: string) => { - const backupPathname = join( - parsedProps.backups.pathname!, - parsedProps.pathname, - ); + const backupPathname = join(backupsPathname!, pathname); const files = ["", "-shm", "-wal", DATABASE_PEPPER_FILE, DATABASE_KEY_FILE]; for (const file of files) try { await Deno.copyFile( - parsedProps.pathname + file, - parsedProps.backups.pathname + file, + getModulePath(pathname + file), + getModulePath(backupPathname + file), ); // deno-lint-ignore no-empty } catch (_) {} - const backupDb = await Deno.openKv(parsedProps.backups.pathname); + const backupDb = await Deno.openKv(getModulePath(backupPathname)); //this removes 'shm' and 'wal' files and closes correctly the db backupDb.close(); @@ -202,14 +199,14 @@ export const getDb = (props: DbProps = {}): DbMutable => { : null; await compressFiles( - files.map((file) => parsedProps.backups.pathname + file), - join(parsedProps.backups.pathname!, backupFilename + ".zip"), + files.map((file) => getModulePath(backupPathname) + file), + join(getModulePath(backupsPathname)!, backupFilename + ".zip"), filePassword, ); for (const file of files) try { - await Deno.remove(parsedProps.backups.pathname + file); + await Deno.remove(getModulePath(backupPathname) + file); // deno-lint-ignore no-empty } catch (_) {} @@ -218,7 +215,7 @@ export const getDb = (props: DbProps = {}): DbMutable => { const files = []; - for await (const entry of walk(parsedProps.backups.pathname!, { + for await (const entry of walk(getModulePath(backupsPathname)!, { includeDirs: false, })) files.push(entry); @@ -232,7 +229,7 @@ export const getDb = (props: DbProps = {}): DbMutable => { } const s3Client = getS3(s3); - await s3Client.syncPath(backupsPathname, true); + await s3Client.syncPath(getModulePath(backupsPathname), true); const s3Files = await s3Client.getFiles(); s3Files.sort((fileA, fileB) => (fileA.name > fileB.name ? -1 : 1)); @@ -251,7 +248,7 @@ export const getDb = (props: DbProps = {}): DbMutable => { const files: BackupFile[] = []; - for await (const { name, path } of walk(parsedProps.backups.pathname!, { + for await (const { name, path } of walk(getModulePath(backupsPathname)!, { includeDirs: false, })) { const { size } = await Deno.stat(path); @@ -269,7 +266,7 @@ export const getDb = (props: DbProps = {}): DbMutable => { const s3Client = getS3(s3); return await s3Client.getObject(name); } - return await Deno.readFile(join(parsedProps.backups.pathname!, name)); + return await Deno.readFile(join(getModulePath(backupsPathname)!, name)); }; const removeBackup = async (name: string): Promise => { @@ -277,7 +274,7 @@ export const getDb = (props: DbProps = {}): DbMutable => { const s3Client = getS3(s3); return await s3Client.removeFiles(name); } - return await Deno.remove(join(parsedProps.backups.pathname!, name)); + return await Deno.remove(join(getModulePath(backupsPathname)!, name)); }; const visualize = async () => { diff --git a/src/utils/path.utils.ts b/src/utils/path.utils.ts index 6548eb5..823b942 100644 --- a/src/utils/path.utils.ts +++ b/src/utils/path.utils.ts @@ -3,5 +3,5 @@ import * as path from "@std/path"; export const getExecPath = (): string => Deno.execPath(); export const getPath = (): string => path.dirname(getExecPath()); -export const getModulePath = (filePath: string): string => +export const getModulePath = (filePath: string = ""): string => path.join(path.dirname(path.fromFileUrl(Deno.mainModule)), filePath);