diff --git a/.changeset/deep-rivers-attend.md b/.changeset/deep-rivers-attend.md new file mode 100644 index 00000000..88f4e9ee --- /dev/null +++ b/.changeset/deep-rivers-attend.md @@ -0,0 +1,5 @@ +--- +'@hono/node-ws': patch +--- + +Fix missing code and reason on `CloseEvent` diff --git a/packages/node-ws/src/index.test.ts b/packages/node-ws/src/index.test.ts index e27792d6..c7d6d212 100644 --- a/packages/node-ws/src/index.test.ts +++ b/packages/node-ws/src/index.test.ts @@ -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((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 () => { diff --git a/packages/node-ws/src/index.ts b/packages/node-ws/src/index.ts index e86b432a..60aa14f0 100644 --- a/packages/node-ws/src/index.ts +++ b/packages/node-ws/src/index.ts @@ -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?.(