File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @hono/node-ws ' : patch
3+ ---
4+
5+ Add a ` CloseEvent ` class to avoid exception "CloseEvent is not defined"
Original file line number Diff line number Diff line change 1+ interface CloseEventInit extends EventInit {
2+ code ?: number ;
3+ reason ?: string ;
4+ wasClean ?: boolean ;
5+ }
6+
7+ /**
8+ * @link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
9+ */
10+ export const CloseEvent = globalThis . CloseEvent ?? class extends Event {
11+ #eventInitDict
12+
13+ constructor (
14+ type : string ,
15+ eventInitDict : CloseEventInit = { }
16+ ) {
17+ super ( type , eventInitDict )
18+ this . #eventInitDict = eventInitDict
19+ }
20+
21+ get wasClean ( ) : boolean {
22+ return this . #eventInitDict. wasClean ?? false
23+ }
24+
25+ get code ( ) : number {
26+ return this . #eventInitDict. code ?? 0
27+ }
28+
29+ get reason ( ) : string {
30+ return this . #eventInitDict. reason ?? ''
31+ }
32+ }
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ describe('WebSocket helper', () => {
1313
1414 beforeEach ( async ( ) => {
1515 app = new Hono ( )
16+
1617 ; ( { injectWebSocket, upgradeWebSocket } = createNodeWebSocket ( { app } ) )
1718
1819 server = await new Promise < ServerType > ( ( resolve ) => {
@@ -108,6 +109,21 @@ describe('WebSocket helper', () => {
108109 connections . forEach ( ( ws ) => ws . close ( ) )
109110 } )
110111
112+ it ( 'CloseEvent should be executed without crash' , async ( ) => {
113+ app . get (
114+ '/' ,
115+ upgradeWebSocket ( ( ) => ( {
116+ onClose ( ) {
117+ // doing some stuff here
118+ } ,
119+ } ) )
120+ )
121+
122+ const ws = new WebSocket ( 'ws://localhost:3030/' )
123+ await new Promise < void > ( ( resolve ) => ws . on ( 'open' , resolve ) )
124+ ws . close ( )
125+ } )
126+
111127 it ( 'Should be able to send and receive binary content with good length' , async ( ) => {
112128 const mainPromise = new Promise < WSMessageReceive > ( ( resolve ) =>
113129 app . get (
@@ -126,7 +142,7 @@ describe('WebSocket helper', () => {
126142 await new Promise < void > ( ( resolve ) => ws . on ( 'open' , resolve ) )
127143 ws . send ( binaryData )
128144
129- const receivedMessage = await mainPromise ;
145+ const receivedMessage = await mainPromise
130146 expect ( receivedMessage ) . toBeInstanceOf ( Buffer )
131147 expect ( ( receivedMessage as Buffer ) . byteLength ) . toBe ( binaryData . length )
132148
Original file line number Diff line number Diff line change 1- import { Buffer } from 'buffer'
21import type { Server } from 'node:http'
32import type { Http2SecureServer , Http2Server } from 'node:http2'
43import type { Hono } from 'hono'
54import type { UpgradeWebSocket , WSContext } from 'hono/ws'
65import type { WebSocket } from 'ws'
76import { WebSocketServer } from 'ws'
87import type { IncomingMessage } from 'http'
8+ import { CloseEvent } from './events'
99
1010export interface NodeWebSocket {
1111 upgradeWebSocket : UpgradeWebSocket
You can’t perform that action at this time.
0 commit comments