Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Feb 25, 2020
2 parents 5341a2a + 1c8b6b9 commit d5dc421
Show file tree
Hide file tree
Showing 24 changed files with 270 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ package-lock.json
dist
.DS_Store
.vscode/
manually-test-on-heroku.js
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ env:
node_js:
- lts/dubnium
- lts/erbium
# node 13.7 seems to have changed behavior of async iterators exiting early on streams
# if 13.8 still has this problem when it comes down I'll talk to the node team about the change
# in the mean time...peg to 13.6
- 13.6

addons:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ For richer information consult the commit log on github with referenced pull req

We do not include break-fix version release in this file.

### pg@7.18.0

- This will likely be the last minor release before pg@8.0.
- This version contains a few bug fixes and adds a deprecation warning for [a pending change in 8.0](https://github.com/brianc/node-postgres/issues/2009#issuecomment-579371651) which will flip the default behavior over SSL from `rejectUnauthorized` from `false` to `true` making things more secure in the general use case.

### pg-query-stream@3.0.0

- [Rewrote stream internals](https://github.com/brianc/node-postgres/pull/2051) to better conform to node stream semantics. This should make pg-query-stream much better at respecting [highWaterMark](https://nodejs.org/api/stream.html#stream_new_stream_readable_options) and getting rid of some edge case bugs when using pg-query-stream as an async iterator. Due to the size and nature of this change (effectively a full re-write) it's safest to bump the semver major here, though almost all tests remain untouched and still passing, which brings us to a breaking change to the API....
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ When you open an issue please provide:

You can also follow me [@briancarlson](https://twitter.com/briancarlson) if that's your thing. I try to always announce noteworthy changes & developments with node-postgres on Twitter.

### Sponsorship :star:
## Sponsorship :two_hearts:

[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.
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:

Also, you can view a historical list of all [previous and existing sponsors](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md).
<div align="center">
<a href="https://www.timescale.com" target="_blank">
<img height="80" src="https://node-postgres.com/timescale.svg" />
</a>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAQAAAB0m0auAAAADElEQVR42mNkIBIAAABSAAI2VLqiAAAAAElFTkSuQmCC" />
<a href="https://crate.io" target="_blank">
<img height="80" src="https://node-postgres.com/crate-io.png" />
</a>
</div>

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.

## Contributing

Expand Down
3 changes: 2 additions & 1 deletion SPONSORS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
node-postgres is made possible by the helpful contributors from the community well as the following generous supporters on [Patreon](https://www.patreon.com/node_postgres).
node-postgres is made possible by the helpful contributors from the community as well as the following generous supporters on [GitHub Sponsors](https://github.com/sponsors/brianc) and [Patreon](https://www.patreon.com/node_postgres).

# Leaders

- [MadKudu](https://www.madkudu.com) - [@madkudu](https://twitter.com/madkudu)
- [Third Iron](https://thirdiron.com/)
- [Timescale](https://timescale.com)
- [Nafundi](https://nafundi.com)
- [CrateDB](https://crate.io/)

# Supporters

Expand Down
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"useWorkspaces": true,
"version": "independent",
"ignoreChanges": [
"**/*.md"
"**/*.md",
"**/test/**"
]
}
4 changes: 2 additions & 2 deletions packages/pg-cursor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-cursor",
"version": "2.1.3",
"version": "2.1.6",
"description": "Query cursor extension for node-postgres",
"main": "index.js",
"directories": {
Expand All @@ -21,7 +21,7 @@
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-prettier": "^3.1.1",
"mocha": "^6.2.2",
"pg": "^7.17.1",
"pg": "^7.18.2",
"prettier": "^1.18.2"
},
"prettier": {
Expand Down
9 changes: 7 additions & 2 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class PendingItem {
}
}

function throwOnDoubleRelease () {
throw new Error('Release called on client which has already been released to the pool.')
}

function promisify (Promise, callback) {
if (callback) {
return { callback: callback, result: undefined }
Expand Down Expand Up @@ -256,7 +260,7 @@ class Pool extends EventEmitter {

client.release = (err) => {
if (released) {
throw new Error('Release called on client which has already been released to the pool.')
throwOnDoubleRelease()
}

released = true
Expand Down Expand Up @@ -292,7 +296,8 @@ class Pool extends EventEmitter {
_release (client, idleListener, err) {
client.on('error', idleListener)

if (err || this.ending) {
// TODO(bmc): expose a proper, public interface _queryable and _ending
if (err || this.ending || !client._queryable || client._ending) {
this._remove(client)
this._pulseQueue()
return
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-pool/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-pool",
"version": "2.0.9",
"version": "2.0.10",
"description": "Connection pool for node-postgres",
"main": "index.js",
"directories": {
Expand Down
8 changes: 4 additions & 4 deletions packages/pg-pool/test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ describe('pool error handling', function () {
const pool = new Pool({ max: 1, port: closeServer.address().port, host: 'localhost' })
pool.connect((err) => {
expect(err).to.be.an(Error)
if (err.errno) {
expect(err.errno).to.be('ECONNRESET')
if (err.code) {
expect(err.code).to.be('ECONNRESET')
}
})
pool.connect((err) => {
expect(err).to.be.an(Error)
if (err.errno) {
expect(err.errno).to.be('ECONNRESET')
if (err.code) {
expect(err.code).to.be('ECONNRESET')
}
closeServer.close(() => {
pool.end(done)
Expand Down
54 changes: 54 additions & 0 deletions packages/pg-pool/test/releasing-clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Pool = require('../')

const expect = require('expect.js')
const net = require('net')

describe('releasing clients', () => {
it('removes a client which cannot be queried', async () => {
// make a pool w/ only 1 client
const pool = new Pool({ max: 1 })
expect(pool.totalCount).to.eql(0)
const client = await pool.connect()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)
// reach into the client and sever its connection
client.connection.end()

// wait for the client to error out
const err = await new Promise((resolve) => client.once('error', resolve))
expect(err).to.be.ok()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)

// try to return it to the pool - this removes it because its broken
client.release()
expect(pool.totalCount).to.eql(0)
expect(pool.idleCount).to.eql(0)

// make sure pool still works
const { rows } = await pool.query('SELECT NOW()')
expect(rows).to.have.length(1)
await pool.end()
})

it('removes a client which is ending', async () => {
// make a pool w/ only 1 client
const pool = new Pool({ max: 1 })
expect(pool.totalCount).to.eql(0)
const client = await pool.connect()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)
// end the client gracefully (but you shouldn't do this with pooled clients)
client.end()

// try to return it to the pool
client.release()
expect(pool.totalCount).to.eql(0)
expect(pool.idleCount).to.eql(0)

// make sure pool still works
const { rows } = await pool.query('SELECT NOW()')
expect(rows).to.have.length(1)
await pool.end()
})
})
1 change: 1 addition & 0 deletions packages/pg-query-stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PgQueryStream extends Readable {
this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor)
this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor)
this.handleError = this.cursor.handleError.bind(this.cursor)
this.handleEmptyQuery = this.cursor.handleEmptyQuery.bind(this.cursor)
}

submit(connection) {
Expand Down
6 changes: 3 additions & 3 deletions packages/pg-query-stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-query-stream",
"version": "3.0.0",
"version": "3.0.3",
"description": "Postgres query result returned as readable stream",
"main": "index.js",
"scripts": {
Expand All @@ -27,12 +27,12 @@
"concat-stream": "~1.0.1",
"eslint-plugin-promise": "^3.5.0",
"mocha": "^6.2.2",
"pg": "^7.17.1",
"pg": "^7.18.2",
"stream-spec": "~0.3.5",
"stream-tester": "0.0.5",
"through": "~2.3.4"
},
"dependencies": {
"pg-cursor": "^2.1.3"
"pg-cursor": "^2.1.6"
}
}
20 changes: 20 additions & 0 deletions packages/pg-query-stream/test/empty-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require('assert')
const helper = require('./helper')
const QueryStream = require('../')

helper('empty-query', function (client) {
it('handles empty query', function(done) {
const stream = new QueryStream('-- this is a comment', [])
const query = client.query(stream)
query.on('end', function () {
// nothing should happen for empty query
done();
}).on('data', function () {
// noop to kick off reading
})
})

it('continues to function after stream', function (done) {
client.query('SELECT NOW()', done)
})
})
16 changes: 13 additions & 3 deletions packages/pg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ When you open an issue please provide:

You can also follow me [@briancarlson](https://twitter.com/briancarlson) if that's your thing. I try to always announce noteworthy changes & developments with node-postgres on Twitter.

### Sponsorship :star:
## Sponsorship :two_hearts:

[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.
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:

Also, you can view a historical list of all [previous and existing sponsors](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md).
<div align="center">
<a href="https://www.timescale.com" target="_blank">
<img height="80" src="https://node-postgres.com/timescale.svg" />
</a>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAQAAAB0m0auAAAADElEQVR42mNkIBIAAABSAAI2VLqiAAAAAElFTkSuQmCC" />
<a href="https://crate.io" target="_blank">
<img height="80" src="https://node-postgres.com/crate-io.png" />
</a>
</div>

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.


## Contributing
Expand Down
11 changes: 10 additions & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,16 @@ Client.prototype.query = function (config, values, callback) {
Client.prototype.end = function (cb) {
this._ending = true

if (this.activeQuery) {
// if we have never connected, then end is a noop, callback immediately
if (this.connection.stream.readyState === 'closed') {
if (cb) {
cb()
} else {
return this._Promise.resolve()
}
}

if (this.activeQuery || !this._queryable) {
// if we have an active query we need to force a disconnect
// on the socket - otherwise a hung query could block end forever
this.connection.stream.destroy()
Expand Down
13 changes: 9 additions & 4 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ Connection.prototype.connect = function (port, host) {
this.stream.once('data', function (buffer) {
var responseCode = buffer.toString('utf8')
switch (responseCode) {
case 'N': // Server does not support SSL connections
return self.emit('error', new Error('The server does not support SSL connections'))
case 'S': // Server supports SSL connections, continue with a secure connection
break
case 'N': // Server does not support SSL connections
self.stream.end()
return self.emit('error', new Error('The server does not support SSL connections'))
default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
self.stream.end()
return self.emit('error', new Error('There was an error establishing an SSL connection'))
}
var tls = require('tls')
Expand All @@ -98,9 +100,8 @@ Connection.prototype.connect = function (port, host) {
options.servername = host
}
self.stream = tls.connect(options)
self.attachListeners(self.stream)
self.stream.on('error', reportStreamError)

self.attachListeners(self.stream)
self.emit('sslconnect')
})
}
Expand Down Expand Up @@ -331,6 +332,10 @@ Connection.prototype.end = function () {
// 0x58 = 'X'
this.writer.add(emptyBuffer)
this._ending = true
if (!this.stream.writable) {
this.stream.end()
return
}
return this.stream.write(END_BUFFER, () => {
this.stream.end()
})
Expand Down
5 changes: 4 additions & 1 deletion packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ Client.prototype._connect = function (cb) {
this.connectionParameters.getLibpqConnectionString(function (err, conString) {
if (err) return cb(err)
self.native.connect(conString, function (err) {
if (err) return cb(err)
if (err) {
self.native.end()
return cb(err)
}

// set internal states to connected
self._connected = true
Expand Down
4 changes: 2 additions & 2 deletions packages/pg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "7.17.1",
"version": "7.18.2",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"database",
Expand All @@ -23,7 +23,7 @@
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-packet-stream": "^1.1.0",
"pg-pool": "^2.0.9",
"pg-pool": "^2.0.10",
"pg-types": "^2.1.0",
"pgpass": "1.x",
"semver": "4.3.2"
Expand Down

0 comments on commit d5dc421

Please sign in to comment.