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

Remove 'loaded' property from external node info object

and fix everything that doing this broke
This commit is contained in:
Nick O'Leary 2015-03-30 21:49:20 +01:00
parent 2a8a885271
commit b201828236
8 changed files with 127 additions and 118 deletions

View File

@ -29,7 +29,7 @@ var settings = require("../settings");
module.exports = {
getAll: function(req,res) {
if (req.get("accept") == "application/json") {
res.json(redNodes.getNodeList().map(function(n) { delete n.loaded; return n }));
res.json(redNodes.getNodeList());
} else {
res.send(redNodes.getNodeConfigs());
}
@ -43,7 +43,7 @@ module.exports = {
var node = req.body;
var promise;
if (node.module) {
var module = redNodes.getNodeModuleInfo(node.module);
var module = redNodes.getModuleInfo(node.module);
if (module) {
res.json(400,{message:"Module already loaded"});
return;
@ -72,7 +72,7 @@ module.exports = {
var mod = req.params.mod;
try {
var promise = null;
var module = redNodes.getNodeModuleInfo(mod);
var module = redNodes.getModuleInfo(mod);
if (!module) {
res.send(404);
return;
@ -83,9 +83,11 @@ module.exports = {
promise.then(function() {
res.send(204);
}).otherwise(function(err) {
res.json(400,{message:err.toString()});
console.log(err.stack);
res.json(400,{message:err.toString()});
});
} catch(err) {
console.log(err.stack);
res.json(400,{message:err.toString()});
}
},
@ -96,7 +98,7 @@ module.exports = {
if (req.get("accept") === "application/json") {
result = redNodes.getNodeInfo(id);
if (result) {
result.version = redNodes.getModuleVersion(req.params.mod);
delete result.loaded;
}
} else {
result = redNodes.getNodeConfig(id);
@ -112,7 +114,7 @@ module.exports = {
var module = req.params.mod;
var result = redNodes.getModuleInfo(module);
if (result) {
res.send(result);
res.json(result);
} else {
res.send(404);
}
@ -135,6 +137,7 @@ module.exports = {
if (!node) {
res.send(404);
} else {
delete node.loaded;
res.json(putNode(node, body.enabled));
}
} catch(err) {

View File

@ -85,12 +85,12 @@ function removeNode(id) {
}
function removeModule(module) {
var info = registry.getNodeModuleInfo(module);
var info = registry.getModuleInfo(module);
if (!info) {
throw new Error("Unrecognised module: "+module);
} else {
for (var i=0;i<info.length;i++) {
checkTypeInUse(module+"/"+info[i]);
for (var i=0;i<info.nodes.length;i++) {
checkTypeInUse(module+"/"+info.nodes[i].name);
}
return registry.removeModule(module);
}
@ -124,11 +124,8 @@ module.exports = {
getNodeInfo: registry.getNodeInfo,
getNodeList: registry.getNodeList,
getNodeModuleInfo: registry.getNodeModuleInfo,
getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig,

View File

@ -34,9 +34,6 @@ function filterNodeInfo(n) {
types: n.types,
enabled: n.enabled
};
if (n.hasOwnProperty("loaded")) {
r.loaded = n.loaded;
}
if (n.hasOwnProperty("module")) {
r.module = n.module;
}
@ -82,7 +79,6 @@ var registry = (function() {
if (nodes.hasOwnProperty(node)) {
var config = nodes[node];
var n = filterNodeInfo(config);
delete n.loaded;
delete n.err;
delete n.file;
delete n.id;
@ -230,13 +226,18 @@ var registry = (function() {
if (module) {
var config = module.nodes[getNode(id)];
if (config) {
return filterNodeInfo(config);
var info = filterNodeInfo(config);
if (config.hasOwnProperty("loaded")) {
info.loaded = config.loaded;
}
info.version = module.version;
return info;
}
}
}
return null;
},
getNodeList: function() {
getNodeList: function(filter) {
var list = [];
for (var module in moduleConfigs) {
/* istanbul ignore else */
@ -245,7 +246,11 @@ var registry = (function() {
for (var node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) {
list.push(filterNodeInfo(nodes[node]));
var nodeInfo = filterNodeInfo(nodes[node]);
nodeInfo.version = moduleConfigs[module].version;
if (!filter || filter(nodes[node])) {
list.push(nodeInfo);
}
}
}
}
@ -257,16 +262,7 @@ var registry = (function() {
for (var module in moduleNodes) {
/* istanbul ignore else */
if (moduleNodes.hasOwnProperty(module)) {
var nodes = moduleNodes[module];
var m = {
name: module,
version: moduleConfigs[module].version,
nodes: []
};
for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(moduleConfigs[module].nodes[nodes[i]]));
}
list.push(m);
list.push(registry.getModuleInfo(module));
}
}
return list;
@ -280,16 +276,15 @@ var registry = (function() {
nodes: []
};
for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(moduleConfigs[module].nodes[nodes[i]]));
var nodeInfo = filterNodeInfo(moduleConfigs[module].nodes[nodes[i]]);
nodeInfo.version = m.version;
m.nodes.push(nodeInfo);
}
return m;
} else {
return null;
}
},
getModuleVersion: function(module) {
return moduleConfigs[module].version;
},
registerNodeConstructor: function(type,constructor) {
if (nodeConstructors[type]) {
throw new Error(type+" already registered");
@ -373,10 +368,6 @@ var registry = (function() {
return nodeTypeToId[type];
},
getNodeModuleInfo: function(module) {
return moduleNodes[module];
},
enableNodeSet: function(typeOrId) {
if (!settings.available()) {
throw new Error("Settings unavailable");
@ -526,7 +517,7 @@ function scanDirForNodesModules(dir,moduleName) {
var files = fs.readdirSync(dir);
for (var i=0;i<files.length;i++) {
var fn = files[i];
if (!registry.getNodeModuleInfo(fn)) {
if (!registry.getModuleInfo(fn)) {
if (!moduleName || fn == moduleName) {
var pkgfn = path.join(dir,fn,"package.json");
try {
@ -626,9 +617,7 @@ function loadNodesFromModule(moduleDir,pkg) {
function loadNodeConfig(file,module,name,version) {
var id = module + "/" + name;
var info = registry.getNodeInfo(id);
var isEnabled = true;
if (info) {
if (info.hasOwnProperty("loaded")) {
throw new Error(file+" already loaded");
@ -819,7 +808,7 @@ function addModule(module) {
throw new Error("Settings unavailable");
}
var nodes = [];
if (registry.getNodeModuleInfo(module)) {
if (registry.getModuleInfo(module)) {
return when.reject(new Error("Module already loaded"));
}
var moduleFiles = scanTreeForNodesModules(module);
@ -831,7 +820,9 @@ function addModule(module) {
moduleFiles.forEach(function(moduleFile) {
nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package));
});
return loadNodeList(nodes);
return loadNodeList(nodes).then(function() {
return registry.getModuleInfo(module);
});
}
module.exports = {
@ -844,11 +835,8 @@ module.exports = {
getNodeInfo: registry.getNodeInfo,
getNodeList: registry.getNodeList,
getNodeModuleInfo: registry.getNodeModuleInfo,
getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig,

View File

@ -65,9 +65,8 @@ function start() {
redNodes.init(settings,storage,app);
redNodes.load().then(function() {
var i;
var nodes = redNodes.getNodeList();
var nodeErrors = nodes.filter(function(n) { return n.err!=null;});
var nodeMissing = nodes.filter(function(n) { return n.module && n.enabled && !n.loaded && !n.err;});
var nodeErrors = redNodes.getNodeList(function(n) { return n.err!=null;});
var nodeMissing = redNodes.getNodeList(function(n) { return n.module && n.enabled && !n.loaded && !n.err;});
if (nodeErrors.length > 0) {
log.warn("------------------------------------------");
if (settings.verbose) {
@ -114,15 +113,15 @@ function start() {
function reportAddedModules(info) {
comms.publish("node/added",info,false);
if (info.length > 0) {
comms.publish("node/added",info.nodes,false);
if (info.nodes.length > 0) {
log.info("Added node types:");
for (var i=0;i<info.length;i++) {
for (var j=0;j<info[i].types.length;j++) {
for (var i=0;i<info.nodes.length;i++) {
for (var j=0;j<info.nodes[i].types.length;j++) {
log.info(" - "+
(info[i].module?info[i].module+":":"")+
info[i].types[j]+
(info[i].err?" : "+info[i].err:"")
(info.nodes[i].module?info.nodes[i].module+":":"")+
info.nodes[i].types[j]+
(info.nodes[i].err?" : "+info.nodes[i].err:"")
);
}
}

View File

@ -116,16 +116,12 @@ describe("nodes api", function() {
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo', function(id) {
return {"node-red/123":{id:"node-red/123"}}[id];
});
var getModuleVersion = sinon.stub(redNodes,'getModuleVersion', function(module) {
return {"node-red": {version: "0.0.1"}}[module];
});
request(app)
.get('/nodes/node-red/123')
.set('Accept', 'application/json')
.expect(200)
.end(function(err,res) {
getNodeInfo.restore();
getModuleVersion.restore();
if (err) {
throw err;
}
@ -210,16 +206,11 @@ describe("nodes api", function() {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
return null;
});
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(module) {
if (module === "foo") {
return {
name:"foo",
nodes:[{id:"123"}]
};
}
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo');
getModuleInfo.onCall(0).returns(null);
getModuleInfo.onCall(1).returns({
name:"foo",
nodes:[{id:"123"}]
});
var installModule = sinon.stub(server,'installModule', function() {
return when.resolve({id:"123"});
@ -231,7 +222,6 @@ describe("nodes api", function() {
.expect(200)
.end(function(err,res) {
settingsAvailable.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
installModule.restore();
if (err) {
@ -248,8 +238,8 @@ describe("nodes api", function() {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
return {id:"123"};
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:{id:"123"}};
});
var installModule = sinon.stub(server,'installModule', function() {
return when.resolve({id:"123"});
@ -261,7 +251,7 @@ describe("nodes api", function() {
.expect(400)
.end(function(err,res) {
settingsAvailable.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
installModule.restore();
if (err) {
throw err;
@ -274,7 +264,7 @@ describe("nodes api", function() {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null;
});
var installModule = sinon.stub(server,'installModule', function() {
@ -287,7 +277,7 @@ describe("nodes api", function() {
.expect(400)
.end(function(err,res) {
settingsAvailable.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
installModule.restore();
if (err) {
throw err;
@ -300,7 +290,7 @@ describe("nodes api", function() {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null;
});
var installModule = sinon.stub(server,'installModule', function() {
@ -315,7 +305,7 @@ describe("nodes api", function() {
.expect(404)
.end(function(err,res) {
settingsAvailable.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
installModule.restore();
if (err) {
throw err;
@ -350,8 +340,8 @@ describe("nodes api", function() {
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo',function(id) {
return null;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
return {id:"123"};
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:[{id:"123"}]};
});
var uninstallModule = sinon.stub(server,'uninstallModule', function() {
return when.resolve({id:"123"});
@ -363,7 +353,7 @@ describe("nodes api", function() {
.end(function(err,res) {
settingsAvailable.restore();
getNodeInfo.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
uninstallModule.restore();
if (err) {
throw err;
@ -379,7 +369,7 @@ describe("nodes api", function() {
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo',function(id) {
return null;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null;
});
@ -389,7 +379,7 @@ describe("nodes api", function() {
.end(function(err,res) {
settingsAvailable.restore();
getNodeInfo.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
if (err) {
throw err;
}
@ -404,8 +394,8 @@ describe("nodes api", function() {
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo',function(id) {
return null;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
return {id:"123"};
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:[{id:"123"}]};
});
var uninstallModule = sinon.stub(server,'uninstallModule', function() {
return when.reject(new Error("test error"));
@ -417,7 +407,7 @@ describe("nodes api", function() {
.end(function(err,res) {
settingsAvailable.restore();
getNodeInfo.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
uninstallModule.restore();
if (err) {
throw err;

View File

@ -219,13 +219,13 @@ describe("red/nodes/index", function() {
return randomNodeInfo;
}
});
sinon.stub(registry,"getNodeModuleInfo",function(module) {
sinon.stub(registry,"getModuleInfo",function(module) {
if (module == "node-red") {
return ["foo"];
return {nodes:[{name:"foo"}]};
} else if (module == "doesnotexist") {
return null;
} else {
return randomModuleInfo.nodes;
return randomModuleInfo;
}
});
sinon.stub(registry,"removeModule",function(id) {
@ -234,7 +234,7 @@ describe("red/nodes/index", function() {
});
after(function() {
registry.getNodeInfo.restore();
registry.getNodeModuleInfo.restore();
registry.getModuleInfo.restore();
registry.removeModule.restore();
});

View File

@ -40,16 +40,52 @@ describe('red/nodes/registry', function() {
}
var settings = stubSettings({},false,null);
var settingsWithStorage = stubSettings({},true,null);
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion"}});
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion",nodes:{"node":{id:"node-red/testName",name:"test",types:["a","b"],enabled:true}}}});
it('loads initial config', function(done) {
typeRegistry.init(settingsWithStorageAndInitialConfig);
var version = typeRegistry.getModuleVersion("node-red");
should.exist(version);
version.should.eql("testVersion");
typeRegistry.getNodeList().should.have.lengthOf(1);
done();
});
it('migrates legacy format', function(done) {
var settings = {
available: function() { return true; },
set: sinon.stub().returns(when.resolve()),
get: function() { return {
"123": {
"name": "72-sentiment.js",
"types": [
"sentiment"
],
"enabled": true
},
"456": {
"name": "20-inject.js",
"types": [
"inject"
],
"enabled": true
},
"789": {
"name": "testModule:a-module.js",
"types": [
"example"
],
"enabled":true,
"module":"testModule"
}
}}
};
var expected = JSON.parse('{"node-red":{"name":"node-red","nodes":{"sentiment":{"name":"sentiment","types":["sentiment"],"enabled":true,"module":"node-red"},"inject":{"name":"inject","types":["inject"],"enabled":true,"module":"node-red"}}},"testModule":{"name":"testModule","nodes":{"a-module.js":{"name":"a-module.js","types":["example"],"enabled":true,"module":"testModule"}}}}');
typeRegistry.init(settings);
settings.set.calledOnce.should.be.true;
settings.set.args[0][1].should.eql(expected);
done();
});
it('handles nodes that export a function', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
@ -356,14 +392,18 @@ describe('red/nodes/registry', function() {
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var id = list[0].id;
var type = list[0].types[0];
var id = "node-red/TestNode1";
var type = "test-node-1";
var info = typeRegistry.getNodeInfo(id);
info.should.have.property("loaded");
delete info.loaded;
list[0].should.eql(info);
var info2 = typeRegistry.getNodeInfo(type);
info2.should.have.property("loaded");
delete info2.loaded;
list[0].should.eql(info2);
done();
@ -472,13 +512,10 @@ describe('red/nodes/registry', function() {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule").then(function(nodes) {
var list = typeRegistry.getModuleList();
var module = typeRegistry.getModuleInfo(list[0].name);
module.should.have.property("name", list[0].name);
module.should.have.property("nodes", nodes);
typeRegistry.addModule("TestNodeModule").then(function(modInfo) {
var info = typeRegistry.getModuleInfo("TestNodeModule");
modInfo.should.eql(info);
should.not.exist(typeRegistry.getModuleInfo("does-not-exist"));
done();
@ -597,7 +634,7 @@ describe('red/nodes/registry', function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addModule("TestNodeModule").then(function(node) {
typeRegistry.addModule("TestNodeModule").then(function(modInfo) {
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
list[0].should.have.property("id","TestNodeModule/TestNodeMod1");
@ -614,8 +651,6 @@ describe('red/nodes/registry', function() {
list[1].should.have.property("enabled",true);
list[1].should.have.property("err");
node.should.eql(list);
done();
}).catch(function(e) {
done(e);
@ -671,9 +706,6 @@ describe('red/nodes/registry', function() {
modules[0].should.have.property("name","TestNodeModule");
modules[0].should.have.property("version","0.0.1");
var version = typeRegistry.getModuleVersion("TestNodeModule");
version.should.eql("0.0.1");
done();
}).catch(function(e) {
done(e);

View File

@ -105,11 +105,11 @@ describe("red/server", function() {
commsStart.restore();
});
it("reports errored/missing modules",function(done) {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
return [
{ err:"errored",name:"errName" }, // error
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
];
].filter(cb);
});
server.init({},{testSettings: true, httpAdminRoot:"/", load:function() { return when.resolve();}});
server.start().then(function() {
@ -132,13 +132,13 @@ describe("red/server", function() {
});
});
it("initiates load of missing modules",function(done) {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
return [
{ err:"errored",name:"errName" }, // error
{ err:"errored",name:"errName" }, // error
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]}, // missing
{ module:"node-red",enabled:true,loaded:false,types:["typeC","typeD"]} // missing
];
].filter(cb);
});
var serverInstallModule = sinon.stub(server,"installModule",function(name) { return when.resolve();});
server.init({},{testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
@ -161,10 +161,10 @@ describe("red/server", function() {
});
});
it("reports errored modules when verbose is enabled",function(done) {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
return [
{ err:"errored",name:"errName" } // error
];
].filter(cb);
});
server.init({},{testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
server.start().then(function() {
@ -240,17 +240,17 @@ describe("red/server", function() {
});
it("reports added modules", function() {
var nodes = [
var nodes = {nodes:[
{types:["a"]},
{module:"foo",types:["b"]},
{types:["c"],err:"error"}
];
]};
var result = server.reportAddedModules(nodes);
result.should.equal(nodes);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/added");
commsMessages[0].msg.should.eql(nodes);
commsMessages[0].msg.should.eql(nodes.nodes);
});
it("reports removed modules", function() {
@ -305,7 +305,7 @@ describe("red/server", function() {
});
});
it("succeeds when module is found", function(done) {
var nodeInfo = {module:"foo",types:["a"]};
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
@ -317,7 +317,7 @@ describe("red/server", function() {
info.should.eql(nodeInfo);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/added");
commsMessages[0].msg.should.eql(nodeInfo);
commsMessages[0].msg.should.eql(nodeInfo.nodes);
done();
}).otherwise(function(err) {
done(err);