-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Remove direct http dependency #3213
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,30 @@ var Router = require('router'); | |
var req = require('./request'); | ||
var res = require('./response'); | ||
|
||
/** | ||
* Only reqire http when requested | ||
*/ | ||
var http = {} | ||
var _http | ||
Object.defineProperty(http, 'IncomingMessage', { | ||
get: function () { | ||
_http = _http || require('http') | ||
return _http.IncomingMessage | ||
} | ||
}) | ||
Object.defineProperty(http, 'ServerResponse', { | ||
get: function () { | ||
_http = _http || require('http') | ||
return _http.ServerResponse | ||
} | ||
}) | ||
Object.defineProperty(http, 'createServer', { | ||
get: function () { | ||
_http = _http || require('http') | ||
return _http.createServer | ||
} | ||
}) | ||
|
||
/** | ||
* Expose `createApplication()`. | ||
*/ | ||
|
@@ -33,21 +57,26 @@ exports = module.exports = createApplication; | |
* @api public | ||
*/ | ||
|
||
function createApplication() { | ||
function createApplication (opts) { | ||
var options = opts || {} | ||
options.reqProto = options.reqProto || http.IncomingMessage.prototype | ||
options.resProto = options.resProto || http.ServerResponse.prototype | ||
options.createServer = options.createServer || http.createServer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of making the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this idea and honestly any use cases where you might use node's server but custom req/res is probably not really that viable anyway. Maybe to nitpick the name idea though, what about |
||
|
||
var app = function(req, res, next) { | ||
app.handle(req, res, next); | ||
}; | ||
|
||
mixin(app, EventEmitter.prototype, false); | ||
mixin(app, proto, false); | ||
|
||
app.createServer = options.createServer | ||
// expose the prototype that will get set on requests | ||
app.request = Object.create(req, { | ||
app.request = Object.create(req(options.reqProto), { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤷 no idea what I was thinking when I wrote this. I agree that feels more clear to read. I cannot make time to make these changes now but I will when I get time to revisit this pr. |
||
app: { configurable: true, enumerable: true, writable: true, value: app } | ||
}) | ||
|
||
// expose the prototype that will get set on responses | ||
app.response = Object.create(res, { | ||
app.response = Object.create(res(options.resProto), { | ||
app: { configurable: true, enumerable: true, writable: true, value: app } | ||
}) | ||
|
||
|
@@ -60,8 +89,8 @@ function createApplication() { | |
*/ | ||
|
||
exports.application = proto; | ||
exports.request = req; | ||
exports.response = res; | ||
exports.request = req.proto | ||
exports.response = res.proto | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these exports are meaningless now because they're just empty objects? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah probably, hard to remember if I had even looked at that since it was so long ago, but I think you are right. I will hold off on addressing these because the other review comments are much more important for us to address first imo. |
||
|
||
/** | ||
* Expose constructors. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,25 +16,25 @@ | |
var accepts = require('accepts'); | ||
var isIP = require('net').isIP; | ||
var typeis = require('type-is'); | ||
var http = require('http'); | ||
var fresh = require('fresh'); | ||
var parseRange = require('range-parser'); | ||
var parse = require('parseurl'); | ||
var proxyaddr = require('proxy-addr'); | ||
var setPrototypeOf = require('setprototypeof') | ||
|
||
/** | ||
* Request prototype. | ||
* @public | ||
*/ | ||
|
||
var req = Object.create(http.IncomingMessage.prototype) | ||
exports = module.exports = function (proto) { | ||
return setPrototypeOf(req, proto) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's almost functionally the same for a single server, but this would be clearer by making all of Edit: This isn't required if you instead do the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree with this including the edit. The use case of multiple servers was always bad with express patching things though so I am not sure this makes it worse, but the mixin way seems like it would make it better in general. I am +1 for going that route and calling these other comments resolved. |
||
} | ||
|
||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
|
||
module.exports = req | ||
var req = module.exports.proto = {} | ||
|
||
/** | ||
* Return request header. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly I think this should just deprecate this in favor of people explicitly writing
http.createServer(app).listen(...)
. It also removes one more option from thecreateApplication
options which is only ever used once here. It took me a while to follow the indirect assignments around and I think that's bad for developers and users to understanding what's going on.But I do understand why it's done after 30 minutes of review and won't block on this issue. I would be in favor of refactoring this to assign
app.listen
within the create function instead. That way there's less indirect dependencies that's hard to reason about andcreateServer
is used right where it's defined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of the options to achieve this I think this one is my preference. I believe any of these loose the
once
behavior, but I am not sure it makes a big difference but I agree simplifying things like this is good. I honestly cannot remember if there used to be cases where the server could emiterror
events more than once, but maybe that was an old node behavior?