diff --git a/backend/sync_lib/Cargo.toml b/backend/sync_lib/Cargo.toml index 1e1b048..7dc07b6 100644 --- a/backend/sync_lib/Cargo.toml +++ b/backend/sync_lib/Cargo.toml @@ -1,9 +1,27 @@ [package] name = "sync_lib" version = "0.1.0" -edition = "2024" +authors = ["Andras Schmelczer "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] [dependencies] base64 = "0.22.1" - +reconcile = { path = "../reconcile" } +wasm-bindgen = "0.2.84" +getrandom = { version = "0.2.3", features = ["js"] } thiserror = {workspace = true} + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.7", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[features] +default = ["console_error_panic_hook"] diff --git a/backend/sync_lib/README.md b/backend/sync_lib/README.md new file mode 100644 index 0000000..6b68408 --- /dev/null +++ b/backend/sync_lib/README.md @@ -0,0 +1,84 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. diff --git a/backend/sync_lib/src/errors.rs b/backend/sync_lib/src/errors.rs index acc865e..d8809ee 100644 --- a/backend/sync_lib/src/errors.rs +++ b/backend/sync_lib/src/errors.rs @@ -1,5 +1,6 @@ use base64::DecodeError; use thiserror::Error; +use wasm_bindgen::JsValue; #[derive(Error, Debug)] pub enum SyncLibError { @@ -22,3 +23,7 @@ impl From for SyncLibError { } } } + +impl From for JsValue { + fn from(val: SyncLibError) -> Self { JsValue::from_str(&val.to_string()) } +} diff --git a/backend/sync_lib/src/lib.rs b/backend/sync_lib/src/lib.rs index 40e0521..7a913c8 100644 --- a/backend/sync_lib/src/lib.rs +++ b/backend/sync_lib/src/lib.rs @@ -1,19 +1,42 @@ -use base64::{Engine as _, engine::general_purpose::STANDARD_NO_PAD}; +use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; use errors::SyncLibError; +use wasm_bindgen::prelude::*; pub mod errors; +// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global +// allocator. +#[cfg(feature = "wee_alloc")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[wasm_bindgen] pub fn bytes_to_base64(input: &[u8]) -> String { STANDARD_NO_PAD.encode(input) } +#[wasm_bindgen] pub fn string_to_base64(input: &str) -> String { bytes_to_base64(input.as_bytes()) } +#[wasm_bindgen] pub fn base64_to_bytes(input: &str) -> Result, SyncLibError> { STANDARD_NO_PAD.decode(input).map_err(SyncLibError::from) } +#[wasm_bindgen] pub fn base64_to_string(input: &str) -> Result { let bytes = base64_to_bytes(input)?; String::from_utf8(bytes).map_err(SyncLibError::from) } +#[wasm_bindgen] pub fn is_binary(data: &[u8]) -> bool { data.iter().any(|&b| b == 0) } + +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/backend/sync_wasm/tests/web.rs b/backend/sync_lib/tests/web.rs similarity index 100% rename from backend/sync_wasm/tests/web.rs rename to backend/sync_lib/tests/web.rs diff --git a/backend/sync_wasm/Cargo.toml b/backend/sync_wasm/Cargo.toml deleted file mode 100644 index 0eec017..0000000 --- a/backend/sync_wasm/Cargo.toml +++ /dev/null @@ -1,27 +0,0 @@ -[package] -name = "sync-wasm" -version = "0.1.0" -authors = ["Andras Schmelczer "] -edition = "2018" - -[lib] -crate-type = ["cdylib", "rlib"] - -[features] -default = ["console_error_panic_hook"] - -[dependencies] -sync_lib = { path = "../sync_lib" } -reconcile = { path = "../reconcile" } -wasm-bindgen = "0.2.84" -getrandom = { version = "0.2.3", features = ["js"] } - - -# The `console_error_panic_hook` crate provides better debugging of panics by -# logging them with `console.error`. This is great for development, but requires -# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for -# code size when deploying. -console_error_panic_hook = { version = "0.1.7", optional = true } - -[dev-dependencies] -wasm-bindgen-test = "0.3.34" diff --git a/backend/sync_wasm/src/lib.rs b/backend/sync_wasm/src/lib.rs deleted file mode 100644 index c010e4f..0000000 --- a/backend/sync_wasm/src/lib.rs +++ /dev/null @@ -1,17 +0,0 @@ -mod utils; - -use wasm_bindgen::prelude::*; - -// // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global -// // allocator. -// #[cfg(feature = "wee_alloc")] -// #[global_allocator] -// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -// #[wasm_bindgen] -// extern "C" { -// fn alert(s: &str); -// } - -#[wasm_bindgen] -pub fn greet() -> String { "hi".to_string() } diff --git a/backend/sync_wasm/src/utils.rs b/backend/sync_wasm/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/backend/sync_wasm/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub fn set_panic_hook() { - // When the `console_error_panic_hook` feature is enabled, we can call the - // `set_panic_hook` function at least once during initialization, and then - // we will get better error messages if our code ever panics. - // - // For more details see - // https://github.com/rustwasm/console_error_panic_hook#readme - #[cfg(feature = "console_error_panic_hook")] - console_error_panic_hook::set_once(); -} diff --git a/plugin/esbuild.config.mjs b/plugin/esbuild.config.mjs index e961409..bafba48 100644 --- a/plugin/esbuild.config.mjs +++ b/plugin/esbuild.config.mjs @@ -108,7 +108,7 @@ const context = await esbuild.context({ wasmPlugin, wasmPack({ target: "web", - path: "../backend/sync_wasm", + path: "../backend/sync_lib", }), { name: "post-compile",