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

Allow nodes to be installed by path name

This commit is contained in:
Nick O'Leary 2015-11-09 16:52:14 +00:00
parent 437b01a0ff
commit cb01920ee6
5 changed files with 96 additions and 76 deletions

View File

@ -59,20 +59,17 @@ module.exports = {
comms.publish("node/added",info.nodes,false); comms.publish("node/added",info.nodes,false);
if (node.module) { if (node.module) {
log.audit({event: "nodes.install",module:node.module},req); log.audit({event: "nodes.install",module:node.module},req);
res.json(redNodes.getModuleInfo(node.module)); res.json(info);
} else if (node.file) {
log.audit({event: "nodes.install",file:node.file},req);
res.json(info.nodes[0]);
} }
}).otherwise(function(err) { }).otherwise(function(err) {
if (err.code === 404) { if (err.code === 404) {
log.audit({event: "nodes.install",module:node.module,file:node.file,error:"not_found"},req); log.audit({event: "nodes.install",module:node.module,error:"not_found"},req);
res.status(404).end(); res.status(404).end();
} else if (err.code) { } else if (err.code) {
log.audit({event: "nodes.install",module:node.module,error:err.code},req); log.audit({event: "nodes.install",module:node.module,error:err.code},req);
res.status(400).json({error:err.code, message:err.message}); res.status(400).json({error:err.code, message:err.message});
} else { } else {
log.audit({event: "nodes.install",module:node.module,file:node.file,error:err.code||"unexpected_error",message:err.toString()},req); log.audit({event: "nodes.install",module:node.module,error:err.code||"unexpected_error",message:err.toString()},req);
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()}); res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
} }
}); });

View File

