Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more JS APIs #44

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ impl PublicDirectory {
self.previous
}

/// Gets the metadata of the directory
pub fn get_metadata<'a>(self: &'a Rc<Self>) -> &'a Metadata {
&self.metadata
}

/// Gets the directory nodes along specified path.
///
/// Supports cases where the entire path does not exist.
Expand Down
12 changes: 11 additions & 1 deletion crates/fs/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ impl PublicFile {
}
}

// Gets the previous value of the file.
/// Gets the previous value of the file.
pub fn get_previous(self: &Rc<Self>) -> Option<Cid> {
self.previous
}

/// Gets the metadata of the file
pub fn get_metadata<'a>(self: &'a Rc<Self>) -> &'a Metadata {
&self.metadata
}

/// Gets the content cid of a file
pub fn get_content_cid(self: &Rc<Self>) -> Cid {
self.userland
}

/// Stores file in provided block store.
///
/// # Examples
Expand Down
5 changes: 5 additions & 0 deletions crates/fs/public/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ impl PublicNode {
matches!(self, Self::Dir(_))
}

/// Returns true if the underlying node is a file.
pub fn is_file(&self) -> bool {
matches!(self, Self::File(_))
}

/// Gets the node kind.
pub fn kind(&self) -> UnixFsNodeKind {
match self {
Expand Down
19 changes: 18 additions & 1 deletion crates/wasm/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use wnfs::{
BlockStore as WnfsBlockStore, Id,
};

use crate::fs::{BlockStore, ForeignBlockStore, JsResult, PublicNode};
use crate::fs::{metadata::JsMetadata, BlockStore, ForeignBlockStore, JsResult, PublicNode};
use crate::value;

/// A directory in a WNFS public file system.
Expand Down Expand Up @@ -252,6 +252,23 @@ impl PublicDirectory {
}))
}

/// Gets the previous cid of the directory or null if it doesn't have a previous cid.
#[wasm_bindgen(js_name = "previousCid")]
pub fn previous_cid(&self) -> JsValue {
match self.0.get_previous() {
Some(cid) => {
let cid_u8array = Uint8Array::from(&cid.to_bytes()[..]);
value!(cid_u8array)
}
None => JsValue::NULL,
}
}

/// Gets the metadata of the directory
pub fn metadata(&self) -> JsResult<JsValue> {
JsMetadata(self.0.get_metadata()).try_into()
}

/// Converts directory to a node.
#[wasm_bindgen(js_name = "asNode")]
pub fn as_node(&self) -> PublicNode {
Expand Down
70 changes: 64 additions & 6 deletions crates/wasm/fs/public/file.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! The bindgen API for PublicFile.

use crate::{fs::metadata::JsMetadata, value};
use chrono::{DateTime, Utc};
use js_sys::{Error, Uint8Array};
use wasm_bindgen::prelude::wasm_bindgen;
use wnfs::{ipld::Cid, public::PublicFile as WnfsPublicFile, Id};
use js_sys::{Error, Promise, Uint8Array};
use std::rc::Rc;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;
use wnfs::{ipld::Cid, public::PublicFile as WnfsPublicFile, BlockStore as WnfsBlockStore, Id};

use crate::fs::JsResult;
use crate::fs::{BlockStore, ForeignBlockStore, JsResult};

/// A file in a WNFS public file system.
#[wasm_bindgen]
pub struct PublicFile(WnfsPublicFile);
pub struct PublicFile(pub(crate) Rc<WnfsPublicFile>);

#[wasm_bindgen]
impl PublicFile {
Expand All @@ -21,12 +24,67 @@ impl PublicFile {
let cid = Cid::try_from(&cid.to_vec()[..])
.map_err(|e| Error::new(&format!("Invalid CID: {e}")))?;

Ok(PublicFile(WnfsPublicFile::new(time, cid)))
Ok(PublicFile(Rc::new(WnfsPublicFile::new(time, cid))))
}

/// Gets a unique id for node.
#[wasm_bindgen(js_name = "getId")]
pub fn get_id(&self) -> String {
self.0.get_id()
}

/// Stores a file in provided block store.
pub fn store(&self, store: BlockStore) -> JsResult<Promise> {
let file = Rc::clone(&self.0);
let mut store = ForeignBlockStore(store);

Ok(future_to_promise(async move {
let cid = file
.store(&mut store)
.await
.map_err(|e| Error::new(&format!("Cannot add to store: {e}")))?;

let cid_u8array = Uint8Array::from(&cid.to_bytes()[..]);

Ok(value!(cid_u8array))
}))
}

/// Loads a file given its CID from the block store.
pub fn load(cid: Uint8Array, store: BlockStore) -> JsResult<Promise> {
let store = ForeignBlockStore(store);
let cid = Cid::read_bytes(cid.to_vec().as_slice())
.map_err(|e| Error::new(&format!("Cannot parse cid: {e}")))?;
matheus23 marked this conversation as resolved.
Show resolved Hide resolved
Ok(future_to_promise(async move {
let file: WnfsPublicFile = store
.get_deserializable(&cid)
.await
.map_err(|e| Error::new(&format!("Couldn't deserialize directory: {e}")))?;

Ok(value!(PublicFile(Rc::new(file))))
}))
}

/// Gets the previous cid of the file or null if the file doesn't have a previous cid.
#[wasm_bindgen(js_name = "previousCid")]
pub fn previous_cid(&self) -> JsValue {
match self.0.get_previous() {
Some(cid) => {
let cid_u8array = Uint8Array::from(&cid.to_bytes()[..]);
value!(cid_u8array)
}
None => JsValue::NULL,
}
}

/// Gets the metadata of this file.
pub fn metadata(&self) -> JsResult<JsValue> {
JsMetadata(self.0.get_metadata()).try_into()
}

/// Gets the content cid of the file.
#[wasm_bindgen(js_name = "contentCid")]
pub fn content_cid(&self) -> Uint8Array {
Uint8Array::from(&self.0.get_content_cid().to_bytes()[..])
}
matheus23 marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 16 additions & 1 deletion crates/wasm/fs/public/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use js_sys::Error;
use wasm_bindgen::prelude::wasm_bindgen;
use wnfs::{public::PublicNode as WnfsPublicNode, Id};

use crate::fs::{JsResult, PublicDirectory};
use crate::fs::{JsResult, PublicDirectory, PublicFile};

/// Wraps a wnfs PublicNode.
#[wasm_bindgen]
Expand All @@ -20,11 +20,26 @@ impl PublicNode {
Ok(PublicDirectory(dir))
}

#[wasm_bindgen(js_name = "asFile")]
pub fn as_file(&self) -> JsResult<PublicFile> {
let file = self
.0
.as_file()
.map_err(|e| Error::new(&format!("Cannot cast to a file: {e}")))?;

Ok(PublicFile(file))
}

#[wasm_bindgen(js_name = "isDir")]
pub fn is_dir(&self) -> bool {
self.0.is_dir()
}

#[wasm_bindgen(js_name = "isFile")]
pub fn is_file(&self) -> bool {
self.0.is_file()
}

#[wasm_bindgen(js_name = "getId")]
pub fn get_id(&self) -> String {
self.0.get_id()
Expand Down