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

feat: add get_raw_content_cid #385

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions wnfs-wasm/src/fs/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ impl PublicFile {
JsMetadata(self.0.get_metadata()).try_into()
}

/// Gets the content cid of the file.
#[wasm_bindgen(js_name = "getRawContentCid")]
pub fn get_raw_content_cid(&self, store: BlockStore) -> JsResult<Promise> {
let mut file = Rc::clone(&self.0);
let store = ForeignBlockStore(store);

Ok(future_to_promise(async move {
let content_cid: Cid = file.get_raw_content_cid(&store).await;
Ok(value!(Uint8Array::from(&content_cid.to_bytes()[..])))
}))
}

/// Gets the content of the file at given offset & with an optional byte limit.
#[wasm_bindgen(js_name = "readAt")]
pub fn read_at(
Expand Down
22 changes: 22 additions & 0 deletions wnfs-wasm/tests/public.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,26 @@ test.describe("PublicDirectory", () => {

expect(result).toEqual(longString);
});

test("A PublicFile has a content CID", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
wnfs: { PublicFile },
mock: { CID, MemoryBlockStore },
} = await window.setup();

const store = new MemoryBlockStore();
const time = new Date();
const file = new PublicFile(time);

const content = new TextEncoder().encode("hello");
const file2 = await file.setContent(time, content, store);

const cid_bytes = await file2.getRawContentCid(store);
return cid_bytes ? CID.decode(cid_bytes).toV1().toString() : undefined;
});

expect(result).not.toBeUndefined();
expect(result).toEqual("bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq");
});
});
6 changes: 6 additions & 0 deletions wnfs/src/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ impl PublicFile {
Ok(())
}

/// Gets the content cid of the file.
pub async fn get_raw_content_cid(&self, store: &impl BlockStore) -> Cid {
let content_cid: Result<Cid> = self.userland.resolve_cid(store).await;
content_cid.unwrap()
}

/// Gets the previous value of the file.
///
/// # Examples
Expand Down
Loading