mirror of
https://github.com/NebulaServices/Nebula.git
synced 2025-05-12 11:30:01 -04:00
Switch workerware to a submodule
This commit is contained in:
parent
7a0695ac9c
commit
5d4397d38b
6 changed files with 13 additions and 174 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "workerware"]
|
||||||
|
path = workerware
|
||||||
|
url = https://github.com/mercuryworkshop/workerware
|
|
@ -11,6 +11,9 @@ import { defineConfig, envField } from "astro/config";
|
||||||
import { viteStaticCopy } from "vite-plugin-static-copy";
|
import { viteStaticCopy } from "vite-plugin-static-copy";
|
||||||
import { version } from "./package.json";
|
import { version } from "./package.json";
|
||||||
import { parsedDoc } from "./server/config.js";
|
import { parsedDoc } from "./server/config.js";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
const workerwarePath = fileURLToPath(new URL('./workerware/src', import.meta.url));
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
experimental: {
|
experimental: {
|
||||||
env: {
|
env: {
|
||||||
|
@ -65,6 +68,11 @@ export default defineConfig({
|
||||||
src: `${baremuxPath}/**/*`.replace(/\\/g, "/"),
|
src: `${baremuxPath}/**/*`.replace(/\\/g, "/"),
|
||||||
dest: "baremux",
|
dest: "baremux",
|
||||||
overwrite: false
|
overwrite: false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: `${workerwarePath}/**/*`.replace(/\\/g, "/"),
|
||||||
|
dest: 'workerware',
|
||||||
|
overwrite: false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
class WWError extends Error {
|
|
||||||
constructor(message) {
|
|
||||||
super(message);
|
|
||||||
this.name = "[WorkerWare Exception]";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,168 +0,0 @@
|
||||||
importScripts("./WWError.js");
|
|
||||||
const dbg = console.log.bind(console, "[WorkerWare]");
|
|
||||||
const time = console.time.bind(console, "[WorkerWare]");
|
|
||||||
const timeEnd = console.timeEnd.bind(console, "[WorkerWare]");
|
|
||||||
|
|
||||||
/*
|
|
||||||
OPTS:
|
|
||||||
debug - Enables debug logging.
|
|
||||||
randomNames - Generate random names for middlewares.
|
|
||||||
timing - Logs timing for each middleware.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const defaultOpt = {
|
|
||||||
debug: false,
|
|
||||||
randomNames: false,
|
|
||||||
timing: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
const validEvents = [
|
|
||||||
"abortpayment",
|
|
||||||
"activate",
|
|
||||||
"backgroundfetchabort",
|
|
||||||
"backgroundfetchclick",
|
|
||||||
"backgroundfetchfail",
|
|
||||||
"backgroundfetchsuccess",
|
|
||||||
"canmakepayment",
|
|
||||||
"contentdelete",
|
|
||||||
"cookiechange",
|
|
||||||
"fetch",
|
|
||||||
"install",
|
|
||||||
"message",
|
|
||||||
"messageerror",
|
|
||||||
"notificationclick",
|
|
||||||
"notificationclose",
|
|
||||||
"paymentrequest",
|
|
||||||
"periodicsync",
|
|
||||||
"push",
|
|
||||||
"pushsubscriptionchange",
|
|
||||||
"sync",
|
|
||||||
];
|
|
||||||
|
|
||||||
class WorkerWare {
|
|
||||||
constructor(opt) {
|
|
||||||
this._opt = Object.assign({}, defaultOpt, opt);
|
|
||||||
this._middlewares = [];
|
|
||||||
}
|
|
||||||
info() {
|
|
||||||
return {
|
|
||||||
version: "0.1.0",
|
|
||||||
middlewares: this._middlewares,
|
|
||||||
options: this._opt,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
use(middleware) {
|
|
||||||
let validateMW = this.validateMiddleware(middleware);
|
|
||||||
if (validateMW.error) throw new WWError(validateMW.error);
|
|
||||||
// This means the middleware is an anonymous function, or the user is silly and named their function "function"
|
|
||||||
if (middleware.function.name == "function") middleware.name = crypto.randomUUID();
|
|
||||||
if (!middleware.name) middleware.name = middleware.function.name;
|
|
||||||
if (this._opt.randomNames) middleware.name = crypto.randomUUID();
|
|
||||||
if (this._opt.debug) dbg("Adding middleware:", middleware.name);
|
|
||||||
this._middlewares.push(middleware);
|
|
||||||
}
|
|
||||||
// Run all middlewares for the event type passed in.
|
|
||||||
run(event) {
|
|
||||||
const middlewares = this._middlewares;
|
|
||||||
const returnList = [];
|
|
||||||
let fn = async () => {
|
|
||||||
for (let i = 0; i < middlewares.length; i++) {
|
|
||||||
if (middlewares[i].events.includes(event.type)) {
|
|
||||||
if (this._opt.timing) console.time(middlewares[i].name);
|
|
||||||
// Add the configuration to the event object.
|
|
||||||
event.workerware = {
|
|
||||||
config: middlewares[i].configuration || {},
|
|
||||||
};
|
|
||||||
if (!middlewares[i].explicitCall) {
|
|
||||||
let res = await middlewares[i].function(event);
|
|
||||||
if (this._opt.timing) console.timeEnd(middlewares[i].name);
|
|
||||||
returnList.push(res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnList;
|
|
||||||
};
|
|
||||||
return fn;
|
|
||||||
}
|
|
||||||
deleteByName(middlewareID) {
|
|
||||||
if (this._opt.debug) dbg("Deleting middleware:", middlewareID);
|
|
||||||
this._middlewares = this._middlewares.filter((mw) => mw.name !== middlewareID);
|
|
||||||
}
|
|
||||||
deleteByEvent(middlewareEvent) {
|
|
||||||
if (this._opt.debug) dbg("Deleting middleware by event:", middlewareEvent);
|
|
||||||
this._middlewares = this._middlewares.filter((mw) => !mw.events.includes(middlewareEvent));
|
|
||||||
}
|
|
||||||
get() {
|
|
||||||
return this._middlewares;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
Run a single middleware by ID.
|
|
||||||
This assumes that the user knows what they're doing, and is running the middleware on an event that it's supposed to run on.
|
|
||||||
*/
|
|
||||||
runMW(name, event) {
|
|
||||||
const middlewares = this._middlewares;
|
|
||||||
if (this._opt.debug) dbg("Running middleware:", name);
|
|
||||||
// if (middlewares.includes(name)) {
|
|
||||||
// return middlewares[name](event);
|
|
||||||
// } else {
|
|
||||||
// throw new WWError("Middleware not found!");
|
|
||||||
// }
|
|
||||||
let didCall = false;
|
|
||||||
for (let i = 0; i < middlewares.length; i++) {
|
|
||||||
if (middlewares[i].name == name) {
|
|
||||||
didCall = true;
|
|
||||||
event.workerware = {
|
|
||||||
config: middlewares[i].configuration || {},
|
|
||||||
}
|
|
||||||
if (this._opt.timing) console.time(middlewares[i].name);
|
|
||||||
let call = middlewares[i].function(event);
|
|
||||||
if (this._opt.timing) console.timeEnd(middlewares[i].name);
|
|
||||||
return call;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!didCall) {
|
|
||||||
throw new WWError("Middleware not found!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// type middlewareManifest = {
|
|
||||||
// function: Function,
|
|
||||||
// name?: string,
|
|
||||||
// events: string[], // Should be a union of validEvents.
|
|
||||||
// configuration?: Object // Optional configuration for the middleware.
|
|
||||||
// }
|
|
||||||
validateMiddleware(middleware) {
|
|
||||||
if (!middleware.function)
|
|
||||||
return {
|
|
||||||
error: "middleware.function is required",
|
|
||||||
};
|
|
||||||
if (typeof middleware.function !== "function")
|
|
||||||
return {
|
|
||||||
error: "middleware.function must be typeof function",
|
|
||||||
};
|
|
||||||
if (typeof middleware.configuration !== "object" && middleware.configuration !== undefined) {
|
|
||||||
return {
|
|
||||||
error: "middleware.configuration must be typeof object",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (!middleware.events)
|
|
||||||
return {
|
|
||||||
error: "middleware.events is required",
|
|
||||||
};
|
|
||||||
if (!Array.isArray(middleware.events))
|
|
||||||
return {
|
|
||||||
error: "middleware.events must be an array",
|
|
||||||
};
|
|
||||||
if (middleware.events.some((ev) => !validEvents.includes(ev)))
|
|
||||||
return {
|
|
||||||
error: "Invalid event type! Must be one of the following: " + validEvents.join(", "),
|
|
||||||
};
|
|
||||||
if (middleware.explicitCall && typeof middleware.explicitCall !== "boolean") {
|
|
||||||
return {
|
|
||||||
error: "middleware.explicitCall must be typeof boolean",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
error: undefined,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -89,6 +89,7 @@ const marketPlaceSettings = {
|
||||||
const script = eval(pluginScript);
|
const script = eval(pluginScript);
|
||||||
const inject = await script() as unknown as SWPlugin;
|
const inject = await script() as unknown as SWPlugin;
|
||||||
if (plugin.remove) {
|
if (plugin.remove) {
|
||||||
|
//@ts-ignore freaking types BRO
|
||||||
const plug = plugins.filter(({ name }) => name !== plugin.name);
|
const plug = plugins.filter(({ name }) => name !== plugin.name);
|
||||||
swPlugins.push({remove: true, host: inject.host, html: inject.html, injectTo: inject.injectTo});
|
swPlugins.push({remove: true, host: inject.host, html: inject.html, injectTo: inject.injectTo});
|
||||||
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plug));
|
localStorage.setItem(Settings.PluginSettings.plugins, JSON.stringify(plug));
|
||||||
|
|
1
workerware
Submodule
1
workerware
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit c28ebbaacf659b570145f2ae8e1f6404d2abe8e2
|
Loading…
Add table
Add a link
Reference in a new issue