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-06-22 10:11:54 +02:00
|
|
|
var memory = require("./memory");
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
var settings;
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// A map of scope id to context instance
|
2018-03-15 07:15:26 +01:00
|
|
|
var contexts = {};
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// A map of store name to instance
|
|
|
|
var stores = {};
|
2018-07-03 15:17:42 +02:00
|
|
|
var storeList = [];
|
|
|
|
var defaultStore;
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// Whether there context storage has been configured or left as default
|
|
|
|
var hasConfiguredStore = false;
|
|
|
|
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function init(_settings) {
|
|
|
|
settings = _settings;
|
2018-07-02 16:21:13 +02:00
|
|
|
stores = {};
|
2018-05-30 03:24:27 +02:00
|
|
|
var seed = settings.functionGlobalContext || {};
|
2018-07-02 16:21:13 +02:00
|
|
|
contexts['global'] = createContext("global",seed);
|
2018-07-02 16:47:47 +02:00
|
|
|
stores["_"] = new memory();
|
2018-07-03 15:17:42 +02:00
|
|
|
defaultStore = "memory";
|
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() {
|
2018-06-29 10:48:38 +02:00
|
|
|
return new Promise(function(resolve,reject) {
|
|
|
|
// load & init plugins in settings.contextStorage
|
2018-07-03 15:17:42 +02:00
|
|
|
var plugins = settings.contextStorage || {};
|
2018-06-29 10:48:38 +02:00
|
|
|
var defaultIsAlias = false;
|
2018-05-30 03:24:27 +02:00
|
|
|
var promises = [];
|
2018-07-03 15:17:42 +02:00
|
|
|
if (plugins && Object.keys(plugins).length > 0) {
|
2018-07-02 16:21:13 +02:00
|
|
|
var hasDefault = plugins.hasOwnProperty('default');
|
|
|
|
var defaultName;
|
2018-06-29 10:48:38 +02:00
|
|
|
for (var pluginName in plugins) {
|
|
|
|
if (plugins.hasOwnProperty(pluginName)) {
|
|
|
|
// "_" is a reserved name - do not allow it to be overridden
|
|
|
|
if (pluginName === "_") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this is setting the 'default' context to be a named plugin
|
|
|
|
if (pluginName === "default" && typeof plugins[pluginName] === "string") {
|
|
|
|
// Check the 'default' alias exists before initialising anything
|
|
|
|
if (!plugins.hasOwnProperty(plugins[pluginName])) {
|
|
|
|
return reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
|
|
|
|
}
|
|
|
|
defaultIsAlias = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
if (!hasDefault && !defaultName) {
|
|
|
|
defaultName = pluginName;
|
|
|
|
}
|
2018-06-29 10:48:38 +02:00
|
|
|
var plugin;
|
|
|
|
if (plugins[pluginName].hasOwnProperty("module")) {
|
|
|
|
// Get the provided config and copy in the 'approved' top-level settings (eg userDir)
|
|
|
|
var config = plugins[pluginName].config || {};
|
|
|
|
copySettings(config, settings);
|
|
|
|
|
|
|
|
if (typeof plugins[pluginName].module === "string") {
|
|
|
|
// This config identifies the module by name - assume it is a built-in one
|
|
|
|
// TODO: check it exists locally, if not, try to require it as-is
|
|
|
|
try {
|
|
|
|
plugin = require("./"+plugins[pluginName].module);
|
|
|
|
} catch(err) {
|
|
|
|
return reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module})));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Assume `module` is an already-required module we can use
|
|
|
|
plugin = plugins[pluginName].module;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// Create a new instance of the plugin by calling its module function
|
2018-07-02 16:21:13 +02:00
|
|
|
stores[pluginName] = plugin(config);
|
2018-06-29 10:48:38 +02:00
|
|
|
} catch(err) {
|
|
|
|
return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()})));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Plugin does not specify a 'module'
|
|
|
|
return reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
|
2018-03-28 08:54:16 +02:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-29 10:48:38 +02:00
|
|
|
|
|
|
|
// Open all of the configured contexts
|
2018-07-02 16:21:13 +02:00
|
|
|
for (var plugin in stores) {
|
|
|
|
if (stores.hasOwnProperty(plugin)) {
|
|
|
|
promises.push(stores[plugin].open());
|
2018-06-29 10:48:38 +02:00
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
// There is a 'default' listed in the configuration
|
|
|
|
if (hasDefault) {
|
|
|
|
// If 'default' is an alias, point it at the right module - we have already
|
|
|
|
// checked that it exists. If it isn't an alias, then it will
|
|
|
|
// already be set to a configured store
|
|
|
|
if (defaultIsAlias) {
|
2018-07-03 15:17:42 +02:00
|
|
|
stores["_"] = stores[plugins["default"]];
|
|
|
|
defaultStore = plugins["default"];
|
|
|
|
} else {
|
|
|
|
stores["_"] = stores["default"];
|
|
|
|
defaultStore = "default";
|
2018-07-02 16:21:13 +02:00
|
|
|
}
|
|
|
|
} else if (defaultName) {
|
|
|
|
// No 'default' listed, so pick first in list as the default
|
2018-07-03 15:17:42 +02:00
|
|
|
stores["_"] = stores[defaultName];
|
|
|
|
defaultStore = defaultName;
|
|
|
|
defaultIsAlias = true;
|
|
|
|
} else {
|
|
|
|
// else there were no stores list the config object - fall through
|
|
|
|
// to below where we default to a memory store
|
|
|
|
storeList = ["memory"];
|
|
|
|
defaultStore = "memory";
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
hasConfiguredStore = true;
|
2018-07-03 15:17:42 +02:00
|
|
|
storeList = Object.keys(stores).filter(n=>!(defaultIsAlias && n==="default") && n!== "_");
|
|
|
|
} else {
|
|
|
|
// No configured plugins
|
|
|
|
promises.push(stores["_"].open())
|
|
|
|
storeList = ["memory"];
|
|
|
|
defaultStore = "memory";
|
2018-06-29 10:48:38 +02:00
|
|
|
}
|
|
|
|
return resolve(Promise.all(promises));
|
|
|
|
});
|
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 getContextStorage(storage) {
|
2018-07-02 16:21:13 +02:00
|
|
|
if (stores.hasOwnProperty(storage)) {
|
2018-06-29 10:48:38 +02:00
|
|
|
// A known context
|
2018-07-02 16:21:13 +02:00
|
|
|
return stores[storage];
|
2018-07-03 15:17:42 +02:00
|
|
|
} else if (stores.hasOwnProperty("_")) {
|
2018-06-29 10:48:38 +02:00
|
|
|
// Not known, but we have a default to fall back to
|
2018-07-03 15:17:42 +02:00
|
|
|
return stores["_"];
|
2018-03-28 08:54:16 +02:00
|
|
|
} else {
|
2018-06-29 10:48:38 +02:00
|
|
|
// Not known and no default configured
|
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-07-02 16:21:13 +02:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function createContext(id,seed) {
|
2018-07-02 16:21:13 +02:00
|
|
|
// Seed is only set for global context - sourced from functionGlobalContext
|
2018-03-23 09:18:56 +01:00
|
|
|
var scope = id;
|
|
|
|
var obj = seed || {};
|
2018-07-02 16:21:13 +02:00
|
|
|
var seedKeys;
|
|
|
|
if (seed) {
|
|
|
|
seedKeys = Object.keys(seed);
|
|
|
|
}
|
2018-06-22 10:11:54 +02:00
|
|
|
obj.get = function(key, storage, callback) {
|
2018-06-26 04:43:37 +02:00
|
|
|
var context;
|
|
|
|
if (!storage && !callback) {
|
2018-07-02 16:21:13 +02:00
|
|
|
context = stores["_"];
|
2018-06-26 04:43:37 +02:00
|
|
|
} else {
|
|
|
|
if (typeof storage === 'function') {
|
|
|
|
callback = storage;
|
2018-07-03 15:17:42 +02:00
|
|
|
storage = "_";
|
2018-06-26 04:43:37 +02:00
|
|
|
}
|
|
|
|
if (typeof callback !== 'function'){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
|
|
|
context = getContextStorage(storage);
|
2018-06-01 04:44:45 +02:00
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
if (seed) {
|
|
|
|
// Get the value from the underlying store. If it is undefined,
|
|
|
|
// check the seed for a default value.
|
|
|
|
if (callback) {
|
2018-07-12 04:13:29 +02:00
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
context.get(scope,key,function(err, v) {
|
|
|
|
if (v === undefined) {
|
|
|
|
callback(err, seed[key]);
|
|
|
|
} else {
|
|
|
|
callback(err, v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If key is an array, get the value of each key.
|
|
|
|
var storeValues = [];
|
|
|
|
var keys = key.slice();
|
|
|
|
var _key = keys.shift();
|
|
|
|
var cb = function(err, v) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
if (v === undefined) {
|
|
|
|
storeValues.push(seed[_key]);
|
|
|
|
} else {
|
|
|
|
storeValues.push(v);
|
|
|
|
}
|
|
|
|
if (keys.length === 0) {
|
|
|
|
callback.apply(null, [null].concat(storeValues));
|
|
|
|
} else {
|
|
|
|
_key = keys.shift();
|
|
|
|
context.get(scope, _key, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
context.get(scope, _key, cb);
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
} else {
|
|
|
|
// No callback, attempt to do this synchronously
|
|
|
|
var storeValue = context.get(scope,key);
|
|
|
|
if (storeValue === undefined) {
|
|
|
|
return seed[key];
|
|
|
|
} else {
|
|
|
|
return storeValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-12 04:13:29 +02:00
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
return context.get(scope, key, callback);
|
|
|
|
} else {
|
|
|
|
var storeValues = [];
|
|
|
|
var keys = key.slice();
|
|
|
|
var cb = function(err, v) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
storeValues.push(v);
|
|
|
|
if (keys.length === 0) {
|
|
|
|
callback.apply(null, [null].concat(storeValues));
|
|
|
|
} else {
|
|
|
|
context.get(scope, keys.shift(), cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
context.get(scope, keys.shift(), cb);
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
};
|
2018-06-22 10:11:54 +02:00
|
|
|
obj.set = function(key, value, storage, callback) {
|
2018-06-26 04:43:37 +02:00
|
|
|
var context;
|
|
|
|
if (!storage && !callback) {
|
2018-07-02 16:21:13 +02:00
|
|
|
context = stores["_"];
|
2018-06-26 04:43:37 +02:00
|
|
|
} else {
|
|
|
|
if (typeof storage === 'function') {
|
|
|
|
callback = storage;
|
2018-07-03 15:17:42 +02:00
|
|
|
storage = "_";
|
2018-06-26 04:43:37 +02:00
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function') {
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
|
|
|
context = getContextStorage(storage);
|
2018-06-01 04:44:45 +02:00
|
|
|
}
|
2018-07-13 13:59:45 +02:00
|
|
|
if (!Array.isArray(key)) {
|
2018-07-12 12:19:55 +02:00
|
|
|
context.set(scope, key, value, callback);
|
|
|
|
} else {
|
2018-07-13 13:59:45 +02:00
|
|
|
// If key is an array, set each key-value pair.
|
|
|
|
// If the value array is longer than the key array, then the extra values are ignored.
|
2018-07-12 12:19:55 +02:00
|
|
|
var index = 0;
|
2018-07-13 13:59:45 +02:00
|
|
|
var values = [].concat(value); // Convert the value to an array
|
|
|
|
var cb = function(err) {
|
2018-07-12 12:19:55 +02:00
|
|
|
if (err) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index++;
|
|
|
|
if (index === key.length) {
|
|
|
|
if (callback) {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-13 13:59:45 +02:00
|
|
|
if(index < values.length) {
|
|
|
|
context.set(scope, key[index], values[index], cb);
|
|
|
|
} else {
|
|
|
|
// If the value array is shorter than the key array, use null for missing values.
|
|
|
|
context.set(scope, key[index], null, cb);
|
|
|
|
}
|
2018-07-12 12:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-07-13 13:59:45 +02:00
|
|
|
context.set(scope, key[index], values[index], cb);
|
2018-07-12 12:19:55 +02:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
};
|
2018-06-22 10:11:54 +02:00
|
|
|
obj.keys = function(storage, callback) {
|
2018-06-26 04:43:37 +02:00
|
|
|
var context;
|
|
|
|
if (!storage && !callback) {
|
2018-07-02 16:21:13 +02:00
|
|
|
context = stores["_"];
|
2018-06-26 04:43:37 +02:00
|
|
|
} else {
|
|
|
|
if (typeof storage === 'function') {
|
|
|
|
callback = storage;
|
2018-07-03 15:17:42 +02:00
|
|
|
storage = "_";
|
2018-06-26 04:43:37 +02:00
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
|
|
|
context = getContextStorage(storage);
|
2018-06-01 04:44:45 +02:00
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
if (seed) {
|
|
|
|
if (callback) {
|
|
|
|
context.keys(scope, function(err,keys) {
|
|
|
|
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var keys = context.keys(scope);
|
|
|
|
return Array.from(new Set(seedKeys.concat(keys)).keys())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return context.keys(scope, callback);
|
|
|
|
}
|
2018-06-07 14:51:11 +02:00
|
|
|
};
|
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
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
newContext.global = contexts['global'];
|
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-07-02 16:21:13 +02:00
|
|
|
if(!hasConfiguredStore){
|
|
|
|
// only delete context if there's no configured storage.
|
2018-05-30 08:23:34 +02:00
|
|
|
var contextId = id;
|
|
|
|
if (flowId) {
|
|
|
|
contextId = id+":"+flowId;
|
|
|
|
}
|
|
|
|
delete contexts[contextId];
|
2018-07-02 16:21:13 +02:00
|
|
|
return stores["_"].delete(contextId);
|
2018-05-30 08:23:34 +02:00
|
|
|
}else{
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.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 = [];
|
2018-07-02 16:21:13 +02:00
|
|
|
for(var plugin in stores){
|
|
|
|
if(stores.hasOwnProperty(plugin)){
|
|
|
|
promises.push(stores[plugin].clean(Object.keys(flowConfig.allNodes)));
|
2018-05-30 08:23:34 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-29 23:16:51 +01:00
|
|
|
for (var id in contexts) {
|
2018-06-27 17:05:17 +02:00
|
|
|
if (contexts.hasOwnProperty(id) && id !== "global") {
|
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-06-20 12:50:55 +02:00
|
|
|
return Promise.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 = [];
|
2018-07-02 16:21:13 +02:00
|
|
|
for(var plugin in stores){
|
|
|
|
if(stores.hasOwnProperty(plugin)){
|
|
|
|
promises.push(stores[plugin].close());
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.all(promises);
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:17:42 +02:00
|
|
|
function listStores() {
|
|
|
|
return {default:defaultStore,stores:storeList};
|
|
|
|
}
|
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
module.exports = {
|
2018-03-23 09:18:56 +01:00
|
|
|
init: init,
|
|
|
|
load: load,
|
2018-07-03 15:17:42 +02:00
|
|
|
listStores: listStores,
|
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
|
|
|
};
|