[Plugins] Fix updating, and make it hash-based

This commit is contained in:
Beef 2023-02-05 23:30:51 +00:00
parent b0df91db7a
commit 6ed08a0ac9
2 changed files with 24 additions and 19 deletions

View file

@ -14,9 +14,9 @@ type EvaledPlugin = {
export const plugins = wrapSync(createStorage<Indexable<Plugin>>("VENDETTA_PLUGINS"));
const loadedPlugins: Indexable<EvaledPlugin> = {};
export async function fetchPlugin(id: string, enabled = true) {
export async function fetchPlugin(id: string) {
if (!id.endsWith("/")) id += "/";
if (typeof id !== "string" || id in plugins) throw new Error("Plugin ID invalid or taken");
const existingPlugin = plugins[id];
let pluginManifest: PluginManifest;
@ -26,26 +26,31 @@ export async function fetchPlugin(id: string, enabled = true) {
throw new Error(`Failed to fetch manifest for ${id}`);
}
let pluginJs: string;
let pluginJs: string | undefined;
// TODO: Remove duplicate error if possible
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}`);
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}`);
}
if (pluginJs.length === 0) throw new Error(`Failed to fetch JS for ${id}`);
}
if (pluginJs.length === 0) throw new Error(`Failed to fetch JS for ${id}`);
plugins[id] = {
id: id,
manifest: pluginManifest,
enabled: false,
update: true,
js: pluginJs,
enabled: existingPlugin?.enabled ?? false,
update: existingPlugin?.update ?? true,
js: pluginJs ?? existingPlugin.js,
};
}
export async function installPlugin(id: string, enabled = true) {
if (typeof id !== "string" || id in plugins) throw new Error("Plugin already installed");
await fetchPlugin(id);
if (enabled) await startPlugin(id);
}
@ -114,8 +119,8 @@ export async function initPlugins() {
await awaitSyncWrapper(plugins);
const allIds = Object.keys(plugins);
await Promise.allSettled(allIds.map((pl) => fetchPlugin(pl, false)));
for (const pl of allIds.filter((pl) => plugins[pl].enabled)) startPlugin(pl);
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;
}

View file

@ -3,7 +3,7 @@ import { Forms } from "@ui/components";
import { showToast } from "@ui/toasts";
import { getAssetIDByName } from "@ui/assets";
import { useProxy } from "@lib/storage";
import { plugins, fetchPlugin, startPlugin } from "@lib/plugins";
import { plugins, installPlugin } from "@lib/plugins";
import PluginCard from "@ui/settings/components/PluginCard";
const { FormInput, FormRow } = Forms;
@ -24,7 +24,7 @@ export default function Plugins() {
label="Install plugin"
leading={<FormRow.Icon source={getAssetIDByName("ic_add_18px")} />}
onPress={() => {
fetchPlugin(pluginUrl).then(() => {
installPlugin(pluginUrl).then(() => {
setPluginUrl("");
}).catch((e: Error) => {
showToast(e.message, getAssetIDByName("Small"));