Skip to content

Commit 8494cb4

Browse files
briancgabegorelick
andauthored
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 #1894 (comment) * Add test for `native` enumeration Co-authored-by: Gabe Gorelick <[email protected]>
1 parent 5b01eb0 commit 8494cb4

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

packages/pg/lib/index.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,28 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
4444
module.exports = new PG(Client)
4545

4646
// lazy require native module...the native module may not have installed
47-
module.exports.__defineGetter__('native', function () {
48-
delete module.exports.native
49-
var native = null
50-
try {
51-
native = new PG(require('./native'))
52-
} catch (err) {
53-
if (err.code !== 'MODULE_NOT_FOUND') {
54-
throw err
47+
Object.defineProperty(module.exports, 'native', {
48+
configurable: true,
49+
enumerable: false,
50+
get() {
51+
var native = null
52+
try {
53+
native = new PG(require('./native'))
54+
} catch (err) {
55+
if (err.code !== 'MODULE_NOT_FOUND') {
56+
throw err
57+
}
58+
/* eslint-disable no-console */
59+
console.error(err.message)
60+
/* eslint-enable no-console */
5561
}
56-
/* eslint-disable no-console */
57-
console.error(err.message)
58-
/* eslint-enable no-console */
62+
63+
// overwrite module.exports.native so that getter is never called again
64+
Object.defineProperty(module.exports, 'native', {
65+
value: native
66+
})
67+
68+
return native
5969
}
60-
module.exports.native = native
61-
return native
6270
})
6371
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
"use strict"
3+
const helper = require('./../test-helper')
4+
const assert = require('assert')
5+
6+
const suite = new helper.Suite()
7+
8+
suite.test('Native should not be enumerable', () => {
9+
const keys = Object.keys(helper.pg)
10+
assert.strictEqual(keys.indexOf('native'), -1)
11+
})

0 commit comments

Comments
 (0)