Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function parse(str, options = {}) {
}

const pathname = result.pathname.slice(1) || null
config.database = pathname ? decodeURI(pathname) : null
config.database = pathname ? decodeURIComponent(pathname) : null

if (config.ssl === 'true' || config.ssl === '1') {
config.ssl = true
Expand Down
33 changes: 31 additions & 2 deletions packages/pg-connection-string/test/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,44 @@ describe('parse', function () {
subject.user?.should.equal('user')
subject.password?.should.equal('pass')
subject.host?.should.equal('localhost')
subject.database?.should.equal('%2Fdbname')
subject.database?.should.equal('/dbname')
})

it('decodes percent-encoded reserved characters in the database name', function () {
const cases: Array<[string, string]> = [
['my%23db', 'my#db'],
['my%2Bdb', 'my+db'],
['my%3Fdb', 'my?db'],
['my%2Fdb', 'my/db'],
['my%3Adb', 'my:db'],
['my%40db', 'my@db'],
['my%26db', 'my&db'],
['my%3Ddb', 'my=db'],
['my%2523db', 'my%23db'],
]
for (const [encoded, expected] of cases) {
const subject = parse(`postgres://user:pass@localhost/${encoded}`)
subject.database?.should.equal(expected, encoded)
}
})

it('keeps unescaped reserved characters in the database name', function () {
const subject = parse('postgres://user:pass@localhost/my+db:x@y&z=1')
subject.database?.should.equal('my+db:x@y&z=1')
})

it('decodes percent-encoded characters in the database name before the query string', function () {
const subject = parse('postgres://user:pass@localhost/my%23db?application_name=app')
subject.database?.should.equal('my#db')
subject.application_name?.should.equal('app')
})

it('configuration parameter host treats encoded host as part of the db name', function () {
const 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')
subject.database?.should.equal('/unix/socket/dbname')
})

it('configuration parameter application_name', function () {
Expand Down
Loading