server: fix plugin default export when device is a class or function. fix pluginId state missing.

This commit is contained in:
Koushik Dutta
2022-04-18 13:56:04 -07:00
parent 738e65cedb
commit 167a3b9681
5 changed files with 15 additions and 11 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/server",
"version": "0.0.168",
"version": "0.0.169",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/server",
"version": "0.0.168",
"version": "0.0.169",
"license": "ISC",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.8",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/server",
"version": "0.0.168",
"version": "0.0.169",
"description": "",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.8",

View File

@@ -561,16 +561,17 @@ export function attachPluginRemote(peer: RpcPeer, options?: PluginRemoteAttachOp
try {
peer.evalLocal(script, zipOptions?.filename || '/plugin/main.nodejs.js', params);
pluginConsole?.log('plugin successfully loaded');
await options?.onPluginReady?.(ret, params, exports.default);
const defaultExport = exports.default;
let pluginInstance = exports.default;
// support exporting a plugin class, plugin main function,
// or a plugin instance
if (defaultExport.toString().startsWith('class '))
return new defaultExport();
if (typeof defaultExport === 'function')
return await defaultExport();
return defaultExport;
if (pluginInstance.toString().startsWith('class '))
pluginInstance = new pluginInstance();
if (typeof pluginInstance === 'function')
pluginInstance = await pluginInstance();
await options?.onPluginReady?.(ret, params, pluginInstance);
return pluginInstance;
}
catch (e) {
pluginConsole?.error('plugin failed to start', e);

View File

@@ -125,6 +125,8 @@ export class SystemManagerImpl implements SystemManager {
getDeviceByName(name: string): any {
for (const id of Object.keys(this.state)) {
const s = this.state[id];
if ((s.interfaces?.value as string[])?.includes(ScryptedInterface.ScryptedPlugin) && s.pluginId?.value === name)
return this.getDeviceById(id);
if (s.name.value === name)
return this.getDeviceById(id);
}

View File

@@ -765,7 +765,8 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
setState(pluginDevice, ScryptedInterfaceProperty.providedInterfaces, PluginDeviceProxyHandler.sortInterfaces(interfaces));
}
if (!pluginDevice.pluginId) {
const pluginId: string = getState(pluginDevice, ScryptedInterfaceProperty.pluginId);
if (!pluginId) {
dirty = true;
setState(pluginDevice, ScryptedInterfaceProperty.pluginId, pluginDevice.pluginId);
}