tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] ipc fallback issues

Open thewh1teagle opened this issue 2 years ago • 1 comments

Describe the bug

Recently we merged pull/9038 to fallback to old postMessage ipc in case of errors in fetch when invoking commands. It does fallback to postMessage, and it works if the tauri command return string, but if the command return JSON (any object) it never returns and the promise never full filled.

Reproduction (windows)

Create new project with

npx tauri init

Change tauri inside Cargo.toml to use latest tauri

tauri = { git = "https://github.com/tauri-apps/tauri", branch = "dev", features = [] }

Change tauri.conf.json so it will open twitter and fallback to postMessage due to CSP errors and expose global tauri

"app": {
  "withGlobalTauri": true
},
"windows": [
      {
        "fullscreen": false,
        "height": 600,
        "resizable": true,
        "title": "app",
        "width": 800,
        "url": "https://twitter.com/",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36"
      }
    ]

Add new command which returns json

#[tauri::command]
fn test() -> serde_json::Value { // window.TAURI.core.invoke("test")
  println!("test is called!");
  serde_json::json!({})
}

Start the app using npx tauri dev Open dev tools and invoke test command window.__TAURI__.core.invoke("test")

Expected behavior

It should invoke test command, and the promise should resolve (fullfilled) with the empty object

Full tauri info output

WARNING: no lock files found, defaulting to npm

[✔️] Environment
    - OS: Windows 10.0.22631 X64
    ✔️ WebView2: 122.0.2365.92
    ✔️ MSVC: Visual Studio Community 2022
    ✔️ rustc: 1.75.0 (82e1608df 2023-12-21)
    ✔️ cargo: 1.75.0 (1d8b05cdd 2023-11-20)
    ✔️ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔️ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.17.1
    - npm: 9.6.7

[-] Packages
    - tauri [RUST]: git+https://github.com/thewh1teagle/tauri?branch=dev1#e9cb74ccf72b33d8dcf8d31ec12202bcff82dc4e (2.0.0-beta.13)
    - tauri-build [RUST]: no manifest (2.0.0-beta.10, 2.0.0-beta.10)
    - wry [RUST]: 0.37.0
    - tao [RUST]: 0.26.1
    - tauri-cli [RUST]: 1.5.9
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 2.0.0-beta.1

[-] App
    - build-type: bundle
    - CSP: unset

Stack trace

No response

Additional context

This PR added ipc fallback https://github.com/tauri-apps/tauri/pull/9038

I suspect that the issue coming from protocol.rs and it doesn't handles JSON response correctly

Update

The following patch fixed the issue for me:

# tauri - patch which fix ipc fallback to postMessage due to csp issues on external webview
# Otherwise when tauri command return json the invoke promise command doesn't resolve - by github.com/thewh1teagle
# github.com/tauri-apps/tauri

diff --git a/core/tauri/src/ipc/protocol.rs b/core/tauri/src/ipc/protocol.rs
index 4b29c372b..59eb0490a 100644
--- a/core/tauri/src/ipc/protocol.rs
+++ b/core/tauri/src/ipc/protocol.rs
@@ -313,7 +313,7 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
 
               match &response {
                 InvokeResponse::Ok(InvokeBody::Json(v)) => {
-                  if !(cfg!(target_os = "macos") || cfg!(target_os = "ios"))
+                  if !(cfg!(target_os = "macos") || cfg!(target_os = "ios") || cfg!(target_os = "windows"))
                     && matches!(v, JsonValue::Object(_) | JsonValue::Array(_))
                   {
                     let _ = Channel::from_callback_fn(webview, callback).send(v);

thewh1teagle avatar Mar 25 '24 14:03 thewh1teagle

I have encountered the same issue. Has there been any progress from the official side?

l1xnan avatar Jun 12 '24 02:06 l1xnan