Skip to content

Commit

Permalink
Warn when pg.Pool() isn’t called as a constructor
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
charmander committed Dec 6, 2019
1 parent 8f56b8c commit 9029042
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/index.js
Expand Up @@ -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)
}
Expand Down

0 comments on commit 9029042

Please sign in to comment.