Skip to content

Commit d4d6a98

Browse files
fix(core): convert js Map to object before serialization, closes #6078 (#6099)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent a822a6a commit d4d6a98

File tree

9 files changed

+84
-83
lines changed

9 files changed

+84
-83
lines changed

.changes/core-js-Map.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": "patch"
3+
---
4+
5+
Fix serialization of js `Map` when used in `invoke`.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { git = "https://github.com/tauri-apps/wry", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { version = "0.24.1", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
1818
tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
1919
uuid = { version = "1", features = [ "v4" ] }

core/tauri-runtime-wry/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,10 @@ fn decode_path(path: PathBuf) -> PathBuf {
990990
impl From<FileDropEventWrapper> for FileDropEvent {
991991
fn from(event: FileDropEventWrapper) -> Self {
992992
match event.0 {
993-
WryFileDropEvent::Hovered { paths, position: _ } => {
993+
WryFileDropEvent::Hovered(paths) => {
994994
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
995995
}
996-
WryFileDropEvent::Dropped { paths, position: _ } => {
996+
WryFileDropEvent::Dropped(paths) => {
997997
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
998998
}
999999
// default to cancelled

core/tauri-utils/src/pattern/isolation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
algorithm.iv = window.crypto.getRandomValues(new Uint8Array(12))
4444

4545
let encoder = new TextEncoder()
46-
let payloadRaw = encoder.encode(JSON.stringify(data))
46+
let payloadRaw = encoder.encode(__RAW_stringify_ipc_message_fn__(data))
4747

4848
return window.crypto.subtle
4949
.encrypt(algorithm, aesGcmKey, payloadRaw)

core/tauri-utils/src/pattern/isolation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub struct IsolationJavascriptCodegen {
141141
pub struct IsolationJavascriptRuntime<'a> {
142142
/// The key used on the Rust backend and the Isolation Javascript
143143
pub runtime_aes_gcm_key: &'a [u8; 32],
144+
/// The function that stringifies a IPC message.
145+
#[raw]
146+
pub stringify_ipc_message_fn: &'a str,
144147
}
145148

146149
#[cfg(test)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function (message) {
2+
return JSON.stringify(message, (_k, val) => {
3+
if (val instanceof Map) {
4+
let o = {};
5+
val.forEach((v, k) => o[k] = v);
6+
return o;
7+
} else {
8+
return val;
9+
}
10+
})
11+
})

core/tauri/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ impl<R: Runtime> Builder<R> {
10521052
invoke_handler: Box::new(|_| ()),
10531053
invoke_responder: Arc::new(window_invoke_responder),
10541054
invoke_initialization_script:
1055-
"Object.defineProperty(window, '__TAURI_POST_MESSAGE__', { value: (message) => window.ipc.postMessage(JSON.stringify(message)) })".into(),
1055+
format!("Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {{ value: (message) => window.ipc.postMessage({}(message)) }})", crate::manager::STRINGIFY_IPC_MESSAGE_FN),
10561056
on_page_load: Box::new(|_, _| ()),
10571057
pending_windows: Default::default(),
10581058
plugins: PluginStore::default(),

core/tauri/src/manager.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ const WINDOW_FILE_DROP_HOVER_EVENT: &str = "tauri://file-drop-hover";
7373
const WINDOW_FILE_DROP_CANCELLED_EVENT: &str = "tauri://file-drop-cancelled";
7474
const MENU_EVENT: &str = "tauri://menu";
7575

76+
pub(crate) const STRINGIFY_IPC_MESSAGE_FN: &str =
77+
include_str!("../scripts/stringify-ipc-message-fn.js");
78+
7679
#[derive(Default)]
7780
/// Spaced and quoted Content-Security-Policy hash values.
7881
struct CspHashStrings {
@@ -724,6 +727,7 @@ impl<R: Runtime> WindowManager<R> {
724727
let asset = String::from_utf8_lossy(asset.as_ref());
725728
let template = tauri_utils::pattern::isolation::IsolationJavascriptRuntime {
726729
runtime_aes_gcm_key: &aes_gcm_key,
730+
stringify_ipc_message_fn: STRINGIFY_IPC_MESSAGE_FN,
727731
};
728732
match template.render(asset.as_ref(), &Default::default()) {
729733
Ok(asset) => HttpResponseBuilder::new()

0 commit comments

Comments
 (0)