diff --git a/web/src/beta/lib/core/Crust/Infobox/Block/builtin/unsafeBlocks.ts b/web/src/beta/lib/core/Crust/Infobox/Block/builtin/unsafeBlocks.ts index f8c225bedb..8f21cc299b 100644 --- a/web/src/beta/lib/core/Crust/Infobox/Block/builtin/unsafeBlocks.ts +++ b/web/src/beta/lib/core/Crust/Infobox/Block/builtin/unsafeBlocks.ts @@ -14,7 +14,7 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) { const unsafeBlocks: UnsafeBuiltinBlocks | undefined = plugin .map(p => - p.widgets.map(w => { + p.blocks?.map(w => { return { widgetId: `${p.id}/${w.extensionId}`, ...w, @@ -23,11 +23,11 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) { ) .reduce((a, b) => { const newObject: { [key: string]: Component } = {}; - b.forEach(w => { + b?.forEach(w => { newObject[w.widgetId] = w.component; }); return merge(a, newObject); }, {}); - return unsafeBlocks; + return unsafeBlocks || []; } diff --git a/web/src/beta/lib/core/Crust/Widgets/Widget/builtin/unsafeWidgets.ts b/web/src/beta/lib/core/Crust/Widgets/Widget/builtin/unsafeWidgets.ts index 2a84807537..c7dd40fad3 100644 --- a/web/src/beta/lib/core/Crust/Widgets/Widget/builtin/unsafeWidgets.ts +++ b/web/src/beta/lib/core/Crust/Widgets/Widget/builtin/unsafeWidgets.ts @@ -16,7 +16,7 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) { const unsafeWidgets: UnsafeBuiltinWidgets | undefined = plugin .map(p => - p.widgets.map(w => { + p.widgets?.map(w => { return { widgetId: `${p.id}/${w.extensionId}`, ...w, @@ -25,11 +25,11 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) { ) .reduce((a, b) => { const newObject: { [key: string]: Component } = {}; - b.forEach(w => { + b?.forEach(w => { newObject[w.widgetId] = w.component; }); return merge(a, newObject); }, {}); - return unsafeWidgets; + return unsafeWidgets || []; } diff --git a/web/src/beta/lib/unsafeBuiltinPlugins/.gitignore b/web/src/beta/lib/unsafeBuiltinPlugins/.gitignore deleted file mode 100644 index c795b63c43..0000000000 --- a/web/src/beta/lib/unsafeBuiltinPlugins/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -* -!.gitignore -!README.md -!index.ts \ No newline at end of file diff --git a/web/src/beta/lib/unsafeBuiltinPlugins/README.md b/web/src/beta/lib/unsafeBuiltinPlugins/README.md deleted file mode 100644 index cf9550e745..0000000000 --- a/web/src/beta/lib/unsafeBuiltinPlugins/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# *!!!! USE AT OWN RISK !!!!* -#### This folder is for any personal plugins you want to write but be treated as builtin, bypassing the sandboxing that happens to regularly installed plugins. - - -## Basic setup guide -### Server -You must add the URL that is hosting your unsafe builting plugin's YAML file to the server's environment variables `REEARTH_EXT_PLUGIN`. - -For example, you have `REEARTH_EXT_PLUGIN=https://example/com/myPlugin/reearth.yml`, add `REEARTH_EXT_PLUGIN=https://example/com/myPlugin` to your environment variables. - -### Frontend - -unsafeBuiltinPlugins - -Add your plugin code here in their own, plugin separated, directories. Import these in to the unsafeBuiltinPlugins/index.ts file. - -Plugin directory structure -- in the root directory have an index.jsx/tsx file that exports your plugin as default - -``` -type Extension = { - type: T; - extensionId: string; - name: string; - component: -} - -type Plugin = { - id: string; - name: string; - widgets: Extension<"widget">[]; - blocks: Extension<"block">[]; -} -``` \ No newline at end of file diff --git a/web/src/beta/lib/unsafeBuiltinPlugins/index.ts b/web/src/beta/lib/unsafeBuiltinPlugins/index.ts deleted file mode 100644 index f5a9972005..0000000000 --- a/web/src/beta/lib/unsafeBuiltinPlugins/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -// import { default as myExampleUnsafePlugin } from "./myExampleUnsafePlugin/index.jsx"; - -// export default [myExampleUnsafePlugin]; - -export default []; diff --git a/web/src/services/config/index.ts b/web/src/services/config/index.ts index 5eec93acbe..60890e9335 100644 --- a/web/src/services/config/index.ts +++ b/web/src/services/config/index.ts @@ -44,6 +44,7 @@ export type Config = { documentationUrl?: string; marketplaceUrl?: string; extensionUrls?: string[]; + unsafePluginUrls?: string[]; extensions?: Extensions; unsafeBuiltinPlugins?: UnsafeBuiltinPlugin[]; }; @@ -60,7 +61,7 @@ declare global { export default async function loadConfig() { if (window.REEARTH_CONFIG) return; window.REEARTH_CONFIG = defaultConfig; - const config = { + const config: Config = { ...defaultConfig, ...(await (await fetch("/reearth_config.json")).json()), }; @@ -80,7 +81,9 @@ export default async function loadConfig() { config.extensions = extensions; } - config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins(); + if (config.unsafePluginUrls) { + config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins(config.unsafePluginUrls); + } window.REEARTH_CONFIG = config; } diff --git a/web/src/services/config/unsafeBuiltinPlugin.ts b/web/src/services/config/unsafeBuiltinPlugin.ts index 35a6941d10..ddd6caa012 100644 --- a/web/src/services/config/unsafeBuiltinPlugin.ts +++ b/web/src/services/config/unsafeBuiltinPlugin.ts @@ -1,8 +1,10 @@ +import { FC } from "react"; + export type UnsafeBuiltinPlugin = { id: string; name: string; - widgets: UnsafeBuiltinWidget[]; - blocks: UnsafeBuiltinBlock[]; + widgets?: UnsafeBuiltinWidget[]; + blocks?: UnsafeBuiltinBlock[]; }; type UnsafeBuiltinWidget = UnsafeBuiltinPluginExtension<"widget">; @@ -13,19 +15,22 @@ type UnsafeBuiltinPluginExtension = { type: T; extensionId: string; name: string; - component: React.FC; + component: FC; }; export type UnsafeBuiltinWidgets = Record; -export async function loadUnsafeBuiltinPlugins() { - try { - const unsafeBuiltinPlugins = ( - await import(/* @vite-ignore */ "src/beta/lib/unsafeBuiltinPlugins") - ).default as UnsafeBuiltinPlugin[]; - return unsafeBuiltinPlugins; - } catch (e) { - console.error("unsafe builtin plugin load failed", e); - } - return undefined; -} +export const loadUnsafeBuiltinPlugins = async (urls: string[]) => { + return ( + await Promise.all( + urls.map(async url => { + try { + const plugin: UnsafeBuiltinPlugin = (await import(/* @vite-ignore */ url)).default; + return plugin; + } catch (e) { + throw new Error(`Specified unsafe built-in module could not find: ${url} ${e}`); + } + }), + ) + ).filter(Boolean); +};