mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Update runtime apis to support multiple libraries
This commit is contained in:
@@ -14,18 +14,22 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs');
|
||||
var fspath = require('path');
|
||||
|
||||
var runtime;
|
||||
var knownTypes = {};
|
||||
|
||||
var storage;
|
||||
var libraries = {};
|
||||
|
||||
|
||||
function init(runtime) {
|
||||
knownTypes = {
|
||||
'flows': 'node-red'
|
||||
};
|
||||
|
||||
libraries["_examples_"] = require("./examples");
|
||||
libraries["_examples_"].init(runtime);
|
||||
libraries["local"] = require("./local");
|
||||
libraries["local"].init(runtime);
|
||||
|
||||
function init(_runtime) {
|
||||
runtime = _runtime;
|
||||
storage = runtime.storage;
|
||||
knownTypes = {};
|
||||
}
|
||||
|
||||
function registerType(id,type) {
|
||||
@@ -37,66 +41,34 @@ function registerType(id,type) {
|
||||
knownTypes[type] = id;
|
||||
}
|
||||
|
||||
// function getAllEntries(type) {
|
||||
// if (!knownTypes.hasOwnProperty(type)) {
|
||||
// throw new Error(`Unknown library type '${type}'`);
|
||||
// }
|
||||
// }
|
||||
|
||||
function getEntry(type,path) {
|
||||
if (type !== 'flows') {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
}
|
||||
return storage.getLibraryEntry(type,path);
|
||||
function getEntry(library,type,path) {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
}
|
||||
if (libraries.hasOwnProperty(library)) {
|
||||
return libraries[library].getEntry(type,path);
|
||||
} else {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (path.indexOf("_examples_/") === 0) {
|
||||
var m = /^_examples_\/(@.*?\/[^\/]+|[^\/]+)\/(.*)$/.exec(path);
|
||||
if (m) {
|
||||
var module = m[1];
|
||||
var entryPath = m[2];
|
||||
var fullPath = runtime.nodes.getNodeExampleFlowPath(module,entryPath);
|
||||
if (fullPath) {
|
||||
try {
|
||||
fs.readFile(fullPath,'utf8',function(err, data) {
|
||||
runtime.log.audit({event: "library.get",type:"flow",path:path});
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(data);
|
||||
})
|
||||
} catch(err) {
|
||||
return reject(err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// IF we get here, we didn't find the file
|
||||
var error = new Error("not_found");
|
||||
error.code = "not_found";
|
||||
return reject(error);
|
||||
} else {
|
||||
resolve(storage.getFlow(path));
|
||||
}
|
||||
});
|
||||
throw new Error(`Unknown library '${library}'`);
|
||||
}
|
||||
}
|
||||
function saveEntry(type,path,meta,body) {
|
||||
if (type !== 'flows') {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
function saveEntry(library,type,path,meta,body) {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
}
|
||||
if (libraries.hasOwnProperty(library)) {
|
||||
if (libraries[library].hasOwnProperty("saveEntry")) {
|
||||
return libraries[library].saveEntry(type,path,meta,body);
|
||||
} else {
|
||||
throw new Error(`Library '${library}' is read-only`);
|
||||
}
|
||||
return storage.saveLibraryEntry(type,path,meta,body);
|
||||
} else {
|
||||
return storage.saveFlow(path,body);
|
||||
throw new Error(`Unknown library '${library}'`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
register: registerType,
|
||||
// getAllEntries: getAllEntries,
|
||||
getEntry: getEntry,
|
||||
saveEntry: saveEntry
|
||||
|
||||
|
Reference in New Issue
Block a user