Skip to content

Commit

Permalink
Merge branch 'master' into queued-query-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
charmander committed May 4, 2018
2 parents a55da8a + 83ede28 commit 70a2b56
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 72 deletions.
24 changes: 15 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,34 @@ env:

matrix:
include:
- node_js: "lts/boron"
addons:
postgresql: "9.6"
- node_js: "lts/argon"
addons:
postgresql: "9.6"
- node_js: "lts/boron"
- node_js: "9"
addons:
postgresql: "9.6"
- node_js: "10"
addons:
postgresql: "9.6"
- node_js: "lts/carbon"
addons:
postgresql: "9.1"
dist: precise
- node_js: "lts/boron"
- node_js: "lts/carbon"
addons:
postgresql: "9.2"
- node_js: "lts/boron"
- node_js: "lts/carbon"
addons:
postgresql: "9.3"
- node_js: "lts/boron"
- node_js: "lts/carbon"
addons:
postgresql: "9.4"
- node_js: "lts/boron"
- node_js: "lts/carbon"
addons:
postgresql: "9.5"
- node_js: "lts/boron"
addons:
postgresql: "9.6"
- node_js: "8"
- node_js: "lts/carbon"
addons:
postgresql: "9.6"
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var pg = require('pg')
var pool = new pg.Pool()
// your friendly neighboorhood pool interface, without the singleton
// your friendly neighborhood pool interface, without the singleton
pool.connect(function(err, client, done) {
// ...
})
Expand Down Expand Up @@ -113,7 +113,7 @@ client.query('SELECT $1::text as name', ['brianc'])
- Add option to parse JS date objects in query parameters as [UTC](https://github.com/brianc/node-postgres/pull/943)

### v4.4.0
- Warn to `stderr` if a named query exceeds 63 characters which is the max lenght supported by postgres.
- Warn to `stderr` if a named query exceeds 63 characters which is the max length supported by postgres.

### v4.3.0
- Unpin `pg-types` semver. Allow it to float against `pg-types@1.x`.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2010 - 2017 Brian Carlson
Copyright (c) 2010 - 2018 Brian Carlson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<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>
<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>

Non-blocking PostgreSQL client for node.js. Pure JavaScript and optional native libpq bindings.
Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.

## Install

Expand All @@ -19,9 +19,9 @@ $ npm install pg

### Features

* Pure JavaScript client and native libpq bindings share _the same api_
* Pure JavaScript client and native libpq bindings share _the same API_
* Connection pooling
* Extensible js<->postgresql data-type coercion
* Extensible JS<->PostgreSQL data-type coercion
* Supported PostgreSQL features
* Parameterized queries
* Named statements with query plan caching
Expand All @@ -35,18 +35,18 @@ The entire list can be found on our [wiki](https://github.com/brianc/node-postgr

## Support

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!
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!

When you open an issue please provide:
- version of node
- version of postgres
- version of Node
- version of Postgres
- smallest possible snippet of code to reproduce the problem

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.

### Professional Support

I offer professional support for node-postgres. I provide implementation, training, and many years of expertise on how to build applications with node, express, PostgreSQL, and react/redux. Please contact me at [brian.m.carlson@gmail.com](mailto:brian.m.carlson@gmail.com) to discuss how I can help your company be more successful!
I offer professional support for node-postgres. I provide implementation, training, and many years of expertise on how to build applications with Node, Express, PostgreSQL, and React/Redux. Please contact me at [brian.m.carlson@gmail.com](mailto:brian.m.carlson@gmail.com) to discuss how I can help your company be more successful!

### Sponsorship :star:

Expand All @@ -63,11 +63,11 @@ I will __happily__ accept your pull request if it:

## Troubleshooting and FAQ

The causes and solutions to common errors can be found among the [Frequently Asked Questions(FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)
The causes and solutions to common errors can be found among the [Frequently Asked Questions (FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)

## License

Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
Copyright (c) 2010-2018 Brian Carlson (brian.m.carlson@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 2 additions & 0 deletions SPONSORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ node-postgres is made possible by the helpful contributors from the community we
- Rein Petersen
- Arnaud Benhamdine [@abenhamdine](https://twitter.com/abenhamdine)
- Matthew Welke
- Matthew Weber
- Benjie Gillam
31 changes: 19 additions & 12 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var BINARY_MODE = 1
var Connection = function (config) {
EventEmitter.call(this)
config = config || {}
this.stream = config.stream || new net.Stream()
this.stream = config.stream || new net.Socket()
this._keepAlive = config.keepAlive
this.lastBuffer = false
this.lastOffset = 0
Expand Down Expand Up @@ -62,14 +62,15 @@ Connection.prototype.connect = function (port, host) {
self.emit('connect')
})

this.stream.on('error', function (error) {
const reportStreamError = function (error) {
// don't raise ECONNRESET errors - they can & should be ignored
// during disconnect
if (self._ending && error.code === 'ECONNRESET') {
return
}
self.emit('error', error)
})
}
this.stream.on('error', reportStreamError)

this.stream.on('close', function () {
self.emit('end')
Expand All @@ -81,13 +82,19 @@ Connection.prototype.connect = function (port, host) {

this.stream.once('data', function (buffer) {
var responseCode = buffer.toString('utf8')
if (responseCode !== 'S') {
return self.emit('error', new Error('The server does not support SSL connections'))
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
default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
return self.emit('error', new Error('There was an error establishing an SSL connection'))
}
var tls = require('tls')
self.stream = tls.connect({
socket: self.stream,
servername: host,
checkServerIdentity: self.ssl.checkServerIdentity,
rejectUnauthorized: self.ssl.rejectUnauthorized,
ca: self.ssl.ca,
pfx: self.ssl.pfx,
Expand All @@ -97,11 +104,9 @@ Connection.prototype.connect = function (port, host) {
NPNProtocols: self.ssl.NPNProtocols
})
self.attachListeners(self.stream)
self.emit('sslconnect')
self.stream.on('error', reportStreamError)

self.stream.on('error', function (error) {
self.emit('error', error)
})
self.emit('sslconnect')
})
}

Expand Down Expand Up @@ -311,7 +316,9 @@ Connection.prototype.end = function () {
// 0x58 = 'X'
this.writer.add(emptyBuffer)
this._ending = true
return this.stream.write(END_BUFFER)
return this.stream.write(END_BUFFER, () => {
this.stream.end()
})
}

Connection.prototype.close = function (msg, more) {
Expand Down Expand Up @@ -619,13 +626,13 @@ Connection.prototype.parsed = function (buffer, length) {
}

Connection.prototype.parseInt32 = function (buffer) {
var value = buffer.readInt32BE(this.offset, true)
var value = buffer.readInt32BE(this.offset)
this.offset += 4
return value
}

Connection.prototype.parseInt16 = function (buffer) {
var value = buffer.readInt16BE(this.offset, true)
var value = buffer.readInt16BE(this.offset)
this.offset += 2
return value
}
Expand Down
6 changes: 3 additions & 3 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Query.prototype.handlePortalSuspended = function (connection) {

Query.prototype._getRows = function (connection, rows) {
connection.execute({
portal: this.portalName,
portal: this.portal,
rows: rows
}, true)
connection.flush()
Expand All @@ -196,15 +196,15 @@ Query.prototype.prepare = function (connection) {

// http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
connection.bind({
portal: self.portalName,
portal: self.portal,
statement: self.name,
values: self.values,
binary: self.binary
}, true)

connection.describe({
type: 'P',
name: self.portalName || ''
name: self.portal || ''
}, true)

this._getRows(connection, this.rows)
Expand Down
35 changes: 11 additions & 24 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

var types = require('pg-types')
var escape = require('js-string-escape')

// result object returned from query
// in the 'end' event and also
Expand Down Expand Up @@ -65,29 +64,24 @@ Result.prototype._parseRowAsArray = function (rowData) {
return row
}

// rowData is an array of text or binary values
// this turns the row into a JavaScript object
Result.prototype.parseRow = function (rowData) {
return new this.RowCtor(this._parsers, rowData)
var row = {}
for (var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i]
var field = this.fields[i].name
if (rawValue !== null) {
row[field] = this._parsers[i](rawValue)
} else {
row[field] = null
}
}
return row
}

Result.prototype.addRow = function (row) {
this.rows.push(row)
}

var inlineParser = function (fieldName, i) {
return "\nthis['" +
// fields containing single quotes will break
// the evaluated javascript unless they are escaped
// see https://github.com/brianc/node-postgres/issues/507
// Addendum: However, we need to make sure to replace all
// occurences of apostrophes, not just the first one.
// See https://github.com/brianc/node-postgres/issues/934
escape(fieldName) +
"'] = " +
'rowData[' + i + '] == null ? null : parsers[' + i + '](rowData[' + i + ']);'
}

Result.prototype.addFields = function (fieldDescriptions) {
// clears field definitions
// multiple query statements in 1 action can result in multiple sets
Expand All @@ -97,18 +91,11 @@ Result.prototype.addFields = function (fieldDescriptions) {
this.fields = []
this._parsers = []
}
var ctorBody = ''
for (var i = 0; i < fieldDescriptions.length; i++) {
var desc = fieldDescriptions[i]
this.fields.push(desc)
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text')
this._parsers.push(parser)
// this is some craziness to compile the row result parsing
// results in ~60% speedup on large query result sets
ctorBody += inlineParser(desc.name, i)
}
if (!this.rowAsArray) {
this.RowCtor = Function('parsers', 'rowData', ctorBody)
}
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "7.4.0",
"version": "7.4.2",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"database",
Expand All @@ -20,7 +20,6 @@
"main": "./lib",
"dependencies": {
"buffer-writer": "1.0.1",
"js-string-escape": "1.0.1",
"packet-reader": "0.3.1",
"pg-connection-string": "0.1.3",
"pg-pool": "~2.0.3",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/client/network-partition-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Server.prototype.start = function (cb) {
this.socket.on('data', function (data) {
// deny request for SSL
if (data.length == 8) {
this.socket.write(new Buffer('N', 'utf8'))
this.socket.write(Buffer.from('N', 'utf8'))
// consider all authentication requests as good
} else if (!data[0]) {
this.socket.write(buffers.authenticationOk())
Expand All @@ -46,7 +46,7 @@ Server.prototype.start = function (cb) {
}

Server.prototype.drop = function () {
this.socket.end()
this.socket.destroy()
}

Server.prototype.close = function (cb) {
Expand Down

0 comments on commit 70a2b56

Please sign in to comment.