2015-12-29 23:16:51 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +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.
|
|
|
|
**/
|
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
var clone = require("clone");
|
2018-04-20 04:41:29 +02:00
|
|
|
var log = require("../../log");
|
2018-05-30 03:24:27 +02:00
|
|
|
var when = require("when");
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
var settings;
|
2018-03-15 07:15:26 +01:00
|
|
|
var contexts = {};
|
|
|
|
var globalContext = null;
|
2018-03-23 09:18:56 +01:00
|
|
|
var externalContexts = {};
|
2018-03-28 08:54:16 +02:00
|
|
|
var noContextStorage = false;
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
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-24 14:42:40 +02:00
|
|
|
var memory = require("./memory");
|
2018-05-30 03:24:27 +02:00
|
|
|
var seed = settings.functionGlobalContext || {};
|
2018-05-24 14:42:40 +02:00
|
|
|
externalContexts["_"] = memory();
|
2018-05-30 03:24:27 +02:00
|
|
|
externalContexts["_"].setGlobalContext(seed);
|
|
|
|
globalContext = createContext("global",seed);
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function load() {
|
|
|
|
// load & init plugins in settings.contextStorage
|
|
|
|
var plugins = settings.contextStorage;
|
2018-04-23 03:45:30 +02:00
|
|
|
var isAlias = false;
|
2018-03-23 09:18:56 +01:00
|
|
|
if (plugins) {
|
2018-05-30 03:24:27 +02:00
|
|
|
var promises = [];
|
2018-03-28 08:54:16 +02:00
|
|
|
noContextStorage = false;
|
2018-03-23 09:18:56 +01:00
|
|
|
for(var pluginName in plugins){
|
|
|
|
if(pluginName === "_"){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(pluginName === "default" && typeof plugins[pluginName] === "string"){
|
2018-04-23 03:45:30 +02:00
|
|
|
isAlias = true;
|
2018-03-23 09:18:56 +01:00
|
|
|
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-05-30 03:24:27 +02:00
|
|
|
return when.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;
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
externalContexts[pluginName] = plugin(config);
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{
|
2018-05-30 03:24:27 +02:00
|
|
|
return when.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2017-10-10 22:13:38 +02:00
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
for(var plugin in externalContexts){
|
|
|
|
if(externalContexts.hasOwnProperty(plugin)){
|
|
|
|
promises.push(externalContexts[plugin].open());
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 03:45:30 +02:00
|
|
|
if(isAlias){
|
|
|
|
if(externalContexts.hasOwnProperty(plugins["default"])){
|
|
|
|
externalContexts["default"] = externalContexts[plugins["default"]];
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{
|
2018-05-30 03:24:27 +02:00
|
|
|
return when.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-30 03:24:27 +02:00
|
|
|
return when.all(promises);
|
2018-03-28 08:54:16 +02:00
|
|
|
} else {
|
|
|
|
noContextStorage = true;
|
2018-05-30 03:24:27 +02:00
|
|
|
return externalContexts["_"].open();
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function copySettings(config, settings){
|
|
|
|
var copy = ["userDir"]
|
|
|
|
config.settings = {};
|
|
|
|
copy.forEach(function(setting){
|
|
|
|
config.settings[setting] = clone(settings[setting]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:41:29 +02:00
|
|
|
function parseStorage(key) {
|
2018-05-23 03:23:39 +02:00
|
|
|
if (!key || key.charAt(0) !== '#') {
|
2018-04-20 04:41:29 +02:00
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
var endOfStorageName = key.indexOf(".");
|
|
|
|
if (endOfStorageName == -1) {
|
|
|
|
endOfStorageName = key.length;
|
|
|
|
}
|
|
|
|
return key.substring(1,endOfStorageName)||"default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseKey(key) {
|
|
|
|
if (!key) {
|
|
|
|
throw new Error(log._("context.error-key-zero-length"));
|
2018-03-28 08:54:16 +02:00
|
|
|
}
|
2018-04-20 04:41:29 +02:00
|
|
|
var indexSpace = key.indexOf(" ");
|
|
|
|
if (indexSpace != -1) {
|
|
|
|
throw new Error(log._("context.error-unexpected-space-character", {index:indexSpace}));
|
|
|
|
}
|
|
|
|
var keyPath = { storage: "", key: "" };
|
|
|
|
var indexDot = key.indexOf(".");
|
2018-06-01 04:44:45 +02:00
|
|
|
// The key of "#file" should be treated as a key without persistable context.
|
2018-04-20 04:41:29 +02:00
|
|
|
if (indexDot != -1) {
|
|
|
|
keyPath.storage = parseStorage(key);
|
|
|
|
}
|
|
|
|
if (keyPath.storage) {
|
|
|
|
keyPath.key = key.substring(indexDot + 1);
|
2018-03-28 08:54:16 +02:00
|
|
|
} else {
|
|
|
|
keyPath.key = key;
|
|
|
|
}
|
2018-04-20 04:41:29 +02:00
|
|
|
if(!keyPath.key) {
|
|
|
|
throw new Error(log._("context.error-empty-key"));
|
|
|
|
}
|
2018-03-28 08:54:16 +02:00
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:41:29 +02:00
|
|
|
function getContextStorage(storage) {
|
|
|
|
if (noContextStorage || !storage) {
|
2018-03-28 08:54:16 +02:00
|
|
|
return externalContexts["_"];
|
2018-04-20 04:41:29 +02:00
|
|
|
} else if (externalContexts.hasOwnProperty(storage)) {
|
|
|
|
return externalContexts[storage];
|
2018-03-28 08:54:16 +02:00
|
|
|
} else if (externalContexts.hasOwnProperty("default")) {
|
|
|
|
return externalContexts["default"];
|
|
|
|
} else {
|
2018-04-20 04:41:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function createContext(id,seed) {
|
|
|
|
var scope = id;
|
|
|
|
var obj = seed || {};
|
2018-03-15 07:15:26 +01:00
|
|
|
|
|
|
|
obj.get = function(key) {
|
2018-03-28 08:54:16 +02:00
|
|
|
var keyPath = parseKey(key);
|
2018-04-20 04:41:29 +02:00
|
|
|
var context = getContextStorage(keyPath.storage);
|
2018-06-01 04:44:45 +02:00
|
|
|
if(key === keyPath.key){
|
|
|
|
return context.get(scope, keyPath.key);
|
|
|
|
}else{
|
|
|
|
return context.getAsync(scope, keyPath.key);
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
};
|
|
|
|
obj.set = function(key, value) {
|
2018-03-28 08:54:16 +02:00
|
|
|
var keyPath = parseKey(key);
|
2018-04-20 04:41:29 +02:00
|
|
|
var context = getContextStorage(keyPath.storage);
|
2018-06-01 04:44:45 +02:00
|
|
|
if(key === keyPath.key){
|
|
|
|
return context.set(scope, keyPath.key, value);
|
|
|
|
}else{
|
|
|
|
return context.setAsync(scope, keyPath.key, value);
|
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
};
|
2018-04-20 04:41:29 +02:00
|
|
|
obj.keys = function(storage) {
|
|
|
|
var storageName = parseStorage(storage);
|
|
|
|
var context = getContextStorage(storageName);
|
2018-06-01 04:44:45 +02:00
|
|
|
if(!storage){
|
|
|
|
return context.keys(scope);
|
|
|
|
}else{
|
|
|
|
return context.keysAsync(scope);
|
|
|
|
}
|
|
|
|
};
|
2015-12-29 23:16:51 +01:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContext(localId,flowId) {
|
|
|
|
var contextId = localId;
|
|
|
|
if (flowId) {
|
2018-03-23 09:18:56 +01:00
|
|
|
contextId = localId+":"+flowId;
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
2016-07-04 00:08:53 +02: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);
|
2017-04-12 21:48:43 +02:00
|
|
|
}
|
|
|
|
if (globalContext) {
|
|
|
|
newContext.global = globalContext;
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
|
|
|
contexts[contextId] = newContext;
|
|
|
|
return newContext;
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
function deleteContext(id,flowId) {
|
2018-05-30 08:23:34 +02:00
|
|
|
if(noContextStorage){
|
|
|
|
var contextId = id;
|
|
|
|
if (flowId) {
|
|
|
|
contextId = id+":"+flowId;
|
|
|
|
}
|
|
|
|
delete contexts[contextId];
|
|
|
|
return externalContexts["_"].delete(contextId);
|
|
|
|
}else{
|
|
|
|
return when.resolve();
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
function clean(flowConfig) {
|
2018-05-30 08:23:34 +02:00
|
|
|
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)) {
|
2018-03-23 09:18:56 +01:00
|
|
|
var idParts = id.split(":");
|
2016-07-04 00:08:53 +02:00
|
|
|
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
|
2015-12-29 23:16:51 +01:00
|
|
|
delete contexts[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
return when.all(promises);
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
2018-03-15 07:15:26 +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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return when.all(promises);
|
|
|
|
}
|
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
module.exports = {
|
2018-03-23 09:18:56 +01:00
|
|
|
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
|
|
|
};
|