From 50ef9653253a517e93bcb4e6ea7d36ddf8687e94 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Sat, 14 Dec 2024 09:52:32 +0100 Subject: [PATCH] js: use redb (WIP) Signed-off-by: Yuki Kishimoto --- Cargo.lock | 1 + bindings/nostr-sdk-js/Cargo.toml | 3 +- bindings/nostr-sdk-js/examples/negentropy.js | 14 ++++--- .../nostr-sdk-js/examples/webapp/package.json | 2 +- .../nostr-sdk-js/examples/webapp/src/App.js | 38 ++++++++++--------- bindings/nostr-sdk-js/src/database/mod.rs | 18 ++++----- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33971cbce..83856c7d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2963,6 +2963,7 @@ dependencies = [ "console_error_panic_hook", "js-sys", "nostr-connect", + "nostr-redb", "nostr-sdk", "nwc", "tracing", diff --git a/bindings/nostr-sdk-js/Cargo.toml b/bindings/nostr-sdk-js/Cargo.toml index 4e1e06953..a54b29f96 100644 --- a/bindings/nostr-sdk-js/Cargo.toml +++ b/bindings/nostr-sdk-js/Cargo.toml @@ -11,7 +11,8 @@ crate-type = ["cdylib"] console_error_panic_hook = "0.1" js-sys.workspace = true nostr-connect.workspace = true -nostr-sdk = { workspace = true, default-features = false, features = ["all-nips", "indexeddb", "webln"] } +nostr-redb.workspace = true +nostr-sdk = { workspace = true, default-features = false, features = ["all-nips", "webln"] } nwc.workspace = true tracing.workspace = true tracing-subscriber.workspace = true diff --git a/bindings/nostr-sdk-js/examples/negentropy.js b/bindings/nostr-sdk-js/examples/negentropy.js index 740dbcf6b..81628a16e 100644 --- a/bindings/nostr-sdk-js/examples/negentropy.js +++ b/bindings/nostr-sdk-js/examples/negentropy.js @@ -1,22 +1,26 @@ -const { loadWasmAsync, initLogger, LogLevel, SyncOptions, SyncDirection, Filter, Client, NostrDatabase } = require("../"); - -// NOTE: this code work only on browser (due to indexeddb)! +const { loadWasmAsync, initLogger, LogLevel, SyncOptions, SyncDirection, Filter, Client, NostrDatabase, Kind } = require("../"); async function main() { await loadWasmAsync(); initLogger(LogLevel.info()); - let db = await NostrDatabase.indexeddb("js-test"); + let db = await NostrDatabase.inMemory(); let client = Client.builder().database(db).build(); await client.addRelay("wss://relay.damus.io"); await client.connect(); - let filter = new Filter().kind(1).limit(1000); + let filter = new Filter().kind(new Kind(1)).limit(1000); let opts = new SyncOptions().direction(SyncDirection.Down); await client.sync(filter, opts); + + let f = new Filter().limit(2); + let events = await db.query([f]); + events.forEach((e) => { + console.log(e.asJson()) + }) } main(); diff --git a/bindings/nostr-sdk-js/examples/webapp/package.json b/bindings/nostr-sdk-js/examples/webapp/package.json index 94ab64f3f..2abbfb2b2 100644 --- a/bindings/nostr-sdk-js/examples/webapp/package.json +++ b/bindings/nostr-sdk-js/examples/webapp/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { - "@rust-nostr/nostr-sdk": "^0.33.0", + "@rust-nostr/nostr-sdk": "file:../../rust-nostr-nostr-sdk-0.37.0.tgz", "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", diff --git a/bindings/nostr-sdk-js/examples/webapp/src/App.js b/bindings/nostr-sdk-js/examples/webapp/src/App.js index f41dffecc..d715dda98 100644 --- a/bindings/nostr-sdk-js/examples/webapp/src/App.js +++ b/bindings/nostr-sdk-js/examples/webapp/src/App.js @@ -1,12 +1,11 @@ import React, { Component } from "react"; -import { ClientBuilder, NostrSigner, NostrZapper, Filter, LogLevel, NegentropyOptions, Nip07Signer, NostrDatabase, PublicKey, ZapDetails, ZapEntity, ZapType, initLogger, loadWasmAsync } from '@rust-nostr/nostr-sdk' +import { ClientBuilder, EventBuilder, NostrSigner, NostrZapper, Filter, LogLevel, SyncOptions, Nip07Signer, NostrDatabase, PublicKey, ZapDetails, ZapEntity, ZapType, initLogger, loadWasmAsync } from '@rust-nostr/nostr-sdk' import './App.css'; -import {EventBuilder} from "../../../pkg/nostr_sdk_js"; class App extends Component { constructor() { super(); - this.state = { + this.state = { public_key: null, nip07_signer: null, client: null @@ -19,7 +18,8 @@ class App extends Component { // Try to initialize log try { - initLogger(LogLevel.info()); + initLogger(LogLevel.debug()); + console.log("Logger initialized"); } catch (error) {} } @@ -29,33 +29,37 @@ class App extends Component { let nip07_signer = new Nip07Signer(); let signer = NostrSigner.nip07(nip07_signer); let zapper = await NostrZapper.webln(); - let db = await NostrDatabase.indexeddb("nostr-sdk-webapp-example"); + + console.log("Opening database..."); + let db = await NostrDatabase.web("nostr-sdk-webapp-example-2"); + console.log("Database opened."); + let client = new ClientBuilder().signer(signer).zapper(zapper).database(db).build(); let public_key = await nip07_signer.getPublicKey(); - + // Add relays await client.addRelay("wss://relay.damus.io"); await client.addRelay("wss://nos.lol"); await client.addRelay("wss://nostr.oxtr.dev"); - + // Connect to relays await client.connect(); - + // Save client to state this.setState({ client, nip07_signer, public_key }); } catch (error) { - console.log(error) + console.log(error) } }; handleReconcile = async () => { try { let filter = new Filter().author(this.state.public_key); - let opts = new NegentropyOptions(); + let opts = new SyncOptions(); await this.state.client.sync(filter, opts); } catch (error) { - console.log(error) + console.log(error) } } @@ -66,7 +70,7 @@ class App extends Component { console.time("query"); let events = await database.query([filter]); console.timeEnd("query"); - console.log("Got", events.length, "events"); + console.log("Got", events.len(), "events"); } catch (error) { console.log(error) } @@ -74,21 +78,21 @@ class App extends Component { handlePublishTextNote = async () => { try { - let builder = EventBuilder.textNote("Test from rust-nostr JavaScript bindings with NIP07 signer!", []); + let builder = EventBuilder.textNote("Test from rust-nostr JavaScript bindings with NIP07 signer!"); await this.state.client.sendEventBuilder(builder); } catch (error) { - console.log(error) + console.log(error) } }; handleZap = async () => { try { - let pk = PublicKey.fromBech32("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet"); + let pk = PublicKey.parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet"); let entity = ZapEntity.publicKey(pk); let details = new ZapDetails(ZapType.Public).message("Zap for Rust Nostr!"); await this.state.client.zap(entity, 1000, details); } catch (error) { - console.log(error) + console.log(error) } }; @@ -98,7 +102,7 @@ class App extends Component { this.setState({ client: null }); console.log("Logout done"); } catch (error) { - console.log(error) + console.log(error) } }; diff --git a/bindings/nostr-sdk-js/src/database/mod.rs b/bindings/nostr-sdk-js/src/database/mod.rs index 6f2caacbc..c78e1cc2d 100644 --- a/bindings/nostr-sdk-js/src/database/mod.rs +++ b/bindings/nostr-sdk-js/src/database/mod.rs @@ -6,6 +6,7 @@ use std::ops::Deref; use std::sync::Arc; use js_sys::Array; +use nostr_redb::NostrRedb; use nostr_sdk::prelude::*; use wasm_bindgen::prelude::*; @@ -75,22 +76,17 @@ impl From> for JsNostrDatabase { #[wasm_bindgen(js_class = NostrDatabase)] impl JsNostrDatabase { - /// Open/Create database with **unlimited** capacity - pub async fn indexeddb(name: &str) -> Result { - let db = WebDatabase::open(name).await.map_err(into_err)?; + /// Open (or create) persistent web database + pub async fn web(name: &str) -> Result { + let db = NostrRedb::web(name).await.map_err(into_err)?; Ok(Self { inner: db.into_nostr_database(), }) } - /// Open/Create database with **limited** capacity - #[wasm_bindgen(js_name = indexeddbBounded)] - pub async fn indexeddb_bounded(name: &str, max_capacity: u64) -> Result { - let db = Arc::new( - WebDatabase::open_bounded(name, max_capacity as usize) - .await - .map_err(into_err)?, - ); + #[wasm_bindgen(js_name = inMemory)] + pub fn in_memory() -> Result { + let db = NostrRedb::in_memory().map_err(into_err)?; Ok(Self { inner: db.into_nostr_database(), })