From 69af418487ed5e7b51a9e1258b10bdf4925c4340 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Fri, 4 Aug 2023 17:28:32 +0200 Subject: [PATCH] fix(ext/http): unify default gzip compression level (#20050) This tweaks the HTTP response-writer in order to align the two possible execution flows into using the same gzip default compression level, that is `1` (otherwise the implicit default level is `6`). --- ext/http/lib.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/ext/http/lib.rs b/ext/http/lib.rs index c33c1d15e4..7ccebe0610 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; @@ -700,6 +701,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 => { @@ -713,11 +719,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())) } @@ -737,7 +742,10 @@ fn http_response( let (_, writer) = tokio::io::split(b); let writer: Pin> = 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) =