pg 8.2.0 → 8.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  var dns = require('dns')
11
4
 
@@ -40,72 +33,6 @@ var readSSLConfigFromEnvironment = function () {
40
33
  return defaults.ssl
41
34
  }
42
35
 
43
- var 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
36
  // Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
110
37
  var quoteParamValue = function (value) {
111
38
  return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
@@ -118,42 +45,112 @@ var add = function (params, config, paramName) {
118
45
  }
119
46
  }
120
47
 
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))
48
+ class ConnectionParameters {
49
+ constructor(config) {
50
+ // if a string is passed, it is a raw connection string so we parse it into a config
51
+ config = typeof config === 'string' ? parse(config) : config || {}
52
+
53
+ // if the config has a connectionString defined, parse IT into the config we use
54
+ // this will override other default values with what is stored in connectionString
55
+ if (config.connectionString) {
56
+ config = Object.assign({}, config, parse(config.connectionString))
57
+ }
58
+
59
+ this.user = val('user', config)
60
+ this.database = val('database', config)
61
+
62
+ if (this.database === undefined) {
63
+ this.database = this.user
64
+ }
65
+
66
+ this.port = parseInt(val('port', config), 10)
67
+ this.host = val('host', config)
68
+
69
+ // "hiding" the password so it doesn't show up in stack traces
70
+ // or if the client is console.logged
71
+ Object.defineProperty(this, 'password', {
72
+ configurable: true,
73
+ enumerable: false,
74
+ writable: true,
75
+ value: val('password', config),
76
+ })
77
+
78
+ this.binary = val('binary', config)
79
+ this.options = val('options', config)
80
+
81
+ this.ssl = typeof config.ssl === 'undefined' ? readSSLConfigFromEnvironment() : config.ssl
82
+
83
+ // support passing in ssl=no-verify via connection string
84
+ if (this.ssl === 'no-verify') {
85
+ this.ssl = { rejectUnauthorized: false }
86
+ }
87
+
88
+ this.client_encoding = val('client_encoding', config)
89
+ this.replication = val('replication', config)
90
+ // a domain socket begins with '/'
91
+ this.isDomainSocket = !(this.host || '').indexOf('/')
92
+
93
+ this.application_name = val('application_name', config, 'PGAPPNAME')
94
+ this.fallback_application_name = val('fallback_application_name', config, false)
95
+ this.statement_timeout = val('statement_timeout', config, false)
96
+ this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
97
+ this.query_timeout = val('query_timeout', config, false)
98
+
99
+ if (config.connectionTimeoutMillis === undefined) {
100
+ this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
101
+ } else {
102
+ this.connect_timeout = Math.floor(config.connectionTimeoutMillis / 1000)
103
+ }
104
+
105
+ if (config.keepAlive === false) {
106
+ this.keepalives = 0
107
+ } else if (config.keepAlive === true) {
108
+ this.keepalives = 1
109
+ }
110
+
111
+ if (typeof config.keepAliveInitialDelayMillis === 'number') {
112
+ this.keepalives_idle = Math.floor(config.keepAliveInitialDelayMillis / 1000)
113
+ }
142
114
  }
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))
115
+
116
+ getLibpqConnectionString(cb) {
117
+ var params = []
118
+ add(params, this, 'user')
119
+ add(params, this, 'password')
120
+ add(params, this, 'port')
121
+ add(params, this, 'application_name')
122
+ add(params, this, 'fallback_application_name')
123
+ add(params, this, 'connect_timeout')
124
+ add(params, this, 'options')
125
+
126
+ var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
127
+ add(params, ssl, 'sslmode')
128
+ add(params, ssl, 'sslca')
129
+ add(params, ssl, 'sslkey')
130
+ add(params, ssl, 'sslcert')
131
+ add(params, ssl, 'sslrootcert')
132
+
133
+ if (this.database) {
134
+ params.push('dbname=' + quoteParamValue(this.database))
135
+ }
136
+ if (this.replication) {
137
+ params.push('replication=' + quoteParamValue(this.replication))
138
+ }
139
+ if (this.host) {
140
+ params.push('host=' + quoteParamValue(this.host))
141
+ }
142
+ if (this.isDomainSocket) {
143
+ return cb(null, params.join(' '))
144
+ }
145
+ if (this.client_encoding) {
146
+ params.push('client_encoding=' + quoteParamValue(this.client_encoding))
147
+ }
148
+ dns.lookup(this.host, function (err, address) {
149
+ if (err) return cb(err, null)
150
+ params.push('hostaddr=' + quoteParamValue(address))
151
+ return cb(null, params.join(' '))
152
+ })
151
153
  }
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
154
  }
