Skip to content

Commit 9029042

Browse files
committed
Warn when pg.Pool() isn’t called as a constructor
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).
1 parent 8f56b8c commit 9029042

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,26 @@ var defaults = require('./defaults')
1313
var Connection = require('./connection')
1414
var Pool = require('pg-pool')
1515

16+
let hasNewTarget
17+
18+
try {
19+
// eslint-disable-next-line eval
20+
eval('(function () { new.target })')
21+
hasNewTarget = true
22+
} catch (error) {
23+
hasNewTarget = false
24+
}
25+
1626
const poolFactory = (Client) => {
1727
var BoundPool = function (options) {
28+
// new.target is a syntax error in Node 4
29+
// eslint-disable-next-line eval
30+
if (hasNewTarget && eval('new.target') === undefined) {
31+
// process.emitWarning is supported when new.target is supported
32+
// eslint-disable-next-line node/no-unsupported-features/node-builtins
33+
process.emitWarning('Constructing a pg.Pool without new is deprecated and will stop working in pg 8.', 'DeprecationWarning', 'PG-POOL-NEW')
34+
}
35+
1836
var config = Object.assign({ Client: Client }, options)
1937
return new Pool(config)
2038
}

0 commit comments

Comments
 (0)