Skip to content

Commit c7b12b2

Browse files
committed
Proper options normalization
1 parent df3799f commit c7b12b2

File tree

2 files changed

+30
-54
lines changed

2 files changed

+30
-54
lines changed

lib/frontend.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ function auth(type, x, options) {
9393
return auths[type](type, x, options) || ''
9494
}
9595

96-
function AuthenticationMD5Password(type, x, { user, password }) {
96+
function AuthenticationMD5Password(type, x, { user, pass }) {
9797
return bytes
9898
.p()
99-
.str('md5' + md5(Buffer.concat([Buffer.from(md5(password + user)), x.slice(9)])))
99+
.str('md5' + md5(Buffer.concat([Buffer.from(md5(pass + user)), x.slice(9)])))
100100
.z(1)
101101
.end()
102102
}
@@ -119,7 +119,7 @@ function SASLContinue(type, x, options) {
119119
const res = x.utf8Slice(9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})
120120

121121
const saltedPassword = crypto.pbkdf2Sync(
122-
options.password,
122+
options.pass,
123123
Buffer.from(res.s, 'base64'),
124124
parseInt(res.i), 32,
125125
'sha256'

lib/index.js

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os from 'os'
22
import crypto from 'crypto'
3-
import url from 'url'
3+
import Url from 'url'
44
import Connection from './connection.js'
55
import Queue from './queue.js'
66
import {
@@ -301,7 +301,6 @@ export default function Postgres(url, options) {
301301
if (ended)
302302
return ended
303303

304-
let c
305304
let destroy
306305

307306
if (timeout === 0)
@@ -384,53 +383,30 @@ export default function Postgres(url, options) {
384383
}
385384
}
386385

387-
function parseOptions(url, options = {}) {
388-
const env = process.env // eslint-disable-line
389-
390-
options = Object.assign({
391-
connection: {},
392-
host : env.PGHOST || 'localhost',
393-
port : env.PGPORT || 5432,
394-
database : env.PGDATABASE || 'postgres',
395-
username : env.PGUSERNAME || os.userInfo().username,
396-
password : env.PGPASSWORD || '',
397-
max : Math.max(1, os.cpus().length - 1),
398-
fifo : false,
399-
transform : x => x
400-
},
401-
typeof url === 'string' ? parseUrl(url) : url,
402-
options,
403-
{
404-
...mergeUserTypes(options.types),
405-
nonce: crypto.randomBytes(18).toString('base64')
406-
}
407-
)
408-
409-
options.user = String(options.username || options.user)
410-
options.pass = String(options.password || options.pass)
411-
options.host = String(options.hostname || options.host)
412-
options.database = options.db || options.database
413-
options.port = parseInt(options.port)
414-
415-
if ('application_name' in options.connection === false)
416-
options.connection.application_name = 'postgres.js'
417-
418-
if (!options.path && options.host.indexOf('/') > -1)
419-
options.path = options.host + '/.s.PGSQL.' + options.port
420-
421-
return options
422-
}
423-
424-
function parseUrl(x) {
425-
x = url.parse(x)
426-
427-
const config = {}
428-
429-
x.host && (config.host = x.hostname)
430-
x.port && (config.port = x.port)
431-
x.path && (config.database = x.path.slice(1))
432-
x.auth && (config.username = x.auth.split(':')[0])
433-
x.auth && (config.password = x.auth.split(':')[1])
434-
435-
return config
386+
function parseOptions(uri, options) {
387+
const o = options || uri
388+
, env = process.env // eslint-disable-line
389+
, url = options ? Url.parse(uri, true) : { query: {}, pathname: '' }
390+
, host = o.hostname || o.host || url.hostname || env.PGHOST || 'localhost'
391+
, port = o.port || url.port || env.PGPORT || 5432
392+
393+
return {
394+
host,
395+
port,
396+
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
397+
database : o.database || o.db || url.pathname.slice(1) || env.PGDATABASE || 'postgres',
398+
user : o.user || o.username || url.user || env.PGUSERNAME || os.userInfo().username,
399+
pass : o.pass || o.password || url.pass || env.PGPASSWORD || '',
400+
max : o.max || url.query.max || Math.max(1, os.cpus().length),
401+
fifo : o.fifo || url.query.fifo || false,
402+
transform : o.transform || (x => x),
403+
types : o.types || {},
404+
ssl : o.ssl || url.ssl || false,
405+
none : crypto.randomBytes(18).toString('base64'),
406+
connection : {
407+
application_name: 'postgres.js',
408+
...o.connection
409+
},
410+
...mergeUserTypes(o.types)
411+
}
436412
}

0 commit comments

Comments
 (0)