2013-01-22 05:09:44 +08:00
|
|
|
var dns = require('dns');
|
2013-01-22 05:44:55 +08:00
|
|
|
var path = require('path');
|
2013-01-22 05:09:44 +08:00
|
|
|
|
2013-01-18 08:14:13 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
|
|
|
|
|
|
|
var val = function(key, config) {
|
2013-01-24 08:16:18 +08:00
|
|
|
return config[key] ||
|
|
|
|
process.env['PG' + key.toUpperCase()] ||
|
2013-01-18 08:14:13 +08:00
|
|
|
defaults[key];
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
2013-01-18 08:14:13 +08:00
|
|
|
|
|
|
|
var url = require('url');
|
|
|
|
//parses a connection string
|
|
|
|
var parse = function(str) {
|
2013-12-20 02:37:26 +08:00
|
|
|
var config;
|
2013-01-18 08:14:13 +08:00
|
|
|
//unix socket
|
|
|
|
if(str.charAt(0) === '/') {
|
2013-12-20 02:37:26 +08:00
|
|
|
config = str.split(' ');
|
|
|
|
return { host: config[0], database: config[1] };
|
2013-01-18 08:14:13 +08:00
|
|
|
}
|
2013-01-25 09:05:55 +08:00
|
|
|
// url parse expects spaces encoded as %20
|
2013-12-20 02:37:26 +08:00
|
|
|
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
|
2013-02-21 08:17:18 +08:00
|
|
|
var result = url.parse(str, true);
|
2013-12-20 02:37:26 +08:00
|
|
|
config = {};
|
|
|
|
if(result.protocol == 'socket:') {
|
|
|
|
config.host = decodeURI(result.pathname);
|
|
|
|
config.database = result.query.db;
|
|
|
|
config.client_encoding = result.query.encoding;
|
|
|
|
return config;
|
|
|
|
}
|
2013-01-18 08:14:13 +08:00
|
|
|
config.host = result.hostname;
|
2013-12-20 02:37:26 +08:00
|
|
|
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
|
2013-01-18 08:14:13 +08:00
|
|
|
var auth = (result.auth || ':').split(':');
|
|
|
|
config.user = auth[0];
|
|
|
|
config.password = auth[1];
|
|
|
|
config.port = result.port;
|
2013-02-21 08:17:18 +08:00
|
|
|
|
|
|
|
var ssl = result.query.ssl;
|
|
|
|
if (ssl === 'true' || ssl === '1') {
|
|
|
|
config.ssl = true;
|
|
|
|
}
|
|
|
|
|
2013-01-18 08:14:13 +08:00
|
|
|
return config;
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
|
|
|
|
2013-09-06 05:51:16 +08:00
|
|
|
var useSsl = function() {
|
|
|
|
switch(process.env.PGSSLMODE) {
|
|
|
|
case "disable":
|
|
|
|
return false;
|
|
|
|
case "prefer":
|
|
|
|
case "require":
|
|
|
|
case "verify-ca":
|
|
|
|
case "verify-full":
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return defaults.ssl;
|
|
|
|
};
|
|
|
|
|
2013-01-18 08:27:48 +08:00
|
|
|
var ConnectionParameters = function(config) {
|
|
|
|
config = typeof config == 'string' ? parse(config) : (config || {});
|
|
|
|
this.user = val('user', config);
|
|
|
|
this.database = val('database', config);
|
2013-01-21 21:36:45 +08:00
|
|
|
this.port = parseInt(val('port', config), 10);
|
2013-01-18 08:27:48 +08:00
|
|
|
this.host = val('host', config);
|
|
|
|
this.password = val('password', config);
|
|
|
|
this.binary = val('binary', config);
|
2013-09-06 05:51:16 +08:00
|
|
|
this.ssl = config.ssl || useSsl();
|
2013-06-07 03:24:12 +08:00
|
|
|
this.client_encoding = val("client_encoding", config);
|
2013-01-22 05:44:55 +08:00
|
|
|
//a domain socket begins with '/'
|
2013-01-22 05:09:44 +08:00
|
|
|
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) {
|
2013-01-25 05:07:53 +08:00
|
|
|
var params = [];
|
2013-01-22 05:09:44 +08:00
|
|
|
add(params, this, 'user');
|
|
|
|
add(params, this, 'password');
|
|
|
|
add(params, this, 'port');
|
|
|
|
if(this.database) {
|
|
|
|
params.push("dbname='" + this.database + "'");
|
|
|
|
}
|
2013-12-11 08:24:55 +08:00
|
|
|
if(this.host) {
|
2013-02-24 00:11:44 +08:00
|
|
|
params.push("host=" + this.host);
|
2013-12-11 08:24:55 +08:00
|
|
|
}
|
|
|
|
if(this.isDomainSocket) {
|
2013-01-22 05:09:44 +08:00
|
|
|
return cb(null, params.join(' '));
|
2013-01-25 05:07:53 +08:00
|
|
|
}
|
2013-06-07 03:06:52 +08:00
|
|
|
if(this.client_encoding) {
|
2013-06-07 03:16:36 +08:00
|
|
|
params.push("client_encoding='" + this.client_encoding + "'");
|
2013-06-07 03:06:52 +08:00
|
|
|
}
|
2013-01-22 05:09:44 +08:00
|
|
|
dns.lookup(this.host, function(err, address) {
|
|
|
|
if(err) return cb(err, null);
|
|
|
|
params.push("hostaddr=" + address);
|
|
|
|
return cb(null, params.join(' '));
|
|
|
|
});
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
2013-01-18 08:14:13 +08:00
|
|
|
|
|
|
|
module.exports = ConnectionParameters;
|