Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-11794] Use wasm-bindgen directly #1077

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bitwarden-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.28.1", optional = true, features = ["tokio"] }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
validator = { version = "0.18.1", features = ["derive"] }
wasm-bindgen = "0.2.93"
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }
zxcvbn = { version = ">=3.0.1, <4.0", optional = true }

Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-core/src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, RwLock};
use wasm_bindgen::prelude::*;

use reqwest::header::{self, HeaderValue};

Expand All @@ -12,8 +13,10 @@

/// The main struct to interact with the Bitwarden SDK.
#[derive(Debug)]
#[wasm_bindgen]

Check warning on line 16 in crates/bitwarden-core/src/client/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/client/client.rs#L16

Added line #L16 was not covered by tests
pub struct Client {
#[doc(hidden)]
#[wasm_bindgen(skip)]
pub internal: InternalClient,
}

Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ uniffi = [
"bitwarden-crypto/uniffi",
"dep:uniffi",
] # Uniffi bindings
wasm = [] # Typescript and WASM bindings

[dependencies]
base64 = ">=0.22.1, <0.23"
Expand All @@ -42,6 +43,11 @@ thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.28.1", optional = true }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }

# [target.'cfg(feature = "wasm")'.dependencies]
serde-wasm-bindgen = "0.6.5"
tsify-next = { version = "0.5.4", features = ["js"], default-features = false }
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] }

[dev-dependencies]
tokio = { version = "1.36.0", features = ["rt", "macros"] }

Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-vault/src/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
// #[cfg(feature = "wasm")]
use tsify_next::Tsify;
// #[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::CipherListView;

Expand Down Expand Up @@ -38,6 +42,7 @@
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]

Check warning on line 45 in crates/bitwarden-vault/src/totp.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/totp.rs#L45

Added line #L45 was not covered by tests
pub struct TotpResponse {
/// Generated TOTP code
pub code: String,
Expand Down
7 changes: 7 additions & 0 deletions crates/bitwarden-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ argon2 = { version = ">=0.5.0, <0.6", features = [
"alloc",
"zeroize",
], default-features = false }
bitwarden = { workspace = true }
bitwarden-vault = { workspace = true, features = ["wasm"] }
bitwarden-json = { path = "../bitwarden-json", features = [
"secrets",
"internal",
] }
chrono = { version = ">=0.4.26, <0.5", default-features = true, features = [
"serde",
] }
console_error_panic_hook = "0.1.7"
console_log = { version = "1.0.0", features = ["color"] }
js-sys = "0.3.68"
log = "0.4.20"
serde = { version = "1.0.196", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
tsify-next = { version = "0.5.4", features = ["js"], default-features = false }
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.41"

Expand Down
8 changes: 4 additions & 4 deletions crates/bitwarden-wasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ else
fi

# Optimize size
wasm-opt -Os ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm
wasm-opt -Os ./languages/js/wasm/node/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/node/bitwarden_wasm_bg.wasm
# wasm-opt -Os ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm
# wasm-opt -Os ./languages/js/wasm/node/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/node/bitwarden_wasm_bg.wasm

# Transpile to JS
wasm2js ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
npx terser ./languages/js/wasm/bitwarden_wasm_bg.wasm.js -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
# wasm2js ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
# npx terser ./languages/js/wasm/bitwarden_wasm_bg.wasm.js -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
19 changes: 1 addition & 18 deletions crates/bitwarden-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,7 @@ use log::{set_max_level, Level};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;

#[wasm_bindgen]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

fn convert_level(level: LogLevel) -> Level {
match level {
LogLevel::Trace => Level::Trace,
LogLevel::Debug => Level::Debug,
LogLevel::Info => Level::Info,
LogLevel::Warn => Level::Warn,
LogLevel::Error => Level::Error,
}
}
use crate::log_level::{convert_level, LogLevel};

// Rc<...> is to avoid needing to take ownership of the Client during our async run_command
// function https://github.com/rustwasm/wasm-bindgen/issues/2195#issuecomment-799588401
Expand Down
38 changes: 38 additions & 0 deletions crates/bitwarden-wasm/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::fmt::{Display, Formatter};

// Name is converted from *Error to *Exception, so we can't just name the enum Error because
// Exception already exists
#[derive(Debug)]
pub enum BitwardenError {
E(bitwarden::error::Error),
}

impl From<bitwarden::Error> for BitwardenError {
fn from(e: bitwarden::Error) -> Self {
Self::E(e.into())
}

Check warning on line 13 in crates/bitwarden-wasm/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/error.rs#L11-L13

Added lines #L11 - L13 were not covered by tests
}

impl From<bitwarden::error::Error> for BitwardenError {
fn from(e: bitwarden::error::Error) -> Self {
Self::E(e)
}

Check warning on line 19 in crates/bitwarden-wasm/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/error.rs#L17-L19

Added lines #L17 - L19 were not covered by tests
}

impl Display for BitwardenError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::E(e) => Display::fmt(e, f),
}
}

