From 4395c41041b3a0b707970f5a8b842ccebabc4b96 Mon Sep 17 00:00:00 2001 From: wearrrrr Date: Sun, 16 Jun 2024 20:44:25 -0500 Subject: [PATCH] Begin to work on workerware. --- astro.config.mjs | 1 - index.js | 2 +- masqr.js | 4 +--- public/sw.js | 20 ++++++++++++++++++- public/workerware/workerware.js | 28 +++++++++++++++++++++++++++ src/components/ts/TransportManager.ts | 6 ++---- 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 public/workerware/workerware.js diff --git a/astro.config.mjs b/astro.config.mjs index a5e64da..9d9219c 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -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: [ diff --git a/index.js b/index.js index 4a669dd..fb72232 100644 --- a/index.js +++ b/index.js @@ -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/")); diff --git a/masqr.js b/masqr.js index 54c3865..7aef42e 100644 --- a/masqr.js +++ b/masqr.js @@ -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), diff --git a/public/sw.js b/public/sw.js index 9740f12..619c977 100644 --- a/public/sw.js +++ b/public/sw.js @@ -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)) { diff --git a/public/workerware/workerware.js b/public/workerware/workerware.js new file mode 100644 index 0000000..b1fd7a5 --- /dev/null +++ b/public/workerware/workerware.js @@ -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 || ""); + 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; + } +} \ No newline at end of file diff --git a/src/components/ts/TransportManager.ts b/src/components/ts/TransportManager.ts index fbe26ce..b0f5fbe 100644 --- a/src/components/ts/TransportManager.ts +++ b/src/components/ts/TransportManager.ts @@ -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); }); }); }