Skip to content

Commit 2f0bbec

Browse files
daxpeddajakobhellermann
authored andcommitted
Replace GZIP compression with Brotli
1 parent 27b90da commit 2f0bbec

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ tokio = { version = "1.11", default-features = false, features = ["rt-multi-thre
2323
tower-http = { version = "0.3", features = ["compression-full", "fs", "set-header", "trace"] }
2424
tower = "0.4"
2525
fastrand = "1.5"
26-
flate2 = "1.0"
26+
brotli = { version = "3", default-features = false, features = ["std"]}
2727
rcgen = { version = "0.9", default-features = false }

src/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::net::SocketAddr;
22

3-
use axum::headers::{ContentEncoding, HeaderName};
3+
use axum::headers::HeaderName;
44
use axum::http::{HeaderValue, StatusCode};
55
use axum::response::{Html, IntoResponse, Response};
66
use axum::routing::{get, get_service};
7-
use axum::{Router, TypedHeader};
7+
use axum::Router;
88
use axum_server::tls_rustls::RustlsConfig;
99
use axum_server_dual_protocol::ServerExt;
1010
use tower::ServiceBuilder;
@@ -47,7 +47,7 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
4747
let serve_dir = get_service(ServeDir::new(".")).handle_error(internal_server_error);
4848

4949
let serve_wasm = || async move {
50-
(TypedHeader(ContentEncoding::gzip()), WithContentType("application/wasm", compressed_wasm))
50+
([("content-encoding", "br")], WithContentType("application/wasm", compressed_wasm))
5151
};
5252

5353
let app = Router::new()

src/wasm_bindgen.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use anyhow::Context;
33
use std::path::Path;
44
use tracing::debug;
55

6-
const COMPRESSION_LEVEL: u32 = 2;
7-
86
pub struct WasmBindgenOutput {
97
pub js: String,
108
pub compressed_wasm: Vec<u8>,
@@ -34,15 +32,11 @@ pub fn generate(wasm_file: &Path) -> Result<WasmBindgenOutput> {
3432
Ok(WasmBindgenOutput { js, compressed_wasm })
3533
}
3634

37-
fn compress(bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
38-
use flate2::write::GzEncoder;
39-
use flate2::Compression;
40-
use std::io::prelude::*;
41-
42-
let mut encoder = GzEncoder::new(Vec::new(), Compression::new(COMPRESSION_LEVEL));
35+
fn compress(mut bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
36+
use brotli::enc::{self, BrotliEncoderParams};
4337

44-
encoder.write_all(bytes)?;
45-
let compressed = encoder.finish()?;
38+
let mut output = Vec::new();
39+
enc::BrotliCompress(&mut bytes, &mut output, &BrotliEncoderParams::default())?;
4640

47-
Ok(compressed)
41+
Ok(output)
4842
}

0 commit comments

Comments
 (0)