pg-connection-string: avoid clobbering port from queryparams (#2833)

If the connection string is something like:
    postgresql://demo:password@/postgres?host=localhost&port=26258

Then the port from the query parameters should be used. Previously, the
parsing function would end up with a null port, and the default port
would end up being used by the connecetion package.
This commit is contained in:
Rafi Shamim 2023-07-21 12:57:02 -04:00 committed by GitHub
parent 3644730d2b
commit cf24ef28ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -38,7 +38,6 @@ function parse(str) {
config.user = config.user || decodeURIComponent(result.username)
config.password = config.password || decodeURIComponent(result.password)
config.port = result.port
if (result.protocol == 'socket:') {
config.host = decodeURI(result.pathname)
config.database = result.searchParams.get('db')
@ -53,6 +52,10 @@ function parse(str) {
// Only prepend the hostname to the pathname if it is not a URL encoded Unix socket host.
result.pathname = hostname + result.pathname
}
if (!config.port) {
// Only set the port if there is no equivalent query param.
config.port = result.port
}
const pathname = result.pathname.slice(1) || null
config.database = pathname ? decodeURI(pathname) : null

View File

@ -318,4 +318,10 @@ describe('parse', function () {
var subject = parse(connectionString)
subject.keepalives.should.equal('0')
})
it('use the port specified in the query parameters', function () {
var connectionString = 'postgres:///?host=localhost&port=1234'
var subject = parse(connectionString)
subject.port.should.equal('1234')
})
})