@@ -2,30 +2,30 @@ import { it } from 'bun:test'
22import * as assert from 'node:assert'
33import { once } from 'node:events'
44import { App , type Request } from '@tinyhttp/app'
5- import { type Server , type ServerOptions , WebSocketServer } from 'ws'
5+ import { type Server , type ServerOptions , WebSocket , WebSocketServer } from 'ws'
66import { type TinyWSRequest , tinyws } from '../src/index'
77
88const s = ( handler : ( req : TinyWSRequest ) => void , opts ?: ServerOptions , inst ?: Server ) => {
99 const app = new App < Request & TinyWSRequest > ( )
1010
11- app . use ( tinyws ( opts , inst ) )
1211 app . use ( '/ws' , async ( req ) => {
1312 if ( typeof req . ws !== 'undefined' ) {
1413 handler ( req )
1514 }
1615 } )
1716
18- return app
17+ return { app, opts , inst }
1918}
2019
2120it ( 'should respond with a message' , async ( ) => {
22- const app = s ( async ( req ) => {
21+ const { app } = s ( async ( req ) => {
2322 const ws = await req ?. ws ( )
2423
2524 return ws . send ( 'hello there' )
2625 } )
2726
2827 const server = app . listen ( 4443 , undefined , 'localhost' )
28+ tinyws ( app , server )
2929
3030 const ws = new WebSocket ( 'ws://localhost:4443/ws' )
3131
@@ -37,15 +37,16 @@ it('should respond with a message', async () => {
3737} )
3838
3939it ( 'should resolve a `.ws` property' , async ( ) => {
40- const app = s ( async ( req ) => {
40+ const { app } = s ( async ( req ) => {
4141 const ws = await req . ws ( )
4242
43- assert . ok ( ws instanceof WebSocket )
43+ assert . ok ( typeof ws . send === 'function' )
4444
4545 return ws . send ( 'hello there' )
4646 } )
4747
4848 const server = app . listen ( 4444 , undefined , 'localhost' )
49+ tinyws ( app , server )
4950
5051 const ws = new WebSocket ( 'ws://localhost:4444/ws' )
5152
@@ -56,11 +57,11 @@ it('should resolve a `.ws` property', async () => {
5657} )
5758
5859it ( 'should pass ws options' , async ( ) => {
59- const app = s (
60+ const { app, opts } = s (
6061 async ( req ) => {
6162 const ws = await req . ws ( )
6263
63- assert . ok ( ws instanceof WebSocket )
64+ assert . ok ( typeof ws . send === 'function' )
6465
6566 ws . on ( 'error' , ( err ) => {
6667 assert . match ( err . message , / M a x p a y l o a d s i z e e x c e e d e d / )
@@ -74,6 +75,7 @@ it('should pass ws options', async () => {
7475 )
7576
7677 const server = app . listen ( 4445 , undefined , 'localhost' )
78+ tinyws ( app , server , opts )
7779
7880 const ws = new WebSocket ( 'ws://localhost:4445/ws' )
7981
@@ -86,15 +88,16 @@ it('should pass ws options', async () => {
8688} )
8789
8890it ( 'should accept messages' , async ( ) => {
89- const app = s ( async ( req ) => {
91+ const { app } = s ( async ( req ) => {
9092 const ws = await req . ws ( )
9193
92- assert . ok ( ws instanceof WebSocket )
94+ assert . ok ( typeof ws . send === 'function' )
9395
9496 return ws . on ( 'message' , ( msg ) => ws . send ( `You sent: ${ msg } ` ) )
9597 } )
9698
9799 const server = app . listen ( 4446 , undefined , 'localhost' )
100+ tinyws ( app , server )
98101
99102 const ws = new WebSocket ( 'ws://localhost:4446/ws' )
100103
@@ -114,14 +117,14 @@ it('supports passing a server instance', async () => {
114117 const wss = new WebSocketServer ( { noServer : true } )
115118
116119 wss . on ( 'connection' , ( socket ) => {
117- assert . ok ( socket instanceof WebSocket )
120+ assert . ok ( typeof socket . send === 'function' )
118121 } )
119122
120- const app = s (
123+ const { app, inst } = s (
121124 async ( req ) => {
122125 const ws = await req . ws ( )
123126
124- assert . ok ( ws instanceof WebSocket )
127+ assert . ok ( typeof ws . send === 'function' )
125128
126129 return ws . send ( 'hello there' )
127130 } ,
@@ -130,6 +133,7 @@ it('supports passing a server instance', async () => {
130133 )
131134
132135 const server = app . listen ( 4447 , undefined , 'localhost' )
136+ tinyws ( app , server , { } , inst )
133137
134138 const ws = new WebSocket ( 'ws://localhost:4447/ws' )
135139
@@ -139,6 +143,80 @@ it('supports passing a server instance', async () => {
139143 ws . close ( )
140144} )
141145
142- it ( 'returns a middleware function' , ( ) => {
143- assert . ok ( typeof tinyws ( ) === 'function' )
146+ it ( 'returns a WebSocketServer instance' , ( ) => {
147+ const app = new App ( )
148+ const server = app . listen ( 4448 , undefined , 'localhost' )
149+ const wss = tinyws ( app , server )
150+ assert . ok ( wss instanceof WebSocketServer )
151+ server . close ( )
152+ } )
153+
154+ it ( 'restricts WebSocket to specified paths' , async ( ) => {
155+ const app = new App < Request & TinyWSRequest > ( )
156+
157+ app . use ( '/ws' , async ( req , res ) => {
158+ if ( req . ws ) {
159+ const ws = await req . ws ( )
160+ return ws . send ( 'allowed' )
161+ }
162+ res . send ( 'no ws' )
163+ } )
164+
165+ app . use ( '/other' , async ( req , res ) => {
166+ if ( req . ws ) {
167+ const ws = await req . ws ( )
168+ return ws . send ( 'should not happen' )
169+ }
170+ res . send ( 'no ws on other' )
171+ } )
172+
173+ const server = app . listen ( 4449 , undefined , 'localhost' )
174+ tinyws ( app , server , { paths : '/ws' } )
175+
176+ // Connection to /ws should work
177+ const ws1 = new WebSocket ( 'ws://localhost:4449/ws' )
178+ const [ data ] = await once ( ws1 , 'message' )
179+ assert . equal ( data . toString ( ) , 'allowed' )
180+ ws1 . close ( )
181+
182+ // Connection to /other should not have req.ws
183+ const ws2 = new WebSocket ( 'ws://localhost:4449/other' )
184+ const [ err ] = await once ( ws2 , 'error' )
185+ assert . ok ( err )
186+ ws2 . close ( )
187+
188+ server . close ( )
189+ } )
190+
191+ it ( 'supports multiple paths' , async ( ) => {
192+ const app = new App < Request & TinyWSRequest > ( )
193+
194+ app . use ( '/ws1' , async ( req ) => {
195+ if ( req . ws ) {
196+ const ws = await req . ws ( )
197+ return ws . send ( 'ws1' )
198+ }
199+ } )
200+
201+ app . use ( '/ws2' , async ( req ) => {
202+ if ( req . ws ) {
203+ const ws = await req . ws ( )
204+ return ws . send ( 'ws2' )
205+ }
206+ } )
207+
208+ const server = app . listen ( 4450 , undefined , 'localhost' )
209+ tinyws ( app , server , { paths : [ '/ws1' , '/ws2' ] } )
210+
211+ const ws1 = new WebSocket ( 'ws://localhost:4450/ws1' )
212+ const [ data1 ] = await once ( ws1 , 'message' )
213+ assert . equal ( data1 . toString ( ) , 'ws1' )
214+ ws1 . close ( )
215+
216+ const ws2 = new WebSocket ( 'ws://localhost:4450/ws2' )
217+ const [ data2 ] = await once ( ws2 , 'message' )
218+ assert . equal ( data2 . toString ( ) , 'ws2' )
219+ ws2 . close ( )
220+
221+ server . close ( )
144222} )
0 commit comments