hook up the backend api

This commit is contained in:
AAGaming
2024-02-21 17:39:13 -05:00
parent 4696583680
commit a449181802
3 changed files with 9 additions and 34 deletions
+2
View File
@@ -43,6 +43,8 @@ export default defineConfig({
chunkFileNames: (chunkInfo) => { chunkFileNames: (chunkInfo) => {
return 'chunk-[hash].js'; return 'chunk-[hash].js';
}, },
sourcemap: true,
sourcemapPathTransform: (relativeSourcePath) => relativeSourcePath.replace(/^\.\.\//, `decky://decky/loader/`),
}, },
onwarn: function (message, handleWarning) { onwarn: function (message, handleWarning) {
if (hiddenWarnings.some((warning) => message.code === warning)) return; if (hiddenWarnings.some((warning) => message.code === warning)) return;
+7 -33
View File
@@ -66,11 +66,9 @@ class PluginLoader extends Logger {
private reloadLock: boolean = false; private reloadLock: boolean = false;
// stores a list of plugin names which requested to be reloaded // stores a list of plugin names which requested to be reloaded
private pluginReloadQueue: { name: string; version?: string }[] = []; private pluginReloadQueue: { name: string; version?: string }[] = [];
private apiKeys: Map<string, string> = new Map();
constructor() { constructor() {
super(PluginLoader.name); super(PluginLoader.name);
console.log(import.meta.url);
DeckyBackend.addEventListener('loader/notify_updates', this.notifyUpdates.bind(this)); DeckyBackend.addEventListener('loader/notify_updates', this.notifyUpdates.bind(this));
DeckyBackend.addEventListener('loader/import_plugin', this.importPlugin.bind(this)); DeckyBackend.addEventListener('loader/import_plugin', this.importPlugin.bind(this));
@@ -123,6 +121,8 @@ class PluginLoader extends Logger {
initFilepickerPatches(); initFilepickerPatches();
this.initPluginBackendAPI();
Promise.all([this.getUserInfo(), this.updateVersion()]) Promise.all([this.getUserInfo(), this.updateVersion()])
.then(() => this.loadPlugins()) .then(() => this.loadPlugins())
.then(() => this.checkPluginUpdates()) .then(() => this.checkPluginUpdates())
@@ -334,14 +334,8 @@ class PluginLoader extends Logger {
try { try {
switch (loadType) { switch (loadType) {
case PluginLoadType.ESMODULE_V1: case PluginLoadType.ESMODULE_V1:
const uuid = this.initPluginBackendAPIConnection(name); const plugin_exports = await import(`http://127.0.0.1:1337/plugins/${name}/dist/index.js`);
let plugin_export: () => Plugin; let plugin = plugin_exports.default();
try {
plugin_export = await import(`http://127.0.0.1:1337/plugins/${name}/dist/index.js#apiKey=${uuid}`);
} finally {
this.destroyPluginBackendAPIConnection(uuid);
}
let plugin = plugin_export();
this.plugins.push({ this.plugins.push({
...plugin, ...plugin,
@@ -508,25 +502,12 @@ class PluginLoader extends Logger {
} }
} }
destroyPluginBackendAPIConnection(uuid: string) {
if (this.apiKeys.delete(uuid)) {
this.debug(`backend api connection init data destroyed for ${uuid}`);
}
}
initPluginBackendAPI() { initPluginBackendAPI() {
// Things will break *very* badly if plugin code touches this outside of @decky/backend, so lets make that clear. // Things will break *very* badly if plugin code touches this outside of @decky/backend, so lets make that clear.
window.__DECKY_SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_deckyPluginBackendAPIInit = { window.__DECKY_SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_deckyPluginBackendAPIInit = {
connect: (version: number, key: string) => { connect: (version: number, pluginName: string) => {
if (!this.apiKeys.has(key)) {
throw new Error(`Backend API key ${key} is invalid.`);
}
const pluginName = this.apiKeys.get(key)!;
if (version <= 0) { if (version <= 0) {
this.destroyPluginBackendAPIConnection(key); throw new Error(`Plugin ${pluginName} requested invalid backend api version ${version}.`);
throw new Error(`UUID ${key} requested invalid backend api version ${version}.`);
} }
const backendAPI = { const backendAPI = {
@@ -538,19 +519,12 @@ class PluginLoader extends Logger {
}, },
}; };
this.destroyPluginBackendAPIConnection(key); this.debug(`${pluginName} connected to backend API.`);
return backendAPI; return backendAPI;
}, },
}; };
} }
initPluginBackendAPIConnection(pluginName: string) {
const key = crypto.randomUUID();
this.apiKeys.set(key, pluginName);
return key;
}
createLegacyPluginAPI(pluginName: string) { createLegacyPluginAPI(pluginName: string) {
const pluginAPI = { const pluginAPI = {
routerHook: this.routerHook, routerHook: this.routerHook,
-1
View File
@@ -44,7 +44,6 @@ declare global {
window?.DeckyPluginLoader?.deinit(); window?.DeckyPluginLoader?.deinit();
window.DeckyPluginLoader = new PluginLoader(); window.DeckyPluginLoader = new PluginLoader();
DeckyPluginLoader.init(); DeckyPluginLoader.init();
console.log(import.meta.url);
})(); })();
export default i18n; export default i18n;