Skip to content

Commit

Permalink
add chunk and chunk size indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
mum-never-proud committed Apr 18, 2020
1 parent 19a6aa5 commit b685198
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ function morgan (format, options) {
// response data
res._startAt = undefined
res._startTime = undefined
res._chunkSize = 0

const write = res.write
res.write = function (body) {
console.log('Response body before sending: ', body)
res._chunkSize += Buffer.byteLength(body)
write.call(this, body)
}

// record request start
recordStartTime.call(req)
Expand Down Expand Up @@ -149,19 +157,19 @@ function morgan (format, options) {
* Apache combined log format.
*/

morgan.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
morgan.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent" ":chunked"')

/**
* Apache common log format.
*/

morgan.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]')
morgan.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :content-length ":chunked"')

/**
* Default format.
*/

morgan.format('default', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
morgan.format('default', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent" ":chunked"')
deprecate.property(morgan, 'default', 'default format: use combined format')

/**
Expand Down Expand Up @@ -330,6 +338,22 @@ morgan.token('user-agent', function getUserAgentToken (req) {
return req.headers['user-agent']
})

/**
* response chunked?
*/

morgan.token('chunked', function getTransferEncoding (_, res) {
return res._contentLength ? undefined : 'chunked'
})

/**
* content-length
*/

morgan.token('content-length', function getTransferEncoding (_, res) {
return res._chunkSize ? res._chunkSize : res._contentLength
})

/**
* request header
*/
Expand Down

0 comments on commit b685198

Please sign in to comment.