From 24a6740de1e6529dbd4bb4dded00814eb78ead28 Mon Sep 17 00:00:00 2001 From: Moritz Peters Date: Wed, 10 Jan 2018 18:56:22 +0100 Subject: [PATCH 1/3] fix: change usage of _header to headersSernt --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f190c689..c5fa036c 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ function compression (options) { return false } - if (!this._header) { + if (!this.headersSent) { this._implicitHeader() } @@ -94,7 +94,7 @@ function compression (options) { return false } - if (!this._header) { + if (!this.headersSent) { // estimate the length if (!this.getHeader('Content-Length')) { length = chunkLength(chunk, encoding) From 14271613dedd9ab714d294a1cb180cea5101ded2 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sun, 3 Nov 2024 17:38:44 -0500 Subject: [PATCH 2/3] improve logic --- index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9f14082a..c518c6e8 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ function compression (options) { return false } - if (!this.headersSent) { + if (!headersSent(res)) { this._implicitHeader() } @@ -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) @@ -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 +} From 9c96376ee11ec382c654afbaea1ceaa4139dfb1f Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sun, 3 Nov 2024 17:40:36 -0500 Subject: [PATCH 3/3] update history --- HISTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 7f625d08..3d224eda 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +unreleased +========== + * Use `res.headersSent` when available + 1.7.5 / 2024-10-31 ==========