feat: add partly returning statements and remove DecryptionResult
This commit is contained in:
parent
6bb11e60b7
commit
99774ea523
3 changed files with 39 additions and 54 deletions
2
build.sh
2
build.sh
|
@ -1,3 +1,3 @@
|
|||
#!/bin/bash
|
||||
wasm-pack build --scope duskflower --release --target bundler
|
||||
wasm-pack build --scope duskflower --release --weak-refs --target bundler
|
||||
jq '. |= . + {"publishConfig": {"registry": "https://git.duskflower.dev/api/packages/duskflower/npm/"}}' pkg/package.json > pkg/package.json.tmp && mv pkg/package.json.tmp pkg/package.json
|
||||
|
|
32
src/lib.rs
32
src/lib.rs
|
@ -39,19 +39,6 @@ pub mod signal {
|
|||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct DecryptionResult {
|
||||
database_statements: Vec<String>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl DecryptionResult {
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn database_statements(&self) -> Vec<String> {
|
||||
self.database_statements.clone()
|
||||
}
|
||||
}
|
||||
|
||||
// Add position field to ByteReader
|
||||
struct ByteReader {
|
||||
data: Vec<u8>,
|
||||
|
@ -356,6 +343,8 @@ pub struct BackupDecryptor {
|
|||
header_data: Option<HeaderData>,
|
||||
initialisation_vector: Option<Vec<u8>>,
|
||||
database_statements: Vec<String>,
|
||||
// how many statements were already passed back to JS for partial passing
|
||||
returned_database_statements_length: usize,
|
||||
ciphertext_buf: Vec<u8>,
|
||||
plaintext_buf: Vec<u8>,
|
||||
total_file_size: usize,
|
||||
|
@ -380,6 +369,7 @@ impl BackupDecryptor {
|
|||
header_data: None,
|
||||
initialisation_vector: None,
|
||||
database_statements: Vec::new(),
|
||||
returned_database_statements_length: 0,
|
||||
ciphertext_buf: Vec::new(),
|
||||
plaintext_buf: Vec::new(),
|
||||
total_file_size: 0,
|
||||
|
@ -622,9 +612,17 @@ impl BackupDecryptor {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn finish(self) -> Result<DecryptionResult, JsValue> {
|
||||
Ok(DecryptionResult {
|
||||
database_statements: self.database_statements,
|
||||
})
|
||||
pub fn get_new_decrypted_statements(&mut self) -> Result<Vec<String>, JsValue> {
|
||||
let result =
|
||||
Ok(self.database_statements[self.returned_database_statements_length..].to_vec());
|
||||
|
||||
self.returned_database_statements_length = self.database_statements.len();
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn finish(self) -> Result<Vec<String>, JsValue> {
|
||||
Ok(self.database_statements.clone())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,8 @@ import { db } from "./db";
|
|||
export async function decryptBackup(
|
||||
file: File,
|
||||
passphrase: string,
|
||||
progressCallback: (progres: number) => void,
|
||||
progressCallback: (progress: number) => void,
|
||||
statementsCallback?: (statements: string[]) => void,
|
||||
) {
|
||||
const fileSize = file.size;
|
||||
console.log("Starting decryption of file size:", fileSize);
|
||||
|
@ -30,53 +31,30 @@ export async function decryptBackup(
|
|||
while (!done) {
|
||||
try {
|
||||
done = decryptor.process_chunk(passphrase);
|
||||
} catch (e) {
|
||||
console.error("Error processing chunk:", e);
|
||||
throw e;
|
||||
} catch (error) {
|
||||
console.error("Error processing chunk:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
statementsCallback?.(decryptor.get_new_decrypted_statements());
|
||||
|
||||
offset += chunkSize;
|
||||
// console.log(`Completed chunk, new offset: ${offset}`);
|
||||
// if (performance.memory) {
|
||||
// const memoryInfo = performance.memory;
|
||||
// console.log(`Total JS Heap Size: ${memoryInfo.totalJSHeapSize} bytes`);
|
||||
// console.log(`Used JS Heap Size: ${memoryInfo.usedJSHeapSize} bytes`);
|
||||
// console.log(`JS Heap Size Limit: ${memoryInfo.jsHeapSizeLimit} bytes`);
|
||||
// } else {
|
||||
// console.log("Memory information is not available in this environment.");
|
||||
// }
|
||||
}
|
||||
|
||||
// console.log("All chunks processed, finishing up");
|
||||
const result = decryptor.finish();
|
||||
|
||||
// decryptor.free();
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
console.error("Decryption failed:", e);
|
||||
throw e;
|
||||
} catch (error) {
|
||||
console.error("Decryption failed:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function decrypt(file: File, passphrase: string) {
|
||||
try {
|
||||
const result = await decryptBackup(file, passphrase, console.info);
|
||||
|
||||
// console.log(result, result.database_bytes);
|
||||
|
||||
// console.log("Database bytes length:", result.databaseBytes.length);
|
||||
// console.log(
|
||||
// "Database bytes as string (partly)",
|
||||
// new TextDecoder().decode(result.database_bytes.slice(0, 1024 * 50)),
|
||||
// );
|
||||
|
||||
// console.log(result.database_statements);
|
||||
|
||||
return result;
|
||||
// console.log("Preferences:", result.preferences);
|
||||
// console.log("Key values:", result.keyValues);
|
||||
} catch (error) {
|
||||
console.error("Decryption failed:", error);
|
||||
}
|
||||
|
@ -86,14 +64,19 @@ const App: Component = () => {
|
|||
const [passphrase, setPassphrase] = createSignal("");
|
||||
const [backupFile, setBackupFile] = createSignal<File>();
|
||||
|
||||
let executed = 0;
|
||||
|
||||
createEffect(
|
||||
on(
|
||||
backupFile,
|
||||
(currentBackupFile) => {
|
||||
if (currentBackupFile) {
|
||||
decrypt(currentBackupFile, passphrase()).then((result) => {
|
||||
if (result) {
|
||||
for (const statement of result.database_statements) {
|
||||
decryptBackup(
|
||||
currentBackupFile,
|
||||
passphrase(),
|
||||
console.info,
|
||||
(statements) => {
|
||||
for (const statement of statements) {
|
||||
try {
|
||||
console.log("executing");
|
||||
db.exec(statement);
|
||||
|
@ -104,7 +87,11 @@ const App: Component = () => {
|
|||
}
|
||||
}
|
||||
|
||||
console.log("All statements executed");
|
||||
executed += statements.length;
|
||||
},
|
||||
).then((result) => {
|
||||
if (result) {
|
||||
console.log("All statements executed: ", result.length, executed);
|
||||
|
||||
console.log(
|
||||
db.exec("SELECT * from message", {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue