Skip to content

Commit 4467d11

Browse files
authored
Fix connection uri encoding (#497)
1 parent be0e604 commit 4467d11

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/index.js

+11-1
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

+5
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ t('Connect using uri', async() =>
351351
})]
352352
)
353353

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

0 commit comments

Comments
 (0)