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

Add node add/remove/enable/disable apis to registry

This commit is contained in:
Nick O'Leary 2014-08-04 17:12:54 +01:00
parent 495dd3f2e0
commit 960d15491d
8 changed files with 410 additions and 68 deletions

View File

@ -51,6 +51,26 @@ function init(_settings,storage) {
registry.init(_settings);
}
function removeNode(info) {
var nodeInfo = registry.getNodeInfo(info);
var inUse = {};
flows.each(function(n) {
inUse[n.type] = (inUse[n.type]||0)+1;
});
var nodesInUse = [];
nodeInfo.types.forEach(function(t) {
if (inUse[t]) {
nodesInUse.push(t);
}
});
if (nodesInUse.length > 0) {
var msg = nodesInUse.join(", ");
throw Error("Type in use: "+msg);
}
return registry.removeNode(nodeInfo.id);
}
module.exports = {
// Lifecycle
init: init,
@ -60,6 +80,9 @@ module.exports = {
createNode: createNode,
getNode: flows.get,
addNode: registry.addNode,
removeNode: removeNode,
// Node type registry
registerType: registerType,
getType: registry.get,

View File

@ -46,14 +46,42 @@ var registry = (function() {
var nodeConfigs = {};
var nodeList = [];
var nodeConstructors = {};
var nodeTypeToId = {};
return {
addNodeSet: function(id,set) {
if (!set.err) {
set.types.forEach(function(t) {
nodeTypeToId[t] = id;
});
}
nodeConfigs[id] = set;
nodeList.push(id);
nodeConfigCache = null;
},
getNodeSet: function(id) {
return nodeConfigs[id];
removeNode: function(id) {
var config = nodeConfigs[id];
if (config) {
delete nodeConfigs[id];
var i = nodeList.indexOf(id);
if (i > -1) {
nodeList.splice(i,1);
}
config.types.forEach(function(t) {
delete nodeConstructors[t];
delete nodeTypeToId[t];
});
nodeConfigCache = null;
}
return filterNodeInfo(config);
},
getNodeInfo: function(typeOrId) {
if (nodeTypeToId[typeOrId]) {
return filterNodeInfo(nodeConfigs[nodeTypeToId[typeOrId]]);
} else if (nodeConfigs[typeOrId]) {
return filterNodeInfo(nodeConfigs[typeOrId]);
}
return null;
},
getNodeList: function() {
return nodeList.map(function(id) {
@ -65,9 +93,11 @@ var registry = (function() {
if (nodeConstructors[type]) {
throw new Error(type+" already registered");
}
//TODO: Ensure type is known - but doing so will break some tests
// that don't have a way to register a node template ahead
// of registering the constructor
util.inherits(constructor,Node);
nodeConstructors[type] = constructor;
events.emit("type-registered",type);
},
@ -87,9 +117,11 @@ var registry = (function() {
script += config.script;
}
}
result += '<script type="text/javascript">';
result += UglifyJS.minify(script, {fromString: true}).code;
result += '</script>';
if (script.length > 0) {
result += '<script type="text/javascript">';
result += UglifyJS.minify(script, {fromString: true}).code;
result += '</script>';
}
nodeConfigCache = result;
}
return nodeConfigCache;
@ -107,7 +139,11 @@ var registry = (function() {
},
getNodeConstructor: function(type) {
return nodeConstructors[type];
var config = nodeConfigs[nodeTypeToId[type]];
if (!config || config.enabled) {
return nodeConstructors[type];
}
return null;
},
clear: function() {
@ -115,6 +151,31 @@ var registry = (function() {
nodeConfigs = {};
nodeList = [];
nodeConstructors = {};
nodeTypeToId = {};
},
getTypeId: function(type) {
return nodeTypeToId[type];
},
enableNodeSet: function(id) {
var config = nodeConfigs[id];
if (config) {
if (config.err) {
throw new Error("cannot enable node with error");
}
config.enabled = true;
nodeConfigCache = null;
}
},
disableNodeSet: function(id) {
var config = nodeConfigs[id];
if (config) {
config.enabled = false;
nodeConfigCache = null;
}
}
}
})();
@ -220,7 +281,7 @@ function loadNodesFromModule(moduleDir,pkg) {
for (var n in nodes) {
if (nodes.hasOwnProperty(n)) {
var file = path.join(moduleDir,nodes[n]);
results.push(loadNodeConfig(file,pkg.name+":"+n));
results.push(loadNodeConfig(file,pkg.name,n));
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
if (iconDirs.indexOf(iconDir) == -1) {
if (fs.existsSync(iconDir)) {
@ -249,21 +310,28 @@ function loadNodesFromModule(moduleDir,pkg) {
* types: an array of node type names in this file
* }
*/
function loadNodeConfig(file,name) {
function loadNodeConfig(file,module,name) {
var id = crypto.createHash('sha1').update(file).digest("hex");
if (registry.getNodeSet(id)) {
if (registry.getNodeInfo(id)) {
throw new Error(file+" already loaded");
}
var node = {
id: id,
file: file,
name: name||path.basename(file),
template: file.replace(/\.js$/,".html"),
enabled: true
}
if (module) {
node.name = module+":"+name;
node.module = module;
} else {
node.name = path.basename(file)
}
var content = fs.readFileSync(node.template,'utf8');
var $ = cheerio.load(content);
@ -293,6 +361,12 @@ function loadNodeConfig(file,name) {
node.config = template;
node.script = script;
for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered";
break;
}
}
registry.addNodeSet(id,node);
return node;
}
@ -324,7 +398,11 @@ function load(defaultNodesDir,disableNodePathScan) {
}
var nodes = [];
nodeFiles.forEach(function(file) {
nodes.push(loadNodeConfig(file));
try {
nodes.push(loadNodeConfig(file));
} catch(err) {
//
}
});
// TODO: disabling npm module loading if defaultNodesDir set
@ -392,28 +470,44 @@ function loadNodeModule(node) {
}
}
function loadNode(file) {
var info = null;
try {
info = loadNodeConfig(file);
} catch(err) {
return when.reject(err);
function addNode(options) {
var nodes = [];
if (options.file) {
try {
nodes.push(loadNodeConfig(options.file));
} catch(err) {
return when.reject(err);
}
} else if (options.module) {
var moduleFiles = scanTreeForNodesModules(options.module);
moduleFiles.forEach(function(moduleFile) {
nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package));
});
}
return loadNodeModule(info).then(function(info) {
return filterNodeInfo(info);
var promises = [];
nodes.forEach(function(node) {
promises.push(loadNodeModule(node));
});
return when.settle(promises).then(function(results) {
return results.map(function(r) {
return filterNodeInfo(r.value);
});
});
}
module.exports = {
init:init,
load:load,
clear: registry.clear,
registerType: registry.registerNodeConstructor,
get: registry.getNodeConstructor,
getNodeInfo: registry.getNodeInfo,
getNodeList: registry.getNodeList,
getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig,
loadNode: loadNode
addNode: addNode,
removeNode: registry.removeNode,
enableNode: registry.enableNodeSet,
disableNode: registry.disableNodeSet
}

View File

@ -18,7 +18,7 @@ var should = require("should");
var fs = require('fs-extra');
var path = require('path');
var when = require("when");
var defer = when.defer();
var sinon = require('sinon');
var index = require("../../../red/nodes/index");
@ -31,20 +31,14 @@ describe("red/nodes/index", function() {
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var storage = {
getFlows: function() {
var defer = when.defer();
defer.resolve(testFlows);
return defer.promise;
return when(testFlows);
},
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"tab1":{"b":1,"c":2}});
});
return when({"tab1":{"b":1,"c":2}});
},
saveFlows: function(conf) {
var defer = when.defer();
defer.resolve();
should.deepEqual(testFlows, conf);
return defer.promise;
return when();
},
saveCredentials: function(creds) {
return when(true);
@ -75,29 +69,6 @@ describe("red/nodes/index", function() {
});
it('flows should be initialised',function(done) {
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var storage = {
getFlows: function() {
var defer = when.defer();
defer.resolve(testFlows);
return defer.promise;
},
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"tab1":{"b":1,"c":2}});
});
},
saveFlows: function(conf) {
var defer = when.defer();
defer.resolve();
should.deepEqual(testFlows, conf);
return defer.promise;
},
saveCredentials: function(creds) {
return when(true);
}
};
index.init({}, storage);
index.loadFlows().then(function() {
should.deepEqual(testFlows, index.getFlows());
@ -111,7 +82,6 @@ describe("red/nodes/index", function() {
describe("registerType should register credentials definition", function() {
var http = require('http');
var express = require('express');
var sinon = require('sinon');
var app = express();
var server = require("../../../red/server");
var credentials = require("../../../red/nodes/credentials");
@ -161,4 +131,58 @@ describe("red/nodes/index", function() {
});
describe('allows nodes to be removed from the registry', function() {
var registry = require("../../../red/nodes/registry");
var randomNodeInfo = {id:"5678",types:["random"]};
before(function() {
sinon.stub(registry,"getNodeInfo",function(id) {
if (id == "test") {
return {id:"1234",types:["test"]};
} else {
return randomNodeInfo;
}
});
sinon.stub(registry,"removeNode",function(id) {
return randomNodeInfo;
});
});
after(function() {
registry.getNodeInfo.restore();
registry.removeNode.restore();
});
it(': allows an unused node type to be removed',function(done) {
index.init({}, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var info = index.removeNode("random");
registry.removeNode.calledOnce.should.be.true;
registry.removeNode.calledWith("5678").should.be.true;
info.should.eql(randomNodeInfo);
done();
}).otherwise(function(err) {
done(err);
});
});
it(': prevents removing a node type that is in use',function(done) {
index.init({}, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeNode("test");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
});
});

View File

@ -53,7 +53,7 @@ describe('NodeRegistry', function() {
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-1");
(typeof nodeConstructor).should.be.equal("function");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
@ -74,7 +74,7 @@ describe('NodeRegistry', function() {
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-2");
(typeof nodeConstructor).should.be.equal("function");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
@ -96,7 +96,7 @@ describe('NodeRegistry', function() {
list[0].should.have.property("err","fail");
var nodeConstructor = typeRegistry.get("test-node-3");
(typeof nodeConstructor).should.be.equal("undefined");
(nodeConstructor === null).should.be.true;
done();
}).catch(function(e) {
@ -117,10 +117,10 @@ describe('NodeRegistry', function() {
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-multiple-1a");
(typeof nodeConstructor).should.be.equal("function");
nodeConstructor.should.be.type("function");
nodeConstructor = typeRegistry.get("test-node-multiple-1b");
(typeof nodeConstructor).should.be.equal("function");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
@ -283,13 +283,13 @@ describe('NodeRegistry', function() {
});
});
it('allows nodes to be added', function(done) {
it('allows nodes to be added by filename', function(done) {
typeRegistry.init({});
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.loadNode(resourcesDir + "TestNode1/TestNode1.js").then(function(node) {
typeRegistry.addNode({file: resourcesDir + "TestNode1/TestNode1.js"}).then(function(node) {
list = typeRegistry.getNodeList();
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
@ -297,7 +297,8 @@ describe('NodeRegistry', function() {
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
node.should.eql(list[0]);
node.should.be.an.Array.and.have.lengthOf(1);
node.should.eql(list);
done();
}).catch(function(e) {
@ -309,13 +310,43 @@ describe('NodeRegistry', function() {
});
});
it('returns node info by type or id', function(done) {
typeRegistry.init({});
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
var id = list[0].id;
var type = list[0].types[0];
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");
var info = typeRegistry.getNodeInfo(id);
list[0].should.eql(info);
var info2 = typeRegistry.getNodeInfo(type);
list[0].should.eql(info2);
done();
}).catch(function(e) {
done(e);
});
});
it('rejects adding duplicate nodes', function(done) {
typeRegistry.init({});
typeRegistry.load(resourcesDir + "TestNode1",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
typeRegistry.loadNode(resourcesDir + "TestNode1" + path.sep + "TestNode1.js").then(function(node) {
typeRegistry.addNode({file:resourcesDir + "TestNode1" + path.sep + "TestNode1.js"}).then(function(node) {
done(new Error("duplicate node loaded"));
}).otherwise(function(e) {
var list = typeRegistry.getNodeList();
@ -328,6 +359,34 @@ describe('NodeRegistry', function() {
});
});
it('removes nodes from the registry', function(done) {
typeRegistry.init({});
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
list[0].should.have.property("types",["test-node-1"]);
typeRegistry.getNodeConfigs().length.should.be.greaterThan(0);
var info = typeRegistry.removeNode(list[0].id);
info.should.eql(list[0]);
typeRegistry.getNodeList().should.be.an.Array.and.be.empty;
typeRegistry.getNodeConfigs().length.should.equal(0);
var nodeConstructor = typeRegistry.get("test-node-1");
(typeof nodeConstructor).should.be.equal("undefined");
done();
}).catch(function(e) {
done(e);
});
});
it('scans the node_modules path for node files', function(done) {
var fs = require("fs");
var path = require("path");
@ -362,12 +421,18 @@ describe('NodeRegistry', function() {
typeRegistry.init({});
typeRegistry.load("wontexist",false).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list.should.be.an.Array.and.have.lengthOf(2);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNodeModule:TestNodeMod1");
list[0].should.have.property("types",["test-node-mod-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
list[1].should.have.property("id");
list[1].should.have.property("name","TestNodeModule:TestNodeMod2");
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",false);
list[1].should.have.property("err");
eventEmitSpy.callCount.should.equal(2);
@ -389,4 +454,129 @@ describe('NodeRegistry', function() {
});
});
it('allows nodes to be added by module name', function(done) {
var fs = require("fs");
var path = require("path");
var pathJoin = (function() {
var _join = path.join;
return sinon.stub(path,"join",function() {
if (arguments.length == 3 && arguments[2] == "package.json") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1],arguments[2]);
}
if (arguments.length == 2 && arguments[1] == "TestNodeModule") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1]);
}
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
return sinon.stub(fs,"readdirSync",function(dir) {
var result = [];
if (callCount == 1) {
result = originalReaddirSync(resourcesDir + "TestNodeModule" + path.sep + "node_modules");
}
callCount++;
return result;
});
})();
typeRegistry.init({});
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addNode({module: "TestNodeModule"}).then(function(node) {
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNodeModule:TestNodeMod1");
list[0].should.have.property("types",["test-node-mod-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
list[1].should.have.property("id");
list[1].should.have.property("name","TestNodeModule:TestNodeMod2");
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",false);
list[1].should.have.property("err");
node.should.eql(list);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
}).finally(function() {
readdirSync.restore();
pathJoin.restore();
});
});
it('allows nodes to be enabled and disabled', function(done) {
typeRegistry.init({});
typeRegistry.load(resourcesDir+path.sep+"TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
list[0].should.have.property("enabled",true);
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
typeRegistry.disableNode(list[0].id);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
typeRegistry.getNodeConfigs().length.should.equal(0);
typeRegistry.enableNode(list[0].id);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
}).catch(function(e) {
done(e);
});
});
it('does not allow a node with error to be enabled', function(done) {
typeRegistry.init({});
typeRegistry.load(resourcesDir+path.sep+"TestNode3",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode3.js");
list[0].should.have.property("enabled",false);
list[0].should.have.property("err");
/*jshint immed: false */
(function() {
typeRegistry.enable(list[0].id);
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
});

View File

@ -0,0 +1 @@
This file exists just to ensure the parent directory is in the repository.

View File

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

View File

@ -0,0 +1,4 @@
// A test node that exports a function
module.exports = function(RED) {
throw new Error("fail to load");
}

View File

@ -4,7 +4,8 @@
"description" : "A test node module",
"node-red" : {
"nodes": {
"TestNodeMod1": "TestNodeModule.js"
"TestNodeMod1": "TestNodeModule.js",
"TestNodeMod2": "TestNodeModule2.js"
}
}
}