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

Split up nodes.js into components

This commit is contained in:
Nick O'Leary 2014-05-03 22:26:35 +01:00
parent df9744084c
commit 7eed375111
14 changed files with 820 additions and 556 deletions

View File

@ -149,7 +149,7 @@ DebugNode.logHandler.on("log",function(msg) {
DebugNode.send(msg); DebugNode.send(msg);
} }
}); });
RED.nodes.addLogHandler(DebugNode.logHandler); RED.log.addHandler(DebugNode.logHandler);
RED.httpAdmin.post("/debug/:id/:state", function(req,res) { RED.httpAdmin.post("/debug/:id/:state", function(req,res) {
var node = RED.nodes.getNode(req.params.id); var node = RED.nodes.getNode(req.params.id);

View File

@ -45,7 +45,8 @@
"grunt-cli": "0.1.13", "grunt-cli": "0.1.13",
"grunt-simple-mocha": "0.4.0", "grunt-simple-mocha": "0.4.0",
"mocha": "1.18.2", "mocha": "1.18.2",
"should": "3.3.1" "should": "3.3.1",
"sinon": "1.9.1"
}, },
"engines": { "engines": {
"node": ">=0.8" "node": ">=0.8"

39
red/log.js Normal file
View File

@ -0,0 +1,39 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var logHandlers = [];
var ConsoleLogHandler = new EventEmitter();
ConsoleLogHandler.on("log",function(msg) {
util.log("["+msg.level+"] ["+msg.type+":"+(msg.name||msg.id)+"] "+msg.msg);
});
var log = module.exports = {
addHandler: function(func) {
},
log: function(msg) {
for (var i in logHandlers) {
logHandlers[i].emit("log",msg);
}
}
}
log.addHandler(ConsoleLogHandler);

View File

@ -1,536 +0,0 @@
/**
* Copyright 2013 IBM Corp.
*
* 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.
**/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var fs = require("fs");
var path = require("path");
var clone = require("clone");
var when = require("when");
var whenNode = require('when/node');
var events = require("./events");
var storage = null;
var settings = null;
var registry = (function() {
var nodes = {};
var logHandlers = [];
var obj = {
add: function(n) {
nodes[n.id] = n;
n.on("log",function(msg) {
for (var i in logHandlers) {
logHandlers[i].emit("log",msg);
}
});
},
get: function(i) {
return nodes[i];
},
clear: function() {
events.emit("nodes-stopping");
for (var n in nodes) {
nodes[n].close();
}
events.emit("nodes-stopped");
nodes = {};
},
each: function(cb) {
for (var n in nodes) {
cb(nodes[n]);
}
},
addLogHandler: function(handler) {
logHandlers.push(handler);
}
}
return obj;
})();
var ConsoleLogHandler = new EventEmitter();
ConsoleLogHandler.on("log",function(msg) {
util.log("["+msg.level+"] ["+msg.type+":"+(msg.name||msg.id)+"] "+msg.msg);
});
registry.addLogHandler(ConsoleLogHandler);
var node_type_registry = (function() {
var node_types = {};
var node_configs = [];
var obj = {
register: function(type,node) {
util.inherits(node, Node);
node_types[type] = node;
events.emit("type-registered",type);
},
registerConfig: function(config) {
node_configs.push(config);
},
get: function(type) {
return node_types[type];
},
getNodeConfigs: function() {
var result = "";
for (var i=0;i<node_configs.length;i++) {
result += node_configs[i];
}
return result;
},
count: function() {
return Object.keys(node_types).length;
}
}
return obj;
})();
function Node(n) {
this.id = n.id;
registry.add(this);
this.type = n.type;
if (n.name) {
this.name = n.name;
}
this.wires = n.wires||[];
}
util.inherits(Node,EventEmitter);
Node.prototype.close = function() {
// called when a node is removed
this.emit("close");
}
Node.prototype.send = function(msg) {
// instanceof doesn't work for some reason here
if (msg == null) {
msg = [];
} else if (!util.isArray(msg)) {
msg = [msg];
}
for (var i in this.wires) {
var wires = this.wires[i];
if (i < msg.length) {
if (msg[i] != null) {
var msgs = msg[i];
if (!util.isArray(msg[i])) {
msgs = [msg[i]];
}
//if (wires.length == 1) {
// // Single recipient, don't need to clone the message
// var node = registry.get(wires[0]);
// if (node) {
// for (var k in msgs) {
// var mm = msgs[k];
// node.receive(mm);
// }
// }
//} else {
// Multiple recipients, must send message copies
for (var j in wires) {
var node = registry.get(wires[j]);
if (node) {
for (var k in msgs) {
var mm = msgs[k];
// Temporary fix for #97
// TODO: remove this http-node-specific fix somehow
var req = mm.req;
var res = mm.res;
delete mm.req;
delete mm.res;
var m = clone(mm);
if (req) {
m.req = req;
mm.req = req;
}
if (res) {
m.res = res;
mm.res = res;
}
node.receive(m);
}
}
}
//}
}
}
}
}
Node.prototype.receive = function(msg) {
this.emit("input",msg);
}
Node.prototype.log = function(msg) {
var o = {level:'log',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.warn = function(msg) {
var o = {level:'warn',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.error = function(msg) {
var o = {level:'error',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
var credentials = {};
function addCredentials(id,creds) {
credentials[id] = creds;
if (!storage) {
// Do this lazily to ensure the storage provider as been initialised
storage = require("./storage");
}
storage.saveCredentials(credentials);
}
function getCredentials(id) {
return credentials[id];
}
function deleteCredentials(id) {
delete credentials[id];
storage.saveCredentials(credentials);
}
function createNode(node,def) {
Node.call(node,def);
}
function load(_settings) {
return when.promise(function(resolve,reject) {
settings = _settings;
var RED = require("./red.js");
function loadTemplate(templateFilename) {
return when.promise(function(resolve,reject) {
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
node_type_registry.registerConfig(content);
resolve();
}, function(err) {
reject("missing template file");
});
});
}
function loadNode(nodeDir, nodeFn) {
return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == nodeFn) {
resolve();
return;
}
}
}
var nodeFilename = path.join(nodeDir,nodeFn);
var templateFilename = nodeFilename.replace(/\.js$/,".html");
var r = require(nodeFilename);
if (typeof r === "function") {
try {
var promise = r(RED);
if (promise != null && typeof promise.then === "function") {
promise.then(function() {
resolve(loadTemplate(templateFilename));
},function(err) {
reject(err);
});
} else {
resolve(loadTemplate(templateFilename));
}
} catch(err) {
reject(err);
}
} else {
resolve(loadTemplate(templateFilename));
}
});
}
function loadNodesFromModule(moduleDir,pkg) {
var nodes = pkg['node-red'].nodes||{};
var promises = [];
for (var n in nodes) {
promises.push(when.promise(function(resolve) {
loadNode(moduleDir,nodes[n]).then(resolve, function(err) {
resolve({'fn':pkg.name+":"+n,err:err});
});
}));
}
return when.promise(function(resolve,reject) {
var errors = [];
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
}
function scanForNodes(dir) {
return when.promise(function(resolve,reject) {
var pm = path.join(dir,"node_modules");
var promises = [];
promises.push(when.promise(function(resolve,reject) {
whenNode.call(fs.readdir,pm).then(function(files) {
var promises = [];
files.forEach(function(fn) {
var pkgfn = path.join(pm,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
promises.push(loadNodesFromModule(moduleDir,pkg));
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
}
}
});
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
},function(err) {
resolve([]);
})
}));
var up = path.resolve(path.join(dir,".."));
if (up !== dir) {
promises.push(scanForNodes(up))
}
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
}
function loadNodes(dir) {
return when.promise(function(resolve,reject) {
var promises = [];
whenNode.call(fs.readdir,dir).done(function(files) {
files = files.sort();
files.forEach(function(fn) {
var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) {
loadNode(dir,fn).then(resolve, function(err) {
resolve({'fn':fn,err:err});
});
}));
}
} else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) {
loadNodes(path.join(dir,fn)).then(function(errs) {
resolve(errs);
});
}));
} else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn));
}
}
});
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
}, function(err) {
resolve([]);
// non-existant dir
});
});
}
var promises = [];
promises.push(loadNodes(__dirname+"/../nodes"));
promises.push(scanForNodes(__dirname+"/../nodes"));
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) {
promises.push(loadNodes(dir[i]));
}
}
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
}
var activeConfig = [];
var missingTypes = [];
events.on('type-registered',function(type) {
if (missingTypes.length > 0) {
var i = missingTypes.indexOf(type);
if (i != -1) {
missingTypes.splice(i,1);
util.log("[red] Missing type registered: "+type);
}
if (missingTypes.length == 0) {
parseConfig();
}
}
});
function getNode(nid) {
return registry.get(nid);
}
function stopFlows() {
if (activeConfig&&activeConfig.length > 0) {
util.log("[red] Stopping flows");
}
registry.clear();
}
function setConfig(conf) {
stopFlows();
activeConfig = conf;
if (!storage) {
// Do this lazily to ensure the storage provider as been initialised
storage = require("./storage");
}
storage.getCredentials().then(function(creds) {
credentials = creds;
parseConfig();
}).otherwise(function(err) {
util.log("[red] Error loading credentials : "+err);
});
}
function getConfig() {
return activeConfig;
}
var parseConfig = function() {
missingTypes = [];
for (var i in activeConfig) {
var type = activeConfig[i].type;
// TODO: remove workspace in next release+1
if (type != "workspace" && type != "tab") {
var nt = node_type_registry.get(type);
if (!nt && missingTypes.indexOf(type) == -1) {
missingTypes.push(type);
}
}
};
if (missingTypes.length > 0) {
util.log("[red] Waiting for missing types to be registered:");
for (var i in missingTypes) {
util.log("[red] - "+missingTypes[i]);
}
return;
}
util.log("[red] Starting flows");
events.emit("nodes-starting");
for (var i in activeConfig) {
var nn = null;
// TODO: remove workspace in next release+1
if (activeConfig[i].type != "workspace" && activeConfig[i].type != "tab") {
var nt = node_type_registry.get(activeConfig[i].type);
if (nt) {
try {
nn = new nt(activeConfig[i]);
}
catch (err) {
util.log("[red] "+activeConfig[i].type+" : "+err);
}
}
// console.log(nn);
if (nn == null) {
util.log("[red] unknown type: "+activeConfig[i].type);
}
}
}
// Clean up any orphaned credentials
var deletedCredentials = false;
for (var c in credentials) {
var n = registry.get(c);
if (!n) {
deletedCredentials = true;
delete credentials[c];
}
}
if (deletedCredentials) {
storage.saveCredentials(credentials);
}
events.emit("nodes-started");
}
module.exports = {
Node:Node,
addCredentials: addCredentials,
getCredentials: getCredentials,
deleteCredentials: deleteCredentials,
createNode: createNode,
registerType: node_type_registry.register,
getType: node_type_registry.get,
getNodeConfigs: node_type_registry.getNodeConfigs,
addLogHandler: registry.addLogHandler,
load: load,
getNode: getNode,
stopFlows: stopFlows,
setConfig: setConfig,
getConfig: getConfig
}

