Revenge/src/lib/plugins.ts

135 lines
4.3 KiB
TypeScript
Raw Normal View History

import { Indexable, PluginManifest, Plugin } from "@types";
2023-01-30 13:59:47 +00:00
import { awaitSyncWrapper, createStorage, wrapSync } from "@lib/storage";
2023-02-04 16:54:03 +00:00
import safeFetch from "@utils/safeFetch";
2023-01-07 23:05:14 +00:00
import logger from "@lib/logger";
2023-01-03 08:05:16 +00:00
// TODO: Properly implement hash-based updating
2023-01-03 08:05:16 +00:00
type EvaledPlugin = {
onLoad?(): void;
onUnload(): void;
settings: JSX.Element;
2023-01-03 08:05:16 +00:00
};
2023-01-30 13:59:47 +00:00
export const plugins = wrapSync(createStorage<Indexable<Plugin>>("VENDETTA_PLUGINS"));
2023-01-07 23:05:14 +00:00
const loadedPlugins: Indexable<EvaledPlugin> = {};
export async function fetchPlugin(id: string) {
2023-01-07 23:05:14 +00:00
if (!id.endsWith("/")) id += "/";
const existingPlugin = plugins[id];
let pluginManifest: PluginManifest;
try {
2023-02-04 16:54:03 +00:00
pluginManifest = await (await safeFetch(id + "manifest.json", { cache: "no-store" })).json();
} catch {
2023-01-07 23:05:14 +00:00
throw new Error(`Failed to fetch manifest for ${id}`);
}
let pluginJs: string | undefined;
2023-01-03 08:05:16 +00:00
if (existingPlugin?.manifest.hash !== pluginManifest.hash) {
try {
// by polymanifest spec, plugins should always specify their main file, but just in case
pluginJs = await (await safeFetch(id + (pluginManifest.main || "index.js"), { cache: "no-store" })).text();
} catch {
throw new Error(`Failed to fetch JS for ${id}`);
}
2023-01-03 08:05:16 +00:00
if (pluginJs.length === 0) throw new Error(`Failed to fetch JS for ${id}`);
}
2023-01-04 08:01:56 +00:00
2023-01-03 08:05:16 +00:00
plugins[id] = {
id: id,
manifest: pluginManifest,
enabled: existingPlugin?.enabled ?? false,
update: existingPlugin?.update ?? true,
js: pluginJs ?? existingPlugin.js,
};
}
2023-01-30 00:40:56 +00:00
export async function installPlugin(id: string, enabled = true) {
if (!id.endsWith("/")) id += "/";
if (typeof id !== "string" || id in plugins) throw new Error("Plugin already installed");
await fetchPlugin(id);
2023-01-30 00:40:56 +00:00
if (enabled) await startPlugin(id);
2023-01-03 08:05:16 +00:00
}
export async function evalPlugin(plugin: Plugin) {
const vendettaForPlugins = {
...window.vendetta,
plugin: {
manifest: plugin.manifest,
// Wrapping this with wrapSync is NOT an option.
storage: await createStorage<Indexable<any>>(plugin.id),
}
};
2023-01-04 08:01:56 +00:00
const pluginString = `vendetta=>{return ${plugin.js}}\n//# sourceURL=${plugin.id}`;
2023-01-03 08:05:16 +00:00
2023-01-04 08:01:56 +00:00
const raw = (0, eval)(pluginString)(vendettaForPlugins);
const ret = typeof raw == "function" ? raw() : raw;
return ret.default || ret;
2023-01-03 08:05:16 +00:00
}
export async function startPlugin(id: string) {
if (!id.endsWith("/")) id += "/";
2023-01-03 08:05:16 +00:00
const plugin = plugins[id];
if (!plugin) throw new Error("Attempted to start non-existent plugin");
try {
const pluginRet: EvaledPlugin = await evalPlugin(plugin);
2023-01-03 08:05:16 +00:00
loadedPlugins[id] = pluginRet;
pluginRet.onLoad?.();
plugin.enabled = true;
} catch(e) {
logger.error(`Plugin ${plugin.id} errored whilst loading, and will be unloaded`, e);
try {
loadedPlugins[plugin.id]?.onUnload?.();
} catch(e2) {
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e2);
}
delete loadedPlugins[id];
plugin.enabled = false;
}
}
export function stopPlugin(id: string, disable = true) {
if (!id.endsWith("/")) id += "/";
2023-01-03 08:05:16 +00:00
const plugin = plugins[id];
const pluginRet = loadedPlugins[id];
if (!plugin) throw new Error("Attempted to stop non-existent plugin");
if (!pluginRet) throw new Error("Attempted to stop a non-started plugin");
try {
loadedPlugins[plugin.id]?.onUnload?.();
} catch(e) {
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e);
}
delete loadedPlugins[id];
disable && (plugin.enabled = false);
2023-01-03 08:05:16 +00:00
}
2023-01-07 23:05:14 +00:00
export function removePlugin(id: string) {
if (!id.endsWith("/")) id += "/";
const plugin = plugins[id];
if (plugin.enabled) stopPlugin(id);
2023-01-07 23:05:14 +00:00
delete plugins[id];
}
export async function initPlugins() {
2023-01-30 13:59:47 +00:00
await awaitSyncWrapper(plugins);
2023-01-30 13:59:47 +00:00
const allIds = Object.keys(plugins);
await Promise.allSettled(allIds.filter(pl => plugins[pl].enabled && plugins[pl].update).map(pl => fetchPlugin(pl)));
for (const pl of allIds.filter(pl => plugins[pl].enabled)) startPlugin(pl);
return stopAllPlugins;
2023-01-30 13:59:47 +00:00
}
2023-02-05 00:10:34 +00:00
const stopAllPlugins = () => Object.keys(loadedPlugins).forEach(p => stopPlugin(p, false));
export const getSettings = (id: string) => loadedPlugins[id]?.settings;