Skip to content

Commit 22d5adb

Browse files
committed
Merge pull request #497 from hoegaarden/application_name
application_name
2 parents ec08034 + 4f00e5a commit 22d5adb

File tree

5 files changed

+153
-19
lines changed

5 files changed

+153
-19
lines changed

lib/client.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,12 @@ Client.prototype.connect = function(callback) {
5252
if(self.ssl) {
5353
con.requestSsl();
5454
} else {
55-
con.startup({
56-
user: self.user,
57-
database: self.database
58-
});
55+
con.startup(self.getStartupConf());
5956
}
6057
});
6158

6259
con.on('sslconnect', function() {
63-
con.startup({
64-
user: self.user,
65-
database: self.database
66-
});
60+
con.startup(self.getStartupConf());
6761
});
6862

6963
function checkPgPass(cb) {
@@ -194,6 +188,23 @@ Client.prototype.connect = function(callback) {
194188

195189
};
196190

191+
Client.prototype.getStartupConf = function() {
192+
var params = this.connectionParameters;
193+
194+
var data = {
195+
user : params.user ,
196+
database : params.database
197+
// client_encoding : "'".concat(params.client_encoding).concat("'")
198+
};
199+
200+
var appName = params.application_name || params.fallback_application_name;
201+
if (appName) {
202+
data.application_name = appName;
203+
}
204+
205+
return data;
206+
};
207+
197208
Client.prototype.cancel = function(client, query) {
198209
if(client.activeQuery == query) {
199210
var con = this.connection;

lib/connection-parameters.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ var path = require('path');
33

44
var defaults = require(__dirname + '/defaults');
55

6-
var val = function(key, config) {
6+
var val = function(key, config, envVar) {
7+
if (envVar === undefined) {
8+
envVar = process.env[ 'PG' + key.toUpperCase() ];
9+
} else if (envVar === false) {
10+
// do nothing ... use false
11+
} else {
12+
envVar = process.env[ envVar ];
13+
}
14+
715
return config[key] ||
8-
process.env['PG' + key.toUpperCase()] ||
16+
envVar ||
917
defaults[key];
1018
};
1119

@@ -22,6 +30,14 @@ var parse = function(str) {
2230
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
2331
var result = url.parse(str, true);
2432
config = {};
33+
34+
if (result.query.application_name) {
35+
config.application_name = result.query.application_name;
36+
}
37+
if (result.query.fallback_application_name) {
38+
config.fallback_application_name = result.query.fallback_application_name;
39+
}
40+
2541
if(result.protocol == 'socket:') {
2642
config.host = decodeURI(result.pathname);
2743
config.database = result.query.db;
@@ -68,6 +84,9 @@ var ConnectionParameters = function(config) {
6884
this.client_encoding = val("client_encoding", config);
6985
//a domain socket begins with '/'
7086
this.isDomainSocket = (!(this.host||'').indexOf('/'));
87+
88+
this.application_name = val('application_name', config, 'PGAPPNAME');
89+
this.fallback_application_name = val('fallback_application_name', config, false);
7190
};
7291

7392
var add = function(params, config, paramName) {
@@ -82,6 +101,9 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
82101
add(params, this, 'user');
83102
add(params, this, 'password');
84103
add(params, this, 'port');
104+
add(params, this, 'application_name');
105+
add(params, this, 'fallback_application_name');
106+
85107
if(this.database) {
86108
params.push("dbname='" + this.database + "'");
87109
}

lib/connection.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,19 @@ Connection.prototype.requestSsl = function(config) {
117117
};
118118

119119
Connection.prototype.startup = function(config) {
120-
var bodyBuffer = this.writer
120+
var writer = this.writer
121121
.addInt16(3)
122122
.addInt16(0)
123-
.addCString('user')
124-
.addCString(config.user)
125-
.addCString('database')
126-
.addCString(config.database)
127-
.addCString('client_encoding')
128-
.addCString("'utf-8'")
129-
.addCString('').flush();
123+
;
124+
125+
Object.keys(config).forEach(function(key){
126+
var val = config[key];
127+
writer.addCString(key).addCString(val);
128+
});
129+
130+
writer.addCString('client_encoding').addCString("'utf-8'");
131+
132+
var bodyBuffer = writer.addCString('').flush();
130133
//this message is sent without a code
131134

132135
var length = bodyBuffer.length + 4;

lib/defaults.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ var defaults = module.exports = {
3838

3939
client_encoding: "",
4040

41-
ssl: false
41+
ssl: false,
42+
43+
application_name : undefined,
44+
fallback_application_name: undefined
4245
};
4346

4447
//parse int8 so you can get your count values as actual numbers
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var helper = require('./test-helper');
2+
var Client = helper.Client;
3+
4+
var conInfo = helper.config;
5+
6+
function getConInfo(override) {
7+
var newConInfo = {};
8+
Object.keys(conInfo).forEach(function(k){
9+
newConInfo[k] = conInfo[k];
10+
});
11+
Object.keys(override || {}).forEach(function(k){
12+
newConInfo[k] = override[k];
13+
});
14+
return newConInfo;
15+
}
16+
17+
function getAppName(conf, cb) {
18+
var client = new Client(conf);
19+
client.connect(assert.success(function(){
20+
client.query('SHOW application_name', assert.success(function(res){
21+
var appName = res.rows[0].application_name;
22+
cb(appName);
23+
client.end();
24+
}));
25+
}));
26+
}
27+
28+
test('No default appliation_name ', function(){
29+
var conf = getConInfo();
30+
getAppName(conf, function(res){
31+
assert.strictEqual(res, '');
32+
});
33+
});
34+
35+
test('fallback_application_name is used', function(){
36+
var fbAppName = 'this is my app';
37+
var conf = getConInfo({
38+
'fallback_application_name' : fbAppName
39+
});
40+
getAppName(conf, function(res){
41+
assert.strictEqual(res, fbAppName);
42+
});
43+
});
44+
45+
test('application_name is used', function(){
46+
var appName = 'some wired !@#$% application_name';
47+
var conf = getConInfo({
48+
'application_name' : appName
49+
});
50+
getAppName(conf, function(res){
51+
assert.strictEqual(res, appName);
52+
});
53+
});
54+
55+
test('application_name has precedence over fallback_application_name', function(){
56+
var appName = 'some wired !@#$% application_name';
57+
var fbAppName = 'some other strange $$test$$ appname';
58+
var conf = getConInfo({
59+
'application_name' : appName ,
60+
'fallback_application_name' : fbAppName
61+
});
62+
getAppName(conf, function(res){
63+
assert.strictEqual(res, appName);
64+
});
65+
});
66+
67+
test('application_name from connection string', function(){
68+
var appName = 'my app';
69+
var conParams = require(__dirname + '/../../../lib/connection-parameters');
70+
var conf;
71+
if (process.argv[2]) {
72+
conf = new conParams(process.argv[2]+'?application_name='+appName);
73+
} else {
74+
conf = 'postgres://?application_name='+appName;
75+
}
76+
getAppName(conf, function(res){
77+
assert.strictEqual(res, appName);
78+
});
79+
});
80+
81+
82+
83+
// TODO: make the test work for native client too
84+
if (!helper.args.native) {
85+
test('application_name is read from the env', function(){
86+
var appName = process.env.PGAPPNAME = 'testest';
87+
var conf = getConInfo({
88+
'just some bla' : 'to fool the pool'
89+
});
90+
getAppName(conf, function(res){
91+
delete process.env.PGAPPNAME;
92+
assert.strictEqual(res, appName);
93+
});
94+
});
95+
}

0 commit comments

Comments
 (0)