Begin to work on workerware.
This commit is contained in:
parent
9e0360cb2b
commit
4395c41041
6 changed files with 51 additions and 10 deletions
|
|
@ -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: [
|
||||
|
|
|
|||
2
index.js
2
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/"));
|
||||
|
|
|
|||
4
masqr.js
4
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),
|
||||
|
|
|
|||
20
public/sw.js
20
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)) {
|
||||
|
|
|
|||
28
public/workerware/workerware.js
Normal file
28
public/workerware/workerware.js
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue