Expose kResponseHeaders via Symbol.for for caller-provided upgrade headers - #5534
Open
MsfPablo wants to merge 1 commit into
Open
Expose kResponseHeaders via Symbol.for for caller-provided upgrade headers#5534MsfPablo wants to merge 1 commit into
MsfPablo wants to merge 1 commit into
Conversation
When user code calls Server#handleUpgrade from its own 'upgrade' listener
(such as the README example (C) with a manual http.Server), the underlying
WebSocketResponse would overwrite any response headers previously attached
to the request with an empty object, making it impossible to set CORS (or
any other) headers on the upgrade response without using the .cors option.
Switch kResponseHeaders to Symbol.for("engine.io:responseHeaders") so it
is reachable from user code, and merge any pre-existing header bag into
the WebSocketResponse's initial headers instead of starting from {}.
Closes socketio#5497
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5497 by allowing user code that calls
Server#handleUpgradedirectly (e.g. the README example (C) with a custom HTTP server'supgradelistener) to attach additional headers to the upgrade response, such as CORS headers when thecorsoption is not used.Problem
kResponseHeaderswas a privateSymbol(...)allocated at module-init. Because the symbol is local to each import ofengine.io, user code could not reference it from outside the module to set extra upgrade-response headers. In practice this forced callers to either (a) set thecorsoption on theServer— which is incompatible with WebTransport — or (b) drop directhandleUpgradecalls in favour ofserver.attach(httpServer), which is not always an option for projects that share the same HTTP server with other routers.Even if the symbol were reachable, the inner
WebSocketResponseclass previously wiped any pre-existingreq[kResponseHeaders]withreq[kResponseHeaders] = {}at construction time, so caller-supplied headers would still be lost.Fix
kResponseHeaderstoSymbol.for("engine.io:responseHeaders")so user code can reference the well-known slot. The symbol is exported for direct introspection.WebSocketResponse's constructor, merge any pre-existingreq[kResponseHeaders]into the initial header bag rather than overwriting it.Backwards compatibility: code that monkey-patches via the old private symbol will see
undefinedgoing forward (instead of being silently read). Code already usingSymbol.for("engine.io:responseHeaders")will collide by design — that key was undocumented and not used by the project itself.Example
Tests
describe('headers')that attaches two extra headers via the shared symbol, opens a ws handshake, and asserts both are emitted through'headers'."headers"test suite continues to pass unchanged.npm run format:checkis green; build/compile is green.🤖 Generated with Claude Code