Skip to content

Commit bdfdfcc

Browse files
compress wasm file
1 parent 23af597 commit bdfdfcc

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ tokio = { version = "1.11", default-features = false, features = ["rt-multi-thre
2121
tower-http = { version = "0.2", features = ["fs", "trace"] }
2222
tower = "0.4"
2323
fastrand = "1.5"
24+
flate2 = "1.0"

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> Result<(), anyhow::Error> {
3030

3131
let output = wasm_bindgen::generate(&wasm_file)?;
3232

33-
info!("wasm output is {} large", pretty_size(output.wasm.len()));
33+
info!("wasm output is {} large", pretty_size(output.compressed_wasm.len()));
3434

3535
let rt = tokio::runtime::Runtime::new()?;
3636
rt.block_on(server::run_server(options, output))?;

src/server.rs

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

33
use axum::http::{HeaderValue, StatusCode};
4-
use axum::response::{Html, IntoResponse, Response};
4+
use axum::response::{Headers, Html, IntoResponse, Response};
55
use axum::routing::{get, get_service};
66
use axum::Router;
77
use tower::ServiceBuilder;
@@ -19,7 +19,7 @@ pub struct Options {
1919
}
2020

2121
pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<()> {
22-
let WasmBindgenOutput { js, wasm } = output;
22+
let WasmBindgenOutput { js, compressed_wasm } = output;
2323

2424
let middleware_stack = ServiceBuilder::new().into_inner();
2525

@@ -29,10 +29,16 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
2929

3030
let serve_dir = get_service(ServeDir::new(".")).handle_error(internal_server_error);
3131

32+
let serve_wasm = || async move {
33+
let headers = Headers(std::iter::once(("Content-Encoding", "gzip")));
34+
let response = WithContentType("application/wasm", compressed_wasm);
35+
(headers, response)
36+
};
37+
3238
let app = Router::new()
3339
.route("/", get(move || async { Html(html) }))
3440
.route("/api/wasm.js", get(|| async { WithContentType("application/javascript", js) }))
35-
.route("/api/wasm.wasm", get(|| async { WithContentType("application/wasm", wasm) }))
41+
.route("/api/wasm.wasm", get(serve_wasm))
3642
.route("/api/version", get(move || async { version }))
3743
.fallback(serve_dir)
3844
.layer(middleware_stack);

src/wasm_bindgen.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use std::path::Path;
22

3+
use anyhow::Context;
4+
35
use crate::Result;
46

7+
const COMPRESSION_LEVEL: u32 = 2;
8+
59
pub struct WasmBindgenOutput {
610
pub js: String,
7-
pub wasm: Vec<u8>,
11+
pub compressed_wasm: Vec<u8>,
812
}
913
pub fn generate(wasm_file: &Path) -> Result<WasmBindgenOutput> {
1014
let mut output = wasm_bindgen_cli_support::Bindgen::new()
@@ -13,8 +17,23 @@ pub fn generate(wasm_file: &Path) -> Result<WasmBindgenOutput> {
1317
.typescript(false)
1418
.generate_output()?;
1519

16-
Ok(WasmBindgenOutput {
17-
js: output.js().to_owned(),
18-
wasm: output.wasm_mut().emit_wasm(),
19-
})
20+
let js = output.js().to_owned();
21+
let wasm = output.wasm_mut().emit_wasm();
22+
23+
let compressed_wasm = compress(&wasm).context("failed to compress wasm file")?;
24+
25+
Ok(WasmBindgenOutput { js, compressed_wasm })
26+
}
27+
28+
fn compress(bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
29+
use flate2::write::GzEncoder;
30+
use flate2::Compression;
31+
use std::io::prelude::*;
32+
33+
let mut encoder = GzEncoder::new(Vec::new(), Compression::new(COMPRESSION_LEVEL));
34+
35+
encoder.write_all(bytes)?;
36+
let compressed = encoder.finish()?;
37+
38+
Ok(compressed)
2039
}

0 commit comments

Comments
 (0)