Skip to content

Commit

Permalink
Fix connection uri encoding (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnava-nsw authored and porsager committed Feb 1, 2023
1 parent 58fa7b5 commit 5fefec9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,18 @@ function parseUrl(url) {
host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]
host = decodeURIComponent(host.slice(host.indexOf('@') + 1))

const urlObj = new URL(url.replace(host, host.split(',')[0]))

return {
url: new URL(url.replace(host, host.split(',')[0])),
url: {
username: decodeURIComponent(urlObj.username),
password: decodeURIComponent(urlObj.password),
host: urlObj.host,
hostname: urlObj.hostname,
port: urlObj.port,
pathname: urlObj.pathname,
searchParams: urlObj.searchParams
},
multihost: host.indexOf(',') > -1 && host
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ t('Connect using uri', async() =>
})]
)

t('Options from uri with special characters in user and pass', async() => {
const opt = postgres({ user: 'öla', pass: 'pass^word' }).options
return [[opt.user, opt.pass].toString(), 'öla,pass^word']
})

t('Fail with proper error on no host', async() =>
['ECONNREFUSED', (await new Promise((resolve, reject) => {
const sql = postgres('postgres://localhost:33333/' + options.db, {
Expand Down

0 comments on commit 5fefec9

Please sign in to comment.