|
1 | 1 | import os from 'os'
|
2 | 2 | import crypto from 'crypto'
|
3 |
| -import url from 'url' |
| 3 | +import Url from 'url' |
4 | 4 | import Connection from './connection.js'
|
5 | 5 | import Queue from './queue.js'
|
6 | 6 | import {
|
@@ -301,7 +301,6 @@ export default function Postgres(url, options) {
|
301 | 301 | if (ended)
|
302 | 302 | return ended
|
303 | 303 |
|
304 |
| - let c |
305 | 304 | let destroy
|
306 | 305 |
|
307 | 306 | if (timeout === 0)
|
@@ -384,53 +383,30 @@ export default function Postgres(url, options) {
|
384 | 383 | }
|
385 | 384 | }
|
386 | 385 |
|
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 | + } |
436 | 412 | }
|
0 commit comments