Fix some API implementation bugs

This commit is contained in:
NGnius (Graham) 2022-08-31 20:20:02 -04:00
parent 2918f6cd3b
commit ad38087932
7 changed files with 67 additions and 11 deletions

2
Cargo.lock generated
View file

@ -1047,7 +1047,7 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-front" name = "usdpl-front"
version = "0.6.1" version = "0.6.2"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"hex", "hex",

View file

@ -117,10 +117,10 @@ impl Instance {
.map(move |data: bytes::Bytes| { .map(move |data: bytes::Bytes| {
let (packet, _) = match socket::Packet::load_base64(&data) { let (packet, _) = match socket::Packet::load_base64(&data) {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(e) => {
return warp::reply::with_status( return warp::reply::with_status(
warp::http::Response::builder() warp::http::Response::builder()
.body("Failed to load packet".to_string()), .body(format!("Failed to load packet: {}", e)),
warp::http::StatusCode::from_u16(400).unwrap(), warp::http::StatusCode::from_u16(400).unwrap(),
) )
} }
@ -130,10 +130,10 @@ impl Instance {
let response = Self::handle_call(packet, &handlers); let response = Self::handle_call(packet, &handlers);
let _len = match response.dump_base64(&mut buffer) { let _len = match response.dump_base64(&mut buffer) {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(e) => {
return warp::reply::with_status( return warp::reply::with_status(
warp::http::Response::builder() warp::http::Response::builder()
.body("Failed to dump response packet".to_string()), .body(format!("Failed to dump response packet: {}", e)),
warp::http::StatusCode::from_u16(500).unwrap(), warp::http::StatusCode::from_u16(500).unwrap(),
) )
} }

View file

@ -66,3 +66,46 @@ impl Dumpable for RemoteCallResponse {
Ok(len0 + len1) Ok(len0 + len1)
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remote_call_idempotence_test() {
let call = RemoteCall{
id: 42,
function: "something very long just in case this causes unexpected issues".into(),
parameters: vec!["param1".into(), 42f64.into()],
};
let mut buffer = String::with_capacity(crate::socket::PACKET_BUFFER_SIZE);
let len = call.dump_base64(&mut buffer).unwrap();
println!("base64 dumped: `{}` (len: {})", buffer, len);
let (loaded_call, loaded_len) = RemoteCall::load_base64(buffer.as_bytes()).unwrap();
assert_eq!(len, loaded_len, "Expected load and dump lengths to match");
assert_eq!(loaded_call.id, call.id, "RemoteCall.id does not match");
assert_eq!(loaded_call.function, call.function, "RemoteCall.function does not match");
if let Primitive::String(loaded) = &loaded_call.parameters[0] {
if let Primitive::String(original) = &call.parameters[0] {
assert_eq!(loaded, original, "RemoteCall.parameters[0] does not match");
} else {
panic!("Original call parameter 0 is not String")
}
} else {
panic!("Loaded call parameter 0 is not String")
}
if let Primitive::F64(loaded) = &loaded_call.parameters[1] {
if let Primitive::F64(original) = &call.parameters[1] {
assert_eq!(loaded, original, "RemoteCall.parameters[1] does not match");
} else {
panic!("Original call parameter 1 is not f64")
}
} else {
panic!("Loaded call parameter 1 is not f64")
}
}
}

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-front" name = "usdpl-front"
version = "0.6.1" version = "0.6.2"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"] authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
@ -12,7 +12,7 @@ description = "Universal Steam Deck Plugin Library front-end designed for WASM"
crate-type = ["cdylib", "rlib"] crate-type = ["cdylib", "rlib"]
[features] [features]
default = ["wee_alloc"] default = []
decky = ["usdpl-core/decky"] decky = ["usdpl-core/decky"]
crankshaft = ["usdpl-core/crankshaft"] crankshaft = ["usdpl-core/crankshaft"]
debug = ["console_error_panic_hook"] debug = ["console_error_panic_hook"]

View file

@ -16,7 +16,7 @@ $0 [decky|crankshaft|<nothing>]"
fi fi
else else
echo "WARNING: Building for any plugin framework, which may not work for every framework" echo "WARNING: Building for any plugin framework, which may not work for every framework"
RUSTFLAGS="--cfg aes_compact" wasm-pack build --target web RUSTFLAGS="--cfg aes_compact" wasm-pack build --target web --features debug
fi fi
python3 ./scripts/generate_embedded_wasm.py python3 ./scripts/generate_embedded_wasm.py

View file

@ -28,8 +28,11 @@ pub async fn send_js(
let url = format!("http://{}:{}/usdpl/call", socket::HOST_STR, port); let url = format!("http://{}:{}/usdpl/call", socket::HOST_STR, port);
#[allow(unused_variables)]
let (buffer, len) = dump_to_buffer(packet, #[cfg(feature = "encrypt")] key.as_slice())?; let (buffer, len) = dump_to_buffer(packet, #[cfg(feature = "encrypt")] key.as_slice())?;
let string: String = String::from_utf8_lossy(&buffer.as_slice()[..len]).into(); let string: String = String::from_utf8_lossy(buffer.as_slice()).into();
#[cfg(feature="debug")]
crate::imports::console_log(&format!("Dumped base64 `{}` len:{}", string, len));
opts.body(Some(&string.into())); opts.body(Some(&string.into()));
let request = Request::new_with_str_and_init(&url, &opts)?; let request = Request::new_with_str_and_init(&url, &opts)?;
@ -44,8 +47,12 @@ pub async fn send_js(
let text = JsFuture::from(resp.text()?).await?; let text = JsFuture::from(resp.text()?).await?;
let string: JsString = text.dyn_into()?; let string: JsString = text.dyn_into()?;
let rust_str = string.as_string().unwrap();
#[cfg(feature="debug")]
crate::imports::console_log(&format!("Received base64 `{}` len:{}", rust_str, rust_str.len()));
#[cfg(not(feature = "encrypt"))] #[cfg(not(feature = "encrypt"))]
match socket::Packet::load_base64(string.as_string().unwrap().as_bytes()) match socket::Packet::load_base64(rust_str.as_bytes())
.map_err(super::convert::str_to_js)? .map_err(super::convert::str_to_js)?
.0 .0
{ {

View file

@ -79,10 +79,16 @@ pub fn init_usdpl(port: u16) {
/// Get the targeted plugin framework, or "any" if unknown /// Get the targeted plugin framework, or "any" if unknown
#[wasm_bindgen] #[wasm_bindgen]
pub fn target() -> String { pub fn target_usdpl() -> String {
usdpl_core::api::Platform::current().to_string() usdpl_core::api::Platform::current().to_string()
} }
/// Get the UDSPL front-end version
#[wasm_bindgen]
pub fn version_usdpl() -> String {
env!("CARGO_PKG_VERSION").into()
}
/// Get the targeted plugin framework, or "any" if unknown /// Get the targeted plugin framework, or "any" if unknown
#[wasm_bindgen] #[wasm_bindgen]
pub fn set_value(key: String, value: JsValue) -> JsValue { pub fn set_value(key: String, value: JsValue) -> JsValue {