mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Split up nodes.js into components
This commit is contained in:
119
red/nodes/Node.js
Normal file
119
red/nodes/Node.js
Normal 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
62
red/nodes/credentials.js
Normal 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
150
red/nodes/flows.js
Normal 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
50
red/nodes/index.js
Normal 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
257
red/nodes/registry.js
Normal 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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user