119
red/nodes/Node.js Normal file
View File

@ -0,0 +1,119 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var clone = require("clone");
var flows = require("./flows");
function Node(n) {
this.id = n.id;
flows.add(this);
this.type = n.type;
if (n.name) {
this.name = n.name;
}
this.wires = n.wires||[];
}
util.inherits(Node,EventEmitter);
Node.prototype.close = function() {
// called when a node is removed
this.emit("close");
}
Node.prototype.send = function(msg) {
// instanceof doesn't work for some reason here
if (msg == null) {
msg = [];
} else if (!util.isArray(msg)) {
msg = [msg];
}
for (var i in this.wires) {
var wires = this.wires[i];
if (i < msg.length) {
if (msg[i] != null) {
var msgs = msg[i];
if (!util.isArray(msg[i])) {
msgs = [msg[i]];
}
//if (wires.length == 1) {
// // Single recipient, don't need to clone the message
// var node = flows.get(wires[0]);
// if (node) {
// for (var k in msgs) {
// var mm = msgs[k];
// node.receive(mm);
// }
// }
//} else {
// Multiple recipients, must send message copies
for (var j in wires) {
var node = flows.get(wires[j]);
if (node) {
for (var k in msgs) {
var mm = msgs[k];
// Temporary fix for #97
// TODO: remove this http-node-specific fix somehow
var req = mm.req;
var res = mm.res;
delete mm.req;
delete mm.res;
var m = clone(mm);
if (req) {
m.req = req;
mm.req = req;
}
if (res) {
m.res = res;
mm.res = res;
}
node.receive(m);
}
}
}
//}
}
}
}
}
Node.prototype.receive = function(msg) {
this.emit("input",msg);
}
Node.prototype.log = function(msg) {
var o = {level:'log',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.warn = function(msg) {
var o = {level:'warn',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.error = function(msg) {
var o = {level:'error',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
module.exports = Node;

62
red/nodes/credentials.js Normal file
View File

@ -0,0 +1,62 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var util = require("util");
var credentials = {};
var storage = null;
module.exports = {
init: function(_storage) {
storage = _storage;
},
load: function() {
return storage.getCredentials().then(function(creds) {
credentials = creds;
}).otherwise(function(err) {
util.log("[red] Error loading credentials : "+err);
});
},
add: function(id,creds) {
credentials[id] = creds;
storage.saveCredentials(credentials);
},
get: function(id) {
return credentials[id];
},
delete: function(id) {
delete credentials[id];
storage.saveCredentials(credentials);
},
clean: function(getNode) {
var deletedCredentials = false;
for (var c in credentials) {
var n = getNode(c);
console.log(c,n)
if (!n) {
deletedCredentials = true;
delete credentials[c];
}
}
if (deletedCredentials) {
storage.saveCredentials(credentials);
}
}
}

150
red/nodes/flows.js Normal file
View File

@ -0,0 +1,150 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var util = require("util");
var when = require("when");
var typeRegistry = require("./registry");
var credentials = require("./credentials");
var log = require("../log");
var events = require("../events");
var storage = null;
var nodes = {};
var activeConfig = [];
var missingTypes = [];
events.on('type-registered',function(type) {
if (missingTypes.length > 0) {
var i = missingTypes.indexOf(type);
if (i != -1) {
missingTypes.splice(i,1);
util.log("[red] Missing type registered: "+type);
}
if (missingTypes.length == 0) {
parseConfig();
}
}
});
var parseConfig = function() {
missingTypes = [];
for (var i in activeConfig) {
var type = activeConfig[i].type;
// TODO: remove workspace in next release+1
if (type != "workspace" && type != "tab") {
var nt = typeRegistry.get(type);
if (!nt && missingTypes.indexOf(type) == -1) {
missingTypes.push(type);
}
}
};
if (missingTypes.length > 0) {
util.log("[red] Waiting for missing types to be registered:");
for (var i in missingTypes) {
util.log("[red] - "+missingTypes[i]);
}
return;
}
util.log("[red] Starting flows");
events.emit("nodes-starting");
for (var i in activeConfig) {
var nn = null;
// TODO: remove workspace in next release+1
if (activeConfig[i].type != "workspace" && activeConfig[i].type != "tab") {
var nt = typeRegistry.get(activeConfig[i].type);
if (nt) {
try {
nn = new nt(activeConfig[i]);
}
catch (err) {
util.log("[red] "+activeConfig[i].type+" : "+err);
}
}
// console.log(nn);
if (nn == null) {
util.log("[red] unknown type: "+activeConfig[i].type);
}
}
}
// Clean up any orphaned credentials
credentials.clean(flowNodes.get);
events.emit("nodes-started");
}
function stopFlows() {
if (activeConfig&&activeConfig.length > 0) {
util.log("[red] Stopping flows");
}
flowNodes.clear();
}
var flowNodes = module.exports = {
init: function(_storage) {
storage = _storage;
},
load: function() {
return storage.getFlows().then(function(flows) {
return credentials.load().then(function() {
activeConfig = flows;
if (activeConfig && activeConfig.length > 0) {
parseConfig();
}
});
}).otherwise(function(err) {
util.log("[red] Error loading flows : "+err);
});
},
add: function(n) {
nodes[n.id] = n;
n.on("log",log.log);
},
get: function(i) {
return nodes[i];
},
clear: function() {
events.emit("nodes-stopping");
for (var n in nodes) {
nodes[n].close();
}
events.emit("nodes-stopped");
nodes = {};
},
each: function(cb) {
for (var n in nodes) {
cb(nodes[n]);
}
},
addLogHandler: function(handler) {
logHandlers.push(handler);
},
getFlows: function() {
return activeConfig;
},
setFlows: function(conf) {
return storage.saveFlows(conf).then(function() {
stopFlows();
activeConfig = conf;
parseConfig();
})
},
stopFlows: stopFlows
}

50
red/nodes/index.js Normal file
View File

@ -0,0 +1,50 @@
/**
* Copyright 2013, 2014 IBM Corp.
*
* 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.
**/
var registry = require("./registry");
var credentials = require("./credentials");
var flows = require("./flows");
var Node = require("./Node");
function createNode(node,def) {
Node.call(node,def);
}
function init(_settings,storage) {
credentials.init(storage);
flows.init(storage);
registry.init(_settings);
}
module.exports = {
init: init,
load: registry.load,
addCredentials: credentials.add,
getCredentials: credentials.get,
deleteCredentials: credentials.delete,
createNode: createNode,
registerType: registry.registerType,
getType: registry.get,
getNodeConfigs: registry.getNodeConfigs,
getNode: flows.get,
loadFlows: flows.load,
stopFlows: flows.stopFlows,
setFlows: flows.setFlows,
getFlows: flows.getFlows
}

257
red/nodes/registry.js Normal file
View File

@ -0,0 +1,257 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var util = require("util");
var when = require("when");
var whenNode = require('when/node');
var fs = require("fs");
var path = require("path");
var events = require("../events");
var Node;
var settings;
var node_types = {};
var node_configs = [];
function loadTemplate(templateFilename) {
return when.promise(function(resolve,reject) {
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
typeRegistry.registerConfig(content);
resolve();
}, function(err) {
reject("missing template file");
});
});
}
function loadNode(nodeDir, nodeFn) {
return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == nodeFn) {
resolve();
return;
}
}
}
var nodeFilename = path.join(nodeDir,nodeFn);
var templateFilename = nodeFilename.replace(/\.js$/,".html");
var r = require(nodeFilename);
if (typeof r === "function") {
try {
var promise = r(RED);
if (promise != null && typeof promise.then === "function") {
promise.then(function() {
resolve(loadTemplate(templateFilename));
},function(err) {
reject(err);
});
} else {
resolve(loadTemplate(templateFilename));
}
} catch(err) {
reject(err);
}
} else {
resolve(loadTemplate(templateFilename));
}
});
}
function loadNodesFromModule(moduleDir,pkg) {
var nodes = pkg['node-red'].nodes||{};
var promises = [];
for (var n in nodes) {
promises.push(when.promise(function(resolve) {
loadNode(moduleDir,nodes[n]).then(resolve, function(err) {
resolve({'fn':pkg.name+":"+n,err:err});
});
}));
}
return when.promise(function(resolve,reject) {
var errors = [];
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
}
function scanForNodes(dir) {
return when.promise(function(resolve,reject) {
var pm = path.join(dir,"node_modules");
var promises = [];
promises.push(when.promise(function(resolve,reject) {
whenNode.call(fs.readdir,pm).then(function(files) {
var promises = [];
files.forEach(function(fn) {
var pkgfn = path.join(pm,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
promises.push(loadNodesFromModule(moduleDir,pkg));
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
}
}
});
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
},function(err) {
resolve([]);
})
}));
var up = path.resolve(path.join(dir,".."));
if (up !== dir) {
promises.push(scanForNodes(up))
}
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
}
function loadNodes(dir) {
return when.promise(function(resolve,reject) {
var promises = [];
whenNode.call(fs.readdir,dir).done(function(files) {
files = files.sort();
files.forEach(function(fn) {
var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) {
loadNode(dir,fn).then(resolve, function(err) {
resolve({'fn':fn,err:err});
});
}));
}
} else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) {
loadNodes(path.join(dir,fn)).then(function(errs) {
resolve(errs);
});
}));
} else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn));
}
}
});
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
}, function(err) {
resolve([]);
// non-existant dir
});
});
}
function init(_settings) {
settings = _settings;
}
function load() {
Node = require("./Node");
return when.promise(function(resolve,reject) {
var RED = require("../red.js");
loadNodes(__dirname+"/../../nodes").then(function(errors) {
var promises = [];
promises.push(scanForNodes(__dirname+"/../../nodes"));
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) {
promises.push(loadNodes(dir[i]));
}
}
when.settle(promises).then(function(results) {
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
});
}
var typeRegistry = module.exports = {
init:init,
load:load,
registerType: function(type,node) {
util.inherits(node,Node);
node_types[type] = node;
events.emit("type-registered",type);
},
registerConfig: function(config) {
node_configs.push(config);
},
get: function(type) {
return node_types[type];
},
getNodeConfigs: function() {
var result = "";
for (var i=0;i<node_configs.length;i++) {
result += node_configs[i];
}
return result;
}
}

View File

@ -18,6 +18,7 @@ var events = require("./events");
var server = require("./server"); var server = require("./server");
var nodes = require("./nodes"); var nodes = require("./nodes");
var library = require("./library"); var library = require("./library");
var log = require("./log");
var fs = require("fs"); var fs = require("fs");
var settings = null; var settings = null;
@ -49,7 +50,8 @@ var RED = {
stop: server.stop, stop: server.stop,
nodes: nodes, nodes: nodes,
library: library, library: library,
events: events events: events,
log: log
}; };
RED.__defineGetter__("app", function() { console.log("Deprecated use of RED.app - use RED.httpAdmin instead"); return server.app }); RED.__defineGetter__("app", function() { console.log("Deprecated use of RED.app - use RED.httpAdmin instead"); return server.app });

View File

@ -41,16 +41,15 @@ function createServer(_server,_settings) {
}); });
app.get("/flows",function(req,res) { app.get("/flows",function(req,res) {
res.json(redNodes.getConfig()); res.json(redNodes.getFlows());
}); });
app.post("/flows", app.post("/flows",
express.json(), express.json(),
function(req,res) { function(req,res) {
var flows = req.body; var flows = req.body;
storage.saveFlows(flows).then(function() { redNodes.setFlows(flows).then(function() {
res.json(204); res.json(204);
redNodes.setConfig(flows);
}).otherwise(function(err) { }).otherwise(function(err) {
util.log("[red] Error saving flows : "+err); util.log("[red] Error saving flows : "+err);
res.send(500,err.message); res.send(500,err.message);
@ -70,7 +69,8 @@ function start() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Version: "+RED.version()); util.log("[red] Version: "+RED.version());
util.log("[red] Loading palette nodes"); util.log("[red] Loading palette nodes");
redNodes.load(settings).then(function(nodeErrors) { redNodes.init(settings,storage);
redNodes.load().then(function(nodeErrors) {
if (nodeErrors.length > 0) { if (nodeErrors.length > 0) {
util.log("------------------------------------------"); util.log("------------------------------------------");
if (settings.verbose) { if (settings.verbose) {
@ -84,13 +84,8 @@ function start() {
util.log("------------------------------------------"); util.log("------------------------------------------");
} }
defer.resolve(); defer.resolve();
storage.getFlows().then(function(flows) {
if (flows.length > 0) { redNodes.loadFlows();
redNodes.setConfig(flows);
}
}).otherwise(function(err) {
util.log("[red] Error loading flows : "+err);
});
}); });
}); });

95
test/credentials_spec.js Normal file
View File

@ -0,0 +1,95 @@
/**
* Copyright 2014 IBM Corp.
*
* 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.
**/
var should = require("should");
var sinon = require("sinon");
var when = require("when");
var credentials = require("../red/nodes/credentials");
describe('Credentials', function() {
it('loads from storage',function(done) {
var storage = {
getCredentials: function() {
console.log("ONE");
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
}
};
credentials.init(storage);
credentials.load().then(function() {
credentials.get("a").should.have.property('b',1);
credentials.get("a").should.have.property('c',2);
done();
});
});
it('saves to storage', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
},
saveCredentials: function(creds) {
return when(true);
}
};
sinon.spy(storage,"saveCredentials");
credentials.init(storage);
credentials.load().then(function() {
should.not.exist(credentials.get("b"))
credentials.add('b',{"d":3});
storage.saveCredentials.callCount.should.be.exactly(1);
credentials.get("b").should.have.property('d',3);
done();
});
});
it('deletes from storage', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
},
saveCredentials: function(creds) {
return when(true);
}
};
sinon.spy(storage,"saveCredentials");
credentials.init(storage);
credentials.load().then(function() {
should.exist(credentials.get("a"))
credentials.delete('a');
storage.saveCredentials.callCount.should.be.exactly(1);
should.not.exist(credentials.get("a"));
done();
});
});
})

View File

@ -1,7 +1,23 @@
var should = require("should"); /**
var RedNodes = require("../red/nodes.js"); * Copyright 2014 IBM Corp.
*
* 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.
**/
var RedNode = RedNodes.Node; var should = require("should");
var RedNodes = require("../red/nodes");
var RedNode = require("../red/nodes/Node");
describe('NodeRegistry', function() { describe('NodeRegistry', function() {
it('automatically registers new nodes',function() { it('automatically registers new nodes',function() {

View File

@ -1,7 +1,21 @@
var should = require("should"); /**
var RedNodes = require("../red/nodes.js"); * Copyright 2014 IBM Corp.
*
* 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.
**/
var RedNode = RedNodes.Node; var should = require("should");
var RedNode = require("../red/nodes/Node");
describe('Node', function() { describe('Node', function() {
describe('#constructor',function() { describe('#constructor',function() {