Add support for (fallback_)application_name
This commit is contained in:
parent
ec08034221
commit
7fd79a41c3
@ -52,18 +52,12 @@ Client.prototype.connect = function(callback) {
|
||||
if(self.ssl) {
|
||||
con.requestSsl();
|
||||
} else {
|
||||
con.startup({
|
||||
user: self.user,
|
||||
database: self.database
|
||||
});
|
||||
con.startup(self.getStartupConf());
|
||||
}
|
||||
});
|
||||
|
||||
con.on('sslconnect', function() {
|
||||
con.startup({
|
||||
user: self.user,
|
||||
database: self.database
|
||||
});
|
||||
con.startup(self.getStartupConf());
|
||||
});
|
||||
|
||||
function checkPgPass(cb) {
|
||||
@ -194,6 +188,23 @@ Client.prototype.connect = function(callback) {
|
||||
|
||||
};
|
||||
|
||||
Client.prototype.getStartupConf = function() {
|
||||
var params = this.connectionParameters;
|
||||
|
||||
var data = {
|
||||
user : params.user ,
|
||||
database : params.database
|
||||
// client_encoding : "'".concat(params.client_encoding).concat("'")
|
||||
};
|
||||
|
||||
var appName = params.application_name || params.fallback_application_name;
|
||||
if (appName) {
|
||||
data.application_name = appName;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Client.prototype.cancel = function(client, query) {
|
||||
if(client.activeQuery == query) {
|
||||
var con = this.connection;
|
||||
|
@ -3,9 +3,17 @@ var path = require('path');
|
||||
|
||||
var defaults = require(__dirname + '/defaults');
|
||||
|
||||
var val = function(key, config) {
|
||||
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 ];
|
||||
}
|
||||
|
||||
return config[key] ||
|
||||
process.env['PG' + key.toUpperCase()] ||
|
||||
envVar ||
|
||||
defaults[key];
|
||||
};
|
||||
|
||||
@ -22,6 +30,14 @@ var parse = function(str) {
|
||||
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
|
||||
var result = url.parse(str, true);
|
||||
config = {};
|
||||
|
||||
if (result.query.application_name) {
|
||||
config.application_name = result.query.application_name;
|
||||
}
|
||||
if (result.query.fallback_application_name) {
|
||||
config.fallback_application_name = result.query.fallback_application_name;
|
||||
}
|
||||
|
||||
if(result.protocol == 'socket:') {
|
||||
config.host = decodeURI(result.pathname);
|
||||
config.database = result.query.db;
|
||||
@ -68,6 +84,9 @@ var ConnectionParameters = function(config) {
|
||||
this.client_encoding = val("client_encoding", config);
|
||||
//a domain socket begins with '/'
|
||||
this.isDomainSocket = (!(this.host||'').indexOf('/'));
|
||||
|
||||
this.application_name = val('application_name', config, 'PGAPPNAME');
|
||||
this.fallback_application_name = val('fallback_application_name', config, false);
|
||||
};
|
||||
|
||||
var add = function(params, config, paramName) {
|
||||
@ -82,6 +101,9 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
|
||||
add(params, this, 'user');
|
||||
add(params, this, 'password');
|
||||
add(params, this, 'port');
|
||||
add(params, this, 'application_name');
|
||||
add(params, this, 'fallback_application_name');
|
||||
|
||||
if(this.database) {
|
||||
params.push("dbname='" + this.database + "'");
|
||||
}
|
||||
|
@ -117,16 +117,19 @@ Connection.prototype.requestSsl = function(config) {
|
||||
};
|
||||
|
||||
Connection.prototype.startup = function(config) {
|
||||
var bodyBuffer = this.writer
|
||||
var writer = this.writer
|
||||
.addInt16(3)
|
||||
.addInt16(0)
|
||||
.addCString('user')
|
||||
.addCString(config.user)
|
||||
.addCString('database')
|
||||
.addCString(config.database)
|
||||
.addCString('client_encoding')
|
||||
.addCString("'utf-8'")
|
||||
.addCString('').flush();
|
||||
;
|
||||
|
||||
Object.keys(config).forEach(function(key){
|
||||
var val = config[key];
|
||||
writer.addCString(key).addCString(val);
|
||||
});
|
||||
|
||||
writer.addCString('client_encoding').addCString("'utf-8'");
|
||||
|
||||
var bodyBuffer = writer.addCString('').flush();
|
||||
//this message is sent without a code
|
||||
|
||||
var length = bodyBuffer.length + 4;
|
||||
|
@ -38,7 +38,10 @@ var defaults = module.exports = {
|
||||
|
||||
client_encoding: "",
|
||||
|
||||
ssl: false
|
||||
ssl: false,
|
||||
|
||||
application_name : undefined,
|
||||
fallback_application_name: undefined
|
||||
};
|
||||
|
||||
//parse int8 so you can get your count values as actual numbers
|
||||
|
89
test/integration/client/appname-tests.js
Normal file
89
test/integration/client/appname-tests.js
Normal file
@ -0,0 +1,89 @@
|
||||
var helper = require('./test-helper');
|
||||
var Client = helper.Client;
|
||||
|
||||
var conInfo = helper.config;
|
||||
|
||||
function getConInfo(override) {
|
||||
var newConInfo = {};
|
||||
Object.keys(conInfo).forEach(function(k){
|
||||
newConInfo[k] = conInfo[k];
|
||||
});
|
||||
Object.keys(override || {}).forEach(function(k){
|
||||
newConInfo[k] = override[k];
|
||||
});
|
||||
return newConInfo;
|
||||
}
|
||||
|
||||
function getAppName(conf, cb) {
|
||||
var client = new Client(conf);
|
||||
client.connect(assert.success(function(){
|
||||
client.query('SHOW application_name', assert.success(function(res){
|
||||
var appName = res.rows[0].application_name;
|
||||
cb(appName);
|
||||
client.end();
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
test('No default appliation_name ', function(){
|
||||
var conf = getConInfo();
|
||||
getAppName(conf, function(res){
|
||||
assert.strictEqual(res, '');
|
||||
});
|
||||
});
|
||||
|
||||
test('fallback_application_name is used', function(){
|
||||
var fbAppName = 'this is my app';
|
||||
var conf = getConInfo({
|
||||
'fallback_application_name' : fbAppName
|
||||
});
|
||||
getAppName(conf, function(res){
|
||||
assert.strictEqual(res, fbAppName);
|
||||
});
|
||||
});
|
||||
|
||||
test('application_name is used', function(){
|
||||
var appName = 'some wired !@#$% application_name';
|
||||
var conf = getConInfo({
|
||||
'application_name' : appName
|
||||
});
|
||||
getAppName(conf, function(res){
|
||||
assert.strictEqual(res, appName);
|
||||
});
|
||||
});
|
||||
|
||||
test('application_name has precedence over fallback_application_name', function(){
|
||||
var appName = 'some wired !@#$% application_name';
|
||||
var fbAppName = 'some other strange $$test$$ appname';
|
||||
var conf = getConInfo({
|
||||
'application_name' : appName ,
|
||||
'fallback_application_name' : fbAppName
|
||||
});
|
||||
getAppName(conf, function(res){
|
||||
assert.strictEqual(res, appName);
|
||||
});
|
||||
});
|
||||
|
||||
test('application_name from connection string', function(){
|
||||
var appName = 'my app';
|
||||
var conf = 'postgres:///?application_name='+appName;
|
||||
getAppName(conf, function(res){
|
||||
assert.strictEqual(res, appName);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
// TODO: make the test work for native client too
|
||||
if (!helper.args.native) {
|
||||
test('application_name is read from the env', function(){
|
||||
var appName = process.env.PGAPPNAME = 'testest';
|
||||
var conf = getConInfo({
|
||||
'just some bla' : 'to fool the pool'
|
||||
});
|
||||
getAppName(conf, function(res){
|
||||
delete process.env.PGAPPNAME;
|
||||
assert.strictEqual(res, appName);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user