diff --git a/autonomi/README.md b/autonomi/README.md index babfa51eed..c067c97bb9 100644 --- a/autonomi/README.md +++ b/autonomi/README.md @@ -92,19 +92,6 @@ npm run serve Then go to `http://127.0.0.1:8080/tests-js` in the browser. Here, enter a `ws` multiaddr of a local node and press 'run'. -#### `index.html` - -There is also a simple `index.html` file that runs some JavaScript. - -Build the package (again with the env variables) and run a webserver, e.g. with Python: -```sh -wasm-pack build --dev --target=web autonomi -python -m http.server --directory=autonomi 8000 -``` - -Then visit `http://127.0.0.1:8000/` in your (modern) browser. - - ## Faucet (local) There is no faucet server, but instead you can use the `Deployer wallet private key` printed in the EVM node output to diff --git a/autonomi/index.html b/autonomi/index.html deleted file mode 100644 index 0353446683..0000000000 --- a/autonomi/index.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/autonomi/src/client/wasm.rs b/autonomi/src/client/wasm.rs index c12ebfc9a0..56ebca582e 100644 --- a/autonomi/src/client/wasm.rs +++ b/autonomi/src/client/wasm.rs @@ -74,38 +74,62 @@ impl JsClient { mod archive { use super::*; - use crate::client::archive::Metadata; use crate::client::{address::str_to_addr, archive::Archive}; - use std::{collections::HashMap, path::PathBuf}; - use xor_name::XorName; + use std::path::PathBuf; + + #[wasm_bindgen(js_name = Archive)] + pub struct JsArchive(Archive); + + #[wasm_bindgen(js_class = Archive)] + impl JsArchive { + #[wasm_bindgen(constructor)] + pub fn new() -> Self { + Self(Archive::new()) + } + + #[wasm_bindgen(js_name = addNewFile)] + pub fn add_new_file(&mut self, path: String, data_addr: String) -> Result<(), JsError> { + let path = PathBuf::from(path); + let data_addr = str_to_addr(&data_addr)?; + self.0.add_new_file(path, data_addr); + + Ok(()) + } + + #[wasm_bindgen(js_name = renameFile)] + pub fn rename_file(&mut self, old_path: String, new_path: String) -> Result<(), JsError> { + let old_path = PathBuf::from(old_path); + let new_path = PathBuf::from(new_path); + self.0.rename_file(&old_path, &new_path)?; + + Ok(()) + } + + #[wasm_bindgen] + pub fn map(&self) -> Result { + let files = serde_wasm_bindgen::to_value(self.0.map())?; + Ok(files) + } + } #[wasm_bindgen(js_class = Client)] impl JsClient { #[wasm_bindgen(js_name = archiveGet)] - pub async fn archive_get(&self, addr: String) -> Result { + pub async fn archive_get(&self, addr: String) -> Result { let addr = str_to_addr(&addr)?; - let data = self.0.archive_get(addr).await?; + let archive = self.0.archive_get(addr).await?; + let archive = JsArchive(archive); - // To `Map` (JS) - let data = serde_wasm_bindgen::to_value(&data.map())?; - Ok(data.into()) + Ok(archive) } #[wasm_bindgen(js_name = archivePut)] pub async fn archive_put( &self, - map: JsValue, + archive: &JsArchive, wallet: &JsWallet, ) -> Result { - // From `Map` or `Iterable<[K, V]>` (JS) - let map: HashMap = serde_wasm_bindgen::from_value(map)?; - let mut archive = Archive::new(); - - for (path, (xorname, meta)) in map { - archive.add_file(path, xorname, meta); - } - - let addr = self.0.archive_put(archive, &wallet.0).await?; + let addr = self.0.archive_put(archive.0.clone(), &wallet.0).await?; Ok(addr_to_str(addr)) } diff --git a/autonomi/tests-js/index.js b/autonomi/tests-js/index.js index 0e5edacf55..a44ae3892c 100644 --- a/autonomi/tests-js/index.js +++ b/autonomi/tests-js/index.js @@ -44,7 +44,8 @@ describe('autonomi', function () { it('puts data, creates archive and retrieves it', async () => { const data = randomData(32); const addr = await client.dataPut(data, wallet); - const archive = new Map([["foo", addr]]); + const archive = new atnm.Archive(); + archive.addNewFile("foo", addr); const archiveAddr = await client.archivePut(archive, wallet); const archiveFetched = await client.archiveGet(archiveAddr); diff --git a/autonomi/tests-js/package.json b/autonomi/tests-js/package.json index b33e6d0e30..6da24b1037 100644 --- a/autonomi/tests-js/package.json +++ b/autonomi/tests-js/package.json @@ -1,7 +1,7 @@ { "type": "module", "scripts": { - "serve": "http-server -a 127.0.0.1 ../" + "serve": "http-server -c-1 -a 127.0.0.1 ../" }, "devDependencies": { "chai": "^5.1.1", diff --git a/autonomi/tests/wasm.rs b/autonomi/tests/wasm.rs index 485193ea48..8f27576f06 100644 --- a/autonomi/tests/wasm.rs +++ b/autonomi/tests/wasm.rs @@ -21,15 +21,15 @@ wasm_bindgen_test_configure!(run_in_browser); async fn put() -> Result<(), Box> { enable_logging_wasm("sn_networking,autonomi,wasm"); - let client = Client::connect(&peers_from_env()?).await.unwrap(); + let client = Client::connect(&peers_from_env()?).await?; let wallet = get_funded_wallet(); + let data = gen_random_data(1024 * 1024 * 10); - let data = gen_random_data(1024 * 1024 * 2); // 2MiB - let addr = client.put(data.clone(), &wallet).await.unwrap(); + let addr = client.data_put(data.clone(), &wallet).await?; - sleep(Duration::from_secs(2)).await; + sleep(Duration::from_secs(10)).await; - let data_fetched = client.get(addr).await.unwrap(); + let data_fetched = client.data_get(addr).await?; assert_eq!(data, data_fetched, "data fetched should match data put"); Ok(())