From ad380879327b6372afc8f9649f4a9c73d79f51bc Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Wed, 31 Aug 2022 20:20:02 -0400 Subject: [PATCH] Fix some API implementation bugs --- Cargo.lock | 2 +- usdpl-back/src/instance.rs | 8 +++---- usdpl-core/src/remote_call.rs | 43 +++++++++++++++++++++++++++++++++++ usdpl-front/Cargo.toml | 4 ++-- usdpl-front/build.sh | 2 +- usdpl-front/src/connection.rs | 11 +++++++-- usdpl-front/src/lib.rs | 8 ++++++- 7 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0ae3aa..b9847ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1047,7 +1047,7 @@ dependencies = [ [[package]] name = "usdpl-front" -version = "0.6.1" +version = "0.6.2" dependencies = [ "console_error_panic_hook", "hex", diff --git a/usdpl-back/src/instance.rs b/usdpl-back/src/instance.rs index 9dc1641..f7d673f 100644 --- a/usdpl-back/src/instance.rs +++ b/usdpl-back/src/instance.rs @@ -117,10 +117,10 @@ impl Instance { .map(move |data: bytes::Bytes| { let (packet, _) = match socket::Packet::load_base64(&data) { Ok(x) => x, - Err(_) => { + Err(e) => { return warp::reply::with_status( 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(), ) } @@ -130,10 +130,10 @@ impl Instance { let response = Self::handle_call(packet, &handlers); let _len = match response.dump_base64(&mut buffer) { Ok(x) => x, - Err(_) => { + Err(e) => { return warp::reply::with_status( 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(), ) } diff --git a/usdpl-core/src/remote_call.rs b/usdpl-core/src/remote_call.rs index e9dfa92..e189304 100644 --- a/usdpl-core/src/remote_call.rs +++ b/usdpl-core/src/remote_call.rs @@ -66,3 +66,46 @@ impl Dumpable for RemoteCallResponse { 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") + } + } +} diff --git a/usdpl-front/Cargo.toml b/usdpl-front/Cargo.toml index c4ef6b9..64bb0e3 100644 --- a/usdpl-front/Cargo.toml +++ b/usdpl-front/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "usdpl-front" -version = "0.6.1" +version = "0.6.2" authors = ["NGnius (Graham) "] edition = "2021" license = "GPL-3.0-only" @@ -12,7 +12,7 @@ description = "Universal Steam Deck Plugin Library front-end designed for WASM" crate-type = ["cdylib", "rlib"] [features] -default = ["wee_alloc"] +default = [] decky = ["usdpl-core/decky"] crankshaft = ["usdpl-core/crankshaft"] debug = ["console_error_panic_hook"] diff --git a/usdpl-front/build.sh b/usdpl-front/build.sh index a4d2eba..901ed34 100755 --- a/usdpl-front/build.sh +++ b/usdpl-front/build.sh @@ -16,7 +16,7 @@ $0 [decky|crankshaft|]" fi else 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 python3 ./scripts/generate_embedded_wasm.py diff --git a/usdpl-front/src/connection.rs b/usdpl-front/src/connection.rs index 4e57fd5..2298c8b 100644 --- a/usdpl-front/src/connection.rs +++ b/usdpl-front/src/connection.rs @@ -28,8 +28,11 @@ pub async fn send_js( 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 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())); 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 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"))] - 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)? .0 { diff --git a/usdpl-front/src/lib.rs b/usdpl-front/src/lib.rs index 1ed9f10..a98ffd8 100644 --- a/usdpl-front/src/lib.rs +++ b/usdpl-front/src/lib.rs @@ -79,10 +79,16 @@ pub fn init_usdpl(port: u16) { /// Get the targeted plugin framework, or "any" if unknown #[wasm_bindgen] -pub fn target() -> String { +pub fn target_usdpl() -> 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 #[wasm_bindgen] pub fn set_value(key: String, value: JsValue) -> JsValue {