Begin to work on workerware.

This commit is contained in:
wearrrrr 2024-06-16 20:44:25 -05:00
parent 9e0360cb2b
commit 4395c41041
6 changed files with 51 additions and 10 deletions

View file

@ -3,7 +3,6 @@ import node from "@astrojs/node";
import sitemap from "@astrojs/sitemap";
// https://astro.build/config
export default defineConfig({
site: "https://aluu.xyz",
integrations: [

View file

@ -87,7 +87,7 @@ app.use("/custom-favicon", async (req, res) => {
res.set("Content-Type", "image/png");
res.send(buffer);
} catch {
res.sendStatus(500);
}
});
app.use("/", express.static("dist/client/"));

View file

@ -1,4 +1,5 @@
import path from "path";
import fs from "fs"
const failureFile = fs.readFileSync("Checkfailed.html", "utf8");
@ -33,9 +34,6 @@ export async function masqrCheck(config) {
const licenseCheck = (
await (await fetch(config.licenseServer + pass + "&host=" + req.headers.host)).json()
)["status"];
console.log(
config.licenseServer + pass + "&host=" + req.headers.host + " returned " + licenseCheck
);
if (licenseCheck == "License valid") {
res.cookie("authcheck", "true", {
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),

View file

@ -4,10 +4,28 @@ importScripts("/bare_transport.js");
importScripts("/uv/uv.bundle.js");
importScripts("/uv.config.js");
importScripts(__uv$config.sw);
importScripts("./workerware/workerware.js");
const ww = new WorkerWare({
debug: true,
});
function logContext(ctx, event) {
console.log("Context:", ctx);
console.log("Event:", event);
return void 0;
}
ww.use(logContext)
const uv = new UVServiceWorker();
self.addEventListener("fetch", (event) => {
self.addEventListener("fetch", async (event) => {
let middleware = await ww.run(self, event)();
if (middleware.includes(null)) {
console.log("Aborting Request!")
return;
}
event.respondWith(
(async () => {
if (event.request.url.startsWith(location.origin + __uv$config.prefix)) {

View file

@ -0,0 +1,28 @@
const dbg = console.log.bind(console, "[WorkerWare]");
const defaultOpt = {
debug: false,
}
class WorkerWare {
constructor(opt) {
this._opt = opt;
this._middlewares = [];
}
use(fn) {
if (typeof fn !== 'function') throw new TypeError('[WorkerWare] Middleware must be a function!');
if (this._opt.debug) dbg("Added middleware", fn.name || "<anonymous>");
this._middlewares.push(fn);
}
run(ctx, event) {
const middlewares = this._middlewares;
const returnList = [];
let fn = async () => {
for (let i = 0; i < middlewares.length; i++) {
returnList.push(await middlewares[i](ctx, event));
}
return returnList;
};
return fn;
}
}

View file

@ -72,10 +72,8 @@ export async function registerSW() {
});
return new Promise(async (resolve) => {
await navigator.serviceWorker.register("/sw.js").then((registration) => {
registration.update().then(() => {
console.log("Registered SW!");
resolve(null);
});
console.log("Registered SW!")
resolve(null);
});
});
}