UNPKG

4.95 kBJavaScriptView Raw
1'use strict'
2/**
3 * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
4 * All rights reserved.
5 *
6 * This source code is licensed under the MIT license found in the
7 * README.md file in the root directory of this source tree.
8 */
9
10var dns = require('dns')
11
12var defaults = require('./defaults')
13
14var parse = require('pg-connection-string').parse // parses a connection string
15
16var val = function (key, config, envVar) {
17 if (envVar === undefined) {
18 envVar = process.env['PG' + key.toUpperCase()]
19 } else if (envVar === false) {
20 // do nothing ... use false
21 } else {
22 envVar = process.env[envVar]
23 }
24
25 return config[key] || envVar || defaults[key]
26}
27
28var 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
43var ConnectionParameters = function (config) {
44 // if a string is passed, it is a raw connection string so we parse it into a config
45 config = typeof config === 'string' ? parse(config) : config || {}
46
47 // if the config has a connectionString defined, parse IT into the config we use
48 // this will override other default values with what is stored in connectionString
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 // "hiding" the password so it doesn't show up in stack traces
64 // or if the client is console.logged
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 // support passing in ssl=no-verify via connection string
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 // a domain socket begins with '/'
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// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
110var quoteParamValue = function (value) {
111 return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
112}
113
114var 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
121ConnectionParameters.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
159module.exports = ConnectionParameters