diff --git a/.travis.yml b/.travis.yml index 74f1e4c0c..c115a5754 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,43 +2,25 @@ language: node_js sudo: false dist: trusty before_script: - - node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres + - node script/create-test-tables.js pg://postgres@127.0.0.1:$PGPORT/postgres env: - CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres -node_js: "6" -addons: - postgresql: "9.6" +env: + - POSTGRESQL_VERSION="9.5" + - POSTGRESQL_VERSION="10" + +node_js: + - "6" + - "8" + + +before_install: + - sudo service postgresql stop; + - sudo apt-get install -y --allow-unauthenticated --no-install-recommends --no-install-suggests postgresql-$POSTGRESQL_VERSION postgresql-client-$POSTGRESQL_VERSION postgresql-server-dev-$POSTGRESQL_VERSION + - echo -e "# TYPE DATABASE USER ADDRESS METHOD \nlocal all postgres trust\nlocal all all trust\nhost all all 127.0.0.1/32 trust" | sudo tee /etc/postgresql/$POSTGRESQL_VERSION/main/pg_hba.conf + - export PGPORT=`grep ^port /etc/postgresql/$POSTGRESQL_VERSION/main/postgresql.conf | awk '{print $3}'` + - export PGUSER=postgres + - export PGDATABASE=postgres + - sudo service postgresql restart $POSTGRESQL_VERSION; -matrix: - include: - - node_js: "0.10" - addons: - postgresql: "9.6" - env: [] - - node_js: "0.12" - addons: - postgresql: "9.6" - env: [] - - node_js: "4" - addons: - postgresql: "9.6" - - node_js: "5" - addons: - postgresql: "9.6" - - node_js: "6" - addons: - postgresql: "9.1" - dist: precise - - node_js: "6" - addons: - postgresql: "9.2" - - node_js: "6" - addons: - postgresql: "9.3" - - node_js: "6" - addons: - postgresql: "9.4" - - node_js: "6" - addons: - postgresql: "9.5" diff --git a/CHANGELOG.md b/CHANGELOG.md index a8efbfed6..e955bb523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ For richer information consult the commit log on github with referenced pull req We do not include break-fix version release in this file. +### v6.4.0 + +- Add support for passing `client_encoding` as a connection parameter. Used when decoding strings in the JavaScript driver. The default is still `utf8`. + +### v6.3.0 + +- Deprecate `pg.connect` `pg.end` and `pg.cancel` - favor using `new pg.Pool()` instead of pg singleton. +- Deprecate undocumented but possibly used `query.promise()` method. Use the promise returned directly from `client.query` / `pool.query`. +- Deprecate returning an automatically created query result from `client.query`. Instead return more idomatic responses for callback/promise methods. + +### v6.2.0 + +- Add support for [parsing `replicationStart` messages](https://github.com/brianc/node-postgres/pull/1271/files). + ### v6.1.0 - Add optional callback parameter to the pure JavaScript `client.end` method. The native client already supported this. diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..a7b546d2d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2010 - 2017 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 +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile index 4eddd4f10..a6f7b7968 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ test: test-unit test-all: jshint test-missing-native test-unit test-integration test-native test-binary +test-all-nonative: jshint test-unit test-integration test-binary update-npm: @npm i npm --global diff --git a/README.md b/README.md index 6bb0fe0e9..679b5796d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#node-postgres +# node-postgres [![Build Status](https://secure.travis-ci.org/brianc/node-postgres.svg?branch=master)](http://travis-ci.org/brianc/node-postgres) [![Dependency Status](https://david-dm.org/brianc/node-postgres.svg)](https://david-dm.org/brianc/node-postgres) @@ -15,46 +15,36 @@ $ npm install pg ## Intro & Examples -### Simple example +There are 3 ways of executing queries -```js -var pg = require('pg'); +1. Passing the query to a pool +2. Borrowing a client from a pool and executing the query with it +3. Obtaining an exclusive client and executing the query with it -// instantiate a new client -// the client will read connection information from -// the same environment variables used by postgres cli tools -var client = new pg.Client(); +It is recommended to pass the query to a pool as often as possible. If that isn't possible, because of long and complex transactions for example, borrow a client from a pool. Just remember to initialize the pool only once in your code so you maximize reusability of connections. -// connect to our database -client.connect(function (err) { - if (err) throw err; +### Why pooling? - // execute a query on our database - client.query('SELECT $1::text as name', ['brianc'], function (err, result) { - if (err) throw err; +If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? For one thing, there is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Furthermore, PostgreSQL can support only a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a __very bad thing__. :tm: Additionally, PostgreSQL can only execute 1 query at a time per connected client, so pipelining all queries for all requests through a single, long-lived client will likely introduce a bottleneck into your application if you need high concurrency. - // just print the result to the console - console.log(result.rows[0]); // outputs: { name: 'brianc' } +With that in mind we can imagine a situation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >100 simultaneous requests your web server will attempt to open 100 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, your database will become unresponsive, your app will seem to hang, and everything will break. Boooo! - // disconnect the client - client.end(function (err) { - if (err) throw err; - }); - }); -}); +__Good news__: node-postgres ships with built in client pooling. Client pooling allows your application to use a pool of already connected clients and reuse them for each request to your application. If your app needs to make more queries than there are available clients in the pool the queries will queue instead of overwhelming your database & causing a cascading failure. :thumbsup: -``` +node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convenience. If you want, you can `require('pg-pool')` and use it directly - it's the same as the constructor exported at `pg.Pool`. -### Client pooling +It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git). -If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? For one thing, there is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Furthermore, PostgreSQL can support only a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a __very bad thing__. :tm: Additionally, PostgreSQL can only execute 1 query at a time per connected client, so pipelining all queries for all requests through a single, long-lived client will likely introduce a bottleneck into your application if you need high concurrency. +[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example) -With that in mind we can imagine a situation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >100 simultaneous requests your web server will attempt to open 100 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, your database will become unresponsive, your app will seem to hang, and everything will break. Boooo! +For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html) -__Good news__: node-postgres ships with built in client pooling. Client pooling allows your application to use a pool of already connected clients and reuse them for each request to your application. If your app needs to make more queries than there are available clients in the pool the queries will queue instead of overwhelming your database & causing a cascading failure. :thumbsup: +### Pooling example + +Let's create a pool in `./lib/db.js` which will be reused across the whole project ```javascript -var pg = require('pg'); +const pg = require('pg'); // create a config to configure both pooling behavior // and client options @@ -70,18 +60,63 @@ var config = { idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed }; - //this initializes a connection pool //it will keep idle connections open for 30 seconds //and set a limit of maximum 10 idle clients -var pool = new pg.Pool(config); +const pool = new pg.Pool(config); + +pool.on('error', function (err, client) { + // if an error is encountered by a client while it sits idle in the pool + // the pool itself will emit an error event with both the error and + // the client which emitted the original error + // this is a rare occurrence but can happen if there is a network partition + // between your application and the database, the database restarts, etc. + // and so you might want to handle it and at least log it out + console.error('idle client error', err.message, err.stack); +}); + +//export the query method for passing queries to the pool +module.exports.query = function (text, values, callback) { + console.log('query:', text, values); + return pool.query(text, values, callback); +}; + +// the pool also supports checking out a client for +// multiple operations, such as a transaction +module.exports.connect = function (callback) { + return pool.connect(callback); +}; +``` + +Now if in `./foo.js` you want to pass a query to the pool -// to run a query we can acquire a client from the pool, -// run a query on the client, and then return the client to the pool +```js +const pool = require('./lib/db'); + +//to run a query we just pass it to the pool +//after we're done nothing has to be taken care of +//we don't have to return any client to the pool or close a connection +pool.query('SELECT $1::int AS number', ['2'], function(err, res) { + if(err) { + return console.error('error running query', err); + } + + console.log('number:', res.rows[0].number); +}); +``` + +Or if in `./bar.js` you want borrow a client from the pool + +```js +const pool = require('./lib/db'); + +//ask for a client from the pool pool.connect(function(err, client, done) { if(err) { return console.error('error fetching client from pool', err); } + + //use the client for executing the query client.query('SELECT $1::int AS number', ['1'], function(err, result) { //call `done(err)` to release the client back to the pool (or destroy it if there is an error) done(err); @@ -93,27 +128,39 @@ pool.connect(function(err, client, done) { //output: 1 }); }); - -pool.on('error', function (err, client) { - // if an error is encountered by a client while it sits idle in the pool - // the pool itself will emit an error event with both the error and - // the client which emitted the original error - // this is a rare occurrence but can happen if there is a network partition - // between your application and the database, the database restarts, etc. - // and so you might want to handle it and at least log it out - console.error('idle client error', err.message, err.stack) -}) ``` -node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convenience. If you want, you can `require('pg-pool')` and use it directly - it's the same as the constructor exported at `pg.Pool`. +For more examples, including how to use a connection pool with promises and async/await see the [example](https://github.com/brianc/node-postgres/wiki/Example) page in the wiki. -It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git). +### Obtaining an exclusive client, example +```js +var pg = require('pg'); -[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example) +// instantiate a new client +// the client will read connection information from +// the same environment variables used by postgres cli tools +var client = new pg.Client(); + +// connect to our database +client.connect(function (err) { + if (err) throw err; + // execute a query on our database + client.query('SELECT $1::text as name', ['brianc'], function (err, result) { + if (err) throw err; -For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html) + // just print the result to the console + console.log(result.rows[0]); // outputs: { name: 'brianc' } + + // disconnect the client + client.end(function (err) { + if (err) throw err; + }); + }); +}); + +``` ## [More Documentation](https://github.com/brianc/node-postgres/wiki) @@ -183,6 +230,10 @@ Information about the testing processes is in the [wiki](https://github.com/bria Open source belongs to all of us, and we're all invited to participate! +## 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) + ## Support If at all possible when you open an issue please provide @@ -202,7 +253,7 @@ Follow me [@briancarlson](https://twitter.com/briancarlson) to keep up to date. ## License -Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) +Copyright (c) 2010-2017 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 diff --git a/lib/client.js b/lib/client.js index 86206ab8c..fa3716cd2 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -13,6 +13,7 @@ var pgPass = require('pgpass'); var TypeOverrides = require('./type-overrides'); var ConnectionParameters = require('./connection-parameters'); +var utils = require('./utils'); var Query = require('./query'); var defaults = require('./defaults'); var Connection = require('./connection'); @@ -26,19 +27,24 @@ var Client = function(config) { this.port = this.connectionParameters.port; this.host = this.connectionParameters.host; this.password = this.connectionParameters.password; + this.replication = this.connectionParameters.replication; var c = config || {}; this._types = new TypeOverrides(c.types); + this._ending = false; + this._connecting = false; + this._connectionError = false; this.connection = c.connection || new Connection({ stream: c.stream, ssl: this.connectionParameters.ssl, - keepAlive: c.keepAlive || false + keepAlive: c.keepAlive || false, + client_encoding: this.connectionParameters.client_encoding || 'utf8', }); this.queryQueue = []; this.binary = c.binary || defaults.binary; - this.encoding = 'utf8'; + this.encoding = this.connectionParameters.client_encoding || 'utf8'; this.processID = null; this.secretKey = null; this.ssl = this.connectionParameters.ssl || false; @@ -49,6 +55,7 @@ util.inherits(Client, EventEmitter); Client.prototype.connect = function(callback) { var self = this; var con = this.connection; + this._connecting = true; if(this.host && this.host.indexOf('/') === 0) { con.connect(this.host + '/.s.PGSQL.' + this.port); @@ -106,6 +113,7 @@ Client.prototype.connect = function(callback) { //hook up query handling events to connection //after the connection initially becomes ready for queries con.once('readyForQuery', function() { + self._connecting = false; //delegate rowDescription to active query con.on('rowDescription', function(msg) { @@ -178,34 +186,52 @@ Client.prototype.connect = function(callback) { }); con.on('error', function(error) { - if(self.activeQuery) { + if(this.activeQuery) { var activeQuery = self.activeQuery; - self.activeQuery = null; + this.activeQuery = null; return activeQuery.handleError(error, con); } + + if (this._connecting) { + // set a flag indicating we've seen an error during connection + // the backend will terminate the connection and we don't want + // to throw a second error when the connection is terminated + this._connectionError = true; + } + if(!callback) { - return self.emit('error', error); + return this.emit('error', error); } + con.end(); // make sure ECONNRESET errors don't cause error events callback(error); callback = null; - }); + }.bind(this)); con.once('end', function() { - if ( callback ) { - // haven't received a connection message yet ! + if (callback) { + // haven't received a connection message yet! var err = new Error('Connection terminated'); callback(err); callback = null; return; } - if(self.activeQuery) { + if(this.activeQuery) { var disconnectError = new Error('Connection terminated'); - self.activeQuery.handleError(disconnectError, con); - self.activeQuery = null; + this.activeQuery.handleError(disconnectError, con); + this.activeQuery = null; } - self.emit('end'); - }); + if (!this._ending) { + // if the connection is ended without us calling .end() + // on this client then we have an unexpected disconnection + // treat this as an error unless we've already emitted an error + // during connection. + if (!this._connectionError) { + this.emit('error', new Error('Connection terminated unexpectedly')); + } + } + this.emit('end'); + }.bind(this)); con.on('notice', function(msg) { @@ -226,6 +252,9 @@ Client.prototype.getStartupConf = function() { if (appName) { data.application_name = appName; } + if (params.replication) { + data.replication = '' + params.replication; + } return data; }; @@ -325,10 +354,12 @@ Client.prototype.copyTo = function (text) { throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams"); }; +var DeprecatedEmitterQuery = utils.deprecateEventEmitter(Query); + Client.prototype.query = function(config, values, callback) { //can take in strings, config object or query object var query = (typeof config.submit == 'function') ? config : - new Query(config, values, callback); + new DeprecatedEmitterQuery(config, values, callback); if(this.binary && !query.binary) { query.binary = true; } @@ -342,6 +373,7 @@ Client.prototype.query = function(config, values, callback) { }; Client.prototype.end = function(cb) { + this._ending = true; this.connection.end(); if (cb) { this.connection.once('end', cb); diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index 065eaa0a0..44d10f636 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -57,6 +57,7 @@ var ConnectionParameters = function(config) { this.binary = val('binary', config); this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl; this.client_encoding = val("client_encoding", config); + this.replication = val("replication", config); //a domain socket begins with '/' this.isDomainSocket = (!(this.host||'').indexOf('/')); this.keepAlive = config.keepAlive || false; @@ -65,10 +66,15 @@ var ConnectionParameters = function(config) { this.fallback_application_name = val('fallback_application_name', config, false); }; +// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes +var quoteParamValue = function(value) { + return "'" + ('' + value).replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "'"; +}; + var add = function(params, config, paramName) { var value = config[paramName]; if(value) { - params.push(paramName+"='"+value+"'"); + params.push(paramName + "=" + quoteParamValue(value)); } }; @@ -80,21 +86,30 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) { add(params, this, 'application_name'); add(params, this, 'fallback_application_name'); + var ssl = typeof this.ssl === 'object' ? this.ssl : {sslmode: this.ssl}; + add(params, ssl, 'sslmode'); + add(params, ssl, 'sslca'); + add(params, ssl, 'sslkey'); + add(params, ssl, 'sslcert'); + if(this.database) { - params.push("dbname='" + this.database + "'"); + params.push("dbname=" + quoteParamValue(this.database)); + } + if(this.replication) { + params.push("replication=" + quoteParamValue(this.replication)); } if(this.host) { - params.push("host=" + this.host); + params.push("host=" + quoteParamValue(this.host)); } if(this.isDomainSocket) { return cb(null, params.join(' ')); } if(this.client_encoding) { - params.push("client_encoding='" + this.client_encoding + "'"); + params.push("client_encoding=" + quoteParamValue(this.client_encoding)); } dns.lookup(this.host, function(err, address) { if(err) return cb(err, null); - params.push("hostaddr=" + address); + params.push("hostaddr=" + quoteParamValue(address)); return cb(null, params.join(' ')); }); }; diff --git a/lib/connection.js b/lib/connection.js index 72545e605..d94de03d9 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -41,7 +41,7 @@ var Connection = function(config) { this.lastOffset = 0; this.buffer = null; this.offset = null; - this.encoding = 'utf8'; + this.encoding = config.client_encoding || 'utf8'; this.parsedStatements = {}; this.writer = new Writer(); this.ssl = config.ssl || false; @@ -89,9 +89,6 @@ Connection.prototype.connect = function(port, host) { }); this.stream.on('close', function() { - // NOTE: node-0.10 emits both 'end' and 'close' - // for streams closed by the peer, while - // node-0.8 only emits 'close' self.emit('end'); }); @@ -145,8 +142,6 @@ Connection.prototype.attachListeners = function(stream) { }; Connection.prototype.requestSsl = function() { - this.checkSslResponse = true; - var bodyBuffer = this.writer .addInt16(0x04D2) .addInt16(0x162F).flush(); @@ -427,6 +422,9 @@ Connection.prototype.parseMessage = function(buffer) { case 0x48: //H return this.parseH(buffer, length); + case 0x57: //W + return new Message('replicationStart', length); + case 0x63: //c return new Message('copyDone', length); diff --git a/lib/defaults.js b/lib/defaults.js index b99e4a8c5..b94d33f3c 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the diff --git a/lib/index.js b/lib/index.js index b2372cf4f..a2c2792f4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -54,7 +54,6 @@ PG.prototype.connect = function(config, callback) { callback = config; config = null; } - var poolName = JSON.stringify(config || {}); if (typeof config == 'string') { config = new ConnectionParameters(config); } @@ -66,6 +65,7 @@ PG.prototype.connect = function(config, callback) { config.idleTimeoutMillis = config.idleTimeoutMillis || config.poolIdleTimeout || defaults.poolIdleTimeout; config.log = config.log || config.poolLog || defaults.poolLog; + var poolName = JSON.stringify(config); this._pools[poolName] = this._pools[poolName] || new this.Pool(config); var pool = this._pools[poolName]; if(!pool.listeners('error').length) { diff --git a/lib/native/index.js b/lib/native/index.js index db5dfbf51..473500984 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -46,6 +46,8 @@ var Client = module.exports = function(config) { this.namedQueries = {}; }; +Client.Query = NativeQuery; + util.inherits(Client, EventEmitter); //connect to the backend @@ -139,6 +141,35 @@ Client.prototype.query = function(config, values, callback) { return query; }; +var DeprecatedQuery = require('../utils').deprecateEventEmitter(NativeQuery); + +//send a query to the server +//this method is highly overloaded to take +//1) string query, optional array of parameters, optional function callback +//2) object query with { +// string query +// optional array values, +// optional function callback instead of as a separate parameter +// optional string name to name & cache the query plan +// optional string rowMode = 'array' for an array of results +// } +Client.prototype.query = function(config, values, callback) { + if (typeof config.submit == 'function') { + // accept query(new Query(...), (err, res) => { }) style + if (typeof values == 'function') { + config.callback = values; + } + this._queryQueue.push(config); + this._pulseQueryQueue(); + return config; + } + + var query = new DeprecatedQuery(config, values, callback); + this._queryQueue.push(query); + this._pulseQueryQueue(); + return query; +}; + //disconnect from the backend server Client.prototype.end = function(cb) { var self = this; @@ -178,9 +209,11 @@ Client.prototype._pulseQueryQueue = function(initialConnection) { this._activeQuery = query; query.submit(this); var self = this; - query.once('_done', function() { + var pulseOnDone = function() { self._pulseQueryQueue(); - }); + query.removeListener('_done', pulseOnDone); + }; + query._on('_done', pulseOnDone); }; //attempt to cancel an in-progress query diff --git a/lib/native/query.js b/lib/native/query.js index af75d39b7..214488f60 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -11,15 +11,15 @@ var util = require('util'); var utils = require('../utils'); var NativeResult = require('./result'); -var NativeQuery = module.exports = function(native) { +var NativeQuery = module.exports = function(config, values, callback) { EventEmitter.call(this); - this.native = native; - this.text = null; - this.values = null; - this.name = null; - this.callback = null; + config = utils.normalizeQueryConfig(config, values, callback); + this.text = config.text; + this.values = config.values; + this.name = config.name; + this.callback = config.callback; this.state = 'new'; - this._arrayMode = false; + this._arrayMode = config.rowMode == 'array'; //if the 'row' event is listened for //then emit them as they come in @@ -27,30 +27,51 @@ var NativeQuery = module.exports = function(native) { //this has almost no meaning because libpq //reads all rows into memory befor returning any this._emitRowEvents = false; - this.on('newListener', function(event) { + this._on('newListener', function(event) { if(event === 'row') this._emitRowEvents = true; }.bind(this)); }; util.inherits(NativeQuery, EventEmitter); +// TODO - remove in 7.0 +// this maintains backwards compat so someone could instantiate a query +// manually: `new Query().then()`... +NativeQuery._on = NativeQuery.on; +NativeQuery._once = NativeQuery.once; + + NativeQuery.prototype.then = function(onSuccess, onFailure) { - return this.promise().then(onSuccess, onFailure); + return this._getPromise().then(onSuccess, onFailure); }; NativeQuery.prototype.catch = function(callback) { - return this.promise().catch(callback); + return this._getPromise().catch(callback); }; -NativeQuery.prototype.promise = function() { +NativeQuery.prototype._getPromise = function() { if (this._promise) return this._promise; this._promise = new Promise(function(resolve, reject) { - this.once('end', resolve); - this.once('error', reject); + var onEnd = function (result) { + this.removeListener('error', onError); + this.removeListener('end', onEnd); + resolve(result); + }; + var onError = function (err) { + this.removeListener('error', onError); + this.removeListener('end', onEnd); + reject(err); + }; + this._on('end', onEnd); + this._on('error', onError); }.bind(this)); return this._promise; }; +NativeQuery.prototype.promise = function() { + return this._getPromise(); +}; + NativeQuery.prototype.handleError = function(err) { var self = this; //copy pq error fields into the error object @@ -71,6 +92,7 @@ NativeQuery.prototype.handleError = function(err) { NativeQuery.prototype.submit = function(client) { this.state = 'running'; var self = this; + self.native = client.native; client.native.arrayMode = this._arrayMode; var after = function(err, rows) { diff --git a/lib/native/result.js b/lib/native/result.js index dc68f76c4..519037316 100644 --- a/lib/native/result.js +++ b/lib/native/result.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the diff --git a/lib/pool-factory.js b/lib/pool-factory.js index aa7bd0b19..85f0e6a50 100644 --- a/lib/pool-factory.js +++ b/lib/pool-factory.js @@ -3,7 +3,6 @@ var util = require('util'); var Pool = require('pg-pool'); module.exports = function(Client) { - var BoundPool = function(options) { var config = { Client: Client }; for (var key in options) { diff --git a/lib/query.js b/lib/query.js index f62786022..e9c3d1560 100644 --- a/lib/query.js +++ b/lib/query.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -40,23 +40,43 @@ var Query = function(config, values, callback) { util.inherits(Query, EventEmitter); +// TODO - remove in 7.0 +// this maintains backwards compat so someone could instantiate a query +// manually: `new Query().then()`... +Query._on = Query.on; +Query._once = Query.once; + Query.prototype.then = function(onSuccess, onFailure) { - return this.promise().then(onSuccess, onFailure); + return this._getPromise().then(onSuccess, onFailure); }; Query.prototype.catch = function(callback) { - return this.promise().catch(callback); + return this._getPromise().catch(callback); }; -Query.prototype.promise = function() { +Query.prototype._getPromise = function () { if (this._promise) return this._promise; this._promise = new Promise(function(resolve, reject) { - this.once('end', resolve); - this.once('error', reject); + var onEnd = function (result) { + this.removeListener('error', onError); + this.removeListener('end', onEnd); + resolve(result); + }; + var onError = function (err) { + this.removeListener('error', onError); + this.removeListener('end', onEnd); + reject(err); + }; + this._on('end', onEnd); + this._on('error', onError); }.bind(this)); return this._promise; }; +Query.prototype.promise = function() { + return this._getPromise(); +}; + Query.prototype.requiresPreparation = function() { //named queries must always be prepared if(this.name) { return true; } diff --git a/lib/result.js b/lib/result.js index 2955d921e..fbf98cdf9 100644 --- a/lib/result.js +++ b/lib/result.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the diff --git a/lib/type-overrides.js b/lib/type-overrides.js index e0cf00e6a..905260898 100644 --- a/lib/type-overrides.js +++ b/lib/type-overrides.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the diff --git a/lib/utils.js b/lib/utils.js index 861b7c5b6..cd3ee2ef8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,11 +1,13 @@ /** - * Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com) + * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com) * All rights reserved. * * This source code is licensed under the MIT license found in the * README.md file in the root directory of this source tree. */ +var util = require('util'); + var defaults = require('./defaults'); function escapeElement(elementRepresentation) { @@ -31,6 +33,9 @@ function arrayString(val) { else if(Array.isArray(val[i])) { result = result + arrayString(val[i]); } + else if(val[i] instanceof Buffer) { + result += '\\\\x' + val[i].toString('hex'); + } else { result += escapeElement(prepareValue(val[i])); @@ -137,11 +142,25 @@ function normalizeQueryConfig (config, values, callback) { return config; } + +var deprecateEventEmitter = function(Emitter) { + var Result = function () { + Emitter.apply(this, arguments); + }; + util.inherits(Result, Emitter); + Result.prototype._on = Result.prototype.on; + Result.prototype._once = Result.prototype.once; + Result.prototype.on = Result.prototype.on; + Result.prototype.once = Result.prototype.once; + return Result; +}; + module.exports = { prepareValue: function prepareValueWrapper (value) { //this ensures that extra arguments do not get passed into prepareValue //by accident, eg: from calling values.map(utils.prepareValue) return prepareValue(value); }, - normalizeQueryConfig: normalizeQueryConfig + normalizeQueryConfig: normalizeQueryConfig, + deprecateEventEmitter: deprecateEventEmitter, }; diff --git a/package.json b/package.json index 5d9aef043..750e8bac5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "6.1.6", + "version": "6.4.2", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "postgres", @@ -20,11 +20,11 @@ "dependencies": { "buffer-writer": "1.0.1", "js-string-escape": "1.0.1", - "packet-reader": "0.2.0", + "packet-reader": "0.3.1", "pg-connection-string": "0.1.3", "pg-pool": "1.*", "pg-types": "1.*", - "pgpass": "1.x", + "pgpass": "1.*", "semver": "4.3.2" }, "devDependencies": { @@ -38,7 +38,7 @@ "minNativeVersion": "1.7.0", "scripts": { "changelog": "npm i github-changes && ./node_modules/.bin/github-changes -o brianc -r node-postgres -d pulls -a -v", - "test": "make test-all connectionString=postgres://postgres@localhost:5432/postgres" + "test": "make test-all-nonative connectionString=postgres://postgres@localhost:$PGPORT/postgres" }, "license": "MIT", "engines": { diff --git a/test/integration/client/deprecation-tests.js b/test/integration/client/deprecation-tests.js new file mode 100644 index 000000000..21decc46f --- /dev/null +++ b/test/integration/client/deprecation-tests.js @@ -0,0 +1,23 @@ +var helper = require('./test-helper') + +process.on('warning', function (warning) { + console.log(warning) + throw new Error('Should not emit deprecation warning') +}) + +var client = new helper.pg.Client() + +client.connect(function (err) { + if (err) throw err + client.query('SELECT NOW()') + .then(function (res) { + client.query('SELECT NOW()', function () { + client.end(function () { + }) + }) + }).catch(function (err) { + setImmediate(function () { + throw err + }) + }) +}) diff --git a/test/integration/client/error-handling-tests.js b/test/integration/client/error-handling-tests.js index d3bf36c29..3fcd305bb 100644 --- a/test/integration/client/error-handling-tests.js +++ b/test/integration/client/error-handling-tests.js @@ -1,6 +1,24 @@ var helper = require(__dirname + '/test-helper'); var util = require('util'); + + test('non-query error with callback', function () { + var client = new Client({ + user:'asldkfjsadlfkj' + }); + client.connect(assert.calls(function (err) { + assert(err); + })); + }); + + test('non-query error', function() { + var client = new Client({ + user:'asldkfjsadlfkj' + }); + assert.emits(client, 'error'); + client.connect(); + }); + var createErorrClient = function() { var client = helper.client(); client.once('error', function(err) { @@ -11,9 +29,8 @@ var createErorrClient = function() { return client; }; -test('error handling', function(){ +test('error handling', function() { test('within a simple query', function() { - var client = createErorrClient(); var query = client.query("select omfg from yodas_dsflsd where pixistix = 'zoiks!!!'"); @@ -77,7 +94,6 @@ test('error handling', function(){ }); test('non-query error', function() { - var client = new Client({ user:'asldkfjsadlfkj' }); diff --git a/test/integration/client/json-type-parsing-tests.js b/test/integration/client/json-type-parsing-tests.js index 1c0759bf3..d216aef48 100644 --- a/test/integration/client/json-type-parsing-tests.js +++ b/test/integration/client/json-type-parsing-tests.js @@ -9,7 +9,7 @@ if (helper.config.binary) { test('can read and write json', function() { helper.pg.connect(helper.config, function(err, client, done) { assert.ifError(err); - helper.versionGTE(client, '9.2.0', assert.success(function(jsonSupported) { + helper.versionGTE(client, '90200', assert.success(function(jsonSupported) { if(!jsonSupported) { console.log('skip json test on older versions of postgres'); done(); diff --git a/test/integration/client/network-partition-tests.js b/test/integration/client/network-partition-tests.js new file mode 100644 index 000000000..944df510c --- /dev/null +++ b/test/integration/client/network-partition-tests.js @@ -0,0 +1,90 @@ +var co = require('co') + +var buffers = require('../../test-buffers') +var helper = require('./test-helper') + +var net = require('net') + +var Server = function(response) { + this.server = undefined + this.socket = undefined + this.response = response +} + +Server.prototype.start = function (cb) { + // this is our fake postgres server + // it responds with our specified response immediatley after receiving every buffer + // this is sufficient into convincing the client its connectet to a valid backend + // if we respond with a readyForQuery message + this.server = net.createServer(function (socket) { + this.socket = socket + if (this.response) { + this.socket.on('data', function (data) { + // deny request for SSL + if (data.length == 8) { + this.socket.write(new Buffer('N', 'utf8')) + // consider all authentication requests as good + } else if (!data[0]) { + this.socket.write(buffers.authenticationOk()) + // respond with our canned response + } else { + this.socket.write(this.response) + } + }.bind(this)) + } + }.bind(this)) + + var port = 54321 + + var options = { + host: 'localhost', + port: port, + } + this.server.listen(options.port, options.host, function () { + cb(options) + }) +} + +Server.prototype.drop = function () { + this.socket.end() +} + +Server.prototype.close = function (cb) { + this.server.close(cb) +} + +var testServer = function (server, cb) { + // wait for our server to start + server.start(function(options) { + // connect a client to it + var client = new helper.Client(options) + client.connect() + + // after 50 milliseconds, drop the client + setTimeout(function() { + server.drop() + }, 50) + + // blow up if we don't receive an error + var timeoutId = setTimeout(function () { + throw new Error('Client should have emitted an error but it did not.') + }, 5000) + + // return our wait token + client.on('error', function () { + clearTimeout(timeoutId) + server.close(cb) + }) + }) +} + +// test being disconnected after readyForQuery +const respondingServer = new Server(buffers.readyForQuery()) +testServer(respondingServer, function () { + process.stdout.write('.') + // test being disconnected from a server that never responds + const silentServer = new Server() + testServer(silentServer, function () { + process.stdout.write('.') + }) +}) diff --git a/test/integration/client/query-error-handling-prepared-statement-tests.js b/test/integration/client/query-error-handling-prepared-statement-tests.js index 151646586..c9bb260db 100644 --- a/test/integration/client/query-error-handling-prepared-statement-tests.js +++ b/test/integration/client/query-error-handling-prepared-statement-tests.js @@ -6,7 +6,7 @@ function killIdleQuery(targetQuery) { var pidColName = 'procpid' var queryColName = 'current_query'; client2.connect(assert.success(function() { - helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { + helper.versionGTE(client2, '90200', assert.success(function(isGreater) { if(isGreater) { pidColName = 'pid'; queryColName = 'query'; @@ -29,12 +29,18 @@ test('query killed during query execution of prepared statement', function() { var client = new Client(helper.args); client.connect(assert.success(function() { var sleepQuery = 'select pg_sleep($1)'; - var query1 = client.query({ + + const queryConfig = { name: 'sleep query', text: sleepQuery, - values: [5] }, - assert.calls(function(err, result) { - assert.equal(err.message, 'terminating connection due to administrator command'); + values: [5], + }; + + // client should emit an error because it is unexpectedly disconnected + assert.emits(client, 'error') + + var query1 = client.query(queryConfig, assert.calls(function(err, result) { + assert.equal(err.message, 'terminating connection due to administrator command'); })); query1.on('error', function(err) { @@ -53,7 +59,6 @@ test('query killed during query execution of prepared statement', function() { })); }); - test('client end during query execution of prepared statement', function() { var client = new Client(helper.args); client.connect(assert.success(function() { diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index 2618a49df..e7bd56a58 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -7,7 +7,7 @@ test('error during query execution', function() { var sleepQuery = 'select pg_sleep(5)'; var pidColName = 'procpid' var queryColName = 'current_query'; - helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) { + helper.versionGTE(client, 90200, assert.success(function(isGreater) { if(isGreater) { pidColName = 'pid'; queryColName = 'query'; @@ -43,7 +43,7 @@ if(helper.config.native) return; test('9.3 column error fields', function() { var client = new Client(helper.args); client.connect(assert.success(function() { - helper.versionGTE(client, '9.3.0', assert.success(function(isGreater) { + helper.versionGTE(client, 90300, assert.success(function(isGreater) { if(!isGreater) { return client.end(); } @@ -65,7 +65,7 @@ test('9.3 column error fields', function() { test('9.3 constraint error fields', function() { var client = new Client(helper.args); client.connect(assert.success(function() { - helper.versionGTE(client, '9.3.0', assert.success(function(isGreater) { + helper.versionGTE(client, 90300, assert.success(function(isGreater) { if(!isGreater) { console.log('skip 9.3 error field on older versions of postgres'); return client.end(); diff --git a/test/integration/client/result-metadata-tests.js b/test/integration/client/result-metadata-tests.js index 013630033..b9d09b5d3 100644 --- a/test/integration/client/result-metadata-tests.js +++ b/test/integration/client/result-metadata-tests.js @@ -5,7 +5,7 @@ test('should return insert metadata', function() { pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err); - helper.versionGTE(client, '9.0.0', assert.success(function(hasRowCount) { + helper.versionGTE(client, '90000', assert.success(function(hasRowCount) { client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { assert.isNull(err); assert.equal(result.oid, null); diff --git a/test/integration/connection-pool/double-connection-tests.js b/test/integration/connection-pool/double-connection-tests.js index ae7eb3169..421e1f9ea 100644 --- a/test/integration/connection-pool/double-connection-tests.js +++ b/test/integration/connection-pool/double-connection-tests.js @@ -1,2 +1,2 @@ -var helper = require(__dirname + "/test-helper") +var helper = require("./test-helper") helper.testPoolSize(2); diff --git a/test/integration/connection-pool/ending-empty-pool-tests.js b/test/integration/connection-pool/ending-empty-pool-tests.js index 4f5dd80ad..d1acc6f20 100644 --- a/test/integration/connection-pool/ending-empty-pool-tests.js +++ b/test/integration/connection-pool/ending-empty-pool-tests.js @@ -1,4 +1,4 @@ -var helper = require(__dirname + '/test-helper') +var helper = require('./test-helper') var called = false; test('disconnects', function() { diff --git a/test/integration/connection-pool/ending-pool-tests.js b/test/integration/connection-pool/ending-pool-tests.js index 83f4b1bc2..3a1ab46f9 100644 --- a/test/integration/connection-pool/ending-pool-tests.js +++ b/test/integration/connection-pool/ending-pool-tests.js @@ -1,6 +1,7 @@ -var helper = require(__dirname + '/test-helper') +var helper = require('./test-helper') var called = false; + test('disconnects', function() { var sink = new helper.Sink(4, function() { called = true; diff --git a/test/integration/connection-pool/error-tests.js b/test/integration/connection-pool/error-tests.js index 2cf0501fa..27525bf08 100644 --- a/test/integration/connection-pool/error-tests.js +++ b/test/integration/connection-pool/error-tests.js @@ -1,5 +1,5 @@ -var helper = require(__dirname + "/../test-helper"); -var pg = require(__dirname + "/../../../lib"); +var helper = require("../test-helper"); +var pg = require("../../../lib"); //first make pool hold 2 clients pg.defaults.poolSize = 2; @@ -11,7 +11,7 @@ pg.connect(helper.config, assert.success(function(client, done) { pg.connect(helper.config, assert.success(function(client2, done2) { client2.id = 2; var pidColName = 'procpid'; - helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { + helper.versionGTE(client2, '90200', assert.success(function(isGreater) { var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'; var params = ['idle']; if(!isGreater) { diff --git a/test/integration/connection-pool/idle-timeout-tests.js b/test/integration/connection-pool/idle-timeout-tests.js index 0a60ce504..b0908b6fb 100644 --- a/test/integration/connection-pool/idle-timeout-tests.js +++ b/test/integration/connection-pool/idle-timeout-tests.js @@ -1,4 +1,4 @@ -var helper = require(__dirname + '/test-helper'); +var helper = require('./test-helper'); var _ = require('lodash') const config = _.extend({ }, helper.config, { idleTimeoutMillis: 50 }) diff --git a/test/integration/connection-pool/max-connection-tests.js b/test/integration/connection-pool/max-connection-tests.js index 68c0773f9..944d2fb2e 100644 --- a/test/integration/connection-pool/max-connection-tests.js +++ b/test/integration/connection-pool/max-connection-tests.js @@ -1,2 +1,2 @@ -var helper = require(__dirname + "/test-helper") +var helper = require("./test-helper") helper.testPoolSize(40); diff --git a/test/integration/connection-pool/native-instance-tests.js b/test/integration/connection-pool/native-instance-tests.js index 06fbdb45b..314920c4d 100644 --- a/test/integration/connection-pool/native-instance-tests.js +++ b/test/integration/connection-pool/native-instance-tests.js @@ -1,4 +1,4 @@ -var helper = require(__dirname + "/../test-helper") +var helper = require("./../test-helper") var pg = helper.pg var native = helper.args.native diff --git a/test/integration/connection-pool/optional-config-tests.js b/test/integration/connection-pool/optional-config-tests.js index f0ba2e76e..be7063eb8 100644 --- a/test/integration/connection-pool/optional-config-tests.js +++ b/test/integration/connection-pool/optional-config-tests.js @@ -1,4 +1,4 @@ -var helper = require(__dirname + '/test-helper'); +var helper = require('./test-helper'); //setup defaults helper.pg.defaults.user = helper.args.user; diff --git a/test/integration/connection-pool/single-connection-tests.js b/test/integration/connection-pool/single-connection-tests.js index 5ca0a8884..89f6f069e 100644 --- a/test/integration/connection-pool/single-connection-tests.js +++ b/test/integration/connection-pool/single-connection-tests.js @@ -1,2 +1,2 @@ -var helper = require(__dirname + "/test-helper") +var helper = require("./test-helper") helper.testPoolSize(1); diff --git a/test/integration/connection-pool/single-pool-on-object-config-tests.js b/test/integration/connection-pool/single-pool-on-object-config-tests.js new file mode 100644 index 000000000..81cdf8e45 --- /dev/null +++ b/test/integration/connection-pool/single-pool-on-object-config-tests.js @@ -0,0 +1,13 @@ +var helper = require("../test-helper"); +var pg = require("../../../lib"); + +pg.connect(helper.config, assert.success(function(client, done) { + assert.equal(Object.keys(pg._pools).length, 1); + pg.connect(helper.config, assert.success(function(client2, done2) { + assert.equal(Object.keys(pg._pools).length, 1); + + done(); + done2(); + pg.end(); + })); +})); diff --git a/test/integration/connection-pool/waiting-connection-tests.js b/test/integration/connection-pool/waiting-connection-tests.js index f2519ec55..82572d1e4 100644 --- a/test/integration/connection-pool/waiting-connection-tests.js +++ b/test/integration/connection-pool/waiting-connection-tests.js @@ -1,2 +1,2 @@ -var helper = require(__dirname + "/test-helper") +var helper = require("./test-helper") helper.testPoolSize(200); diff --git a/test/integration/test-helper.js b/test/integration/test-helper.js index c6a4922dc..155c36077 100644 --- a/test/integration/test-helper.js +++ b/test/integration/test-helper.js @@ -13,14 +13,13 @@ helper.client = function(cb) { return client; }; -var semver = require('semver'); -helper.versionGTE = function(client, versionString, callback) { - client.query('SELECT version()', assert.calls(function(err, result) { - if(err) return callback(err); - var version = result.rows[0].version.split(' ')[1]; - return callback(null, semver.gte(version, versionString)); - })); -}; +helper.versionGTE = function (client, testVersion, callback) { + client.query('SHOW server_version_num', assert.calls(function (err, result) { + if (err) return callback(err) + var version = parseInt(result.rows[0].server_version_num, 10) + return callback(null, version >= testVersion) + })) +} //export parent helper stuffs module.exports = helper; diff --git a/test/unit/client/configuration-tests.js b/test/unit/client/configuration-tests.js index 0204af22b..5548e5fb1 100644 --- a/test/unit/client/configuration-tests.js +++ b/test/unit/client/configuration-tests.js @@ -59,6 +59,17 @@ test('client settings', function() { test('initializing from a config string', function() { + test('uses connectionString property', function () { + var client = new Client({ + connectionString: 'postgres://brian:pass@host1:333/databasename' + }) + assert.equal(client.user, 'brian'); + assert.equal(client.password, "pass"); + assert.equal(client.host, "host1"); + assert.equal(client.port, 333); + assert.equal(client.database, "databasename"); + }) + test('uses the correct values from the config string', function() { var client = new Client("postgres://brian:pass@host1:333/databasename") assert.equal(client.user, 'brian'); diff --git a/test/unit/client/connection-string-tests.js b/test/unit/client/connection-string-tests.js deleted file mode 100644 index 9316daa9b..000000000 --- a/test/unit/client/connection-string-tests.js +++ /dev/null @@ -1,27 +0,0 @@ -require(__dirname + '/test-helper'); - -/* - * Perhaps duplicate of test named 'initializing from a config string' in - * configuration-tests.js - */ - -test("using connection string in client constructor", function() { - var client = new Client("postgres://brian:pw@boom:381/lala"); - - test("parses user", function() { - assert.equal(client.user,'brian'); - }); - test("parses password", function() { - assert.equal(client.password, 'pw'); - }); - test("parses host", function() { - assert.equal(client.host, 'boom'); - }); - test('parses port', function() { - assert.equal(client.port, 381) - }); - test('parses database', function() { - assert.equal(client.database, 'lala') - }); -}); - diff --git a/test/unit/client/stream-and-query-error-interaction-tests.js b/test/unit/client/stream-and-query-error-interaction-tests.js index 02d66c628..a60e1f734 100644 --- a/test/unit/client/stream-and-query-error-interaction-tests.js +++ b/test/unit/client/stream-and-query-error-interaction-tests.js @@ -7,12 +7,14 @@ test('emits end when not in query', function() { stream.write = function() { //NOOP } + var client = new Client({connection: new Connection({stream: stream})}); client.connect(assert.calls(function() { client.query('SELECT NOW()', assert.calls(function(err, result) { assert(err); })); })); + assert.emits(client, 'error'); assert.emits(client, 'end'); client.connection.emit('connect'); process.nextTick(function() { diff --git a/test/unit/connection-parameters/creation-tests.js b/test/unit/connection-parameters/creation-tests.js index 33ee7eeeb..85c2bd037 100644 --- a/test/unit/connection-parameters/creation-tests.js +++ b/test/unit/connection-parameters/creation-tests.js @@ -126,7 +126,7 @@ test('libpq connection string building', function() { checkForPart(parts, "user='brian'"); checkForPart(parts, "password='xyz'"); checkForPart(parts, "port='888'"); - checkForPart(parts, "hostaddr=127.0.0.1"); + checkForPart(parts, "hostaddr='127.0.0.1'"); checkForPart(parts, "dbname='bam'"); })); }); @@ -143,7 +143,7 @@ test('libpq connection string building', function() { assert.isNull(err); var parts = constring.split(" "); checkForPart(parts, "user='brian'"); - checkForPart(parts, "hostaddr=127.0.0.1"); + checkForPart(parts, "hostaddr='127.0.0.1'"); })); }); @@ -173,7 +173,23 @@ test('libpq connection string building', function() { assert.isNull(err); var parts = constring.split(" "); checkForPart(parts, "user='brian'"); - checkForPart(parts, "host=/tmp/"); + checkForPart(parts, "host='/tmp/'"); + })); + }); + + test('config contains quotes and backslashes', function() { + var config = { + user: 'not\\brian', + password: 'bad\'chars', + port: 5432, + host: '/tmp/' + }; + var subject = new ConnectionParameters(config); + subject.getLibpqConnectionString(assert.calls(function(err, constring) { + assert.isNull(err); + var parts = constring.split(" "); + checkForPart(parts, "user='not\\\\brian'"); + checkForPart(parts, "password='bad\\'chars'"); })); }); diff --git a/test/unit/connection/inbound-parser-tests.js b/test/unit/connection/inbound-parser-tests.js index 13e6fd9eb..a9910966b 100644 --- a/test/unit/connection/inbound-parser-tests.js +++ b/test/unit/connection/inbound-parser-tests.js @@ -347,6 +347,13 @@ test('Connection', function() { name: 'portalSuspended' }); }); + + test('parses replication start message', function() { + testForMessage(new Buffer([0x57, 0x00, 0x00, 0x00, 0x04]), { + name: 'replicationStart', + length: 4 + }); + }); }); //since the data message on a stream can randomly divide the incomming @@ -465,5 +472,4 @@ test('split buffer, multiple message parsing', function() { splitAndVerifyTwoMessages(1); }); }); - }); diff --git a/test/unit/utils-tests.js b/test/unit/utils-tests.js index d640f9880..19c0b8db7 100644 --- a/test/unit/utils-tests.js +++ b/test/unit/utils-tests.js @@ -126,6 +126,13 @@ test('prepareValue: date array prepared properly', function() { helper.resetTimezoneOffset(); }); +test('prepareValue: buffer array prepared properly', function() { + var buffer1 = Buffer.from ? Buffer.from('dead', 'hex') : new Buffer('dead', 'hex'); + var buffer2 = Buffer.from ? Buffer.from('beef', 'hex') : new Buffer('beef', 'hex'); + var out = utils.prepareValue([buffer1, buffer2]); + assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}'); +}); + test('prepareValue: arbitrary objects prepared properly', function() { var out = utils.prepareValue({ x: 42 }); assert.strictEqual(out, '{"x":42}');