Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased
==========
* Use `res.headersSent` when available

1.7.5 / 2024-10-31
==========

Expand Down
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._header) {
if (!headersSent(res)) {
this._implicitHeader()
}

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

if (!this._header) {
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) {
? chunk
: Buffer.from(chunk, encoding)
}

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