Skip to content

Commit 9dad56a

Browse files
committed
add more functionality to connection parameters
1 parent 6da2560 commit 9dad56a

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lib/connection-parameters.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var dns = require('dns');
2+
var path = require('path');
23

34
var defaults = require(__dirname + '/defaults');
45

@@ -35,6 +36,7 @@ var ConnectionParameters = function(config) {
3536
this.password = val('password', config);
3637
this.binary = val('binary', config);
3738
this.ssl = config.ssl || defaults.ssl;
39+
//a domain socket begins with '/'
3840
this.isDomainSocket = (!(this.host||'').indexOf('/'));
3941
};
4042

@@ -54,7 +56,7 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
5456
params.push("dbname='" + this.database + "'");
5557
}
5658
if(this.isDomainSocket) {
57-
params.push("host=" + this.host);
59+
params.push("host=" + this.getDomainSocketName());
5860
return cb(null, params.join(' '));
5961
}
6062
dns.lookup(this.host, function(err, address) {
@@ -64,4 +66,14 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
6466
});
6567
};
6668

69+
ConnectionParameters.prototype.getDomainSocketName = function() {
70+
var filename = '.s.PGSQL.' + this.port;
71+
72+
//if host is full path to socket fd with port number, just return it
73+
if(this.host.indexOf(filename) > -1) return this.host;
74+
75+
//otherwise, build it from host + standard filename + port
76+
return path.join(this.host, filename);
77+
};
78+
6779
module.exports = ConnectionParameters;

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,20 @@ test('ConnectionParameters initializing from config', function() {
4848
});
4949

5050
test('initializing with unix domain socket', function() {
51-
var subject = new ConnectionParameters('/var/run/pg.sock');
51+
var subject = new ConnectionParameters('/var/run/');
5252
assert.ok(subject.isDomainSocket);
53-
assert.equal(subject.host, '/var/run/pg.sock');
53+
assert.equal(subject.host, '/var/run/');
54+
});
55+
56+
test('builds domain socket', function() {
57+
var subject = new ConnectionParameters({
58+
host: '/var/run/',
59+
port: 1234
60+
});
61+
assert.equal(subject.getDomainSocketName(), '/var/run/.s.PGSQL.1234');
62+
subject.host = '/tmp';
63+
assert.equal(subject.getDomainSocketName(), '/tmp/.s.PGSQL.1234');
64+
assert.equal(subject.getDomainSocketName(), '/tmp/.s.PGSQL.1234');
5465
});
5566

5667
test('libpq connection string building', function() {
@@ -113,14 +124,14 @@ test('libpq connection string building', function() {
113124
user: 'brian',
114125
password: 'asf',
115126
port: 5432,
116-
host: '/var/run/pgsockbla'
127+
host: '/tmp/'
117128
};
118129
var subject = new ConnectionParameters(config);
119130
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
120131
assert.isNull(err);
121132
var parts = constring.split(" ");
122133
checkForPart(parts, "user='brian'");
123-
checkForPart(parts, "host=/var/run/pgsockbla");
134+
checkForPart(parts, "host=/tmp/.s.PGSQL.5432");
124135
}));
125136
});
126137

@@ -138,4 +149,4 @@ test('libpq connection string building', function() {
138149
assert.equal(subject.password, sourceConfig.password);
139150
});
140151

141-
})
152+
});

0 commit comments

Comments
 (0)