Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more modes to set the host as a socket #34

Merged
merged 3 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ function parse(str) {
config.client_encoding = result.query.encoding;
return config;
}
config.host = result.hostname;
if (!config.host) {
// Only set the host if there is no equivalent query param.
config.host = result.hostname;
}

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

Expand Down
37 changes: 37 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ describe('parse', function(){
(subject.database === null).should.equal(true);
});

it('configuration parameter host', function() {
var subject = parse('pg://user:pass@/dbname?host=/unix/socket');
subject.user.should.equal('user');
subject.password.should.equal('pass');
subject.host.should.equal('/unix/socket');
subject.database.should.equal('dbname');
});

it('configuration parameter host overrides url host', function() {
var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket');
subject.host.should.equal('/unix/socket');
});

it('url with encoded socket', function() {
var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname');
subject.user.should.equal('user');
subject.password.should.equal('pass');
subject.host.should.equal('/unix/socket');
subject.database.should.equal('dbname');
});

it('url with real host and an encoded db name', function() {
var subject = parse('pg://user:pass@localhost/%2Fdbname');
subject.user.should.equal('user');
subject.password.should.equal('pass');
subject.host.should.equal('localhost');
subject.database.should.equal('%2Fdbname');
});

it('configuration parameter host treats encoded socket as part of the db name', function() {
var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname?host=localhost');
subject.user.should.equal('user');
subject.password.should.equal('pass');
subject.host.should.equal('localhost');
subject.database.should.equal('%2Funix%2Fsocket/dbname');
});

it('configuration parameter application_name', function(){
var connectionString = 'pg:///?application_name=TheApp';
var subject = parse(connectionString);
Expand Down