Skip to content

Commit c71bcee

Browse files
committed
Fix wasm
Ref #4
1 parent b6fe16d commit c71bcee

File tree

6 files changed

+104
-32
lines changed

6 files changed

+104
-32
lines changed

lib/src/rust/api/relay/options.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ sealed class ConnectionMode with _$ConnectionMode {
1818
const factory ConnectionMode.direct() = ConnectionMode_Direct;
1919

2020
/// Connect through proxy
21+
///
22+
/// This doesn't work on web!
2123
const factory ConnectionMode.proxy({
2224
/// Socket addr (i.e. 127.0.0.1:9050)
2325
required String addr,
2426
}) = ConnectionMode_Proxy;
2527

2628
/// Connect through tor network
29+
///
30+
/// This doesn't work on web!
2731
const factory ConnectionMode.tor({
2832
/// Path where to store data
2933
///

rust/Cargo.lock

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ codegen = ["dep:flutter_rust_bridge_codegen"]
2020
anyhow = "1.0"
2121
flutter_rust_bridge = "=2.0.0"
2222
flutter_rust_bridge_codegen = { version = "=2.0.0", optional = true }
23-
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "3b988fb13e0ea39c13dad116bd63333ea5d2bc36", default-features = false, features = ["all-nips", "tor"] }
23+
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "6dde132193324dede0882f6d3ead039ddee36097", default-features = false, features = ["all-nips", "tor"] }
24+
25+
[lints.rust]
26+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }
2427

2528
[profile.release]
2629
opt-level = 'z' # Optimize for size.

rust/rust-toolchain.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[toolchain]
22
channel = "1.82.0" # When changed, update it also in cargokit.yaml
3+
targets = ["wasm32-unknown-unknown"]

rust/src/api/client/options.rs

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
// Copyright (c) 2023-2024 Rust Nostr Developers
33
// Distributed under the MIT software license
44

5+
#[cfg(not(target_arch = "wasm32"))]
56
use std::net::SocketAddr;
67
use std::ops::Deref;
78

89
use anyhow::Result;
910
use flutter_rust_bridge::frb;
11+
#[cfg(target_arch = "wasm32")]
12+
use nostr_sdk::prelude::*;
13+
#[cfg(not(target_arch = "wasm32"))]
1014
use nostr_sdk::prelude::{self, *};
1115

