You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{Hono}from'hono';import{createBunWebSocket}from'hono/bun';import{hc}from'hono/client';const{ upgradeWebSocket, websocket }=createBunWebSocket();constapp=newHono().use('*',async(c,next)=>{console.log({Authorization: c.req.header('Authorization'),// undefined});awaitnext();}).get('/ws',upgradeWebSocket(()=>({onMessage(event,ws){console.log(`Message from client: ${event.data}`);ws.send('Hello from server!');},onClose: ()=>{console.log('Connection closed');},})),);constserver=Bun.serve({fetch: app.fetch,
websocket,});constclient=hc<typeofapp>(server.url.href,{headers: {Authorization: 'Bearer 123',},});constsocket=client.ws.$ws();awaitnewPromise((resolve)=>setTimeout(resolve,500));socket.close();server.stop();
What is the expected behavior?
Based on the example above: c.req.header('Authorization') should return 'Bearer 123'.
What do you see instead?
Based on the example above: c.req.header('Authorization') instead returns undefined.
Additional information
I of course did a quick look into this and this line seems to be the cause. We just need to pass args as a 2nd argument when creating the new websocket instance.
At a basic level the const args = ... could just be moved up & used for both req.fetch(...) and new WebSocket(...) but I don't know if there is any issue with exposing all arguments when creating the new websocket instance, or if it should be limited to just headers?
The text was updated successfully, but these errors were encountered:
Currently, WebSocket API in JavaScript doesn't support sending headers. WebSocket constructor supports only URL and protocol.
RPC Client uses WebSocket API, so I think we can't resolve it. However writing about it to docs is good idea.
Interesting, good point @nakasyou. After a bit more digging I found this issue created 7 years asking for this to become a standard. However Bun seems to have gone ahead and added support for it anyway.
What version of Hono are you using?
4.4.3
What runtime/platform is your app running on?
Bun
What steps can reproduce the bug?
What is the expected behavior?
Based on the example above:
c.req.header('Authorization')
should return'Bearer 123'
.What do you see instead?
Based on the example above:
c.req.header('Authorization')
instead returnsundefined
.Additional information
I of course did a quick look into this and this line seems to be the cause. We just need to pass
args
as a 2nd argument when creating the new websocket instance.At a basic level the
const args = ...
could just be moved up & used for bothreq.fetch(...)
andnew WebSocket(...)
but I don't know if there is any issue with exposing all arguments when creating the new websocket instance, or if it should be limited to just headers?The text was updated successfully, but these errors were encountered: