Skip to content

Commit

Permalink
Fix wasm
Browse files Browse the repository at this point in the history
Ref #4
  • Loading branch information
yukibtc committed Dec 25, 2024
1 parent b6fe16d commit c71bcee
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 32 deletions.
4 changes: 4 additions & 0 deletions lib/src/rust/api/relay/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ sealed class ConnectionMode with _$ConnectionMode {
const factory ConnectionMode.direct() = ConnectionMode_Direct;

/// Connect through proxy
///
/// This doesn't work on web!
const factory ConnectionMode.proxy({
/// Socket addr (i.e. 127.0.0.1:9050)
required String addr,
}) = ConnectionMode_Proxy;

/// Connect through tor network
///
/// This doesn't work on web!
const factory ConnectionMode.tor({
/// Path where to store data
///
Expand Down
11 changes: 5 additions & 6 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ codegen = ["dep:flutter_rust_bridge_codegen"]
anyhow = "1.0"
flutter_rust_bridge = "=2.0.0"
flutter_rust_bridge_codegen = { version = "=2.0.0", optional = true }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "3b988fb13e0ea39c13dad116bd63333ea5d2bc36", default-features = false, features = ["all-nips", "tor"] }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "6dde132193324dede0882f6d3ead039ddee36097", default-features = false, features = ["all-nips", "tor"] }

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }

[profile.release]
opt-level = 'z' # Optimize for size.
Expand Down
1 change: 1 addition & 0 deletions rust/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[toolchain]
channel = "1.82.0" # When changed, update it also in cargokit.yaml
targets = ["wasm32-unknown-unknown"]
104 changes: 81 additions & 23 deletions rust/src/api/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

#[cfg(not(target_arch = "wasm32"))]
use std::net::SocketAddr;
use std::ops::Deref;

use anyhow::Result;
use flutter_rust_bridge::frb;
#[cfg(target_arch = "wasm32")]
use nostr_sdk::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use nostr_sdk::prelude::{self, *};

