-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathwasm.rs
More file actions
83 lines (70 loc) · 2.63 KB
/
wasm.rs
File metadata and controls
83 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use anyhow::Result;
use bytes::Bytes;
use iroh_blobs::{ticket::BlobTicket, Hash};
use js_sys::Uint8Array;
use tracing::level_filters::LevelFilter;
use tracing_subscriber_wasm::MakeConsoleWriter;
use wasm_bindgen::{prelude::wasm_bindgen, JsError};
#[wasm_bindgen(start)]
fn start() {
console_error_panic_hook::set_once();
tracing_subscriber::fmt()
.with_max_level(LevelFilter::TRACE)
.with_writer(
// To avoide trace events in the browser from showing their JS backtrace
MakeConsoleWriter::default().map_trace_level_to(tracing::Level::DEBUG),
)
// If we don't do this in the browser, we get a runtime error.
.without_time()
.with_ansi(false)
.init();
tracing::info!("(testing logging) Logging setup");
}
#[wasm_bindgen]
pub struct BlobsNode(crate::BlobsNode);
#[wasm_bindgen]
impl BlobsNode {
pub async fn spawn() -> Result<Self, JsError> {
Ok(Self(crate::BlobsNode::spawn().await.map_err(to_js_err)?))
}
pub fn endpoint_id(&self) -> String {
self.0.endpoint_id().to_string()
}
pub async fn import(&self, data: Uint8Array) -> Result<String, JsError> {
let data = uint8array_to_bytes(&data);
tracing::info!("importing data of len {}", data.len());
let ticket = self.0.import(data).await.map_err(to_js_err)?;
Ok(ticket.to_string())
}
pub async fn download(&self, ticket: String) -> Result<String, JsError> {
let ticket: BlobTicket = ticket.parse().map_err(to_js_err)?;
let hash = self.0.download(ticket).await.map_err(to_js_err)?;
Ok(hash.to_string())
}
pub async fn complete_size(&self, hash: String) -> Result<u64, JsError> {
let hash: Hash = hash.parse().map_err(to_js_err)?;
let size = self.0.complete_size(hash).await.map_err(to_js_err)?;
Ok(size)
}
pub async fn get(&self, hash: String) -> Result<Uint8Array, JsError> {
let hash: Hash = hash.parse().map_err(to_js_err)?;
let bytes = self.0.blobs.get_bytes(hash).await?;
Ok(bytes_to_uint8array(&bytes))
}
}
fn to_js_err(err: impl Into<anyhow::Error>) -> JsError {
let err: anyhow::Error = err.into();
JsError::new(&err.to_string())
}
pub fn uint8array_to_bytes(data: &Uint8Array) -> Bytes {
let mut buffer = vec![0u8; data.length() as usize];
data.copy_to(&mut buffer[..]);
Bytes::from(buffer)
}
pub fn bytes_to_uint8array(bytes: &[u8]) -> Uint8Array {
// Create a Uint8Array with the same length
let array = Uint8Array::new_with_length(bytes.len() as u32);
// Copy the bytes into the JS Uint8Array
array.copy_from(bytes);
array
}