Skip to content

Commit

Permalink
feat(api): adds missing metadata functions for the private side (#144)
Browse files Browse the repository at this point in the history
* feat: Add missing PrivateFile metadata functions

* feat: Add missing PrivateDirectory metadata functions

* test: Add tests for new private metadata functions

* chore: Reorganise imports
  • Loading branch information
icidasset authored Jan 16, 2023
1 parent f2fa3d9 commit 7588f69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
16 changes: 14 additions & 2 deletions wnfs-wasm/src/fs/private/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use js_sys::{Array, Date, Promise, Uint8Array};
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;
use wnfs::{
Id, PrivateDirectory as WnfsPrivateDirectory, PrivateOpResult as WnfsPrivateOpResult,
HASH_BYTE_SIZE,
Id, PrivateDirectory as WnfsPrivateDirectory, PrivateNode as WnfsPrivateNode,
PrivateOpResult as WnfsPrivateOpResult, HASH_BYTE_SIZE,
};

use crate::{
fs::{
metadata::JsMetadata,
utils::{self, error},
BlockStore, ForeignBlockStore, JsResult, Namefilter, PrivateForest, PrivateNode, Rng,
},
Expand Down Expand Up @@ -387,6 +388,17 @@ impl PrivateDirectory {
}))
}

/// 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) -> PrivateNode {
PrivateNode(WnfsPrivateNode::Dir(Rc::clone(&self.0)))
}

/// Gets a unique id for node.
#[wasm_bindgen(js_name = "getId")]
pub fn get_id(&self) -> String {
Expand Down
6 changes: 6 additions & 0 deletions wnfs-wasm/src/fs/private/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::{
fs::{
metadata::JsMetadata,
utils::{self, error},
BlockStore, ForeignBlockStore, JsResult, Namefilter, PrivateForest, Rng,
},
Expand Down Expand Up @@ -88,6 +89,11 @@ impl PrivateFile {
}))
}

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

/// Gets a unique id for node.
#[wasm_bindgen(js_name = "getId")]
pub fn get_id(&self) -> String {
Expand Down
28 changes: 28 additions & 0 deletions wnfs-wasm/tests/private.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,32 @@ test.describe("PrivateFile", () => {
expect(length).toEqual(5);
expect(type).toEqual("Uint8Array");
});

test("A PrivateDirectory has the correct metadata", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
wnfs: { Namefilter, PrivateDirectory },
mock: { Rng },
} = await window.setup();

const time = new Date();
return new PrivateDirectory(new Namefilter(), time, new Rng()).metadata();
});

expect(result.created).not.toBeUndefined();
});

test("A PrivateFile has the correct metadata", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
wnfs: { Namefilter, PrivateFile },
mock: { Rng }
} = await window.setup();

const time = new Date();
return new PrivateFile(new Namefilter(), time, new Rng()).metadata();
});

expect(result.created).not.toBeUndefined();
});
});
5 changes: 5 additions & 0 deletions wnfs/src/private/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ impl PrivateFile {
})
}

/// Gets the metadata of the file
pub fn get_metadata(&self) -> &Metadata {
&self.metadata
}

/// Gets the entire content of a file.
///
/// # Examples
Expand Down

0 comments on commit 7588f69

Please sign in to comment.