1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var dns = require('dns')
|
11 |
|
12 | var defaults = require('./defaults')
|
13 |
|
14 | var parse = require('pg-connection-string').parse
|
15 |
|
16 | var val = function (key, config, envVar) {
|
17 | if (envVar === undefined) {
|
18 | envVar = process.env['PG' + key.toUpperCase()]
|
19 | } else if (envVar === false) {
|
20 |
|
21 | } else {
|
22 | envVar = process.env[envVar]
|
23 | }
|
24 |
|
25 | return config[key] || envVar || defaults[key]
|
26 | }
|
27 |
|
28 | var readSSLConfigFromEnvironment = function () {
|
29 | switch (process.env.PGSSLMODE) {
|
30 | case 'disable':
|
31 | return false
|
32 | case 'prefer':
|
33 | case 'require':
|
34 | case 'verify-ca':
|
35 | case 'verify-full':
|
36 | return true
|
37 | case 'no-verify':
|
38 | return { rejectUnauthorized: false }
|
39 | }
|
40 | return defaults.ssl
|
41 | }
|
42 |
|
43 | var ConnectionParameters = function (config) {
|
44 |
|
45 | config = typeof config === 'string' ? parse(config) : config || {}
|
46 |
|
47 |
|
48 |
|
49 | if (config.connectionString) {
|
50 | config = Object.assign({}, config, parse(config.connectionString))
|
51 | }
|
52 |
|
53 | this.user = val('user', config)
|
54 | this.database = val('database', config)
|
55 |
|
56 | if (this.database === undefined) {
|
57 | this.database = this.user
|
58 | }
|
59 |
|
60 | this.port = parseInt(val('port', config), 10)
|
61 | this.host = val('host', config)
|
62 |
|
63 |
|
64 |
|
65 | Object.defineProperty(this, 'password', {
|
66 | configurable: true,
|
67 | enumerable: false,
|
68 | writable: true,
|
69 | value: val('password', config),
|
70 | })
|
71 |
|
72 | this.binary = val('binary', config)
|
73 |
|
74 | this.ssl = typeof config.ssl === 'undefined' ? readSSLConfigFromEnvironment() : config.ssl
|
75 |
|
76 |
|
77 | if (this.ssl === 'no-verify') {
|
78 | this.ssl = { rejectUnauthorized: false }
|
79 | }
|
80 |
|
81 | this.client_encoding = val('client_encoding', config)
|
82 | this.replication = val('replication', config)
|
83 |
|
84 | this.isDomainSocket = !(this.host || '').indexOf('/')
|
85 |
|
86 | this.application_name = val('application_name', config, 'PGAPPNAME')
|
87 | this.fallback_application_name = val('fallback_application_name', config, false)
|
88 | this.statement_timeout = val('statement_timeout', config, false)
|
89 | this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
|
90 | this.query_timeout = val('query_timeout', config, false)
|
91 |
|
92 | if (config.connectionTimeoutMillis === undefined) {
|
93 | this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
|
94 | } else {
|
95 | this.connect_timeout = Math.floor(config.connectionTimeoutMillis / 1000)
|
96 | }
|
97 |
|
98 | if (config.keepAlive === false) {
|
99 | this.keepalives = 0
|
100 | } else if (config.keepAlive === true) {
|
101 | this.keepalives = 1
|
102 | }
|
103 |
|
104 | if (typeof config.keepAliveInitialDelayMillis === 'number') {
|
105 | this.keepalives_idle = Math.floor(config.keepAliveInitialDelayMillis / 1000)
|
106 | }
|
107 | }
|
108 |
|
109 |
|
110 | var quoteParamValue = function (value) {
|
111 | return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
|
112 | }
|
113 |
|
114 | var add = function (params, config, paramName) {
|
115 | var value = config[paramName]
|
116 | if (value !== undefined && value !== null) {
|
117 | params.push(paramName + '=' + quoteParamValue(value))
|
118 | }
|
119 | }
|
120 |
|
121 | ConnectionParameters.prototype.getLibpqConnectionString = function (cb) {
|
122 | var params = []
|
123 | add(params, this, 'user')
|
124 | add(params, this, 'password')
|
125 | add(params, this, 'port')
|
126 | add(params, this, 'application_name')
|
127 | add(params, this, 'fallback_application_name')
|
128 | add(params, this, 'connect_timeout')
|
129 |
|
130 | var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
|
131 | add(params, ssl, 'sslmode')
|
132 | add(params, ssl, 'sslca')
|
133 | add(params, ssl, 'sslkey')
|
134 | add(params, ssl, 'sslcert')
|
135 | add(params, ssl, 'sslrootcert')
|
136 |
|
137 | if (this.database) {
|
138 | params.push('dbname=' + quoteParamValue(this.database))
|
139 | }
|
140 | if (this.replication) {
|
141 | params.push('replication=' + quoteParamValue(this.replication))
|
142 | }
|
143 | if (this.host) {
|
144 | params.push('host=' + quoteParamValue(this.host))
|
145 | }
|
146 | if (this.isDomainSocket) {
|
147 | return cb(null, params.join(' '))
|
148 | }
|
149 | if (this.client_encoding) {
|
150 | params.push('client_encoding=' + quoteParamValue(this.client_encoding))
|
151 | }
|
152 | dns.lookup(this.host, function (err, address) {
|
153 | if (err) return cb(err, null)
|
154 | params.push('hostaddr=' + quoteParamValue(address))
|
155 | return cb(null, params.join(' '))
|
156 | })
|
157 | }
|
158 |
|
159 | module.exports = ConnectionParameters
|