Skip to content

Commit

Permalink
chore: Enable persistence for wasm (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored May 23, 2024
1 parent a4dbaee commit 32c64be
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
36 changes: 35 additions & 1 deletion extensions/warp-ipfs/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -177,6 +177,7 @@ impl Default for StoreSetting {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)]
pub struct Config {
path: Option<PathBuf>,
persist: bool,
bootstrap: Bootstrap,
listen_on: Vec<Multiaddr>,
ipfs_setting: IpfsSetting,
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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<P: AsRef<std::path::Path>>(path: P) -> Config {
Expand All @@ -391,6 +424,7 @@ impl Config {
#[cfg(not(target_arch = "wasm32"))]
pub fn production<P: AsRef<std::path::Path>>(path: P) -> Config {
Config {
persist: true,
bootstrap: Bootstrap::Ipfs,
path: Some(path.as_ref().to_path_buf()),
ipfs_setting: IpfsSetting {
Expand Down
48 changes: 33 additions & 15 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 32c64be

Please sign in to comment.