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.
|
|
|
|
**/
|
|
|
|
|
2023-09-19 17:13:39 +02:00
|
|
|
const clone = require("clone");
|
2021-11-02 01:12:30 +01:00
|
|
|
const log = require("@node-red/util").log;
|
|
|
|
const util = require("@node-red/util").util;
|
|
|
|
const memory = require("./memory");
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2021-11-02 01:12:30 +01:00
|
|
|
let settings;
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// A map of scope id to context instance
|
2021-11-02 01:12:30 +01:00
|
|
|
let contexts = {};
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// A map of store name to instance
|
2021-11-02 01:12:30 +01:00
|
|
|
let stores = {};
|
|
|
|
let storeList = [];
|
|
|
|
let defaultStore;
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
// Whether there context storage has been configured or left as default
|
2021-11-02 01:12:30 +01:00
|
|
|
let hasConfiguredStore = false;
|
2018-07-02 16:21:13 +02:00
|
|
|
|
2018-07-19 00:40:52 +02:00
|
|
|
// Unknown Stores
|
2021-11-02 01:12:30 +01:00
|
|
|
let unknownStores = {};
|
2018-07-19 00:40:52 +02:00
|
|
|
|
|
|
|
function logUnknownStore(name) {
|
2018-07-19 07:12:01 +02:00
|
|
|
if (name) {
|
2021-11-02 01:12:30 +01:00
|
|
|
let count = unknownStores[name] || 0;
|
2018-07-23 16:20:13 +02:00
|
|
|
if (count == 0) {
|
2018-07-19 07:12:01 +02:00
|
|
|
log.warn(log._("context.unknown-store", {name: name}));
|
|
|
|
count++;
|
|
|
|
unknownStores[name] = count;
|
2018-07-23 16:20:13 +02:00
|
|
|
}
|
2018-07-19 00:40:52 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function init(_settings) {
|
|
|
|
settings = _settings;
|
2018-07-21 05:00:54 +02:00
|
|
|
contexts = {};
|
2018-07-02 16:21:13 +02:00
|
|
|
stores = {};
|
2018-07-21 05:00:54 +02:00
|
|
|
storeList = [];
|
|
|
|
hasConfiguredStore = false;
|
2021-11-02 01:12:30 +01:00
|
|
|
initialiseGlobalContext();
|
|
|
|
|
2018-07-23 16:20:13 +02:00
|
|
|
// create a default memory store - used by the unit tests that skip the full
|
|
|
|
// `load()` initialisation sequence.
|
|
|
|
// If the user has any stores configured, this will be disgarded
|
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
|
|
|
|
2021-11-02 01:12:30 +01:00
|
|
|
function initialiseGlobalContext() {
|
|
|
|
const seed = settings.functionGlobalContext || {};
|
|
|
|
contexts['global'] = createContext("global",seed);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-07-16 17:44:33 +02:00
|
|
|
if (!/^[a-zA-Z0-9_]+$/.test(pluginName)) {
|
|
|
|
return reject(new Error(log._("context.error-invalid-module-name", {name:pluginName})));
|
|
|
|
}
|
2018-06-29 10:48:38 +02:00
|
|
|
|
|
|
|
// 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) {
|
2018-10-17 08:43:09 +02:00
|
|
|
return reject(new Error(log._("context.error-loading-module2", {module:plugins[pluginName].module,message:err.toString()})));
|
2018-06-29 10:48:38 +02:00
|
|
|
}
|
|
|
|
} 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-08-09 16:37:04 +02:00
|
|
|
var moduleInfo = plugins[pluginName].module;
|
|
|
|
if (typeof moduleInfo !== 'string') {
|
|
|
|
if (moduleInfo.hasOwnProperty("toString")) {
|
|
|
|
moduleInfo = moduleInfo.toString();
|
|
|
|
} else {
|
|
|
|
moduleInfo = "custom";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info(log._("context.log-store-init", {name:pluginName, info:"module="+moduleInfo}));
|
2018-06-29 10:48:38 +02:00
|
|
|
} catch(err) {
|
2018-10-17 08:43:09 +02:00
|
|
|
return reject(new Error(log._("context.error-loading-module2",{module:pluginName,message:err.toString()})));
|
2018-06-29 10:48:38 +02:00
|
|
|
}
|
|
|
|
} 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
|
2018-07-23 16:20:13 +02:00
|
|
|
log.info(log._("context.log-store-init", {name:"default", info:"module=memory"}));
|
2018-07-03 15:17:42 +02:00
|
|
|
promises.push(stores["_"].open())
|
|
|
|
storeList = ["memory"];
|
|
|
|
defaultStore = "memory";
|
2018-06-29 10:48:38 +02:00
|
|
|
}
|
|
|
|
return resolve(Promise.all(promises));
|
2018-08-16 15:36:11 +02:00
|
|
|
}).catch(function(err) {
|
|
|
|
throw new Error(log._("context.error-loading-module",{message:err.toString()}));
|
2018-06-29 10:48:38 +02:00
|
|
|
});
|
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-23 16:20:13 +02:00
|
|
|
if (storage !== defaultStore) {
|
|
|
|
// It isn't the default store either, so log it
|
|
|
|
logUnknownStore(storage);
|
|
|
|
}
|
2018-07-03 15:17:42 +02:00
|
|
|
return stores["_"];
|
2018-03-28 08:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:10:03 +01:00
|
|
|
function followParentContext(parent, key) {
|
|
|
|
if (key === "$parent") {
|
2019-02-05 06:47:30 +01:00
|
|
|
return [parent, undefined];
|
2019-01-16 15:10:03 +01:00
|
|
|
}
|
|
|
|
else if (key.startsWith("$parent.")) {
|
|
|
|
var len = "$parent.".length;
|
|
|
|
var new_key = key.substring(len);
|
|
|
|
var ctx = parent;
|
|
|
|
while (ctx && new_key.startsWith("$parent.")) {
|
|
|
|
ctx = ctx.$parent;
|
|
|
|
new_key = new_key.substring(len);
|
|
|
|
}
|
|
|
|
return [ctx, new_key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
|
2021-05-26 14:04:09 +02:00
|
|
|
function validateContextKey(key) {
|
|
|
|
try {
|
|
|
|
const keys = Array.isArray(key) ? key : [key];
|
|
|
|
if(!keys.length) { return false }; //no key to get/set
|
|
|
|
for (let index = 0; index < keys.length; index++) {
|
|
|
|
const k = keys[index];
|
|
|
|
if (typeof k !== "string" || !k.length) {
|
|
|
|
return false; //not string or zero-length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:10:03 +01:00
|
|
|
function createContext(id,seed,parent) {
|
2018-07-02 16:21:13 +02:00
|
|
|
// Seed is only set for global context - sourced from functionGlobalContext
|
2021-11-02 01:12:30 +01:00
|
|
|
const scope = id;
|
|
|
|
const obj = {};
|
|
|
|
let seedKeys;
|
|
|
|
let insertSeedValues;
|
2018-07-02 16:21:13 +02:00
|
|
|
if (seed) {
|
|
|
|
seedKeys = Object.keys(seed);
|
2021-11-02 01:12:30 +01:00
|
|
|
seedKeys.forEach(key => {
|
|
|
|
obj[key] = seed[key];
|
|
|
|
})
|
2018-07-23 14:27:43 +02:00
|
|
|
insertSeedValues = function(keys,values) {
|
|
|
|
if (!Array.isArray(keys)) {
|
|
|
|
if (values[0] === undefined) {
|
2018-11-14 21:57:17 +01:00
|
|
|
try {
|
|
|
|
values[0] = util.getObjectProperty(seed,keys);
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === "INVALID_EXPR") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-08-06 16:55:25 +02:00
|
|
|
values[0] = undefined;
|
2018-11-14 21:57:17 +01:00
|
|
|
}
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var i=0;i<keys.length;i++) {
|
|
|
|
if (values[i] === undefined) {
|
2018-11-14 21:57:17 +01:00
|
|
|
try {
|
|
|
|
values[i] = util.getObjectProperty(seed,keys[i]);
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === "INVALID_EXPR") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-08-06 16:55:25 +02:00
|
|
|
values[i] = undefined;
|
2018-11-14 21:57:17 +01:00
|
|
|
}
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 16:21:13 +02:00
|
|
|
}
|
2021-05-26 14:04:09 +02:00
|
|
|
|
2018-09-10 23:30:51 +02:00
|
|
|
Object.defineProperties(obj, {
|
|
|
|
get: {
|
|
|
|
value: function(key, storage, callback) {
|
|
|
|
var context;
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!callback && typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = undefined;
|
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function'){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
2021-05-26 14:04:09 +02:00
|
|
|
if (!validateContextKey(key)) {
|
|
|
|
var err = Error("Invalid context key");
|
|
|
|
if(callback) {
|
|
|
|
return callback(err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
var keyParts = util.parseContextStore(key);
|
|
|
|
key = keyParts.key;
|
|
|
|
if (!storage) {
|
|
|
|
storage = keyParts.store || "_";
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
var result = followParentContext(parent, key);
|
|
|
|
if (result) {
|
|
|
|
var [ctx, new_key] = result;
|
2019-02-05 06:47:30 +01:00
|
|
|
if (ctx && new_key) {
|
2019-01-16 15:10:03 +01:00
|
|
|
return ctx.get(new_key, storage, callback);
|
|
|
|
}
|
|
|
|
else {
|
2019-02-05 06:47:30 +01:00
|
|
|
if (callback) {
|
|
|
|
return callback(undefined);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
} else {
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!storage) {
|
2018-09-10 23:30:51 +02:00
|
|
|
storage = "_";
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
}
|
2018-09-15 00:21:05 +02:00
|
|
|
context = getContextStorage(storage);
|
|
|
|
|
2018-09-10 23:30:51 +02:00
|
|
|
if (callback) {
|
|
|
|
if (!seed) {
|
|
|
|
context.get(scope,key,callback);
|
|
|
|
} else {
|
|
|
|
context.get(scope,key,function() {
|
|
|
|
if (arguments[0]) {
|
|
|
|
callback(arguments[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var results = Array.prototype.slice.call(arguments,[1]);
|
2018-11-14 21:57:17 +01:00
|
|
|
try {
|
|
|
|
insertSeedValues(key,results);
|
|
|
|
} catch(err) {
|
|
|
|
callback.apply(err);
|
|
|
|
return
|
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
// Put the err arg back
|
|
|
|
results.unshift(undefined);
|
|
|
|
callback.apply(null,results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No callback, attempt to do this synchronously
|
|
|
|
var results = context.get(scope,key);
|
|
|
|
if (seed) {
|
|
|
|
if (Array.isArray(key)) {
|
|
|
|
insertSeedValues(key,results);
|
|
|
|
} else if (results === undefined){
|
2018-11-14 21:57:17 +01:00
|
|
|
try {
|
|
|
|
results = util.getObjectProperty(seed,key);
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === "INVALID_EXPR") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
results = undefined;
|
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
2018-07-12 04:13:29 +02:00
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
},
|
|
|
|
set: {
|
|
|
|
value: function(key, value, storage, callback) {
|
|
|
|
var context;
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!callback && typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = undefined;
|
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function'){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
2021-05-26 14:04:09 +02:00
|
|
|
if (!validateContextKey(key)) {
|
|
|
|
var err = Error("Invalid context key");
|
|
|
|
if(callback) {
|
|
|
|
return callback(err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
var keyParts = util.parseContextStore(key);
|
|
|
|
key = keyParts.key;
|
|
|
|
if (!storage) {
|
|
|
|
storage = keyParts.store || "_";
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
var result = followParentContext(parent, key);
|
|
|
|
if (result) {
|
|
|
|
var [ctx, new_key] = result;
|
2019-02-05 06:47:30 +01:00
|
|
|
if (ctx && new_key) {
|
2019-01-16 15:10:03 +01:00
|
|
|
return ctx.set(new_key, value, storage, callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (callback) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
} else {
|
2018-09-15 00:21:05 +02:00
|
|
|
if (!storage) {
|
2018-09-10 23:30:51 +02:00
|
|
|
storage = "_";
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 00:21:05 +02:00
|
|
|
context = getContextStorage(storage);
|
|
|
|
|
2018-09-10 23:30:51 +02:00
|
|
|
context.set(scope, key, value, callback);
|
2018-06-26 04:43:37 +02:00
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
},
|
|
|
|
keys: {
|
|
|
|
value: function(storage, callback) {
|
|
|
|
var context;
|
|
|
|
if (!storage && !callback) {
|
|
|
|
context = stores["_"];
|
|
|
|
} else {
|
|
|
|
if (typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = "_";
|
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function') {
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
|
|
|
context = getContextStorage(storage);
|
|
|
|
}
|
2019-03-04 17:10:39 +01:00
|
|
|
if (seed && settings.exportGlobalContextKeys !== false) {
|
2018-09-10 23:30:51 +02:00
|
|
|
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-07-02 16:21:13 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
});
|
2019-01-16 15:10:03 +01:00
|
|
|
if (parent) {
|
|
|
|
Object.defineProperty(obj, "$parent", {
|
|
|
|
value: parent
|
|
|
|
});
|
|
|
|
}
|
2015-12-29 23:16:51 +01:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:10:03 +01:00
|
|
|
function createRootContext() {
|
|
|
|
var obj = {};
|
|
|
|
Object.defineProperties(obj, {
|
|
|
|
get: {
|
|
|
|
value: function(key, storage, callback) {
|
2020-03-28 00:47:12 +01:00
|
|
|
if (!callback && typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = undefined;
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback()
|
|
|
|
return;
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set: {
|
|
|
|
value: function(key, value, storage, callback) {
|
2020-03-28 00:47:12 +01:00
|
|
|
if (!callback && typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = undefined;
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
keys: {
|
|
|
|
value: function(storage, callback) {
|
2020-03-28 00:47:12 +01:00
|
|
|
if (!callback && typeof storage === 'function') {
|
|
|
|
callback = storage;
|
|
|
|
storage = undefined;
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-16 15:10:03 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:47:12 +01:00
|
|
|
/**
|
|
|
|
* Get a flow-level context object.
|
|
|
|
* @param {string} flowId [description]
|
|
|
|
* @param {string} parentFlowId the id of the parent flow. undefined
|
|
|
|
* @return {object}} the context object
|
|
|
|
*/
|
|
|
|
function getFlowContext(flowId,parentFlowId) {
|
|
|
|
if (contexts.hasOwnProperty(flowId)) {
|
|
|
|
return contexts[flowId];
|
|
|
|
}
|
|
|
|
var parentContext = contexts[parentFlowId];
|
|
|
|
if (!parentContext) {
|
|
|
|
parentContext = createRootContext();
|
|
|
|
contexts[parentFlowId] = parentContext;
|
|
|
|
// throw new Error("Flow "+flowId+" is missing parent context "+parentFlowId);
|
|
|
|
}
|
|
|
|
var newContext = createContext(flowId,undefined,parentContext);
|
|
|
|
contexts[flowId] = newContext;
|
|
|
|
return newContext;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContext(nodeId, flowId) {
|
|
|
|
var contextId = nodeId;
|
2015-12-29 23:16:51 +01:00
|
|
|
if (flowId) {
|
2020-03-28 00:47:12 +01:00
|
|
|
contextId = nodeId+":"+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];
|
|
|
|
}
|
2020-03-28 00:47:12 +01:00
|
|
|
var newContext = createContext(contextId);
|
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
if (flowId) {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = contexts[flowId];
|
|
|
|
if (!flowContext) {
|
|
|
|
// This is most likely due to a unit test for a node which doesn't
|
|
|
|
// initialise the flow properly.
|
|
|
|
// To keep things working, initialise the missing context.
|
|
|
|
// This *does not happen* in normal node-red operation
|
|
|
|
flowContext = createContext(flowId,undefined,createRootContext());
|
|
|
|
contexts[flowId] = flowContext;
|
2019-01-16 15:10:03 +01:00
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
Object.defineProperty(newContext, 'flow', {
|
2019-01-16 15:10:03 +01:00
|
|
|
value: flowContext
|
2018-09-10 23:30:51 +02:00
|
|
|
});
|
2017-04-12 21:48:43 +02:00
|
|
|
}
|
2018-09-10 23:30:51 +02:00
|
|
|
Object.defineProperty(newContext, 'global', {
|
|
|
|
value: contexts['global']
|
|
|
|
})
|
2015-12-29 23:16:51 +01:00
|
|
|
contexts[contextId] = newContext;
|
|
|
|
return newContext;
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2021-11-02 01:12:30 +01:00
|
|
|
/**
|
|
|
|
* Delete the context of the given node/flow/global
|
|
|
|
*
|
|
|
|
* If the user has configured a context store, this
|
|
|
|
* will no-op a request to delete node/flow context.
|
|
|
|
*/
|
2015-12-29 23:16:51 +01:00
|
|
|
function deleteContext(id,flowId) {
|
2021-11-02 01:12:30 +01:00
|
|
|
if (id === "global") {
|
|
|
|
// 1. delete global from all configured stores
|
|
|
|
var promises = [];
|
|
|
|
for(var plugin in stores){
|
|
|
|
if(stores.hasOwnProperty(plugin)){
|
|
|
|
promises.push(stores[plugin].delete('global'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
// 2. delete global context
|
|
|
|
delete contexts['global'];
|
|
|
|
// 3. reinitialise global context
|
|
|
|
initialiseGlobalContext();
|
|
|
|
})
|
|
|
|
} else if (!hasConfiguredStore) {
|
2018-07-02 16:21:13 +02:00
|
|
|
// 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);
|
2021-11-02 01:12:30 +01: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
|
|
|
|
2021-11-02 01:12:30 +01:00
|
|
|
/**
|
|
|
|
* Delete any contexts that are no longer in use
|
|
|
|
* @param flowConfig object includes allNodes as object of id->node
|
|
|
|
*
|
|
|
|
* If flowConfig is undefined, all flow/node contexts will be removed
|
|
|
|
**/
|
2015-12-29 23:16:51 +01:00
|
|
|
function clean(flowConfig) {
|
2023-03-03 19:46:49 +01:00
|
|
|
flowConfig = flowConfig || { allNodes: {}, subflows: {} };
|
|
|
|
const knownNodes = new Set(Object.keys(flowConfig.allNodes))
|
|
|
|
|
|
|
|
// We need to alias all of the subflow instance contents
|
2023-03-03 21:02:46 +01:00
|
|
|
for (const subflow of Object.values(flowConfig.subflows || {})) {
|
2023-03-03 19:46:49 +01:00
|
|
|
subflow.instances.forEach(instance => {
|
|
|
|
for (const nodeId of Object.keys(subflow.nodes || {})) {
|
|
|
|
knownNodes.add(`${instance.id}-${nodeId}`)
|
|
|
|
}
|
|
|
|
for (const nodeId of Object.keys(subflow.configs || {})) {
|
|
|
|
knownNodes.add(`${instance.id}-${nodeId}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
var promises = [];
|
2023-03-03 19:46:49 +01:00
|
|
|
for (const store of Object.values(stores)){
|
|
|
|
promises.push(store.clean(Array.from(knownNodes)));
|
2018-05-30 08:23:34 +02:00
|
|
|
}
|
2023-03-03 19:46:49 +01:00
|
|
|
for (const id of Object.keys(contexts)) {
|
|
|
|
if (id !== "global") {
|
2018-03-23 09:18:56 +01:00
|
|
|
var idParts = id.split(":");
|
2023-03-03 19:46:49 +01:00
|
|
|
if (!knownNodes.has(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
|
|
|
|
2021-11-02 01:12:30 +01:00
|
|
|
/**
|
|
|
|
* Deletes all contexts, including global and reinitialises global to
|
|
|
|
* initial state.
|
|
|
|
*/
|
|
|
|
function clear() {
|
|
|
|
return clean().then(function() {
|
|
|
|
return deleteContext('global')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
2020-03-28 00:47:12 +01:00
|
|
|
getFlowContext:getFlowContext,
|
2016-06-11 23:53:27 +02:00
|
|
|
delete: deleteContext,
|
2018-05-30 03:24:27 +02:00
|
|
|
clean: clean,
|
2021-11-02 01:12:30 +01:00
|
|
|
clear: clear,
|
2018-07-20 16:26:47 +02:00
|
|
|
close: close
|
2015-12-29 23:16:51 +01:00
|
|
|
};
|