executor: fix port lookup

Use the global `isFinite` function to find the first valid port
number. This is the same behaviour as `_.isFinite` in lodash@2.x.

As a result, only values that are a valid number are accepted, e.g.
"0" is accepted but "0xy" is not.
This commit is contained in:
Miroslav Bajtoš 2015-04-22 09:08:51 +02:00
parent e3763ac28e
commit e5fd8c7975
2 changed files with 25 additions and 17 deletions

View File

@ -115,9 +115,7 @@ function setPort(app, instructions) {
process.env.npm_package_config_port, process.env.npm_package_config_port,
app.get('port'), app.get('port'),
3000 3000
], function isNumberLike(v) { ], isFinite);
return Number.isFinite(parseInt(v, 10));
});
if (port !== undefined) { if (port !== undefined) {
var portType = typeof port; var portType = typeof port;

View File

@ -294,6 +294,21 @@ describe('executor', function() {
}); });
describe('with PaaS and npm env variables', function() { describe('with PaaS and npm env variables', function() {
beforeEach(function cleanEnvironment() {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
delete process.env.npm_config_host;
delete process.env.OPENSHIFT_SLS_IP;
delete process.env.OPENSHIFT_NODEJS_IP;
delete process.env.HOST;
delete process.env.npm_package_config_host;
delete process.env.npm_config_port;
delete process.env.OPENSHIFT_SLS_PORT;
delete process.env.OPENSHIFT_NODEJS_PORT;
delete process.env.PORT;
delete process.env.npm_package_config_port;
});
function bootWithDefaults() { function bootWithDefaults() {
app = loopback(); app = loopback();
boot.execute(app, someInstructions({ boot.execute(app, someInstructions({
@ -322,7 +337,7 @@ describe('executor', function() {
assertHonored('PORT', 'HOST'); assertHonored('PORT', 'HOST');
}); });
it('should prioritize sources', function() { it('should prioritize host sources', function() {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
process.env.npm_config_host = randomHost(); process.env.npm_config_host = randomHost();
process.env.OPENSHIFT_SLS_IP = randomHost(); process.env.OPENSHIFT_SLS_IP = randomHost();
@ -332,13 +347,9 @@ describe('executor', function() {
bootWithDefaults(); bootWithDefaults();
assert.equal(app.get('host'), process.env.npm_config_host); assert.equal(app.get('host'), process.env.npm_config_host);
});
delete process.env.npm_config_host; it('should prioritize port sources', function() {
delete process.env.OPENSHIFT_SLS_IP;
delete process.env.OPENSHIFT_NODEJS_IP;
delete process.env.HOST;
delete process.env.npm_package_config_host;
process.env.npm_config_port = randomPort(); process.env.npm_config_port = randomPort();
process.env.OPENSHIFT_SLS_PORT = randomPort(); process.env.OPENSHIFT_SLS_PORT = randomPort();
process.env.OPENSHIFT_NODEJS_PORT = randomPort(); process.env.OPENSHIFT_NODEJS_PORT = randomPort();
@ -346,14 +357,7 @@ describe('executor', function() {
process.env.npm_package_config_port = randomPort(); process.env.npm_package_config_port = randomPort();
bootWithDefaults(); bootWithDefaults();
assert.equal(app.get('host'), process.env.npm_config_host);
assert.equal(app.get('port'), process.env.npm_config_port); assert.equal(app.get('port'), process.env.npm_config_port);
delete process.env.npm_config_port;
delete process.env.OPENSHIFT_SLS_PORT;
delete process.env.OPENSHIFT_NODEJS_PORT;
delete process.env.PORT;
delete process.env.npm_package_config_port;
}); });
function randomHost() { function randomHost() {
@ -373,6 +377,12 @@ describe('executor', function() {
boot.execute(app, someInstructions({ config: { port: undefined } })); boot.execute(app, someInstructions({ config: { port: undefined } }));
assert.equal(app.get('port'), 3000); assert.equal(app.get('port'), 3000);
}); });
it('should ignore non-numeric port values in ENV', function() {
process.env.PORT = '123invalid';
boot.execute(app, someInstructions({ config: { port: 3000 } }));
assert.equal(app.get('port'), 3000);
});
}); });
it('calls function exported by boot/init.js', function() { it('calls function exported by boot/init.js', function() {