Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GZIP compression with Brotli #22

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace GZIP compression with Brotli
daxpedda committed Aug 6, 2022
commit 65bd5c222376eade21531b04d14ee9c9b8edae7e
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -22,5 +22,5 @@ tokio = { version = "1.11", default-features = false, features = ["rt-multi-thre
tower-http = { version = "0.3", features = ["fs", "set-header", "trace"] }
tower = "0.4"
fastrand = "1.5"
flate2 = "1.0"
brotli = { version = "3", default-features = false, features = ["std"]}
rcgen = { version = "0.9", default-features = false }
6 changes: 3 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::net::SocketAddr;

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

let serve_wasm = || async move {
(TypedHeader(ContentEncoding::gzip()), WithContentType("application/wasm", compressed_wasm))
([("content-encoding", "br")], WithContentType("application/wasm", compressed_wasm))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit unfortunate, see hyperium/headers#116.

};

let app = Router::new()
16 changes: 5 additions & 11 deletions src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ use anyhow::Context;
use std::path::Path;
use tracing::debug;

const COMPRESSION_LEVEL: u32 = 2;

pub struct WasmBindgenOutput {
pub js: String,
pub compressed_wasm: Vec<u8>,
@@ -34,15 +32,11 @@ pub fn generate(wasm_file: &Path) -> Result<WasmBindgenOutput> {
Ok(WasmBindgenOutput { js, compressed_wasm })
}

fn compress(bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
use flate2::write::GzEncoder;
use flate2::Compression;
use std::io::prelude::*;

let mut encoder = GzEncoder::new(Vec::new(), Compression::new(COMPRESSION_LEVEL));
fn compress(mut bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
use brotli::enc::{self, BrotliEncoderParams};

encoder.write_all(bytes)?;
let compressed = encoder.finish()?;
let mut output = Vec::new();
enc::BrotliCompress(&mut bytes, &mut output, &BrotliEncoderParams::default())?;

Ok(compressed)
Ok(output)
}