2016-06-24 23:56:43 +08:00
|
|
|
/**
|
2017-05-18 05:47:07 +08:00
|
|
|
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
2016-06-24 23:56:43 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* README.md file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 05:40:45 +08:00
|
|
|
var url = require('url');
|
2013-01-22 05:09:44 +08:00
|
|
|
var dns = require('dns');
|
|
|
|
|
2015-10-10 17:10:51 +08:00
|
|
|
var defaults = require('./defaults');
|
2013-01-18 08:14:13 +08:00
|
|
|
|
2014-01-06 01:08:58 +08:00
|
|
|
var val = function(key, config, envVar) {
|
|
|
|
if (envVar === undefined) {
|
|
|
|
envVar = process.env[ 'PG' + key.toUpperCase() ];
|
|
|
|
} else if (envVar === false) {
|
|
|
|
// do nothing ... use false
|
|
|
|
} else {
|
|
|
|
envVar = process.env[ envVar ];
|
|
|
|
}
|
|
|
|
|
2013-01-24 08:16:18 +08:00
|
|
|
return config[key] ||
|
2014-01-06 01:08:58 +08:00
|
|
|
envVar ||
|
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
|
|
|
|
|
|
|
//parses a connection string
|
2014-07-06 07:54:15 +08:00
|
|
|
var parse = require('pg-connection-string').parse;
|
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":
|
2014-06-12 09:18:11 +08:00
|
|
|
case "verify-ca":
|
2013-09-06 05:51:16 +08:00
|
|
|
case "verify-full":
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return defaults.ssl;
|
|
|
|
};
|
|
|
|
|
2013-01-18 08:27:48 +08:00
|
|
|
var ConnectionParameters = function(config) {
|
2016-06-08 05:09:22 +08:00
|
|
|
//if a string is passed, it is a raw connection string so we parse it into a config
|
2013-01-18 08:27:48 +08:00
|
|
|
config = typeof config == 'string' ? parse(config) : (config || {});
|
2016-06-08 05:09:22 +08:00
|
|
|
//if the config has a connectionString defined, parse IT into the config we use
|
|
|
|
//this will override other default values with what is stored in connectionString
|
|
|
|
if(config.connectionString) {
|
|
|
|
config = parse(config.connectionString);
|
|
|
|
}
|
2013-01-18 08:27:48 +08:00
|
|
|
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);
|
2016-07-11 05:26:36 +08:00
|
|
|
this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl;
|
2013-06-07 03:24:12 +08:00
|
|
|
this.client_encoding = val("client_encoding", config);
|
2017-04-25 02:24:30 +08:00
|
|
|
this.replication = val("replication", 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('/'));
|
2016-12-07 22:46:26 +08:00
|
|
|
this.keepAlive = config.keepAlive || false;
|
2014-01-06 01:08:58 +08:00
|
|
|
|
|
|
|
this.application_name = val('application_name', config, 'PGAPPNAME');
|
|
|
|
this.fallback_application_name = val('fallback_application_name', config, false);
|
2013-01-22 05:09:44 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 01:19:13 +08:00
|
|
|
// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
|
|
|
|
var quoteParamValue = function(value) {
|
|
|
|
return "'" + ('' + value).replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "'";
|
|
|
|
};
|
|
|
|
|
2013-01-22 05:09:44 +08:00
|
|
|
var add = function(params, config, paramName) {
|
|
|
|
var value = config[paramName];
|
|
|
|
if(value) {
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push(paramName + "=" + quoteParamValue(value));
|
2013-01-22 05:09:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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');
|
2014-01-06 01:08:58 +08:00
|
|
|
add(params, this, 'application_name');
|
|
|
|
add(params, this, 'fallback_application_name');
|
|
|
|
|
2017-04-19 22:55:56 +08:00
|
|
|
var ssl = typeof this.ssl === 'object' ? this.ssl : {sslmode: this.ssl};
|
|
|
|
add(params, ssl, 'sslmode');
|
|
|
|
add(params, ssl, 'sslca');
|
|
|
|
add(params, ssl, 'sslkey');
|
|
|
|
add(params, ssl, 'sslcert');
|
|
|
|
|
2013-01-22 05:09:44 +08:00
|
|
|
if(this.database) {
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push("dbname=" + quoteParamValue(this.database));
|
2013-01-22 05:09:44 +08:00
|
|
|
}
|
2017-04-25 02:24:30 +08:00
|
|
|
if(this.replication) {
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push("replication=" + quoteParamValue(this.replication));
|
2013-01-22 05:09:44 +08:00
|
|
|
}
|
2013-12-11 08:24:55 +08:00
|
|
|
if(this.host) {
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push("host=" + quoteParamValue(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) {
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push("client_encoding=" + quoteParamValue(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);
|
2017-05-16 01:19:13 +08:00
|
|
|
params.push("hostaddr=" + quoteParamValue(address));
|
2013-01-22 05:09:44 +08:00
|
|
|
return cb(null, params.join(' '));
|
|
|
|
});
|
2013-01-18 08:27:48 +08:00
|
|
|
};
|
2013-01-18 08:14:13 +08:00
|
|
|
|
|
|
|
module.exports = ConnectionParameters;
|