Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Remove base64 encoding from wasm boundary (#1358)
Browse files Browse the repository at this point in the history
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1358
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: crapStone <crapstone@noreply.codeberg.org>
Co-committed-by: crapStone <crapstone@noreply.codeberg.org>
  • Loading branch information
2 people authored and 6543 committed May 16, 2022
1 parent 5dd2f1d commit 42b7760
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
3 changes: 2 additions & 1 deletion frontend/src-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/src-wasm/Cargo.toml
Expand Up @@ -13,11 +13,11 @@ repository = "https://codeberg.org/Calciumdibromid/CaBr2"
crate-type = ["cdylib", "rlib"]

[dependencies]
base64 = "0.13.0"
cfg-if = "1.0.0"
chrono = { version = "0.4.19", features = ["wasmbind"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
console_log = { version = "0.2.0", features = ["color"] }
js-sys = "0.3.57"
load_save = { path = "../../crates/load_save/", default-features = false }
log = "0.4.17"
search = { path = "../../crates/search/", default-features = false }
Expand All @@ -28,7 +28,7 @@ wasm-bindgen = { version = "=0.2.80", features = ["serde-serialize"] } # TODO(#1
wasm-bindgen-futures = "0.4.30"

[features]
default = ["load_save/beryllium", "search/gestis"]
default = ["load_save/beryllium", "load_save/cabr2", "search/gestis"]
debug_build = ["console_error_panic_hook"]

[profile.release]
Expand Down
10 changes: 4 additions & 6 deletions frontend/src-wasm/src/lib.rs
Expand Up @@ -2,8 +2,8 @@ mod impls;

use std::{env, future::Future};

use base64::encode;
use cfg_if::cfg_if;
use js_sys::Uint8Array;
use log::Level;
use serde::Serialize;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -35,20 +35,18 @@ pub async fn init() {
load_save::init(search::get_provider_mapping().await).await;
}

/// Converts a `CaBr2Document` into a `base64` encoded `string` that can be saved by the client.
/// Converts a `CaBr2Document` into a binary array that can be saved by the client.
///
/// May throw errors.
#[wasm_bindgen]
pub async fn save_document(file_type: String, document: String) -> Result<String> {
// TODO change back to original signature when it is supported by `wasm-bindgen`
// pub async fn save_document(file_type: String, document: String) -> Result<Vec<u8>> {
pub async fn save_document(file_type: String, document: String) -> Result<Uint8Array> {
let document: CaBr2Document = match serde_json::from_str(&document) {
Ok(document) => document,
Err(err) => return Err(JsValue::from(err.to_string())),
};

match load_save::save_document(file_type, document).await {
Ok(res) => Ok(encode(res)),
Ok(res) => Ok(Uint8Array::from(res.as_slice())),
Err(err) => Err(JsValue::from(err.to_string())),
}
}
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/app/@core/services/loadSave/web/loadSave.service.ts
Expand Up @@ -76,18 +76,12 @@ export class LoadSaveService implements ILoadSaveService {
});
break;

case 'cb2':
const blob = new Blob([JSON.stringify(document)], { type: 'text/plain' });
downloadFile(blob, fileType);
sub.next();
break;

default:
logger.debug('saving file with wasm:', fileType);
wasm
.save_document(fileType, JSON.stringify(document))
.then((contents: string) => {
const blob2 = new Blob([window.atob(contents)], { type: 'application/octet-stream' });
.then((contents: Uint8Array) => {
const blob2 = new Blob([contents], { type: 'application/octet-stream' });
downloadFile(blob2, fileType);
sub.next();
})
Expand Down

0 comments on commit 42b7760

Please sign in to comment.