Skip to content

Commit

Permalink
Use res.headersSent when available
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 27, 2017
1 parent 7348b5c commit ea47a7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 1 addition & 5 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
unreleased
==========

* Use `res.headersSent` when available
* deps: basic-auth@~2.0.0
- Use `safe-buffer` for improved Buffer API
* deps: debug@2.6.9
* deps: depd@~1.1.1
- Remove unnecessary `Buffer` loading

1.8.2 / 2017-05-23
==================

* deps: debug@2.6.8
- Fix `DEBUG_MAX_ARRAY_LENGTH`
- deps: ms@2.0.0

1.8.1 / 2017-02-04
==================
Expand Down
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ morgan.format('tiny', ':method :url :status :res[content-length] - :response-tim

morgan.format('dev', function developmentFormatLine (tokens, req, res) {
// get the status code if response written
var status = res._header
var status = headersSent(res)
? res.statusCode
: undefined

Expand Down Expand Up @@ -261,7 +261,7 @@ morgan.token('date', function getDateToken (req, res, format) {
*/

morgan.token('status', function getStatusToken (req, res) {
return res._header
return headersSent(res)
? String(res.statusCode)
: undefined
})
Expand Down Expand Up @@ -328,7 +328,7 @@ morgan.token('req', function getRequestToken (req, res, field) {
*/

morgan.token('res', function getResponseHeader (req, res, field) {
if (!res._header) {
if (!headersSent(res)) {
return undefined
}

Expand Down Expand Up @@ -469,6 +469,20 @@ function getip (req) {
undefined
}

/**
* Determine if the response headers have been sent.
*
* @param {object} res
* @returns {boolean}
* @private
*/

function headersSent (res) {
return typeof res.headersSent !== 'boolean'
? Boolean(res._header)
: res.headersSent
}

/**
* Pad number to two digits.
*
Expand Down

0 comments on commit ea47a7d

Please sign in to comment.