diff --git a/cache_buster/all.html b/cache_buster/all.html index d8aa8a7..ff7ca02 100644 --- a/cache_buster/all.html +++ b/cache_buster/all.html @@ -3,5 +3,5 @@

List of all items[] -

Structs

Enums

Constants

+

Structs

Enums

Constants

\ No newline at end of file diff --git a/cache_buster/constant.CACHE_BUSTER_DATA_FILE.html b/cache_buster/constant.CACHE_BUSTER_DATA_FILE.html index cf3aca1..01c6842 100644 --- a/cache_buster/constant.CACHE_BUSTER_DATA_FILE.html +++ b/cache_buster/constant.CACHE_BUSTER_DATA_FILE.html @@ -1,7 +1,7 @@ CACHE_BUSTER_DATA_FILE in cache_buster - Rust

Constant cache_buster::CACHE_BUSTER_DATA_FILE[][src]

pub const CACHE_BUSTER_DATA_FILE: &str = "./src/cache_buster_data.json";

file to which filemap is written during compilation + Change settings

Constant cache_buster::CACHE_BUSTER_DATA_FILE[][src]

pub const CACHE_BUSTER_DATA_FILE: &str = "./src/cache_buster_data.json";

file to which filemap is written during compilation include this to .gitignore

\ No newline at end of file diff --git a/cache_buster/index.html b/cache_buster/index.html index 6ad193f..e0077d4 100644 --- a/cache_buster/index.html +++ b/cache_buster/index.html @@ -1,7 +1,7 @@ cache_buster - Rust

Crate cache_buster[][src]

Crate cache_buster[][src]

What is cache busting?

To optimise network load time, browsers cache static files. Caching greatly improves performance but how do you inform browsers to invalidate cache when your files have changed?

@@ -54,7 +54,7 @@ a file that it doesn’t have. Pretty neat, isn’t it?

// the path to the file before setting up for cache busting files.get("./dist/github.svg");

Re-exports

-
pub use processor::BusterBuilder;
pub use filemap::Files;

Modules

+
pub use processor::BusterBuilder;
pub use processor::NoHashCategory;
pub use filemap::Files;

Modules

filemap

Module describing runtime compoenet for fetching modified filenames

processor

Module describing file processor that changes filenames to setup cache-busting

Constants

diff --git a/cache_buster/processor/enum.BusterBuilderError.html b/cache_buster/processor/enum.BusterBuilderError.html index f73445c..b83e9f7 100644 --- a/cache_buster/processor/enum.BusterBuilderError.html +++ b/cache_buster/processor/enum.BusterBuilderError.html @@ -1,7 +1,7 @@ BusterBuilderError in cache_buster::processor - Rust

Enum cache_buster::processor::BusterBuilderError[][src]

#[non_exhaustive]
+                Change settings

Enum cache_buster::processor::BusterBuilderError[][src]

