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

View File

@ -277,4 +277,50 @@ describe('NodeRegistry', function() {
done(e); 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/x-red" data-help-name="test-node-1"></script>
<script type="text/javascript">RED.nodes.registerType('test-node-1',{});</script> <script type="text/javascript">RED.nodes.registerType('test-node-1',{});</script>
<style></style> <style></style>
<p>this should be filtered out</p>