1216
use crate::api::relay::options::ConnectionMode;
@@ -79,9 +83,18 @@ impl _ClientOptions {
7983

8084
/// Connection
8185
pub fn connection(&self, connection: _Connection) -> Self {
82-
let mut builder = self.clone();
83-
builder.inner = builder.inner.connection(connection.inner);
84-
builder
86+
#[cfg(not(target_arch = "wasm32"))]
87+
{
88+
let mut builder = self.clone();
89+
builder.inner = builder.inner.connection(connection.inner);
90+
builder
91+
}
92+
93+
#[cfg(target_arch = "wasm32")]
94+
{
95+
let _ = connection;
96+
self.clone()
97+
}
8598
}
8699

87100
// /// Set custom relay limits
@@ -109,7 +122,6 @@ impl _ClientOptions {
109122
}
110123

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

130142
/// Connection
131143
#[derive(Clone)]
132-
#[cfg(not(target_arch = "wasm32"))]
133144
#[frb(name = "Connection")]
134145
pub struct _Connection {
146+
#[cfg(not(target_arch = "wasm32"))]
135147
inner: Connection,
148+
#[cfg(target_arch = "wasm32")]
149+
inner: (),
136150
}
137151

138-
#[cfg(not(target_arch = "wasm32"))]
139152
#[frb(sync)]
140153
impl _Connection {
141154
pub fn new() -> Self {
142155
Self {
156+
#[cfg(not(target_arch = "wasm32"))]
143157
inner: Connection::default(),
158+
#[cfg(target_arch = "wasm32")]
159+
inner: (),
144160
}
145161
}
146162

147163
/// Set connection mode (default: direct)
148164
pub fn mode(&self, mode: ConnectionMode) -> Result<Self> {
149-
let mode: prelude::ConnectionMode = mode.try_into()?;
150-
let mut builder = self.clone();
151-
builder.inner = builder.inner.mode(mode);
152-
Ok(builder)
165+
#[cfg(not(target_arch = "wasm32"))]
166+
{
167+
let mode: prelude::ConnectionMode = mode.try_into()?;
168+
let mut builder = self.clone();
169+
builder.inner = builder.inner.mode(mode);
170+
Ok(builder)
171+
}
172+
173+
#[cfg(target_arch = "wasm32")]
174+
{
175+
let _ = mode;
176+
Ok(self.clone())
177+
}
153178
}
154179

155180
/// Set connection target (default: all)
156181
pub fn target(&self, target: ConnectionTarget) -> Self {
157-
let mut builder = self.clone();
158-
builder.inner = builder.inner.target(target.into());
159-
builder
182+
#[cfg(not(target_arch = "wasm32"))]
183+
{
184+
let mut builder = self.clone();
185+
builder.inner = builder.inner.target(target.into());
186+
builder
187+
}
188+
189+
#[cfg(target_arch = "wasm32")]
190+
{
191+
let _ = target;
192+
self.clone()
193+
}
160194
}
161195

162196
/// Set proxy (ex. `127.0.0.1:9050`)
163197
pub fn addr(&self, addr: &str) -> Result<Self> {
164-
let mut builder = self.clone();
165-
let addr: SocketAddr = addr.parse()?;
166-
builder.inner = builder.inner.proxy(addr);
167-
Ok(builder)
198+
#[cfg(not(target_arch = "wasm32"))]
199+
{
200+
let mut builder = self.clone();
201+
let addr: SocketAddr = addr.parse()?;
202+
builder.inner = builder.inner.proxy(addr);
203+
Ok(builder)
204+
}
205+
206+
#[cfg(target_arch = "wasm32")]
207+
{
208+
let _ = addr;
209+
Ok(self.clone())
210+
}
168211
}
169212

170213
/// Use embedded tor client
171214
///
172215
/// This not work on `android` and/or `ios` targets.
173216
/// Use [`Connection::embedded_tor_with_path`] instead.
174217
pub fn embedded_tor(&self) -> Self {
175-
let mut builder = self.clone();
176-
builder.inner = builder.inner.embedded_tor();
177-
builder
218+
#[cfg(not(target_arch = "wasm32"))]
219+
{
220+
let mut builder = self.clone();
221+
builder.inner = builder.inner.embedded_tor();
222+
builder
223+
}
224+
225+
#[cfg(target_arch = "wasm32")]
226+
self.clone()
178227
}
179228

180229
/// Use embedded tor client
181230
///
182231
/// Specify a path where to store data
183232
pub fn embedded_tor_with_path(&self, data_path: String) -> Self {
184-
let mut builder = self.clone();
185-
builder.inner = builder.inner.embedded_tor_with_path(data_path);
186-
builder
233+
#[cfg(not(target_arch = "wasm32"))]
234+
{
235+
let mut builder = self.clone();
236+
builder.inner = builder.inner.embedded_tor_with_path(data_path);
237+
builder
238+
}
239+
240+
#[cfg(target_arch = "wasm32")]
241+
{
242+
let _ = data_path;
243+
self.clone()
244+
}
187245
}
188246
}

rust/src/api/relay/options.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) 2023-2024 Rust Nostr Developers
33
// Distributed under the MIT software license
44

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

78
use anyhow::Error;
@@ -12,13 +13,15 @@ pub enum ConnectionMode {
1213
/// Direct connection
1314
Direct,
1415
/// Connect through proxy
15-
#[cfg(not(target_arch = "wasm32"))]
16+
///
17+
/// This doesn't work on web!
1618
Proxy {
1719
/// Socket addr (i.e. 127.0.0.1:9050)
1820
addr: String,
1921
},
2022
/// Connect through tor network
21-
#[cfg(not(target_arch = "wasm32"))]
23+
///
24+
/// This doesn't work on web!
2225
Tor {
2326
/// Path where to store data
2427
///
@@ -51,10 +54,14 @@ impl TryFrom<ConnectionMode> for prelude::ConnectionMode {
5154
ConnectionMode::Direct => Ok(Self::Direct),
5255
#[cfg(not(target_arch = "wasm32"))]
5356
ConnectionMode::Proxy { addr } => Ok(Self::Proxy(addr.parse()?)),
57+
#[cfg(target_arch = "wasm32")]
58+
ConnectionMode::Proxy { .. } => unreachable!("Proxy is not supported on wasm!"),
5459
#[cfg(not(target_arch = "wasm32"))]
5560
ConnectionMode::Tor { custom_path } => Ok(Self::Tor {
5661
custom_path: custom_path.map(PathBuf::from),
5762
}),
63+
#[cfg(target_arch = "wasm32")]
64+
ConnectionMode::Tor { .. } => unreachable!("Tor is not supported on wasm!"),
5865
}
5966
}
6067
}

0 commit comments

Comments
 (0)