1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow multiple instances of a given storage module to exist

This commit is contained in:
HirokiUchikawa 2018-05-24 21:42:40 +09:00
parent 7fafa21a1b
commit 28d05e2449
6 changed files with 157 additions and 144 deletions

View File

@ -29,8 +29,8 @@ function init(_settings) {
externalContexts = {};
// init memory plugin
externalContexts["_"] = require("./memory");
externalContexts["_"].init();
var memory = require("./memory");
externalContexts["_"] = memory();
globalContext = createContext("global",settings.functionGlobalContext || {});
}
@ -61,8 +61,7 @@ function load() {
} else {
plugin = plugins[pluginName].module;
}
plugin.init(config);
externalContexts[pluginName] = plugin;
externalContexts[pluginName] = plugin(config);
}else{
throw new Error(log._("context.error-module-not-defined", {storage:pluginName}));
}

View File

@ -18,61 +18,64 @@ var JsonDB = require('node-json-db');
var fs = require('fs-extra');
var path = require("path");
var configs;
var storageBaseDir;
var storages;
function createStorage(scope) {
function createStorage(storageBaseDir, scope) {
var i = scope.indexOf(":")
if(i === -1){
if(scope === "global"){
storages[scope] = new JsonDB(path.join(storageBaseDir,"global",scope), true, true);
return new JsonDB(path.join(storageBaseDir,"global",scope), true, true);
}else{ // scope:flow
storages[scope] = new JsonDB(path.join(storageBaseDir,scope,"flow"), true, true);
return new JsonDB(path.join(storageBaseDir,scope,"flow"), true, true);
}
}else{ // scope:local
var ids = scope.split(":")
storages[scope] = new JsonDB(path.join(storageBaseDir,ids[1],ids[0]), true, true);
return new JsonDB(path.join(storageBaseDir,ids[1],ids[0]), true, true);
}
}
var localfilesystem = {
init: function(_configs) {
configs = _configs;
storages = {};
if (!configs.dir) {
if(configs.settings && configs.settings.userDir){
storageBaseDir = path.join(configs.settings.userDir,"contexts");
function getStoragePath(config) {
var base = config.base || "contexts";
var storageBaseDir;
if (!config.dir) {
if(config.settings && config.settings.userDir){
storageBaseDir = path.join(config.settings.userDir, base);
}else{
try {
fs.statSync(path.join(process.env.NODE_RED_HOME,".config.json"));
storageBaseDir = path.join(process.env.NODE_RED_HOME,"contexts");
storageBaseDir = path.join(process.env.NODE_RED_HOME, base);
} catch(err) {
try {
// Consider compatibility for older versions
if (process.env.HOMEPATH) {
fs.statSync(path.join(process.env.HOMEPATH,".node-red",".config.json"));
storageBaseDir = path.join(process.env.HOMEPATH,".node-red","contexts");
storageBaseDir = path.join(process.env.HOMEPATH, ".node-red", base);
}
} catch(err) {
}
if (!storageBaseDir) {
storageBaseDir = path.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red","contexts");
storageBaseDir = path.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red", base);
}
}
}
}else{
storageBaseDir = configs.dir;
storageBaseDir = path.join(config.dir, base);
}
},
get: function (scope, key) {
if(!storages[scope]){
createStorage(scope);
return storageBaseDir;
}
function LocalFileSystem(config){
this.config = config;
this.storageBaseDir = getStoragePath(this.config);
this.storages = {};
}
LocalFileSystem.prototype.get = function (scope, key) {
if(!this.storages[scope]){
return undefined;
}
try{
storages[scope].reload();
return storages[scope].getData("/" + key.replace(/\./g,"/"));
this.storages[scope].reload();
return this.storages[scope].getData("/" + key.replace(/\./g,"/"));
}catch(err){
if(err.name === "DataError"){
return undefined;
@ -80,40 +83,44 @@ var localfilesystem = {
throw err;
}
}
},
}
set: function (scope, key, value) {
if(!storages[scope]){
createStorage(scope);
LocalFileSystem.prototype.set = function(scope, key, value) {
if(!this.storages[scope]){
this.storages[scope] = createStorage(this.storageBaseDir ,scope);
}
if(value){
storages[scope].push("/" + key.replace(/\./g,"/"), value);
this.storages[scope].push("/" + key.replace(/\./g,"/"), value);
}else{
storages[scope].delete("/" + key.replace(/\./g,"/"));
this.storages[scope].delete("/" + key.replace(/\./g,"/"));
}
},
keys: function (scope) {
if(!storages[scope]){
}
LocalFileSystem.prototype.keys = function(scope) {
if(!this.storages[scope]){
return [];
}
return Object.keys(storages[scope].getData("/"));
},
delete: function(scope){
if(storages[scope]){
storages[scope].delete("/");
return Object.keys(this.storages[scope].getData("/"));
}
LocalFileSystem.prototype.delete = function(scope){
if(this.storages[scope]){
this.storages[scope].delete("/");
if(scope.indexOf(":") === -1){
fs.removeSync(path.dirname(storages[scope].filename));
fs.removeSync(path.dirname(this.storages[scope].filename));
}else{
try{
fs.statSync(storages[scope].filename);
fs.unlinkSync(storages[scope].filename);
fs.statSync(this.storages[scope].filename);
fs.unlinkSync(this.storages[scope].filename);
}catch(err){
console.log("deleted");
}
}
delete storages[scope];
delete this.storages[scope];
}
}
module.exports = function(config){
return new LocalFileSystem(config);
};
module.exports = localfilesystem;

View File

@ -16,43 +16,53 @@
var util = require("../../util");
var data;
function Memory(config){
this.data = {};
}
var memory = {
init: function(config) {
data = {};
},
get: function(scope, key) {
if(!data[scope]){
data[scope] = {};
Memory.prototype.get = function(scope, key) {
if(!this.data[scope]){
return undefined;
}
return util.getMessageProperty(data[scope],key);
},
set: function(scope, key, value) {
if(!data[scope]){
data[scope] = {};
return util.getMessageProperty(this.data[scope],key);
};
Memory.prototype.set =function(scope, key, value) {
if(!this.data[scope]){
this.data[scope] = {};
}
util.setMessageProperty(data[scope],key,value);
},
keys: function(scope){
if(!data[scope]){
data[scope] = {};
util.setMessageProperty(this.data[scope],key,value);
};
Memory.prototype.keys = function(scope){
if(!this.data[scope]){
return [];
}
var keysData = Object.keys(data[scope]);
if (scope !== "global") {
return keysData;
return Object.keys(this.data[scope]);
} else {
return keysData.filter(function (key) {
return Object.keys(this.data[scope]).filter(function (key) {
return key !== "set" && key !== "get" && key !== "keys";
});
}
},
delete: function(scope){
delete data[scope];
},
setGlobalContext: function(seed){
data["global"] = seed;
}
};
module.exports = memory;
Memory.prototype.delete = function(scope){
delete this.data[scope];
};
Memory.prototype.open = function(){
return true;
};
Memory.prototype.close = function(){
delete this.data;
};
Memory.prototype.setGlobalContext= function(seed){
this.data["global"] = seed;
};
module.exports = function(config){
return new Memory(config);
};

View File

@ -151,14 +151,13 @@ describe('context', function() {
var stubDelete = sinon.stub();
var contextStorage={
test:{
module: {
init: function() {
return true;
},
get: stubGet,
set: stubSet,
keys: stubKeys,
delete: stubDelete
module: function(config){
function Test(){}
Test.prototype.get = stubGet;
Test.prototype.set = stubSet;
Test.prototype.keys = stubKeys;
Test.prototype.delete = stubDelete;
return new Test(config);
},
config:{}
}
@ -228,14 +227,13 @@ describe('context', function() {
var contextDefaultStorage={
default: "test",
test:{
module: {
init: function() {
return true;
},
get: stubGet,
set: stubSet,
keys: stubKeys,
delete: stubDelete
module: function(config){
function Test(){}
Test.prototype.get = stubGet;
Test.prototype.set = stubSet;
Test.prototype.keys = stubKeys;
Test.prototype.delete = stubDelete;
return new Test(config);
},
config:{}
}

View File

@ -17,23 +17,21 @@
var should = require('should');
var fs = require('fs-extra');
var path = require("path");
var context = require('../../../../../red/runtime/nodes/context/localfilesystem');
var LocalFileSystem = require('../../../../../red/runtime/nodes/context/localfilesystem');
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
describe('localfilesystem',function() {
var context;
beforeEach(function() {
context.init({dir: resourcesDir});
context = LocalFileSystem({dir: resourcesDir});
});
afterEach(function() {
context.delete("nodeX");
context.delete("nodeY");
afterEach(function(done) {
fs.remove(resourcesDir).then(function(result){
return done(result);
});
after(function() {
fs.removeSync(resourcesDir);
});
describe('#get/set',function() {

View File

@ -15,12 +15,13 @@
**/
var should = require('should');
var context = require('../../../../../red/runtime/nodes/context/memory');
var Memory = require('../../../../../red/runtime/nodes/context/memory');
describe('memory',function() {
var context;
beforeEach(function() {
context.init({});
context = Memory({});
});
describe('#get/set',function() {