node-red/red/runtime/nodes/context/index.js

233 lines
7.1 KiB
JavaScript
Raw Normal View History

2015-12-29 23:16:51 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2015-12-29 23:16:51 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var clone = require("clone");
var log = require("../../log");
var memory = require("./memory");
var settings;
var contexts = {};
var globalContext = null;
var externalContexts = {};
2018-06-26 04:43:37 +02:00
var noContextStorage = true;
function init(_settings) {
settings = _settings;
externalContexts = {};
2015-12-29 23:16:51 +01:00
2018-03-28 08:54:16 +02:00
// init memory plugin
2018-05-30 03:24:27 +02:00
var seed = settings.functionGlobalContext || {};
externalContexts["_"] = memory();
2018-05-30 03:24:27 +02:00
externalContexts["_"].setGlobalContext(seed);
globalContext = createContext("global",seed);
}
function load() {
// load & init plugins in settings.contextStorage
var plugins = settings.contextStorage;
var isAlias = false;
if (plugins) {
2018-05-30 03:24:27 +02:00
var promises = [];
2018-03-28 08:54:16 +02:00
noContextStorage = false;
for(var pluginName in plugins){
if(pluginName === "_"){
continue;
}
if(pluginName === "default" && typeof plugins[pluginName] === "string"){
isAlias = true;
continue;
}
var plugin;
if(plugins[pluginName].hasOwnProperty("module")){
var config = plugins[pluginName].config || {};
copySettings(config, settings);
2018-03-28 08:54:16 +02:00
if(typeof plugins[pluginName].module === "string") {
try{
plugin = require("./"+plugins[pluginName].module);
}catch(err){
2018-06-20 12:50:55 +02:00
return Promise.reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module})));
2018-03-28 08:54:16 +02:00
}
} else {
plugin = plugins[pluginName].module;
}
externalContexts[pluginName] = plugin(config);
}else{
2018-06-20 12:50:55 +02:00
return Promise.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
}
}
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].open());
}
}
if(isAlias){
if(externalContexts.hasOwnProperty(plugins["default"])){
externalContexts["default"] = externalContexts[plugins["default"]];
}else{
2018-06-20 12:50:55 +02:00
return Promise.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
}
}
2018-06-20 12:50:55 +02:00
return Promise.all(promises);
2018-03-28 08:54:16 +02:00
} else {
noContextStorage = true;
2018-05-30 03:24:27 +02:00
return externalContexts["_"].open();
}
}
function copySettings(config, settings){
var copy = ["userDir"]
config.settings = {};
copy.forEach(function(setting){
config.settings[setting] = clone(settings[setting]);
});
}
function getContextStorage(storage) {
2018-06-26 04:43:37 +02:00
if (externalContexts.hasOwnProperty(storage)) {
return externalContexts[storage];
2018-03-28 08:54:16 +02:00
} else if (externalContexts.hasOwnProperty("default")) {
return externalContexts["default"];
} else {
var contextError = new Error(log._("context.error-use-undefined-storage", {storage:storage}));
2018-03-28 08:54:16 +02:00
contextError.name = "ContextError";
throw contextError;
}
}
function createContext(id,seed) {
var scope = id;
var obj = seed || {};
obj.get = function(key, storage, callback) {
2018-06-26 04:43:37 +02:00
var context;
if (!storage && !callback) {
context = externalContexts["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
2018-06-26 04:43:37 +02:00
return context.get(scope, key, callback);
};
obj.set = function(key, value, storage, callback) {
2018-06-26 04:43:37 +02:00
var context;
if (!storage && !callback) {
context = externalContexts["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
2018-06-26 04:43:37 +02:00
context.set(scope, key, value, callback);
};
obj.keys = function(storage, callback) {
2018-06-26 04:43:37 +02:00
var context;
if (!storage && !callback) {
context = externalContexts["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
2018-06-26 04:43:37 +02:00
return context.keys(scope, callback);
};
2015-12-29 23:16:51 +01:00
return obj;
}
function getContext(localId,flowId) {
var contextId = localId;
if (flowId) {
contextId = localId+":"+flowId;
2015-12-29 23:16:51 +01:00
}
if (contexts.hasOwnProperty(contextId)) {
2015-12-29 23:16:51 +01:00
return contexts[contextId];
}
var newContext = createContext(contextId);
if (flowId) {
newContext.flow = getContext(flowId);
}
if (globalContext) {
newContext.global = globalContext;
2015-12-29 23:16:51 +01:00
}
contexts[contextId] = newContext;
return newContext;
}
2015-12-29 23:16:51 +01:00
function deleteContext(id,flowId) {
if(noContextStorage){
var contextId = id;
if (flowId) {
contextId = id+":"+flowId;
}
delete contexts[contextId];
return externalContexts["_"].delete(contextId);
}else{
2018-06-20 12:50:55 +02:00
return Promise.resolve();
2015-12-29 23:16:51 +01:00
}
}
2015-12-29 23:16:51 +01:00
function clean(flowConfig) {
var promises = [];
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].clean(Object.keys(flowConfig.allNodes)));
}
}
2015-12-29 23:16:51 +01:00
for (var id in contexts) {
if (contexts.hasOwnProperty(id)) {
var idParts = id.split(":");
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
2015-12-29 23:16:51 +01:00
delete contexts[id];
}
}
}
2018-06-20 12:50:55 +02:00
return Promise.all(promises);
2015-12-29 23:16:51 +01:00
}
2018-05-30 03:24:27 +02:00
function close() {
var promises = [];
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].close());
}
}
2018-06-20 12:50:55 +02:00
return Promise.all(promises);
2018-05-30 03:24:27 +02:00
}
2015-12-29 23:16:51 +01:00
module.exports = {
init: init,
load: load,
2015-12-29 23:16:51 +01:00
get: getContext,
2016-06-11 23:53:27 +02:00
delete: deleteContext,
2018-05-30 03:24:27 +02:00
clean: clean,
close: close
2015-12-29 23:16:51 +01:00
};