source: node_modules/pg/lib/connection-parameters.js@ 6c7cfa6

main
Last change on this file since 6c7cfa6 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 5.8 KB
Line 
1'use strict'
2
3const dns = require('dns')
4
5const defaults = require('./defaults')
6
7const parse = require('pg-connection-string').parse // parses a connection string
8
9const val = function (key, config, envVar) {
10 if (config[key]) {
11 return config[key]
12 }
13
14 if (envVar === undefined) {
15 envVar = process.env['PG' + key.toUpperCase()]
16 } else if (envVar === false) {
17 // do nothing ... use false
18 } else {
19 envVar = process.env[envVar]
20 }
21
22 return envVar || defaults[key]
23}
24
25const readSSLConfigFromEnvironment = function () {
26 switch (process.env.PGSSLMODE) {
27 case 'disable':
28 return false
29 case 'prefer':
30 case 'require':
31 case 'verify-ca':
32 case 'verify-full':
33 return true
34 case 'no-verify':
35 return { rejectUnauthorized: false }
36 }
37 return defaults.ssl
38}
39
40// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
41const quoteParamValue = function (value) {
42 return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
43}
44
45const add = function (params, config, paramName) {
46 const value = config[paramName]
47 if (value !== undefined && value !== null) {
48 params.push(paramName + '=' + quoteParamValue(value))
49 }
50}
51
52class ConnectionParameters {
53 constructor(config) {
54 // if a string is passed, it is a raw connection string so we parse it into a config
55 config = typeof config === 'string' ? parse(config) : config || {}
56
57 // if the config has a connectionString defined, parse IT into the config we use
58 // this will override other default values with what is stored in connectionString
59 if (config.connectionString) {
60 config = Object.assign({}, config, parse(config.connectionString))
61 }
62
63 this.user = val('user', config)
64 this.database = val('database', config)
65
66 if (this.database === undefined) {
67 this.database = this.user
68 }
69
70 this.port = parseInt(val('port', config), 10)
71 this.host = val('host', config)
72
73 // "hiding" the password so it doesn't show up in stack traces
74 // or if the client is console.logged
75 Object.defineProperty(this, 'password', {
76 configurable: true,
77 enumerable: false,
78 writable: true,
79 value: val('password', config),
80 })
81
82 this.binary = val('binary', config)
83 this.options = val('options', config)
84
85 this.ssl = typeof config.ssl === 'undefined' ? readSSLConfigFromEnvironment() : config.ssl
86
87 if (typeof this.ssl === 'string') {
88 if (this.ssl === 'true') {
89 this.ssl = true
90 }
91 }
92 // support passing in ssl=no-verify via connection string
93 if (this.ssl === 'no-verify') {
94 this.ssl = { rejectUnauthorized: false }
95 }
96 if (this.ssl && this.ssl.key) {
97 Object.defineProperty(this.ssl, 'key', {
98 enumerable: false,
99 })
100 }
101
102 // How to negotiate SSL: 'postgres' (default, the traditional SSLRequest
103 // handshake) or 'direct' (start the TLS handshake immediately on connect).
104 this.sslnegotiation = val('sslnegotiation', config, 'PGSSLNEGOTIATION')
105 if (this.sslnegotiation !== undefined && this.sslnegotiation !== 'postgres' && this.sslnegotiation !== 'direct') {
106 throw new Error(
107 `Invalid sslnegotiation value: "${this.sslnegotiation}". Valid values are "postgres" and "direct".`
108 )
109 }
110 if (this.sslnegotiation === 'direct' && !this.ssl) {
111 throw new Error('sslnegotiation=direct requires SSL to be enabled')
112 }
113
114 this.client_encoding = val('client_encoding', config)
115 this.replication = val('replication', config)
116 // a domain socket begins with '/'
117 this.isDomainSocket = !(this.host || '').indexOf('/')
118
119 this.application_name = val('application_name', config, 'PGAPPNAME')
120 this.fallback_application_name = val('fallback_application_name', config, false)
121 this.statement_timeout = val('statement_timeout', config, false)
122 this.lock_timeout = val('lock_timeout', config, false)
123 this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
124 this.query_timeout = val('query_timeout', config, false)
125
126 if (config.connectionTimeoutMillis === undefined) {
127 this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
128 } else {
129 this.connect_timeout = Math.floor(config.connectionTimeoutMillis / 1000)
130 }
131
132 if (config.keepAlive === false) {
133 this.keepalives = 0
134 } else if (config.keepAlive === true) {
135 this.keepalives = 1
136 }
137
138 if (typeof config.keepAliveInitialDelayMillis === 'number') {
139 this.keepalives_idle = Math.floor(config.keepAliveInitialDelayMillis / 1000)
140 }
141 }
142
143 getLibpqConnectionString(cb) {
144 const params = []
145 add(params, this, 'user')
146 add(params, this, 'password')
147 add(params, this, 'port')
148 add(params, this, 'application_name')
149 add(params, this, 'fallback_application_name')
150 add(params, this, 'connect_timeout')
151 add(params, this, 'options')
152
153 const ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
154 add(params, ssl, 'sslmode')
155 add(params, ssl, 'sslca')
156 add(params, ssl, 'sslkey')
157 add(params, ssl, 'sslcert')
158 add(params, ssl, 'sslrootcert')
159 add(params, this, 'sslnegotiation')
160
161 if (this.database) {
162 params.push('dbname=' + quoteParamValue(this.database))
163 }
164 if (this.replication) {
165 params.push('replication=' + quoteParamValue(this.replication))
166 }
167 if (this.host) {
168 params.push('host=' + quoteParamValue(this.host))
169 }
170 if (this.isDomainSocket) {
171 return cb(null, params.join(' '))
172 }
173 if (this.client_encoding) {
174 params.push('client_encoding=' + quoteParamValue(this.client_encoding))
175 }
176 dns.lookup(this.host, function (err, address) {
177 if (err) return cb(err, null)
178 params.push('hostaddr=' + quoteParamValue(address))
179 return cb(null, params.join(' '))
180 })
181 }
182}
183
184module.exports = ConnectionParameters
Note: See TracBrowser for help on using the repository browser.