From 27bee1d0bcc105c6dabea68b197cd25030acda0d Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Sat, 10 Dec 2016 15:16:51 -0800 Subject: [PATCH 1/9] Fix CI (#1179) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use container-based CI * Remove unnecessary CI configuration * Use Node 6/PostgreSQL 9.6 as default test … rather than testing 0.10 twice with unspecified PostgreSQL. * Use `precise` for PostgreSQL 9.1 According to https://docs.travis-ci.com/user/database-setup/, 9.1 isn’t supported on trusty. * Fix Node 0.10 and 0.12 CI builds These binaries appear to have been built using g++ with flags that clang doesn’t support. Or something. --- .travis.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78d7f67..74f1e4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,35 +1,35 @@ language: node_js -sudo: required +sudo: false dist: trusty before_script: - node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres env: - CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres +node_js: "6" addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-4.8 + postgresql: "9.6" matrix: include: - node_js: "0.10" addons: - postgresql: "9.5" + postgresql: "9.6" + env: [] - node_js: "0.12" addons: - postgresql: "9.5" + postgresql: "9.6" + env: [] - node_js: "4" addons: - postgresql: "9.5" + postgresql: "9.6" - node_js: "5" addons: - postgresql: "9.5" + postgresql: "9.6" - node_js: "6" addons: postgresql: "9.1" + dist: precise - node_js: "6" addons: postgresql: "9.2" @@ -41,4 +41,4 @@ matrix: postgresql: "9.4" - node_js: "6" addons: - postgresql: "9.5" + postgresql: "9.5" From 5d821c3acb5a2e72e3353f295b07568d521b0f57 Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Sat, 10 Dec 2016 15:28:48 -0800 Subject: [PATCH 2/9] Use more correct escaping for array elements (#1177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s not JSON. --- lib/utils.js | 14 +++++++++--- test/integration/client/array-tests.js | 30 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index b70be5b..017aa5c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,6 +8,14 @@ var defaults = require('./defaults'); +function escapeElement(elementRepresentation) { + var escaped = elementRepresentation + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"'); + + return '"' + escaped + '"'; +} + // convert a JS array to a postgres array literal // uses comma separator so won't work for types like box that use // a different array separator. @@ -25,7 +33,7 @@ function arrayString(val) { } else { - result = result + JSON.stringify(prepareValue(val[i])); + result += escapeElement(prepareValue(val[i])); } } result = result + '}'; @@ -104,7 +112,7 @@ function dateToString(date) { } function dateToStringUTC(date) { - + var ret = pad(date.getUTCFullYear(), 4) + '-' + pad(date.getUTCMonth() + 1, 2) + '-' + pad(date.getUTCDate(), 2) + 'T' + @@ -112,7 +120,7 @@ function dateToStringUTC(date) { pad(date.getUTCMinutes(), 2) + ':' + pad(date.getUTCSeconds(), 2) + '.' + pad(date.getUTCMilliseconds(), 3); - + return ret + "+00:00"; } diff --git a/test/integration/client/array-tests.js b/test/integration/client/array-tests.js index e01a252..2525b8a 100644 --- a/test/integration/client/array-tests.js +++ b/test/integration/client/array-tests.js @@ -1,6 +1,36 @@ var helper = require(__dirname + "/test-helper"); var pg = helper.pg; +test('serializing arrays', function() { + pg.connect(helper.config, assert.calls(function(err, client, done) { + assert.isNull(err); + + test('nulls', function() { + client.query('SELECT $1::text[] as array', [[null]], assert.success(function(result) { + var array = result.rows[0].array; + assert.lengthIs(array, 1); + assert.isNull(array[0]); + })); + }); + + test('elements containing JSON-escaped characters', function() { + var param = '\\"\\"'; + + for (var i = 1; i <= 0x1f; i++) { + param += String.fromCharCode(i); + } + + client.query('SELECT $1::text[] as array', [[param]], assert.success(function(result) { + var array = result.rows[0].array; + assert.lengthIs(array, 1); + assert.equal(array[0], param); + })); + + done(); + }); + })); +}); + test('parsing array results', function() { pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err); From 5feacd66d0499ff672f52bc3b68db657eeafa556 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sat, 10 Dec 2016 18:17:09 -0600 Subject: [PATCH 3/9] Remove redundant test This functionality is already tested in the node-pg-types repo. --- test/integration/client/type-coercion-tests.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 70eff30..d961c29 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -198,19 +198,3 @@ helper.pg.connect(helper.config, assert.calls(function(err, client, done) { done(); }) })) - -if(!helper.config.binary) { - test("postgres date type", function() { - var client = helper.client(); - var testDate = new Date(2010, 9, 31); - client.on('error', function(err) { - console.log(err); - client.end(); - }); - client.query("SELECT $1::date", [testDate], assert.calls(function(err, result){ - assert.isNull(err); - assert.strictEqual(result.rows[0].date.toString(), new Date(Date.UTC(2010, 9, 31)).toString()); - })); - client.on('drain', client.end.bind(client)); - }); -} From c4879e321da1f3c0229fdd303b19b9f02503578c Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sat, 10 Dec 2016 18:58:27 -0600 Subject: [PATCH 4/9] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index afc5313..01545dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "6.1.0", + "version": "6.1.1", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "postgres", From 48a9738a0b6a43600e106788e6c7824bd2f07afd Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Tue, 13 Dec 2016 05:36:13 -0800 Subject: [PATCH 5/9] Run inbound parser tests (#1182) They were disabled by 4cdd7a116bab03c6096ab1af8f0f522b04993768 without comment; it seems that this might have been unintentional? In any case, they should probably be enabled, updated, or removed. --- test/unit/connection/inbound-parser-tests.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/connection/inbound-parser-tests.js b/test/unit/connection/inbound-parser-tests.js index 55d71d5..13e6fd9 100644 --- a/test/unit/connection/inbound-parser-tests.js +++ b/test/unit/connection/inbound-parser-tests.js @@ -1,5 +1,4 @@ require(__dirname+'/test-helper'); -return false; var Connection = require(__dirname + '/../../../lib/connection'); var buffers = require(__dirname + '/../../test-buffers'); var PARSE = function(buffer) { From 981960b445c041ad8c97c631470b5e5c367eb60f Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Tue, 13 Dec 2016 05:50:07 -0800 Subject: [PATCH 6/9] Remove confusing conditions (#1159) * Remove unreachable code * Remove redundant condition Every path with `!this.values` results in `false` regardless of `this.binary`. --- lib/query.js | 6 ++---- lib/utils.js | 3 --- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/query.js b/lib/query.js index 9e474d1..36d52ba 100644 --- a/lib/query.js +++ b/lib/query.js @@ -65,11 +65,9 @@ Query.prototype.requiresPreparation = function() { if(this.rows) { return true; } //don't prepare empty text queries if(!this.text) { return false; } - //binary should be prepared to specify results should be in binary - //unless there are no parameters - if(this.binary && !this.values) { return false; } //prepare if there are values - return (this.values || 0).length > 0; + if(!this.values) { return false; } + return this.values.length > 0; }; diff --git a/lib/utils.js b/lib/utils.js index 017aa5c..861b7c5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -64,9 +64,6 @@ var prepareValue = function(val, seen) { if(typeof val === 'object') { return prepareObject(val, seen); } - if (typeof val === 'undefined') { - throw new Error('SQL queries with undefined where clause option'); - } return val.toString(); }; From 7f35240a5ceb7d89c5a72c1297b56a8e4031eaac Mon Sep 17 00:00:00 2001 From: Brian C Date: Tue, 13 Dec 2016 11:51:36 -0600 Subject: [PATCH 7/9] Fix for utf-8 characters in md5 passwords (#1183) This is the same fix as supplied in 1178 but includes a test. Closes #1178 --- lib/client.js | 2 +- test/unit/client/md5-password-tests.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 54ab017..770f353 100644 --- a/lib/client.js +++ b/lib/client.js @@ -344,7 +344,7 @@ Client.prototype.end = function(cb) { }; Client.md5 = function(string) { - return crypto.createHash('md5').update(string).digest('hex'); + return crypto.createHash('md5').update(string, 'utf-8').digest('hex'); }; // expose a Query constructor diff --git a/test/unit/client/md5-password-tests.js b/test/unit/client/md5-password-tests.js index bd5ca46..3c54036 100644 --- a/test/unit/client/md5-password-tests.js +++ b/test/unit/client/md5-password-tests.js @@ -17,5 +17,8 @@ test('md5 authentication', function() { .addCString(password).join(true,'p')); }); }); - +}); + +test('md5 of utf-8 strings', function() { + assert.equal(Client.md5('😊'), '5deda34cd95f304948d2bc1b4a62c11e'); }); From 2c636c750fb0acf7735c684403edb613b0345a93 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Tue, 13 Dec 2016 11:53:57 -0600 Subject: [PATCH 8/9] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 01545dc..017e04c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "6.1.1", + "version": "6.1.2", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "postgres", From f6c40b9331c90d794d5fcbb1d4ae2f28eabd4d42 Mon Sep 17 00:00:00 2001 From: Alexander Mochalin Date: Fri, 16 Dec 2016 20:44:19 +0500 Subject: [PATCH 9/9] parse int8[] (#1152) * parse int8[] * missing semicolon * test * test fixed * test fixed * test fixed. again. --- lib/defaults.js | 8 +++++++- test/integration/client/parse-int-8-tests.js | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/defaults.js b/lib/defaults.js index a05c21f..b99e4a8 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -62,7 +62,13 @@ var defaults = module.exports = { parseInputDatesAsUTC: false }; +var pgTypes = require('pg-types'); +// save default parsers +var parseBigInteger = pgTypes.getTypeParser(20, 'text'); +var parseBigIntegerArray = pgTypes.getTypeParser(1016, 'text'); + //parse int8 so you can get your count values as actual numbers module.exports.__defineSetter__("parseInt8", function(val) { - require('pg-types').setTypeParser(20, 'text', val ? parseInt : function(val) { return val; }); + pgTypes.setTypeParser(20, 'text', val ? pgTypes.getTypeParser(23, 'text') : parseBigInteger); + pgTypes.setTypeParser(1016, 'text', val ? pgTypes.getTypeParser(1007, 'text') : parseBigIntegerArray); }); diff --git a/test/integration/client/parse-int-8-tests.js b/test/integration/client/parse-int-8-tests.js index 7028e90..42228cb 100644 --- a/test/integration/client/parse-int-8-tests.js +++ b/test/integration/client/parse-int-8-tests.js @@ -6,11 +6,18 @@ test('ability to turn on and off parser', function() { pg.connect(helper.config, assert.success(function(client, done) { pg.defaults.parseInt8 = true; client.query('CREATE TEMP TABLE asdf(id SERIAL PRIMARY KEY)'); - client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) { + client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) { + assert.strictEqual(0, res.rows[0].count); + assert.strictEqual(1, res.rows[0].array[0]); + assert.strictEqual(2, res.rows[0].array[1]); + assert.strictEqual(3, res.rows[0].array[2]); pg.defaults.parseInt8 = false; - client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) { + client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) { done(); - assert.strictEqual("0", res.rows[0].count); + assert.strictEqual('0', res.rows[0].count); + assert.strictEqual('1', res.rows[0].array[0]); + assert.strictEqual('2', res.rows[0].array[1]); + assert.strictEqual('3', res.rows[0].array[2]); pg.end(); })); }));