Skip to content

fix: preserve writeHead headers when statusMessage is undefined - #288

Open
dyk1454683243-sudo wants to merge 1 commit into
expressjs:masterfrom
dyk1454683243-sudo:cursor/fix-writehead-undefined-status-7f9b
Open

dyk1454683243-sudo wants to merge 1 commit into
expressjs:masterfrom
dyk1454683243-sudo:cursor/fix-writehead-undefined-status-7f9b

Conversation

@dyk1454683243-sudo

Copy link
Copy Markdown

Fixes #254

Problem

res.writeHead(statusCode, undefined, headers) (and the same call with null as the status text) silently drops the headers when compression is mounted. Native http.ServerResponse applies those headers and uses the default reason phrase for the status code (Created for 201, OK for 200). It does not stringify undefined into the status line.

This is the Vercel AI SDK path: response.writeHead(status ?? 200, statusText, headers) when statusText is omitted.

Root cause: on-headers@1.1.x picks the headers argument with typeof arguments[1] === 'string'. A non-string second argument is treated as the headers object, so arguments[2] is never read. jshttp/on-headers#49 is still open; #254 stays broken until that ships or compression compensates.

Fix

Wrap res.writeHead outside on-headers and rewrite the 3-argument form the same way Node does (obj ??= reason):

String status messages, 1- and 2-argument calls, and the empty-string reason phrase are unchanged.

Tests

Notes

Earlier compression PRs for this issue (#273, #274, #282) were closed in favor of on-headers#49. This change is a small, local workaround so #254 can close without waiting on that release, and it does not regress the 3-arg headers-in-the-second-slot overload.

on-headers treats a non-string 2nd writeHead argument as the headers
object, so writeHead(code, undefined, headers) dropped the headers.
Normalize that 3-arg form the same way Node does (obj ??= reason).

Fixes expressjs#254

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
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

2 participants