finalhandler
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -0,1 +1,9 @@ | ||
0.5.1 / 2016-11-12 | ||
================== | ||
* Fix exception when `err.headers` is not an object | ||
* deps: statuses@~1.3.1 | ||
* perf: hoist regular expressions | ||
* perf: remove duplicate validation path | ||
0.5.0 / 2016-06-15 | ||
@@ -2,0 +10,0 @@ ================== |
98
index.js
/*! | ||
* finalhandler | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2016 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -25,2 +25,5 @@ */ | ||
var DOUBLE_SPACE_REGEXP = /\x20{2}/g | ||
var NEWLINE_REGEXP = /\n/g | ||
/* istanbul ignore next */ | ||
@@ -59,3 +62,3 @@ var defer = typeof setImmediate === 'function' | ||
return function (err) { | ||
var headers = Object.create(null) | ||
var headers | ||
var status | ||
@@ -72,16 +75,12 @@ | ||
// respect status code from error | ||
status = getErrorStatusCode(err) || res.statusCode | ||
status = getErrorStatusCode(err) | ||
// default status code to 500 if outside valid range | ||
if (typeof status !== 'number' || status < 400 || status > 599) { | ||
status = 500 | ||
// respect headers from error | ||
if (status !== undefined) { | ||
headers = getErrorHeaders(err) | ||
} | ||
// 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] | ||
} | ||
// fallback to status code on response | ||
if (status === undefined) { | ||
status = getResponseStatusCode(res) | ||
} | ||
@@ -94,4 +93,4 @@ | ||
msg = escapeHtml(msg) | ||
.replace(/\n/g, '<br>') | ||
.replace(/\x20{2}/g, ' ') + '\n' | ||
.replace(NEWLINE_REGEXP, '<br>') | ||
.replace(DOUBLE_SPACE_REGEXP, ' ') + '\n' | ||
} else { | ||
@@ -122,2 +121,26 @@ status = 404 | ||
/** | ||
* Get headers from Error object. | ||
* | ||
* @param {Error} err | ||
* @return {object} | ||
* @private | ||
*/ | ||
function getErrorHeaders (err) { | ||
if (!err.headers || typeof err.headers !== 'object') { | ||
return undefined | ||
} | ||
var headers = Object.create(null) | ||
var keys = Object.keys(err.headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
headers[key] = err.headers[key] | ||
} | ||
return headers | ||
} | ||
/** | ||
* Get status code from Error object. | ||
@@ -145,2 +168,21 @@ * | ||
/** | ||
* Get status code from response. | ||
* | ||
* @param {OutgoingMessage} res | ||
* @return {number} | ||
* @private | ||
*/ | ||
function getResponseStatusCode (res) { | ||
var status = res.statusCode | ||
// default status code to 500 if outside valid range | ||
if (typeof status !== 'number' || status < 400 || status > 599) { | ||
status = 500 | ||
} | ||
return status | ||
} | ||
/** | ||
* Send response. | ||
@@ -163,7 +205,3 @@ * | ||
// response headers | ||
var keys = Object.keys(headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
res.setHeader(key, headers[key]) | ||
} | ||
setHeaders(res, headers) | ||
@@ -197,1 +235,21 @@ // security header for content sniffing | ||
} | ||
/** | ||
* Set response headers from an object. | ||
* | ||
* @param {OutgoingMessage} res | ||
* @param {object} headers | ||
* @private | ||
*/ | ||
function setHeaders (res, headers) { | ||
if (!headers) { | ||
return | ||
} | ||
var keys = Object.keys(headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
res.setHeader(key, headers[key]) | ||
} | ||
} |
{ | ||
"name": "finalhandler", | ||
"description": "Node.js final http responder", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -12,11 +12,12 @@ "license": "MIT", | ||
"on-finished": "~2.3.0", | ||
"statuses": "~1.3.0", | ||
"statuses": "~1.3.1", | ||
"unpipe": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"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", | ||
"eslint": "3.10.0", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-markdown": "1.0.0-beta.3", | ||
"eslint-plugin-promise": "3.3.2", | ||
"eslint-plugin-standard": "2.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "2.5.3", | ||
@@ -35,7 +36,7 @@ "readable-stream": "2.1.2", | ||
"scripts": { | ||
"lint": "eslint **/*.js", | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | ||
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/" | ||
} | ||
} |
@@ -13,2 +13,6 @@ # finalhandler | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
```sh | ||
@@ -20,3 +24,3 @@ $ npm install finalhandler | ||
```js | ||
``` | ||
var finalhandler = require('finalhandler') | ||
@@ -126,3 +130,3 @@ ``` | ||
function logerror(err) { | ||
function logerror (err) { | ||
console.error(err.stack || err.toString()) | ||
@@ -129,0 +133,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13728
199
147
9
Updatedstatuses@~1.3.1