Fix connection string parsing for overriden hosts (#2977)

* Add failing test

* Fix test

This corresponds to what was line 48 previously, see https://github.com/brianc/node-postgres/pull/2971/files#diff-08a5e82487ebd9b43751630019753901fae0a111f8d009ad2e9d194445e96922L48

* Update packages/pg-connection-string/index.js

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>

---------

Co-authored-by: Brian C <brian.m.carlson@gmail.com>
Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
This commit is contained in:
Adam Jones 2023-05-31 16:24:08 +01:00 committed by GitHub
parent 65406985b9
commit c38ecf3405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View File

@ -49,7 +49,8 @@ function parse(str) {
if (!config.host) {
// Only set the host if there is no equivalent query param.
config.host = decodeURIComponent(hostname)
} else if (hostname) {
} else if (hostname && /^%2f/i.test(hostname)) {
// Only prepend the hostname to the pathname if it is not a URL encoded Unix socket host.
result.pathname = hostname + result.pathname
}

View File

@ -149,6 +149,7 @@ describe('parse', function () {
it('configuration parameter host overrides url host', function () {
var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket')
subject.database.should.equal('dbname')
subject.host.should.equal('/unix/socket')
})