Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
improve logic
  • Loading branch information
bjohansebas committed Nov 3, 2024
commit 14271613dedd9ab714d294a1cb180cea5101ded2
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function compression (options) {
return false
}

if (!this.headersSent) {
if (!headersSent(res)) {
this._implicitHeader()
}

Expand All @@ -94,7 +94,7 @@ function compression (options) {
return false
}

if (!this.headersSent) {
if (!headersSent(res)) {
// estimate the length
if (!this.getHeader('Content-Length')) {
length = chunkLength(chunk, encoding)
Expand Down Expand Up @@ -281,3 +281,17 @@ function toBuffer (chunk, encoding) {
? Buffer.from(chunk, encoding)
: chunk
}

/**
* 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
}