Skip to content

Commit e7547d2

Browse files
committed
feat: prepare for wasm support
1 parent 70a00ce commit e7547d2

File tree

7 files changed

+83
-50
lines changed

7 files changed

+83
-50
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ futures-buffered = "0.2.4"
3131
futures-lite = "2.3.0"
3232
futures-util = { version = "0.3.25" }
3333
hex = "0.4"
34-
iroh = { version = "0.94" }
34+
iroh = { version = "0.94", default-features = false }
3535
iroh-tickets = { version = "0.1"}
36-
iroh-blobs = { version = "0.96" }
37-
iroh-gossip = { version = "0.94", features = ["net"] }
36+
iroh-blobs = { git = "https://github.com/n0-computer/iroh-blobs", branch = "Frando/wasm-nopatch", default-features = false }
37+
iroh-gossip = { version = "0.94", features = ["net"], default-features = false }
3838
iroh-metrics = { version = "0.36", default-features = false }
39-
irpc = { version = "0.10.0" }
39+
irpc = { git = "https://github.com/n0-computer/irpc", branch = "main", default-features = false }
4040
n0-future = "0.3"
4141
num_enum = "0.7"
4242
postcard = { version = "1", default-features = false, features = [
4343
"alloc",
4444
"use-std",
4545
"experimental-derive",
4646
] }
47-
quinn = { package = "iroh-quinn", version = "0.14.0" }
47+
quinn = { package = "iroh-quinn", version = "0.14.0", optional = true }
4848
rand = "0.9.2"
4949
redb = { version = "2.6.3" }
5050
self_cell = "1.0.3"
@@ -74,8 +74,10 @@ tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
7474
tracing-test = "0.2.5"
7575

7676
[features]
77-
default = ["metrics"]
77+
default = ["metrics", "rpc", "fs-store"]
7878
metrics = ["iroh-metrics/metrics", "iroh/metrics"]
79+
rpc = ["dep:quinn", "irpc/rpc", "iroh-blobs/rpc"]
80+
fs-store = ["iroh-blobs/fs-store"]
7981

8082
[package.metadata.docs.rs]
8183
all-features = true

