From d9e470e6ffc1b69ab553867921efc4dc9f1e6314 Mon Sep 17 00:00:00 2001 From: Hinton Date: Mon, 2 Sep 2024 14:44:47 +0200 Subject: [PATCH] Resolve some review feedback --- Cargo.lock | 1 - crates/bitwarden-db-tests/src/lib.rs | 16 ++++++++++++++++ crates/bitwarden-db/src/wasm/mod.rs | 2 +- crates/bitwarden-json/Cargo.toml | 1 - crates/bitwarden-napi/binding.d.ts | 5 ++++- .../src-ts/bitwarden_client/index.ts | 12 +++++++++--- crates/bitwarden-napi/src/client.rs | 11 +++-------- crates/bitwarden-uniffi/src/lib.rs | 2 +- crates/bitwarden-wasm/src/client.rs | 2 +- 9 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e71937c9..d82364468 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,6 @@ dependencies = [ "schemars", "serde", "serde_json", - "uuid", ] [[package]] diff --git a/crates/bitwarden-db-tests/src/lib.rs b/crates/bitwarden-db-tests/src/lib.rs index 8b710d53c..b7d2772dd 100644 --- a/crates/bitwarden-db-tests/src/lib.rs +++ b/crates/bitwarden-db-tests/src/lib.rs @@ -9,8 +9,21 @@ extern "C" { } */ +// Ensure resources are cleaned up after tests run +#[cfg(not(target_arch = "wasm32"))] +struct TestResources; +#[cfg(not(target_arch = "wasm32"))] +impl Drop for TestResources { + fn drop(&mut self) { + let _ = std::fs::remove_file("test.sqlite"); + } +} + #[wasm_bindgen(js_name = runTests)] pub async fn run_tests() { + #[cfg(not(target_arch = "wasm32"))] + let cleanup = TestResources; + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); let db = Database::default().await.unwrap(); @@ -18,6 +31,9 @@ pub async fn run_tests() { test_create_select(&db).await; print!("Ran tests"); + + #[cfg(not(target_arch = "wasm32"))] + core::hint::black_box(cleanup); } pub async fn test_version(db: &Database) { diff --git a/crates/bitwarden-db/src/wasm/mod.rs b/crates/bitwarden-db/src/wasm/mod.rs index 265b2787d..fd0f55a55 100644 --- a/crates/bitwarden-db/src/wasm/mod.rs +++ b/crates/bitwarden-db/src/wasm/mod.rs @@ -20,7 +20,7 @@ extern "C" { type SqliteDatabase; #[wasm_bindgen(static_method_of = SqliteDatabase)] - async fn factory(name: &str) -> JsValue; + async fn create(name: &str) -> JsValue; #[wasm_bindgen(method)] async fn get_version(this: &SqliteDatabase) -> JsValue; diff --git a/crates/bitwarden-json/Cargo.toml b/crates/bitwarden-json/Cargo.toml index 6e78e1940..1c044091b 100644 --- a/crates/bitwarden-json/Cargo.toml +++ b/crates/bitwarden-json/Cargo.toml @@ -25,7 +25,6 @@ log = ">=0.4.18, <0.5" schemars = ">=0.8.12, <0.9" serde = { version = ">=1.0, <2.0", features = ["derive"] } serde_json = ">=1.0.96, <2.0" -uuid = { version = ">=1.3.3, <2", features = ["serde", "v4"] } [lints] diff --git a/crates/bitwarden-napi/binding.d.ts b/crates/bitwarden-napi/binding.d.ts index 174d33d92..3fd9451e5 100644 --- a/crates/bitwarden-napi/binding.d.ts +++ b/crates/bitwarden-napi/binding.d.ts @@ -11,6 +11,9 @@ export const enum LogLevel { Error = 4, } export declare class BitwardenClient { - constructor(settingsInput?: string | undefined | null, logLevel?: LogLevel | undefined | null); + static create( + settingsInput?: string | undefined | null, + logLevel?: LogLevel | undefined | null, + ): Promise; runCommand(commandInput: string): Promise; } diff --git a/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts b/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts index 52a53ef4f..927b2e9ba 100644 --- a/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts +++ b/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts @@ -30,11 +30,17 @@ function handleResponse(response: { } export class BitwardenClient { - client: rust.BitwardenClient; + private client: rust.BitwardenClient; - constructor(settings?: ClientSettings, loggingLevel?: LogLevel) { + static async create(settings?: ClientSettings, loggingLevel?: LogLevel) { const settingsJson = settings == null ? null : Convert.clientSettingsToJson(settings); - this.client = new rust.BitwardenClient(settingsJson, loggingLevel ?? LogLevel.Info); + new BitwardenClient( + await rust.BitwardenClient.create(settingsJson, loggingLevel ?? LogLevel.Info), + ); + } + + private constructor(client: rust.BitwardenClient) { + this.client = client; } async accessTokenLogin(accessToken: string, stateFile?: string): Promise { diff --git a/crates/bitwarden-napi/src/client.rs b/crates/bitwarden-napi/src/client.rs index ccae8a35b..04321baae 100644 --- a/crates/bitwarden-napi/src/client.rs +++ b/crates/bitwarden-napi/src/client.rs @@ -27,14 +27,14 @@ pub struct BitwardenClient(JsonClient); #[napi] impl BitwardenClient { - #[napi(constructor)] - pub fn new(settings_input: Option, log_level: Option) -> Self { + #[napi(factory)] + pub async fn create(settings_input: Option, log_level: Option) -> Self { // This will only fail if another logger was already initialized, so we can ignore the // result let _ = env_logger::Builder::from_default_env() .filter_level(convert_level(log_level.unwrap_or(LogLevel::Info))) .try_init(); - Self(new(settings_input)) + Self(bitwarden_json::client::Client::new(settings_input).await) } #[napi] @@ -42,8 +42,3 @@ impl BitwardenClient { self.0.run_command(&command_input).await } } - -#[tokio::main] -async fn new(settings_string: Option) -> JsonClient { - JsonClient::new(settings_string).await -} diff --git a/crates/bitwarden-uniffi/src/lib.rs b/crates/bitwarden-uniffi/src/lib.rs index 298fd2113..2b116cf0c 100644 --- a/crates/bitwarden-uniffi/src/lib.rs +++ b/crates/bitwarden-uniffi/src/lib.rs @@ -32,7 +32,7 @@ pub struct Client(bitwarden::Client); impl Client { /// Initialize a new instance of the SDK client #[uniffi::constructor] - pub async fn factory(settings: Option) -> Arc { + pub async fn create(settings: Option) -> Arc { init_logger(); #[cfg(target_os = "android")] diff --git a/crates/bitwarden-wasm/src/client.rs b/crates/bitwarden-wasm/src/client.rs index 4548f7e46..15092844b 100644 --- a/crates/bitwarden-wasm/src/client.rs +++ b/crates/bitwarden-wasm/src/client.rs @@ -35,7 +35,7 @@ pub struct BitwardenClient(Rc); #[wasm_bindgen] impl BitwardenClient { #[wasm_bindgen] - pub async fn factory(settings_input: Option, log_level: Option) -> Self { + pub async fn create(settings_input: Option, log_level: Option) -> Self { console_error_panic_hook::set_once(); if let Err(e) = console_log::init_with_level(convert_level(log_level.unwrap_or(LogLevel::Info)))