Skip to content

Commit 8e5641c

Browse files
maritzbjohansebas
andauthoredNov 9, 2024··
Use headersSent instead of _header (#129)
* fix: change usage of _header to headersSernt * improve logic * update history --------- Co-authored-by: Sebastian Beltran <[email protected]>
1 parent 7b7f84b commit 8e5641c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

Diff for: ‎HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
unreleased
2+
==========
3+
* Use `res.headersSent` when available
4+
15
1.7.5 / 2024-10-31
26
==========
37

Diff for: ‎index.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function compression (options) {
8080
return false
8181
}
8282

83-
if (!this._header) {
83+
if (!headersSent(res)) {
8484
this._implicitHeader()
8585
}
8686

@@ -94,7 +94,7 @@ function compression (options) {
9494
return false
9595
}
9696

97-
if (!this._header) {
97+
if (!headersSent(res)) {
9898
// estimate the length
9999
if (!this.getHeader('Content-Length')) {
100100
length = chunkLength(chunk, encoding)
@@ -281,3 +281,17 @@ function toBuffer (chunk, encoding) {
281281
? chunk
282282
: Buffer.from(chunk, encoding)
283283
}
284+
285+
/**
286+
* Determine if the response headers have been sent.
287+
*
288+
* @param {object} res
289+
* @returns {boolean}
290+
* @private
291+
*/
292+
293+
function headersSent (res) {
294+
return typeof res.headersSent !== 'boolean'
295+
? Boolean(res._header)
296+
: res.headersSent
297+
}

0 commit comments

Comments
 (0)
Please sign in to comment.