@ -28,37 +28,74 @@ var child_process = require('child_process');
var settings; var settings;
var moduleRe = /^[^/]+$/;
var slashRe = process.platform === "win32" ? /\\|[/]/ : /[/]/;
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
} }
function checkModulePath(folder) {
var moduleName;
var err;
var fullPath = path.resolve(folder);
var packageFile = path.join(fullPath,'package.json');
if (fs.existsSync(packageFile)) {
var pkg = require(packageFile);
moduleName = pkg.name;
if (!pkg['node-red']) {
// TODO: nls
err = new Error("Invalid Node-RED module");
err.code = 'invalid_module';
throw err;
}
} else {
err = new Error("Module not found");
err.code = 404;
throw err;
}
return moduleName;
}
function checkExistingModule(module) {
if (registry.getModuleInfo(module)) {
// TODO: nls
var err = new Error("Module already loaded");
err.code = "module_already_loaded";
throw err;
}
}
function installModule(module) { function installModule(module) {
//TODO: ensure module is 'safe' //TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) { var installName = module;
reject(new Error(log._("server.install.invalid")));
return; try {
} if (moduleRe.test(module)) {
if (registry.getModuleInfo(module)) { // Simple module name - assume it can be npm installed
// TODO: nls } else if (slashRe.test(module)) {
var err = new Error("Module already loaded"); // A path - check if there's a valid package.json
err.code = "module_already_loaded"; installName = module;
reject(err); module = checkModulePath(module);
return; }
checkExistingModule(module);
} catch(err) {
return reject(err);
} }
log.info(log._("server.install.installing",{name: module})); log.info(log._("server.install.installing",{name: module}));
var installDir = settings.userDir || process.env.NODE_RED_HOME || "."; var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.execFile('npm',['install','--production',module], var child = child_process.execFile('npm',['install','--production',installName],
{ {
cwd: installDir cwd: installDir
}, },
function(err, stdin, stdout) { function(err, stdin, stdout) {
if (err) { if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m"); var lookFor404 = new RegExp(" 404 .*"+installName+"$","m");
if (lookFor404.test(stdout)) { if (lookFor404.test(stdout)) {
log.warn(log._("server.install.install-failed-not-found",{name:module})); log.warn(log._("server.install.install-failed-not-found",{name:module}));
var e = new Error(); var e = new Error("Module not found");
e.code = 404; e.code = 404;
reject(e); reject(e);
} else { } else {

View File

@ -321,34 +321,6 @@ 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);
}
}
function loadNodeHelp(node,lang) { function loadNodeHelp(node,lang) {
var dir = path.dirname(node.template); var dir = path.dirname(node.template);
var base = path.basename(node.template); var base = path.basename(node.template);
@ -385,7 +357,6 @@ module.exports = {
init: init, init: init,
load: load, load: load,
addModule: addModule, addModule: addModule,
addFile: addFile,
loadNodeSet: loadNodeSet, loadNodeSet: loadNodeSet,
getNodeHelp: getNodeHelp getNodeHelp: getNodeHelp
} }

View File

@ -208,12 +208,11 @@ describe("nodes api", function() {
}); });
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo'); var getModuleInfo = sinon.stub(redNodes,'getModuleInfo');
getModuleInfo.onCall(0).returns(null); getModuleInfo.onCall(0).returns(null);
getModuleInfo.onCall(1).returns({
name:"foo",
nodes:[{id:"123"}]
});
var installModule = sinon.stub(redNodes,'installModule', function() { var installModule = sinon.stub(redNodes,'installModule', function() {
return when.resolve({id:"123"}); return when.resolve({
name:"foo",
nodes:[{id:"123"}]
});
}); });
request(app) request(app)

View File

@ -17,6 +17,7 @@
var should = require("should"); var should = require("should");
var sinon = require("sinon"); var sinon = require("sinon");
var when = require("when"); var when = require("when");
var path = require("path");
var child_process = require('child_process'); var child_process = require('child_process');
var installer = require("../../../../red/nodes/registry/installer"); var installer = require("../../../../red/nodes/registry/installer");
@ -28,33 +29,25 @@ describe('nodes/registry/installer', function() {
before(function() { before(function() {
installer.init({}); installer.init({});
}); });
afterEach(function() {
if (child_process.execFile.restore) {
child_process.execFile.restore();
}
})
describe("installs module", function() { describe("installs module", function() {
it("rejects invalid module names", function(done) {
var promises = [];
promises.push(installer.installModule("this_wont_exist "));
promises.push(installer.installModule("this_wont_exist;no_it_really_wont"));
when.settle(promises).then(function(results) {
results[0].state.should.be.eql("rejected");
results[1].state.should.be.eql("rejected");
done();
});
});
it("rejects when npm returns a 404", function(done) { it("rejects when npm returns a 404", function(done) {
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) { sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(new Error(),""," 404 this_wont_exist"); cb(new Error(),""," 404 this_wont_exist");
}); });
installer.installModule("this_wont_exist").otherwise(function(err) { installer.installModule("this_wont_exist").otherwise(function(err) {
err.code.should.be.eql(404); err.code.should.be.eql(404);
done(); done();
}).finally(function() {
exec.restore();
}); });
}); });
it("rejects with generic error", function(done) { it("rejects with generic error", function(done) {
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) { sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(new Error("test_error"),"",""); cb(new Error("test_error"),"","");
}); });
@ -62,13 +55,11 @@ describe('nodes/registry/installer', function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).otherwise(function(err) {
done(); done();
}).finally(function() {
exec.restore();
}); });
}); });
it("succeeds when module is found", function(done) { it("succeeds when module is found", function(done) {
var nodeInfo = {nodes:{module:"foo",types:["a"]}}; var nodeInfo = {nodes:{module:"foo",types:["a"]}};
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) { sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(null,"",""); cb(null,"","");
}); });
var addModule = sinon.stub(registry,"addModule",function(md) { var addModule = sinon.stub(registry,"addModule",function(md) {
@ -84,10 +75,37 @@ describe('nodes/registry/installer', function() {
}).otherwise(function(err) { }).otherwise(function(err) {
done(err); done(err);
}).finally(function() { }).finally(function() {
exec.restore();
addModule.restore(); addModule.restore();
}); });
}); });
it("rejects when non-existant path is provided", function(done) {
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","TestNodeModule","node_modules","NonExistant"));
installer.installModule(resourcesDir).then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
err.code.should.eql(404);
done();
});
});
it("succeeds when path is valid node-red module", function(done) {
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
var addModule = sinon.stub(registry,"addModule",function(md) {
return when.resolve(nodeInfo);
});
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","TestNodeModule","node_modules","TestNodeModule"));
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(null,"","");
});
installer.installModule(resourcesDir).then(function(info) {
info.should.eql(nodeInfo);
done();
}).otherwise(function(err) {
done(err);
}).finally(function() {
addModule.restore();
});
});
}); });
describe("uninstalls module", function() { describe("uninstalls module", function() {
it("rejects invalid module names", function(done) { it("rejects invalid module names", function(done) {
@ -106,7 +124,7 @@ describe('nodes/registry/installer', function() {
var removeModule = sinon.stub(registry,"removeModule",function(md) { var removeModule = sinon.stub(registry,"removeModule",function(md) {
return when.resolve(nodeInfo); return when.resolve(nodeInfo);
}); });
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) { sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(new Error("test_error"),"",""); cb(new Error("test_error"),"","");
}); });
@ -115,7 +133,6 @@ describe('nodes/registry/installer', function() {
}).otherwise(function(err) { }).otherwise(function(err) {
done(); done();
}).finally(function() { }).finally(function() {
exec.restore();
removeModule.restore(); removeModule.restore();
}); });
}); });
@ -127,7 +144,7 @@ describe('nodes/registry/installer', function() {
var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) { var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) {
return {nodes:[]}; return {nodes:[]};
}); });
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) { sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(null,"",""); cb(null,"","");
}); });
@ -142,7 +159,6 @@ describe('nodes/registry/installer', function() {
}).otherwise(function(err) { }).otherwise(function(err) {
done(err); done(err);
}).finally(function() { }).finally(function() {
exec.restore();
removeModule.restore(); removeModule.restore();
exists.restore(); exists.restore();
getModuleInfo.restore(); getModuleInfo.restore();