pg 8.0.2 → 8.2.1
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/README.md +34 -39
- package/lib/client.js +42 -34
- package/lib/connection-parameters.js +14 -7
- package/lib/connection.js +56 -538
- package/lib/defaults.js +1 -1
- package/lib/index.js +3 -3
- package/lib/native/client.js +8 -8
- package/lib/native/query.js +27 -22
- package/lib/query.js +44 -24
- package/lib/sasl.js +34 -30
- package/lib/type-overrides.js +7 -4
- package/lib/utils.js +43 -27
- package/package.json +6 -13
- package/lib/connection-fast.js +0 -215
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<span class="badge-npmversion"><a href="https://npmjs.org/package/pg" title="View this project on NPM"><img src="https://img.shields.io/npm/v/pg.svg" alt="NPM version" /></a></span>
|
|
6
6
|
<span class="badge-npmdownloads"><a href="https://npmjs.org/package/pg" title="View this project on NPM"><img src="https://img.shields.io/npm/dm/pg.svg" alt="NPM downloads" /></a></span>
|
|
7
7
|
|
|
8
|
-
Non-blocking PostgreSQL client for Node.js.
|
|
8
|
+
Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.
|
|
9
9
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
@@ -14,30 +14,31 @@ $ npm install pg
|
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
---
|
|
17
|
-
## :star: [Documentation](https://node-postgres.com) :star:
|
|
18
17
|
|
|
18
|
+
## :star: [Documentation](https://node-postgres.com) :star:
|
|
19
19
|
|
|
20
20
|
### Features
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
- Pure JavaScript client and native libpq bindings share _the same API_
|
|
23
|
+
- Connection pooling
|
|
24
|
+
- Extensible JS ↔ PostgreSQL data-type coercion
|
|
25
|
+
- Supported PostgreSQL features
|
|
26
|
+
- Parameterized queries
|
|
27
|
+
- Named statements with query plan caching
|
|
28
|
+
- Async notifications with `LISTEN/NOTIFY`
|
|
29
|
+
- Bulk import & export with `COPY TO/COPY FROM`
|
|
30
30
|
|
|
31
31
|
### Extras
|
|
32
32
|
|
|
33
|
-
node-postgres is by design pretty light on abstractions.
|
|
33
|
+
node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture.
|
|
34
34
|
The entire list can be found on our [wiki](https://github.com/brianc/node-postgres/wiki/Extras).
|
|
35
35
|
|
|
36
36
|
## Support
|
|
37
37
|
|
|
38
|
-
node-postgres is free software.
|
|
38
|
+
node-postgres is free software. If you encounter a bug with the library please open an issue on the [GitHub repo](https://github.com/brianc/node-postgres). If you have questions unanswered by the documentation please open an issue pointing out how the documentation was unclear & I will do my best to make it better!
|
|
39
39
|
|
|
40
40
|
When you open an issue please provide:
|
|
41
|
+
|
|
41
42
|
- version of Node
|
|
42
43
|
- version of Postgres
|
|
43
44
|
- smallest possible snippet of code to reproduce the problem
|
|
@@ -49,10 +50,6 @@ You can also follow me [@briancarlson](https://twitter.com/briancarlson) if that
|
|
|
49
50
|
node-postgres's continued development has been made possible in part by generous finanical support from [the community](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md) and these featured sponsors:
|
|
50
51
|
|
|
51
52
|
<div align="center">
|
|
52
|
-
<a href="https://www.timescale.com" target="_blank">
|
|
53
|
-
<img height="80" src="https://node-postgres.com/timescale.svg" />
|
|
54
|
-
</a>
|
|
55
|
-
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAQAAAB0m0auAAAADElEQVR42mNkIBIAAABSAAI2VLqiAAAAAElFTkSuQmCC" />
|
|
56
53
|
<a href="https://crate.io" target="_blank">
|
|
57
54
|
<img height="80" src="https://node-postgres.com/crate-io.png" />
|
|
58
55
|
</a>
|
|
@@ -60,19 +57,18 @@ node-postgres's continued development has been made possible in part by generous
|
|
|
60
57
|
|
|
61
58
|
If you or your company are benefiting from node-postgres and would like to help keep the project financially sustainable [please consider supporting](https://github.com/sponsors/brianc) its development.
|
|
62
59
|
|
|
63
|
-
|
|
64
60
|
## Contributing
|
|
65
61
|
|
|
66
|
-
|
|
62
|
+
**:heart: contributions!**
|
|
67
63
|
|
|
68
|
-
I will
|
|
69
|
-
|
|
64
|
+
I will **happily** accept your pull request if it:
|
|
65
|
+
|
|
66
|
+
- **has tests**
|
|
70
67
|
- looks reasonable
|
|
71
68
|
- does not break backwards compatibility
|
|
72
69
|
|
|
73
70
|
If your change involves breaking backwards compatibility please please point that out in the pull request & we can discuss & plan when and how to release it and what type of documentation or communicate it will require.
|
|
74
71
|
|
|
75
|
-
|
|
76
72
|
## Troubleshooting and FAQ
|
|
77
73
|
|
|
78
74
|
The causes and solutions to common errors can be found among the [Frequently Asked Questions (FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)
|
|
@@ -81,21 +77,20 @@ The causes and solutions to common errors can be found among the [Frequently Ask
|
|
|
81
77
|
|
|
82
78
|
Copyright (c) 2010-2020 Brian Carlson (brian.m.carlson@gmail.com)
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
80
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
81
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
82
|
+
in the Software without restriction, including without limitation the rights
|
|
83
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
84
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
85
|
+
furnished to do so, subject to the following conditions:
|
|
86
|
+
|
|
87
|
+
The above copyright notice and this permission notice shall be included in
|
|
88
|
+
all copies or substantial portions of the Software.
|
|
89
|
+
|
|
90
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
91
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
92
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
93
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
94
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
95
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
96
|
+
THE SOFTWARE.
|
package/lib/client.js
CHANGED
|
@@ -18,9 +18,6 @@ var ConnectionParameters = require('./connection-parameters')
|
|
|
18
18
|
var Query = require('./query')
|
|
19
19
|
var defaults = require('./defaults')
|
|
20
20
|
var Connection = require('./connection')
|
|
21
|
-
if (process.env.PG_FAST_CONNECTION) {
|
|
22
|
-
Connection = require('./connection-fast')
|
|
23
|
-
}
|
|
24
21
|
|
|
25
22
|
var Client = function (config) {
|
|
26
23
|
EventEmitter.call(this)
|
|
@@ -37,7 +34,7 @@ var Client = function (config) {
|
|
|
37
34
|
configurable: true,
|
|
38
35
|
enumerable: false,
|
|
39
36
|
writable: true,
|
|
40
|
-
value: this.connectionParameters.password
|
|
37
|
+
value: this.connectionParameters.password,
|
|
41
38
|
})
|
|
42
39
|
|
|
43
40
|
this.replication = this.connectionParameters.replication
|
|
@@ -52,13 +49,15 @@ var Client = function (config) {
|
|
|
52
49
|
this._connectionError = false
|
|
53
50
|
this._queryable = true
|
|
54
51
|
|
|
55
|
-
this.connection =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
this.connection =
|
|
53
|
+
c.connection ||
|
|
54
|
+
new Connection({
|
|
55
|
+
stream: c.stream,
|
|
56
|
+
ssl: this.connectionParameters.ssl,
|
|
57
|
+
keepAlive: c.keepAlive || false,
|
|
58
|
+
keepAliveInitialDelayMillis: c.keepAliveInitialDelayMillis || 0,
|
|
59
|
+
encoding: this.connectionParameters.client_encoding || 'utf8',
|
|
60
|
+
})
|
|
62
61
|
this.queryQueue = []
|
|
63
62
|
this.binary = c.binary || defaults.binary
|
|
64
63
|
this.processID = null
|
|
@@ -127,9 +126,10 @@ Client.prototype._connect = function (callback) {
|
|
|
127
126
|
function checkPgPass(cb) {
|
|
128
127
|
return function (msg) {
|
|
129
128
|
if (typeof self.password === 'function') {
|
|
130
|
-
self._Promise
|
|
129
|
+
self._Promise
|
|
130
|
+
.resolve()
|
|
131
131
|
.then(() => self.password())
|
|
132
|
-
.then(pass => {
|
|
132
|
+
.then((pass) => {
|
|
133
133
|
if (pass !== undefined) {
|
|
134
134
|
if (typeof pass !== 'string') {
|
|
135
135
|
con.emit('error', new TypeError('Password must be a string'))
|
|
@@ -140,7 +140,8 @@ Client.prototype._connect = function (callback) {
|
|
|
140
140
|
self.connectionParameters.password = self.password = null
|
|
141
141
|
}
|
|
142
142
|
cb(msg)
|
|
143
|
-
})
|
|
143
|
+
})
|
|
144
|
+
.catch((err) => {
|
|
144
145
|
con.emit('error', err)
|
|
145
146
|
})
|
|
146
147
|
} else if (self.password !== null) {
|
|
@@ -157,22 +158,31 @@ Client.prototype._connect = function (callback) {
|
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
// password request handling
|
|
160
|
-
con.on(
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
con.on(
|
|
162
|
+
'authenticationCleartextPassword',
|
|
163
|
+
checkPgPass(function () {
|
|
164
|
+
con.password(self.password)
|
|
165
|
+
})
|
|
166
|
+
)
|
|
163
167
|
|
|
164
168
|
// password request handling
|
|
165
|
-
con.on(
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
con.on(
|
|
170
|
+
'authenticationMD5Password',
|
|
171
|
+
checkPgPass(function (msg) {
|
|
172
|
+
con.password(utils.postgresMd5PasswordHash(self.user, self.password, msg.salt))
|
|
173
|
+
})
|
|
174
|
+
)
|
|
168
175
|
|
|
169
176
|
// password request handling (SASL)
|
|
170
177
|
var saslSession
|
|
171
|
-
con.on(
|
|
172
|
-
|
|
178
|
+
con.on(
|
|
179
|
+
'authenticationSASL',
|
|
180
|
+
checkPgPass(function (msg) {
|
|
181
|
+
saslSession = sasl.startSession(msg.mechanisms)
|
|
173
182
|
|
|
174
|
-
|
|
175
|
-
|
|
183
|
+
con.sendSASLInitialResponseMessage(saslSession.mechanism, saslSession.response)
|
|
184
|
+
})
|
|
185
|
+
)
|
|
176
186
|
|
|
177
187
|
// password request handling (SASL)
|
|
178
188
|
con.on('authenticationSASLContinue', function (msg) {
|
|
@@ -259,9 +269,7 @@ Client.prototype._connect = function (callback) {
|
|
|
259
269
|
})
|
|
260
270
|
|
|
261
271
|
con.once('end', () => {
|
|
262
|
-
const error = this._ending
|
|
263
|
-
? new Error('Connection terminated')
|
|
264
|
-
: new Error('Connection terminated unexpectedly')
|
|
272
|
+
const error = this._ending ? new Error('Connection terminated') : new Error('Connection terminated unexpectedly')
|
|
265
273
|
|
|
266
274
|
clearTimeout(connectionTimeoutHandle)
|
|
267
275
|
this._errorAllQueries(error)
|
|
@@ -367,7 +375,7 @@ Client.prototype.getStartupConf = function () {
|
|
|
367
375
|
|
|
368
376
|
var data = {
|
|
369
377
|
user: params.user,
|
|
370
|
-
database: params.database
|
|
378
|
+
database: params.database,
|
|
371
379
|
}
|
|
372
380
|
|
|
373
381
|
var appName = params.application_name || params.fallback_application_name
|
|
@@ -422,11 +430,11 @@ Client.prototype.escapeIdentifier = function (str) {
|
|
|
422
430
|
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
423
431
|
Client.prototype.escapeLiteral = function (str) {
|
|
424
432
|
var hasBackslash = false
|
|
425
|
-
var escaped = '
|
|
433
|
+
var escaped = "'"
|
|
426
434
|
|
|
427
435
|
for (var i = 0; i < str.length; i++) {
|
|
428
436
|
var c = str[i]
|
|
429
|
-
if (c === '
|
|
437
|
+
if (c === "'") {
|
|
430
438
|
escaped += c + c
|
|
431
439
|
} else if (c === '\\') {
|
|
432
440
|
escaped += c + c
|
|
@@ -436,7 +444,7 @@ Client.prototype.escapeLiteral = function (str) {
|
|
|
436
444
|
}
|
|
437
445
|
}
|
|
438
446
|
|
|
439
|
-
escaped += '
|
|
447
|
+
escaped += "'"
|
|
440
448
|
|
|
441
449
|
if (hasBackslash === true) {
|
|
442
450
|
escaped = ' E' + escaped
|
|
@@ -488,7 +496,7 @@ Client.prototype.query = function (config, values, callback) {
|
|
|
488
496
|
query = new Query(config, values, callback)
|
|
489
497
|
if (!query.callback) {
|
|
490
498
|
result = new this._Promise((resolve, reject) => {
|
|
491
|
-
query.callback = (err, res) => err ? reject(err) : resolve(res)
|
|
499
|
+
query.callback = (err, res) => (err ? reject(err) : resolve(res))
|
|
492
500
|
})
|
|
493
501
|
}
|
|
494
502
|
}
|
|
@@ -507,7 +515,7 @@ Client.prototype.query = function (config, values, callback) {
|
|
|
507
515
|
|
|
508
516
|
// we already returned an error,
|
|
509
517
|
// just do nothing if query completes
|
|
510
|
-
query.callback = () => {
|
|
518
|
+
query.callback = () => {}
|
|
511
519
|
|
|
512
520
|
// Remove from queue
|
|
513
521
|
var index = this.queryQueue.indexOf(query)
|
|
@@ -555,7 +563,7 @@ Client.prototype.end = function (cb) {
|
|
|
555
563
|
this._ending = true
|
|
556
564
|
|
|
557
565
|
// if we have never connected, then end is a noop, callback immediately
|
|
558
|
-
if (this.connection.
|
|
566
|
+
if (!this.connection._connecting) {
|
|
559
567
|
if (cb) {
|
|
560
568
|
cb()
|
|
561
569
|
} else {
|
|
@@ -22,12 +22,10 @@ var val = function (key, config, envVar) {
|
|
|
22
22
|
envVar = process.env[envVar]
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
return config[key] ||
|
|
26
|
-
envVar ||
|
|
27
|
-
defaults[key]
|
|
25
|
+
return config[key] || envVar || defaults[key]
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
var
|
|
28
|
+
var readSSLConfigFromEnvironment = function () {
|
|
31
29
|
switch (process.env.PGSSLMODE) {
|
|
32
30
|
case 'disable':
|
|
33
31
|
return false
|
|
@@ -36,6 +34,8 @@ var useSsl = function () {
|
|
|
36
34
|
case 'verify-ca':
|
|
37
35
|
case 'verify-full':
|
|
38
36
|
return true
|
|
37
|
+
case 'no-verify':
|
|
38
|
+
return { rejectUnauthorized: false }
|
|
39
39
|
}
|
|
40
40
|
return defaults.ssl
|
|
41
41
|
}
|
|
@@ -66,15 +66,22 @@ var ConnectionParameters = function (config) {
|
|
|
66
66
|
configurable: true,
|
|
67
67
|
enumerable: false,
|
|
68
68
|
writable: true,
|
|
69
|
-
value: val('password', config)
|
|
69
|
+
value: val('password', config),
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
this.binary = val('binary', config)
|
|
73
|
-
|
|
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
|
+
|
|
74
81
|
this.client_encoding = val('client_encoding', config)
|
|
75
82
|
this.replication = val('replication', config)
|
|
76
83
|
// a domain socket begins with '/'
|
|
77
|
-
this.isDomainSocket =
|
|
84
|
+
this.isDomainSocket = !(this.host || '').indexOf('/')
|
|
78
85
|
|
|
79
86
|
this.application_name = val('application_name', config, 'PGAPPNAME')
|
|
80
87
|
this.fallback_application_name = val('fallback_application_name', config, false)
|