[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")); export const plugins = wrapSync(createStorage<Indexable<Plugin>>("VENDETTA_PLUGINS"));
const loadedPlugins: Indexable<EvaledPlugin> = {}; const loadedPlugins: Indexable<EvaledPlugin> = {};
export async function fetchPlugin(id: string, enabled = true) { export async function fetchPlugin(id: string) {
if (!id.endsWith("/")) id += "/"; 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; let pluginManifest: PluginManifest;
@ -26,26 +26,31 @@ export async function fetchPlugin(id: string, enabled = true) {
throw new Error(`Failed to fetch manifest for ${id}`); throw new Error(`Failed to fetch manifest for ${id}`);
} }
let pluginJs: string; let pluginJs: string | undefined;
// TODO: Remove duplicate error if possible if (existingPlugin?.manifest.hash !== pluginManifest.hash) {
try { try {
// by polymanifest spec, plugins should always specify their main file, but just in case // 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(); pluginJs = await (await safeFetch(id + (pluginManifest.main || "index.js"), { cache: "no-store" })).text();
} catch { } catch {
throw new Error(`Failed to fetch JS for ${id}`); 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] = { plugins[id] = {
id: id, id: id,
manifest: pluginManifest, manifest: pluginManifest,
enabled: false, enabled: existingPlugin?.enabled ?? false,
update: true, update: existingPlugin?.update ?? true,
js: pluginJs, 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); if (enabled) await startPlugin(id);
} }
@ -114,8 +119,8 @@ export async function initPlugins() {
await awaitSyncWrapper(plugins); await awaitSyncWrapper(plugins);
const allIds = Object.keys(plugins); const allIds = Object.keys(plugins);
await Promise.allSettled(allIds.map((pl) => fetchPlugin(pl, false))); 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); for (const pl of allIds.filter(pl => plugins[pl].enabled)) startPlugin(pl);
return stopAllPlugins; return stopAllPlugins;
} }

View file

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