mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
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:
parent
3644730d2b
commit
cf24ef28ee
@ -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
|
||||
|
||||
@ -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')
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user