diff --git a/.changes/assets-iter-cow.md b/.changes/assets-iter-cow.md new file mode 100644 index 000000000..43249dfa4 --- /dev/null +++ b/.changes/assets-iter-cow.md @@ -0,0 +1,6 @@ +--- +"tauri-utils": patch:breaking +"tauri": patch:breaking +--- + +The `Assets::iter` function now must return a iterator with `Item = (Cow<'_, str>, Cow<'_, [u8]>)` to be more flexible on contexts where the assets are not `'static`. diff --git a/crates/tauri-utils/src/assets.rs b/crates/tauri-utils/src/assets.rs index 4e85bf56c..24cc5c08b 100644 --- a/crates/tauri-utils/src/assets.rs +++ b/crates/tauri-utils/src/assets.rs @@ -12,6 +12,9 @@ use std::{ path::{Component, Path}, }; +/// Assets iterator. +pub type AssetsIter<'a> = dyn Iterator, Cow<'a, [u8]>)> + 'a; + /// Represent an asset file path in a normalized way. /// /// The following rules are enforced and added if needed: @@ -155,8 +158,13 @@ impl EmbeddedAssets { } /// Iterate on the assets. - pub fn iter(&self) -> Box + '_> { - Box::new(self.assets.into_iter().map(|(k, b)| (*k, *b))) + pub fn iter(&self) -> Box> { + Box::new( + self + .assets + .into_iter() + .map(|(k, b)| (Cow::Borrowed(*k), Cow::Borrowed(*b))), + ) } /// CSP hashes for the given asset. diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 90c5a1cda..bb6ff229c 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -37,7 +37,7 @@ use tauri_runtime::{ window::DragDropEvent, RuntimeInitArgs, }; -use tauri_utils::PackageInfo; +use tauri_utils::{assets::AssetsIter, PackageInfo}; use serde::Serialize; use std::{ @@ -309,7 +309,7 @@ impl AssetResolver { } /// Iterate on all assets. - pub fn iter(&self) -> Box + '_> { + pub fn iter(&self) -> Box> { self.manager.assets.iter() } } diff --git a/crates/tauri/src/ipc/protocol.rs b/crates/tauri/src/ipc/protocol.rs index 0348d5015..23563251b 100644 --- a/crates/tauri/src/ipc/protocol.rs +++ b/crates/tauri/src/ipc/protocol.rs @@ -17,7 +17,6 @@ use http::{ }, HeaderValue, Method, Request, StatusCode, }; -use mime::APPLICATION_OCTET_STREAM; use url::Url; use super::{CallbackFn, InvokeResponse}; @@ -279,7 +278,8 @@ fn handle_ipc_message(request: Request, manager: &AppManager serde_json::from_str::>(request.body()) .map_err(Into::into) .and_then(|message| { - let is_raw = message.payload.content_type() == &APPLICATION_OCTET_STREAM.to_string(); + let is_raw = + message.payload.content_type() == &mime::APPLICATION_OCTET_STREAM.to_string(); let payload = crypto_keys.decrypt(message.payload)?; Ok(Message { cmd: message.cmd, diff --git a/crates/tauri/src/lib.rs b/crates/tauri/src/lib.rs index 237776357..796dc21ec 100644 --- a/crates/tauri/src/lib.rs +++ b/crates/tauri/src/lib.rs @@ -79,6 +79,7 @@ pub use tauri_macros::include_image; pub use tauri_macros::mobile_entry_point; pub use tauri_macros::{command, generate_handler}; +use tauri_utils::assets::AssetsIter; pub use url::Url; pub(crate) mod app; @@ -351,7 +352,7 @@ pub trait Assets: Send + Sync + 'static { fn get(&self, key: &AssetKey) -> Option>; /// Iterator for the assets. - fn iter(&self) -> Box + '_>; + fn iter(&self) -> Box>; /// Gets the hashes for the CSP tag of the HTML on the given path. fn csp_hashes(&self, html_path: &AssetKey) -> Box> + '_>; @@ -362,7 +363,7 @@ impl Assets for EmbeddedAssets { EmbeddedAssets::get(self, key) } - fn iter(&self) -> Box + '_> { + fn iter(&self) -> Box> { EmbeddedAssets::iter(self) } @@ -1084,7 +1085,7 @@ mod test_utils { fn check_spawn_task(task in "[a-z]+") { // create dummy task function let dummy_task = async move { - format!("{task}-run-dummy-task"); + let _ = format!("{task}-run-dummy-task"); }; // call spawn crate::async_runtime::spawn(dummy_task); diff --git a/crates/tauri/src/test/mod.rs b/crates/tauri/src/test/mod.rs index 8bfddf09e..fa0c99e4f 100644 --- a/crates/tauri/src/test/mod.rs +++ b/crates/tauri/src/test/mod.rs @@ -64,7 +64,7 @@ use crate::{ }; use tauri_utils::{ acl::resolved::Resolved, - assets::{AssetKey, CspHash}, + assets::{AssetKey, AssetsIter, CspHash}, config::{AppConfig, Config}, }; @@ -82,8 +82,13 @@ impl Assets for NoopAsset { None } - fn iter(&self) -> Box + '_> { - Box::new(self.assets.iter().map(|(k, b)| (k.as_str(), b.as_slice()))) + fn iter(&self) -> Box> { + Box::new( + self + .assets + .iter() + .map(|(k, b)| (Cow::Borrowed(k.as_str()), Cow::Borrowed(b.as_slice()))), + ) } fn csp_hashes(&self, html_path: &AssetKey) -> Box> + '_> { diff --git a/crates/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap b/crates/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap index cb80b90b3..f0bc309e7 100644 --- a/crates/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap +++ b/crates/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap @@ -13,7 +13,7 @@ Resolved { pattern_string: "https", regexp: Ok( Regex( - "^https$", + "(?u)^https$", ), ), group_name_list: [], @@ -23,45 +23,57 @@ Resolved { inner: Literal { literal: "https", }, + ignore_case: false, }, + has_regexp_group: false, }, username: Component { - pattern_string: "", + pattern_string: "*", regexp: Ok( Regex( - "^$", + "(?u)^(.*)$", ), ), - group_name_list: [], + group_name_list: [ + "0", + ], matcher: Matcher { prefix: "", suffix: "", - inner: Literal { - literal: "", + inner: SingleCapture { + filter: None, + allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, password: Component { - pattern_string: "", + pattern_string: "*", regexp: Ok( Regex( - "^$", + "(?u)^(.*)$", ), ), - group_name_list: [], + group_name_list: [ + "0", + ], matcher: Matcher { prefix: "", suffix: "", - inner: Literal { - literal: "", + inner: SingleCapture { + filter: None, + allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, hostname: Component { pattern_string: "tauri.app", regexp: Ok( Regex( - "^tauri\\.app$", + "(?u)^tauri\\.app$", ), ), group_name_list: [], @@ -71,13 +83,15 @@ Resolved { inner: Literal { literal: "tauri.app", }, + ignore_case: false, }, + has_regexp_group: false, }, port: Component { pattern_string: "", regexp: Ok( Regex( - "^$", + "(?u)^$", ), ), group_name_list: [], @@ -87,13 +101,15 @@ Resolved { inner: Literal { literal: "", }, + ignore_case: false, }, + has_regexp_group: false, }, pathname: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -106,13 +122,15 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, search: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -125,13 +143,15 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, hash: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -144,7 +164,9 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, }, "https://tauri.app", @@ -183,7 +205,7 @@ Resolved { pattern_string: "https", regexp: Ok( Regex( - "^https$", + "(?u)^https$", ), ), group_name_list: [], @@ -193,45 +215,57 @@ Resolved { inner: Literal { literal: "https", }, + ignore_case: false, }, + has_regexp_group: false, }, username: Component { - pattern_string: "", + pattern_string: "*", regexp: Ok( Regex( - "^$", + "(?u)^(.*)$", ), ), - group_name_list: [], + group_name_list: [ + "0", + ], matcher: Matcher { prefix: "", suffix: "", - inner: Literal { - literal: "", + inner: SingleCapture { + filter: None, + allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, password: Component { - pattern_string: "", + pattern_string: "*", regexp: Ok( Regex( - "^$", + "(?u)^(.*)$", ), ), - group_name_list: [], + group_name_list: [ + "0", + ], matcher: Matcher { prefix: "", suffix: "", - inner: Literal { - literal: "", + inner: SingleCapture { + filter: None, + allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, hostname: Component { pattern_string: "tauri.app", regexp: Ok( Regex( - "^tauri\\.app$", + "(?u)^tauri\\.app$", ), ), group_name_list: [], @@ -241,13 +275,15 @@ Resolved { inner: Literal { literal: "tauri.app", }, + ignore_case: false, }, + has_regexp_group: false, }, port: Component { pattern_string: "", regexp: Ok( Regex( - "^$", + "(?u)^$", ), ), group_name_list: [], @@ -257,13 +293,15 @@ Resolved { inner: Literal { literal: "", }, + ignore_case: false, }, + has_regexp_group: false, }, pathname: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -276,13 +314,15 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, search: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -295,13 +335,15 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, hash: Component { pattern_string: "*", regexp: Ok( Regex( - "^(.*)$", + "(?u)^(.*)$", ), ), group_name_list: [ @@ -314,7 +356,9 @@ Resolved { filter: None, allow_empty: true, }, + ignore_case: false, }, + has_regexp_group: false, }, }, "https://tauri.app",