Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/modules/db/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,36 @@ 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(),
);
}
};

/////////////////////////////////////////////////////////////////////
///////ENCRYPT/DECRYPT///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
const getSecretKey = (): Promise<string> =>
Deno.readTextFile(pathname + DATABASE_KEY_FILE);
Deno.readTextFile(getModulePath(pathname) + DATABASE_KEY_FILE);

const $encryptSHA256 = async (text: string): Promise<string> => {
return await encryptSHA256(text, await getSecretKey());
Expand All @@ -46,7 +53,7 @@ export const crypto = ({ pathname }: DbProps): DbCryptoMutable => {
///////BCRYPT/WITH/PEPPER////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
const getPepper = (): Promise<string> =>
Deno.readTextFile(pathname + DATABASE_PEPPER_FILE);
Deno.readTextFile(getModulePath(pathname) + DATABASE_PEPPER_FILE);

const pepperPassword = async (password: string): Promise<string> =>
(await getPepper()) + ":" + password;
Expand Down
39 changes: 18 additions & 21 deletions src/modules/db/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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();

Expand All @@ -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 (_) {}

Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -269,15 +266,15 @@ 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<void> => {
if (s3) {
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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/path.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);