Move transport manager to ts file, hopefully libcurl will work sometime in the following days

This commit is contained in:
wearrrrr 2024-03-16 23:06:33 -05:00
parent 1393206d07
commit a5d590b7ab
10 changed files with 139 additions and 78 deletions

View file

@ -1,7 +1,7 @@
import { uvPath } from "@titaniumnetwork-dev/ultraviolet"; import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport"; import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { baremuxPath } from "@mercuryworkshop/bare-mux";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport"; import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { createBareServer } from "@tomphttp/bare-server-node";
import express from "express"; import express from "express";
import { createServer } from "http"; import { createServer } from "http";
import path from "node:path"; import path from "node:path";
@ -21,6 +21,8 @@ const failureFile = fs.readFileSync("Checkfailed.html", "utf8");
if (!existsSync("./dist")) build(); if (!existsSync("./dist")) build();
const bare = createBareServer("/bare/");
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
console.log(chalk.gray("Starting Rammerhead...")); console.log(chalk.gray("Starting Rammerhead..."));
const rh = createRammerhead(); const rh = createRammerhead();
@ -114,12 +116,8 @@ app.use(express.static(path.join(process.cwd(), "static")));
app.use(express.static(path.join(process.cwd(), "build"))); app.use(express.static(path.join(process.cwd(), "build")));
app.use("/uv/", express.static(uvPath)); app.use("/uv/", express.static(uvPath));
app.use("/epoxy/", express.static(epoxyPath)); app.use("/epoxy/", express.static(epoxyPath));
app.use("/baremux/", express.static(baremuxPath)); app.use("/libcurl/", express.static(libcurlPath))
// Make libcurl a middleware that will send application/javascript and serve the path
app.use("/libcurl/", (req, res, next) => {
res.setHeader("Content-Type", "application/javascript");
next();
}, express.static(libcurlPath));
app.use(express.json()); app.use(express.json());
app.use( app.use(
express.urlencoded({ express.urlencoded({
@ -158,7 +156,9 @@ app.get("*", function (req, res) {
let server = createServer(); let server = createServer();
server.on("request", (req, res) => { server.on("request", (req, res) => {
if (shouldRouteRh(req)) { if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else if (shouldRouteRh(req)) {
routeRhRequest(req, res); routeRhRequest(req, res);
} else { } else {
app(req, res); app(req, res);
@ -166,7 +166,9 @@ server.on("request", (req, res) => {
}); });
server.on("upgrade", (req, socket, head) => { server.on("upgrade", (req, socket, head) => {
if (shouldRouteRh(req)) { if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else if (shouldRouteRh(req)) {
routeRhUpgrade(req, socket, head); routeRhUpgrade(req, socket, head);
} else if (req.url.endsWith("/")) { } else if (req.url.endsWith("/")) {
wisp.routeRequest(req, socket, head); wisp.routeRequest(req, socket, head);

59
package-lock.json generated
View file

@ -14,6 +14,7 @@
"@mercuryworkshop/libcurl-transport": "^1.2.4", "@mercuryworkshop/libcurl-transport": "^1.2.4",
"@titaniumnetwork-dev/ultraviolet": "^3.0.0", "@titaniumnetwork-dev/ultraviolet": "^3.0.0",
"@tomphttp/bare-client": "^2.2.0-alpha", "@tomphttp/bare-client": "^2.2.0-alpha",
"@tomphttp/bare-server-node": "^2.0.3",
"astro": "^4.4.1", "astro": "^4.4.1",
"chalk": "^5.3.0", "chalk": "^5.3.0",
"compression": "^1.7.4", "compression": "^1.7.4",
@ -1327,6 +1328,34 @@
"resolved": "https://registry.npmjs.org/@tomphttp/bare-client/-/bare-client-2.2.0-alpha.tgz", "resolved": "https://registry.npmjs.org/@tomphttp/bare-client/-/bare-client-2.2.0-alpha.tgz",
"integrity": "sha512-xhcflOpwr92tkpp8SoDhB3nK3LHMBIjx+vgow37XobQew2k0/mXSxmaU7BsDFpOIa1CcLCEsR8gWn0v7Cy9+7Q==" "integrity": "sha512-xhcflOpwr92tkpp8SoDhB3nK3LHMBIjx+vgow37XobQew2k0/mXSxmaU7BsDFpOIa1CcLCEsR8gWn0v7Cy9+7Q=="
}, },
"node_modules/@tomphttp/bare-server-node": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@tomphttp/bare-server-node/-/bare-server-node-2.0.3.tgz",
"integrity": "sha512-IGzZspDwzto+oPsvlV99OALJKH3X1nRWnpBE8EY6nrqu5I83xw3uSUacEdHNnW4rXG0IQ8vZwMH87VOMoArJ3A==",
"dependencies": {
"async-exit-hook": "^2.0.1",
"commander": "^10.0.1",
"dotenv": "^16.0.3",
"http-errors": "^2.0.0",
"ipaddr.js": "^2.1.0",
"source-map-support": "^0.5.21",
"ws": "^8.13.0"
},
"bin": {
"bare-server-node": "bin.js"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@tomphttp/bare-server-node/node_modules/ipaddr.js": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.1.0.tgz",
"integrity": "sha512-LlbxQ7xKzfBusov6UMi4MFpEg0m+mAm9xyNGEduwXMEDuf4WfzB/RZwMVYEd7IKGvh4IUkEXYxtAVu9T3OelJQ==",
"engines": {
"node": ">= 10"
}
},
"node_modules/@tootallnate/once": { "node_modules/@tootallnate/once": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz",
@ -2059,6 +2088,11 @@
"ieee754": "^1.2.1" "ieee754": "^1.2.1"
} }
}, },
"node_modules/buffer-from": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ=="
},
"node_modules/builtin-modules": { "node_modules/builtin-modules": {
"version": "3.3.0", "version": "3.3.0",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz",
@ -2407,6 +2441,14 @@
"url": "https://github.com/sponsors/wooorm" "url": "https://github.com/sponsors/wooorm"
} }
}, },
"node_modules/commander": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz",
"integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==",
"engines": {
"node": ">=14"
}
},
"node_modules/common-ancestor-path": { "node_modules/common-ancestor-path": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz", "resolved": "https://registry.npmjs.org/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz",
@ -10139,6 +10181,14 @@
"node": ">= 10" "node": ">= 10"
} }
}, },
"node_modules/source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/source-map-js": { "node_modules/source-map-js": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
@ -10160,6 +10210,15 @@
"urix": "^0.1.0" "urix": "^0.1.0"
} }
}, },
"node_modules/source-map-support": {
"version": "0.5.21",
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz",
"integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==",
"dependencies": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"
}
},
"node_modules/source-map-url": { "node_modules/source-map-url": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.1.tgz", "resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.1.tgz",

View file

@ -15,6 +15,7 @@
"@mercuryworkshop/libcurl-transport": "^1.2.4", "@mercuryworkshop/libcurl-transport": "^1.2.4",
"@titaniumnetwork-dev/ultraviolet": "^3.0.0", "@titaniumnetwork-dev/ultraviolet": "^3.0.0",
"@tomphttp/bare-client": "^2.2.0-alpha", "@tomphttp/bare-client": "^2.2.0-alpha",
"@tomphttp/bare-server-node": "^2.0.3",
"astro": "^4.4.1", "astro": "^4.4.1",
"chalk": "^5.3.0", "chalk": "^5.3.0",
"compression": "^1.7.4", "compression": "^1.7.4",

View file

@ -1,5 +1,5 @@
importScripts("/epoxy/index.js"); importScripts("/epoxy/index.js");
importScripts("/libcurl/index.cjs"); importScripts("/libcurl/index.js");
importScripts("/uv/uv.bundle.js"); importScripts("/uv/uv.bundle.js");
importScripts("/uv.config.js"); importScripts("/uv.config.js");
importScripts(__uv$config.sw); importScripts(__uv$config.sw);

View file

@ -27,9 +27,9 @@ const openPageWith = [
]; ];
const transportsList = [ const transportsList = [
{ name: "Bare", value: "BareMod.BareClient" },
{ name: "Epoxy", value: "EpxMod.EpoxyClient" }, { name: "Epoxy", value: "EpxMod.EpoxyClient" },
{ name: "Libcurl", value: "CurlMod.LibcurlClient" } { name: "Libcurl", value: "CurlMod.LibcurlClient" },
{ name: "Bare", value: "BareMod.BareClient" },
] ]
--- ---

View file

@ -294,6 +294,7 @@ const t = useTranslations(lang);
applySavedLocalStorage("alu__selectedProxy", "dropdown__selected-proxy"); applySavedLocalStorage("alu__selectedProxy", "dropdown__selected-proxy");
applySavedLocalStorage("alu__search_engine", "dropdown__search-engine"); applySavedLocalStorage("alu__search_engine", "dropdown__search-engine");
applySavedLocalStorage("alu__selectedOpenWith", "dropdown__open-with"); applySavedLocalStorage("alu__selectedOpenWith", "dropdown__open-with");
applySavedLocalStorage("alu__selectedTransport", "dropdown__transport");
let selectedProxyDropdown = document.getElementById("dropdown__selected-proxy-menu"); let selectedProxyDropdown = document.getElementById("dropdown__selected-proxy-menu");
let searchEngineDropdown = document.getElementById("dropdown__search-engine-menu"); let searchEngineDropdown = document.getElementById("dropdown__search-engine-menu");
let openWithDropdown = document.getElementById("dropdown__open-with-menu"); let openWithDropdown = document.getElementById("dropdown__open-with-menu");

View file

@ -1,59 +0,0 @@
<script>
// @ts-ignore
// I HATE YOU VSCODE AAAAAAAAAAAAAA
import { SetTransport, registerRemoteListener } from "@mercuryworkshop/bare-mux";
// @ts-check
declare global {
interface Window {
__uv$config: {
prefix: string;
};
}
};
type transportConfig = {
wisp: string;
wasm?: string;
}
const wispURLDefault =
(location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/";
class TransportManager {
private transport = "EpxMod.EpoxyClient";
constructor(transport?: string) {
if (transport) {
this.transport = transport;
}
if (localStorage.getItem("alu__selectedTransport") != null && !transport) {
this.transport = JSON.parse(localStorage.getItem("alu__selectedTransport")!).value;
}
if (localStorage.getItem("alu__selectedTransport") == null) {
// Set the default transport for the next reload.
localStorage.setItem("alu__selectedTransport", JSON.stringify({ value: this.transport }));
}
}
getTransport() {
return this.transport;
}
setTransport(transport: string, wispURL = wispURLDefault) {
this.transport = transport;
let transportConfig: transportConfig = { wisp: wispURL };
if (this.transport == "CurlMod.LibcurlClient") {
transportConfig.wasm = "https://cdn.jsdelivr.net/npm/libcurl.js@latest/libcurl.wasm";
}
SetTransport(this.transport, transportConfig);
}
}
const TransportMgr = new TransportManager();
registerRemoteListener(navigator.serviceWorker.controller!);
let swRegister = navigator.serviceWorker.register("/sw.js", {
scope: window.__uv$config.prefix,
});
swRegister.then((registration) => {
registration.update().then(() => {
TransportMgr.setTransport(TransportMgr.getTransport(), localStorage.getItem("alu__wispUrl") || wispURLDefault);
});
});
</script>

View file

@ -0,0 +1,60 @@
// @ts-ignore
// For some reason, VSCode can't find the bare-mux package. It exists and compiling works, but vscode throws a fit.
import { SetTransport, registerRemoteListener } from "@mercuryworkshop/bare-mux";
// @ts-check
declare global {
interface Window {
__uv$config: {
prefix: string;
};
}
};
type transportConfig = {
wisp: string;
wasm?: string;
}
export const wispURLDefault =
(location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/";
export default class TransportManager {
private transport = "EpxMod.EpoxyClient";
constructor(transport?: string) {
if (transport) {
this.transport = transport;
}
if (localStorage.getItem("alu__selectedTransport") != null && !transport) {
this.transport = JSON.parse(localStorage.getItem("alu__selectedTransport")!).value;
}
if (localStorage.getItem("alu__selectedTransport") == null) {
// Set the default transport for the next reload.
localStorage.setItem("alu__selectedTransport", JSON.stringify({ value: this.transport }));
}
}
getTransport() {
return this.transport;
}
setTransport(transport: string, wispURL = wispURLDefault) {
this.transport = transport;
let transportConfig: transportConfig = { wisp: wispURL };
if (this.transport == "CurlMod.LibcurlClient") {
transportConfig.wasm = "https://cdn.jsdelivr.net/npm/libcurl.js@latest/libcurl.wasm";
}
SetTransport(this.transport, transportConfig);
}
}
const TransportMgr = new TransportManager();
export function initTransport() {
registerRemoteListener(navigator.serviceWorker.controller!);
navigator.serviceWorker.register("/sw.js", {
scope: window.__uv$config.prefix,
}).then((registration) => {
registration.update().then(() => {
TransportMgr.setTransport(TransportMgr.getTransport(), localStorage.getItem("alu__wispUrl") || wispURLDefault);
});
});
}

View file

@ -3,7 +3,6 @@ import Layout from "../../layouts/Layout.astro";
import { STATIC_PATHS, getLangFromUrl, useTranslations } from "../../i18n/utils"; import { STATIC_PATHS, getLangFromUrl, useTranslations } from "../../i18n/utils";
import Link from "../../components/Link.astro"; import Link from "../../components/Link.astro";
import TransportManager from "../../components/TransportManager.astro";
const lang = getLangFromUrl(Astro.url); const lang = getLangFromUrl(Astro.url);
const t = useTranslations(lang); const t = useTranslations(lang);
@ -13,11 +12,8 @@ export function getStaticPaths() {
--- ---
<Layout title={t("pages.home")}> <Layout title={t("pages.home")}>
<script src="/libcurl/index.js" is:inline></script>
<script src="/epoxy/index.js" is:inline></script> <script src="/epoxy/index.js" is:inline></script>
<script src="/libcurl/index.cjs" is:inline></script>
<script src="/baremux/bare.cjs" is:inline></script>
<TransportManager />
<div class="main-content"> <div class="main-content">
<h1 class="title-text">{t("menu.welcome")}</h1> <h1 class="title-text">{t("menu.welcome")}</h1>
<div class="form-wrapper"> <div class="form-wrapper">
@ -68,8 +64,9 @@ export function getStaticPaths() {
</div> </div>
</div> </div>
</Layout> </Layout>
<script> <script>
import { initTransport } from "../../components/ts/TransportManager"
initTransport();
type Suggestion = { type Suggestion = {
phrase: string; phrase: string;
}; };

View file

@ -1,4 +1,4 @@
{ {
"extends": "astro/tsconfigs/strict", "extends": "astro/tsconfigs/strict",
"exclude": ["dist/**", "node_modules/**"], "exclude": ["dist/**", "node_modules/**", "public/games/**"],
} }