diff --git a/wnfs-wasm/playwright.config.ts b/wnfs-wasm/playwright.config.ts index ce14e400..02a3af8e 100644 --- a/wnfs-wasm/playwright.config.ts +++ b/wnfs-wasm/playwright.config.ts @@ -68,7 +68,7 @@ const config: PlaywrightTestConfig = { /* Run your local dev server before starting the tests */ webServer: { - command: "cd tests/server && npx webpack && npx http-server -p 8085 dist", + command: "cd tests/server && npx webpack && npx http-server -- -p 8085 dist", port: 8085, timeout: 10 * 60 * 1000, // 10 minutes }, diff --git a/wnfs-wasm/src/fs/public/file.rs b/wnfs-wasm/src/fs/public/file.rs index 58c95643..5f60a2c7 100644 --- a/wnfs-wasm/src/fs/public/file.rs +++ b/wnfs-wasm/src/fs/public/file.rs @@ -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 { + 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( diff --git a/wnfs-wasm/tests/public.spec.ts b/wnfs-wasm/tests/public.spec.ts index 5c16be44..8d1d605c 100644 --- a/wnfs-wasm/tests/public.spec.ts +++ b/wnfs-wasm/tests/public.spec.ts @@ -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"); + }); }); diff --git a/wnfs/src/public/file.rs b/wnfs/src/public/file.rs index 1dfc6e3d..b157b014 100644 --- a/wnfs/src/public/file.rs +++ b/wnfs/src/public/file.rs @@ -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 = self.userland.resolve_cid(store).await; + content_cid.unwrap() + } + /// Gets the previous value of the file. /// /// # Examples