Compare commits

...

2 Commits

Author SHA1 Message Date
Andrei 2722b4e864
Merge 13d07079df into 237bff1b82 2025-04-26 13:12:01 +09:00
Leia 237bff1b82
fix(node-ws):missing code and reason on CloseEvent (#1138)
fixes #1012
2025-04-26 13:09:26 +09:00
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?.(