Check warning on line 27 in crates/bitwarden-wasm/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/error.rs#L23-L27

Added lines #L23 - L27 were not covered by tests
}

impl std::error::Error for BitwardenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
BitwardenError::E(e) => Some(e),
}
}

Check warning on line 35 in crates/bitwarden-wasm/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/error.rs#L31-L35

Added lines #L31 - L35 were not covered by tests
}

pub type Result<T, E = BitwardenError> = std::result::Result<T, E>;
5 changes: 5 additions & 0 deletions crates/bitwarden-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
mod client;
mod log_level;
mod raw_client;

mod error;
mod vault;
21 changes: 21 additions & 0 deletions crates/bitwarden-wasm/src/log_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use log::Level;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]

Check warning on line 4 in crates/bitwarden-wasm/src/log_level.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/log_level.rs#L4

Added line #L4 was not covered by tests
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

pub fn convert_level(level: LogLevel) -> Level {
match level {
LogLevel::Trace => Level::Trace,
LogLevel::Debug => Level::Debug,
LogLevel::Info => Level::Info,
LogLevel::Warn => Level::Warn,
LogLevel::Error => Level::Error,

Check warning on line 19 in crates/bitwarden-wasm/src/log_level.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/log_level.rs#L13-L19

Added lines #L13 - L19 were not covered by tests
}
}

Check warning on line 21 in crates/bitwarden-wasm/src/log_level.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/log_level.rs#L21

Added line #L21 was not covered by tests
38 changes: 38 additions & 0 deletions crates/bitwarden-wasm/src/raw_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::sync::Arc;
use wasm_bindgen::prelude::*;

use crate::{
log_level::{convert_level, LogLevel},
vault::ClientVault,
};

// Rc<...> is to avoid needing to take ownership of the Client during our async run_command
// function https://github.com/rustwasm/wasm-bindgen/issues/2195#issuecomment-799588401
#[wasm_bindgen]

Check warning on line 11 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L11

Added line #L11 was not covered by tests
pub struct RawBitwardenClient(Arc<bitwarden::Client>);

#[wasm_bindgen]

Check warning on line 14 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L14

Added line #L14 was not covered by tests
impl RawBitwardenClient {
#[wasm_bindgen(constructor)]
pub fn new(_settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {
// let settings = Self::parse_settings(settings_input);
console_error_panic_hook::set_once();
if let Err(e) =
console_log::init_with_level(convert_level(log_level.unwrap_or(LogLevel::Info)))

Check warning on line 21 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L17-L21

Added lines #L17 - L21 were not covered by tests
{
panic!("failed to initialize logger: {:?}", e);
}
Self(Arc::new(bitwarden::Client::new(None)))
}

Check warning on line 26 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L23-L26

Added lines #L23 - L26 were not covered by tests

#[wasm_bindgen]
pub fn vault(&self) -> ClientVault {
ClientVault(self.0.clone())
}

Check warning on line 31 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L29-L31

Added lines #L29 - L31 were not covered by tests

#[wasm_bindgen]
pub fn hello(&self) -> String {
println!("Hello from Rust!");
"Hello from Rust!".to_string()
}

Check warning on line 37 in crates/bitwarden-wasm/src/raw_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/raw_client.rs#L34-L37

Added lines #L34 - L37 were not covered by tests
}
Loading
Loading