Skip to content

hattip-ws/uwebsockets does not forward Hattip context to connected peers #1

Description

@aleclarson

I ran out of time to implement this, but if anyone's interested in contributing, take a look at how it's done in the hattip-ws/node adapter. The difficulty is that UWS (like Node) doesn't handle upgrade requests in a normal request handler, instead opting for a special, out-of-line handler. Also, unlike with Deno/Bun/Cloudflare, crossws wraps the UWS/Node request in a "proxy" object (presumedly for normalization), so the call to forwardHattipContext is less straight-forward.

Here's the hattip-ws/node implementation:

export function createWebSocketAdapter(
options?: WebSocketAdapterOptions
): WebSocketAdapter {
const upgradeRequests = new WeakMap<http.IncomingMessage, Buffer>()
let upgradedContext: RequestContext | undefined
const { handleUpgrade, ...adapter } = crossws(
interceptUpgrade(options, request => {
forwardHattipContext(request, upgradedContext!)
upgradedContext = undefined
}) as any
)
return {
...(adapter as WebSocketAdapter),
handler: context => {
const { request } = context.platform
const head = upgradeRequests.get(request)
if (head) {
upgradeRequests.delete(request)
upgradedContext = context
return handleUpgrade(request, request.socket, head)
}
return context.next()
},
configureServer(server) {
server.on('upgrade', (request, _socket, head) => {
if (request.headers.upgrade === 'websocket') {
upgradeRequests.set(request, head)
// Forward the upgrade request to the Hattip handler.
const response = new http.ServerResponse(request)
server.emit('request', request, response)
}
})
},
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions