From 718b2859077a735ccad4d793d9f476326735a63b Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Sat, 13 Jan 2024 04:40:01 -0800 Subject: [PATCH] various small changes, allow bundling the wasm in the js file --- README.md | 4 ++-- client/build.sh | 13 +++++++++---- client/src/lib.rs | 1 - client/src/web/index.html | 1 + client/src/web/index.js | 5 +---- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 86bd708..1a735bd 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,5 @@ ### Client -1. Make sure you have the `wasm32-unknown-unknown` target installed, `wasm-bindgen` executable installed (and optionally `wasm-opt`), and `bash`, `python3` packages (`python3` is used for `http.server` module) -2. Run `bash build.sh` to build without wasm-opt and start a webserver, add any argument to call wasm-opt +1. Make sure you have the `wasm32-unknown-unknown` target installed, `wasm-bindgen` and `wasm-opt` executables installed, and `bash`, `python3` packages (`python3` is used for `http.server` module) +2. Run `bash build.sh` to build without wasm-opt and start a webserver diff --git a/client/build.sh b/client/build.sh index 8c7ca0e..fa14638 100755 --- a/client/build.sh +++ b/client/build.sh @@ -4,11 +4,16 @@ shopt -s inherit_errexit rm -rf out/ || true mkdir out/ + cargo build --target wasm32-unknown-unknown --release wasm-bindgen --weak-refs --no-typescript --target no-modules --out-dir out/ ../target/wasm32-unknown-unknown/release/wstcp_client.wasm -if [[ "$#" > 0 ]]; then - mv out/wstcp_client_bg.wasm out/wstcp_client_unoptimized.wasm - wasm-opt -O4 out/wstcp_client_unoptimized.wasm -o out/wstcp_client_bg.wasm -fi + +mv out/wstcp_client_bg.wasm out/wstcp_client_unoptimized.wasm +wasm-opt -O4 out/wstcp_client_unoptimized.wasm -o out/wstcp_client_bg.wasm + +AUTOGENERATED_SOURCE=$(<"out/wstcp_client.js") +WASM_BASE64=$(base64 -w0 out/wstcp_client_bg.wasm) +echo "${AUTOGENERATED_SOURCE//__wbg_init(input) \{/__wbg_init(input) \{input=\'data:application/wasm;base64,$WASM_BASE64\'}" > out/wstcp_client_bundled.js + cp -r src/web/* out/ (cd out; python3 -m http.server) diff --git a/client/src/lib.rs b/client/src/lib.rs index 5d867f3..ac6ecf1 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -82,7 +82,6 @@ async fn send_req(req: http::Request, should_redirect: bool, io: WsTcp } let new_url = redirect_url.clone(); *new_req.uri_mut() = redirect_url; - new_req.headers_mut().remove("Host"); new_req.headers_mut().insert( "Host", HeaderValue::from_str(redirect_url_authority.as_str())?, diff --git a/client/src/web/index.html b/client/src/web/index.html index 1db0a54..defc525 100644 --- a/client/src/web/index.html +++ b/client/src/web/index.html @@ -5,5 +5,6 @@ + running... (wait for the browser alert) diff --git a/client/src/web/index.js b/client/src/web/index.js index 22bd0f7..e422500 100644 --- a/client/src/web/index.js +++ b/client/src/web/index.js @@ -28,8 +28,6 @@ console.warn(url, resp, Object.fromEntries(resp.headers)); console.warn(await resp.text()); } - - alert("you can open console now"); } else if (should_perf_test) { const test_mux = async (url) => { const t0 = performance.now(); @@ -62,11 +60,10 @@ console.warn(`avg mux (10) took ${total_mux} ms or ${total_mux / 1000} s`); console.warn(`avg native (10) took ${total_native} ms or ${total_native / 1000} s`); console.warn(`mux - native: ${total_mux - total_native} ms or ${(total_mux - total_native) / 1000} s`); - - alert("you can open console now"); } else { let resp = await wstcp.fetch("https://httpbin.org/get"); console.warn(resp, Object.fromEntries(resp.headers)); console.warn(await resp.text()); } + alert("you can open console now"); })();