Merge pull request #2785 from node-red/library-plugins

Library plugins
This commit is contained in:
Nick O'Leary
2021-02-25 16:05:23 +00:00
committed by GitHub
22 changed files with 833 additions and 180 deletions

View File

@@ -25,6 +25,27 @@ var api = module.exports = {
runtime = _runtime;
},
// /* *
// * Gets the configuration of a library source.
// * @param {Object} opts
// * @param {User} opts.user - the user calling the api
// * @param {String} opts.library - the library
// * @param {Object} opts.req - the request to log (optional)
// * @return {Promise<String|Object>} - resolves when complete
// * @memberof @node-red/runtime_library
// */
// getConfig: async function(opts) {
// runtime.log.audit({event: "library.getConfig",library:opts.library}, opts.req);
// try {
// return runtime.library.getConfig(opts.library)
// } catch(err) {
// var error = new Error();
// error.code = "not_found";
// error.status = 404;
// throw error;
// }
// },
/**
* Gets an entry from the library.
* @param {Object} opts

View File

@@ -81,7 +81,7 @@ var api = module.exports = {
if (!runtime.settings.disableEditor) {
safeSettings.context = runtime.nodes.listContextStores();
safeSettings.libraries = runtime.library.getLibraries();
if (util.isArray(runtime.settings.paletteCategories)) {
safeSettings.paletteCategories = runtime.settings.paletteCategories;
}
@@ -130,6 +130,7 @@ var api = module.exports = {
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
runtime.settings.exportNodeSettings(safeSettings);
runtime.plugins.exportPluginSettings(safeSettings);
}
return safeSettings;

View File

@@ -74,7 +74,6 @@ function init(userSettings,httpServer,_adminApi) {
adminApi = _adminApi;
}
redNodes.init(runtime);
library.init(runtime);
externalAPI.init(runtime);
}
@@ -104,6 +103,7 @@ function start() {
return i18n.registerMessageCatalog("runtime",path.resolve(path.join(__dirname,"..","locales")),"runtime.json")
.then(function() { return storage.init(runtime)})
.then(function() { return settings.load(storage)})
.then(function() { return library.init(runtime)})
.then(function() {
if (log.metric()) {

View File

@@ -95,7 +95,11 @@ function getEntry(type,path) {
}
module.exports = {
name: '_examples_',
id: "examples",
label: "editor:library.types.examples",
icon: "font-awesome/fa-life-ring",
types: ["flows"],
readOnly: true,
init: init,
getEntry: getEntry
}

View File

@@ -15,23 +15,81 @@
**/
var knownTypes = {};
const {events,log} = require("@node-red/util")
const knownTypes = {};
const libraries = {};
const libraryConfigs = {};
const libraryPlugins = {};
var libraries = {};
// Libraries defined in the settings file. Their configurations
// cannot be modified in the editor.
let runtimeLibraries = [];
// Libraries defined by the user in the editor.
let userLibraries = [];
function init(runtime) {
knownTypes = {
'flows': 'node-red'
};
let runtime;
function init(_runtime) {
runtime = _runtime;
events.removeListener("registry:plugin-added",onPluginAdded);
events.on("registry:plugin-added",onPluginAdded);
knownTypes.flows = 'node-red';
libraries["_examples_"] = require("./examples");
libraries["_examples_"].init(runtime);
libraries["local"] = require("./local");
libraries["local"].init(runtime);
libraryConfigs["local"] = libraries["local"]
libraries["examples"] = require("./examples");
libraries["examples"].init(runtime);
libraryConfigs["examples"] = libraries["examples"]
try {
runtimeLibraries = runtime.settings.editorTheme.library.sources;
} catch(err) {
runtimeLibraries = [];
}
// userLibraries = runtime.settings.get("library")
}
function onPluginAdded(id) {
const plugin = runtime.plugins.getPlugin(id);
if (plugin.type === "node-red-library-source") {
libraryPlugins[plugin.id] = plugin;
runtimeLibraries.forEach(library => {
if (library.type === id) {
library.local = false;
if (!/^[a-z0-9-_]+$/.test(library.id)) {
log.warn(log._("library.failedToInit",{error:log._("library.invalidProperty",{prop:"id",value:library.id})}));
return;
}
try {
libraries[library.id] = new plugin.class(library)
libraryConfigs[library.id] = library;
libraryConfigs[library.id].type = id;
if (libraries[library.id].init) {
libraries[library.id].init().catch(err => {
delete libraries[library.id];
delete libraryConfigs[library.id];
log.warn(log._("library.failedToInit",{library:library.id, error:err.toString()}));
});
}
} catch(err) {
log.warn(log._("library.failedToInit",{library:library.id, error:err.toString()}));
}
}
})
}
}
function registerType(id,type) {
// TODO: would like to enforce this, but currently the tests register the same type multiple
// times and have no way to remove themselves.
@@ -43,31 +101,82 @@ function registerType(id,type) {
function getEntry(library,type,path) {
if (!knownTypes.hasOwnProperty(type)) {
throw new Error(`Unknown library type '${type}'`);
throw new Error(log._("library.unknownType",{type: type}))
}
if (libraries.hasOwnProperty(library)) {
return libraries[library].getEntry(type,path);
} else {
throw new Error(`Unknown library '${library}'`);
throw new Error(log._("library.unknownLibrary",{library: library}))
}
}
function saveEntry(library,type,path,meta,body) {
if (!knownTypes.hasOwnProperty(type)) {
throw new Error(`Unknown library type '${type}'`);
throw new Error(log._("library.unknownType",{type: type}))
}
if (libraries.hasOwnProperty(library)) {
if (libraries[library].hasOwnProperty("saveEntry")) {
if (libraries[library].saveEntry) {
return libraries[library].saveEntry(type,path,meta,body);
} else {
throw new Error(`Library '${library}' is read-only`);
throw new Error(log._("library.readOnly",{library: library}))
}
} else {
throw new Error(`Unknown library '${library}'`);
throw new Error(log._("library.unknownLibrary",{library: library}))
}
}
function getLibraries() {
const libraryList = []
for (let id in libraries) {
if (libraries.hasOwnProperty(id)) {
var config = getConfig(id);
// Don't include the full configuration of each library when providing
// the list of all libraries
delete config.config;
libraryList.push(config);
}
}
return libraryList;
}
function getConfig(id) {
var lib = {
id: id,
label: libraryConfigs[id].label || id,
user: false,
icon: libraryConfigs[id].icon
}
if (libraryConfigs[id].types) {
lib.types = libraryConfigs[id].types
}
if (libraryConfigs[id].readOnly) {
lib.readOnly = libraryConfigs[id].readOnly
}
if (libraryConfigs[id].type) {
lib.type = libraryConfigs[id].type;
var def = libraryPlugins[lib.type];
if (def && def.defaults) {
lib.config = {};
for (var d in def.defaults) {
if (def.defaults.hasOwnProperty(d)) {
if (def.defaults[d].type !== 'password') {
lib.config[d] = libraryConfigs[id][d];
} else if (!!libraryConfigs[id][d]) {
lib.config["has_"+d] = true;
}
}
}
}
}
return lib;
}
module.exports = {
init: init,
getLibraries: getLibraries,
// getConfig: getConfig,
register: registerType,
getEntry: getEntry,
saveEntry: saveEntry

View File

@@ -30,7 +30,9 @@ function saveEntry(type,path,meta,body) {
}
module.exports = {
name: 'local',
id: "local",
label: "editor:library.types.local",
icon: "font-awesome/fa-hdd-o",
init: init,
getEntry: getEntry,
saveEntry: saveEntry

View File

@@ -7,4 +7,5 @@ module.exports = {
getPluginsByType: registry.getPluginsByType,
getPluginList: registry.getPluginList,
getPluginConfigs: registry.getPluginConfigs,
exportPluginSettings: registry.exportPluginSettings
}

View File

@@ -89,7 +89,13 @@
"not-available": "Settings not available",
"property-read-only": "Property '__prop__' is read-only"
},
"library": {
"unknownLibrary": "Unknown library: __library__",
"unknownType": "Unknown library type: __type__",
"readOnly": "Library __library__ is read-only",
"failedToInit": "Failed to initialise library __library__: __error__",
"invalidProperty": "Invalid property __prop__: '__value__'"
},
"nodes": {
"credentials": {
"error":"Error loading credentials: __message__",