From 6835289564270d5dedbfa04524694230a2b66b70 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Mon, 17 Feb 2014 23:36:56 -0500 Subject: [PATCH] remove ServerResonse.headerSent monkey patch node.js ServerResponse contains a headersSent field. Use that instead of our patched misnamed version. --- History.md | 1 + lib/application.js | 2 +- lib/patch.js | 12 ------------ lib/response.js | 6 +++--- test/res.sendfile.js | 6 +++--- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/History.md b/History.md index 04b8ae0e..e680b299 100644 --- a/History.md +++ b/History.md @@ -15,6 +15,7 @@ - `req.accepts*` -> `req.accepts*s` - i.e. `req.acceptsEncoding` -> `req.acceptsEncodings` - `req.params` is now an object instead of an array - `res.locals` is no longer a function. It is a plain js object. Treat it as such. + - `res.headerSent` -> `res.headersSent` to match node.js ServerResponse object * refactor: - `req.accepts*` with [accepts](https://github.com/expressjs/accepts) - `req.is` with [type-is](https://github.com/expressjs/type-is) diff --git a/lib/application.js b/lib/application.js index dc83160d..1dc530ce 100644 --- a/lib/application.js +++ b/lib/application.js @@ -138,7 +138,7 @@ app.handle = function(req, res, done) { // log to stderr in a non-test env if ('test' != env) console.error(err.stack || err.toString()); - if (res.headerSent) return req.socket.destroy(); + if (res.headersSent) return req.socket.destroy(); res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Length', Buffer.byteLength(msg)); if ('HEAD' == req.method) return res.end(); diff --git a/lib/patch.js b/lib/patch.js index 24a20e29..ab832832 100644 --- a/lib/patch.js +++ b/lib/patch.js @@ -20,18 +20,6 @@ if (ServerResponse.prototype._hasConnectPatch) { var setHeader = ServerResponse.prototype.setHeader; var writeHead = ServerResponse.prototype.writeHead; -/** - * Provide a public "header sent" flag - * until node does. - * - * @return {Boolean} - * @api public - */ - -ServerResponse.prototype.__defineGetter__('headerSent', function(){ - return this._header; -}); - /** * Set header `field` to `val`, special-casing * the `Set-Cookie` field for multiple support. diff --git a/lib/response.js b/lib/response.js index 5bfc6472..e9283fff 100644 --- a/lib/response.js +++ b/lib/response.js @@ -311,13 +311,13 @@ res.sendfile = function(path, options, fn){ // clean up cleanup(); - if (!self.headerSent) self.removeHeader('Content-Disposition'); + if (!self.headersSent) self.removeHeader('Content-Disposition'); // callback available if (fn) return fn(err); // list in limbo if there's no callback - if (self.headerSent) return; + if (self.headersSent) return; // delegate next(err); @@ -352,7 +352,7 @@ res.sendfile = function(path, options, fn){ * Optionally providing an alternate attachment `filename`, * and optional callback `fn(err)`. The callback is invoked * when the data transfer is complete, or when an error has - * ocurred. Be sure to check `res.headerSent` if you plan to respond. + * ocurred. Be sure to check `res.headersSent` if you plan to respond. * * This method uses `res.sendfile()`. * diff --git a/test/res.sendfile.js b/test/res.sendfile.js index 30c2ebe0..5532a5a5 100644 --- a/test/res.sendfile.js +++ b/test/res.sendfile.js @@ -42,7 +42,7 @@ describe('res', function(){ app.use(function(req, res){ res.sendfile('test/fixtures/nope.html', function(err){ ++calls; - assert(!res.headerSent); + assert(!res.headersSent); res.send(err.message); }); }); @@ -77,7 +77,7 @@ describe('res', function(){ app.use(function(req, res){ res.sendfile('test/fixtures/foo/../user.html', function(err){ - assert(!res.headerSent); + assert(!res.headersSent); ++calls; res.send(err.message); }); @@ -95,7 +95,7 @@ describe('res', function(){ app.use(function(req, res){ res.sendfile('test/fixtures/user.html', function(err){ - assert(!res.headerSent); + assert(!res.headersSent); req.socket.listeners('error').should.have.length(1); // node's original handler done(); });