npm packaging

This commit is contained in:
CoolElectronics 2024-01-15 14:37:55 -05:00
parent 6e3f968f9c
commit 656eb67bfb
No known key found for this signature in database
GPG key ID: F63593D168636C50
9 changed files with 82 additions and 24 deletions

View file

@ -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<String>,
origin: String,
) -> Result<EpxWebSocket, JsError> {
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<web_sys::Response, JsError> {

View file

@ -1,10 +0,0 @@
<html>
<head>
<title>epoxy</title>
<script src="epoxy_client_bundled.js"></script>
<script src="index.js"></script>
</head>
<body>
running... (wait for the browser alert if not running ws test)
</body>
</html>

View file

@ -1,81 +0,0 @@
(async () => {
console.log(
"%cWASM is significantly slower with DevTools open!",
"color:red;font-size:3rem;font-weight:bold"
);
const should_feature_test = (new URL(window.location.href)).searchParams.has("feature_test");
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();
const tconn0 = performance.now();
// args: websocket url, user agent, redirect limit
let epoxy_client = await new epoxy.EpoxyClient("wss://localhost:4000", navigator.userAgent, 10);
const tconn1 = performance.now();
console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`);
if (should_feature_test) {
for (const url of [
["https://httpbin.org/get", {}],
["https://httpbin.org/gzip", {}],
["https://httpbin.org/brotli", {}],
["https://httpbin.org/redirect/11", {}],
["https://httpbin.org/redirect/1", { redirect: "manual" }]
]) {
let resp = await epoxy_client.fetch(url[0], url[1]);
console.warn(url, resp, Object.fromEntries(resp.headers));
console.warn(await resp.text());
}
} else if (should_perf_test) {
const test_mux = async (url) => {
const t0 = performance.now();
await epoxy_client.fetch(url);
const t1 = performance.now();
return t1 - t0;
};
const test_native = async (url) => {
const t0 = performance.now();
await fetch(url);
const t1 = performance.now();
return t1 - t0;
};
const num_tests = 10;
let total_mux = 0;
for (const _ of Array(num_tests).keys()) {
total_mux += await test_mux("https://httpbin.org/get");
}
total_mux = total_mux / num_tests;
let total_native = 0;
for (const _ of Array(num_tests).keys()) {
total_native += await test_native("https://httpbin.org/get");
}
total_native = total_native / num_tests;
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`);
} else if (should_ws_test) {
let ws = await epoxy_client.connect_ws(
() => console.log("opened"),
() => console.log("closed"),
err => console.error(err),
msg => console.log(msg),
"ws://localhost:9000",
[],
"localhost"
);
await ws.send("data");
} else {
let resp = await epoxy_client.fetch("https://httpbin.org/get");
console.warn(resp, Object.fromEntries(resp.headers));
console.warn(await resp.text());
}
if (!should_ws_test) alert("you can open console now");
})();

View file

@ -26,6 +26,7 @@ pub struct EpxWebSocket {
#[wasm_bindgen]
impl EpxWebSocket {
#[wasm_bindgen(constructor)]
/// DO NOT CALL THIS!!!!!!!!!!!!!!!!!!!
pub fn new() -> Result<EpxWebSocket, JsError> {
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)