fix(node-ws):missing code and reason on CloseEvent (#1138)

fixes #1012
pull/1139/head
Leia 2025-04-26 01:09:26 -03:00 committed by GitHub
parent abb260632f
commit 237bff1b82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/node-ws': patch
---
Fix missing code and reason on `CloseEvent`

View File

@ -170,18 +170,21 @@ describe('WebSocket helper', () => {
})
it('CloseEvent should be executed without crash', async () => {
const testCode = 3001
const testReason = 'Test!'
app.get(
'/',
upgradeWebSocket(() => ({
onClose() {
// doing some stuff here
onClose(event) {
expect(event.code).toBe(testCode)
expect(event.reason).toBe(testReason)
},
}))
)
const ws = new WebSocket('ws://localhost:3030/')
await new Promise<void>((resolve) => ws.on('open', resolve))
ws.close()
ws.close(testCode, testReason)
})
it('Should be able to send and receive binary content with good length', async () => {

View File

@ -126,8 +126,8 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
)
}
})
ws.on('close', () => {
events.onClose?.(new CloseEvent('close'), ctx)
ws.on('close', (code, reason) => {
events.onClose?.(new CloseEvent('close', { code, reason: reason.toString() }), ctx)
})
ws.on('error', (error) => {
events.onError?.(