From 32c64be331f8977a633d0a996dbc7623db90faf6 Mon Sep 17 00:00:00 2001 From: Darius Clark Date: Wed, 22 May 2024 20:48:57 -0400 Subject: [PATCH] chore: Enable persistence for wasm (#526) --- extensions/warp-ipfs/src/config.rs | 36 +++++++++++++++++++++- extensions/warp-ipfs/src/lib.rs | 48 ++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/extensions/warp-ipfs/src/config.rs b/extensions/warp-ipfs/src/config.rs index 1b1aeac1a..48dab7953 100644 --- a/extensions/warp-ipfs/src/config.rs +++ b/extensions/warp-ipfs/src/config.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, str::FromStr, time::Duration}; +use std::{path::PathBuf, time::Duration}; use ipfs::{Multiaddr, Protocol}; use rust_ipfs as ipfs; @@ -177,6 +177,7 @@ impl Default for StoreSetting { #[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)] pub struct Config { path: Option, + persist: bool, bootstrap: Bootstrap, listen_on: Vec, ipfs_setting: IpfsSetting, @@ -190,6 +191,10 @@ pub struct Config { } impl Config { + pub fn persist(&self) -> bool { + self.persist + } + pub fn path(&self) -> Option<&PathBuf> { self.path.as_ref() } @@ -240,6 +245,10 @@ impl Config { &mut self.path } + pub fn persist_mut(&mut self) -> &mut bool { + &mut self.persist + } + pub fn bootstrap_mut(&mut self) -> &mut Bootstrap { &mut self.bootstrap } @@ -283,8 +292,11 @@ impl Config { impl Default for Config { fn default() -> Self { + #[cfg(not(target_arch = "wasm32"))] + use std::str::FromStr; Config { path: None, + persist: false, bootstrap: Bootstrap::Ipfs, #[cfg(not(target_arch = "wasm32"))] listen_on: ["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic-v1"] @@ -366,6 +378,27 @@ impl Config { } } + #[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)] + pub fn minimal_basic() -> Config { + Config { + persist: true, + bootstrap: Bootstrap::None, + listen_on: vec![Multiaddr::empty().with(Protocol::Memory(0))], + ipfs_setting: IpfsSetting { + relay_client: RelayClient { + ..Default::default() + }, + memory_transport: true, + ..Default::default() + }, + store_setting: StoreSetting { + discovery: Discovery::None, + ..Default::default() + }, + ..Default::default() + } + } + /// Minimal production configuration #[cfg(not(target_arch = "wasm32"))] pub fn minimal>(path: P) -> Config { @@ -391,6 +424,7 @@ impl Config { #[cfg(not(target_arch = "wasm32"))] pub fn production>(path: P) -> Config { Config { + persist: true, bootstrap: Bootstrap::Ipfs, path: Some(path.as_ref().to_path_buf()), ipfs_setting: IpfsSetting { diff --git a/extensions/warp-ipfs/src/lib.rs b/extensions/warp-ipfs/src/lib.rs index d6c18b364..010564b1b 100644 --- a/extensions/warp-ipfs/src/lib.rs +++ b/extensions/warp-ipfs/src/lib.rs @@ -347,24 +347,42 @@ impl WarpIpfs { }); // TODO: Uncomment for persistence on wasm once config option is added - // #[cfg(target_arch = "wasm32")] - // { - // // Namespace will used the public key to prevent conflicts between multiple instances during testing. - // uninitialized = uninitialized.set_storage_type(rust_ipfs::StorageType::IndexedDb { - // namespace: Some(keypair.public().to_peer_id().to_string()), - // }); - // } + #[cfg(target_arch = "wasm32")] + { + if self.inner.config.persist() { + // Namespace will used the public key to prevent conflicts between multiple instances during testing. + uninitialized = uninitialized.set_storage_type(rust_ipfs::StorageType::IndexedDb { + namespace: Some(keypair.public().to_peer_id().to_string()), + }); + } + } #[cfg(not(target_arch = "wasm32"))] - if let Some(path) = self.inner.config.path() { - info!("Instance will be persistent"); - info!("Path set: {}", path.display()); - - if !path.is_dir() { - warn!("Path doesnt exist... creating"); - fs::create_dir_all(path).await?; + { + // TODO: Determine if we want to store in temp directory if path isnt set for any reason + // if let (path, true) = (self.inner.config.path(), self.inner.config.persist()) { + // info!("Instance will be persistent"); + // let path = match path { + // Some(path) => path.clone(), + // None => std::env::temp_dir().join(did.to_string() + "_temp") + // }; + // info!("Path set: {}", path.display()); + // if !path.is_dir() { + // warn!("Path doesnt exist... creating"); + // fs::create_dir_all(path).await?; + // } + // uninitialized = uninitialized.set_path(path); + // } + if let Some(path) = self.inner.config.path() { + info!("Instance will be persistent"); + info!("Path set: {}", path.display()); + + if !path.is_dir() { + warn!("Path doesnt exist... creating"); + fs::create_dir_all(path).await?; + } + uninitialized = uninitialized.set_path(path); } - uninitialized = uninitialized.set_path(path); } if matches!(