Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): inject invoke key in custom invoke system script #11025

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/custom-invoke-system-invoke-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Inject `__INVOKE_KEY__` into custom invoke systems so their implementations can properly construct `tauri::webview::InvokeRequest`.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,12 +1316,27 @@ impl<R: Runtime> Builder<R> {
///
/// The `initialization_script` is a script that initializes `window.__TAURI_INTERNALS__.postMessage`.
/// That function must take the `(message: object, options: object)` arguments and send it to the backend.
///
/// Additionally, the script must include a `__INVOKE_KEY__` token that is replaced with a value that must be sent with the IPC payload
/// to check the integrity of the message by the [`crate::WebviewWindow::on_message`] API, e.g.
///
/// ```js
/// const invokeKey = __INVOKE_KEY__;
/// fetch('my-impl://command', {
/// headers: {
/// 'Tauri-Invoke-Key': invokeKey,
/// }
/// })
/// ```
///
/// Note that the implementation details is up to your implementation.
#[must_use]
pub fn invoke_system<F>(mut self, initialization_script: String, responder: F) -> Self
where
F: Fn(&Webview<R>, &str, &InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
{
self.invoke_initialization_script = initialization_script;
self.invoke_initialization_script =
initialization_script.replace("__INVOKE_KEY__", &format!("\"{}\"", self.invoke_key));
self.invoke_responder.replace(Arc::new(responder));
self
}
Expand Down
4 changes: 1 addition & 3 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ impl<'a> PageLoadPayload<'a> {
/// # Stability
///
/// This struct is **NOT** part of the public stable API and is only meant to be used
/// by internal code and external testing/fuzzing tools. If not used with feature `unstable`, this
/// struct is marked `#[non_exhaustive]` and is non-constructable externally.
/// by internal code and external testing/fuzzing tools or custom invoke systems.
#[derive(Debug)]
#[cfg_attr(not(feature = "test"), non_exhaustive)]
pub struct InvokeRequest {
/// The invoke command.
pub cmd: String,
Expand Down
Loading