Skip to content

Stream fix #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ var Connection = function(config) {
util.inherits(Connection, EventEmitter);

Connection.prototype.connect = function(port, host) {

if(this.stream.readyState === 'closed') {
// Old info regarding readyState https://github.com/nodejs/node-v0.x-archive/issues/1752
// this previously checked for this.stream.readyState === 'open'
// however when upgrading to node 14 - LTS readyState was always open
// which prevented code that connected from being called
if (port && host) {
this.stream.connect(port, host);
} else if(this.stream.readyState == 'open') {
this.emit('connect');
}

Expand Down
15 changes: 8 additions & 7 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

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 @@ -75,13 +76,13 @@ Result.prototype.addRow = function(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
fieldName.replace(/'/g, "\\'") +
// 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 + "]);";
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "6.1.5",
"version": "6.1.6",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"postgres",
Expand All @@ -19,6 +19,7 @@
"main": "./lib",
"dependencies": {
"buffer-writer": "1.0.1",
"js-string-escape": "1.0.1",
"packet-reader": "0.2.0",
"pg-connection-string": "0.1.3",
"pg-pool": "1.*",
Expand Down
10 changes: 10 additions & 0 deletions test/integration/client/field-name-escape-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var pg = require('./test-helper').pg

var sql = 'SELECT 1 AS "\\\'/*", 2 AS "\\\'*/\n + process.exit(-1)] = null;\n//"'

var client = new pg.Client()
client.connect()
client.query(sql, function (err, res) {
if (err) throw err
client.end()
})