Skip to content

fix(bun): report the requested subprotocol on WSContext.protocol#5059

Open
greymoth-jp wants to merge 1 commit into
honojs:mainfrom
greymoth-jp:fix/bun-websocket-protocol
Open

fix(bun): report the requested subprotocol on WSContext.protocol#5059
greymoth-jp wants to merge 1 commit into
honojs:mainfrom
greymoth-jp:fix/bun-websocket-protocol

Conversation

@greymoth-jp

Copy link
Copy Markdown

The Bun WebSocket adapter sets WSContext.protocol to the request URL:

protocol: c.req.url,

WSContext.protocol is supposed to mirror the standard WebSocket.protocol property (the connection's subprotocol), so on Bun it currently returns something like http://localhost/ws instead of the subprotocol. The deno and cloudflare adapters read it from the platform socket (socket.protocol and server.protocol), so this is a Bun-only inconsistency.

This reads the first value of the Sec-WebSocket-Protocol header, the same way the deno adapter does (#4955), and stores it on the data that backs WSContext.protocol.

Added a test that upgrades with Sec-WebSocket-Protocol: chat, superchat and asserts wsContext.protocol is chat. Without this change it fails with expected 'http://localhost/ws' to be 'chat'.

  • Add tests
  • Run tests
  • Format with Prettier

The Bun adapter put the request URL in WSContext.protocol. Read the first
value of the Sec-WebSocket-Protocol header instead, so it reports the
subprotocol like the deno and cloudflare adapters do.
@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.11%. Comparing base (97c6fe1) to head (793e82d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5059      +/-   ##
==========================================
+ Coverage   79.09%   79.11%   +0.02%     
==========================================
  Files         154      154              
  Lines       10727    10726       -1     
  Branches     2238     2239       +1     
==========================================
+ Hits         8484     8486       +2     
+ Misses       2243     2240       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a test to test that the value of ws.protocol will be '' if you do not set the sec-websocket-protocol header?

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