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 ddf4f8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Use `res.headersSent` when available
* deps: basic-auth@~2.0.0
- Use `safe-buffer` for improved Buffer API
* deps: debug@2.6.9
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 ddf4f8c

Please sign in to comment.