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
|
#!/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
|
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>;
|
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
|
// Add position field to ByteReader
|
||||||
struct ByteReader {
|
struct ByteReader {
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
|
@ -356,6 +343,8 @@ pub struct BackupDecryptor {
|
||||||
header_data: Option<HeaderData>,
|
header_data: Option<HeaderData>,
|
||||||
initialisation_vector: Option<Vec<u8>>,
|
initialisation_vector: Option<Vec<u8>>,
|
||||||
database_statements: Vec<String>,
|
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>,
|
ciphertext_buf: Vec<u8>,
|
||||||
plaintext_buf: Vec<u8>,
|
plaintext_buf: Vec<u8>,
|
||||||
total_file_size: usize,
|
total_file_size: usize,
|
||||||
|
@ -380,6 +369,7 @@ impl BackupDecryptor {
|
||||||
header_data: None,
|
header_data: None,
|
||||||
initialisation_vector: None,
|
initialisation_vector: None,
|
||||||
database_statements: Vec::new(),
|
database_statements: Vec::new(),
|
||||||
|
returned_database_statements_length: 0,
|
||||||
ciphertext_buf: Vec::new(),
|
ciphertext_buf: Vec::new(),
|
||||||
plaintext_buf: Vec::new(),
|
plaintext_buf: Vec::new(),
|
||||||
total_file_size: 0,
|
total_file_size: 0,
|
||||||
|
@ -622,9 +612,17 @@ impl BackupDecryptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn finish(self) -> Result<DecryptionResult, JsValue> {
|
pub fn get_new_decrypted_statements(&mut self) -> Result<Vec<String>, JsValue> {
|
||||||
Ok(DecryptionResult {
|
let result =
|
||||||
database_statements: self.database_statements,
|
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(
|
export async function decryptBackup(
|
||||||
file: File,
|
file: File,
|
||||||
passphrase: string,
|
passphrase: string,
|
||||||
progressCallback: (progres: number) => void,
|
progressCallback: (progress: number) => void,
|
||||||
|
statementsCallback?: (statements: string[]) => void,
|
||||||
) {
|
) {
|
||||||
const fileSize = file.size;
|
const fileSize = file.size;
|
||||||
console.log("Starting decryption of file size:", fileSize);
|
console.log("Starting decryption of file size:", fileSize);
|
||||||
|
@ -30,53 +31,30 @@ export async function decryptBackup(
|
||||||
while (!done) {
|
while (!done) {
|
||||||
try {
|
try {
|
||||||
done = decryptor.process_chunk(passphrase);
|
done = decryptor.process_chunk(passphrase);
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error("Error processing chunk:", e);
|
console.error("Error processing chunk:", error);
|
||||||
throw e;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
statementsCallback?.(decryptor.get_new_decrypted_statements());
|
||||||
|
|
||||||
offset += chunkSize;
|
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();
|
const result = decryptor.finish();
|
||||||
|
|
||||||
// decryptor.free();
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error("Decryption failed:", e);
|
console.error("Decryption failed:", error);
|
||||||
throw e;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function decrypt(file: File, passphrase: string) {
|
async function decrypt(file: File, passphrase: string) {
|
||||||
try {
|
try {
|
||||||
const result = await decryptBackup(file, passphrase, console.info);
|
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;
|
return result;
|
||||||
// console.log("Preferences:", result.preferences);
|
|
||||||
// console.log("Key values:", result.keyValues);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Decryption failed:", error);
|
console.error("Decryption failed:", error);
|
||||||
}
|
}
|
||||||
|
@ -86,14 +64,19 @@ const App: Component = () => {
|
||||||
const [passphrase, setPassphrase] = createSignal("");
|
const [passphrase, setPassphrase] = createSignal("");
|
||||||
const [backupFile, setBackupFile] = createSignal<File>();
|
const [backupFile, setBackupFile] = createSignal<File>();
|
||||||
|
|
||||||
|
let executed = 0;
|
||||||
|
|
||||||
createEffect(
|
createEffect(
|
||||||
on(
|
on(
|
||||||
backupFile,
|
backupFile,
|
||||||
(currentBackupFile) => {
|
(currentBackupFile) => {
|
||||||
if (currentBackupFile) {
|
if (currentBackupFile) {
|
||||||
decrypt(currentBackupFile, passphrase()).then((result) => {
|
decryptBackup(
|
||||||
if (result) {
|
currentBackupFile,
|
||||||
for (const statement of result.database_statements) {
|
passphrase(),
|
||||||
|
console.info,
|
||||||
|
(statements) => {
|
||||||
|
for (const statement of statements) {
|
||||||
try {
|
try {
|
||||||
console.log("executing");
|
console.log("executing");
|
||||||
db.exec(statement);
|
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(
|
console.log(
|
||||||
db.exec("SELECT * from message", {
|
db.exec("SELECT * from message", {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue