-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
No longer able to monkey-patch HTTP parser #23716
Comments
cc @nodejs/http-parser |
i'd be interested in seeing a parser option added to the public api. |
@devsnek Right now the http parser seems often pretty coupled with the internal http modules (there's some two-way communication about things like upgrade requests) in ways that make it hard to have a good clean API (and require a bunch of Node-version-specific behaviors in http-parser-js), but obviously that could be improved, but I think that's a much larger undertaking than anything I'm inquiring about =). |
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: nodejs#23716 Fixes: creationix/http-parser-js#57
I didn't hear anything back, so investigated Node-level fixes more. Adding a freeParser callback to sockets proved problematic, because it can't just be added to the prototype lest we just move the require() of _http_common into net.js instead (even worse). Simplest approach seemed to be to change internal/child_process to lazy-require the http stack when needed, and there was some precedence for lazy-requiring in that module already. PR #24006 created for this. |
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: nodejs#23716 Fixes: creationix/http-parser-js#57 PR-URL: nodejs#24006 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Fixed in 7d86a53 |
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: #23716 Fixes: creationix/http-parser-js#57 PR-URL: #24006 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: nodejs#23716 Fixes: creationix/http-parser-js#57 PR-URL: nodejs#24006 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: #23716 Fixes: creationix/http-parser-js#57 PR-URL: #24006 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: #23716 Fixes: creationix/http-parser-js#57 PR-URL: #24006 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Hi, I'm the maintainer of http-parser-js, a pure Javascript implementation of the HTTP parser.
Starting with Node v10.1.0, specifically this commit, users can no longer reliably "monkey-patch" this module in to replace the internal HTTP parser (usually done with
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser
), because some boot-strapping code is requiring_http_common
before any user-level code is possibly ran (note: only goes wrong in a forked/child/cluster process, but those are pretty common). For reference our discussion has been over here.I realize support for replacing internal modules is pretty low priority, and when maintaining the Node.js internal modules, maintainers should never have to worry about what 3rd party modules might be doing with their API, but it would be great if we could find some way to keep this module working with future versions of Node, but I've been unable to figure out any workaround as things stand now.
First, a quick note on why this module is important to our users:
request
andexpress
)Anyway, I'd love some help getting this module working reliably again, on Node v10.x+.
Without making changers to Node, is there some other way to monkey-patch our module in? If we accept that, now, some Node modules will have cached the HTTPParser constructor (but never called it) before our user code is ran, maybe there's some other way we can monkey-patch our code in. We should be able to modify HTTPParser.prototype to ours, but the actual constructor call seems like it will always call the native constructor (and we would never have a chance to catch the arguments to said constructor). My research says "no", but is there any trick we can do to get the HTTPParser constructor to call our replacement (even if it calls both the native one and then ours)?
If we were to make a small change to Node, what would be best?
internalBinding('http_parser')
instead ofinternalBinding('http_parser').HTTPParser
as it's doing now. This might have a (more minor) performance impact, as code would need to referencehttp_parser.HTTPParser
instead of justHTTPParser
everywhere. This might also require code that conflicts with the style that is normally used in internal Node modules.Any advice would help, would any PRs doing any of the above be acceptable?
The text was updated successfully, but these errors were encountered: