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

Add loadNode api to registry.js

This commit is contained in:
Nick O'Leary 2014-08-01 23:42:01 +01:00
parent 3ee98e730d
commit 1697aee9f6
9 changed files with 118 additions and 21 deletions

View File

@ -28,6 +28,19 @@ var events = require("../events");
var Node;
var settings;
function filterNodeInfo(n) {
var r = {
id: n.id,
types: n.types,
name: n.name,
enabled: n.enabled
}
if (n.err) {
r.err = n.err.toString();
}
return r;
}
var registry = (function() {
var nodeConfigCache = null;
var nodeConfigs = {};
@ -39,19 +52,13 @@ var registry = (function() {
nodeConfigs[id] = set;
nodeList.push(id);
},
getNodeSet: function(id) {
return nodeConfigs[id];
},
getNodeList: function() {
return nodeList.map(function(id) {
var n = nodeConfigs[id];
var r = {
id: n.id,
types: n.types,
name: n.name,
enabled: n.enabled
}
if (n.err) {
r.err = n.err.toString();
}
return r;
return filterNodeInfo(n);
});
},
registerNodeConstructor: function(type,constructor) {
@ -76,8 +83,8 @@ var registry = (function() {
for (var i=0;i<nodeList.length;i++) {
var config = nodeConfigs[nodeList[i]];
if (config.enabled) {
result += config.config||"";
script += config.script||"";
result += config.config;
script += config.script;
}
}
result += '<script type="text/javascript">';
@ -91,8 +98,8 @@ var registry = (function() {
getNodeConfig: function(id) {
var config = nodeConfigs[id];
if (config) {
var result = config.config||"";
result += '<script type="text/javascript">'+(config.script||"")+'</script>';
var result = config.config;
result += '<script type="text/javascript">'+config.script+'</script>';
return result;
} else {
return null;
@ -244,6 +251,10 @@ function loadNodesFromModule(moduleDir,pkg) {
function loadNodeConfig(file,name) {
var id = crypto.createHash('sha1').update(file).digest("hex");
if (registry.getNodeSet(id)) {
throw new Error(file+" already loaded");
}
var node = {
id: id,
file: file,
@ -268,11 +279,9 @@ function loadNodeConfig(file,name) {
}
var openTag = "<"+el.name;
var closeTag = "</"+el.name+">";
if (el.attribs) {
for (var j in el.attribs) {
if (el.attribs.hasOwnProperty(j)) {
openTag += " "+j+'="'+el.attribs[j]+'"';
}
for (var j in el.attribs) {
if (el.attribs.hasOwnProperty(j)) {
openTag += " "+j+'="'+el.attribs[j]+'"';
}
}
openTag += ">";
@ -330,7 +339,7 @@ function load(defaultNodesDir) {
}
var promises = [];
nodes.forEach(function(node) {
promises.push(loadNode(node));
promises.push(loadNodeModule(node));
});
//resolve([]);
@ -351,7 +360,7 @@ function load(defaultNodesDir) {
* err: any error encountered whilst loading the node
*
*/
function loadNode(node) {
function loadNodeModule(node) {
var nodeDir = path.dirname(node.file);
var nodeFn = path.basename(node.file);
try {
@ -383,6 +392,19 @@ function loadNode(node) {
}
function loadNode(file) {
var info = null;
try {
info = loadNodeConfig(file);
} catch(err) {
return when.reject(err);
}
return loadNodeModule(info).then(function(info) {
return filterNodeInfo(info);
});
}
module.exports = {
init:init,
load:load,
@ -392,4 +414,5 @@ module.exports = {
getNodeList: registry.getNodeList,
getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig,
loadNode: loadNode
}

View File

@ -277,4 +277,50 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('allows nodes to be added', function(done) {
typeRegistry.init({});
typeRegistry.load("wontexist").then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.loadNode(__dirname+"/resources/TestNode1/TestNode1.js").then(function(node) {
list = typeRegistry.getNodeList();
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
node.should.eql(list[0]);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('rejects adding duplicate nodes', function(done) {
typeRegistry.init({});
typeRegistry.load(__dirname+"/resources/TestNode1").then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
typeRegistry.loadNode(__dirname+"/resources/TestNode1/TestNode1.js").then(function(node) {
done(new Error("duplicate node loaded"));
}).otherwise(function(e) {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
done();
});
}).catch(function(e) {
done(e);
});
});
});

View File

@ -0,0 +1,4 @@
<script type="text/x-red" data-template-name="should-not-load-1"></script>
<script type="text/x-red" data-help-name="should-not-load-1"></script>
<script type="text/javascript">RED.nodes.registerType('should-not-load-1',{});</script>
<style></style>

View File

@ -0,0 +1,5 @@
// A test node that exports a function
module.exports = function(RED) {
function TestNode(n) {}
RED.nodes.registerType("should-not-load-1",TestNode);
}

View File

@ -0,0 +1,4 @@
<script type="text/x-red" data-template-name="should-not-load-2"></script>
<script type="text/x-red" data-help-name="should-not-load-2"></script>
<script type="text/javascript">RED.nodes.registerType('should-not-load-2',{});</script>
<style></style>

View File

@ -0,0 +1,5 @@
// A test node that exports a function
module.exports = function(RED) {
function TestNode(n) {}
RED.nodes.registerType("should-not-load-2",TestNode);
}

View File

@ -0,0 +1,4 @@
<script type="text/x-red" data-template-name="should-not-load-3"></script>
<script type="text/x-red" data-help-name="should-not-load-3"></script>
<script type="text/javascript">RED.nodes.registerType('should-not-load-3',{});</script>
<style></style>

View File

@ -0,0 +1,5 @@
// A test node that exports a function
module.exports = function(RED) {
function TestNode(n) {}
RED.nodes.registerType("should-not-load-3",TestNode);
}

View File

@ -2,3 +2,4 @@
<script type="text/x-red" data-help-name="test-node-1"></script>
<script type="text/javascript">RED.nodes.registerType('test-node-1',{});</script>
<style></style>
<p>this should be filtered out</p>