Skip to content

Commit 9b89828

Browse files
authored
Merge pull request #34 from danielrozenberg/support-other-socket-modes
Support more modes to set the host as a socket
2 parents a483bdf + 7ec9b70 commit 9b89828

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ function parse(str) {
3535
config.client_encoding = result.query.encoding;
3636
return config;
3737
}
38-
config.host = result.hostname;
38+
if (!config.host) {
39+
// Only set the host if there is no equivalent query param.
40+
config.host = result.hostname;
41+
}
3942

43+
// If the host is missing it might be a URL-encoded path to a socket.
44+
var pathname = result.pathname;
45+
if (!config.host && pathname && /^%2f/i.test(pathname)) {
46+
var pathnameSplit = pathname.split('/');
47+
config.host = decodeURIComponent(pathnameSplit[0]);
48+
pathname = pathnameSplit.splice(1).join('/');
49+
}
4050
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
4151
// only strip the slash if it is present.
42-
var pathname = result.pathname;
4352
if (pathname && pathname.charAt(0) === '/') {
44-
pathname = result.pathname.slice(1) || null;
53+
pathname = pathname.slice(1) || null;
4554
}
4655
config.database = pathname && decodeURI(pathname);
4756

test/parse.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,43 @@ describe('parse', function(){
120120
(subject.database === null).should.equal(true);
121121
});
122122

123+
it('configuration parameter host', function() {
124+
var subject = parse('pg://user:pass@/dbname?host=/unix/socket');
125+
subject.user.should.equal('user');
126+
subject.password.should.equal('pass');
127+
subject.host.should.equal('/unix/socket');
128+
subject.database.should.equal('dbname');
129+
});
130+
131+
it('configuration parameter host overrides url host', function() {
132+
var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket');
133+
subject.host.should.equal('/unix/socket');
134+
});
135+
136+
it('url with encoded socket', function() {
137+
var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname');
138+
subject.user.should.equal('user');
139+
subject.password.should.equal('pass');
140+
subject.host.should.equal('/unix/socket');
141+
subject.database.should.equal('dbname');
142+
});
143+
144+
it('url with real host and an encoded db name', function() {
145+
var subject = parse('pg://user:pass@localhost/%2Fdbname');
146+
subject.user.should.equal('user');
147+
subject.password.should.equal('pass');
148+
subject.host.should.equal('localhost');
149+
subject.database.should.equal('%2Fdbname');
150+
});
151+
152+
it('configuration parameter host treats encoded socket as part of the db name', function() {
153+
var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname?host=localhost');
154+
subject.user.should.equal('user');
155+
subject.password.should.equal('pass');
156+
subject.host.should.equal('localhost');
157+
subject.database.should.equal('%2Funix%2Fsocket/dbname');
158+
});
159+
123160
it('configuration parameter application_name', function(){
124161
var connectionString = 'pg:///?application_name=TheApp';
125162
var subject = parse(connectionString);

0 commit comments

Comments
 (0)