Skip to content

Commit 5fefec9

Browse files
vnava-nswporsager
authored andcommitted
Fix connection uri encoding (#497)
1 parent 58fa7b5 commit 5fefec9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,18 @@ function parseUrl(url) {
486486
host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]
487487
host = decodeURIComponent(host.slice(host.indexOf('@') + 1))
488488

489+
const urlObj = new URL(url.replace(host, host.split(',')[0]))
490+
489491
return {
490-
url: new URL(url.replace(host, host.split(',')[0])),
492+
url: {
493+
username: decodeURIComponent(urlObj.username),
494+
password: decodeURIComponent(urlObj.password),
495+
host: urlObj.host,
496+
hostname: urlObj.hostname,
497+
port: urlObj.port,
498+
pathname: urlObj.pathname,
499+
searchParams: urlObj.searchParams
500+
},
491501
multihost: host.indexOf(',') > -1 && host
492502
}
493503
}

tests/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ t('Connect using uri', async() =>
352352
})]
353353
)
354354

355+
t('Options from uri with special characters in user and pass', async() => {
356+
const opt = postgres({ user: 'öla', pass: 'pass^word' }).options
357+
return [[opt.user, opt.pass].toString(), 'öla,pass^word']
358+
})
359+
355360
t('Fail with proper error on no host', async() =>
356361
['ECONNREFUSED', (await new Promise((resolve, reject) => {
357362
const sql = postgres('postgres://localhost:33333/' + options.db, {

0 commit comments

Comments
 (0)