158
155
 
159
156
  module.exports = ConnectionParameters
package/lib/connection.js CHANGED
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  var net = require('net')
11
4
  var EventEmitter = require('events').EventEmitter
@@ -13,201 +6,203 @@ var util = require('util')
13
6
 
14
7
  const { parse, serialize } = require('pg-protocol')
15
8
 
16
- // TODO(bmc) support binary mode at some point
17
- var Connection = function (config) {
18
- EventEmitter.call(this)
19
- config = config || {}
20
- this.stream = config.stream || new net.Socket()
21
- this._keepAlive = config.keepAlive
22
- this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
23
- this.lastBuffer = false
24
- this.parsedStatements = {}
25
- this.ssl = config.ssl || false
26
- this._ending = false
27
- this._emitMessage = false
28
- var self = this
29
- this.on('newListener', function (eventName) {
30
- if (eventName === 'message') {
31
- self._emitMessage = true
32
- }
33
- })
34
- }
35
-
36
- util.inherits(Connection, EventEmitter)
37
-
38
- Connection.prototype.connect = function (port, host) {
39
- var self = this
40
-
41
- this._connecting = true
42
- this.stream.setNoDelay(true)
43
- this.stream.connect(port, host)
9
+ const flushBuffer = serialize.flush()
10
+ const syncBuffer = serialize.sync()
11
+ const endBuffer = serialize.end()
44
12
 
45
- this.stream.once('connect', function () {
46
- if (self._keepAlive) {
47
- self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
48
- }
49
- self.emit('connect')
50
- })
13
+ // TODO(bmc) support binary mode at some point
14
+ class Connection extends EventEmitter {
15
+ constructor(config) {
16
+ super()
17
+ config = config || {}
18
+ this.stream = config.stream || new net.Socket()
19
+ this._keepAlive = config.keepAlive
20
+ this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
21
+ this.lastBuffer = false
22
+ this.parsedStatements = {}
23
+ this.ssl = config.ssl || false
24
+ this._ending = false
25
+ this._emitMessage = false
26
+ var self = this
27
+ this.on('newListener', function (eventName) {
28
+ if (eventName === 'message') {
29
+ self._emitMessage = true
30
+ }
31
+ })
32
+ }
51
33
 
52
- const reportStreamError = function (error) {
53
- // errors about disconnections should be ignored during disconnect
54
- if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
55
- return
56
- }
57
- self.emit('error', error)
58
- }
59
- this.stream.on('error', reportStreamError)
60
-
61
- this.stream.on('close', function () {
62
- self.emit('end')
63
- })
64
-
65
- if (!this.ssl) {
66
- return this.attachListeners(this.stream)
67
- }
68
-
69
- this.stream.once('data', function (buffer) {
70
- var responseCode = buffer.toString('utf8')
71
- switch (responseCode) {
72
- case 'S': // Server supports SSL connections, continue with a secure connection
73
- break
74
- case 'N': // Server does not support SSL connections
75
- self.stream.end()
76
- return self.emit('error', new Error('The server does not support SSL connections'))
77
- default:
78
- // Any other response byte, including 'E' (ErrorResponse) indicating a server error
79
- self.stream.end()
80
- return self.emit('error', new Error('There was an error establishing an SSL connection'))
34
+ connect(port, host) {
35
+ var self = this
36
+
37
+ this._connecting = true
38
+ this.stream.setNoDelay(true)
39
+ this.stream.connect(port, host)
40
+
41
+ this.stream.once('connect', function () {
42
+ if (self._keepAlive) {
43
+ self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
44
+ }
45
+ self.emit('connect')
46
+ })
47
+
48
+ const reportStreamError = function (error) {
49
+ // errors about disconnections should be ignored during disconnect
50
+ if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
51
+ return
52
+ }
53
+ self.emit('error', error)
81
54
  }
82
- var tls = require('tls')
83
- const options = Object.assign(
84
- {
85
- socket: self.stream,
86
- },
87
- self.ssl
88
- )
89
- if (net.isIP(host) === 0) {
90
- options.servername = host
91
- }
92
- self.stream = tls.connect(options)
93
- self.attachListeners(self.stream)
94
- self.stream.on('error', reportStreamError)
55
+ this.stream.on('error', reportStreamError)
95
56
 
96
- self.emit('sslconnect')
97
- })
98
- }
57
+ this.stream.on('close', function () {
58
+ self.emit('end')
59
+ })
99
60
 
100
- Connection.prototype.attachListeners = function (stream) {
101
- stream.on('end', () => {
102
- this.emit('end')
103
- })
104
- parse(stream, (msg) => {
105
- var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
106
- if (this._emitMessage) {
107
- this.emit('message', msg)
61
+ if (!this.ssl) {
62
+ return this.attachListeners(this.stream)
108
63
  }
109
- this.emit(eventName, msg)
110
- })
111
- }
112
64
 
113
- Connection.prototype.requestSsl = function () {
114
- this.stream.write(serialize.requestSsl())
115
- }
65
+ this.stream.once('data', function (buffer) {
66
+ var responseCode = buffer.toString('utf8')
67
+ switch (responseCode) {
68
+ case 'S': // Server supports SSL connections, continue with a secure connection
69
+ break
70
+ case 'N': // Server does not support SSL connections
71
+ self.stream.end()
72
+ return self.emit('error', new Error('The server does not support SSL connections'))
73
+ default:
74
+ // Any other response byte, including 'E' (ErrorResponse) indicating a server error
75
+ self.stream.end()
76
+ return self.emit('error', new Error('There was an error establishing an SSL connection'))
77
+ }
78
+ var tls = require('tls')
79
+ const options = Object.assign(
80
+ {
81
+ socket: self.stream,
82
+ },
83
+ self.ssl
84
+ )
85
+ if (net.isIP(host) === 0) {
86
+ options.servername = host
87
+ }
88
+ try {
89
+ self.stream = tls.connect(options)
90
+ } catch (err) {
91
+ return self.emit('error', err)
92
+ }
93
+ self.attachListeners(self.stream)
94
+ self.stream.on('error', reportStreamError)
95
+
96
+ self.emit('sslconnect')
97
+ })
98
+ }
116
99
 
117
- Connection.prototype.startup = function (config) {
118
- this.stream.write(serialize.startup(config))
119
- }
100
+ attachListeners(stream) {
101
+ stream.on('end', () => {
102
+ this.emit('end')
103
+ })
104
+ parse(stream, (msg) => {
105
+ var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
106
+ if (this._emitMessage) {
107
+ this.emit('message', msg)
108
+ }
109
+ this.emit(eventName, msg)
110
+ })
111
+ }
120
112
 
121
- Connection.prototype.cancel = function (processID, secretKey) {
122
- this._send(serialize.cancel(processID, secretKey))
123
- }
113
+ requestSsl() {
114
+ this.stream.write(serialize.requestSsl())
115
+ }
124
116
 
125
- Connection.prototype.password = function (password) {
126
- this._send(serialize.password(password))
127
- }
117
+ startup(config) {
118
+ this.stream.write(serialize.startup(config))
119
+ }
128
120
 
129
- Connection.prototype.sendSASLInitialResponseMessage = function (mechanism, initialResponse) {
130
- this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
131
- }
121
+ cancel(processID, secretKey) {
122
+ this._send(serialize.cancel(processID, secretKey))
123
+ }
132
124
 
133
- Connection.prototype.sendSCRAMClientFinalMessage = function (additionalData) {
134
- this._send(serialize.sendSCRAMClientFinalMessage(additionalData))
135
- }
125
+ password(password) {
126
+ this._send(serialize.password(password))
127
+ }
136
128
 
137
- Connection.prototype._send = function (buffer) {
138
- if (!this.stream.writable) {
139
- return false
129
+ sendSASLInitialResponseMessage(mechanism, initialResponse) {
130
+ this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
140
131
  }
141
- return this.stream.write(buffer)
142
- }
143
132
 
144
- Connection.prototype.query = function (text) {
145
- this._send(serialize.query(text))
146
- }
133
+ sendSCRAMClientFinalMessage(additionalData) {
134
+ this._send(serialize.sendSCRAMClientFinalMessage(additionalData))
135
+ }
147
136
 
148
- // send parse message
149
- Connection.prototype.parse = function (query) {
150
- this._send(serialize.parse(query))
151
- }
137
+ _send(buffer) {
138
+ if (!this.stream.writable) {
139
+ return false
140
+ }
141
+ return this.stream.write(buffer)
142
+ }
152
143
 
153
- // send bind message
154
- // "more" === true to buffer the message until flush() is called
155
- Connection.prototype.bind = function (config) {
156
- this._send(serialize.bind(config))
157
- }
144
+ query(text) {
145
+ this._send(serialize.query(text))
146
+ }
158
147
 
159
- // send execute message
160
- // "more" === true to buffer the message until flush() is called
161
- Connection.prototype.execute = function (config) {
162
- this._send(serialize.execute(config))
163
- }
148
+ // send parse message
149
+ parse(query) {
150
+ this._send(serialize.parse(query))
151
+ }
164
152
 
165
- const flushBuffer = serialize.flush()
166
- Connection.prototype.flush = function () {
167
- if (this.stream.writable) {
168
- this.stream.write(flushBuffer)
153
+ // send bind message
154
+ bind(config) {
155
+ this._send(serialize.bind(config))
169
156
  }
170
- }
171
157
 
172
- const syncBuffer = serialize.sync()
173
- Connection.prototype.sync = function () {
174
- this._ending = true
175
- this._send(syncBuffer)
176
- this._send(flushBuffer)
177
- }
158
+ // send execute message
159
+ execute(config) {
160
+ this._send(serialize.execute(config))
161
+ }
178
162
 
179
- const endBuffer = serialize.end()
163
+ flush() {
164
+ if (this.stream.writable) {
165
+ this.stream.write(flushBuffer)
166
+ }
167
+ }
180
168
 
181
- Connection.prototype.end = function () {
182
- // 0x58 = 'X'
183
- this._ending = true
184
- if (!this._connecting || !this.stream.writable) {
185
- this.stream.end()
186
- return
169
+ sync() {
170
+ this._ending = true
171
+ this._send(flushBuffer)
172
+ this._send(syncBuffer)
187
173
  }
188
- return this.stream.write(endBuffer, () => {
189
- this.stream.end()
190
- })
191
- }
192
174
 
193
- Connection.prototype.close = function (msg) {
194
- this._send(serialize.close(msg))
195
- }
175
+ end() {
176
+ // 0x58 = 'X'
177
+ this._ending = true
178
+ if (!this._connecting || !this.stream.writable) {
179
+ this.stream.end()
180
+ return
181
+ }
182
+ return this.stream.write(endBuffer, () => {
183
+ this.stream.end()
184
+ })
185
+ }
196
186
 
197
- Connection.prototype.describe = function (msg) {
198
- this._send(serialize.describe(msg))
199
- }
187
+ close(msg) {
188
+ this._send(serialize.close(msg))
189
+ }
200
190
 
201
- Connection.prototype.sendCopyFromChunk = function (chunk) {
202
- this._send(serialize.copyData(chunk))
203
- }
191
+ describe(msg) {
192
+ this._send(serialize.describe(msg))
193
+ }
204
194
 
205
- Connection.prototype.endCopyFrom = function () {
206
- this._send(serialize.copyDone())
207
- }
195
+ sendCopyFromChunk(chunk) {
196
+ this._send(serialize.copyData(chunk))
197
+ }
208
198
 
209
- Connection.prototype.sendCopyFail = function (msg) {
210
- this._send(serialize.copyFail(msg))
199
+ endCopyFrom() {
200
+ this._send(serialize.copyDone())
201
+ }
202
+
203
+ sendCopyFail(msg) {
204
+ this._send(serialize.copyFail(msg))
205
+ }
211
206
  }
212
207
 
213
208
  module.exports = Connection
package/lib/defaults.js CHANGED
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  module.exports = {
11
4
  // database host. defaults to localhost
@@ -53,6 +46,8 @@ module.exports = {
53
46
 
54
47
  fallback_application_name: undefined,
55
48
 
49
+ options: undefined,
50
+
56
51
  parseInputDatesAsUTC: false,
57
52
 
58
53
  // max milliseconds any query using this connection will execute for before timing out in error.
package/lib/index.js CHANGED
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  var Client = require('./client')
11
4
  var defaults = require('./defaults')
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  // eslint-disable-next-line
11
4
  var Native = require('pg-native')
@@ -1,11 +1,4 @@
1
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
2
 
10
3
  var EventEmitter = require('events').EventEmitter
11
4
  var util = require('util')