From 9029042466261d14860920384e820e26aa7146fc Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Thu, 5 Dec 2019 17:35:14 -0800 Subject: [PATCH] =?UTF-8?q?Warn=20when=20pg.Pool()=20isn=E2=80=99t=20calle?= =?UTF-8?q?d=20as=20a=20constructor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in preparation for #1541. `eval` is a bit awkward, but it’s more accurate than an `instanceof` check and will work on platforms new enough to support pg 8 (i.e. only not Node 4). --- lib/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/index.js b/lib/index.js index 8e000a378..4ce0eac3d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,8 +13,26 @@ var defaults = require('./defaults') var Connection = require('./connection') var Pool = require('pg-pool') +let hasNewTarget + +try { + // eslint-disable-next-line eval + eval('(function () { new.target })') + hasNewTarget = true +} catch (error) { + hasNewTarget = false +} + const poolFactory = (Client) => { var BoundPool = function (options) { + // new.target is a syntax error in Node 4 + // eslint-disable-next-line eval + if (hasNewTarget && eval('new.target') === undefined) { + // process.emitWarning is supported when new.target is supported + // eslint-disable-next-line node/no-unsupported-features/node-builtins + process.emitWarning('Constructing a pg.Pool without new is deprecated and will stop working in pg 8.', 'DeprecationWarning', 'PG-POOL-NEW') + } + var config = Object.assign({ Client: Client }, options) return new Pool(config) }