use sqlite for wasm

This commit is contained in:
2026-10-25 20:09:46 +07:00
parent 29aa7f05aa
commit ded0e27a56
8 changed files with 420 additions and 25 deletions
+4
View File
@@ -20,6 +20,7 @@ futures.workspace = true
anyhow.workspace = true
webbrowser.workspace = true
serde_json.workspace = true
log.workspace = true
mime_guess = "2.0.4"
@@ -31,6 +32,7 @@ data-encoding.workspace = true
js-sys = "0.3"
nostr-browser-signer.workspace = true
nostr-memory.workspace = true
nostr-sqlite.workspace = true
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
@@ -43,6 +45,8 @@ web-sys = { version = "0.3", features = [
"Url",
"Window",
] }
sqlite-wasm-rs = "0.5"
sqlite-wasm-vfs = "0.2"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
browser-signer-proxy = { path = "../browser-signer-proxy" }
+3 -4
View File
@@ -16,8 +16,6 @@ use nostr_connect::prelude::*;
use nostr_gossip_memory::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use nostr_lmdb::prelude::*;
#[cfg(target_arch = "wasm32")]
use nostr_memory::prelude::*;
use nostr_sdk::prelude::*;
mod blossom;
@@ -28,6 +26,8 @@ mod nip05;
mod nip4e;
mod signer;
#[cfg(target_arch = "wasm32")]
pub mod web_database;
#[cfg(target_arch = "wasm32")]
pub mod web_file;
@@ -114,7 +114,6 @@ impl NostrRegistry {
let signer = UniversalSigner::new(Keys::generate());
let authenticator = SignerAuthenticator::new(signer.clone());
// Construct the nostr lmdb instance
#[cfg(not(target_arch = "wasm32"))]
let database = cx.foreground_executor().block_on(async move {
NostrLmdb::open(config_dir().join("nostr"))
@@ -123,7 +122,7 @@ impl NostrRegistry {
});
#[cfg(target_arch = "wasm32")]
let database = MemoryDatabase::unbounded();
let database = web_database::take();
// Construct the nostr client
let client = ClientBuilder::default()
+62
View File
@@ -0,0 +1,62 @@
use std::cell::RefCell;
use std::sync::Arc;
use nostr_memory::prelude::*;
use nostr_sqlite::prelude::NostrSqlite;
use sqlite_wasm_rs::WasmOsCallback;
use sqlite_wasm_vfs::relaxed_idb;
const DATABASE_PATH: &str = "coop.sqlite3";
const VFS_NAME: &str = "coop-relaxed-idb";
thread_local! {
static DATABASE: RefCell<Option<NostrSqlite>> = const { RefCell::new(None) };
}
/// Install the IndexedDB storage backend and open the database.
pub async fn open() {
match open_persistent().await {
Some(database) => DATABASE.with(|slot| *slot.borrow_mut() = Some(database)),
None => log::error!("Falling back to the in-memory nostr database"),
}
}
/// Hand the opened database to the client constructor
pub(crate) fn take() -> Arc<dyn NostrDatabase> {
let database = DATABASE.with(|slot| slot.borrow_mut().take());
match database {
Some(database) => Arc::new(database),
None => {
log::error!("Persistent web database was not opened, falling back to memory");
Arc::new(MemoryDatabase::unbounded())
}
}
}
/// Open the database on IndexedDB storage, or `None` when unavailable
async fn open_persistent() -> Option<NostrSqlite> {
let options = relaxed_idb::RelaxedIdbCfgBuilder::new()
.vfs_name(VFS_NAME)
.build();
if let Err(e) = relaxed_idb::install::<WasmOsCallback>(&options, true).await {
log::warn!("IndexedDB storage unavailable: {e:?}");
return None;
}
match NostrSqlite::builder()
.persistent_with_vfs(DATABASE_PATH, VFS_NAME)
.build()
.await
{
Ok(database) => {
log::info!("Nostr database opened on IndexedDB storage");
Some(database)
}
Err(e) => {
log::error!("Failed to open the nostr database on IndexedDB: {e:?}");
None
}
}
}