Skip to content

Commit

Permalink
second attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 22, 2024
1 parent fe49194 commit 77e56a1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 742 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ getrandom = { version = "0.2", features = ["js", "wasm-bindgen"] }
[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"
wasm-bindgen = "=0.2.87"
wasm-bindgen-futures = "=0.4.37"
js-sys = "0.3"
web-sys = { version = "0.3", features = [
"console",
Expand Down
38 changes: 25 additions & 13 deletions src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;

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

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

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

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

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

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

impl FFIFriendlyBlockStore {
pub fn new(ffi_store: Box<dyn FFIStore>) -> Self {
//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

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

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

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

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

impl FFIStore for KVBlockStore {
impl<'a> FFIStore<'a> 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 All @@ -54,4 +54,4 @@ impl FFIStore for KVBlockStore {
bucket.set(&key, &value)?;
Ok(())
}
}
}
99 changes: 1 addition & 98 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,100 +1,3 @@
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
}

#[wasm_bindgen]
impl WasmPrivateDirectoryHelper {
#[wasm_bindgen(constructor)]
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)
.map_err(|e| JsValue::from_str(&e))?;

Ok(WasmPrivateDirectoryHelper {
inner: helper.0
})
}

#[wasm_bindgen]
pub fn write_file(&mut self, path: String, content: Vec<u8>, mtime: i64) -> Result<String, JsValue> {
let path_segments = PrivateDirectoryHelper::parse_path(path);
self.inner.synced_write_file(&path_segments, content, mtime)
.map(|cid| cid.to_string())
.map_err(|e| JsValue::from_str(&e))
}

#[wasm_bindgen]
pub fn read_file(&mut self, path: String) -> Result<Vec<u8>, JsValue> {
let path_segments = PrivateDirectoryHelper::parse_path(path);
self.inner.synced_read_file(&path_segments)
.map_err(|e| JsValue::from_str(&e))
}
}
pub mod private_forest;
Loading

0 comments on commit 77e56a1

Please sign in to comment.