finalhandler
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -0,1 +1,11 @@ | ||
0.5.0 / 2016-06-15 | ||
================== | ||
* Change invalid or non-numeric status code to 500 | ||
* Overwrite status message to match set status code | ||
* Prefer `err.statusCode` if `err.status` is invalid | ||
* Set response headers from `err.headers` object | ||
* Use `statuses` instead of `http` module for status messages | ||
- Includes all defined status messages | ||
0.4.1 / 2015-12-02 | ||
@@ -2,0 +12,0 @@ ================== |
78
index.js
@@ -16,4 +16,4 @@ /*! | ||
var escapeHtml = require('escape-html') | ||
var http = require('http') | ||
var onFinished = require('on-finished') | ||
var statuses = require('statuses') | ||
var unpipe = require('unpipe') | ||
@@ -29,3 +29,3 @@ | ||
? setImmediate | ||
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } | ||
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) } | ||
var isFinished = onFinished.isFinished | ||
@@ -50,3 +50,3 @@ | ||
function finalhandler(req, res, options) { | ||
function finalhandler (req, res, options) { | ||
var opts = options || {} | ||
@@ -61,3 +61,4 @@ | ||
return function (err) { | ||
var status = res.statusCode | ||
var headers = Object.create(null) | ||
var status | ||
@@ -72,15 +73,17 @@ // ignore 404 on in-flight response | ||
if (err) { | ||
// respect err.statusCode | ||
if (err.statusCode) { | ||
status = err.statusCode | ||
} | ||
// respect status code from error | ||
status = getErrorStatusCode(err) || res.statusCode | ||
// respect err.status | ||
if (err.status) { | ||
status = err.status | ||
// default status code to 500 if outside valid range | ||
if (typeof status !== 'number' || status < 400 || status > 599) { | ||
status = 500 | ||
} | ||
// default status code to 500 | ||
if (!status || status < 400) { | ||
status = 500 | ||
// respect err.headers | ||
if (err.headers && (err.status === status || err.statusCode === status)) { | ||
var keys = Object.keys(err.headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
headers[key] = err.headers[key] | ||
} | ||
} | ||
@@ -90,7 +93,7 @@ | ||
var msg = env === 'production' | ||
? http.STATUS_CODES[status] | ||
? statuses[status] | ||
: err.stack || err.toString() | ||
msg = escapeHtml(msg) | ||
.replace(/\n/g, '<br>') | ||
.replace(/ /g, ' ') + '\n' | ||
.replace(/\x20{2}/g, ' ') + '\n' | ||
} else { | ||
@@ -110,6 +113,9 @@ status = 404 | ||
if (res._header) { | ||
return req.socket.destroy() | ||
debug('cannot %d after headers sent', status) | ||
req.socket.destroy() | ||
return | ||
} | ||
send(req, res, status, msg) | ||
// send response | ||
send(req, res, status, headers, msg) | ||
} | ||
@@ -119,2 +125,24 @@ } | ||
/** | ||
* Get status code from Error object. | ||
* | ||
* @param {Error} err | ||
* @return {number} | ||
* @private | ||
*/ | ||
function getErrorStatusCode (err) { | ||
// check err.status | ||
if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) { | ||
return err.status | ||
} | ||
// check err.statusCode | ||
if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) { | ||
return err.statusCode | ||
} | ||
return undefined | ||
} | ||
/** | ||
* Send response. | ||
@@ -125,2 +153,3 @@ * | ||
* @param {number} status | ||
* @param {object} headers | ||
* @param {string} body | ||
@@ -130,6 +159,15 @@ * @private | ||
function send(req, res, status, body) { | ||
function write() { | ||
function send (req, res, status, headers, body) { | ||
function write () { | ||
// response status | ||
res.statusCode = status | ||
res.statusMessage = statuses[status] | ||
// response headers | ||
var keys = Object.keys(headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
res.setHeader(key, headers[key]) | ||
} | ||
// security header for content sniffing | ||
@@ -136,0 +174,0 @@ res.setHeader('X-Content-Type-Options', 'nosniff') |
{ | ||
"name": "finalhandler", | ||
"description": "Node.js final http responder", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -12,8 +12,13 @@ "license": "MIT", | ||
"on-finished": "~2.3.0", | ||
"statuses": "~1.3.0", | ||
"unpipe": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.4.1", | ||
"mocha": "2.3.4", | ||
"readable-stream": "2.0.4", | ||
"eslint": "2.12.0", | ||
"eslint-config-standard": "5.3.1", | ||
"eslint-plugin-promise": "1.3.2", | ||
"eslint-plugin-standard": "1.3.2", | ||
"istanbul": "0.4.3", | ||
"mocha": "2.5.3", | ||
"readable-stream": "2.1.2", | ||
"supertest": "1.1.0" | ||
@@ -30,2 +35,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint **/*.js", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
@@ -32,0 +38,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", |
@@ -28,4 +28,13 @@ # finalhandler | ||
write out a 404 response to the `res`. If it is truthy, an error response will | ||
be written out to the `res`, and `res.statusCode` is set from `err.status`. | ||
be written out to the `res`. | ||
When an error is written, the following information is added to the response: | ||
* The `res.statusCode` is set from `err.status` (or `err.statusCode`). If | ||
this value is outside the 4xx or 5xx range, it will be set to 500. | ||
* The `res.statusMessage` is set according to the status code. | ||
* The body will be the HTML of the status code message if `env` is | ||
`'production'`, otherwise will be `err.stack`. | ||
* Any headers specified in an `err.headers` object. | ||
The final handler will also unpipe anything from `req` when it is invoked. | ||
@@ -128,3 +137,3 @@ | ||
[node-image]: https://img.shields.io/node/v/finalhandler.svg | ||
[node-url]: http://nodejs.org/download/ | ||
[node-url]: https://nodejs.org/en/download | ||
[travis-image]: https://img.shields.io/travis/pillarjs/finalhandler.svg | ||
@@ -131,0 +140,0 @@ [travis-url]: https://travis-ci.org/pillarjs/finalhandler |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
12405
154
143
0
5
8
+ Addedstatuses@~1.3.0
+ Addedstatuses@1.3.1(transitive)