src/api.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use std::{
66
future::Future,
7-
net::SocketAddr,
87
path::Path,
98
pin::Pin,
109
sync::{
@@ -14,18 +13,14 @@ use std::{
1413
task::{ready, Poll},
1514
};
1615

17-
use anyhow::{Context, Result};
16+
use anyhow::Result;
1817
use bytes::Bytes;
1918
use iroh::EndpointAddr;
2019
use iroh_blobs::{
2120
api::blobs::{AddPathOptions, AddProgressItem, ExportMode, ExportOptions, ExportProgress},
2221
Hash,
2322
};
24-
use irpc::rpc::Handler;
25-
use n0_future::{
26-
task::{self, AbortOnDropHandle},
27-
FutureExt, Stream, StreamExt,
28-
};
23+
use n0_future::{FutureExt, Stream, StreamExt};
2924

3025
use self::{
3126
actor::RpcActor,
@@ -67,19 +62,25 @@ impl DocsApi {
6762
}
6863

6964
/// Connect to a remote docs service
70-
pub fn connect(endpoint: quinn::Endpoint, addr: SocketAddr) -> Result<DocsApi> {
65+
#[cfg(feature = "rpc")]
66+
pub fn connect(endpoint: quinn::Endpoint, addr: std::net::SocketAddr) -> Result<DocsApi> {
7167
Ok(DocsApi {
7268
inner: Client::quinn(endpoint, addr),
7369
})
7470
}
7571

7672
/// Listen for incoming RPC connections
77-
pub fn listen(&self, endpoint: quinn::Endpoint) -> Result<AbortOnDropHandle<()>> {
73+
#[cfg(feature = "rpc")]
74+
pub fn listen(
75+
&self,
76+
endpoint: quinn::Endpoint,
77+
) -> Result<n0_future::task::AbortOnDropHandle<()>> {
78+
use anyhow::Context;
7879
let local = self
7980
.inner
8081
.as_local()
8182
.context("cannot listen on remote API")?;
82-
let handler: Handler<DocsProtocol> = Arc::new(move |msg, _rx, tx| {
83+
let handler: irpc::rpc::Handler<DocsProtocol> = Arc::new(move |msg, _rx, tx| {
8384
let local = local.clone();
8485
Box::pin(async move {
8586
match msg {
@@ -114,8 +115,8 @@ impl DocsApi {
114115
}
115116
})
116117
});
117-
let join_handle = task::spawn(irpc::rpc::listen(endpoint, handler));
118-
Ok(AbortOnDropHandle::new(join_handle))
118+
let join_handle = n0_future::task::spawn(irpc::rpc::listen(endpoint, handler));
119+
Ok(n0_future::task::AbortOnDropHandle::new(join_handle))
119120
}
120121

121122
/// Creates a new document author.

src/api/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub struct AuthorDeleteResponse;
304304

305305
// Use the macro to generate both the DocsProtocol and DocsMessage enums
306306
// plus implement Channels for each type
307-
#[rpc_requests(message = DocsMessage)]
307+
#[rpc_requests(message = DocsMessage, rpc_feature = "rpc")]
308308
#[derive(Serialize, Deserialize, Debug)]
309309
pub enum DocsProtocol {
310310
#[rpc(tx = oneshot::Sender<RpcResult<OpenResponse>>)]

src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use futures_lite::{Stream, StreamExt};
1313
use iroh::{Endpoint, EndpointAddr, PublicKey};
1414
use iroh_blobs::{
1515
api::{blobs::BlobStatus, downloader::Downloader, Store},
16-
store::fs::options::{ProtectCb, ProtectOutcome},
16+
store::{ProtectCb, ProtectOutcome},
1717
Hash,
1818
};
1919
use iroh_gossip::net::Gossip;

src/protocol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl Docs {
2828

2929
/// Create a new [`Builder`] for the docs protocol, using a persistent replica and author storage
3030
/// in the given directory.
31+
#[cfg(feature = "fs-store")]
3132
pub fn persistent(path: PathBuf) -> Builder {
3233
Builder {
3334
path: Some(path),

tests/util.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use iroh::{discovery::IntoDiscovery, dns::DnsResolver, EndpointId, RelayMode, SecretKey};
9-
use iroh_blobs::store::fs::options::{GcConfig, Options};
9+
use iroh_blobs::store::GcConfig;
1010
use iroh_docs::{engine::ProtectCallbackHandler, protocol::Docs};
1111
use iroh_gossip::net::Gossip;
1212

@@ -64,7 +64,7 @@ impl Client {
6464
pub struct Builder {
6565
endpoint: iroh::endpoint::Builder,
6666
use_n0_discovery: bool,
67-
path: Option<PathBuf>,
67+
storage: Storage,
6868
// node_discovery: Option<Box<dyn Discovery>>,
6969
gc_interval: Option<std::time::Duration>,
7070
#[debug(skip)]
@@ -97,9 +97,10 @@ impl Builder {
9797
let endpoint = builder.bind().await?;
9898
let mut router = iroh::protocol::Router::builder(endpoint.clone());
9999
let gossip = Gossip::builder().spawn(endpoint.clone());
100-
let mut docs_builder = match self.path {
101-
Some(ref path) => Docs::persistent(path.to_path_buf()),
102-
None => Docs::memory(),
100+
let mut docs_builder = match self.storage {
101+
Storage::Memory => Docs::memory(),
102+
#[cfg(feature = "fs-store")]
103+
Storage::Persistent(ref path) => Docs::persistent(path.to_path_buf()),
103104
};
104105
if let Some(protect_cb) = protect_cb {
105106
docs_builder = docs_builder.protect_handler(protect_cb);
@@ -183,11 +184,11 @@ impl Builder {
183184
self
184185
}
185186

186-
fn new(path: Option<PathBuf>) -> Self {
187+
fn new(storage: Storage) -> Self {
187188
Self {
188189
endpoint: iroh::Endpoint::empty_builder(RelayMode::Disabled),
189190
use_n0_discovery: true,
190-
path,
191+
storage,
191192
gc_interval: None,
192193
bind_random_port: false,
193194
// node_discovery: None,
@@ -197,30 +198,38 @@ impl Builder {
197198
}
198199
}
199200

201+
#[derive(Debug)]
202+
enum Storage {
203+
Memory,
204+
#[cfg(feature = "fs-store")]
205+
Persistent(PathBuf),
206+
}
207+
200208
impl Node {
201209
/// Creates a new node with memory storage
202210
pub fn memory() -> Builder {
203-
Builder::new(None)
211+
Builder::new(Storage::Memory)
204212
}
205213

206214
/// Creates a new node with persistent storage
215+
#[cfg(feature = "fs-store")]
207216
pub fn persistent(path: impl AsRef<Path>) -> Builder {
208-
let path = Some(path.as_ref().to_owned());
209-
Builder::new(path)
217+
Builder::new(Storage::Persistent(path.as_ref().to_owned()))
210218
}
211219
}
212220

213221
impl Builder {
214222
/// Spawns the node
215223
pub async fn spawn(self) -> anyhow::Result<Node> {
216-
let (store, protect_handler) = match self.path {
217-
None => {
224+
let (store, protect_handler) = match self.storage {
225+
Storage::Memory => {
218226
let store = iroh_blobs::store::mem::MemStore::new();
219227
((*store).clone(), None)
220228
}
221-
Some(ref path) => {
229+
#[cfg(feature = "fs-store")]
230+
Storage::Persistent(ref path) => {
222231
let db_path = path.join("blobs.db");
223-
let mut opts = Options::new(path);
232+
let mut opts = iroh_blobs::store::fs::options::Options::new(path);
224233
let protect_handler = if let Some(interval) = self.gc_interval {
225234
let (handler, cb) = ProtectCallbackHandler::new();
226235
opts.gc = Some(GcConfig {

0 commit comments

Comments
 (0)