refactor(core)!: change Assets::iter item to use Cow (#10907)

* refactor(core): change `Assets::iter` item to use `Cow`

make the iterator more flexible to support Assets implementations that do not rely on static assets

* fix test?

* lint

* lint

* clippy again
This commit is contained in:
Lucas Fernandes Nogueira 2024-09-05 13:42:22 -03:00 committed by GitHub
parent d9c8d3cc8d
commit faa259bacf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 108 additions and 44 deletions

View File

@ -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`.

View File

@ -12,6 +12,9 @@ use std::{
path::{Component, Path},
};
/// Assets iterator.
pub type AssetsIter<'a> = dyn Iterator<Item = (Cow<'a, str>, 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<dyn Iterator<Item = (&str, &[u8])> + '_> {
Box::new(self.assets.into_iter().map(|(k, b)| (*k, *b)))
pub fn iter(&self) -> Box<AssetsIter<'_>> {
Box::new(
self
.assets
.into_iter()
.map(|(k, b)| (Cow::Borrowed(*k), Cow::Borrowed(*b))),
)
}
/// CSP hashes for the given asset.

View File

@ -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<R: Runtime> AssetResolver<R> {
}
/// Iterate on all assets.
pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
pub fn iter(&self) -> Box<AssetsIter<'_>> {
self.manager.assets.iter()
}
}

View File

@ -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<R: Runtime>(request: Request<String>, manager: &AppManager
serde_json::from_str::<IsolationMessage<'_>>(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,

View File

@ -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<R: Runtime>: Send + Sync + 'static {
fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>>;
/// Iterator for the assets.
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_>;
fn iter(&self) -> Box<tauri_utils::assets::AssetsIter<'_>>;
/// Gets the hashes for the CSP tag of the HTML on the given path.
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_>;
@ -362,7 +363,7 @@ impl<R: Runtime> Assets<R> for EmbeddedAssets {
EmbeddedAssets::get(self, key)
}
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
fn iter(&self) -> Box<AssetsIter<'_>> {
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);

View File

@ -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<R: Runtime> Assets<R> for NoopAsset {
None
}
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
Box::new(self.assets.iter().map(|(k, b)| (k.as_str(), b.as_slice())))
fn iter(&self) -> Box<AssetsIter<'_>> {
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<dyn Iterator<Item = CspHash<'_>> + '_> {

View File

@ -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",