Format shit

This commit is contained in:
MotorTruck1221
2025-05-04 23:57:30 -06:00
parent a78ab8e1b2
commit 5f640d8e06
21 changed files with 338 additions and 298 deletions
+69 -47
View File
@@ -2,35 +2,35 @@ import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
import { StoreManager } from "./storage";
const createScript = (src: string, defer?: boolean) => {
const script = document.createElement('script') as HTMLScriptElement;
const script = document.createElement("script") as HTMLScriptElement;
script.src = src;
if (defer) script.defer = defer;
return document.body.appendChild(script);
}
};
/**
* This class automatically sets up and handles lots of stuff for us.
*
* It registers/fixes errors with SW reg
* It creates our bareMux worker
* And other stuff.
*
* @example
* import { SW } from "@utils/proxy.ts";
* const handler = new SW();
* //Consume the methods
* // Or if an instance is already running
* import { SW } from "@utils/proxy.ts";
* const handler = SW.getInstance();
* //Consume the methods
*/
* This class automatically sets up and handles lots of stuff for us.
*
* It registers/fixes errors with SW reg
* It creates our bareMux worker
* And other stuff.
*
* @example
* import { SW } from "@utils/proxy.ts";
* const handler = new SW();
* //Consume the methods
* // Or if an instance is already running
* import { SW } from "@utils/proxy.ts";
* const handler = SW.getInstance();
* //Consume the methods
*/
class SW {
#baremuxConn?: BareMuxConnection;
#scramjetController?: ScramjetController;
#serviceWorker?: ServiceWorkerRegistration;
#storageManager: StoreManager<"radius||settings">;
static #instance = new Set();
static #instance = new Set();
static *getInstance() {
for (const val of SW.#instance.keys()) {
yield val as SW;
@@ -38,41 +38,59 @@ class SW {
}
#search(input: string, template: string) {
try { return new URL(input).toString() } catch (_) {};
try {
return new URL(input).toString();
} catch (_) {}
try {
const url = new URL(`http://${input}`);
if (url.hostname.includes(".")) return url.toString();
} catch (_) {};
} catch (_) {}
return template.replace("%s", encodeURIComponent(input));
}
encodeURL(string: string): string {
const proxy = this.#storageManager.getVal("proxy") as 'uv' | 'sj';
const input = this.#search(string, this.#storageManager.getVal('searchEngine'));
return proxy === 'uv' ? `${__uv$config.prefix}${__uv$config.encodeUrl!(input)}` : this.#scramjetController!.encodeUrl(input)
const proxy = this.#storageManager.getVal("proxy") as "uv" | "sj";
const input = this.#search(string, this.#storageManager.getVal("searchEngine"));
return proxy === "uv"
? `${__uv$config.prefix}${__uv$config.encodeUrl!(input)}`
: this.#scramjetController!.encodeUrl(input);
}
async setTransport(transport?: 'epoxy' | 'libcurl', get?: boolean) {
console.log('Setting transport');
if (get) return this.#storageManager.getVal('transport');
this.#storageManager.setVal("transport", transport || this.#storageManager.getVal("transport") || 'epoxy');
switch(transport) {
case 'epoxy': {
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [ { wisp: this.#storageManager.getVal('wispServer') }]);
async setTransport(transport?: "epoxy" | "libcurl", get?: boolean) {
console.log("Setting transport");
if (get) return this.#storageManager.getVal("transport");
this.#storageManager.setVal(
"transport",
transport || this.#storageManager.getVal("transport") || "epoxy"
);
switch (transport) {
case "epoxy": {
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") }
]);
}
case 'libcurl': {
await this.#baremuxConn!.setTransport("/libcurl/index.mjs", [ { wisp: this.#storageManager.getVal('wispServer') }]);
case "libcurl": {
await this.#baremuxConn!.setTransport("/libcurl/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") }
]);
}
default: {
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [ { wisp: this.#storageManager.getVal('wispServer') }]);
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") }
]);
}
}
}
async wispServer(wispServer?: string, set?: true) {
this.#storageManager.setVal("wispServer", wispServer || this.#storageManager.getVal('wispServer') || (location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/wisp/");
this.#storageManager.setVal(
"wispServer",
wispServer ||
this.#storageManager.getVal("wispServer") ||
(location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/wisp/"
);
if (set) await this.setTransport();
}
@@ -82,22 +100,25 @@ class SW {
const checkScripts = (): Promise<void> => {
return new Promise((resolve) => {
const t = setInterval(() => {
if (typeof __uv$config !== 'undefined' && typeof ScramjetController !== 'undefined') {
if (
typeof __uv$config !== "undefined" &&
typeof ScramjetController !== "undefined"
) {
clearInterval(t);
resolve();
}
});
});
};
createScript('/vu/uv.bundle.js', true);
createScript('/vu/uv.config.js', true);
createScript('/marcs/scramjet.controller.js', true);
createScript("/vu/uv.bundle.js", true);
createScript("/vu/uv.config.js", true);
createScript("/marcs/scramjet.controller.js", true);
checkScripts().then(async () => {
this.#baremuxConn = new BareMuxConnection("/erab/worker.js");
await this.setTransport();
this.#scramjetController = new ScramjetController({
prefix: '/~/scramjet/',
prefix: "/~/scramjet/",
files: {
wasm: "/marcs/scramjet.wasm.wasm",
worker: "/marcs/scramjet.worker.js",
@@ -109,19 +130,20 @@ class SW {
rewriterLogs: false
}
});
if ("serviceWorker" in navigator) {
if ("serviceWorker" in navigator) {
await this.#scramjetController.init();
navigator.serviceWorker.ready.then(async (reg) => {
console.log('SW ready to go!');
console.log("SW ready to go!");
this.#serviceWorker = reg;
});
navigator.serviceWorker.register("/sw.js", { scope: '/' });
}
else {
throw new Error('Your browser is not supported! This website uses Service Workers heavily.');
navigator.serviceWorker.register("/sw.js", { scope: "/" });
} else {
throw new Error(
"Your browser is not supported! This website uses Service Workers heavily."
);
}
});
};
}
}
export { SW };
+51 -51
View File
@@ -3,37 +3,37 @@ import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
import { SW } from "@utils/proxy.ts";
import { SearchEngines } from "./types";
/**
* The settings class
* Initializes it's own StorageManager, and handles everything within the class itself
*
* @example
* // Create a new Settings instance (needs to be done only once)
* import { Settings } from "@utils/settings.ts";
* const settings = new Settings();
* //Consume any of the methods with:
* settings.methodName();
*
* // Most of the time, you'll want to get the running instance this can be done with
* import { Settings } from "@utils/settings.ts";
* const settings = await Settings.getInstance();
* //Consume any of the methods with:
* settings.methodName();
*/
* The settings class
* Initializes it's own StorageManager, and handles everything within the class itself
*
* @example
* // Create a new Settings instance (needs to be done only once)
* import { Settings } from "@utils/settings.ts";
* const settings = new Settings();
* //Consume any of the methods with:
* settings.methodName();
*
* // Most of the time, you'll want to get the running instance this can be done with
* import { Settings } from "@utils/settings.ts";
* const settings = await Settings.getInstance();
* //Consume any of the methods with:
* settings.methodName();
*/
class Settings {
// Our own internal StorageManager so things never interfere
#storageManager: StoreManager<"radius||settings">;
static #instance = new Set();
static #instance = new Set();
/**
* Method to get the current or other Settings instance(s)
*
*
* @example
* const settings = await Settings.getInstance();
* // Consume the other methods
*/
* Method to get the current or other Settings instance(s)
*
*
* @example
* const settings = await Settings.getInstance();
* // Consume the other methods
*/
static async getInstance() {
function *get() {
function* get() {
for (const instance of Settings.#instance.keys()) {
yield instance!;
}
@@ -47,50 +47,50 @@ class Settings {
resolve(true);
}
}, 100);
})
}
});
};
await ready();
return get().next().value! as Settings;
}
/**
* Set's the theme either to the current theme OR to a new one
*
* @example
* // Retrieve the Settings instance
* const settings = await Settings.getInstance();
*
* // Consume the method
* settings.theme() // Whatever value is in localstorage at the time
* settings.theme('theme name') // A new theme based off of the class name
*/
* Set's the theme either to the current theme OR to a new one
*
* @example
* // Retrieve the Settings instance
* const settings = await Settings.getInstance();
*
* // Consume the method
* settings.theme() // Whatever value is in localstorage at the time
* settings.theme('theme name') // A new theme based off of the class name
*/
theme(theme?: string) {
this.#storageManager.setVal('theme', theme || this.#storageManager.getVal('theme'));
theme === 'default'
? document.documentElement.className = 'default'
: document.documentElement.className = theme || this.#storageManager.getVal('theme');
this.#storageManager.setVal("theme", theme || this.#storageManager.getVal("theme"));
theme === "default"
? (document.documentElement.className = "default")
: (document.documentElement.className = theme || this.#storageManager.getVal("theme"));
}
proxy(prox?: 'uv' | 'sj') {
this.#storageManager.setVal('proxy', prox || 'uv');
proxy(prox?: "uv" | "sj") {
this.#storageManager.setVal("proxy", prox || "uv");
}
searchEngine(engine?: string) {
this.#storageManager.setVal('searchEngine', engine || SearchEngines.DuckDuckGo);
this.#storageManager.setVal("searchEngine", engine || SearchEngines.DuckDuckGo);
}
async *#init() {
yield this.theme(this.#storageManager.getVal('theme') || 'default');
yield this.theme(this.#storageManager.getVal("theme") || "default");
}
constructor() {
this.#storageManager = new StoreManager("radius||settings");
Settings.#instance.add(this);
(async() => {
for await (const _ of this.#init());
(async () => {
for await (const _ of this.#init());
})();
}
}
export { Settings }
export { Settings };
+1 -1
View File
@@ -14,4 +14,4 @@ class StoreManager<Prefix extends string> {
}
}
export { StoreManager }
export { StoreManager };
+2 -2
View File
@@ -1,7 +1,7 @@
import type { Props } from "astro";
interface SettingsProps extends Props {
active: 'appearance' | 'credits' | 'links' | 'proxy';
active: "appearance" | "credits" | "links" | "proxy";
title: string;
}
@@ -14,7 +14,7 @@ type DropdownOptions = {
const SearchEngines: Record<string, string> = {
DuckDuckGo: "https://duckduckgo.com/?q=%s",
Google: "https://google.com/search?q=%s",
Bing: "https://bing.com/search?q=%s",
Bing: "https://bing.com/search?q=%s"
};
export { type SettingsProps, type DropdownOptions, SearchEngines };