From e52d06ef9cca91535bb1c9242cd4ff5bab2fa554 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Mon, 17 May 2021 21:26:07 -0400 Subject: [PATCH] accept cache path as option to encrypt()/decrypt() --- crypt.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crypt.js b/crypt.js index 5f02663..01227f2 100644 --- a/crypt.js +++ b/crypt.js @@ -2,10 +2,8 @@ import * as crypto from 'crypto'; import * as fs from 'fs'; import { cpus } from 'os'; import { dirname } from 'path'; -import { fileURLToPath } from 'url'; import { deserialize, serialize } from 'v8'; -const cache_path = dirname(fileURLToPath(import.meta.url)) + '/cache'; const num_processors = cpus().length; export function init({ @@ -67,7 +65,7 @@ function get_info(crypt_dir, passphrase) { return info; } -async function get_plain_index(plain_dir, hash_algorithm, filter) { +async function get_plain_index(plain_dir, hash_algorithm, filter, cache_path) { let cache; try { cache = deserialize(fs.readFileSync(cache_path)); @@ -157,7 +155,7 @@ function make_stream_queue() { }; } -export async function encrypt({ plain: plain_dir, crypt: crypt_dir, filter }) { +export async function encrypt({ plain: plain_dir, crypt: crypt_dir, cache: cache_path, filter }) { const added = new Set(); const deleted = new Set(); const updated = new Set(); @@ -165,7 +163,7 @@ export async function encrypt({ plain: plain_dir, crypt: crypt_dir, filter }) { const info = get_info(crypt_dir); const { keyLength, ivLength } = crypto.getCipherInfo(info.cipher_algorithm); // CONSTRUCT PLAIN INDEX - const plain_index = await get_plain_index(plain_dir, info.hash_algorithm, filter); + const plain_index = await get_plain_index(plain_dir, info.hash_algorithm, filter, cache_path); // CREATE INDEX OF PLAIN FILES AS THEY WILL APPEAR IN THE CRYPT INDEX const path_hmac_lookup = new Map(); for (const path of plain_index.keys()) { @@ -263,7 +261,7 @@ export function clean({ crypt: crypt_dir, passphrase }) { return { deleted: crypt_filenames }; } -export async function decrypt({ plain: plain_dir, crypt: crypt_dir, filter, passphrase }) { +export async function decrypt({ crypt: crypt_dir, plain: plain_dir, cache: cache_path, filter, passphrase }) { const added = new Set(); const deleted = new Set(); const updated = new Set(); @@ -271,7 +269,7 @@ export async function decrypt({ plain: plain_dir, crypt: crypt_dir, filter, pass // READ CRYPT INDEX const info = get_info(crypt_dir, passphrase); // CONSTRUCT PLAIN INDEX - const plain_index = await get_plain_index(plain_dir, info.hash_algorithm, filter); + const plain_index = await get_plain_index(plain_dir, info.hash_algorithm, filter, cache_path); // CREATE INDEX OF PLAIN FILES AS THEY WILL APPEAR IN THE CRYPT INDEX // DELETE MISSING FILES const path_hmac_lookup = new Map();