Skip to content

Commit

Permalink
fix(node-ws): create only one WebSocketServer instead of per websocke…
Browse files Browse the repository at this point in the history
…t request (#539)
  • Loading branch information
mikestopcontinues authored May 26, 2024
1 parent e779454 commit ec6ec4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-lions-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/node-ws': patch
---

create only one WebSocketServer instead of per websocket request
2 changes: 1 addition & 1 deletion packages/node-ws/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('WebSocket helper', () => {
)
})
injectWebSocket(server)
const ws = new WebSocket('ws://localhost:3030/')
new WebSocket('ws://localhost:3030/')

expect(await mainPromise).toBe(true)
})
Expand Down
33 changes: 9 additions & 24 deletions packages/node-ws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from 'buffer'
import type { Server } from 'node:http'
import type { Http2SecureServer, Http2Server } from 'node:http2'
import type { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'
import type { UpgradeWebSocket, WSContext } from 'hono/ws'
import { WebSocketServer } from 'ws'

Expand All @@ -15,27 +14,17 @@ export interface NodeWebSocketInit {
baseUrl?: string | URL
}

/**
* Extended for telling WebSocket
* @internal
*/
class WSResponse extends Response {
readonly wss: WebSocketServer
constructor(wss: WebSocketServer) {
super()
this.wss = wss
}
}

/**
* Create WebSockets for Node.js
* @param init Options
* @returns NodeWebSocket
*/
export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
const wss = new WebSocketServer({noServer: true})

return {
injectWebSocket(server) {
;(server as Server).on('upgrade', async (request, socket, head) => {
server.on('upgrade', async (request, socket, head) => {
const url = new URL(request.url ?? '/', init.baseUrl ?? 'http://localhost')
const headers = new Headers()
for (const key in request.headers) {
Expand All @@ -45,15 +34,11 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
}
headers.append(key, Array.isArray(value) ? value[0] : value)
}
const res = (await init.app.request(url, {
await init.app.request(url, {
headers: headers,
})) as Response | WSResponse
if (!(res instanceof WSResponse)) {
socket.destroy()
return
}
res.wss.handleUpgrade(request, socket, head, (ws) => {
res.wss.emit('connection', ws, request)
})
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)
})
})
},
Expand All @@ -64,7 +49,6 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
await next()
return
}
const wss = new WebSocketServer({ noServer: true })
const events = await createEvents(c)
wss.on('connection', (ws) => {
const ctx: WSContext = {
Expand Down Expand Up @@ -109,7 +93,8 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
)
})
})
return new WSResponse(wss)

return new Response()
},
}
}

0 comments on commit ec6ec4e

Please sign in to comment.