Merge pull request #252 from brianc/connection-parameters

Connection parameters
This commit is contained in:
Brian C 2013-01-23 20:29:52 -08:00
commit 07862726c0
11 changed files with 203 additions and 267 deletions

View File

@ -2,6 +2,7 @@ var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ConnectionParameters = require(__dirname + '/connection-parameters');
var Query = require(__dirname + '/query');
var utils = require(__dirname + '/utils');
var defaults = require(__dirname + '/defaults');
@ -10,20 +11,21 @@ var CopyFromStream = require(__dirname + '/copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/copystream').CopyToStream;
var Client = function(config) {
EventEmitter.call(this);
if(typeof config === 'string') {
config = utils.normalizeConnectionInfo(config)
}
this.connectionParameters = new ConnectionParameters(config);
this.user = this.connectionParameters.user;
this.database = this.connectionParameters.database;
this.port = this.connectionParameters.port;
this.host = this.connectionParameters.host;
this.password = this.connectionParameters.password;
config = config || {};
this.user = config.user || defaults.user;
this.database = config.database || defaults.database;
this.port = config.port || defaults.port;
this.host = config.host || defaults.host;
this.connection = config.connection || new Connection({
stream: config.stream,
ssl: config.ssl
});
this.queryQueue = [];
this.password = config.password || defaults.password;
this.binary = config.binary || defaults.binary;
this.encoding = 'utf8';
this.processID = null;

View File

@ -1,3 +1,6 @@
var dns = require('dns');
var path = require('path');
var defaults = require(__dirname + '/defaults');
var val = function(key, config) {
@ -33,6 +36,44 @@ var ConnectionParameters = function(config) {
this.password = val('password', config);
this.binary = val('binary', config);
this.ssl = config.ssl || defaults.ssl;
//a domain socket begins with '/'
this.isDomainSocket = (!(this.host||'').indexOf('/'));
};
var add = function(params, config, paramName) {
var value = config[paramName];
if(value) {
params.push(paramName+"='"+value+"'");
}
};
ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
var params = []
add(params, this, 'user');
add(params, this, 'password');
add(params, this, 'port');
if(this.database) {
params.push("dbname='" + this.database + "'");
}
if(this.isDomainSocket) {
params.push("host=" + this.getDomainSocketName());
return cb(null, params.join(' '));
}
dns.lookup(this.host, function(err, address) {
if(err) return cb(err, null);
params.push("hostaddr=" + address);
return cb(null, params.join(' '));
});
};
ConnectionParameters.prototype.getDomainSocketName = function() {
var filename = '.s.PGSQL.' + this.port;
//if host is full path to socket fd with port number, just return it
if(this.host.indexOf(filename) > -1) return this.host;
//otherwise, build it from host + standard filename + port
return path.join(this.host, filename);
};
module.exports = ConnectionParameters;

View File

@ -1,5 +1,7 @@
//require the c++ bindings & export to javascript
var EventEmitter = require('events').EventEmitter;
var ConnectionParameters = require(__dirname + '/../connection-parameters');
var utils = require(__dirname + "/../utils");
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
@ -28,7 +30,7 @@ var nativeConnect = p.connect;
p.connect = function(cb) {
var self = this;
utils.buildLibpqConnectionString(this._config, function(err, conString) {
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
if(err) {
return cb ? cb(err) : self.emit('error', err);
}
@ -143,13 +145,13 @@ var clientBuilder = function(config) {
connection._queryQueue = [];
connection._namedQueries = {};
connection._activeQuery = null;
connection._config = utils.normalizeConnectionInfo(config);
connection.connectionParameters = new ConnectionParameters(config);
//attach properties to normalize interface with pure js client
connection.user = connection._config.user;
connection.password = connection._config.password;
connection.database = connection._config.database;
connection.host = connection._config.host;
connection.port = connection._config.port;
connection.user = connection.connectionParameters.user;
connection.password = connection.connectionParameters.password;
connection.database = connection.connectionParameters.database;
connection.host = connection.connectionParameters.host;
connection.port = connection.connectionParameters.port;
connection.on('connect', function() {
connection._connected = true;
connection._pulseQueryQueue(true);

View File

@ -13,88 +13,6 @@ if(typeof events.EventEmitter.prototype.once !== 'function') {
};
}
var parseConnectionString = function(str) {
//unix socket
if(str.charAt(0) === '/') {
return { host: str };
}
var result = url.parse(str);
var config = {};
config.host = result.hostname;
config.database = result.pathname ? result.pathname.slice(1) : null
var auth = (result.auth || ':').split(':');
config.user = auth[0];
config.password = auth[1];
config.port = result.port;
return config;
};
//allows passing false as property to remove it from config
var norm = function(config, propName) {
config[propName] = (config[propName] || (config[propName] === false ? undefined : defaults[propName]))
};
//normalizes connection info
//which can be in the form of an object
//or a connection string
var normalizeConnectionInfo = function(config) {
switch(typeof config) {
case 'object':
norm(config, 'user');
norm(config, 'password');
norm(config, 'host');
norm(config, 'port');
norm(config, 'database');
return config;
case 'string':
return normalizeConnectionInfo(parseConnectionString(config));
default:
throw new Error("Unrecognized connection config parameter: " + config);
}
};
var add = function(params, config, paramName) {
var value = config[paramName];
if(value) {
params.push(paramName+"='"+value+"'");
}
}
//builds libpq specific connection string
//from a supplied config object
//the config object conforms to the interface of the config object
//accepted by the pure javascript client
var getLibpgConString = function(config, callback) {
if(typeof config == 'object') {
var params = []
add(params, config, 'user');
add(params, config, 'password');
add(params, config, 'port');
if(config.database) {
params.push("dbname='" + config.database + "'");
}
if(config.host) {
if (!config.host.indexOf("/")) {
params.push("host=" + config.host);
} else {
if(config.host != 'localhost' && config.host != '127.0.0.1') {
//do dns lookup
return require('dns').lookup(config.host, function(err, address) {
if(err) return callback(err, null);
params.push("hostaddr="+address)
callback(null, params.join(" "))
})
}
params.push("hostaddr=127.0.0.1 ");
}
}
callback(null, params.join(" "));
} else {
throw new Error("Unrecognized config type for connection");
}
}
//converts values from javascript types
//to their 'raw' counterparts for use as a postgres parameter
//note: you can override this function to provide your own conversion mechanism
@ -126,12 +44,6 @@ function normalizeQueryConfig (config, values, callback) {
}
module.exports = {
normalizeConnectionInfo: normalizeConnectionInfo,
//only exported here to make testing of this method possible
//since it contains quite a bit of logic and testing for
//each connection scenario in an integration test is impractical
buildLibpqConnectionString: getLibpgConString,
parseConnectionString: parseConnectionString,
prepareValue: prepareValue,
normalizeQueryConfig: normalizeQueryConfig
}

View File

@ -494,7 +494,7 @@ protected:
}
if (copied == 0) {
//wait for next read ready
//result was not handled copmpletely
//result was not handled completely
return false;
} else if (copied == -1) {
this->copyOutMode_ = false;
@ -503,6 +503,7 @@ protected:
this->copyOutMode_ = false;
return true;
}
return false;
}
bool HandleResult(PGresult* result)
{
@ -546,6 +547,7 @@ protected:
printf("YOU SHOULD NEVER SEE THIS! PLEASE OPEN AN ISSUE ON GITHUB! Unrecogized query status: %s\n", PQresStatus(status));
break;
}
return true;
}
void EmitCommandMetaData(PGresult* result)

View File

@ -1,14 +1,5 @@
var config = {};
if(process.argv[2]) {
config = require(__dirname + '/../lib/utils').parseConnectionString(process.argv[2]);
}
//TODO use these environment variables in lib/ code
//http://www.postgresql.org/docs/8.4/static/libpq-envars.html
config.host = config.host || process.env['PGHOST'] || process.env['PGHOSTADDR'];
config.port = config.port || process.env['PGPORT'];
config.database = config.database || process.env['PGDATABASE'];
config.user = config.user || process.env['PGUSER'];
config.password = config.password || process.env['PGPASSWORD'];
var ConnectionParameters = require(__dirname + '/../lib/connection-parameters');
var config = new ConnectionParameters(process.argv[2]);
for(var i = 0; i < process.argv.length; i++) {
switch(process.argv[i].toLowerCase()) {

View File

@ -1,6 +1,13 @@
var helper = require(__dirname + '/test-helper');
var pg = helper.pg;
//clear process.env
var realEnv = {};
for(var key in process.env) {
realEnv[key] = process.env[key];
if(!key.indexOf('PG')) delete process.env[key];
}
test('default values', function() {
assert.same(pg.defaults,{
user: process.env.USER,
@ -44,3 +51,8 @@ if(!helper.args.native) {
})
}
//restore process.env
for(var key in realEnv) {
process.env[key] = realEnv[key];
}

View File

@ -58,8 +58,10 @@ test('COPY TO', function () {
});
});
});
test('COPY TO, queue queries', function () {
pg.connect(helper.config, function (error, client) {
if(helper.config.native) return false;
pg.connect(helper.config, assert.calls(function (error, client) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
var query1Done = false,
@ -73,7 +75,7 @@ test('COPY TO, queue queries', function () {
//imitate long query, to make impossible,
//that copy query end callback runs after
//second query callback
client.query("SELECT pg_sleep(5)", function () {
client.query("SELECT pg_sleep(1)", function () {
query2Done = true;
assert.ok(copyQueryDone && query2Done, "second query has to be executed after others");
});
@ -93,15 +95,17 @@ test('COPY TO, queue queries', function () {
pg.end(helper.config);
}, "COPY IN stream should emit end event after all rows");
});
});
}));
});
test("COPY TO incorrect usage with large data", function () {
if(helper.config.native) return false;
//when many data is loaded from database (and it takes a lot of time)
//there are chance, that query will be canceled before it ends
//but if there are not so much data, cancel message may be
//send after copy query ends
//so we need to test both situations
pg.connect(helper.config, function (error, client) {
pg.connect(helper.config, assert.calls(function (error, client) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//this has to report error in standart way, instead of just throwing exception
@ -116,10 +120,12 @@ test("COPY TO incorrect usage with large data", function () {
}));
})
);
});
}));
});
test("COPY TO incorrect usage with small data", function () {
pg.connect(helper.config, function (error, client) {
if(helper.config.native) return false;
pg.connect(helper.config, assert.calls(function (error, client) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//this has to report error in standart way, instead of just throwing exception
@ -134,7 +140,7 @@ test("COPY TO incorrect usage with small data", function () {
}));
})
);
});
}));
});
test("COPY FROM incorrect usage", function () {

View File

@ -4,8 +4,8 @@ test('client settings', function() {
test('defaults', function() {
var client = new Client();
assert.equal(client.user, process.env.USER);
assert.equal(client.database, process.env.USER);
assert.equal(client.user, process.env['PGUSER'] || process.env.USER);
assert.equal(client.database, process.env['PGDATABASE'] || process.env.USER);
assert.equal(client.port, 5432);
});
@ -41,11 +41,11 @@ test('initializing from a config string', function() {
test('when not including all values the defaults are used', function() {
var client = new Client("pg://host1")
assert.equal(client.user, process.env.USER)
assert.equal(client.password, null)
assert.equal(client.user, process.env['PGUSER'] || process.env.USER)
assert.equal(client.password, process.env['PGPASSWORD'] || null)
assert.equal(client.host, "host1")
assert.equal(client.port, 5432)
assert.equal(client.database, process.env.USER)
assert.equal(client.port, process.env['PGPORT'] || 5432)
assert.equal(client.database, process.env['PGDATABASE'] || process.env.USER)
})

View File

@ -26,6 +26,7 @@ var compare = function(actual, expected, type) {
test('ConnectionParameters initializing from defaults', function() {
var subject = new ConnectionParameters();
compare(subject, defaults, 'defaults');
assert.ok(subject.isDomainSocket === false);
});
test('ConnectionParameters initializing from config', function() {
@ -43,4 +44,109 @@ test('ConnectionParameters initializing from config', function() {
};
var subject = new ConnectionParameters(config);
compare(subject, config, 'config');
assert.ok(subject.isDomainSocket === false);
});
test('initializing with unix domain socket', function() {
var subject = new ConnectionParameters('/var/run/');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/var/run/');
});
test('builds domain socket', function() {
var subject = new ConnectionParameters({
host: '/var/run/',
port: 1234
});
assert.equal(subject.getDomainSocketName(), '/var/run/.s.PGSQL.1234');
subject.host = '/tmp';
assert.equal(subject.getDomainSocketName(), '/tmp/.s.PGSQL.1234');
assert.equal(subject.getDomainSocketName(), '/tmp/.s.PGSQL.1234');
});
test('libpq connection string building', function() {
var checkForPart = function(array, part) {
assert.ok(array.indexOf(part) > -1, array.join(" ") + " did not contain " + part);
}
test('builds simple string', function() {
var config = {
user: 'brian',
password: 'xyz',
port: 888,
host: 'localhost',
database: 'bam'
}
var subject = new ConnectionParameters(config);
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'");
checkForPart(parts, "password='xyz'");
checkForPart(parts, "port='888'");
checkForPart(parts, "hostaddr=127.0.0.1");
checkForPart(parts, "dbname='bam'");
}));
});
test('builds dns string', function() {
var config = {
user: 'brian',
password: 'asdf',
port: 5432,
host: 'localhost'
};
var subject = new ConnectionParameters(config);
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'");
checkForPart(parts, "hostaddr=127.0.0.1");
}));
});
test('error when dns fails', function() {
var config = {
user: 'brian',
password: 'asf',
port: 5432,
host: 'asdlfkjasldfkksfd#!$!!!!..com'
};
var subject = new ConnectionParameters(config);
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
assert.ok(err);
assert.isNull(constring)
}));
});
test('connecting to unix domain socket', function() {
var config = {
user: 'brian',
password: 'asf',
port: 5432,
host: '/tmp/'
};
var subject = new ConnectionParameters(config);
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'");
checkForPart(parts, "host=/tmp/.s.PGSQL.5432");
}));
});
test('password contains < and/or > characters', function () {
return false;
var sourceConfig = {
user:'brian',
password: 'hello<ther>e',
port: 5432,
host: 'localhost',
database: 'postgres'
}
var connectionString = 'pg://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
var subject = new ConnectionParameters(connectionString);
assert.equal(subject.password, sourceConfig.password);
});
});

