pg 7.18.2 → 8.0.3

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.
package/lib/connection.js CHANGED
@@ -14,8 +14,6 @@ var util = require('util')
14
14
  var Writer = require('buffer-writer')
15
15
  var Reader = require('packet-reader')
16
16
 
17
- var warnDeprecation = require('./compat/warn-deprecation')
18
-
19
17
  var TEXT_MODE = 0
20
18
  var BINARY_MODE = 1
21
19
  var Connection = function (config) {
@@ -37,7 +35,7 @@ var Connection = function (config) {
37
35
  this._emitMessage = false
38
36
  this._reader = new Reader({
39
37
  headerSize: 1,
40
- lengthPadding: -4
38
+ lengthPadding: -4,
41
39
  })
42
40
  var self = this
43
41
  this.on('newListener', function (eventName) {
@@ -52,13 +50,10 @@ util.inherits(Connection, EventEmitter)
52
50
  Connection.prototype.connect = function (port, host) {
53
51
  var self = this
54
52
 
55
- if (this.stream.readyState === 'closed') {
56
- this.stream.connect(port, host)
57
- } else if (this.stream.readyState === 'open') {
58
- this.emit('connect')
59
- }
53
+ this._connecting = true
54
+ this.stream.connect(port, host)
60
55
 
61
- this.stream.on('connect', function () {
56
+ this.stream.once('connect', function () {
62
57
  if (self._keepAlive) {
63
58
  self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
64
59
  }
@@ -90,26 +85,18 @@ Connection.prototype.connect = function (port, host) {
90
85
  case 'N': // Server does not support SSL connections
91
86
  self.stream.end()
92
87
  return self.emit('error', new Error('The server does not support SSL connections'))
93
- default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
88
+ default:
89
+ // Any other response byte, including 'E' (ErrorResponse) indicating a server error
94
90
  self.stream.end()
95
91
  return self.emit('error', new Error('There was an error establishing an SSL connection'))
96
92
  }
97
93
  var tls = require('tls')
98
- const options = {
99
- socket: self.stream,
100
- checkServerIdentity: self.ssl.checkServerIdentity || tls.checkServerIdentity,
101
- rejectUnauthorized: self.ssl.rejectUnauthorized,
102
- ca: self.ssl.ca,
103
- pfx: self.ssl.pfx,
104
- key: self.ssl.key,
105
- passphrase: self.ssl.passphrase,
106
- cert: self.ssl.cert,
107
- secureOptions: self.ssl.secureOptions,
108
- NPNProtocols: self.ssl.NPNProtocols
109
- }
110
- if (typeof self.ssl.rejectUnauthorized !== 'boolean') {
111
- warnDeprecation('Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.', 'PG-SSL-VERIFY')
112
- }
94
+ const options = Object.assign(
95
+ {
96
+ socket: self.stream,
97
+ },
98
+ self.ssl
99
+ )
113
100
  if (net.isIP(host) === 0) {
114
101
  options.servername = host
115
102
  }
@@ -141,23 +128,16 @@ Connection.prototype.attachListeners = function (stream) {
141
128
  }
142
129
 
143
130
  Connection.prototype.requestSsl = function () {
144
- var bodyBuffer = this.writer
145
- .addInt16(0x04D2)
146
- .addInt16(0x162F).flush()
131
+ var bodyBuffer = this.writer.addInt16(0x04d2).addInt16(0x162f).flush()
147
132
 
148
133
  var length = bodyBuffer.length + 4
149
134
 
150
- var buffer = new Writer()
151
- .addInt32(length)
152
- .add(bodyBuffer)
153
- .join()
135
+ var buffer = new Writer().addInt32(length).add(bodyBuffer).join()
154
136
  this.stream.write(buffer)
155
137
  }
156
138
 
157
139
  Connection.prototype.startup = function (config) {
158
- var writer = this.writer
159
- .addInt16(3)
160
- .addInt16(0)
140
+ var writer = this.writer.addInt16(3).addInt16(0)
161
141
 
162
142
  Object.keys(config).forEach(function (key) {
163
143
  var val = config[key]
@@ -171,27 +151,16 @@ Connection.prototype.startup = function (config) {
171
151
 
172
152
  var length = bodyBuffer.length + 4
173
153
 
174
- var buffer = new Writer()
175
- .addInt32(length)
176
- .add(bodyBuffer)
177
- .join()
154
+ var buffer = new Writer().addInt32(length).add(bodyBuffer).join()
178
155
  this.stream.write(buffer)
179
156
  }
180
157
 
181
158
  Connection.prototype.cancel = function (processID, secretKey) {
182
- var bodyBuffer = this.writer
183
- .addInt16(1234)
184
- .addInt16(5678)
185
- .addInt32(processID)
186
- .addInt32(secretKey)
187
- .flush()
159
+ var bodyBuffer = this.writer.addInt16(1234).addInt16(5678).addInt32(processID).addInt32(secretKey).flush()
188
160
 
189
161
  var length = bodyBuffer.length + 4
190
162
 
191
- var buffer = new Writer()
192
- .addInt32(length)
193
- .add(bodyBuffer)
194
- .join()
163
+ var buffer = new Writer().addInt32(length).add(bodyBuffer).join()
195
164
  this.stream.write(buffer)
196
165
  }
197
166
 
@@ -202,18 +171,14 @@ Connection.prototype.password = function (password) {
202
171
 
203
172
  Connection.prototype.sendSASLInitialResponseMessage = function (mechanism, initialResponse) {
204
173
  // 0x70 = 'p'
205
- this.writer
206
- .addCString(mechanism)
207
- .addInt32(Buffer.byteLength(initialResponse))
208
- .addString(initialResponse)
174
+ this.writer.addCString(mechanism).addInt32(Buffer.byteLength(initialResponse)).addString(initialResponse)
209
175
 
210
176
  this._send(0x70)
211
177
  }
212
178
 
213
179
  Connection.prototype.sendSCRAMClientFinalMessage = function (additionalData) {
214
180
  // 0x70 = 'p'
215
- this.writer
216
- .addString(additionalData)
181
+ this.writer.addString(additionalData)
217
182
 
218
183
  this._send(0x70)
219
184
  }
@@ -277,13 +242,17 @@ Connection.prototype.bind = function (config, more) {
277
242
  var values = config.values || []
278
243
  var len = values.length
279
244
  var useBinary = false
280
- for (var j = 0; j < len; j++) { useBinary |= values[j] instanceof Buffer }
281
- var buffer = this.writer
282
- .addCString(config.portal)
283
- .addCString(config.statement)
284
- if (!useBinary) { buffer.addInt16(0) } else {
245
+ for (var j = 0; j < len; j++) {
246
+ useBinary |= values[j] instanceof Buffer
247
+ }
248
+ var buffer = this.writer.addCString(config.portal).addCString(config.statement)
249
+ if (!useBinary) {
250
+ buffer.addInt16(0)
251
+ } else {
285
252
  buffer.addInt16(len)
286
- for (j = 0; j < len; j++) { buffer.addInt16(values[j] instanceof Buffer) }
253
+ for (j = 0; j < len; j++) {
254
+ buffer.addInt16(values[j] instanceof Buffer)
255
+ }
287
256
  }
288
257
  buffer.addInt16(len)
289
258
  for (var i = 0; i < len; i++) {
@@ -315,9 +284,7 @@ Connection.prototype.execute = function (config, more) {
315
284
  config = config || {}
316
285
  config.portal = config.portal || ''
317
286
  config.rows = config.rows || ''
318
- this.writer
319
- .addCString(config.portal)
320
- .addInt32(config.rows)
287
+ this.writer.addCString(config.portal).addInt32(config.rows)
321
288
 
322
289
  // 0x45 = 'E'
323
290
  this._send(0x45, more)
@@ -346,7 +313,7 @@ Connection.prototype.end = function () {
346
313
  // 0x58 = 'X'
347
314
  this.writer.add(emptyBuffer)
348
315
  this._ending = true
349
- if (!this.stream.writable) {
316
+ if (!this._connecting || !this.stream.writable) {
350
317
  this.stream.end()
351
318
  return
352
319
  }
@@ -602,7 +569,7 @@ Connection.prototype._readValue = function (buffer) {
602
569
  }
603
570
 
604
571
  // parses error
605
- Connection.prototype.parseE = function (buffer, length) {
572
+ Connection.prototype.parseE = function (buffer, length, isNotice) {
606
573
  var fields = {}
607
574
  var fieldType = this.readString(buffer, 1)
608
575
  while (fieldType !== '\0') {
@@ -611,10 +578,10 @@ Connection.prototype.parseE = function (buffer, length) {
611
578
  }
612
579
 
613
580
  // the msg is an Error instance
614
- var msg = new Error(fields.M)
581
+ var msg = isNotice ? { message: fields.M } : new Error(fields.M)
615
582
 
616
583
  // for compatibility with Message
617
- msg.name = 'error'
584
+ msg.name = isNotice ? 'notice' : 'error'
618
585
  msg.length = length
619
586
 
620
587
  msg.severity = fields.S
@@ -638,7 +605,7 @@ Connection.prototype.parseE = function (buffer, length) {
638
605
 
639
606
  // same thing, different name
640
607
  Connection.prototype.parseN = function (buffer, length) {
641
- var msg = this.parseE(buffer, length)
608
+ var msg = this.parseE(buffer, length, true)
642
609
  msg.name = 'notice'
643
610
  return msg
644
611
  }
package/lib/defaults.js CHANGED
@@ -15,7 +15,7 @@ module.exports = {
15
15
  user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
16
16
 
17
17
  // name of database to connect
18
- database: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
18
+ database: undefined,
19
19
 
20
20
  // database user's password
21
21
  password: null,
@@ -70,7 +70,7 @@ module.exports = {
70
70
 
71
71
  keepalives: 1,
72
72
 
73
- keepalives_idle: 0
73
+ keepalives_idle: 0,
74
74
  }
75
75
 
76
76
  var pgTypes = require('pg-types')
package/lib/index.js CHANGED
@@ -7,25 +7,17 @@
7
7
  * README.md file in the root directory of this source tree.
8
8
  */
9
9
 
10
- var util = require('util')
11
10
  var Client = require('./client')
12
11
  var defaults = require('./defaults')
13
12
  var Connection = require('./connection')
14
13
  var Pool = require('pg-pool')
15
- const checkConstructor = require('./compat/check-constructor')
16
14
 
17
15
  const poolFactory = (Client) => {
18
- var BoundPool = function (options) {
19
- // eslint-disable-next-line no-eval
20
- checkConstructor('pg.Pool', 'PG-POOL-NEW', () => eval('new.target'))
21
-
22
- var config = Object.assign({ Client: Client }, options)
23
- return new Pool(config)
16
+ return class BoundPool extends Pool {
17
+ constructor(options) {
18
+ super(options, Client)
19
+ }
24
20
  }
25
-
26
- util.inherits(BoundPool, Pool)
27
-
28
- return BoundPool
29
21
  }
30
22
 
31
23
  var PG = function (clientConstructor) {
@@ -44,20 +36,28 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
44
36
  module.exports = new PG(Client)
45
37
 
46
38
  // lazy require native module...the native module may not have installed
47
- module.exports.__defineGetter__('native', function () {
48
- delete module.exports.native
49
- var native = null
50
- try {
51
- native = new PG(require('./native'))
52
- } catch (err) {
53
- if (err.code !== 'MODULE_NOT_FOUND') {
54
- throw err
39
+ Object.defineProperty(module.exports, 'native', {
40
+ configurable: true,
41
+ enumerable: false,
42
+ get() {
43
+ var native = null
44
+ try {
45
+ native = new PG(require('./native'))
46
+ } catch (err) {
47
+ if (err.code !== 'MODULE_NOT_FOUND') {
48
+ throw err
49
+ }
50
+ /* eslint-disable no-console */
51
+ console.error(err.message)
52
+ /* eslint-enable no-console */
55
53
  }
56
- /* eslint-disable no-console */
57
- console.error(err.message)
58
- /* eslint-enable no-console */
59
- }
60
- module.exports.native = native
61
- return native
54
+
55
+ // overwrite module.exports.native so that getter is never called again
56
+ Object.defineProperty(module.exports, 'native', {
57
+ value: native,
58
+ })
59
+
60
+ return native
61
+ },
62
62
  })
63
63
  }
@@ -22,7 +22,7 @@ assert(semver.gte(Native.version, pkg.minNativeVersion), msg)
22
22
 
23
23
  var NativeQuery = require('./query')
24
24
 
25
- var Client = module.exports = function (config) {
25
+ var Client = (module.exports = function (config) {
26
26
  EventEmitter.call(this)
27
27
  config = config || {}
28
28
 
@@ -30,7 +30,7 @@ var Client = module.exports = function (config) {
30
30
  this._types = new TypeOverrides(config.types)
31
31
 
32
32
  this.native = new Native({
33
- types: this._types
33
+ types: this._types,
34
34
  })
35
35
 
36
36
  this._queryQueue = []
@@ -41,16 +41,24 @@ var Client = module.exports = function (config) {
41
41
 
42
42
  // keep these on the object for legacy reasons
43
43
  // for the time being. TODO: deprecate all this jazz
44
- var cp = this.connectionParameters = new ConnectionParameters(config)
44
+ var cp = (this.connectionParameters = new ConnectionParameters(config))
45
45
  this.user = cp.user
46
- this.password = cp.password
46
+
47
+ // "hiding" the password so it doesn't show up in stack traces
48
+ // or if the client is console.logged
49
+ Object.defineProperty(this, 'password', {
50
+ configurable: true,
51
+ enumerable: false,
52
+ writable: true,
53
+ value: cp.password,
54
+ })
47
55
  this.database = cp.database
48
56
  this.host = cp.host
49
57
  this.port = cp.port
50
58
 
51
59
  // a hash to hold named queries
52
60
  this.namedQueries = {}
53
- }
61
+ })
54
62
 
55
63
  Client.Query = NativeQuery
56
64
 
@@ -107,7 +115,7 @@ Client.prototype._connect = function (cb) {
107
115
  self.native.on('notification', function (msg) {
108
116
  self.emit('notification', {
109
117
  channel: msg.relname,
110
- payload: msg.extra
118
+ payload: msg.extra,
111
119
  })
112
120
  })
113
121
 
@@ -172,7 +180,7 @@ Client.prototype.query = function (config, values, callback) {
172
180
  resolveOut = resolve
173
181
  rejectOut = reject
174
182
  })
175
- query.callback = (err, res) => err ? rejectOut(err) : resolveOut(res)
183
+ query.callback = (err, res) => (err ? rejectOut(err) : resolveOut(res))
176
184
  }
177
185
  }
178
186
 
@@ -240,7 +248,7 @@ Client.prototype.end = function (cb) {
240
248
  var result
241
249
  if (!cb) {
242
250
  result = new this._Promise(function (resolve, reject) {
243
- cb = (err) => err ? reject(err) : resolve()
251
+ cb = (err) => (err ? reject(err) : resolve())
244
252
  })
245
253
  }
246
254
  this.native.end(function () {
@@ -11,7 +11,7 @@ var EventEmitter = require('events').EventEmitter
11
11
  var util = require('util')
12
12
  var utils = require('../utils')
13
13
 
14
- var NativeQuery = module.exports = function (config, values, callback) {
14
+ var NativeQuery = (module.exports = function (config, values, callback) {
15
15
  EventEmitter.call(this)
16
16
  config = utils.normalizeQueryConfig(config, values, callback)
17
17
  this.text = config.text
@@ -27,27 +27,30 @@ var NativeQuery = module.exports = function (config, values, callback) {
27
27
  // this has almost no meaning because libpq
28
28
  // reads all rows into memory befor returning any
29
29
  this._emitRowEvents = false
30
- this.on('newListener', function (event) {
31
- if (event === 'row') this._emitRowEvents = true
32
- }.bind(this))
33
- }
30
+ this.on(
31
+ 'newListener',
32
+ function (event) {
33
+ if (event === 'row') this._emitRowEvents = true
34
+ }.bind(this)
35
+ )
36
+ })
34
37
 
35
38
  util.inherits(NativeQuery, EventEmitter)
36
39
 
37
40
  var errorFieldMap = {
38
41
  /* eslint-disable quote-props */
39
- 'sqlState': 'code',
40
- 'statementPosition': 'position',
41
- 'messagePrimary': 'message',
42
- 'context': 'where',
43
- 'schemaName': 'schema',
44
- 'tableName': 'table',
45
- 'columnName': 'column',
46
- 'dataTypeName': 'dataType',
47
- 'constraintName': 'constraint',
48
- 'sourceFile': 'file',
49
- 'sourceLine': 'line',
50
- 'sourceFunction': 'routine'
42
+ sqlState: 'code',
43
+ statementPosition: 'position',
44
+ messagePrimary: 'message',
45
+ context: 'where',
46
+ schemaName: 'schema',
47
+ tableName: 'table',
48
+ columnName: 'column',
49
+ dataTypeName: 'dataType',
50
+ constraintName: 'constraint',
51
+ sourceFile: 'file',
52
+ sourceLine: 'line',
53
+ sourceFunction: 'routine',
51
54
  }
52
55
 
53
56
  NativeQuery.prototype.handleError = function (err) {
@@ -77,10 +80,12 @@ NativeQuery.prototype.catch = function (callback) {
77
80
 
78
81
  NativeQuery.prototype._getPromise = function () {
79
82
  if (this._promise) return this._promise
80
- this._promise = new Promise(function (resolve, reject) {
81
- this._once('end', resolve)
82
- this._once('error', reject)
83
- }.bind(this))
83
+ this._promise = new Promise(
84
+ function (resolve, reject) {
85
+ this._once('end', resolve)
86
+ this._once('error', reject)
87
+ }.bind(this)
88
+ )
84
89
  return this._promise
85
90
  }
86
91
 
@@ -105,7 +110,7 @@ NativeQuery.prototype.submit = function (client) {
105
110
  if (self._emitRowEvents) {
106
111
  if (results.length > 1) {
107
112
  rows.forEach((rowOfRows, i) => {
108
- rowOfRows.forEach(row => {
113
+ rowOfRows.forEach((row) => {
109
114
  self.emit('row', row, results[i])
110
115
  })
111
116
  })