mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
update external context
- Implement `delete` function - Swap default easily - Change memory context as a plugin - Update localfilesystem plugin - Change file/folder structure
This commit is contained in:
committed by
HirokiUchikawa
parent
b4b70a988e
commit
e33ec0cf50
@@ -14,57 +14,110 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var clone = require("clone");
|
||||
var util = require("../../util");
|
||||
var log = require("../../log");
|
||||
var externalContext = require("./external");
|
||||
|
||||
var settings;
|
||||
var contexts = {};
|
||||
var globalContext = null;
|
||||
var externalContexts = {};
|
||||
|
||||
var re = /^(\$.*?)\.(.+)|^(\$.*)/;
|
||||
function init(_settings) {
|
||||
settings = _settings;
|
||||
externalContexts = {};
|
||||
|
||||
function createContext(id,seed) {
|
||||
var flowId = id;
|
||||
var data = seed || {};
|
||||
var obj = seed || {};
|
||||
// init meomory plugin
|
||||
externalContexts["_"] = require("./memory");
|
||||
externalContexts["_"].init();
|
||||
globalContext = createContext("global",settings.functionGlobalContext || {});
|
||||
}
|
||||
|
||||
function get(key) {
|
||||
return util.getMessageProperty(data,key);
|
||||
};
|
||||
function set(key, value) {
|
||||
util.setMessageProperty(data,key,value);
|
||||
};
|
||||
function keys() {
|
||||
var keysData = Object.keys(data);
|
||||
if (seed == null) {
|
||||
return keysData;
|
||||
} else {
|
||||
return keysData.filter(function (key) {
|
||||
return key !== "set" && key !== "get" && key !== "keys";
|
||||
});
|
||||
function load() {
|
||||
// load & init plugins in settings.contextStorage
|
||||
var plugins = settings.contextStorage;
|
||||
var alias = null;
|
||||
if (plugins) {
|
||||
for(var pluginName in plugins){
|
||||
if(pluginName === "_"){
|
||||
continue;
|
||||
}
|
||||
if(pluginName === "default" && typeof plugins[pluginName] === "string"){
|
||||
alias = plugins[pluginName];
|
||||
continue;
|
||||
}
|
||||
var plugin;
|
||||
if(plugins[pluginName].hasOwnProperty("module")){
|
||||
var config = plugins[pluginName].config || {};
|
||||
copySettings(config, settings);
|
||||
try{
|
||||
plugin = require("./"+plugins[pluginName].module);
|
||||
}catch(err){
|
||||
throw new Error(plugins[pluginName].module + " could not be loaded");
|
||||
}
|
||||
plugin.init(config);
|
||||
externalContexts[pluginName] = plugin;
|
||||
}else{
|
||||
throw new Error("module is is not defined in settings.contextStorage." + plugins[pluginName] );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
obj.get = function(key) {
|
||||
if(externalContext.canUse(key)) {
|
||||
return externalContext.get(key, flowId);
|
||||
}else{
|
||||
return get(key);
|
||||
}
|
||||
};
|
||||
obj.set = function(key, value) {
|
||||
if(externalContext.canUse(key)) {
|
||||
externalContext.set(key, value, flowId);
|
||||
}else{
|
||||
set(key, value);
|
||||
if(alias){
|
||||
if(externalContexts.hasOwnProperty(alias)){
|
||||
externalContexts["default"] = externalContexts[alias];
|
||||
}else{
|
||||
throw new Error("default is invalid" + plugins["default"])
|
||||
}
|
||||
}
|
||||
}
|
||||
obj.keys = function(key) {
|
||||
if(externalContext.canUse(key)) {
|
||||
return externalContext.keys(key, flowId);
|
||||
}else{
|
||||
return keys();
|
||||
}
|
||||
|
||||
function copySettings(config, settings){
|
||||
var copy = ["userDir"]
|
||||
config.settings = {};
|
||||
copy.forEach(function(setting){
|
||||
config.settings[setting] = clone(settings[setting]);
|
||||
});
|
||||
}
|
||||
|
||||
function createContext(id,seed) {
|
||||
var scope = id;
|
||||
var obj = seed || {};
|
||||
|
||||
obj.get = function(key) {
|
||||
var result = parseKey(key);
|
||||
if(!result){
|
||||
return externalContexts["_"].get(key, scope);
|
||||
}
|
||||
if(externalContexts.hasOwnProperty(result[0])){
|
||||
return externalContexts[result[0]].get(result[1], scope);
|
||||
}else if(externalContexts.hasOwnProperty("defalut")){
|
||||
return externalContexts["defalut"].get(result[1], scope);
|
||||
}else{
|
||||
throw new Error(result[0] + " is not defined in setting.js");
|
||||
}
|
||||
};
|
||||
obj.set = function(key, value) {
|
||||
var result = parseKey(key);
|
||||
if(!result){
|
||||
return externalContexts["_"].set(key, value, scope);
|
||||
}
|
||||
if(externalContexts.hasOwnProperty(result[0])){
|
||||
externalContexts[result[0]].set(result[1], value, scope);
|
||||
}else if(externalContexts.hasOwnProperty("defalut")){
|
||||
externalContexts["defalut"].set(result[1], value, scope);
|
||||
}else{
|
||||
throw new Error(result[0] + " is not defined in setting.js");
|
||||
}
|
||||
};
|
||||
obj.keys = function() {
|
||||
//TODO: discuss about keys() behavior
|
||||
var keys = [];
|
||||
for(var plugin in externalContexts){
|
||||
keys.concat(externalContexts[plugin].keys(scope));
|
||||
}
|
||||
return keys;
|
||||
};
|
||||
if(id === "global"){
|
||||
externalContexts["_"].setGlobalContext(seed);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
@@ -72,7 +125,7 @@ function createContext(id,seed) {
|
||||
function getContext(localId,flowId) {
|
||||
var contextId = localId;
|
||||
if (flowId) {
|
||||
contextId = localId+"_"+flowId;
|
||||
contextId = localId+":"+flowId;
|
||||
}
|
||||
if (contexts.hasOwnProperty(contextId)) {
|
||||
return contexts[contextId];
|
||||
@@ -91,7 +144,10 @@ function getContext(localId,flowId) {
|
||||
function deleteContext(id,flowId) {
|
||||
var contextId = id;
|
||||
if (flowId) {
|
||||
contextId = id+"_"+flowId;
|
||||
contextId = id+":"+flowId;
|
||||
}
|
||||
for(var plugin in externalContexts){
|
||||
externalContexts[plugin].delete(contextId);
|
||||
}
|
||||
delete contexts[contextId];
|
||||
}
|
||||
@@ -102,19 +158,36 @@ function clean(flowConfig) {
|
||||
var node;
|
||||
for (var id in contexts) {
|
||||
if (contexts.hasOwnProperty(id)) {
|
||||
var idParts = id.split("_");
|
||||
var idParts = id.split(":");
|
||||
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
|
||||
for(var plugin in externalContexts){
|
||||
externalContexts[plugin].delete(id);
|
||||
}
|
||||
delete contexts[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseKey(key){
|
||||
var keys = null;
|
||||
if(!key){
|
||||
return null;
|
||||
}
|
||||
var index_$ = key.indexOf("$");
|
||||
var index_dot = key.indexOf(".", 1);
|
||||
if(index_$ === 0 && index_dot){
|
||||
keys = [];
|
||||
keys[0] = key.substring(1,index_dot);
|
||||
keys[1] = key.substring(index_dot + 1);
|
||||
keys[0] = keys[0] || "default";
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(settings) {
|
||||
globalContext = createContext("global",settings.functionGlobalContext || {});
|
||||
externalContext.init(settings);
|
||||
},
|
||||
init: init,
|
||||
load: load,
|
||||
get: getContext,
|
||||
delete: deleteContext,
|
||||
clean:clean
|
||||
|
Reference in New Issue
Block a user