View File

@ -21,144 +21,6 @@ test("EventEmitter.once", function(t) {
});
test('normalizing connection info', function() {
test('with objects', function() {
test('empty object uses defaults', function() {
var input = {};
var output = utils.normalizeConnectionInfo(input);
assert.equal(output.user, defaults.user);
assert.equal(output.database, defaults.database);
assert.equal(output.port, defaults.port);
assert.equal(output.host, defaults.host);
assert.equal(output.password, defaults.password);
});
test('full object ignores defaults', function() {
var input = {
user: 'test1',
database: 'test2',
port: 'test3',
host: 'test4',
password: 'test5'
};
assert.equal(utils.normalizeConnectionInfo(input), input);
});
test('connection string', function() {
test('non-unix socket', function() {
test('uses defaults', function() {
var input = "";
var output = utils.normalizeConnectionInfo(input);
assert.equal(output.user, defaults.user);
assert.equal(output.database, defaults.database);
assert.equal(output.port, defaults.port);
assert.equal(output.host, defaults.host);
assert.equal(output.password, defaults.password);
});
test('ignores defaults if string contains them all', function() {
var input = "tcp://user1:pass2@host3:3333/databaseName";
var output = utils.normalizeConnectionInfo(input);
assert.equal(output.user, 'user1');
assert.equal(output.database, 'databaseName');
assert.equal(output.port, 3333);
assert.equal(output.host, 'host3');
assert.equal(output.password, 'pass2');
})
});
test('unix socket', function() {
test('uses defaults', function() {
var input = "/var/run/postgresql";
var output = utils.normalizeConnectionInfo(input);
assert.equal(output.user, process.env.USER);
assert.equal(output.host, '/var/run/postgresql');
assert.equal(output.database, process.env.USER);
assert.equal(output.port, 5432);
});
test('uses overridden defaults', function() {
defaults.host = "/var/run/postgresql";
defaults.user = "boom";
defaults.password = "yeah";
defaults.port = 1234;
var output = utils.normalizeConnectionInfo("asdf");
assert.equal(output.user, "boom");
assert.equal(output.password, "yeah");
assert.equal(output.port, 1234);
assert.equal(output.host, "/var/run/postgresql");
})
})
})
})
})
test('libpq connection string building', function() {
var checkForPart = function(array, part) {
assert.ok(array.indexOf(part) > -1, array.join(" ") + " did not contain " + part);
}
test('builds simple string', function() {
var config = {
user: 'brian',
password: 'xyz',
port: 888,
host: 'localhost',
database: 'bam'
}
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
assert.isNull(err)
var parts = constring.split(" ");
checkForPart(parts, "user='brian'")
checkForPart(parts, "password='xyz'")
checkForPart(parts, "port='888'")
checkForPart(parts, "hostaddr=127.0.0.1")
checkForPart(parts, "dbname='bam'")
}))
})
test('builds dns string', function() {
var config = {
user: 'brian',
password: 'asdf',
port: 5432,
host: 'localhost'
}
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'")
checkForPart(parts, "hostaddr=127.0.0.1")
}))
})
test('error when dns fails', function() {
var config = {
user: 'brian',
password: 'asf',
port: 5432,
host: 'asdlfkjasldfkksfd#!$!!!!..com'
}
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
assert.ok(err);
assert.isNull(constring)
}))
})
test('password contains < and/or > characters', function () {
return false;
var sourceConfig = {
user:'brian',
password: 'hello<ther>e',
port: 5432,
host: 'localhost',
database: 'postgres'
}
var connectionString = 'pg://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
var config = utils.parseConnectionString(connectionString);
assert.same(config, sourceConfig);
});
})
test('types are exported', function() {
var pg = require(__dirname + '/../../lib/index');
assert.ok(pg.types);