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

http: fix legacy http.Client instanceof #8103

Closed
wants to merge 1 commit into from

Conversation

bengl
Copy link
Member

@bengl bengl commented Aug 14, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

http

Description of change

Ensures that objects created with http.createClient() have http.Client.prototype as their prototype.

The new internalUtil.deprecate() fixes instantiation of deprecated classes, but in this case, since the unwrapped deprecated class is instantiated and returned, the prototype on the created object is not the same as the exposed class.

Setting the prototype on the new object to be prototype of the wrapped/exposed class fixes this, so now the following works fine:

http.createClient() instanceof http.Client
Alternatives
  • Instantiate the wrapped class. This would work fine, but you'd double up the deprecation messages, which is undesirable.
  • Use Object.create(http.Client.prototype) and then call the unwrapped constructor on the result (e.g. with Client.call(client, port, host)). I think this is pretty much equivalent to what I did, so I'm happy to change it to this if people think it's cleaner.
  • Ignore this since it's deprecated. I'd rather not, since this is something intuitive and previously working (the instanceof relationship) that was broken by deprecating.

Ensures that objects created with http.createClient() have
http.Client.prototype as their prototype.
@nodejs-github-bot nodejs-github-bot added the http Issues or PRs related to the http subsystem. label Aug 14, 2016
@addaleax
Copy link
Member

Good catch. Maybe this change should just have assigned the prototype instead of extending the chain, i.e. deprecated.prototype = fn.prototype?

@mscdex
Copy link
Contributor

mscdex commented Aug 14, 2016

We should probably remove this altogether in v7. The old http client API has been (hard) deprecated at least since 2012.

@bengl
Copy link
Member Author

bengl commented Aug 14, 2016

@mscdex Yup, for sure. For now though I think this needs to be unbroken for LTS branches.

@addaleax Hmm that's probably a more correct change, and fixes any general case of deprecating both the class and the factory like was done here. Also has the bonus of not touching deprecated code. I'll give that a shot, and if it works and breaks nothing, I'll PR-ify it and close this one.

bengl added a commit to bengl/node that referenced this pull request Aug 14, 2016
Ensure the wrapped class prototype is exactly the unwrapped class
prototype, rather than an object whose prototype is the unwrapped
class prototype.

This ensures that instances of the unwrapped class are instances
of the wrapped class. This is useful when both a wrapped class and
a factory for the unwrapped class are both exposed.

Ref: nodejs#8103
@bengl bengl mentioned this pull request Aug 14, 2016
4 tasks
@bengl
Copy link
Member Author

bengl commented Aug 14, 2016

Closing in favor of #8105

@bengl bengl closed this Aug 14, 2016
jasnell pushed a commit that referenced this pull request Aug 18, 2016
Ensure the wrapped class prototype is exactly the unwrapped class
prototype, rather than an object whose prototype is the unwrapped
class prototype.

This ensures that instances of the unwrapped class are instances
of the wrapped class. This is useful when both a wrapped class and
a factory for the unwrapped class are both exposed.

Ref: #8103
PR-URL: #8105
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
evanlucas pushed a commit that referenced this pull request Aug 20, 2016
Ensure the wrapped class prototype is exactly the unwrapped class
prototype, rather than an object whose prototype is the unwrapped
class prototype.

This ensures that instances of the unwrapped class are instances
of the wrapped class. This is useful when both a wrapped class and
a factory for the unwrapped class are both exposed.

Ref: #8103
PR-URL: #8105
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants