Skip to content

fix: preserve headers when statusText is undefined in writeHead - #282

Closed
vaibhavmashal wants to merge 1 commit into
expressjs:masterfrom
vaibhavmashal:fix/writehead-undefined-status-text
Closed

fix: preserve headers when statusText is undefined in writeHead#282
vaibhavmashal wants to merge 1 commit into
expressjs:masterfrom
vaibhavmashal:fix/writehead-undefined-status-text

Conversation

@vaibhavmashal

Copy link
Copy Markdown

Fixes #254

Description

When
es.writeHead(statusCode, reason, headers)\ is called with 3 arguments where
eason/\statusText\ is \undefined\ (or not a string), the 3rd argument (\headers) was not recognized by \on-headers\ and got dropped.

This wraps
es.writeHead\ in \index.js\ to forward (statusCode, headers)\ when \�rguments.length > 2 && typeof reason !== 'string', ensuring response headers are properly set and preserved.

Changes

  • Wrapped
    es.writeHead\ in \index.js\ to normalize 3-argument \writeHead\ calls where
    eason\ is not a string.
  • Added comprehensive unit tests in \ est/compression.js\ covering
    es.writeHead(200, undefined, headers)\ and
    es.writeHead(200, 'OK', headers)\ with both object and array header forms.

When res.writeHead is called with 3 arguments where statusText/reason is undefined (or not a string), ensure headers passed as the 3rd argument are preserved and merged into the response headers.

Fixes expressjs#254
Copilot AI lite review requested due to automatic review settings August 30, 2026 05:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kilisamemarisaaa kilisamemarisaaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wrapper regresses another native writeHead overload. When the third argument is nullish, Node falls back to the non-string second argument as the headers:

res.writeHead(200, { 'X-Second': 'preserved' }, undefined)
res.writeHead(200, { 'X-Second': 'preserved' }, null)

On Node 24.12.0, native writeHead and compression at the base behavior both preserve X-Second; at this PR's cc2c530 both calls silently drop it. The same loss occurs when compression's filter returns false, because the wrapper normalizes the arguments before the compression decision.

The existing suite still passes (61 passing, 1 pending) and lint is clean, so the new tests do not cover this overload. Could the normalization retain the second argument when headers == null (for example, use headers == null ? reason : headers) and add regression tests for both undefined and null third arguments?

@bjohansebas

Copy link
Copy Markdown
Member

close in favor of jshttp/on-headers#49

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.

Compression middleware changes behavior of undefined statusText in writeHead

4 participants