Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chunk and chunk size indicator #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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