Move transport manager to ts file, hopefully libcurl will work sometime in the following days
This commit is contained in:
parent
1393206d07
commit
a5d590b7ab
10 changed files with 139 additions and 78 deletions
20
index.js
20
index.js
|
|
@ -1,7 +1,7 @@
|
|||
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
||||
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
||||
import { baremuxPath } from "@mercuryworkshop/bare-mux";
|
||||
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
||||
import { createBareServer } from "@tomphttp/bare-server-node";
|
||||
import express from "express";
|
||||
import { createServer } from "http";
|
||||
import path from "node:path";
|
||||
|
|
@ -21,6 +21,8 @@ const failureFile = fs.readFileSync("Checkfailed.html", "utf8");
|
|||
|
||||
if (!existsSync("./dist")) build();
|
||||
|
||||
const bare = createBareServer("/bare/");
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
console.log(chalk.gray("Starting Rammerhead..."));
|
||||
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("/uv/", express.static(uvPath));
|
||||
app.use("/epoxy/", express.static(epoxyPath));
|
||||
app.use("/baremux/", express.static(baremuxPath));
|
||||
// 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("/libcurl/", express.static(libcurlPath))
|
||||
|
||||
app.use(express.json());
|
||||
app.use(
|
||||
express.urlencoded({
|
||||
|
|
@ -158,7 +156,9 @@ app.get("*", function (req, res) {
|
|||
|
||||
let server = createServer();
|
||||
server.on("request", (req, res) => {
|
||||
if (shouldRouteRh(req)) {
|
||||
if (bare.shouldRoute(req)) {
|
||||
bare.routeRequest(req, res);
|
||||
} else if (shouldRouteRh(req)) {
|
||||
routeRhRequest(req, res);
|
||||
} else {
|
||||
app(req, res);
|
||||
|
|
@ -166,7 +166,9 @@ server.on("request", (req, res) => {
|
|||
});
|
||||
|
||||
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);
|
||||
} else if (req.url.endsWith("/")) {
|
||||
wisp.routeRequest(req, socket, head);
|
||||
|
|
|
|||
59
package-lock.json
generated
59
package-lock.json
generated
|
|
@ -14,6 +14,7 @@
|
|||
"@mercuryworkshop/libcurl-transport": "^1.2.4",
|
||||
"@titaniumnetwork-dev/ultraviolet": "^3.0.0",
|
||||
"@tomphttp/bare-client": "^2.2.0-alpha",
|
||||
"@tomphttp/bare-server-node": "^2.0.3",
|
||||
"astro": "^4.4.1",
|
||||
"chalk": "^5.3.0",
|
||||
"compression": "^1.7.4",
|
||||
|
|
@ -1327,6 +1328,34 @@
|
|||
"resolved": "https://registry.npmjs.org/@tomphttp/bare-client/-/bare-client-2.2.0-alpha.tgz",
|
||||
"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": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz",
|
||||
|
|
@ -2059,6 +2088,11 @@
|
|||
"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": {
|
||||
"version": "3.3.0",
|
||||
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz",
|
||||
|
|
@ -2407,6 +2441,14 @@
|
|||
"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": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz",
|
||||
|
|
@ -10139,6 +10181,14 @@
|
|||
"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": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
|
||||
|
|
@ -10160,6 +10210,15 @@
|
|||
"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": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.1.tgz",
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
"@mercuryworkshop/libcurl-transport": "^1.2.4",
|
||||
"@titaniumnetwork-dev/ultraviolet": "^3.0.0",
|
||||
"@tomphttp/bare-client": "^2.2.0-alpha",
|
||||
"@tomphttp/bare-server-node": "^2.0.3",
|
||||
"astro": "^4.4.1",
|
||||
"chalk": "^5.3.0",
|
||||
"compression": "^1.7.4",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
importScripts("/epoxy/index.js");
|
||||
importScripts("/libcurl/index.cjs");
|
||||
importScripts("/libcurl/index.js");
|
||||
importScripts("/uv/uv.bundle.js");
|
||||
importScripts("/uv.config.js");
|
||||
importScripts(__uv$config.sw);
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ const openPageWith = [
|
|||
];
|
||||
|
||||
const transportsList = [
|
||||
{ name: "Bare", value: "BareMod.BareClient" },
|
||||
{ name: "Epoxy", value: "EpxMod.EpoxyClient" },
|
||||
{ name: "Libcurl", value: "CurlMod.LibcurlClient" }
|
||||
{ name: "Libcurl", value: "CurlMod.LibcurlClient" },
|
||||
{ name: "Bare", value: "BareMod.BareClient" },
|
||||
]
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -294,6 +294,7 @@ const t = useTranslations(lang);
|
|||
applySavedLocalStorage("alu__selectedProxy", "dropdown__selected-proxy");
|
||||
applySavedLocalStorage("alu__search_engine", "dropdown__search-engine");
|
||||
applySavedLocalStorage("alu__selectedOpenWith", "dropdown__open-with");
|
||||
applySavedLocalStorage("alu__selectedTransport", "dropdown__transport");
|
||||
let selectedProxyDropdown = document.getElementById("dropdown__selected-proxy-menu");
|
||||
let searchEngineDropdown = document.getElementById("dropdown__search-engine-menu");
|
||||
let openWithDropdown = document.getElementById("dropdown__open-with-menu");
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
60
src/components/ts/TransportManager.ts
Normal file
60
src/components/ts/TransportManager.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -3,7 +3,6 @@ import Layout from "../../layouts/Layout.astro";
|
|||
|
||||
import { STATIC_PATHS, getLangFromUrl, useTranslations } from "../../i18n/utils";
|
||||
import Link from "../../components/Link.astro";
|
||||
import TransportManager from "../../components/TransportManager.astro";
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const t = useTranslations(lang);
|
||||
|
||||
|
|
@ -13,11 +12,8 @@ export function getStaticPaths() {
|
|||
---
|
||||
|
||||
<Layout title={t("pages.home")}>
|
||||
<script src="/libcurl/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">
|
||||
<h1 class="title-text">{t("menu.welcome")}</h1>
|
||||
<div class="form-wrapper">
|
||||
|
|
@ -68,8 +64,9 @@ export function getStaticPaths() {
|
|||
</div>
|
||||
</div>
|
||||
</Layout>
|
||||
|
||||
<script>
|
||||
import { initTransport } from "../../components/ts/TransportManager"
|
||||
initTransport();
|
||||
type Suggestion = {
|
||||
phrase: string;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"extends": "astro/tsconfigs/strict",
|
||||
"exclude": ["dist/**", "node_modules/**"],
|
||||
"exclude": ["dist/**", "node_modules/**", "public/games/**"],
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue