@@ -27,6 +27,7 @@ lazy_static! {
2727 MediaType :: parse_flexible( "video/*" ) . unwrap( ) ,
2828 MediaType :: parse_flexible( "application/wasm" ) . unwrap( ) ,
2929 MediaType :: parse_flexible( "application/octet-stream" ) . unwrap( ) ,
30+ MediaType :: parse_flexible( "text/event-stream" ) . unwrap( ) ,
3031 ] ;
3132 static ref CACHED_FILES : RwLock <HashMap <( String , CachedEncoding ) , & ' static [ u8 ] >> = {
3233 let m = HashMap :: new( ) ;
@@ -48,14 +49,14 @@ lazy_static! {
4849/// - `video/*`
4950/// - `application/wasm`
5051/// - `application/octet-stream`
52+ /// - `text/event-stream`
5153///
5254/// # Usage
5355///
5456/// Attach the compression [fairing](/rocket/fairing/) to your Rocket
5557/// application:
5658///
5759/// ```rust
58- ///
5960/// use rocket_async_compression::Compression;
6061///
6162///
@@ -66,27 +67,28 @@ lazy_static! {
6667/// # ;
6768///
6869/// ```
69- pub struct Compression ( pub Level ) ;
70+ pub struct Compression {
71+ pub level : Level ,
72+ pub excluded_content_types : Vec < MediaType > ,
73+ }
7074
7175impl Compression {
72- /// Returns a fairing that compresses outgoing requests.
76+ /// Returns a fairing that compresses outgoing requests. Uses default compression level and excluded content types.
7377 ///
7478 /// ## Example
7579 /// To attach this fairing, simply call `attach` on the application's
7680 /// `Rocket` instance with `Compression::fairing()`:
7781 ///
7882 /// ```rust
79- ///
8083 /// use rocket_async_compression::Compression;
8184 ///
8285 /// rocket::build()
8386 /// // ...
8487 /// .attach(Compression::fairing())
8588 /// // ...
86- /// # ;
8789 /// ```
8890 pub fn fairing ( ) -> Compression {
89- Compression ( Level :: Default )
91+ Compression :: with_level ( Level :: Default )
9092 }
9193
9294 /// Returns a fairing that compresses outgoing requests with the specified
@@ -95,17 +97,31 @@ impl Compression {
9597 /// ## Example
9698 ///
9799 /// ```rust
98- ///
99100 /// use rocket_async_compression::{Compression, Level};
100101 ///
101102 /// rocket::build()
102103 /// // ...
103104 /// .attach(Compression::with_level(Level::Fastest))
104105 /// // ...
105- /// # ;
106106 /// ```
107107 pub fn with_level ( level : Level ) -> Compression {
108- Compression ( level)
108+ Compression {
109+ level,
110+ excluded_content_types : EXCLUSIONS . clone ( ) ,
111+ }
112+ }
113+
114+ /// Replaces the default list of excluded content types with the provided list.
115+ pub fn exlude_content_types ( self , excluded_content_types : Vec < MediaType > ) -> Self {
116+ Compression {
117+ excluded_content_types,
118+ ..self
119+ }
120+ }
121+
122+ /// Returns a mutable reference to the list of excluded content types.
123+ pub fn excluded_content_types ( & mut self ) -> & mut Vec < MediaType > {
124+ & mut self . excluded_content_types
109125 }
110126}
111127
@@ -119,7 +135,12 @@ impl Fairing for Compression {
119135 }
120136
121137 async fn on_response < ' r > ( & self , request : & ' r Request < ' _ > , response : & mut Response < ' r > ) {
122- super :: CompressionUtils :: compress_response ( request, response, & EXCLUSIONS , self . 0 ) ;
138+ super :: CompressionUtils :: compress_response (
139+ request,
140+ response,
141+ & self . excluded_content_types ,
142+ self . level ,
143+ ) ;
123144 }
124145}
125146
@@ -137,7 +158,6 @@ impl Fairing for Compression {
137158/// application:
138159///
139160/// ```rust
140- ///
141161/// use rocket_async_compression::CachedCompression;
142162///
143163/// rocket::build()
@@ -157,7 +177,6 @@ impl Fairing for Compression {
157177/// ..Default::default()
158178/// })
159179/// // ...
160- /// # ;
161180/// ```
162181///
163182///
@@ -204,7 +223,7 @@ impl CachedCompression {
204223 }
205224 }
206225
207- /// Caches Vec<&str> to Vec<String>.
226+ /// Caches ` Vec<&str>` to ` Vec<String>` .
208227 pub fn static_paths ( paths : Vec < & str > ) -> Vec < String > {
209228 paths. into_iter ( ) . map ( Into :: into) . collect ( )
210229 }
0 commit comments