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

Restore ability to add nodes by filename

This commit is contained in:
Nick O'Leary 2015-05-27 14:11:11 +01:00
parent 4f174308b9
commit 53258eeede
9 changed files with 228 additions and 141 deletions

View File

@ -53,20 +53,30 @@ module.exports = {
return;
}
promise = server.installModule(node.module);
} else if (node.file) {
promise = server.installNode(node.file);
} else {
log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req);
res.json(400,{error:"invalid_request", message:"Invalid request"});
return;
}
promise.then(function(info) {
log.audit({event: "nodes.install",module:node.module},req);
res.json(redNodes.getModuleInfo(node.module));
if (node.module) {
log.audit({event: "nodes.install",module:node.module},req);
res.json(redNodes.getModuleInfo(node.module));
} else if (node.file) {
log.audit({event: "nodes.install",file:node.file},req);
res.json(info.nodes[0]);
}
}).otherwise(function(err) {
if (err.code === 404) {
log.audit({event: "nodes.install",module:node.module,error:"not_found"},req);
log.audit({event: "nodes.install",module:node.module,file:node.file,error:"not_found"},req);
res.send(404);
} else if (err.code) {
log.audit({event: "nodes.install",module:node.module,error:err.code},req);
res.json(400,{error:err.code, message:err.message});
} else {
log.audit({event: "nodes.install",module:node.module,error:err.code||"unexpected_error",message:err.toString()},req);
log.audit({event: "nodes.install",module:node.module,file:node.file,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
}
});
@ -161,9 +171,10 @@ module.exports = {
res.send(404);
} else {
delete node.loaded;
var result = putNode(node, body.enabled);
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled},req);
res.json(result);
putNode(node, body.enabled).then(function(result) {
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled},req);
res.json(result);
});
}
} catch(err) {
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
@ -192,28 +203,13 @@ module.exports = {
}
var nodes = module.nodes;
var promises = [];
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i];
var info;
if (node.err || node.enabled !== body.enabled) {
if (body.enabled) {
info = redNodes.enableNode(node.id);
} else {
info = redNodes.disableNode(node.id);
}
if (info.enabled === body.enabled && !info.err) {
comms.publish("node/"+(body.enabled?"enabled":"disabled"),info,false);
log.info(" "+(body.enabled?"Enabled":"Disabled")+" node types:");
for (var j = 0; j < info.types.length; j++) {
log.info(" - " + info.types[j]);
}
} else if (body.enabled && info.err) {
log.warn("Failed to enable node:");
log.warn(" - "+info.name+" : "+info.err);
}
}
promises.push(putNode(nodes[i],body.enabled));
}
res.json(redNodes.getModuleInfo(mod));
when.settle(promises).then(function() {
res.json(redNodes.getModuleInfo(mod));
});
} catch(err) {
log.audit({event: "nodes.module.set",module:mod,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
@ -223,27 +219,30 @@ module.exports = {
function putNode(node, enabled) {
var info;
var promise;
if (!node.err && node.enabled === enabled) {
info = node;
promise = when.resolve(node);
} else {
if (enabled) {
info = redNodes.enableNode(node.id);
promise = redNodes.enableNode(node.id);
} else {
info = redNodes.disableNode(node.id);
promise = redNodes.disableNode(node.id);
}
if (info.enabled === enabled && !info.err) {
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
log.info(" "+(enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
log.info(" - "+info.types[i]);
return promise.then(function(info) {
if (info.enabled === enabled && !info.err) {
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
log.info(" "+(enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
log.info(" - "+info.types[i]);
}
} else if (enabled && info.err) {
log.warn("Failed to enable node:");
log.warn(" - "+info.name+" : "+info.err);
}
} else if (enabled && info.err) {
log.warn("Failed to enable node:");
log.warn(" - "+info.name+" : "+info.err);
}
return info;
});
}
return info;
return promise;
}

View File

@ -115,6 +115,7 @@ module.exports = {
getNode: flows.get,
eachNode: flows.eachNode,
addFile: registry.addFile,
addModule: registry.addModule,
removeModule: removeModule,

View File

@ -36,6 +36,12 @@ function load(defaultNodesDir,disableNodePathScan) {
return loader.load(defaultNodesDir,disableNodePathScan);
}
function addFile(file) {
var info = "node-red/"+path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,"");
return loader.addFile(file).then(function() {
return registry.getNodeInfo(info);
});
}
function addModule(module) {
return loader.addModule(module).then(function() {
return registry.getModuleInfo(module);
@ -43,13 +49,15 @@ function addModule(module) {
}
function enableNodeSet(typeOrId) {
registry.enableNodeSet(typeOrId);
var nodeSet = registry.getNodeInfo(typeOrId);
if (!nodeSet.loaded) {
loader.loadNodeSet(nodeSet);
return registry.getNodeInfo(typeOrId);
}
return nodeSet;
return registry.enableNodeSet(typeOrId).then(function() {
var nodeSet = registry.getNodeInfo(typeOrId);
if (!nodeSet.loaded) {
return loader.loadNodeSet(registry.getFullNodeInfo(typeOrId)).then(function() {
return registry.getNodeInfo(typeOrId);
});
}
return when.resolve(nodeSet);
});
}
module.exports = {
@ -71,6 +79,7 @@ module.exports = {
enableNode: enableNodeSet,
disableNode: registry.disableNodeSet,
addFile: addFile,
addModule: addModule,
removeModule: registry.removeModule,

View File

@ -45,7 +45,7 @@ function loadNodeFiles(nodeFiles) {
for (var module in nodeFiles) {
/* istanbul ignore else */
if (nodeFiles.hasOwnProperty(module)) {
if (!registry.getModuleInfo(module)) {
if (module == "node-red" || !registry.getModuleInfo(module)) {
var first = true;
for (var node in nodeFiles[module].nodes) {
/* istanbul ignore else */
@ -174,6 +174,7 @@ function loadNodeSet(node) {
var nodeFn = path.basename(node.file);
if (!node.enabled) {
return when.resolve(node);
} else {
}
try {
var loadPromise = null;
@ -228,7 +229,9 @@ function addModule(module) {
}
var nodes = [];
if (registry.getModuleInfo(module)) {
return when.reject(new Error("Module already loaded"));
var e = new Error("Module already loaded");
e.code = "module_already_loaded";
return when.reject(e);
}
try {
var moduleFiles = localfilesystem.getModuleFiles(module);
@ -238,9 +241,38 @@ function addModule(module) {
}
}
function addFile(file) {
if (!settings.available()) {
throw new Error("Settings unavailable");
}
var info = registry.getNodeInfo("node-red/"+path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""));
if (info) {
var err = new Error("File already loaded");
err.code = "file_already_loaded";
return when.reject(err);
}
var nodeFiles = localfilesystem.getLocalFile(file);
if (nodeFiles) {
var fileObj = {};
fileObj[nodeFiles.module] = {
name: nodeFiles.module,
version: nodeFiles.version,
nodes: {}
};
fileObj[nodeFiles.module].nodes[nodeFiles.name] = nodeFiles;
return loadNodeFiles(fileObj);
} else {
var e = new Error();
e.code = 404;
return when.reject(e);
}
}
module.exports = {
init: init,
load: load,
addModule: addModule,
addFile: addFile,
loadNodeSet: loadNodeSet
}

View File

@ -34,6 +34,26 @@ function init(_settings,_defaultNodesDir,_disableNodePathScan) {
}
}
function getLocalFile(file) {
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == path.basename(file)) {
return null;
}
}
}
if (fs.existsSync(file.replace(/\.js$/,".html"))) {
return {
file: file,
module: "node-red",
name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
version: settings.version
};
}
return null;
}
/**
* Synchronously walks the directory looking for node files.
@ -54,24 +74,9 @@ function getLocalNodeFiles(dir) {
var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
var valid = true;
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == fn) {
valid = false;
break;
}
}
}
valid = valid && fs.existsSync(path.join(dir,fn.replace(/\.js$/,".html")));
if (valid) {
result.push({
file: path.join(dir,fn),
module: "node-red",
name: path.basename(fn).replace(/^\d+-/,"").replace(/\.js$/,""),
version: settings.version
});
var info = getLocalFile(path.join(dir,fn));
if (info) {
result.push(info);
}
}
} else if (stats.isDirectory()) {
@ -256,5 +261,6 @@ function getModuleFiles(module) {
module.exports = {
init: init,
getNodeFiles: getNodeFiles,
getLocalFile: getLocalFile,
getModuleFiles: getModuleFiles
}

View File

@ -47,7 +47,7 @@ function init(_settings) {
function filterNodeInfo(n) {
var r = {
id: n.id,
id: n.id||n.module+"/"+n.name,
name: n.name,
types: n.types,
enabled: n.enabled
@ -220,7 +220,6 @@ function getNodeInfo(typeOrId) {
if (nodeTypeToId[typeOrId]) {
id = nodeTypeToId[typeOrId];
}
/* istanbul ignore else */
if (id) {
var module = moduleConfigs[getModule(id)];
@ -239,6 +238,23 @@ function getNodeInfo(typeOrId) {
return null;
}
function getFullNodeInfo(typeOrId) {
// Used by index.enableNodeSet so that .file can be retrieved to pass
// to loader.loadNodeSet
var id = typeOrId;
if (nodeTypeToId[typeOrId]) {
id = nodeTypeToId[typeOrId];
}
/* istanbul ignore else */
if (id) {
var module = moduleConfigs[getModule(id)];
if (module) {
return module.nodes[getNode(id)];
}
}
return null;
}
function getNodeList(filter) {
var list = [];
for (var module in moduleConfigs) {
@ -380,22 +396,18 @@ function enableNodeSet(typeOrId) {
if (nodeTypeToId[typeOrId]) {
id = nodeTypeToId[typeOrId];
}
var config;
try {
config = moduleConfigs[getModule(id)].nodes[getNode(id)];
delete config.err;
config.enabled = true;
//if (!config.loaded) {
// // TODO: honour the promise this returns
// loadNodeModule(config);
//}
nodeConfigCache = null;
saveNodeList();
return saveNodeList().then(function() {
return filterNodeInfo(config);
});
} catch (err) {
throw new Error("Unrecognised id: "+typeOrId);
}
return filterNodeInfo(config);
}
function disableNodeSet(typeOrId) {
@ -412,11 +424,12 @@ function disableNodeSet(typeOrId) {
// TODO: persist setting
config.enabled = false;
nodeConfigCache = null;
saveNodeList();
return saveNodeList().then(function() {
return filterNodeInfo(config);
});
} catch (err) {
throw new Error("Unrecognised id: "+id);
}
return filterNodeInfo(config);
}
function cleanModuleList() {
@ -472,6 +485,7 @@ var registry = module.exports = {
removeModule: removeModule,
getNodeInfo: getNodeInfo,
getFullNodeInfo: getFullNodeInfo,
getNodeList: getNodeList,
getModuleList: getModuleList,
getModuleInfo: getModuleInfo,

View File

@ -142,6 +142,19 @@ function reportRemovedModules(removedNodes) {
return removedNodes;
}
function installNode(file) {
return when.promise(function(resolve,reject) {
resolve(redNodes.addFile(file).then(function(info) {
var module = redNodes.getModuleInfo(info.module);
module.nodes = module.nodes.filter(function(d) {
return d.id==info.id;
});
return reportAddedModules(module);
}));
});
}
function installModule(module) {
//TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) {
@ -149,6 +162,14 @@ function installModule(module) {
reject(new Error("Invalid module name"));
return;
}
if (redNodes.getModuleInfo(module)) {
var err = new Error("Module already loaded");
err.code = "module_already_loaded";
reject(err);
return;
}
log.info("Installing module: "+module);
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module,
@ -252,6 +273,7 @@ var serverAPI = module.exports = {
reportRemovedModules: reportRemovedModules,
installModule: installModule,
uninstallModule: uninstallModule,
installNode: installNode,
get app() { return app },
get nodeApp() { return nodeApp },

View File

@ -528,7 +528,7 @@ describe("nodes api", function() {
return {id:"123",enabled: false};
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
return {id:"123",enabled: true,types:['a']};
return when.resolve({id:"123",enabled: true,types:['a']});
});
request(app)
@ -557,7 +557,7 @@ describe("nodes api", function() {
return {id:"123",enabled: true};
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
return {id:"123",enabled: false,types:['a']};
return when.resolve({id:"123",enabled: false,types:['a']});
});
request(app)
@ -587,11 +587,11 @@ describe("nodes api", function() {
return {id:"123",enabled: state};
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
return {id:"123",enabled: true,types:['a']};
return when.resolve({id:"123",enabled: true,types:['a']});
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
return {id:"123",enabled: false,types:['a']};
return when.resolve({id:"123",enabled: false,types:['a']});
});
request(app)
@ -633,11 +633,11 @@ describe("nodes api", function() {
return {id:"123",enabled: state, err:"foo" };
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
return {id:"123",enabled: true,types:['a']};
return when.resolve({id:"123",enabled: true,types:['a']});
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
return {id:"123",enabled: false,types:['a']};
return when.resolve({id:"123",enabled: false,types:['a']});
});
request(app)
@ -683,11 +683,11 @@ describe("nodes api", function() {
var enableNode = sinon.stub(redNodes,'enableNode');
enableNode.onFirstCall().returns((function() {
n1.enabled = true;
return n1;
return when.resolve(n1);
})());
enableNode.onSecondCall().returns((function() {
n2.enabled = true;
return n2;
return when.resolve(n2);
})());
enableNode.returns(null);
@ -724,11 +724,11 @@ describe("nodes api", function() {
var disableNode = sinon.stub(redNodes,'disableNode');
disableNode.onFirstCall().returns((function() {
n1.enabled = false;
return n1;
return when.resolve(n1);
})());
disableNode.onSecondCall().returns((function() {
n2.enabled = false;
return n2;
return when.resolve(n2);
})());
disableNode.returns(null);
@ -763,11 +763,11 @@ describe("nodes api", function() {
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
node.enabled = true;
return node;
return when.resolve(node);
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
node.enabled = false;
return node;
return when.resolve(node);
});
request(app)
@ -812,11 +812,11 @@ describe("nodes api", function() {
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
node.enabled = true;
return node;
return when.resolve(node);
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
node.enabled = false;
return node;
return when.resolve(node);
});
request(app)

View File

@ -837,28 +837,30 @@ describe('red/nodes/registry/index', function() {
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
var info = typeRegistry.disableNode(list[0].id);
info.should.have.property("id",list[0].id);
info.should.have.property("enabled",false);
typeRegistry.disableNode(list[0].id).then(function(info) {
info.should.have.property("id",list[0].id);
info.should.have.property("enabled",false);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
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.getNodeConfigs().length.should.equal(0);
var info2 = typeRegistry.enableNode(list[0].id);
info2.should.have.property("id",list[0].id);
info2.should.have.property("enabled",true);
typeRegistry.enableNode(list[0].id).then(function(info2) {
info2.should.have.property("id",list[0].id);
info2.should.have.property("enabled",true);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
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);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
done();
});
});
}).catch(function(e) {
done(e);
});
@ -879,30 +881,32 @@ describe('red/nodes/registry/index', function() {
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
var info = typeRegistry.disableNode(list[0].types[0]);
info.should.have.property("id",list[0].id);
info.should.have.property("types",list[0].types);
info.should.have.property("enabled",false);
typeRegistry.disableNode(list[0].types[0]).then(function(info) {;
info.should.have.property("id",list[0].id);
info.should.have.property("types",list[0].types);
info.should.have.property("enabled",false);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
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.getNodeConfigs().length.should.equal(0);
var info2 = typeRegistry.enableNode(list[0].types[0]);
info2.should.have.property("id",list[0].id);
info2.should.have.property("types",list[0].types);
info2.should.have.property("enabled",true);
typeRegistry.enableNode(list[0].types[0]).then(function(info2) {
info2.should.have.property("id",list[0].id);
info2.should.have.property("types",list[0].types);
info2.should.have.property("enabled",true);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
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);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
done();
});
});
}).catch(function(e) {
done(e);
});