Skip to content

fix: resolve #254 - preserve headers set via writeHead - #273

Closed
abhii734 wants to merge 1 commit into
expressjs:masterfrom
abhii734:fix/writehead-headers
Closed

abhii734 wants to merge 1 commit into
expressjs:masterfrom
abhii734:fix/writehead-headers

Conversation

@abhii734

Copy link
Copy Markdown

Bug (compression#254)

When user calls res.writeHead(200, undefined, {'foo': 'bar'}) with custom headers, the compression middleware overwrites those headers when it calls this.writeHead(this.statusCode) without passing the headers.

Fix

Check res._header to detect if writeHead was already called with headers, and skip calling writeHead again if headers exist.

Verification

  1. Setup compression middleware with filter returning false (no compression)
  2. Call res.writeHead(200, undefined, {'foo': 'bar'})
  3. Before fix: headers not set
  4. After fix: headers preserved

When user calls res.writeHead(200, undefined, {'foo': 'bar'}),
compression middleware was overwriting those headers by calling
writeHead again without the headers. Check res._header to detect
if writeHead was already called.
@UlisesGascon

Copy link
Copy Markdown
Member

Thanks for looking into this @abhii734 and for helping the project!

The headers are dropped inside on-headers before compression ever sees them, so this change cannot restore them, and res.headersSent is already !!res._header on all supported Node versions, so the new condition is equivalent to the old one. The fix is jshttp/on-headers#49.

Closing in favor of that, and #254 stays open until it is released.

@UlisesGascon UlisesGascon self-assigned this Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants