From 428c40d83dd154c4601bc4ece0653fb7555bea54 Mon Sep 17 00:00:00 2001 From: Seokju Na Date: Sun, 27 Oct 2024 16:24:30 +0900 Subject: [PATCH 1/3] add cache --- crates/webview-bundle-tauri/Cargo.toml | 5 ++ crates/webview-bundle-tauri/src/cache.rs | 61 +++++++++++++++++++++++ crates/webview-bundle-tauri/src/config.rs | 20 ++++++-- crates/webview-bundle-tauri/src/lib.rs | 33 ++++++++---- 4 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 crates/webview-bundle-tauri/src/cache.rs diff --git a/crates/webview-bundle-tauri/Cargo.toml b/crates/webview-bundle-tauri/Cargo.toml index ceeacf4..588da26 100644 --- a/crates/webview-bundle-tauri/Cargo.toml +++ b/crates/webview-bundle-tauri/Cargo.toml @@ -12,6 +12,11 @@ webview-bundle = { workspace = true } async-trait = { workspace = true } buildstructor = "0.5.4" +lru = { version = "0.12.5", optional = true } +mime_guess = { workspace = true } tauri = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } + +[features] +cache-lru = ["dep:lru"] diff --git a/crates/webview-bundle-tauri/src/cache.rs b/crates/webview-bundle-tauri/src/cache.rs new file mode 100644 index 0000000..8793c5e --- /dev/null +++ b/crates/webview-bundle-tauri/src/cache.rs @@ -0,0 +1,61 @@ +use std::hash::Hash; +use webview_bundle::Bundle; + +pub trait Cache { + fn has(&self, key: &K) -> bool; + fn get(&mut self, key: &K) -> Option<&V>; + fn set(&mut self, key: K, value: V); +} + +#[derive(Clone, Default)] +pub struct NoopCache; + +impl Cache for NoopCache { + fn has(&self, _key: &String) -> bool { + false + } + + fn get(&mut self, _key: &String) -> Option<&Bundle> { + None + } + + fn set(&mut self, _key: String, _value: Bundle) {} +} + +#[cfg(feature = "cache-lru")] +#[derive(Clone)] +pub struct LruCache { + cache: lru::LruCache, +} + +#[cfg(feature = "cache-lru")] +impl LruCache { + pub fn new(size: usize) -> Self { + Self { + cache: lru::LruCache::::new( + std::num::NonZeroUsize::new(size).expect("size is not non zero"), + ), + } + } + + pub fn unbounded() -> Self { + Self { + cache: lru::LruCache::::unbounded(), + } + } +} + +#[cfg(feature = "cache-lru")] +impl Cache for LruCache { + fn has(&self, key: &String) -> bool { + self.cache.contains(key) + } + + fn get(&mut self, key: &String) -> Option<&Bundle> { + self.cache.get(key) + } + + fn set(&mut self, key: String, value: Bundle) { + self.cache.put(key, value); + } +} diff --git a/crates/webview-bundle-tauri/src/config.rs b/crates/webview-bundle-tauri/src/config.rs index 29dc3c6..0468b38 100644 --- a/crates/webview-bundle-tauri/src/config.rs +++ b/crates/webview-bundle-tauri/src/config.rs @@ -1,20 +1,32 @@ +use crate::cache::Cache; use crate::loader::Loader; +use webview_bundle::Bundle; -pub struct Config { +pub struct Config +where + L: Loader + Send + Sync, + C: Cache + Send + Sync, +{ loader: L, + cache: C, } #[buildstructor::buildstructor] -impl Config +impl Config where L: Loader + Send + Sync, + C: Cache + Send + Sync, { #[builder] - pub fn new(loader: L) -> Self { - Self { loader } + pub fn new(loader: L, cache: C) -> Self { + Self { loader, cache } } pub fn loader(&self) -> &L { &self.loader } + + pub fn cache(&self) -> &C { + &self.cache + } } diff --git a/crates/webview-bundle-tauri/src/lib.rs b/crates/webview-bundle-tauri/src/lib.rs index a119717..ddd4e4a 100644 --- a/crates/webview-bundle-tauri/src/lib.rs +++ b/crates/webview-bundle-tauri/src/lib.rs @@ -1,20 +1,23 @@ -mod config; -mod error; -mod loader; +pub mod cache; +pub mod config; +pub mod error; +pub mod loader; -pub use config::Config; -pub use error::Error; -pub use loader::{FSLoader, Loader}; +use crate::cache::Cache; +use crate::config::Config; +use crate::loader::Loader; use std::path::Path; use tauri::http::{Method, Response, Uri}; use tauri::plugin::{PluginApi, TauriPlugin}; use tauri::{plugin, AppHandle, Manager, Runtime}; +use webview_bundle::Bundle; -pub fn init(scheme: &'static str, config: F) -> TauriPlugin +pub fn init(scheme: &'static str, config: F) -> TauriPlugin where R: Runtime, L: Loader + Send + Sync + 'static, - F: FnOnce(&AppHandle, PluginApi) -> Result, Box> + C: Cache + Send + Sync + 'static, + F: FnOnce(&AppHandle, PluginApi) -> Result, Box> + Send + 'static, { @@ -33,13 +36,13 @@ where let uri = request.uri().clone(); let app = ctx.app_handle().clone(); tauri::async_runtime::spawn(async move { - let config = app.state::>(); + let config = app.state::>(); let bundle = config.loader().load(&uri).await.unwrap(); let filepath = uri_to_filepath(&uri); - let buf = bundle.read_file(filepath).unwrap(); + let buf = bundle.read_file(&filepath).unwrap(); // TODO: handle file not found error responder.respond( Response::builder() - .header("content-type", "text/html") + .header("content-type", mime_types_from_filepath(&filepath)) .header("content-length", buf.len()) .status(200) .body(buf) @@ -61,3 +64,11 @@ fn uri_to_filepath(uri: &Uri) -> String { } [filepath, index_html].join("/") } + +fn mime_types_from_filepath(filepath: &String) -> String { + let guess = mime_guess::from_path(filepath); + guess + .first() + .map(|x| x.to_string()) + .unwrap_or("text/plain".to_string()) +} From 3a88632b70d192fd9720117569849fa9c259107c Mon Sep 17 00:00:00 2001 From: Seokju Na Date: Sun, 27 Oct 2024 16:24:35 +0900 Subject: [PATCH 2/3] example --- examples/tauri-simple/src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/tauri-simple/src/main.rs b/examples/tauri-simple/src/main.rs index c4d6202..dfaad0f 100644 --- a/examples/tauri-simple/src/main.rs +++ b/examples/tauri-simple/src/main.rs @@ -1,7 +1,9 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use tauri::{Manager, WebviewUrl}; -use webview_bundle_tauri::{Config, FSLoader}; +use webview_bundle_tauri::cache::NoopCache; +use webview_bundle_tauri::config::Config; +use webview_bundle_tauri::loader::FSLoader; fn main() { tauri::Builder::default() @@ -10,7 +12,10 @@ fn main() { dir.pop(); dir.pop(); dir.push("examples/tauri-simple"); - let config = Config::builder().loader(FSLoader::from_dir(dir)).build(); + let config = Config::builder() + .cache(NoopCache::default()) + .loader(FSLoader::from_dir(dir)) + .build(); Ok(config) })) .setup(|app| { From 75c8ab2dd047cd385c471826300f8d6129cdfcef Mon Sep 17 00:00:00 2001 From: Seokju Na Date: Sun, 27 Oct 2024 16:24:39 +0900 Subject: [PATCH 3/3] update cargo --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 1c68824..0c77bac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ bincode = "2.0.0-rc.3" biome_console = "0.5.7" bpaf = { version = "0.9.14", features = ["derive"] } lz4_flex = "0.11.3" +mime_guess = "2.0.5" napi = { version = "2.16.11", default-features = false, features = ["napi4", "async"] } napi-build = "2.1.3" napi-derive = "2.16.12"