use crate::api::relay::options::ConnectionMode;
Expand Down Expand Up @@ -79,9 +83,18 @@ impl _ClientOptions {

/// Connection
pub fn connection(&self, connection: _Connection) -> Self {
let mut builder = self.clone();
builder.inner = builder.inner.connection(connection.inner);
builder
#[cfg(not(target_arch = "wasm32"))]
{
let mut builder = self.clone();
builder.inner = builder.inner.connection(connection.inner);
builder
}

#[cfg(target_arch = "wasm32")]
{
let _ = connection;
self.clone()
}
}

// /// Set custom relay limits
Expand Down Expand Up @@ -109,7 +122,6 @@ impl _ClientOptions {
}

/// Connection target
#[cfg(not(target_arch = "wasm32"))]
pub enum ConnectionTarget {
/// Use proxy for all relays
All,
Expand All @@ -129,60 +141,106 @@ impl From<ConnectionTarget> for prelude::ConnectionTarget {

/// Connection
#[derive(Clone)]
#[cfg(not(target_arch = "wasm32"))]
#[frb(name = "Connection")]
pub struct _Connection {
#[cfg(not(target_arch = "wasm32"))]
inner: Connection,
#[cfg(target_arch = "wasm32")]
inner: (),
}

#[cfg(not(target_arch = "wasm32"))]
#[frb(sync)]
impl _Connection {
pub fn new() -> Self {
Self {
#[cfg(not(target_arch = "wasm32"))]
inner: Connection::default(),
#[cfg(target_arch = "wasm32")]
inner: (),
}
}

/// Set connection mode (default: direct)
pub fn mode(&self, mode: ConnectionMode) -> Result<Self> {
let mode: prelude::ConnectionMode = mode.try_into()?;
let mut builder = self.clone();
builder.inner = builder.inner.mode(mode);
Ok(builder)
#[cfg(not(target_arch = "wasm32"))]
{
let mode: prelude::ConnectionMode = mode.try_into()?;
let mut builder = self.clone();
builder.inner = builder.inner.mode(mode);
Ok(builder)
}

#[cfg(target_arch = "wasm32")]
{
let _ = mode;
Ok(self.clone())
}
}

/// Set connection target (default: all)
pub fn target(&self, target: ConnectionTarget) -> Self {
let mut builder = self.clone();
builder.inner = builder.inner.target(target.into());
builder
#[cfg(not(target_arch = "wasm32"))]
{
let mut builder = self.clone();
builder.inner = builder.inner.target(target.into());
builder
}

#[cfg(target_arch = "wasm32")]
{
let _ = target;
self.clone()
}
}

/// Set proxy (ex. `127.0.0.1:9050`)
pub fn addr(&self, addr: &str) -> Result<Self> {
let mut builder = self.clone();
let addr: SocketAddr = addr.parse()?;
builder.inner = builder.inner.proxy(addr);
Ok(builder)
#[cfg(not(target_arch = "wasm32"))]
{
let mut builder = self.clone();
let addr: SocketAddr = addr.parse()?;
builder.inner = builder.inner.proxy(addr);
Ok(builder)
}

#[cfg(target_arch = "wasm32")]
{
let _ = addr;
Ok(self.clone())
}
}

/// Use embedded tor client
///
/// This not work on `android` and/or `ios` targets.
/// Use [`Connection::embedded_tor_with_path`] instead.
pub fn embedded_tor(&self) -> Self {
let mut builder = self.clone();
builder.inner = builder.inner.embedded_tor();
builder
#[cfg(not(target_arch = "wasm32"))]
{
let mut builder = self.clone();
builder.inner = builder.inner.embedded_tor();
builder
}

#[cfg(target_arch = "wasm32")]
self.clone()
}

/// Use embedded tor client
///
/// Specify a path where to store data
pub fn embedded_tor_with_path(&self, data_path: String) -> Self {
let mut builder = self.clone();
builder.inner = builder.inner.embedded_tor_with_path(data_path);
builder
#[cfg(not(target_arch = "wasm32"))]
{
let mut builder = self.clone();
builder.inner = builder.inner.embedded_tor_with_path(data_path);
builder
}

#[cfg(target_arch = "wasm32")]
{
let _ = data_path;
self.clone()
}
}
}
11 changes: 9 additions & 2 deletions rust/src/api/relay/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

#[cfg(not(target_arch = "wasm32"))]
use std::path::PathBuf;

use anyhow::Error;
Expand All @@ -12,13 +13,15 @@ pub enum ConnectionMode {
/// Direct connection
Direct,
/// Connect through proxy
#[cfg(not(target_arch = "wasm32"))]
///
/// This doesn't work on web!
Proxy {
/// Socket addr (i.e. 127.0.0.1:9050)
addr: String,
},
/// Connect through tor network
#[cfg(not(target_arch = "wasm32"))]
///
/// This doesn't work on web!
Tor {
/// Path where to store data
///
Expand Down Expand Up @@ -51,10 +54,14 @@ impl TryFrom<ConnectionMode> for prelude::ConnectionMode {
ConnectionMode::Direct => Ok(Self::Direct),
#[cfg(not(target_arch = "wasm32"))]
ConnectionMode::Proxy { addr } => Ok(Self::Proxy(addr.parse()?)),
#[cfg(target_arch = "wasm32")]
ConnectionMode::Proxy { .. } => unreachable!("Proxy is not supported on wasm!"),
#[cfg(not(target_arch = "wasm32"))]
ConnectionMode::Tor { custom_path } => Ok(Self::Tor {
custom_path: custom_path.map(PathBuf::from),
}),
#[cfg(target_arch = "wasm32")]
ConnectionMode::Tor { .. } => unreachable!("Tor is not supported on wasm!"),
}
}
}

0 comments on commit c71bcee

Please sign in to comment.