mirror of
https://github.com/brianc/node-postgres.git
synced 2026-02-01 16:47:23 +00:00
Make native non-enumerable (#2065)
* Make `native` non-enumerable Making it non-enumerable means less spurious "Cannot find module" errors in your logs when iterating over `pg` objects. `Object.defineProperty` has been available since Node 0.12. See https://github.com/brianc/node-postgres/issues/1894#issuecomment-543300178 * Add test for `native` enumeration Co-authored-by: Gabe Gorelick <gabegorelick@gmail.com>
This commit is contained in:
parent
5b01eb062d
commit
8494cb449b
@ -44,20 +44,28 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
|
|||||||
module.exports = new PG(Client)
|
module.exports = new PG(Client)
|
||||||
|
|
||||||
// lazy require native module...the native module may not have installed
|
// lazy require native module...the native module may not have installed
|
||||||
module.exports.__defineGetter__('native', function () {
|
Object.defineProperty(module.exports, 'native', {
|
||||||
delete module.exports.native
|
configurable: true,
|
||||||
var native = null
|
enumerable: false,
|
||||||
try {
|
get() {
|
||||||
native = new PG(require('./native'))
|
var native = null
|
||||||
} catch (err) {
|
try {
|
||||||
if (err.code !== 'MODULE_NOT_FOUND') {
|
native = new PG(require('./native'))
|
||||||
throw err
|
} catch (err) {
|
||||||
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
console.error(err.message)
|
||||||
|
/* eslint-enable no-console */
|
||||||
}
|
}
|
||||||
/* eslint-disable no-console */
|
|
||||||
console.error(err.message)
|
// overwrite module.exports.native so that getter is never called again
|
||||||
/* eslint-enable no-console */
|
Object.defineProperty(module.exports, 'native', {
|
||||||
|
value: native
|
||||||
|
})
|
||||||
|
|
||||||
|
return native
|
||||||
}
|
}
|
||||||
module.exports.native = native
|
|
||||||
return native
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
11
packages/pg/test/integration/gh-issues/1992-tests.js
Normal file
11
packages/pg/test/integration/gh-issues/1992-tests.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
"use strict"
|
||||||
|
const helper = require('./../test-helper')
|
||||||
|
const assert = require('assert')
|
||||||
|
|
||||||
|
const suite = new helper.Suite()
|
||||||
|
|
||||||
|
suite.test('Native should not be enumerable', () => {
|
||||||
|
const keys = Object.keys(helper.pg)
|
||||||
|
assert.strictEqual(keys.indexOf('native'), -1)
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user