Skip to content

Commit e7fd676

Browse files
authored
feat(core): inject invoke key in custom invoke system script (#11025)
* feat(core): inject invoke key in custom invoke system script * fix fmt
1 parent e266f2f commit e7fd676

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Inject `__INVOKE_KEY__` into custom invoke systems so their implementations can properly construct `tauri::webview::InvokeRequest`.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/src/app.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,12 +1316,27 @@ impl<R: Runtime> Builder<R> {
13161316
///
13171317
/// The `initialization_script` is a script that initializes `window.__TAURI_INTERNALS__.postMessage`.
13181318
/// That function must take the `(message: object, options: object)` arguments and send it to the backend.
1319+
///
1320+
/// Additionally, the script must include a `__INVOKE_KEY__` token that is replaced with a value that must be sent with the IPC payload
1321+
/// to check the integrity of the message by the [`crate::WebviewWindow::on_message`] API, e.g.
1322+
///
1323+
/// ```js
1324+
/// const invokeKey = __INVOKE_KEY__;
1325+
/// fetch('my-impl://command', {
1326+
/// headers: {
1327+
/// 'Tauri-Invoke-Key': invokeKey,
1328+
/// }
1329+
/// })
1330+
/// ```
1331+
///
1332+
/// Note that the implementation details is up to your implementation.
13191333
#[must_use]
13201334
pub fn invoke_system<F>(mut self, initialization_script: String, responder: F) -> Self
13211335
where
13221336
F: Fn(&Webview<R>, &str, &InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
13231337
{
1324-
self.invoke_initialization_script = initialization_script;
1338+
self.invoke_initialization_script =
1339+
initialization_script.replace("__INVOKE_KEY__", &format!("\"{}\"", self.invoke_key));
13251340
self.invoke_responder.replace(Arc::new(responder));
13261341
self
13271342
}

crates/tauri/src/webview/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ impl<'a> PageLoadPayload<'a> {
115115
/// # Stability
116116
///
117117
/// This struct is **NOT** part of the public stable API and is only meant to be used
118-
/// by internal code and external testing/fuzzing tools. If not used with feature `unstable`, this
119-
/// struct is marked `#[non_exhaustive]` and is non-constructable externally.
118+
/// by internal code and external testing/fuzzing tools or custom invoke systems.
120119
#[derive(Debug)]
121-
#[cfg_attr(not(feature = "test"), non_exhaustive)]
122120
pub struct InvokeRequest {
123121
/// The invoke command.
124122
pub cmd: String,

0 commit comments

Comments
 (0)