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) |
|
} |
|
}) |
|
}, |
|
} |
|
} |
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/nodeadapter. 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,crosswswraps the UWS/Node request in a "proxy" object (presumedly for normalization), so the call toforwardHattipContextis less straight-forward.Here's the
hattip-ws/nodeimplementation:hattip-ws/src/adapters/node/node.ts
Lines 41 to 78 in ef25ef7