#[non_exhaustive]
 pub enum BusterBuilderError {
     UninitializedField(&'static str),
     ValidationError(String),
@@ -10,14 +10,14 @@ pub enum BusterBuilderError {
                    Variants (Non-exhaustive)
 
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
UninitializedField(&'static str)

Uninitialized field

ValidationError(String)

Custom validation error

-

Trait Implementations

impl Debug for BusterBuilderError[src]

impl Display for BusterBuilderError[src]

impl Error for BusterBuilderError[src]

Trait Implementations

impl Debug for BusterBuilderError[src]

impl Display for BusterBuilderError[src]

impl Error for BusterBuilderError[src]

impl From<String> for BusterBuilderError[src]

impl From<UninitializedFieldError> for BusterBuilderError[src]

impl From<String> for BusterBuilderError[src]

impl From<UninitializedFieldError> for BusterBuilderError[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

 /*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
@@ -140,6 +141,7 @@
 
 pub mod processor;
 pub use processor::BusterBuilder;
+pub use processor::NoHashCategory;
 pub mod filemap;
 pub use filemap::Files;
 
diff --git a/src/cache_buster/processor.rs.html b/src/cache_buster/processor.rs.html
index ce0751f..4380c97 100644
--- a/src/cache_buster/processor.rs.html
+++ b/src/cache_buster/processor.rs.html
@@ -419,6 +419,138 @@
 416
 417
 418
+419
+420
+421
+422
+423
+424
+425
+426
+427
+428
+429
+430
+431
+432
+433
+434
+435
+436
+437
+438
+439
+440
+441
+442
+443
+444
+445
+446
+447
+448
+449
+450
+451
+452
+453
+454
+455
+456
+457
+458
+459
+460
+461
+462
+463
+464
+465
+466
+467
+468
+469
+470
+471
+472
+473
+474
+475
+476
+477
+478
+479
+480
+481
+482
+483
+484
+485
+486
+487
+488
+489
+490
+491
+492
+493
+494
+495
+496
+497
+498
+499
+500
+501
+502
+503
+504
+505
+506
+507
+508
+509
+510
+511
+512
+513
+514
+515
+516
+517
+518
+519
+520
+521
+522
+523
+524
+525
+526
+527
+528
+529
+530
+531
+532
+533
+534
+535
+536
+537
+538
+539
+540
+541
+542
+543
+544
+545
+546
+547
+548
+549
+550
 
 /*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
@@ -469,6 +601,26 @@
 
 use crate::*;
 
+#[derive(Debug, Clone)]
+/// Items to avoid hash calculation.
+///
+/// This is useful when serving vendor static files which are interlinked, where chaing
+/// file names should mean changing how the vendor files pulls its dependencies --- which are
+/// beyond the abilities of `cache_buster`.
+///
+/// ```rust
+/// use cache_buster::NoHashCategory;
+///
+/// let extensions = NoHashCategory::FileExtentions(vec!["wasm"]);
+/// let files = NoHashCategory::FileExtentions(vec!["swagger-ui-bundle.js", "favicon-16x16.png"]);
+/// ```
+pub enum NoHashCategory<'a> {
+    /// vector of file extensions that should be avoided for hash processing
+    FileExtentions(Vec<&'a str>),
+    /// list of file paths that should be avoided for file processing
+    FilePaths(Vec<&'a str>),
+}
+
 /// Configuration for setting up cache-busting
 #[derive(Debug, Clone, Builder)]
 #[builder(build_fn(validate = "Self::validate"))]
@@ -493,18 +645,22 @@
     /// They will be copied over without including a hash in the filename
     /// Path should be relative to [self.source]
     #[builder(default)]
-    no_hash: Vec<&'a str>,
+    no_hash: Vec<NoHashCategory<'a>>,
 }
 
 impl<'a> BusterBuilder<'a> {
     fn validate(&self) -> Result<(), String> {
-        for file in self.no_hash.iter() {
-            for file in file.iter() {
-                if !Path::new(&self.source.as_ref().unwrap())
-                    .join(file)
-                    .exists()
-                {
-                    return Err(format!("File {} doesn't exist", file));
+        for no_hash_configs in self.no_hash.iter() {
+            for no_hash in no_hash_configs.iter() {
+                if let NoHashCategory::FilePaths(files) = no_hash {
+                    for file in files.iter() {
+                        if !Path::new(&self.source.as_ref().unwrap())
+                            .join(file)
+                            .exists()
+                        {
+                            return Err(format!("File {} doesn't exist", file));
+                        }
+                    }
                 }
             }
         }
@@ -546,23 +702,64 @@
         let mut process_worker = |path: &Path| {
             let contents = Self::read_to_string(&path).unwrap();
             let hash = Self::hasher(&contents);
-            let new_name = if self.no_hash.iter().any(|no_hash| {
-                let no_hash = Path::new(&self.source).join(&no_hash);
-                no_hash == path
-            }) {
-                format!(
-                    "{}.{}",
-                    path.file_stem().unwrap().to_str().unwrap(),
-                    path.extension().unwrap().to_str().unwrap()
-                )
-            } else {
-                format!(
-                    "{}.{}.{}",
-                    path.file_stem().unwrap().to_str().unwrap(),
-                    hash,
-                    path.extension().unwrap().to_str().unwrap()
-                )
+
+            let get_name = |no_hash: bool| -> String {
+                if no_hash {
+                    format!(
+                        "{}.{}",
+                        path.file_stem().unwrap().to_str().unwrap(),
+                        path.extension().unwrap().to_str().unwrap()
+                    )
+                } else {
+                    format!(
+                        "{}.{}.{}",
+                        path.file_stem().unwrap().to_str().unwrap(),
+                        hash,
+                        path.extension().unwrap().to_str().unwrap()
+                    )
+                }
             };
+
+            let no_hash_status = self.no_hash.iter().any(|no_hash| {
+                match no_hash {
+                    NoHashCategory::FilePaths(paths) => {
+                        let no_hash_status = paths
+                            .iter()
+                            .any(|file_path| Path::new(&self.source).join(&file_path) == path);
+                        no_hash_status
+                    }
+                    NoHashCategory::FileExtentions(extensions) => {
+                        let mut no_hash_status = false;
+                        if let Some(cur_extention) = path.extension() {
+                            // .unwrap().to_str().unwrap();
+                            if let Some(cur_extention) = cur_extention.to_str() {
+                                no_hash_status = extensions.iter().any(|ext| &cur_extention == ext);
+                            }
+                        }
+                        no_hash_status
+                    }
+                }
+            });
+
+            let new_name = get_name(no_hash_status);
+
+            //            let new_name = if self.no_hash.iter().any(|no_hash| {
+            //                let no_hash = Path::new(&self.source).join(&no_hash);
+            //                no_hash == path
+            //            }) {
+            //                format!(
+            //                    "{}.{}",
+            //                    path.file_stem().unwrap().to_str().unwrap(),
+            //                    path.extension().unwrap().to_str().unwrap()
+            //                )
+            //            } else {
+            //                format!(
+            //                    "{}.{}.{}",
+            //                    path.file_stem().unwrap().to_str().unwrap(),
+            //                    hash,
+            //                    path.extension().unwrap().to_str().unwrap()
+            //                )
+            //            };
             self.copy(path, &new_name);
             let (source, destination) = self.gen_map(path, &&new_name);
             let _ = file_map.add(
@@ -735,7 +932,9 @@
             mime::IMAGE_GIF,
         ];
 
-        let no_hash = vec!["bbell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+        let no_hash =
+            NoHashCategory::FilePaths(vec!["bbell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"]);
+
         assert!(BusterBuilder::default()
             .source("./dist")
             .result("/tmp/prod2i")
@@ -743,27 +942,28 @@
             .copy(true)
             .follow_links(true)
             .prefix("/test")
-            .no_hash(no_hash.clone())
+            .no_hash(vec![no_hash.clone()])
             .build()
             .is_err())
     }
 
     fn no_specific_mime() {
         delete_file();
-        use std::{thread, time};
+        //use std::{thread, time};
 
-        let sleep = time::Duration::from_secs(4);
+        //let sleep = time::Duration::from_secs(4);
 
-        thread::sleep(sleep);
+        //thread::sleep(sleep);
 
         const WASM: &str = "858fd6c482cc75111d54.module.wasm";
-        let no_hash = vec![WASM, "bell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+        let no_hash_files = vec![WASM, "bell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+        let no_hash = NoHashCategory::FilePaths(no_hash_files.clone());
         let config = BusterBuilder::default()
             .source("./dist")
             .result("/tmp/prod2ii")
             .copy(true)
             .follow_links(true)
-            .no_hash(no_hash.clone())
+            .no_hash(vec![no_hash.clone()])
             .build()
             .unwrap();
         config.process().unwrap();
@@ -778,7 +978,7 @@
                 && source.file_name() == dest.file_name()
         }));
 
-        no_hash.iter().for_each(|file| {
+        no_hash_files.iter().for_each(|file| {
             assert!(files.map.iter().any(|(k, v)| {
                 let source = Path::new(k);
                 let dest = Path::new(&v);
@@ -833,9 +1033,73 @@
         cleanup(&config);
     }
 
+    fn no_hash_extension_works() {
+        delete_file();
+        use std::{thread, time};
+
+        let sleep = time::Duration::from_secs(4);
+        const APPLICATION_WASM: &str = "wasm";
+        const WASM: &str = "858fd6c482cc75111d54.module.wasm";
+
+        thread::sleep(sleep);
+
+        let no_hash_extensions = vec![APPLICATION_WASM];
+        let no_hash_ext = NoHashCategory::FileExtentions(no_hash_extensions.clone());
+
+        let no_hash_paths = vec!["bell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+        let no_hash_cat = NoHashCategory::FilePaths(no_hash_paths.clone());
+        let no_hash = vec![no_hash_cat, no_hash_ext];
+
+        let config = BusterBuilder::default()
+            .source("./dist")
+            .result("/tmp/prodnohashextension")
+            .copy(true)
+            .follow_links(true)
+            .no_hash(no_hash.clone())
+            .build()
+            .unwrap();
+        config.process().unwrap();
+        let files = Files::load();
+
+        assert!(files.map.iter().any(|(k, v)| {
+            let dest = Path::new(&v);
+            dest.extension().unwrap().to_str().unwrap() == APPLICATION_WASM && dest.exists()
+        }));
+
+        let no_hash_file = Path::new(&config.result).join(WASM);
+        assert!(files.map.iter().any(|(k, v)| {
+            let source = Path::new(&config.source).join(k);
+            let dest = Path::new(&v);
+            dest.file_name() == no_hash_file.file_name()
+                && dest.exists()
+                && source.file_name() == dest.file_name()
+        }));
+
+        no_hash_paths.iter().for_each(|file| {
+            assert!(files.map.iter().any(|(k, v)| {
+                let source = Path::new(k);
+                let dest = Path::new(&v);
+                let no_hash = Path::new(file);
+                source == &Path::new(&config.source).join(file)
+                    && dest.exists()
+                    && no_hash.file_name() == dest.file_name()
+            }));
+        });
+
+        for (k, v) in files.map.iter() {
+            let src = Path::new(&k);
+            let dest = Path::new(&v);
+
+            assert_eq!(src.exists(), dest.exists());
+        }
+
+        cleanup(&config);
+    }
+
     pub fn runner() {
         prefix_works();
         no_specific_mime();
+        no_hash_extension_works();
     }
 }