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

Merge branch 'master' into repackage

This commit is contained in:
Nick O'Leary 2018-08-15 20:46:56 +01:00
commit a3aec6b939
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
13 changed files with 510 additions and 228 deletions

View File

@ -1,3 +1,12 @@
#### 0.19.1: Maintenance Release
- Pull in latest twitter node
- Handle windows paths for context storage
- Handle persisting objects with circular refs in context
- Ensure js editor can expand to fill available space
- Add example localfilesystem contextStorage to settings
- Fix template node handling of nested context tags
#### 0.19: Milestone Release #### 0.19: Milestone Release
Editor Editor
@ -59,6 +68,7 @@ Runtime
- Handle loading empty nodesDir - Handle loading empty nodesDir
- Add 'private' property to userDir generated package.json - Add 'private' property to userDir generated package.json
- Add RED.require to allow nodes to access other modules - Add RED.require to allow nodes to access other modules
- Ensure add/remove modules are run sequentially
#### 0.18.7: Maintenance Release #### 0.18.7: Maintenance Release

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red", "name": "node-red",
"version": "0.18.7", "version": "0.19.1",
"description": "A visual tool for wiring the Internet of Things", "description": "A visual tool for wiring the Internet of Things",
"homepage": "http://nodered.org", "homepage": "http://nodered.org",
"license": "Apache-2.0", "license": "Apache-2.0",
@ -39,7 +39,7 @@
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"body-parser": "1.18.3", "body-parser": "1.18.3",
"cheerio": "0.22.0", "cheerio": "0.22.0",
"clone": "2.1.1", "clone": "2.1.2",
"cookie": "0.3.1", "cookie": "0.3.1",
"cookie-parser": "1.4.3", "cookie-parser": "1.4.3",
"cors": "2.8.4", "cors": "2.8.4",
@ -50,7 +50,7 @@
"fs-extra": "5.0.0", "fs-extra": "5.0.0",
"fs.notify": "0.0.4", "fs.notify": "0.0.4",
"hash-sum": "1.0.2", "hash-sum": "1.0.2",
"i18next": "^11.4.0", "i18next": "11.6.0",
"is-utf8": "0.2.1", "is-utf8": "0.2.1",
"js-yaml": "3.12.0", "js-yaml": "3.12.0",
"json-stringify-safe": "5.0.1", "json-stringify-safe": "5.0.1",
@ -60,11 +60,11 @@
"mime": "1.4.1", "mime": "1.4.1",
"mqtt": "2.18.3", "mqtt": "2.18.3",
"multer": "1.3.1", "multer": "1.3.1",
"mustache": "2.3.0", "mustache": "2.3.1",
"node-red-node-email": "0.1.*", "node-red-node-email": "0.1.*",
"node-red-node-feedparser": "^0.1.12", "node-red-node-feedparser": "^0.1.12",
"node-red-node-rbe": "0.2.*", "node-red-node-rbe": "0.2.*",
"node-red-node-twitter": "*", "node-red-node-twitter": "^1.1.0",
"nopt": "4.0.1", "nopt": "4.0.1",
"oauth2orize": "1.11.0", "oauth2orize": "1.11.0",
"on-headers": "1.0.1", "on-headers": "1.0.1",
@ -72,10 +72,10 @@
"passport-http-bearer": "1.0.1", "passport-http-bearer": "1.0.1",
"passport-oauth2-client-password": "0.1.2", "passport-oauth2-client-password": "0.1.2",
"raw-body": "2.3.3", "raw-body": "2.3.3",
"request": "2.87.0", "request": "2.88.0",
"semver": "5.5.0", "semver": "5.5.0",
"sentiment": "2.1.0", "sentiment": "2.1.0",
"uglify-js": "3.4.5", "uglify-js": "3.4.7",
"when": "3.7.8", "when": "3.7.8",
"ws": "1.1.5", "ws": "1.1.5",
"xml2js": "0.4.19" "xml2js": "0.4.19"
@ -84,7 +84,7 @@
"bcrypt": "~2.0.0" "bcrypt": "~2.0.0"
}, },
"devDependencies": { "devDependencies": {
"chromedriver": "^2.40.0", "chromedriver": "^2.41.0",
"grunt": "~1.0.3", "grunt": "~1.0.3",
"grunt-chmod": "~1.1.1", "grunt-chmod": "~1.1.1",
"grunt-cli": "~1.2.0", "grunt-cli": "~1.2.0",
@ -94,7 +94,7 @@
"grunt-contrib-concat": "~1.0.1", "grunt-contrib-concat": "~1.0.1",
"grunt-contrib-copy": "~1.0.0", "grunt-contrib-copy": "~1.0.0",
"grunt-contrib-jshint": "~1.1.0", "grunt-contrib-jshint": "~1.1.0",
"grunt-contrib-uglify": "~3.3.0", "grunt-contrib-uglify": "~3.4.0",
"grunt-contrib-watch": "~1.1.0", "grunt-contrib-watch": "~1.1.0",
"grunt-jsdoc": "^2.2.1", "grunt-jsdoc": "^2.2.1",
"grunt-jsdoc-to-markdown": "^4.0.0", "grunt-jsdoc-to-markdown": "^4.0.0",

View File

@ -16,7 +16,7 @@
RED.editor.types._js = (function() { RED.editor.types._js = (function() {
var template = '<script type="text/x-red" data-template-name="_js"><div class="form-row node-text-editor-row" style="width: 700px"><div style="height: 200px;min-height: 150px;" class="node-text-editor" id="node-input-js"></div></div></script>'; var template = '<script type="text/x-red" data-template-name="_js"><div class="form-row node-text-editor-row"><div style="height: 200px;min-height: 150px;" class="node-text-editor" id="node-input-js"></div></div></script>';
return { return {
init: function() { init: function() {
@ -58,7 +58,6 @@ RED.editor.types._js = (function() {
for (var i=0;i<rows.size();i++) { for (var i=0;i<rows.size();i++) {
height -= $(rows[i]).outerHeight(true); height -= $(rows[i]).outerHeight(true);
} }
height -= (parseInt($("#dialog-form").css("marginTop"))+parseInt($("#dialog-form").css("marginBottom")));
$(".node-text-editor").css("height",height+"px"); $(".node-text-editor").css("height",height+"px");
expressionEditor.resize(); expressionEditor.resize();
}, },

View File

@ -280,8 +280,7 @@ RED.sidebar.info = (function() {
if (infoText) { if (infoText) {
setInfoText(infoText); setInfoText(infoText);
} }
$(".sidebar-node-info-stack").scrollTop(0);
$(".node-info-property-header").click(function(e) { $(".node-info-property-header").click(function(e) {
e.preventDefault(); e.preventDefault();
expandedSections["property"] = !expandedSections["property"]; expandedSections["property"] = !expandedSections["property"];
@ -395,10 +394,9 @@ RED.sidebar.info = (function() {
function set(html,title) { function set(html,title) {
// tips.stop(); // tips.stop();
// sections.show(); // sections.show();
// nodeSection.container.hide();
infoSection.title.text(title||"");
refresh(null); refresh(null);
$(infoSection.content).empty(); nodeSection.container.hide();
infoSection.title.text(title||RED._("sidebar.info.info"));
setInfoText(html); setInfoText(html);
$(".sidebar-node-info-stack").scrollTop(0); $(".sidebar-node-info-stack").scrollTop(0);
} }

View File

@ -65,13 +65,28 @@
.attr('height',chartSize[1]/nav_scale/scaleFactor) .attr('height',chartSize[1]/nav_scale/scaleFactor)
} }
} }
function toggle() {
if (!isShowing) {
isShowing = true;
$("#btn-navigate").addClass("selected");
resizeNavBorder();
refreshNodes();
$("#chart").on("scroll",onScroll);
navContainer.fadeIn(200);
} else {
isShowing = false;
navContainer.fadeOut(100);
$("#chart").off("scroll",onScroll);
$("#btn-navigate").removeClass("selected");
}
}
return { return {
init: function() { init: function() {
$(window).resize(resizeNavBorder); $(window).resize(resizeNavBorder);
RED.events.on("sidebar:resize",resizeNavBorder); RED.events.on("sidebar:resize",resizeNavBorder);
RED.actions.add("core:toggle-navigator",toggle);
var hideTimeout; var hideTimeout;
navContainer = $('<div>').css({ navContainer = $('<div>').css({
@ -141,23 +156,12 @@
$("#btn-navigate").click(function(evt) { $("#btn-navigate").click(function(evt) {
evt.preventDefault(); evt.preventDefault();
if (!isShowing) { toggle();
isShowing = true;
$("#btn-navigate").addClass("selected");
resizeNavBorder();
refreshNodes();
$("#chart").on("scroll",onScroll);
navContainer.fadeIn(200);
} else {
isShowing = false;
navContainer.fadeOut(100);
$("#chart").off("scroll",onScroll);
$("#btn-navigate").removeClass("selected");
}
}) })
}, },
refresh: refreshNodes, refresh: refreshNodes,
resize: resizeNavBorder resize: resizeNavBorder,
toggle: toggle
} }

View File

@ -61,7 +61,13 @@
} }
#user-settings-tab-view { #user-settings-tab-view {
position: absolute;
top:0;
right: 0;
left: 0;
bottom: 0;
padding: 8px 20px 20px; padding: 8px 20px 20px;
overflow-y: scroll;
} }
.user-settings-row { .user-settings-row {
padding: 5px 10px 2px; padding: 5px 10px 2px;

View File

@ -19,6 +19,19 @@ module.exports = function(RED) {
var mustache = require("mustache"); var mustache = require("mustache");
var yaml = require("js-yaml"); var yaml = require("js-yaml");
function extractTokens(tokens,set) {
set = set || new Set();
tokens.forEach(function(token) {
if (token[0] !== 'text') {
set.add(token[1]);
if (token.length > 4) {
extractTokens(token[4],set);
}
}
});
return set;
}
function parseContext(key) { function parseContext(key) {
var match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key); var match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key);
if (match) { if (match) {
@ -36,22 +49,16 @@ module.exports = function(RED) {
* flow and global context * flow and global context
*/ */
function NodeContext(msg, nodeContext, parent, escapeStrings, promises, results) { function NodeContext(msg, nodeContext, parent, escapeStrings, cachedContextTokens) {
this.msgContext = new mustache.Context(msg,parent); this.msgContext = new mustache.Context(msg,parent);
this.nodeContext = nodeContext; this.nodeContext = nodeContext;
this.escapeStrings = escapeStrings; this.escapeStrings = escapeStrings;
this.promises = promises; this.cachedContextTokens = cachedContextTokens;
this.results = results;
} }
NodeContext.prototype = new mustache.Context(); NodeContext.prototype = new mustache.Context();
NodeContext.prototype.lookup = function (name) { NodeContext.prototype.lookup = function (name) {
var results = this.results;
if (results) {
var val = results.shift();
return val;
}
// try message first: // try message first:
try { try {
var value = this.msgContext.lookup(name); var value = this.msgContext.lookup(name);
@ -64,7 +71,6 @@ module.exports = function(RED) {
value = value.replace(/\f/g, "\\f"); value = value.replace(/\f/g, "\\f");
value = value.replace(/[\b]/g, "\\b"); value = value.replace(/[\b]/g, "\\b");
} }
this.promises.push(Promise.resolve(value));
return value; return value;
} }
@ -76,36 +82,18 @@ module.exports = function(RED) {
var field = context.field; var field = context.field;
var target = this.nodeContext[type]; var target = this.nodeContext[type];
if (target) { if (target) {
var promise = new Promise((resolve, reject) => { return this.cachedContextTokens[name];
var callback = (err, val) => { }
if (err) {
reject(err);
} else {
resolve(val);
} }
};
target.get(field, store, callback);
});
this.promises.push(promise);
return ''; return '';
} }
else {
this.promises.push(Promise.resolve(''));
return '';
}
}
else {
this.promises.push(Promise.resolve(''));
return '';
}
}
catch(err) { catch(err) {
throw err; throw err;
} }
} }
NodeContext.prototype.push = function push (view) { NodeContext.prototype.push = function push (view) {
return new NodeContext(view, this.nodeContext, this.msgContext, undefined, this.promises, this.results); return new NodeContext(view, this.nodeContext, this.msgContext, undefined, this.cachedContextTokens);
}; };
function TemplateNode(n) { function TemplateNode(n) {
@ -118,8 +106,8 @@ module.exports = function(RED) {
this.outputFormat = n.output || "str"; this.outputFormat = n.output || "str";
var node = this; var node = this;
node.on("input", function(msg) {
function output(value) { function output(msg,value) {
/* istanbul ignore else */ /* istanbul ignore else */
if (node.outputFormat === "json") { if (node.outputFormat === "json") {
value = JSON.parse(value); value = JSON.parse(value);
@ -145,6 +133,9 @@ module.exports = function(RED) {
}); });
} }
} }
node.on("input", function(msg) {
try { try {
/*** /***
* Allow template contents to be defined externally * Allow template contents to be defined externally
@ -160,19 +151,44 @@ module.exports = function(RED) {
if (node.syntax === "mustache") { if (node.syntax === "mustache") {
var is_json = (node.outputFormat === "json"); var is_json = (node.outputFormat === "json");
var promises = []; var promises = [];
mustache.render(template, new NodeContext(msg, node.context(), null, is_json, promises, null)); var tokens = extractTokens(mustache.parse(template));
Promise.all(promises).then(function (values) { var resolvedTokens = {};
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, null, values)); tokens.forEach(function(name) {
output(value); var context = parseContext(name);
if (context) {
var type = context.type;
var store = context.store;
var field = context.field;
var target = node.context()[type];
if (target) {
var promise = new Promise((resolve, reject) => {
target.get(field, store, (err, val) => {
if (err) {
reject(err);
} else {
resolvedTokens[name] = val;
resolve();
}
});
});
promises.push(promise);
return;
}
}
});
Promise.all(promises).then(function() {
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, resolvedTokens));
output(msg, value);
}).catch(function (err) { }).catch(function (err) {
node.error(err.message); node.error(err.message,msg);
}); });
} else { } else {
output(template); output(msg, template);
} }
} }
catch(err) { catch(err) {
node.error(err.message); node.error(err.message, msg);
} }
}); });
} }

View File

@ -112,7 +112,15 @@ function load() {
try { try {
// Create a new instance of the plugin by calling its module function // Create a new instance of the plugin by calling its module function
stores[pluginName] = plugin(config); stores[pluginName] = plugin(config);
log.info(log._("context.log-store-init", {name:pluginName, info:"module="+plugins[pluginName].module})); 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}));
} catch(err) { } catch(err) {
return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()}))); return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()})));
} }

View File

@ -19,12 +19,16 @@
* *
* Configuration options: * Configuration options:
* { * {
* base: "contexts", // the base directory to use * base: "context", // the base directory to use
* // default: "contexts" * // default: "context"
* dir: "/path/to/storage", // the directory to create the base directory in * dir: "/path/to/storage", // the directory to create the base directory in
* // default: settings.userDir * // default: settings.userDir
* cache: true // whether to cache contents in memory * cache: true, // whether to cache contents in memory
* // default: true * // default: true
* flushInterval: 30 // if cache is enabled, the minimum interval
* // between writes to storage, in seconds. This
* can be used to reduce wear on underlying storage.
* default: 30 seconds
* } * }
* *
* *
@ -44,9 +48,12 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require("path"); var path = require("path");
var util = require("../../util"); var util = require("../../util");
var log = require("../../log");
var safeJSONStringify = require("json-stringify-safe");
var MemoryStore = require("./memory"); var MemoryStore = require("./memory");
function getStoragePath(storageBaseDir, scope) { function getStoragePath(storageBaseDir, scope) {
if(scope.indexOf(":") === -1){ if(scope.indexOf(":") === -1){
if(scope === "global"){ if(scope === "global"){
@ -61,7 +68,7 @@ function getStoragePath(storageBaseDir, scope) {
} }
function getBasePath(config) { function getBasePath(config) {
var base = config.base || "contexts"; var base = config.base || "context";
var storageBaseDir; var storageBaseDir;
if (!config.dir) { if (!config.dir) {
if(config.settings && config.settings.userDir){ if(config.settings && config.settings.userDir){
@ -102,12 +109,38 @@ function loadFile(storagePath){
}); });
} }
function listFiles(storagePath) {
var promises = [];
return fs.readdir(storagePath).then(function(files) {
files.forEach(function(file) {
promises.push(fs.readdir(path.join(storagePath,file)).then(function(subdirFiles) {
return subdirFiles.map(subfile => path.join(file,subfile));
}))
});
return Promise.all(promises);
}).then(dirs => dirs.reduce((acc, val) => acc.concat(val), []));
}
function stringify(value) {
var hasCircular;
var result = safeJSONStringify(value,null,4,function(k,v){hasCircular = true})
return { json: result, circular: hasCircular };
}
function LocalFileSystem(config){ function LocalFileSystem(config){
this.config = config; this.config = config;
this.storageBaseDir = getBasePath(this.config); this.storageBaseDir = getBasePath(this.config);
if (config.hasOwnProperty('cache')?config.cache:true) { if (config.hasOwnProperty('cache')?config.cache:true) {
this.cache = MemoryStore({}); this.cache = MemoryStore({});
} }
this.pendingWrites = {};
this.knownCircularRefs = {};
if (config.hasOwnProperty('flushInterval')) {
this.flushInterval = Math.max(0,config.flushInterval) * 1000;
} else {
this.flushInterval = 30000;
}
} }
LocalFileSystem.prototype.open = function(){ LocalFileSystem.prototype.open = function(){
@ -115,25 +148,17 @@ LocalFileSystem.prototype.open = function(){
if (this.cache) { if (this.cache) {
var scopes = []; var scopes = [];
var promises = []; var promises = [];
var subdirs = []; return listFiles(self.storageBaseDir).then(function(files) {
var subdirPromises = []; files.forEach(function(file) {
return fs.readdir(self.storageBaseDir).then(function(dirs){ var parts = file.split(path.sep);
dirs.forEach(function(fn) { if (parts[0] === 'global') {
var p = getStoragePath(self.storageBaseDir ,fn)+".json"; scopes.push("global");
scopes.push(fn); } else if (parts[1] === 'flow.json') {
promises.push(loadFile(p)); scopes.push(parts[0])
subdirs.push(path.join(self.storageBaseDir,fn)); } else {
subdirPromises.push(fs.readdir(path.join(self.storageBaseDir,fn))); scopes.push(parts[1].substring(0,parts[1].length-5)+":"+parts[0]);
})
return Promise.all(subdirPromises);
}).then(function(dirs) {
dirs.forEach(function(files,i) {
files.forEach(function(fn) {
if (fn !== 'flow.json' && fn !== 'global.json') {
scopes.push(fn.substring(0,fn.length-5)+":"+scopes[i]);
promises.push(loadFile(path.join(subdirs[i],fn)))
} }
}); promises.push(loadFile(path.join(self.storageBaseDir,file)));
}) })
return Promise.all(promises); return Promise.all(promises);
}).then(function(res) { }).then(function(res) {
@ -149,13 +174,40 @@ LocalFileSystem.prototype.open = function(){
}else{ }else{
return Promise.reject(err); return Promise.reject(err);
} }
}).then(function() {
self._flushPendingWrites = function() {
var scopes = Object.keys(self.pendingWrites);
self.pendingWrites = {};
var promises = [];
var newContext = self.cache._export();
scopes.forEach(function(scope) {
var storagePath = getStoragePath(self.storageBaseDir,scope);
var context = newContext[scope];
var stringifiedContext = stringify(context);
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
log.warn("Context "+scope+" contains a circular reference that cannot be persisted");
self.knownCircularRefs[scope] = true;
} else {
delete self.knownCircularRefs[scope];
}
log.debug("Flushing localfilesystem context scope "+scope);
promises.push(fs.outputFile(storagePath + ".json", stringifiedContext.json, "utf8"));
});
delete self._pendingWriteTimeout;
return Promise.all(promises);
}
}); });
} else { } else {
return Promise.resolve(); return fs.ensureDir(self.storageBaseDir);
} }
} }
LocalFileSystem.prototype.close = function(){ LocalFileSystem.prototype.close = function(){
if (this.cache && this._flushPendingWrites) {
clearTimeout(this._pendingWriteTimeout);
delete this._pendingWriteTimeout;
return this._flushPendingWrites();
}
return Promise.resolve(); return Promise.resolve();
} }
@ -188,13 +240,17 @@ LocalFileSystem.prototype.get = function(scope, key, callback) {
}; };
LocalFileSystem.prototype.set = function(scope, key, value, callback) { LocalFileSystem.prototype.set = function(scope, key, value, callback) {
var self = this;
var storagePath = getStoragePath(this.storageBaseDir ,scope); var storagePath = getStoragePath(this.storageBaseDir ,scope);
if (this.cache) { if (this.cache) {
this.cache.set(scope,key,value,callback); this.cache.set(scope,key,value,callback);
// With cache enabled, no need to re-read the file prior to writing. this.pendingWrites[scope] = true;
var newContext = this.cache._export()[scope]; if (this._pendingWriteTimeout) {
fs.outputFile(storagePath + ".json", JSON.stringify(newContext, undefined, 4), "utf8").catch(function(err) { // there's a pending write which will handle this
}); return;
} else {
this._pendingWriteTimeout = setTimeout(function() { self._flushPendingWrites.call(self)}, this.flushInterval);
}
} else if (callback && typeof callback !== 'function') { } else if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
} else { } else {
@ -214,7 +270,14 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) {
} }
util.setObjectProperty(obj,key[i],v); util.setObjectProperty(obj,key[i],v);
} }
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8"); var stringifiedContext = stringify(obj);
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
log.warn("Context "+scope+" contains a circular reference that cannot be persisted");
self.knownCircularRefs[scope] = true;
} else {
delete self.knownCircularRefs[scope];
}
return fs.outputFile(storagePath + ".json", stringifiedContext.json, "utf8");
}).then(function(){ }).then(function(){
if(typeof callback === "function"){ if(typeof callback === "function"){
callback(null); callback(null);
@ -254,36 +317,45 @@ LocalFileSystem.prototype.delete = function(scope){
cachePromise = Promise.resolve(); cachePromise = Promise.resolve();
} }
var that = this; var that = this;
delete this.pendingWrites[scope];
return cachePromise.then(function() { return cachePromise.then(function() {
var storagePath = getStoragePath(that.storageBaseDir,scope); var storagePath = getStoragePath(that.storageBaseDir,scope);
return fs.remove(storagePath + ".json"); return fs.remove(storagePath + ".json");
}); });
} }
LocalFileSystem.prototype.clean = function(activeNodes){ LocalFileSystem.prototype.clean = function(_activeNodes) {
var activeNodes = {};
_activeNodes.forEach(function(node) { activeNodes[node] = true });
var self = this; var self = this;
var cachePromise; var cachePromise;
if (this.cache) { if (this.cache) {
cachePromise = this.cache.clean(activeNodes); cachePromise = this.cache.clean(_activeNodes);
} else { } else {
cachePromise = Promise.resolve(); cachePromise = Promise.resolve();
} }
return cachePromise.then(function() { this.knownCircularRefs = {};
return fs.readdir(self.storageBaseDir).then(function(dirs){ return cachePromise.then(() => listFiles(self.storageBaseDir)).then(function(files) {
return Promise.all(dirs.reduce(function(result, item){ var promises = [];
if(item !== "global" && activeNodes.indexOf(item) === -1){ files.forEach(function(file) {
result.push(fs.remove(path.join(self.storageBaseDir,item))); var parts = file.split(path.sep);
var removePromise;
if (parts[0] === 'global') {
// never clean global
return;
} else if (!activeNodes[parts[0]]) {
// Flow removed - remove the whole dir
removePromise = fs.remove(path.join(self.storageBaseDir,parts[0]));
} else if (parts[1] !== 'flow.json' && !activeNodes[parts[1].substring(0,parts[1].length-5)]) {
// Node removed - remove the context file
removePromise = fs.remove(path.join(self.storageBaseDir,file));
} }
return result; if (removePromise) {
},[])); promises.push(removePromise);
}).catch(function(err){
if(err.code == 'ENOENT') {
return Promise.resolve();
}else{
return Promise.reject(err);
} }
}); });
}); return Promise.all(promises)
})
} }
module.exports = function(config){ module.exports = function(config){

View File

@ -213,6 +213,17 @@ module.exports = {
// j5board:require("johnny-five").Board({repl:false}) // j5board:require("johnny-five").Board({repl:false})
}, },
// Context Storage
// The following property can be used to enable context storage. The configuration
// provided here will enable file-based context that flushes to disk every 30 seconds.
// Refer to the documentation for further options: https://nodered.org/docs/api/context/
//
//contextStorage: {
// default: {
// module:"localfilesystem"
// },
//},
// The following property can be used to order the categories in the editor // The following property can be used to order the categories in the editor
// palette. If a node's category is not in the list, the category will get // palette. If a node's category is not in the list, the category will get
// added to the end of the palette. // added to the end of the palette.
@ -245,5 +256,5 @@ module.exports = {
// To enable the Projects feature, set this value to true // To enable the Projects feature, set this value to true
enabled: false enabled: false
} }
} },
} }

View File

@ -177,6 +177,53 @@ describe('template node', function() {
}); });
}); });
it('should handle nested context tags - property not set', function(done) {
// This comes from the Coursera Node-RED course and is a good example of
// multiple conditional tags
var template = `{{#flow.time}}time={{flow.time}}{{/flow.time}}{{^flow.time}}!time{{/flow.time}}{{#flow.random}}random={{flow.random}}randomtime={{flow.randomtime}}{{/flow.random}}{{^flow.random}}!random{{/flow.random}}`;
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:template,wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', '!time!random');
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo",topic: "bar"});
});
});
})
it('should handle nested context tags - property set', function(done) {
// This comes from the Coursera Node-RED course and is a good example of
// multiple conditional tags
var template = `{{#flow.time}}time={{flow.time}}{{/flow.time}}{{^flow.time}}!time{{/flow.time}}{{#flow.random}}random={{flow.random}}randomtime={{flow.randomtime}}{{/flow.random}}{{^flow.random}}!random{{/flow.random}}`;
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:template,wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'time=123random=456randomtime=789');
done();
} catch(err) {
done(err);
}
});
n1.context().flow.set(["time","random","randomtime"],["123","456","789"],function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
})
it('should modify payload from two persistable flow context', function(done) { it('should modify payload from two persistable flow context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{flow[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}]; var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{flow[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() { helper.load(templateNode, flow, function() {

View File

@ -17,6 +17,7 @@
var should = require("should"); var should = require("should");
var sinon = require('sinon'); var sinon = require('sinon');
var path = require("path"); var path = require("path");
var fs = require('fs-extra');
var Context = require("../../../../../red/runtime/nodes/context/index"); var Context = require("../../../../../red/runtime/nodes/context/index");
describe('context', function() { describe('context', function() {
@ -273,6 +274,8 @@ describe('context', function() {
sandbox.reset(); sandbox.reset();
return Context.clean({allNodes:{}}).then(function(){ return Context.clean({allNodes:{}}).then(function(){
return Context.close(); return Context.close();
}).then(function(){
return fs.remove(resourcesDir);
}); });
}); });
@ -286,11 +289,11 @@ describe('context', function() {
}); });
it('should load memory module', function() { it('should load memory module', function() {
Context.init({contextStorage:{memory:{module:"memory"}}}); Context.init({contextStorage:{memory:{module:"memory"}}});
Context.load(); return Context.load();
}); });
it('should load localfilesystem module', function() { it('should load localfilesystem module', function() {
Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}}); Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}});
Context.load(); return Context.load();
}); });
it('should ignore reserved storage name `_`', function(done) { it('should ignore reserved storage name `_`', function(done) {
Context.init({contextStorage:{_:{module:testPlugin}}}); Context.init({contextStorage:{_:{module:testPlugin}}});

View File

@ -21,13 +21,16 @@ var LocalFileSystem = require('../../../../../red/runtime/nodes/context/localfil
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context")); var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
var defaultContextBase = "context";
describe('localfilesystem',function() { describe('localfilesystem',function() {
var context;
before(function() { before(function() {
return fs.remove(resourcesDir); return fs.remove(resourcesDir);
}); });
describe('#get/set',function() {
var context;
beforeEach(function() { beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir, cache: false}); context = LocalFileSystem({dir: resourcesDir, cache: false});
return context.open(); return context.open();
@ -35,18 +38,20 @@ describe('localfilesystem',function() {
afterEach(function() { afterEach(function() {
return context.clean([]).then(function(){ return context.clean([]).then(function(){
return context.close().then(function(){ return context.close();
}).then(function(){
return fs.remove(resourcesDir); return fs.remove(resourcesDir);
}); });
}); });
});
describe('#get/set',function() {
it('should store property',function(done) { it('should store property',function(done) {
context.get("nodeX","foo",function(err, value){ context.get("nodeX","foo",function(err, value){
if (err) { return done(err); }
should.not.exist(value); should.not.exist(value);
context.set("nodeX","foo","test",function(err){ context.set("nodeX","foo","test",function(err){
if (err) { return done(err); }
context.get("nodeX","foo",function(err, value){ context.get("nodeX","foo",function(err, value){
if (err) { return done(err); }
value.should.be.equal("test"); value.should.be.equal("test");
done(); done();
}); });
@ -348,7 +353,7 @@ describe('localfilesystem',function() {
}); });
it('should handle empty context file', function (done) { it('should handle empty context file', function (done) {
fs.outputFile(path.join(resourcesDir,"contexts","nodeX","flow.json"),"",function(){ fs.outputFile(path.join(resourcesDir,defaultContextBase,"nodeX","flow.json"),"",function(){
context.get("nodeX", "foo", function (err, value) { context.get("nodeX", "foo", function (err, value) {
should.not.exist(value); should.not.exist(value);
context.set("nodeX", "foo", "test", function (err) { context.set("nodeX", "foo", "test", function (err) {
@ -362,7 +367,7 @@ describe('localfilesystem',function() {
}); });
it('should throw an error when reading corrupt context file', function (done) { it('should throw an error when reading corrupt context file', function (done) {
fs.outputFile(path.join(resourcesDir, "contexts", "nodeX", "flow.json"),"{abc",function(){ fs.outputFile(path.join(resourcesDir, defaultContextBase, "nodeX", "flow.json"),"{abc",function(){
context.get("nodeX", "foo", function (err, value) { context.get("nodeX", "foo", function (err, value) {
should.exist(err); should.exist(err);
done(); done();
@ -372,6 +377,20 @@ describe('localfilesystem',function() {
}); });
describe('#keys',function() { describe('#keys',function() {
var context;
beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir, cache: false});
return context.open();
});
afterEach(function() {
return context.clean([]).then(function(){
return context.close();
}).then(function(){
return fs.remove(resourcesDir);
});
});
it('should enumerate context keys', function(done) { it('should enumerate context keys', function(done) {
context.keys("nodeX",function(err, value){ context.keys("nodeX",function(err, value){
value.should.be.an.Array(); value.should.be.an.Array();
@ -436,6 +455,20 @@ describe('localfilesystem',function() {
}); });
describe('#delete',function() { describe('#delete',function() {
var context;
beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir, cache: false});
return context.open();
});
afterEach(function() {
return context.clean([]).then(function(){
return context.close();
}).then(function(){
return fs.remove(resourcesDir);
});
});
it('should delete context',function(done) { it('should delete context',function(done) {
context.get("nodeX","foo",function(err, value){ context.get("nodeX","foo",function(err, value){
should.not.exist(value); should.not.exist(value);
@ -466,64 +499,36 @@ describe('localfilesystem',function() {
}); });
describe('#clean',function() { describe('#clean',function() {
it('should clean unnecessary context',function(done) { var context;
context.get("nodeX","foo",function(err, value){ var contextGet;
should.not.exist(value); var contextSet;
context.get("nodeY","foo",function(err, value){ beforeEach(function() {
should.not.exist(value); context = LocalFileSystem({dir: resourcesDir, cache: false});
context.set("nodeX","foo","testX",function(err){ contextGet = function(scope,key) {
context.set("nodeY","foo","testY",function(err){ return new Promise((res,rej) => {
context.get("nodeX","foo",function(err, value){ context.get(scope,key, function(err,value) {
value.should.be.equal("testX"); if (err) {
context.get("nodeY","foo",function(err, value){ rej(err);
value.should.be.equal("testY"); } else {
context.clean([]).then(function(){ res(value);
context.get("nodeX","foo",function(err, value){ }
should.not.exist(value); })
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
done();
});
});
});
});
});
});
});
}); });
}
contextSet = function(scope,key,value) {
return new Promise((res,rej) => {
context.set(scope,key,value, function(err) {
if (err) {
rej(err);
} else {
res();
}
})
}); });
}
return context.open();
}); });
it('should not clean active context',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","testX",function(err){
context.set("nodeY","foo","testY",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
context.clean(["nodeX"]).then(function(){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
done();
});
});
});
});
});
});
});
});
});
});
});
describe('#if cache is enabled',function() {
afterEach(function() { afterEach(function() {
return context.clean([]).then(function(){ return context.clean([]).then(function(){
return context.close().then(function(){ return context.close().then(function(){
@ -531,23 +536,101 @@ describe('localfilesystem',function() {
}); });
}); });
}); });
it('should clean unnecessary context',function(done) {
contextSet("global","foo","testGlobal").then(function() {
return contextSet("nodeX:flow1","foo","testX");
}).then(function() {
return contextSet("nodeY:flow2","foo","testY");
}).then(function() {
return contextGet("nodeX:flow1","foo");
}).then(function(value) {
value.should.be.equal("testX");
}).then(function() {
return contextGet("nodeY:flow2","foo");
}).then(function(value) {
value.should.be.equal("testY");
}).then(function() {
return context.clean([])
}).then(function() {
return contextGet("nodeX:flow1","foo");
}).then(function(value) {
should.not.exist(value);
}).then(function() {
return contextGet("nodeY:flow2","foo");
}).then(function(value) {
should.not.exist(value);
}).then(function() {
return contextGet("global","foo");
}).then(function(value) {
value.should.eql("testGlobal");
}).then(done).catch(done);
});
it('should not clean active context',function(done) {
contextSet("global","foo","testGlobal").then(function() {
return contextSet("nodeX:flow1","foo","testX");
}).then(function() {
return contextSet("nodeY:flow2","foo","testY");
}).then(function() {
return contextGet("nodeX:flow1","foo");
}).then(function(value) {
value.should.be.equal("testX");
}).then(function() {
return contextGet("nodeY:flow2","foo");
}).then(function(value) {
value.should.be.equal("testY");
}).then(function() {
return context.clean(["flow1","nodeX"])
}).then(function() {
return contextGet("nodeX:flow1","foo");
}).then(function(value) {
value.should.be.equal("testX");
}).then(function() {
return contextGet("nodeY:flow2","foo");
}).then(function(value) {
should.not.exist(value);
}).then(function() {
return contextGet("global","foo");
}).then(function(value) {
value.should.eql("testGlobal");
}).then(done).catch(done);
});
});
describe('#if cache is enabled',function() {
var context;
beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir, cache: false});
return context.open();
});
afterEach(function() {
return context.clean([]).then(function(){
return context.close();
}).then(function(){
return fs.remove(resourcesDir);
});
});
it('should load contexts into the cache',function() { it('should load contexts into the cache',function() {
var globalData = {key:"global"}; var globalData = {key:"global"};
var flowData = {key:"flow"}; var flowData = {key:"flow"};
var nodeData = {key:"node"}; var nodeData = {key:"node"};
return Promise.all([ return Promise.all([
fs.outputFile(path.join(resourcesDir,"contexts","global","global.json"), JSON.stringify(globalData,null,4), "utf8"), fs.outputFile(path.join(resourcesDir,defaultContextBase,"global","global.json"), JSON.stringify(globalData,null,4), "utf8"),
fs.outputFile(path.join(resourcesDir,"contexts","flow","flow.json"), JSON.stringify(flowData,null,4), "utf8"), fs.outputFile(path.join(resourcesDir,defaultContextBase,"flow","flow.json"), JSON.stringify(flowData,null,4), "utf8"),
fs.outputFile(path.join(resourcesDir,"contexts","flow","node.json"), JSON.stringify(nodeData,null,4), "utf8") fs.outputFile(path.join(resourcesDir,defaultContextBase,"flow","node.json"), JSON.stringify(nodeData,null,4), "utf8")
]).then(function(){ ]).then(function(){
context = LocalFileSystem({dir: resourcesDir, cache: true}); context = LocalFileSystem({dir: resourcesDir, cache: true});
return context.open(); return context.open();
}).then(function(){ }).then(function(){
return Promise.all([ return Promise.all([
fs.remove(path.join(resourcesDir,"contexts","global","global.json")), fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json")),
fs.remove(path.join(resourcesDir,"contexts","flow","flow.json")), fs.remove(path.join(resourcesDir,defaultContextBase,"flow","flow.json")),
fs.remove(path.join(resourcesDir,"contexts","flow","node.json")) fs.remove(path.join(resourcesDir,defaultContextBase,"flow","node.json"))
]); ]);
}).then(function(){ }).then(function(){
context.get("global","key").should.be.equal("global"); context.get("global","key").should.be.equal("global");
@ -557,19 +640,31 @@ describe('localfilesystem',function() {
}); });
it('should store property to the cache',function() { it('should store property to the cache',function() {
context = LocalFileSystem({dir: resourcesDir, cache: true}); context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 1});
return context.open().then(function(){ return context.open().then(function(){
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
context.set("global","foo","bar",function(err){ context.set("global","foo","bar",function(err){
if(err){ if(err){
reject(err); reject(err);
} else { } else {
fs.readJson(path.join(resourcesDir,defaultContextBase,"global","global.json")).then(function(data) {
// File should not exist as flush hasn't happened
reject("File global/global.json should not exist");
}).catch(function(err) {
setTimeout(function() {
fs.readJson(path.join(resourcesDir,defaultContextBase,"global","global.json")).then(function(data) {
data.should.eql({foo:'bar'});
resolve(); resolve();
}).catch(function(err) {
reject(err);
});
},1100)
})
} }
}); });
}); });
}).then(function(){ }).then(function(){
return fs.remove(path.join(resourcesDir,"contexts","global","global.json")); return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
}).then(function(){ }).then(function(){
context.get("global","foo").should.be.equal("bar"); context.get("global","foo").should.be.equal("bar");
}) })
@ -577,11 +672,11 @@ describe('localfilesystem',function() {
it('should enumerate context keys in the cache',function() { it('should enumerate context keys in the cache',function() {
var globalData = {foo:"bar"}; var globalData = {foo:"bar"};
fs.outputFile(path.join(resourcesDir,"contexts","global","global.json"), JSON.stringify(globalData,null,4), "utf8").then(function(){ fs.outputFile(path.join(resourcesDir,defaultContextBase,"global","global.json"), JSON.stringify(globalData,null,4), "utf8").then(function(){
context = LocalFileSystem({dir: resourcesDir, cache: true}); context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
return context.open() return context.open()
}).then(function(){ }).then(function(){
return fs.remove(path.join(resourcesDir,"contexts","global","global.json")); return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
}).then(function(){ }).then(function(){
var keys = context.keys("global"); var keys = context.keys("global");
keys.should.have.length(1); keys.should.have.length(1);
@ -596,7 +691,7 @@ describe('localfilesystem',function() {
}); });
}); });
}).then(function(){ }).then(function(){
return fs.remove(path.join(resourcesDir,"contexts","global","global.json")); return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
}).then(function(){ }).then(function(){
var keys = context.keys("global"); var keys = context.keys("global");
keys.should.have.length(2); keys.should.have.length(2);
@ -605,7 +700,7 @@ describe('localfilesystem',function() {
}); });
it('should delete context in the cache',function() { it('should delete context in the cache',function() {
context = LocalFileSystem({dir: resourcesDir, cache: true}); context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
return context.open().then(function(){ return context.open().then(function(){
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
context.set("global","foo","bar",function(err){ context.set("global","foo","bar",function(err){
@ -628,10 +723,10 @@ describe('localfilesystem',function() {
var flowAData = {key:"flowA"}; var flowAData = {key:"flowA"};
var flowBData = {key:"flowB"}; var flowBData = {key:"flowB"};
return Promise.all([ return Promise.all([
fs.outputFile(path.join(resourcesDir,"contexts","flowA","flow.json"), JSON.stringify(flowAData,null,4), "utf8"), fs.outputFile(path.join(resourcesDir,defaultContextBase,"flowA","flow.json"), JSON.stringify(flowAData,null,4), "utf8"),
fs.outputFile(path.join(resourcesDir,"contexts","flowB","flow.json"), JSON.stringify(flowBData,null,4), "utf8") fs.outputFile(path.join(resourcesDir,defaultContextBase,"flowB","flow.json"), JSON.stringify(flowBData,null,4), "utf8")
]).then(function(){ ]).then(function(){
context = LocalFileSystem({dir: resourcesDir, cache: true}); context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
return context.open(); return context.open();
}).then(function(){ }).then(function(){
context.get("flowA","key").should.be.equal("flowA"); context.get("flowA","key").should.be.equal("flowA");
@ -645,6 +740,19 @@ describe('localfilesystem',function() {
}); });
describe('Configuration', function () { describe('Configuration', function () {
var context;
beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir, cache: false});
return context.open();
});
afterEach(function() {
return context.clean([]).then(function(){
return context.close();
}).then(function(){
return fs.remove(resourcesDir);
});
});
it('should change a base directory', function (done) { it('should change a base directory', function (done) {
var differentBaseContext = LocalFileSystem({ var differentBaseContext = LocalFileSystem({
base: "contexts2", base: "contexts2",
@ -688,7 +796,7 @@ describe('localfilesystem',function() {
it('should use NODE_RED_HOME', function (done) { it('should use NODE_RED_HOME', function (done) {
var oldNRH = process.env.NODE_RED_HOME; var oldNRH = process.env.NODE_RED_HOME;
process.env.NODE_RED_HOME = resourcesDir; process.env.NODE_RED_HOME = resourcesDir;
fs.mkdirSync(resourcesDir); fs.ensureDirSync(resourcesDir);
fs.writeFileSync(path.join(resourcesDir,".config.json"),""); fs.writeFileSync(path.join(resourcesDir,".config.json"),"");
var nrHomeContext = LocalFileSystem({ var nrHomeContext = LocalFileSystem({
base: "contexts2", base: "contexts2",