Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make native non-enumerable #1992

Closed

Conversation

gabegorelick
Copy link
Contributor

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)

@gabegorelick gabegorelick force-pushed the nonenumerable-native branch 3 times, most recently from 41c411a to 32ba854 Compare October 17, 2019 18:49
@gabegorelick
Copy link
Contributor Author

Test failures are from tests that are flaky on master.

lib/index.js Outdated
throw err
var native
Object.defineProperty(module.exports, 'native', {
enumerable: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of a get here implies writable: false, and Object.defineProperty defaults to configurable: false. Any of those could be breaking changes, depending on how clients are interactive with native (are they overwriting it?).

lib/index.js Outdated Show resolved Hide resolved
@charmander
Copy link
Collaborator

Cloning or iterating over pg is a very strange thing to do to begin with… not that I think this change shouldn’t go in.

@gabegorelick
Copy link
Contributor Author

Cloning or iterating over pg is a very strange thing to do to begin with

Unintentional cloning is pretty common in ORMs like Sequelize where models have a reference to DB connections. So making a deep copy of a model instance ends up enumerating pg instances. See sequelize/sequelize#3781.

But cloning a pg instance directly? I agree that is strange.

I think a more likely use case that this PR could break is consumers overriding or deleting native, perhaps to avoid the console.log. Unfortunately, this change will cause such use cases to throw an error.

@1valdis
Copy link

1valdis commented Oct 28, 2019

Can we have this merged and released ASAP, please? In sequelize, there are weird errors cannot find module "pg-native" happening at seemingly random situations and I couldn't find the root cause...

@charmander
Copy link
Collaborator

@1valdis That’s probably a bug you or Sequelize should fix, not hide. Deep-cloning clients is a bad idea.

@gabegorelick
Copy link
Contributor Author

Any chance this will get merged?

@brianc brianc added this to the [email protected] milestone Nov 11, 2019
@brianc
Copy link
Owner

brianc commented Nov 11, 2019

yah we try to be as pedantic as possible about breaking changes and this is technically a non-backwards compatible change. I agree it's super bizarre to enumerate the pg instance but folks do bizarre things sometimes.

It's also a small thing, but i'd like to see at least a simple unit test that does something like

assert.equals(Object.keys(pg).includes('native'), false)

or something just to future-proof against this changing.

I also totally agree w/ charmander...if code is 'randomly' throwing an error about pg-native not being installed it's nothing to do w/ this module directly (though making the prop non-enumerable will probably help those situations) because pg doesn't do anything randomly wrt requring native so it's the consuming module's responsibility to fix that random error, not here.

@gabegorelick
Copy link
Contributor Author

yah we try to be as pedantic as possible about breaking changes and this is technically a non-backwards compatible change. I agree it's super bizarre to enumerate the pg instance but folks do bizarre things sometimes.

@brianc Does that mean this PR should be reworked to be backwards compatible, or that you're fine releasing a new major version of node-pg? I'm not sure there's an easy way to make it backwards compatible. Instead we may have to introduce something like #1894 that allows consumers to opt-in to this change.

@brianc
Copy link
Owner

brianc commented Nov 19, 2019

I think leaving this as non-backwards compatible is fine. I need to do a semver major bump soon and I'll sweep it into those changes when I do that. If things pan out well I'll be able to do it this weekend, but if not definitely in the next few weeks. Holidays + lots of travel make things extra slow...but I appreciate the work and will release it ASAP!

@1valdis
Copy link

1valdis commented Nov 20, 2019

Awesome news. Waiting for the release, hoping that Sequelize won't break with major version change of pg. Is there a (preliminary) list of breaking changes?

@brianc
Copy link
Owner

brianc commented Nov 20, 2019 via email

lib/index.js Outdated Show resolved Hide resolved
lib/index.js Outdated Show resolved Hide resolved
lib/index.js Outdated Show resolved Hide resolved
@gabegorelick gabegorelick force-pushed the nonenumerable-native branch 2 times, most recently from 07f44a7 to 18a8e14 Compare December 6, 2019 15:57
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 brianc#1894 (comment)
@gabegorelick
Copy link
Contributor Author

Rebased to fix merge conflicts, but otherwise no changes were made.

@brianc Let me know if there's anything I can help with to get this merged and released.

@brianc
Copy link
Owner

brianc commented Jan 3, 2020 via email

@charmander
Copy link
Collaborator

Landed on the 8.0 branch with a test in #2065.

@charmander charmander closed this Jan 15, 2020
@gabegorelick gabegorelick deleted the nonenumerable-native branch January 15, 2020 22:44
@michaelbromley
Copy link

I agree it's super bizarre to enumerate the pg instance but folks do bizarre things sometimes. #1992 (comment)

I've just been chasing down this issue and I think in my case it is caused by TypeScript's implementation of ES module imports:

https://github.com/microsoft/tslib/blob/3cf8ae045d949fe7a636d8274d0974a4f30ede8d/tslib.js#L227-L233

    __importStar = function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
        result["default"] = mod;
        return result;
    };

My use-case is using pg in a Jest test file written in TypeScript. I look forward to the 8.0 release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants