commit ca1eeb5ab858b311820463586d1014584b5faf3d
parent 6b22c7078a6cf1918372777ec644b6dba4e23d09
Author: triesap <triesap@radroots.dev>
Date: Mon, 19 Jan 2026 01:32:17 +0000
app-core: add idb value helper
- add idb value alias and byte extraction helper
- wire idb value module exports
- add non-wasm test for idb value fallback
- add wasm-bindgen dependency for wasm idb utilities
Diffstat:
4 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -1167,6 +1167,7 @@ dependencies = [
"js-sys",
"serde",
"serde_json",
+ "wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml
@@ -20,6 +20,7 @@ base64 = { workspace = true }
js-sys = { workspace = true }
web-sys = { workspace = true }
wasm-bindgen-futures = { workspace = true }
+wasm-bindgen = { workspace = true }
[dev-dependencies]
futures = "0.3"
diff --git a/crates/core/src/idb/mod.rs b/crates/core/src/idb/mod.rs
@@ -1,5 +1,6 @@
pub mod config;
pub mod types;
+pub mod value;
pub use config::{
IDB_CONFIG_CIPHER_AES_GCM,
@@ -22,3 +23,4 @@ pub use config::{
RADROOTS_IDB_STORES,
};
pub use types::RadrootsClientIdbConfig;
+pub use value::{idb_value_as_bytes, RadrootsClientIdbValue};
diff --git a/crates/core/src/idb/value.rs b/crates/core/src/idb/value.rs
@@ -0,0 +1,34 @@
+#[cfg(target_arch = "wasm32")]
+pub type RadrootsClientIdbValue = wasm_bindgen::JsValue;
+#[cfg(not(target_arch = "wasm32"))]
+pub type RadrootsClientIdbValue = ();
+
+#[cfg(target_arch = "wasm32")]
+pub fn idb_value_as_bytes(value: &RadrootsClientIdbValue) -> Option<Vec<u8>> {
+ if value.is_instance_of::<js_sys::Uint8Array>()
+ || value.is_instance_of::<js_sys::ArrayBuffer>()
+ || js_sys::ArrayBuffer::is_view(value)
+ {
+ let array = js_sys::Uint8Array::new(value);
+ let mut out = vec![0u8; array.length() as usize];
+ array.copy_to(&mut out);
+ return Some(out);
+ }
+ None
+}
+
+#[cfg(not(target_arch = "wasm32"))]
+pub fn idb_value_as_bytes(_value: &RadrootsClientIdbValue) -> Option<Vec<u8>> {
+ None
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{idb_value_as_bytes, RadrootsClientIdbValue};
+
+ #[test]
+ fn non_wasm_returns_none() {
+ let value: RadrootsClientIdbValue = ();
+ assert!(idb_value_as_bytes(&value).is_none());
+ }
+}