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
5 changes: 3 additions & 2 deletions packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ function parse(str, options = {}) {
let result
let dummyHost = false
if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
// Ensure spaces are encoded as %20
str = encodeURI(str).replace(/%25(\d\d)/g, '%$1')
// Ensure spaces are encoded as %20, and undo the double encoding of
// existing percent-escapes (e.g. %2F, %3a) that encodeURI also produces
str = encodeURI(str).replace(/%25([0-9a-f]{2})/gi, '%$1')
}

try {
Expand Down
12 changes: 12 additions & 0 deletions packages/pg-connection-string/test/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ describe('parse', function () {
subject.database?.should.equal(' u%20rl')
})

it('keeps percent-encoded characters when the url also contains a space', function () {
const subject = parse('postgres://app:s3cr%2Ft%3A@localhost/db?options=-c search_path=app')
subject.password?.should.equal('s3cr/t:')
subject.options?.should.equal('-c search_path=app')
})

it('keeps lowercase percent-encoded characters when the url also contains a stray percent sign', function () {
const subject = parse('postgres://app:s3cr%2bt@localhost/50%off')
subject.password?.should.equal('s3cr+t')
subject.database?.should.equal('50%off')
})

it('relative url sets database', function () {
const relative = 'different_db_on_default_host'
const subject = parse(relative)
Expand Down
Loading