Skip to content

Commit

Permalink
Merge pull request maidsafe#2267 from b-zee/feat-archive-wasm
Browse files Browse the repository at this point in the history
feat archive wasm
  • Loading branch information
b-zee authored Oct 18, 2024
2 parents f5b6b24 + 0fc6f23 commit ce6104a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 113 deletions.
13 changes: 0 additions & 13 deletions autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 0 additions & 75 deletions autonomi/index.html

This file was deleted.

60 changes: 42 additions & 18 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue, JsError> {
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<js_sys::Map, JsError> {
pub async fn archive_get(&self, addr: String) -> Result<JsArchive, JsError> {
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<K, V>` (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<String, JsError> {
// From `Map<K, V>` or `Iterable<[K, V]>` (JS)
let map: HashMap<PathBuf, (XorName, Metadata)> = 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))
}
Expand Down
3 changes: 2 additions & 1 deletion autonomi/tests-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion autonomi/tests-js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 5 additions & 5 deletions autonomi/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ wasm_bindgen_test_configure!(run_in_browser);
async fn put() -> Result<(), Box<dyn std::error::Error>> {
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(())
Expand Down

0 comments on commit ce6104a

Please sign in to comment.