From aedaa59afe6028fb1a13187695325e8dbacb2c30 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Tue, 27 Jul 2021 16:40:32 +0200 Subject: [PATCH 01/12] Add support for using promises in Cursor methods (#2554) * Add similar promise variables to read() and close() as seen in query() * Add testing for promise specific usage * Simplify tests as no real callbacks are involved Removes usage of `done()` since we can end the test when we exit the function Co-Authored-By: Charmander <~@charmander.me> * Switch to let over var Co-authored-by: Charmander <~@charmander.me> --- packages/pg-cursor/index.js | 40 ++++++++++++++++------ packages/pg-cursor/test/promises.js | 51 +++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 packages/pg-cursor/test/promises.js diff --git a/packages/pg-cursor/index.js b/packages/pg-cursor/index.js index 8e8552be8..b77fd5977 100644 --- a/packages/pg-cursor/index.js +++ b/packages/pg-cursor/index.js @@ -17,6 +17,7 @@ class Cursor extends EventEmitter { this._queue = [] this.state = 'initialized' this._result = new Result(this._conf.rowMode, this._conf.types) + this._Promise = this._conf.Promise || global.Promise this._cb = null this._rows = null this._portal = null @@ -198,6 +199,14 @@ class Cursor extends EventEmitter { } close(cb) { + let promise + + if (!cb) { + promise = new this._Promise((resolve, reject) => { + cb = (err) => (err ? reject(err) : resolve()) + }) + } + if (!this.connection || this.state === 'done') { if (cb) { return setImmediate(cb) @@ -213,23 +222,34 @@ class Cursor extends EventEmitter { cb() }) } + + // Return the promise (or undefined) + return promise } read(rows, cb) { - if (this.state === 'idle' || this.state === 'submitted') { - return this._getRows(rows, cb) - } - if (this.state === 'busy' || this.state === 'initialized') { - return this._queue.push([rows, cb]) - } - if (this.state === 'error') { - return setImmediate(() => cb(this._error)) + let promise + + if (!cb) { + promise = new this._Promise((resolve, reject) => { + cb = (err, rows) => (err ? reject(err) : resolve(rows)) + }) } - if (this.state === 'done') { - return setImmediate(() => cb(null, [])) + + if (this.state === 'idle' || this.state === 'submitted') { + this._getRows(rows, cb) + } else if (this.state === 'busy' || this.state === 'initialized') { + this._queue.push([rows, cb]) + } else if (this.state === 'error') { + setImmediate(() => cb(this._error)) + } else if (this.state === 'done') { + setImmediate(() => cb(null, [])) } else { throw new Error('Unknown state: ' + this.state) } + + // Return the promise (or undefined) + return promise } } diff --git a/packages/pg-cursor/test/promises.js b/packages/pg-cursor/test/promises.js new file mode 100644 index 000000000..7b36dab8f --- /dev/null +++ b/packages/pg-cursor/test/promises.js @@ -0,0 +1,51 @@ +const assert = require('assert') +const Cursor = require('../') +const pg = require('pg') + +const text = 'SELECT generate_series as num FROM generate_series(0, 5)' + +describe('cursor using promises', function () { + beforeEach(function (done) { + const client = (this.client = new pg.Client()) + client.connect(done) + + this.pgCursor = function (text, values) { + return client.query(new Cursor(text, values || [])) + } + }) + + afterEach(function () { + this.client.end() + }) + + it('resolve with result', async function () { + const cursor = this.pgCursor(text) + const res = await cursor.read(6) + assert.strictEqual(res.length, 6) + }) + + it('reject with error', function (done) { + const cursor = this.pgCursor('select asdfasdf') + cursor.read(1).error((err) => { + assert(err) + done() + }) + }) + + it('read multiple times', async function () { + const cursor = this.pgCursor(text) + let res + + res = await cursor.read(2) + assert.strictEqual(res.length, 2) + + res = await cursor.read(3) + assert.strictEqual(res.length, 3) + + res = await cursor.read(1) + assert.strictEqual(res.length, 1) + + res = await cursor.read(1) + assert.strictEqual(res.length, 0) + }) +}) From 684cd09bcecbf5ad5f451fdf608a3e9a9444524e Mon Sep 17 00:00:00 2001 From: Brian Crowell Date: Tue, 27 Jul 2021 11:29:07 -0500 Subject: [PATCH 02/12] Allow Node to exit if the pool is idle (#2568) Based on the suggestion from #2078. This adds ref/unref methods to the Connection and Client classes and then uses them to allow the process to exit if all of the connections in the pool are idle. This behavior is controlled by the allowExitOnIdle flag to the Pool constructor; it defaults to the old behavior. --- packages/pg-pool/index.js | 11 ++++++++ packages/pg-pool/test/idle-timeout-exit.js | 16 +++++++++++ packages/pg-pool/test/idle-timeout.js | 31 ++++++++++++++++++++++ packages/pg/lib/client.js | 8 ++++++ packages/pg/lib/connection.js | 8 ++++++ 5 files changed, 74 insertions(+) create mode 100644 packages/pg-pool/test/idle-timeout-exit.js diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 403d05a19..5557de5c0 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -83,6 +83,7 @@ class Pool extends EventEmitter { this.options.max = this.options.max || this.options.poolSize || 10 this.options.maxUses = this.options.maxUses || Infinity + this.options.allowExitOnIdle = this.options.allowExitOnIdle || false this.log = this.options.log || function () {} this.Client = this.options.Client || Client || require('pg').Client this.Promise = this.options.Promise || global.Promise @@ -136,6 +137,7 @@ class Pool extends EventEmitter { const idleItem = this._idle.pop() clearTimeout(idleItem.timeoutId) const client = idleItem.client + client.ref() const idleListener = idleItem.idleListener return this._acquireClient(client, pendingItem, idleListener, false) @@ -323,6 +325,15 @@ class Pool extends EventEmitter { this.log('remove idle client') this._remove(client) }, this.options.idleTimeoutMillis) + + if (this.options.allowExitOnIdle) { + // allow Node to exit if this is all that's left + tid.unref() + } + } + + if (this.options.allowExitOnIdle) { + client.unref() } this._idle.push(new IdleItem(client, idleListener, tid)) diff --git a/packages/pg-pool/test/idle-timeout-exit.js b/packages/pg-pool/test/idle-timeout-exit.js new file mode 100644 index 000000000..1292634a8 --- /dev/null +++ b/packages/pg-pool/test/idle-timeout-exit.js @@ -0,0 +1,16 @@ +// This test is meant to be spawned from idle-timeout.js +if (module === require.main) { + const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1' + const Pool = require('../index') + + const pool = new Pool({ idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) }) + pool.query('SELECT NOW()', (err, res) => console.log('completed first')) + pool.on('remove', () => { + console.log('removed') + done() + }) + + setTimeout(() => { + pool.query('SELECT * from generate_series(0, 1000)', (err, res) => console.log('completed second')) + }, 50) +} diff --git a/packages/pg-pool/test/idle-timeout.js b/packages/pg-pool/test/idle-timeout.js index fd9fba4a4..0bb097565 100644 --- a/packages/pg-pool/test/idle-timeout.js +++ b/packages/pg-pool/test/idle-timeout.js @@ -4,6 +4,8 @@ const expect = require('expect.js') const describe = require('mocha').describe const it = require('mocha').it +const { fork } = require('child_process') +const path = require('path') const Pool = require('../') @@ -84,4 +86,33 @@ describe('idle timeout', () => { return pool.end() }) ) + + it('unrefs the connections and timeouts so the program can exit when idle when the allowExitOnIdle option is set', function (done) { + const child = fork(path.join(__dirname, 'idle-timeout-exit.js'), [], { + silent: true, + env: { ...process.env, ALLOW_EXIT_ON_IDLE: '1' }, + }) + let result = '' + child.stdout.setEncoding('utf8') + child.stdout.on('data', (chunk) => (result += chunk)) + child.on('error', (err) => done(err)) + child.on('close', () => { + expect(result).to.equal('completed first\ncompleted second\n') + done() + }) + }) + + it('keeps old behavior when allowExitOnIdle option is not set', function (done) { + const child = fork(path.join(__dirname, 'idle-timeout-exit.js'), [], { + silent: true, + }) + let result = '' + child.stdout.setEncoding('utf8') + child.stdout.on('data', (chunk) => (result += chunk)) + child.on('error', (err) => done(err)) + child.on('close', () => { + expect(result).to.equal('completed first\ncompleted second\nremoved\n') + done() + }) + }) }) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 1e1e83374..589aa9f84 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -577,6 +577,14 @@ class Client extends EventEmitter { return result } + ref() { + this.connection.ref() + } + + unref() { + this.connection.unref() + } + end(cb) { this._ending = true diff --git a/packages/pg/lib/connection.js b/packages/pg/lib/connection.js index 7d45de2b7..ebb2f099d 100644 --- a/packages/pg/lib/connection.js +++ b/packages/pg/lib/connection.js @@ -177,6 +177,14 @@ class Connection extends EventEmitter { this._send(syncBuffer) } + ref() { + this.stream.ref() + } + + unref() { + this.stream.unref() + } + end() { // 0x58 = 'X' this._ending = true From f824d74afe99b21de2681cd665e4cee74e769960 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Tue, 27 Jul 2021 11:35:55 -0500 Subject: [PATCH 03/12] Update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e368ff9..5347e3557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ 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@8.7.0 + +- Add optional config to [pool](https://github.com/brianc/node-postgres/pull/2568) to allow process to exit if pool is idle. + +### pg-cursor@2.7.0 + +- Convert to [es6 class](https://github.com/brianc/node-postgres/pull/2553) +- Add support for promises [to cursor methods](https://github.com/brianc/node-postgres/pull/2554) + ### pg@8.6.0 - Better [SASL](https://github.com/brianc/node-postgres/pull/2436) error messages & more validation on bad configuration. From d8ce457e83146a960fee9328789142327b0c8f70 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 27 Jul 2021 11:36:35 -0500 Subject: [PATCH 04/12] Bump handlebars from 4.7.6 to 4.7.7 (#2538) Bumps [handlebars](https://github.com/wycats/handlebars.js) from 4.7.6 to 4.7.7. - [Release notes](https://github.com/wycats/handlebars.js/releases) - [Changelog](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md) - [Commits](https://github.com/wycats/handlebars.js/compare/v4.7.6...v4.7.7) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/yarn.lock b/yarn.lock index e579f984e..3372de6a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3066,9 +3066,9 @@ growl@1.10.5: integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA== handlebars@^4.0.1, handlebars@^4.7.6: - version "4.7.6" - resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.6.tgz#d4c05c1baf90e9945f77aa68a7a219aa4a7df74e" - integrity sha512-1f2BACcBfiwAfStCKZNrUCgqNZkGsAT7UM3kkYtXuLo0KnaVfjKOyf7PRzB6++aK9STyT1Pd2ZCPe3EGOXleXA== + version "4.7.7" + resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1" + integrity sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA== dependencies: minimist "^1.2.5" neo-async "^2.6.0" @@ -6125,9 +6125,9 @@ typescript@^4.0.3: integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg== uglify-js@^3.1.4: - version "3.11.1" - resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.11.1.tgz#32d274fea8aac333293044afd7f81409d5040d38" - integrity sha512-OApPSuJcxcnewwjSGGfWOjx3oix5XpmrK9Z2j0fTRlHGoZ49IU6kExfZTM0++fCArOOCet+vIfWwFHbvWqwp6g== + version "3.13.5" + resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.13.5.tgz#5d71d6dbba64cf441f32929b1efce7365bb4f113" + integrity sha512-xtB8yEqIkn7zmOyS2zUNBsYCBRhDkvlNxMMY2smuJ/qA8NCHeQvKCF3i9Z4k8FJH4+PJvZRtMrPynfZ75+CSZw== uid-number@0.0.6: version "0.0.6" From 83aae778e8dcb3fb35a84de6667e21e0c8276a99 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 27 Jul 2021 11:37:10 -0500 Subject: [PATCH 05/12] Bump ssri from 6.0.1 to 6.0.2 (#2531) Bumps [ssri](https://github.com/npm/ssri) from 6.0.1 to 6.0.2. - [Release notes](https://github.com/npm/ssri/releases) - [Changelog](https://github.com/npm/ssri/blob/v6.0.2/CHANGELOG.md) - [Commits](https://github.com/npm/ssri/compare/v6.0.1...v6.0.2) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 3372de6a5..ad4eed181 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5647,9 +5647,9 @@ sshpk@^1.7.0: tweetnacl "~0.14.0" ssri@^6.0.0, ssri@^6.0.1: - version "6.0.1" - resolved "https://registry.yarnpkg.com/ssri/-/ssri-6.0.1.tgz#2a3c41b28dd45b62b63676ecb74001265ae9edd8" - integrity sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA== + version "6.0.2" + resolved "https://registry.yarnpkg.com/ssri/-/ssri-6.0.2.tgz#157939134f20464e7301ddba3e90ffa8f7728ac5" + integrity sha512-cepbSq/neFK7xB6A50KHN0xHDotYzq58wWCa5LeWqnPrHG8GzfEjO/4O8kpmcGW+oaxkvhEJCWgbgNk4/ZV93Q== dependencies: figgy-pudding "^3.5.1" From 0da7882f45d0c63d4bb310c7d137434ef4b22d18 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 27 Jul 2021 11:42:04 -0500 Subject: [PATCH 06/12] Bump y18n from 4.0.0 to 4.0.1 (#2506) Bumps [y18n](https://github.com/yargs/y18n) from 4.0.0 to 4.0.1. - [Release notes](https://github.com/yargs/y18n/releases) - [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md) - [Commits](https://github.com/yargs/y18n/commits) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index ad4eed181..e779f038c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6388,9 +6388,9 @@ xtend@^4.0.0, xtend@~4.0.1: integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== y18n@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b" - integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w== + version "4.0.1" + resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.1.tgz#8db2b83c31c5d75099bb890b23f3094891e247d4" + integrity sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ== yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3: version "3.1.1" From 779803fbce195ae5610761606dcdcd78ca4cd439 Mon Sep 17 00:00:00 2001 From: Brian C Date: Tue, 27 Jul 2021 12:23:30 -0500 Subject: [PATCH 07/12] Add ref/unref noop to native client (#2581) * Add ref/unref noop to native client * Use promise.catch in test * Make partition test not flake on old node * Fix test flake on old node --- packages/pg-cursor/test/promises.js | 2 +- packages/pg/lib/native/client.js | 3 +++ .../integration/client/connection-timeout-tests.js | 11 ++++++----- .../integration/client/network-partition-tests.js | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/pg-cursor/test/promises.js b/packages/pg-cursor/test/promises.js index 7b36dab8f..1635a1a8b 100644 --- a/packages/pg-cursor/test/promises.js +++ b/packages/pg-cursor/test/promises.js @@ -26,7 +26,7 @@ describe('cursor using promises', function () { it('reject with error', function (done) { const cursor = this.pgCursor('select asdfasdf') - cursor.read(1).error((err) => { + cursor.read(1).catch((err) => { assert(err) done() }) diff --git a/packages/pg/lib/native/client.js b/packages/pg/lib/native/client.js index 6cf800d0e..d1faeb3d8 100644 --- a/packages/pg/lib/native/client.js +++ b/packages/pg/lib/native/client.js @@ -285,6 +285,9 @@ Client.prototype.cancel = function (query) { } } +Client.prototype.ref = function () {} +Client.prototype.unref = function () {} + Client.prototype.setTypeParser = function (oid, format, parseFn) { return this._types.setTypeParser(oid, format, parseFn) } diff --git a/packages/pg/test/integration/client/connection-timeout-tests.js b/packages/pg/test/integration/client/connection-timeout-tests.js index 843fa95bb..6b99698bc 100644 --- a/packages/pg/test/integration/client/connection-timeout-tests.js +++ b/packages/pg/test/integration/client/connection-timeout-tests.js @@ -13,7 +13,7 @@ const options = { database: 'existing', } -const serverWithConnectionTimeout = (timeout, callback) => { +const serverWithConnectionTimeout = (port, timeout, callback) => { const sockets = new Set() const server = net.createServer((socket) => { @@ -47,11 +47,11 @@ const serverWithConnectionTimeout = (timeout, callback) => { } } - server.listen(options.port, options.host, () => callback(closeServer)) + server.listen(port, options.host, () => callback(closeServer)) } suite.test('successful connection', (done) => { - serverWithConnectionTimeout(0, (closeServer) => { + serverWithConnectionTimeout(options.port, 0, (closeServer) => { const timeoutId = setTimeout(() => { throw new Error('Client should have connected successfully but it did not.') }, 3000) @@ -67,12 +67,13 @@ suite.test('successful connection', (done) => { }) suite.test('expired connection timeout', (done) => { - serverWithConnectionTimeout(options.connectionTimeoutMillis * 2, (closeServer) => { + const opts = { ...options, port: 54322 } + serverWithConnectionTimeout(opts.port, opts.connectionTimeoutMillis * 2, (closeServer) => { const timeoutId = setTimeout(() => { throw new Error('Client should have emitted an error but it did not.') }, 3000) - const client = new helper.Client(options) + const client = new helper.Client(opts) client .connect() .then(() => client.end()) diff --git a/packages/pg/test/integration/client/network-partition-tests.js b/packages/pg/test/integration/client/network-partition-tests.js index 993396401..2ac100dff 100644 --- a/packages/pg/test/integration/client/network-partition-tests.js +++ b/packages/pg/test/integration/client/network-partition-tests.js @@ -11,6 +11,7 @@ var Server = function (response) { this.response = response } +let port = 54321 Server.prototype.start = function (cb) { // this is our fake postgres server // it responds with our specified response immediatley after receiving every buffer @@ -39,7 +40,7 @@ Server.prototype.start = function (cb) { }.bind(this) ) - var port = 54321 + port = port + 1 var options = { host: 'localhost', From f3b0ee4c09cd01e37baf580d72dffc43edcc29f3 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Tue, 27 Jul 2021 12:41:17 -0500 Subject: [PATCH 08/12] Publish - pg-cursor@2.7.0 - pg-pool@3.4.0 - pg-query-stream@4.2.0 - pg@8.7.0 --- packages/pg-cursor/package.json | 4 ++-- packages/pg-pool/package.json | 2 +- packages/pg-query-stream/package.json | 6 +++--- packages/pg/package.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/pg-cursor/package.json b/packages/pg-cursor/package.json index 5607ea955..be43e15f6 100644 --- a/packages/pg-cursor/package.json +++ b/packages/pg-cursor/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "2.6.0", + "version": "2.7.0", "description": "Query cursor extension for node-postgres", "main": "index.js", "directories": { @@ -18,7 +18,7 @@ "license": "MIT", "devDependencies": { "mocha": "^7.1.2", - "pg": "^8.6.0" + "pg": "^8.7.0" }, "peerDependencies": { "pg": "^8" diff --git a/packages/pg-pool/package.json b/packages/pg-pool/package.json index b92e7df90..e23191828 100644 --- a/packages/pg-pool/package.json +++ b/packages/pg-pool/package.json @@ -1,6 +1,6 @@ { "name": "pg-pool", - "version": "3.3.0", + "version": "3.4.0", "description": "Connection pool for node-postgres", "main": "index.js", "directories": { diff --git a/packages/pg-query-stream/package.json b/packages/pg-query-stream/package.json index d01b18d86..63697b387 100644 --- a/packages/pg-query-stream/package.json +++ b/packages/pg-query-stream/package.json @@ -1,6 +1,6 @@ { "name": "pg-query-stream", - "version": "4.1.0", + "version": "4.2.0", "description": "Postgres query result returned as readable stream", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -37,13 +37,13 @@ "concat-stream": "~1.0.1", "eslint-plugin-promise": "^3.5.0", "mocha": "^7.1.2", - "pg": "^8.6.0", + "pg": "^8.7.0", "stream-spec": "~0.3.5", "stream-tester": "0.0.5", "ts-node": "^8.5.4", "typescript": "^4.0.3" }, "dependencies": { - "pg-cursor": "^2.6.0" + "pg-cursor": "^2.7.0" } } diff --git a/packages/pg/package.json b/packages/pg/package.json index af71629f3..10c941466 100644 --- a/packages/pg/package.json +++ b/packages/pg/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "8.6.0", + "version": "8.7.0", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "database", @@ -23,7 +23,7 @@ "buffer-writer": "2.0.0", "packet-reader": "1.0.0", "pg-connection-string": "^2.5.0", - "pg-pool": "^3.3.0", + "pg-pool": "^3.4.0", "pg-protocol": "^1.5.0", "pg-types": "^2.1.0", "pgpass": "1.x" From 86d31a6fad6ee05facd85bc5f83ca081ebe725b7 Mon Sep 17 00:00:00 2001 From: Brian C Date: Tue, 27 Jul 2021 17:27:05 -0500 Subject: [PATCH 09/12] Only call client.ref if it exists * Only call client.ref if it exists. Fixes #2582 * Make test requiring port less flakey * Bump port range Fixes #2582 Fixes #2584 --- packages/pg-pool/index.js | 2 +- packages/pg/test/integration/client/connection-timeout-tests.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 5557de5c0..48bf5c788 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -137,7 +137,7 @@ class Pool extends EventEmitter { const idleItem = this._idle.pop() clearTimeout(idleItem.timeoutId) const client = idleItem.client - client.ref() + client.ref && client.ref() const idleListener = idleItem.idleListener return this._acquireClient(client, pendingItem, idleListener, false) diff --git a/packages/pg/test/integration/client/connection-timeout-tests.js b/packages/pg/test/integration/client/connection-timeout-tests.js index 6b99698bc..7a3ee4447 100644 --- a/packages/pg/test/integration/client/connection-timeout-tests.js +++ b/packages/pg/test/integration/client/connection-timeout-tests.js @@ -7,7 +7,7 @@ const suite = new helper.Suite() const options = { host: 'localhost', - port: 54321, + port: Math.floor(Math.random() * 2000) + 2000, connectionTimeoutMillis: 2000, user: 'not', database: 'existing', From 92b4d37926c276d343bfe56447ff6f526af757cf Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Tue, 27 Jul 2021 17:33:19 -0500 Subject: [PATCH 10/12] Publish - pg-cursor@2.7.1 - pg-pool@3.4.1 - pg-query-stream@4.2.1 - pg@8.7.1 --- packages/pg-cursor/package.json | 4 ++-- packages/pg-pool/package.json | 2 +- packages/pg-query-stream/package.json | 6 +++--- packages/pg/package.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/pg-cursor/package.json b/packages/pg-cursor/package.json index be43e15f6..b85000aba 100644 --- a/packages/pg-cursor/package.json +++ b/packages/pg-cursor/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "2.7.0", + "version": "2.7.1", "description": "Query cursor extension for node-postgres", "main": "index.js", "directories": { @@ -18,7 +18,7 @@ "license": "MIT", "devDependencies": { "mocha": "^7.1.2", - "pg": "^8.7.0" + "pg": "^8.7.1" }, "peerDependencies": { "pg": "^8" diff --git a/packages/pg-pool/package.json b/packages/pg-pool/package.json index e23191828..d479ae55f 100644 --- a/packages/pg-pool/package.json +++ b/packages/pg-pool/package.json @@ -1,6 +1,6 @@ { "name": "pg-pool", - "version": "3.4.0", + "version": "3.4.1", "description": "Connection pool for node-postgres", "main": "index.js", "directories": { diff --git a/packages/pg-query-stream/package.json b/packages/pg-query-stream/package.json index 63697b387..5f332e8cd 100644 --- a/packages/pg-query-stream/package.json +++ b/packages/pg-query-stream/package.json @@ -1,6 +1,6 @@ { "name": "pg-query-stream", - "version": "4.2.0", + "version": "4.2.1", "description": "Postgres query result returned as readable stream", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -37,13 +37,13 @@ "concat-stream": "~1.0.1", "eslint-plugin-promise": "^3.5.0", "mocha": "^7.1.2", - "pg": "^8.7.0", + "pg": "^8.7.1", "stream-spec": "~0.3.5", "stream-tester": "0.0.5", "ts-node": "^8.5.4", "typescript": "^4.0.3" }, "dependencies": { - "pg-cursor": "^2.7.0" + "pg-cursor": "^2.7.1" } } diff --git a/packages/pg/package.json b/packages/pg/package.json index 10c941466..930a7d928 100644 --- a/packages/pg/package.json +++ b/packages/pg/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "8.7.0", + "version": "8.7.1", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "database", @@ -23,7 +23,7 @@ "buffer-writer": "2.0.0", "packet-reader": "1.0.0", "pg-connection-string": "^2.5.0", - "pg-pool": "^3.4.0", + "pg-pool": "^3.4.1", "pg-protocol": "^1.5.0", "pg-types": "^2.1.0", "pgpass": "1.x" From 98cd59e3e7bd14f77d5f31dbc4115a9de9d26db1 Mon Sep 17 00:00:00 2001 From: Brian C Date: Thu, 29 Jul 2021 17:17:15 -0500 Subject: [PATCH 11/12] Return promise on cursor end (#2589) * Return promise on cursor end * Remove redudant if --- packages/pg-cursor/index.js | 15 +++++---------- packages/pg-cursor/test/close.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/pg-cursor/index.js b/packages/pg-cursor/index.js index b77fd5977..ddfb2b4ca 100644 --- a/packages/pg-cursor/index.js +++ b/packages/pg-cursor/index.js @@ -208,20 +208,15 @@ class Cursor extends EventEmitter { } if (!this.connection || this.state === 'done') { - if (cb) { - return setImmediate(cb) - } else { - return - } + setImmediate(cb) + return promise } this._closePortal() this.state = 'done' - if (cb) { - this.connection.once('readyForQuery', function () { - cb() - }) - } + this.connection.once('readyForQuery', function () { + cb() + }) // Return the promise (or undefined) return promise diff --git a/packages/pg-cursor/test/close.js b/packages/pg-cursor/test/close.js index e63512abd..b34161a17 100644 --- a/packages/pg-cursor/test/close.js +++ b/packages/pg-cursor/test/close.js @@ -23,6 +23,17 @@ describe('close', function () { }) }) + it('can close a finished cursor a promise', function (done) { + const cursor = new Cursor(text) + this.client.query(cursor) + cursor.read(100, (err) => { + assert.ifError(err) + cursor.close().then(() => { + this.client.query('SELECT NOW()', done) + }) + }) + }) + it('closes cursor early', function (done) { const cursor = new Cursor(text) this.client.query(cursor) From 947ccee346f0d598e135548e1e4936a9a008fc6f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 5 Aug 2021 00:59:44 +0000 Subject: [PATCH 12/12] Bump tar from 4.4.13 to 4.4.15 (#2592) Bumps [tar](https://github.com/npm/node-tar) from 4.4.13 to 4.4.15. - [Release notes](https://github.com/npm/node-tar/releases) - [Changelog](https://github.com/npm/node-tar/blob/main/CHANGELOG.md) - [Commits](https://github.com/npm/node-tar/compare/v4.4.13...v4.4.15) --- updated-dependencies: - dependency-name: tar dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index e779f038c..bc5330a1d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5868,9 +5868,9 @@ table@^5.2.3: string-width "^3.0.0" tar@^4.4.10, tar@^4.4.12, tar@^4.4.8: - version "4.4.13" - resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525" - integrity sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA== + version "4.4.15" + resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.15.tgz#3caced4f39ebd46ddda4d6203d48493a919697f8" + integrity sha512-ItbufpujXkry7bHH9NpQyTXPbJ72iTlXgkBAYsAjDXk3Ds8t/3NfO5P4xZGy7u+sYuQUbimgzswX4uQIEeNVOA== dependencies: chownr "^1.1.1" fs-minipass "^1.2.5"