summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/http/lib.rs20
1 files changed, 14 insertions, 6 deletions
diff --git a/ext/http/lib.rs b/ext/http/lib.rs
index 5a8788f92..21c051cae 100644
--- a/ext/http/lib.rs
+++ b/ext/http/lib.rs
@@ -2,6 +2,7 @@
use async_compression::tokio::write::BrotliEncoder;
use async_compression::tokio::write::GzipEncoder;
+use async_compression::Level;
use cache_control::CacheControl;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
@@ -702,6 +703,11 @@ fn http_response(
compressing: bool,
encoding: Encoding,
) -> Result<(HttpResponseWriter, hyper::Body), AnyError> {
+ // Gzip, after level 1, doesn't produce significant size difference.
+ // This default matches nginx default gzip compression level (1):
+ // https://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_comp_level
+ const GZIP_DEFAULT_COMPRESSION_LEVEL: u8 = 1;
+
match data {
Some(data) if compressing => match encoding {
Encoding::Brotli => {
@@ -715,11 +721,10 @@ fn http_response(
Ok((HttpResponseWriter::Closed, writer.into_inner().into()))
}
Encoding::Gzip => {
- // Gzip, after level 1, doesn't produce significant size difference.
- // Probably the reason why nginx's default gzip compression level is
- // 1.
- // https://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_comp_level
- let mut writer = GzEncoder::new(Vec::new(), Compression::new(1));
+ let mut writer = GzEncoder::new(
+ Vec::new(),
+ Compression::new(GZIP_DEFAULT_COMPRESSION_LEVEL.into()),
+ );
writer.write_all(&data)?;
Ok((HttpResponseWriter::Closed, writer.finish()?.into()))
}
@@ -739,7 +744,10 @@ fn http_response(
let (_, writer) = tokio::io::split(b);
let writer: Pin<Box<dyn tokio::io::AsyncWrite>> = match encoding {
Encoding::Brotli => Box::pin(BrotliEncoder::new(writer)),
- Encoding::Gzip => Box::pin(GzipEncoder::new(writer)),
+ Encoding::Gzip => Box::pin(GzipEncoder::with_quality(
+ writer,
+ Level::Precise(GZIP_DEFAULT_COMPRESSION_LEVEL.into()),
+ )),
_ => unreachable!(), // forbidden by accepts_compression
};
let (stream, shutdown_handle) =