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

Http2SecureServer Is Not Exported #21434

Closed
shellscape opened this issue Jun 21, 2018 · 13 comments
Closed

Http2SecureServer Is Not Exported #21434

shellscape opened this issue Jun 21, 2018 · 13 comments
Labels
feature request Issues that request new features to be added to Node.js. http2 Issues or PRs related to the http2 subsystem.

Comments

@shellscape
Copy link

shellscape commented Jun 21, 2018

  • Version: 10.5.0
  • Platform: MacOS 10.13.2
  • Subsystem: None

While writing tests for a project I found myself wanting to assert that the return value from a function was an instance of Http2SecureServer. However, upon trying to find the export for Http2SecureServer, I wound up at https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js and found that the class isn't actually listed in the exports. So there's no discernible way to run an instanceof against my return variable, as I can't reference the class.

It's an unfortunate limitation; while testing that an object is an instance of tls.Server is fine and good, I would much rather add an extra layer of assertiveness and make sure that my return value was in fact an instance of Http2SecureServer, as the mechanisms in my use case can also return https.Server, which also inherits from tls.Server.

On the surface this seems like a glaring omission. But there's probably a reason for that. What's the scoop?

Update: It would seem the same is true for Http2Server as well.

@ljharb
Copy link
Member

ljharb commented Jun 21, 2018

What about using .constructor?

@shellscape
Copy link
Author

@ljharb I'm not sure how that gets me to

expect(server).toBeInstanceOf(http2.Http2SecureServer);

While server.constructor requires using util.inspect to get anything meaningful in a jest snapshot, and server.constructor.name returns "Http2SecureServer" but isn't the same as asserting the instance. Regardless, this still seems like a rather large omission and unnecessary pain point. I'd like to hear from the folks who worked on this to learn why it was left out of the exports.

@ChALkeR ChALkeR added feature request Issues that request new features to be added to Node.js. http2 Issues or PRs related to the http2 subsystem. labels Jun 21, 2018
@apapirovski
Copy link
Member

@shellscape Why not just use http2.createSecureServer().constructor in your tests? Node.js won't export anything other than Http2SecureServer from createSecureServer().

@targos
Copy link
Member

targos commented Jun 24, 2018

This makes me wonder why the API is http2.createSecureServer() and not new http2.SecureServer().

@TimothyGu
Copy link
Member

TimothyGu commented Jun 24, 2018

@targos It could easily be:

function createSecureServer(options, handler) {
assertIsObject(options, 'options');
return new Http2SecureServer(options, handler);
}
function createServer(options, handler) {
if (typeof options === 'function') {
handler = options;
options = {};
}
assertIsObject(options, 'options');
return new Http2Server(options, handler);
}

/cc @jasnell

@ljharb
Copy link
Member

ljharb commented Jun 24, 2018

Requiring the api to be a constructor more tightly couples the implementation detail that it’s a class.

@TimothyGu
Copy link
Member

TimothyGu commented Jun 24, 2018

@ljharb Agreed. In this case however I think it's fair to make Http2Server is a class though.

@apapirovski
Copy link
Member

I think this keeps compatibility with how we do things in the other modules. It's easy enough to get the constructor instance for testing that IMO nothing needs to be changed.

@targos
Copy link
Member

targos commented Jun 24, 2018

But in other modules we expose the constructor. http2 is the outlier. It's also inconsistent with itself: http2.Http2ServerRequest and http2.Http2ServerResponse are exposed and the documentation mentions http2.Server and http2.SecureServer which don't exist: https://nodejs.org/dist/latest-v10.x/docs/api/http2.html#http2_class_http2_http2serverrequest

@shellscape
Copy link
Author

shellscape commented Jun 24, 2018

Yeah the lack of consistency (as classes being exposed through exports are much more common) was why I initially created the issue. As @targos mentions, the way the documentation is written also suggests the class should be available on the export. There doesn't seem to be a cohesiveness between namespaces (net, tls, http, https, http2) as to how classes should or should not be exported. And no hint in the documentation as to which are and which are not.

@apapirovski
Copy link
Member

apapirovski commented Jun 24, 2018

But in other modules we expose the constructor.

And IMO it's a mistake which can lead to all sorts of repercussions later on. The current behaviour also makes it explicit that there's one API to create the server.

It's important to note that the only reason we exposed Http2ServerRequest and Http2ServerResponse is to allow users to create extended versions in an officially supported manner, where we provide a clear contract around their usage. (And by comparison, neither Http2Session nor Http2Stream are exposed.) That choice was also driven by the fact that http exported ServerRequest and ServerResponse. If that was not the case, we would be in a much better position to improve on it going forward.

I also don't see an applicable use case for exposing this particular class as one can easily get access to the constructor in order to do unit testing.

@jasnell
Copy link
Member

jasnell commented Jun 24, 2018

This design was intentional specifically to allow more flexibility in the implementation as it evolves. Exposing the constrictor directly exposes a number of issues in terms of maintenance. I'm -1 right now for exposing the constructor directly without having a clear use case

@shellscape
Copy link
Author

shellscape commented Jun 24, 2018

@jasnell @apapirovski I really don't anyone to gloss over the docs being unclear on this. If the constructor is not exposed, I can definitely live with that. But the docs at the very least need help in stating which are exported and which are not.

jasnell added a commit to jasnell/node that referenced this issue Aug 12, 2018
@gdams gdams closed this as completed in e4bbfda Aug 12, 2018
gdams pushed a commit that referenced this issue Aug 12, 2018
PR-URL: #22247
Fixes: #21434
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
rvagg pushed a commit that referenced this issue Aug 13, 2018
PR-URL: #22247
Fixes: #21434
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
kjin pushed a commit to kjin/node that referenced this issue Sep 25, 2018
PR-URL: nodejs#22247
Fixes: nodejs#21434
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
kjin pushed a commit to kjin/node that referenced this issue Oct 16, 2018
PR-URL: nodejs#22247
Fixes: nodejs#21434
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
BethGriggs pushed a commit that referenced this issue Oct 17, 2018
Backport-PR-URL: #22850
PR-URL: #22247
Fixes: #21434
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. http2 Issues or PRs related to the http2 subsystem.
Projects
None yet
Development

No branches or pull requests

7 participants