Skip to content

Commit a539414

Browse files
committed
Updates based on PR comments
1 parent a83967f commit a539414

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/main.rs

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

3939
let output = wasm_bindgen::generate(&options, &wasm_file)?;
4040

41-
info!("compressed wasm output is {} large", pretty_size(output.compressed_wasm.len()));
41+
info!("compressed wasm output is {} large", pretty_size(output.br_compressed_wasm.len()));
4242

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

src/server.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
6161
}
6262

6363
fn get_router(options: &Options, output: WasmBindgenOutput) -> Router {
64-
let WasmBindgenOutput { js, compressed_wasm, gzip_compressed_wasm, snippets, local_modules } =
64+
let WasmBindgenOutput { js, br_compressed_wasm, gzip_compressed_wasm, snippets, local_modules } =
6565
output;
6666

6767
let middleware_stack = ServiceBuilder::new()
@@ -89,26 +89,22 @@ fn get_router(options: &Options, output: WasmBindgenOutput) -> Router {
8989
get_service(ServeDir::new(options.directory.clone())).handle_error(internal_server_error);
9090

9191
let serve_wasm = |headers: HeaderMap| async move {
92-
println!(" request headers: {:?}", headers);
9392
if let Some(accept_encoding) = headers.get(ACCEPT_ENCODING) {
9493
match from_utf8(accept_encoding.as_bytes()) {
9594
Ok(encodings) => {
9695
let split_encodings: Vec<&str> = encodings.split(",").map(str::trim).collect();
97-
println!("split_encodings: {:?}", split_encodings);
9896
if split_encodings.contains(&"br") {
99-
println!("serving br compressed wasm");
10097
Ok((
10198
[("content-encoding", "br")],
102-
WithContentType("application/wasm", compressed_wasm),
99+
WithContentType("application/wasm", br_compressed_wasm),
103100
))
104101
} else if split_encodings.contains(&"gzip") {
105-
println!("serving gzip compressed wasm");
106102
Ok((
107103
[("content-encoding", "gzip")],
108104
WithContentType("application/wasm", gzip_compressed_wasm),
109105
))
110106
} else {
111-
println!("No support for requested encoding!");
107+
tracing::warn!("Unsupported encoding in request for wasm.wasm");
112108
Err((
113109
StatusCode::BAD_REQUEST,
114110
format!("Unsupported encoding(s): {:?}", split_encodings),
@@ -229,7 +225,7 @@ mod tests {
229225
fn fake_wasm_bindgen_output() -> WasmBindgenOutput {
230226
WasmBindgenOutput {
231227
js: "fake js".to_string(),
232-
compressed_wasm: FAKE_BR_COMPRESSED_WASM.to_vec(),
228+
br_compressed_wasm: FAKE_BR_COMPRESSED_WASM.to_vec(),
233229
gzip_compressed_wasm: FAKE_GZIP_COMPRESSED_WASM.to_vec(),
234230
snippets: HashMap::<String, Vec<String>>::new(),
235231
local_modules: HashMap::<String, String>::new(),
@@ -248,11 +244,8 @@ mod tests {
248244
let client = make_test_client();
249245

250246
// Test without any supported compression
251-
let mut res =
252-
client.get("/api/wasm.wasm").header("accept-encoding", "deflate").send().await;
247+
let res = client.get("/api/wasm.wasm").header("accept-encoding", "deflate").send().await;
253248
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
254-
let result = res.chunk().await.unwrap();
255-
println!("Bad Request result: {:?}", result);
256249
}
257250

258251
#[tokio::test]

src/wasm_bindgen.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const COMPRESSION_LEVEL: u32 = 2;
99

1010
pub struct WasmBindgenOutput {
1111
pub js: String,
12-
pub compressed_wasm: Vec<u8>,
12+
pub br_compressed_wasm: Vec<u8>,
1313
pub gzip_compressed_wasm: Vec<u8>,
1414
pub snippets: HashMap<String, Vec<String>>,
1515
pub local_modules: HashMap<String, String>,
@@ -40,25 +40,29 @@ pub fn generate(options: &Options, wasm_file: &Path) -> Result<WasmBindgenOutput
4040

4141
debug!("br compressing wasm...");
4242
let start = std::time::Instant::now();
43-
let compressed_wasm = br_compress(&wasm).context("failed to compress wasm file")?;
43+
let br_compressed_wasm = br_compress(&wasm).context("failed to compress wasm file")?;
4444
debug!("compressing took {:?}", start.elapsed());
4545

4646
debug!("gzip compressing wasm...");
4747
let start = std::time::Instant::now();
4848
let gzip_compressed_wasm = gzip_compress(&wasm).context("failed to compress wasm file")?;
4949
debug!("compressing took {:?}", start.elapsed());
5050

51-
Ok(WasmBindgenOutput { js, compressed_wasm, gzip_compressed_wasm, snippets, local_modules })
51+
Ok(WasmBindgenOutput { js, br_compressed_wasm, gzip_compressed_wasm, snippets, local_modules })
5252
}
5353

5454
fn br_compress(mut bytes: &[u8]) -> Result<Vec<u8>, std::io::Error> {
5555
use brotli::enc::{self, BrotliEncoderParams};
5656

5757
let mut output = Vec::new();
58-
enc::BrotliCompress(&mut bytes, &mut output, &BrotliEncoderParams {
59-
quality: 5, // https://github.com/jakobhellermann/wasm-server-runner/pull/22#issuecomment-1235804905
60-
..Default::default()
61-
})?;
58+
enc::BrotliCompress(
59+
&mut bytes,
60+
&mut output,
61+
&BrotliEncoderParams {
62+
quality: 5, // https://github.com/jakobhellermann/wasm-server-runner/pull/22#issuecomment-1235804905
63+
..Default::default()
64+
},
65+
)?;
6266

6367
Ok(output)
6468
}

0 commit comments

Comments
 (0)