From 656eb67bfbd04ddb0b19ce0776ec38b959c98e8f Mon Sep 17 00:00:00 2001 From: CoolElectronics Date: Mon, 15 Jan 2024 14:37:55 -0500 Subject: [PATCH] npm packaging --- .gitignore | 4 ++++ .npmignore | 14 ++++++++++++++ client/build.sh | 21 +++++++++++++-------- client/{src/web/index.js => demo.js} | 4 ++-- client/index.html | 13 +++++++++++++ client/src/lib.rs | 9 ++++++--- client/src/web/index.html | 10 ---------- client/src/websocket.rs | 4 +++- package.json | 27 +++++++++++++++++++++++++++ 9 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 .npmignore rename client/{src/web/index.js => demo.js} (95%) create mode 100644 client/index.html delete mode 100644 client/src/web/index.html create mode 100644 package.json diff --git a/.gitignore b/.gitignore index 534e982..bfd66de 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ server/src/*.pem client/pkg client/out .direnv +client/index.js +client/module.js +client/module.d.ts +pnpm-lock.yaml diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..d0f5b3d --- /dev/null +++ b/.npmignore @@ -0,0 +1,14 @@ +/target +server/ +out/ +client/src +client/pkg +client/out +client/demo.js +client/index.html +client/build.sh +client/Cargo.toml +.direnv +Cargo.toml +Cargo.lock +pnpm-lock.yaml diff --git a/client/build.sh b/client/build.sh index 3f7a42a..d33b608 100755 --- a/client/build.sh +++ b/client/build.sh @@ -7,20 +7,25 @@ mkdir out/ cargo build --target wasm32-unknown-unknown --release echo "[ws] built rust" -wasm-bindgen --weak-refs --no-typescript --target no-modules --out-dir out/ ../target/wasm32-unknown-unknown/release/epoxy_client.wasm +wasm-bindgen --weak-refs --target no-modules --no-modules-global epoxy --out-dir out/ ../target/wasm32-unknown-unknown/release/epoxy_client.wasm echo "[ws] bindgen finished" mv out/epoxy_client_bg.wasm out/epoxy_client_unoptimized.wasm -time wasm-opt -O4 out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm +time wasm-opt out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm echo "[ws] optimized" AUTOGENERATED_SOURCE=$(<"out/epoxy_client.js") WASM_BASE64=$(base64 -w0 out/epoxy_client_bg.wasm) -AUTOGENERATED_SOURCE="${AUTOGENERATED_SOURCE//__wbg_init(input) \{/__wbg_init(input) \{input=\'data:application/wasm;base64,$WASM_BASE64\'}" -AUTOGENERATED_SOURCE="${AUTOGENERATED_SOURCE//let wasm_bindgen/let epoxy}" -AUTOGENERATED_SOURCE="${AUTOGENERATED_SOURCE//wasm_bindgen =/epoxy =}" -echo "${AUTOGENERATED_SOURCE}" > out/epoxy_client_bundled.js +AUTOGENERATED_SOURCE=${AUTOGENERATED_SOURCE//__wbg_init(input) \{/__wbg_init() \{let input=\'data:application/wasm;base64,$WASM_BASE64\'} +AUTOGENERATED_SOURCE=${AUTOGENERATED_SOURCE//return __wbg_finalize_init\(instance\, module\);/__wbg_finalize_init\(instance\, module\); return epoxy} +echo "$AUTOGENERATED_SOURCE" > index.js +cp index.js module.js +echo "module.exports = epoxy" >> module.js -cp -r src/web/* out/ +AUTOGENERATED_TYPEDEFS=$(<"out/epoxy_client.d.ts") +AUTOGENERATED_TYPEDEFS=${AUTOGENERATED_TYPEDEFS%%export class IntoUnderlyingByteSource*} +echo "$AUTOGENERATED_TYPEDEFS" >"module.d.ts" +echo "} export default function epoxy(): Promise;" >> "module.d.ts" + +rm -rf out/ echo "[ws] done!" -(cd out; python3 -m http.server) diff --git a/client/src/web/index.js b/client/demo.js similarity index 95% rename from client/src/web/index.js rename to client/demo.js index 292ecd0..77876b2 100644 --- a/client/src/web/index.js +++ b/client/demo.js @@ -8,11 +8,11 @@ const should_perf_test = (new URL(window.location.href)).searchParams.has("perf_test"); const should_ws_test = (new URL(window.location.href)).searchParams.has("ws_test"); - await epoxy(); + let { EpoxyClient } = await epoxy(); const tconn0 = performance.now(); // args: websocket url, user agent, redirect limit - let epoxy_client = await new epoxy.EpoxyClient("wss://localhost:4000", navigator.userAgent, 10); + let epoxy_client = await new EpoxyClient("wss://localhost:4000", navigator.userAgent, 10); const tconn1 = performance.now(); console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`); diff --git a/client/index.html b/client/index.html new file mode 100644 index 0000000..99d1eb8 --- /dev/null +++ b/client/index.html @@ -0,0 +1,13 @@ + + + + epoxy + + + + + + running... (wait for the browser alert if not running ws test) + + + diff --git a/client/src/lib.rs b/client/src/lib.rs index 89a4554..d47d6c0 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -7,8 +7,8 @@ mod wrappers; use tokioio::TokioIo; use utils::{ReplaceErr, UriExt}; -use wrappers::{IncomingBody, WsStreamWrapper}; use websocket::EpxWebSocket; +use wrappers::{IncomingBody, WsStreamWrapper}; use std::sync::Arc; @@ -17,7 +17,7 @@ use bytes::Bytes; use futures_util::StreamExt; use http::{uri, HeaderName, HeaderValue, Request, Response}; use hyper::{body::Incoming, client::conn::http1::Builder, Uri}; -use js_sys::{Function, Array, Object, Reflect, Uint8Array}; +use js_sys::{Array, Function, Object, Reflect, Uint8Array}; use penguin_mux_wasm::{Multiplexor, MuxStream}; use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector}; use tokio_util::{ @@ -226,7 +226,10 @@ impl EpoxyClient { protocols: Vec, origin: String, ) -> Result { - EpxWebSocket::connect(self, onopen, onclose, onerror, onmessage, url, protocols, origin).await + EpxWebSocket::connect( + self, onopen, onclose, onerror, onmessage, url, protocols, origin, + ) + .await } pub async fn fetch(&self, url: String, options: Object) -> Result { diff --git a/client/src/web/index.html b/client/src/web/index.html deleted file mode 100644 index 2237f3d..0000000 --- a/client/src/web/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - epoxy - - - - - running... (wait for the browser alert if not running ws test) - - diff --git a/client/src/websocket.rs b/client/src/websocket.rs index 56161ce..42937b8 100644 --- a/client/src/websocket.rs +++ b/client/src/websocket.rs @@ -26,6 +26,7 @@ pub struct EpxWebSocket { #[wasm_bindgen] impl EpxWebSocket { #[wasm_bindgen(constructor)] + /// DO NOT CALL THIS!!!!!!!!!!!!!!!!!!! pub fn new() -> Result { Err(jerr!("Use EpoxyClient.connect_ws() instead.")) } @@ -156,7 +157,8 @@ impl EpxWebSocket { let (tx, rx) = oneshot::channel(); self.msg_sender.send(EpxMsg::SendText(payload, tx)).await?; Ok(rx.await??) - }.await; + } + .await; if let Err(ret) = ret { let _ = onerr.call1(&JsValue::null(), &jval!(ret.clone())); Err(ret) diff --git a/package.json b/package.json new file mode 100644 index 0000000..f9916ae --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "@mercuryworkshop/epoxy-tls", + "version": "1.0.1", + "description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser", + "scripts": { + "build": "cd client && ./build.sh" + }, + "keywords": [ + "wasm", + "ssl", + "tls", + "rust", + "proxy", + "http" + ], + "author": "MercuryWorkshop", + "repository": "https://github.com/MercuryWorkshop/epoxy-tls", + "license": "MIT", + "browser": "./client/module.js", + "module": "./client/module.js", + "main": "./client/module.js", + "types": "./client/module.d.ts", + "devDependencies": { + "esbuild": "^0.19.11", + "esbuild-plugin-wasm": "^1.1.0" + } +}