Skip to content

Commit 970d2f6

Browse files
authored
fix: updater + db have incorrect module paths - fix #113 (#115)
1 parent e1222ec commit 970d2f6

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/modules/db/crypto.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,36 @@ import {
1010
encryptSHA256,
1111
encryptToken,
1212
} from "../../utils/encryption.utils.ts";
13+
import { getModulePath } from "../../utils/path.utils.ts";
1314

1415
export const crypto = ({ pathname }: DbProps): DbCryptoMutable => {
1516
const load = async () => {
1617
//generate pep (pepper)
1718
try {
18-
await Deno.stat(pathname + DATABASE_PEPPER_FILE);
19+
await Deno.stat(getModulePath(pathname) + DATABASE_PEPPER_FILE);
1920
} catch (e) {
20-
await Deno.writeTextFile(pathname + DATABASE_PEPPER_FILE, ulid());
21+
await Deno.writeTextFile(
22+
getModulePath(pathname) + DATABASE_PEPPER_FILE,
23+
ulid(),
24+
);
2125
}
2226

2327
//generate key (hash)
2428
try {
25-
await Deno.stat(pathname + DATABASE_KEY_FILE);
29+
await Deno.stat(getModulePath(pathname) + DATABASE_KEY_FILE);
2630
} catch (e) {
27-
await Deno.writeTextFile(pathname + DATABASE_KEY_FILE, ulid());
31+
await Deno.writeTextFile(
32+
getModulePath(pathname) + DATABASE_KEY_FILE,
33+
ulid(),
34+
);
2835
}
2936
};
3037

3138
/////////////////////////////////////////////////////////////////////
3239
///////ENCRYPT/DECRYPT///////////////////////////////////////////////
3340
/////////////////////////////////////////////////////////////////////
3441
const getSecretKey = (): Promise<string> =>
35-
Deno.readTextFile(pathname + DATABASE_KEY_FILE);
42+
Deno.readTextFile(getModulePath(pathname) + DATABASE_KEY_FILE);
3643

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

5158
const pepperPassword = async (password: string): Promise<string> =>
5259
(await getPepper()) + ":" + password;

src/modules/db/main.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,27 @@ export const getDb = (props: DbProps = {}): DbMutable => {
3838
},
3939
} = props;
4040
const parsedProps = {
41-
pathname: getModulePath(pathname),
41+
pathname,
4242
backups: {
4343
onMigration,
44-
pathname: getModulePath(backupsPathname),
44+
pathname: backupsPathname,
4545
},
4646
};
4747
let db: Deno.Kv;
4848

4949
const load = async () => {
5050
try {
51-
await Deno.stat(parsedProps.backups.pathname!);
51+
await Deno.stat(getModulePath(backupsPathname)!);
5252
} catch (e) {
53-
await Deno.mkdir(parsedProps.backups.pathname!);
53+
await Deno.mkdir(getModulePath(backupsPathname)!);
5454
}
5555

5656
await $crypto.load();
57-
db = await Deno.openKv(parsedProps.pathname);
57+
db = await Deno.openKv(getModulePath(pathname));
5858
};
5959

6060
const $checkDbNull = () => {
61-
if (!db) console.error(`Db '${parsedProps.pathname}' is closed!`);
61+
if (!db) console.error(`Db '${pathname}' is closed!`);
6262

6363
return Boolean(db);
6464
};
@@ -176,23 +176,20 @@ export const getDb = (props: DbProps = {}): DbMutable => {
176176
};
177177

178178
const backup = async (name?: string) => {
179-
const backupPathname = join(
180-
parsedProps.backups.pathname!,
181-
parsedProps.pathname,
182-
);
179+
const backupPathname = join(backupsPathname!, pathname);
183180

184181
const files = ["", "-shm", "-wal", DATABASE_PEPPER_FILE, DATABASE_KEY_FILE];
185182

186183
for (const file of files)
187184
try {
188185
await Deno.copyFile(
189-
parsedProps.pathname + file,
190-
parsedProps.backups.pathname + file,
186+
getModulePath(pathname + file),
187+
getModulePath(backupPathname + file),
191188
);
192189
// deno-lint-ignore no-empty
193190
} catch (_) {}
194191

195-
const backupDb = await Deno.openKv(parsedProps.backups.pathname);
192+
const backupDb = await Deno.openKv(getModulePath(backupPathname));
196193
//this removes 'shm' and 'wal' files and closes correctly the db
197194
backupDb.close();
198195

@@ -202,14 +199,14 @@ export const getDb = (props: DbProps = {}): DbMutable => {
202199
: null;
203200

204201
await compressFiles(
205-
files.map((file) => parsedProps.backups.pathname + file),
206-
join(parsedProps.backups.pathname!, backupFilename + ".zip"),
202+
files.map((file) => getModulePath(backupPathname) + file),
203+
join(getModulePath(backupsPathname)!, backupFilename + ".zip"),
207204
filePassword,
208205
);
209206

210207
for (const file of files)
211208
try {
212-
await Deno.remove(parsedProps.backups.pathname + file);
209+
await Deno.remove(getModulePath(backupPathname) + file);
213210
// deno-lint-ignore no-empty
214211
} catch (_) {}
215212

@@ -218,7 +215,7 @@ export const getDb = (props: DbProps = {}): DbMutable => {
218215

219216
const files = [];
220217

221-
for await (const entry of walk(parsedProps.backups.pathname!, {
218+
for await (const entry of walk(getModulePath(backupsPathname)!, {
222219
includeDirs: false,
223220
}))
224221
files.push(entry);
@@ -232,7 +229,7 @@ export const getDb = (props: DbProps = {}): DbMutable => {
232229
}
233230

234231
const s3Client = getS3(s3);
235-
await s3Client.syncPath(backupsPathname, true);
232+
await s3Client.syncPath(getModulePath(backupsPathname), true);
236233

237234
const s3Files = await s3Client.getFiles();
238235
s3Files.sort((fileA, fileB) => (fileA.name > fileB.name ? -1 : 1));
@@ -251,7 +248,7 @@ export const getDb = (props: DbProps = {}): DbMutable => {
251248

252249
const files: BackupFile[] = [];
253250

254-
for await (const { name, path } of walk(parsedProps.backups.pathname!, {
251+
for await (const { name, path } of walk(getModulePath(backupsPathname)!, {
255252
includeDirs: false,
256253
})) {
257254
const { size } = await Deno.stat(path);
@@ -269,15 +266,15 @@ export const getDb = (props: DbProps = {}): DbMutable => {
269266
const s3Client = getS3(s3);
270267
return await s3Client.getObject(name);
271268
}
272-
return await Deno.readFile(join(parsedProps.backups.pathname!, name));
269+
return await Deno.readFile(join(getModulePath(backupsPathname)!, name));
273270
};
274271

275272
const removeBackup = async (name: string): Promise<void> => {
276273
if (s3) {
277274
const s3Client = getS3(s3);
278275
return await s3Client.removeFiles(name);
279276
}
280-
return await Deno.remove(join(parsedProps.backups.pathname!, name));
277+
return await Deno.remove(join(getModulePath(backupsPathname)!, name));
281278
};
282279

283280
const visualize = async () => {

src/utils/path.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import * as path from "@std/path";
33
export const getExecPath = (): string => Deno.execPath();
44
export const getPath = (): string => path.dirname(getExecPath());
55

6-
export const getModulePath = (filePath: string): string =>
6+
export const getModulePath = (filePath: string = ""): string =>
77
path.join(path.dirname(path.fromFileUrl(Deno.mainModule)), filePath);

0 commit comments

Comments
 (0)