Skip to content

Commit ec0d0be

Browse files
committed
build libpq connection string & support domain socket
1 parent b9cedb2 commit ec0d0be

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

lib/connection-parameters.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var dns = require('dns');
2+
13
var defaults = require(__dirname + '/defaults');
24

35
var val = function(key, config) {
@@ -33,6 +35,33 @@ var ConnectionParameters = function(config) {
3335
this.password = val('password', config);
3436
this.binary = val('binary', config);
3537
this.ssl = config.ssl || defaults.ssl;
38+
this.isDomainSocket = (!(this.host||'').indexOf('/'));
39+
};
40+
41+
var add = function(params, config, paramName) {
42+
var value = config[paramName];
43+
if(value) {
44+
params.push(paramName+"='"+value+"'");
45+
}
46+
};
47+
48+
ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
49+
var params = []
50+
add(params, this, 'user');
51+
add(params, this, 'password');
52+
add(params, this, 'port');
53+
if(this.database) {
54+
params.push("dbname='" + this.database + "'");
55+
}
56+
if(this.isDomainSocket) {
57+
params.push("host=" + this.host);
58+
return cb(null, params.join(' '));
59+
}
60+
dns.lookup(this.host, function(err, address) {
61+
if(err) return cb(err, null);
62+
params.push("hostaddr=" + address);
63+
return cb(null, params.join(' '));
64+
});
3665
};
3766

3867
module.exports = ConnectionParameters;

test/unit/connection-parameters/creation-tests.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var compare = function(actual, expected, type) {
2626
test('ConnectionParameters initializing from defaults', function() {
2727
var subject = new ConnectionParameters();
2828
compare(subject, defaults, 'defaults');
29+
assert.ok(subject.isDomainSocket === false);
2930
});
3031

3132
test('ConnectionParameters initializing from config', function() {
@@ -43,4 +44,98 @@ test('ConnectionParameters initializing from config', function() {
4344
};
4445
var subject = new ConnectionParameters(config);
4546
compare(subject, config, 'config');
47+
assert.ok(subject.isDomainSocket === false);
4648
});
49+
50+
test('initializing with unix domain socket', function() {
51+
var subject = new ConnectionParameters('/var/run/pg.sock');
52+
assert.ok(subject.isDomainSocket);
53+
assert.equal(subject.host, '/var/run/pg.sock');
54+
});
55+
56+
test('libpq connection string building', function() {
57+
var checkForPart = function(array, part) {
58+
assert.ok(array.indexOf(part) > -1, array.join(" ") + " did not contain " + part);
59+
}
60+
61+
test('builds simple string', function() {
62+
var config = {
63+
user: 'brian',
64+
password: 'xyz',
65+
port: 888,
66+
host: 'localhost',
67+
database: 'bam'
68+
}
69+
var subject = new ConnectionParameters(config);
70+
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
71+
assert.isNull(err);
72+
var parts = constring.split(" ");
73+
checkForPart(parts, "user='brian'");
74+
checkForPart(parts, "password='xyz'");
75+
checkForPart(parts, "port='888'");
76+
checkForPart(parts, "hostaddr=127.0.0.1");
77+
checkForPart(parts, "dbname='bam'");
78+
}));
79+
});
80+
81+
test('builds dns string', function() {
82+
var config = {
83+
user: 'brian',
84+
password: 'asdf',
85+
port: 5432,
86+
host: 'localhost'
87+
};
88+
var subject = new ConnectionParameters(config);
89+
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
90+
assert.isNull(err);
91+
var parts = constring.split(" ");
92+
checkForPart(parts, "user='brian'");
93+
checkForPart(parts, "hostaddr=127.0.0.1");
94+
}));
95+
});
96+
97+
test('error when dns fails', function() {
98+
var config = {
99+
user: 'brian',
100+
password: 'asf',
101+
port: 5432,
102+
host: 'asdlfkjasldfkksfd#!$!!!!..com'
103+
};
104+
var subject = new ConnectionParameters(config);
105+
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
106+
assert.ok(err);
107+
assert.isNull(constring)
108+
}));
109+
});
110+
111+
test('connecting to unix domain socket', function() {
112+
var config = {
113+
user: 'brian',
114+
password: 'asf',
115+
port: 5432,
116+
host: '/var/run/pgsockbla'
117+
};
118+
var subject = new ConnectionParameters(config);
119+
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
120+
assert.isNull(err);
121+
var parts = constring.split(" ");
122+
checkForPart(parts, "user='brian'");
123+
checkForPart(parts, "host=/var/run/pgsockbla");
124+
}));
125+
});
126+
127+
test('password contains < and/or > characters', function () {
128+
return false;
129+
var sourceConfig = {
130+
user:'brian',
131+
password: 'hello<ther>e',
132+
port: 5432,
133+
host: 'localhost',
134+
database: 'postgres'
135+
}
136+
var connectionString = 'pg://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
137+
var subject = new ConnectionParameters(connectionString);
138+
assert.equal(subject.password, sourceConfig.password);
139+
});
140+
141+
})

0 commit comments

Comments
 (0)