mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Support URL-encoded socket names
This commit is contained in:
parent
0ff40e733b
commit
b309db074f
10
index.js
10
index.js
@ -40,11 +40,17 @@ function parse(str) {
|
||||
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 && pathname.toLowerCase().startsWith('%2f')) {
|
||||
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);
|
||||
|
||||
|
||||
@ -133,6 +133,30 @@ describe('parse', function(){
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user