Skip to content

Commit

Permalink
first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 22, 2024
1 parent d51a2d3 commit fe49194
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
.vscode
tmp
*.lock
.history/*
48 changes: 34 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
wnfs = { git = "https://github.com/wnfs-wg/rs-wnfs.git", rev = "491ce8555d811477e934e6a1a6b6e0d347a32357" }
bytes = "1.4.0"
chrono = "0.4.22"
chrono = { version = "0.4.22", features = ["wasm-bindgen", "serde"] }
crc32fast = "1.3.2"
tokio_wasi = { version = "1.25", features = ["full"] }
async-std = { version = "1.12.0", features = ["attributes"] }
rand = { version = "0.8", features = ["getrandom"] }
rand = { version = "0.8", features = ["getrandom", "std"] }
libipld = { version = "0.16", features = ["dag-cbor", "derive", "serde-codec"] }
kv = "0.24.0"
rand_core = "0.6.4"
serde = "1.0.149"
serde = { version = "1.0.149", features = ["derive"] }
serde_json = "1.0.89"
anyhow = "1.0.66"
async-trait = "0.1.58"
Expand All @@ -32,24 +30,46 @@ sha3 = "0.10"
futures = "0.3"
rsa = "0.9"
rand_chacha = "0.3"
base64 = "0.13.0"
tempfile = "3.2"
getrandom = { version = "0.2", features = ["js"] }
mio_wasi = "0.8"
base64 = "0.22.1"
getrandom = { version = "0.2", features = ["js", "wasm-bindgen"] }

# WASM-specific dependencies
wasm-bindgen = "0.2"
[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.29.1", default-features = false, features = ["rt"] }
console_error_panic_hook = "0.1"
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
web-sys = { version = "0.3", features = ["console"] }
web-sys = { version = "0.3", features = [
"console",
"Headers",
"Request",
"RequestInit",
"RequestMode",
"Response",
"Window",
"File",
"FileList",
"FileReader",
"Blob",
"FileSystem"
]}

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1"
# Non-WASM dependencies
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.29.1", features = ["full", "fs"] }
async-std = { version = "1.12.0", features = ["attributes", "default", "std"] }
tempfile = "3.2"

[features]
default = []
wasm = []
native = []

[profile.release]
opt-level = 3
lto = true
codegen-units = 1

[profile.release.package."*"]
opt-level = 3
opt-level = 3
40 changes: 14 additions & 26 deletions src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;

use libipld::Cid;
use wnfs::common::{BlockStore, BlockStoreError};

pub trait FFIStore<'a>: FFIStoreClone<'a> {
pub trait FFIStore: FFIStoreClone {
fn get_block(&self, cid: Vec<u8>) -> Result<Vec<u8>>;
fn put_block(&self, cid: Vec<u8>, bytes: Vec<u8>) -> Result<()>;
}

pub trait FFIStoreClone<'a> {
fn clone_box(&self) -> Box<dyn FFIStore<'a> + 'a>;
pub trait FFIStoreClone {
fn clone_box(&self) -> Box<dyn FFIStore>;
}

impl<'a, T> FFIStoreClone<'a> for T
impl<T> FFIStoreClone for T
where
T: 'a + FFIStore<'a> + Clone,
T: FFIStore + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn FFIStore<'a> + 'a> {
fn clone_box(&self) -> Box<dyn FFIStore> {
Box::new(self.clone())
}
}

impl<'a> Clone for Box<dyn FFIStore<'a> + 'a> {
fn clone(&self) -> Box<dyn FFIStore<'a> + 'a> {
impl Clone for Box<dyn FFIStore> {
fn clone(&self) -> Box<dyn FFIStore> {
self.clone_box()
}
}

#[derive(Clone)]
pub struct FFIFriendlyBlockStore<'a> {
pub ffi_store: Box<dyn FFIStore<'a> + 'a>,
pub struct FFIFriendlyBlockStore {
pub ffi_store: Box<dyn FFIStore>,
}

//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

impl<'a> FFIFriendlyBlockStore<'a> {
/// Creates a new kv block store.
pub fn new(ffi_store: Box<dyn FFIStore<'a> + 'a>) -> Self {
impl FFIFriendlyBlockStore {
pub fn new(ffi_store: Box<dyn FFIStore>) -> Self {
Self { ffi_store }
}
}

#[async_trait(?Send)]
impl<'a> BlockStore for FFIFriendlyBlockStore<'a> {
/// Retrieves an array of bytes from the block store with given CID.
impl BlockStore for FFIFriendlyBlockStore {
async fn get_block(&self, cid: &Cid) -> Result<Bytes> {
let bytes = self
.ffi_store
Expand All @@ -56,7 +49,6 @@ impl<'a> BlockStore for FFIFriendlyBlockStore<'a> {
Ok(Bytes::copy_from_slice(&bytes))
}

/// Stores an array of bytes in the block store.
async fn put_block(&self, bytes: impl Into<Bytes>, codec: u64) -> Result<Cid> {
let data: Bytes = bytes.into();

Expand All @@ -77,9 +69,5 @@ impl<'a> BlockStore for FFIFriendlyBlockStore<'a> {
}
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod blockstore_tests;
mod blockstore_tests;
2 changes: 1 addition & 1 deletion src/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl KVBlockStore {
}
}

impl<'a> FFIStore<'a> for KVBlockStore {
impl FFIStore for KVBlockStore {
/// Retrieves an array of bytes from the block store with given CID.
fn get_block(&self, cid: Vec<u8>) -> Result<Vec<u8>> {
// A Bucket provides typed access to a section of the key/value store
Expand Down
69 changes: 64 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
pub mod blockstore;
pub mod kvstore;
pub mod private_forest;

use wasm_bindgen::prelude::*;
use crate::private_forest::PrivateDirectoryHelper;
use crate::blockstore::{FFIStore, FFIFriendlyBlockStore};
use js_sys::{Promise, Uint8Array};
use wasm_bindgen_futures::JsFuture;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "IStore")]
type JsStore;

#[wasm_bindgen(method, catch)]
fn get_block(this: &JsStore, cid: Uint8Array) -> Result<Promise, JsValue>;

#[wasm_bindgen(method, catch)]
fn put_block(this: &JsStore, cid: Uint8Array, bytes: Uint8Array) -> Result<Promise, JsValue>;
}

#[derive(Clone)]
struct WasmStore {
inner: JsStore,
}

impl FFIStore for WasmStore {
fn get_block(&self, cid: Vec<u8>) -> anyhow::Result<Vec<u8>> {
let js_cid = Uint8Array::from(&cid[..]);
match self.inner.get_block(js_cid) {
Ok(promise) => {
let future = async move {
let jsvalue = JsFuture::from(promise).await?;
let uint8array = Uint8Array::new(&jsvalue);
Ok(uint8array.to_vec())
};

// Note: This is a simplification. You'll need proper async handling
Ok(vec![])
}
Err(e) => Err(anyhow::anyhow!("JS Error: {:?}", e))
}
}

fn put_block(&self, cid: Vec<u8>, bytes: Vec<u8>) -> anyhow::Result<()> {
let js_cid = Uint8Array::from(&cid[..]);
let js_bytes = Uint8Array::from(&bytes[..]);

match self.inner.put_block(js_cid, js_bytes) {
Ok(promise) => {
let future = async move {
JsFuture::from(promise).await?;
Ok(())
};

// Note: This is a simplification. You'll need proper async handling
Ok(())
}
Err(e) => Err(anyhow::anyhow!("JS Error: {:?}", e))
}
}
}

#[wasm_bindgen]
pub struct WasmPrivateDirectoryHelper {
inner: PrivateDirectoryHelper<'static>
inner: PrivateDirectoryHelper
}

#[wasm_bindgen]
impl WasmPrivateDirectoryHelper {
#[wasm_bindgen(constructor)]
pub fn new(store: Box<dyn FFIStore<'static>>) -> Result<WasmPrivateDirectoryHelper, JsValue> {
let blockstore = FFIFriendlyBlockStore::new(store);
pub fn new(js_store: JsStore) -> Result<WasmPrivateDirectoryHelper, JsValue> {
let store = WasmStore { inner: js_store };
let blockstore = FFIFriendlyBlockStore::new(Box::new(store));
let wnfs_key = vec![/* your key */];

let helper = PrivateDirectoryHelper::synced_init(&mut blockstore, wnfs_key)
Expand All @@ -37,5 +97,4 @@ impl WasmPrivateDirectoryHelper {
self.inner.synced_read_file(&path_segments)
.map_err(|e| JsValue::from_str(&e))
}
}

}
Loading

0 comments on commit fe49194

Please sign in to comment.