-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwasm.rs
76 lines (64 loc) · 2.29 KB
/
wasm.rs
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
use anyhow::{Context, Result};
use n0_future::{Stream, StreamExt};
use serde::Serialize;
use tracing::level_filters::LevelFilter;
use tracing_subscriber_wasm::MakeConsoleWriter;
use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue};
use wasm_streams::readable::sys::ReadableStream as JsReadableStream;
use wasm_streams::ReadableStream;
use crate::node;
#[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 EchoNode(node::EchoNode);
#[wasm_bindgen]
impl EchoNode {
pub async fn spawn() -> Result<Self, JsError> {
Ok(Self(node::EchoNode::spawn().await.map_err(to_js_err)?))
}
pub fn events(&self) -> JsReadableStream {
let stream = self.0.accept_events();
into_js_readable_stream(stream)
}
pub fn node_id(&self) -> String {
self.0.endpoint().node_id().to_string()
}
pub fn connect(&self, node_id: String, payload: String) -> Result<JsReadableStream, JsError> {
let node_id = node_id
.parse()
.context("failed to parse node id")
.map_err(to_js_err)?;
let stream = self.0.connect(node_id, payload);
Ok(into_js_readable_stream(stream))
}
pub fn remote_info(&self) -> Vec<JsValue> {
self.0
.endpoint()
.remote_info_iter()
.map(|info| serde_wasm_bindgen::to_value(&info).unwrap())
.collect::<Vec<_>>()
}
}
fn to_js_err(err: impl Into<anyhow::Error>) -> JsError {
let err: anyhow::Error = err.into();
JsError::new(&err.to_string())
}
fn into_js_readable_stream<T: Serialize>(
stream: impl Stream<Item = T> + 'static,
) -> wasm_streams::readable::sys::ReadableStream {
let stream = stream.map(|event| Ok(serde_wasm_bindgen::to_value(&event).unwrap()));
ReadableStream::from_stream(stream).into_raw()
}