Skip to content

Commit 574330e

Browse files
authored
Update wasm to use the same method as mobile for feature flags (#378)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective <!-- Describe what the purpose of this PR is, for example what bug you're fixing or new feature you're adding. --> Updates wasm to use the same implementation as mobile, i.e. `load_flags`. This uses a hashmap instead of the internal `Flags` enum as we want to prevent additions or removal of flags to be a breaking change. Consumers should pass in the full flag object they get from the config endpoint and let the SDK handle the rest. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent b73c321 commit 574330e

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

crates/bitwarden-core/src/client/flags.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
/// Feature flags for the Bitwarden SDK client.
2-
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
3-
#[cfg_attr(
4-
feature = "wasm",
5-
derive(tsify::Tsify),
6-
tsify(into_wasm_abi, from_wasm_abi)
7-
)]
1+
/// Internal Feature flag representation for the Bitwarden SDK client.
2+
///
3+
/// **Note:** The struct should be deserialized directly from the `api/config` endpoint. Take care
4+
/// to ensure any appropriate aliases are used. By default we use `kebab-schema` but there may be
5+
/// value in having shorter names.
6+
///
7+
/// **Note:** This struct while public, is intended for internal use and may change in future
8+
/// releases.
9+
#[derive(Debug, Default, Clone, serde::Deserialize)]
10+
#[serde(default, rename_all = "kebab-case")]
811
pub struct Flags {
9-
/// Enable cipher key encryption within the `CipherClient`
10-
#[serde(default, rename = "enableCipherKeyEncryption")]
12+
/// Enable cipher key encryption.
13+
#[serde(alias = "enableCipherKeyEncryption", alias = "cipher-key-encryption")]
1114
pub enable_cipher_key_encryption: bool,
1215
}
1316

@@ -41,6 +44,14 @@ mod tests {
4144
assert!(flags.enable_cipher_key_encryption);
4245
}
4346

47+
#[test]
48+
fn test_load_valid_map_alias() {
49+
let mut map = std::collections::HashMap::new();
50+
map.insert("cipher-key-encryption".into(), true);
51+
let flags = Flags::load_from_map(map);
52+
assert!(flags.enable_cipher_key_encryption);
53+
}
54+
4455
#[test]
4556
fn test_load_invalid_map() {
4657
let mut map = std::collections::HashMap::new();

crates/bitwarden-core/src/client/internal.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,14 @@ pub struct InternalClient {
110110
}
111111

112112
impl InternalClient {
113-
#[allow(missing_docs)]
113+
/// Load feature flags. This is intentionally a collection and not the internal `Flag` enum as
114+
/// we want to avoid changes in feature flags from being a breaking change.
114115
#[cfg(feature = "internal")]
115116
pub fn load_flags(&self, flags: std::collections::HashMap<String, bool>) {
116117
*self.flags.write().expect("RwLock is not poisoned") = Flags::load_from_map(flags);
117118
}
118119

119-
#[allow(missing_docs)]
120-
#[cfg(feature = "internal")]
121-
pub fn set_flags(&self, flags: &Flags) {
122-
*self.flags.write().expect("RwLock is not poisoned") = flags.clone();
123-
}
124-
125-
#[allow(missing_docs)]
120+
/// Retrieve the active feature flags.
126121
#[cfg(feature = "internal")]
127122
pub fn get_flags(&self) -> Flags {
128123
self.flags.read().expect("RwLock is not poisoned").clone()

crates/bitwarden-core/src/client/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ mod flags;
1919

2020
pub use client::Client;
2121
pub use client_settings::{ClientSettings, DeviceType};
22-
#[cfg(feature = "internal")]
23-
pub use flags::Flags;
2422

2523
#[allow(missing_docs)]
2624
#[cfg(feature = "internal")]

crates/bitwarden-core/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pub mod secrets_manager;
2424
mod util;
2525

2626
pub use bitwarden_crypto::ZeroizingAllocator;
27-
#[cfg(feature = "internal")]
28-
pub use client::Flags;
2927
pub use client::{Client, ClientSettings, DeviceType};
3028

3129
mod ids;

crates/bitwarden-wasm-internal/src/platform/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
use bitwarden_core::{Client, Flags};
1+
use bitwarden_core::Client;
22
use bitwarden_vault::{Cipher, Folder};
3-
use wasm_bindgen::prelude::*;
3+
use serde::{Deserialize, Serialize};
4+
use tsify::Tsify;
5+
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
46

57
mod repository;
68
pub mod token_provider;
79

10+
/// Active feature flags for the SDK.
11+
#[derive(Serialize, Deserialize, Tsify)]
12+
#[tsify(into_wasm_abi, from_wasm_abi)]
13+
pub struct FeatureFlags {
14+
/// We intentionally use a loose type here to allow for future flags without breaking changes.
15+
#[serde(flatten)]
16+
flags: std::collections::HashMap<String, bool>,
17+
}
18+
819
#[wasm_bindgen]
920
pub struct PlatformClient(Client);
1021

@@ -21,8 +32,8 @@ impl PlatformClient {
2132
}
2233

2334
/// Load feature flags into the client
24-
pub fn load_flags(&self, flags: Flags) -> Result<(), JsValue> {
25-
self.0.internal.set_flags(&flags);
35+
pub fn load_flags(&self, flags: FeatureFlags) -> Result<(), JsValue> {
36+
self.0.internal.load_flags(flags.flags);
2637
Ok(())
2738
}
2839
}

0 commit comments

Comments
 (0)