binfetch_wasm/
lib.rs

1use wasm_bindgen::JsCast;
2
3pub fn basic_fetch(uri: &str) -> Result<Vec<u8>, wasm_bindgen::JsValue> {
4    let r = web_sys::XmlHttpRequest::new()?;
5    r.open_with_async("GET", uri, false)?;
6    r.override_mime_type("text/plain; charset=x-user-defined")?;
7    r.send()?;
8    if r.status().unwrap() != 200 {
9        return Err(wasm_bindgen::JsValue::null());
10    }
11    let rs = r.response()?.dyn_into::<js_sys::JsString>()?;
12    let mut v: Vec<u8> = vec![];
13    for n in rs.iter() {
14        v.push(n.to_le_bytes()[0]);
15    }
16    Ok(v)
17}