11import * as http from 'http'
22import * as process from 'process'
33// eslint-disable-next-line @typescript-eslint/no-var-requires
4- const WebSocket = require ( 'ws' ) // ws works only with requrie
4+ const WebSocket = require ( 'ws' )
55
66import { DEFAULT_PORT , SECURITY_TOKENS } from '@shared/constants/websocket'
77import { Website } from '@shared/types/websocket-message'
@@ -70,7 +70,6 @@ class WebSocketServer {
7070 }
7171
7272 private _handle_connection ( ws : any , request : any ) : void {
73- // Verify security token
7473 const url = new URL ( request . url || '' , `http://localhost:${ DEFAULT_PORT } ` )
7574 const token = url . searchParams . get ( 'token' )
7675
@@ -79,7 +78,6 @@ class WebSocketServer {
7978 return
8079 }
8180
82- // Track if this is a browser connection
8381 const is_browser_client = token == SECURITY_TOKENS . BROWSERS
8482
8583 if ( is_browser_client ) {
@@ -90,18 +88,13 @@ class WebSocketServer {
9088
9189 this . connections . add ( ws )
9290
93- // Handle messages from clients
9491 ws . on ( 'message' , ( message : any ) => this . _handle_message ( message ) )
95-
96- // Handle client disconnection
9792 ws . on ( 'close' , ( ) => this . _handle_disconnection ( ws , is_browser_client ) )
9893 }
9994
10095 private _handle_browser_connection ( ws : WebSocket , url : URL ) : void {
101- // Extract version from URL parameters
10296 const version = url . searchParams . get ( 'version' ) || 'unknown'
10397
104- // Check if there is already a connected browser client
10598 if (
10699 this . current_browser_client &&
107100 this . current_browser_client . ws . readyState == WebSocket . OPEN
@@ -110,9 +103,8 @@ class WebSocketServer {
110103 return
111104 }
112105
113- // Store the new browser client
114106 this . current_browser_client = { ws, version }
115- this . _notify_vscode_clients ( ) // Notify when a browser connects
107+ this . _notify_vscode_clients ( )
116108 }
117109
118110 private _handle_vscode_connection ( ws : WebSocket , url : URL ) : void {
@@ -140,26 +132,22 @@ class WebSocketServer {
140132 const client_id = this . _generate_client_id ( )
141133 this . vscode_clients . set ( client_id , { ws, client_id } )
142134
143- // Send the client ID to the VS Code client
144135 ws . send (
145136 JSON . stringify ( {
146137 action : 'client-id-assignment' ,
147138 client_id
148139 } )
149140 )
150141
151- // Send initial status to new VS Code client
152142 ws . send (
153143 JSON . stringify ( {
154144 action : 'browser-connection-status' ,
155145 has_connected_browsers : this . current_browser_client !== null
156146 } )
157147 )
158148
159- // Send saved websites to new VS Code client
160149 this . _send_saved_websites_to_client ( ws )
161150
162- // Notify browser when a new VSCode client connects (if browser is connected)
163151 if (
164152 this . current_browser_client &&
165153 this . current_browser_client . ws . readyState === WebSocket . OPEN
@@ -209,15 +197,12 @@ class WebSocketServer {
209197 }
210198 this . current_browser_client . ws . send ( JSON . stringify ( legacy_message ) )
211199 } else {
212- // Forward the message as-is for newer browser clients
213200 this . current_browser_client . ws . send ( msg_string )
214201 }
215202 }
216203 } else if ( msg_data . action == 'update-saved-websites' ) {
217- // Store the updated websites
218204 this . saved_websites = msg_data . websites
219205
220- // Forward to VS Code clients
221206 for ( const client of this . vscode_clients . values ( ) ) {
222207 if ( client . ws . readyState == WebSocket . OPEN ) {
223208 client . ws . send ( msg_string )
@@ -227,7 +212,6 @@ class WebSocketServer {
227212 msg_data . action == 'invoke-fast-replace' || // <-- remove few weeks after 19 Apr 2025
228213 msg_data . action == 'apply-chat-response'
229214 ) {
230- // Forward the message to the specific VS Code client based on client_id
231215 const target_client_id = msg_data . client_id
232216 const target_client = this . vscode_clients . get ( target_client_id )
233217 if ( target_client && target_client . ws . readyState == WebSocket . OPEN ) {
@@ -252,9 +236,8 @@ class WebSocketServer {
252236 this . current_browser_client . ws === ws
253237 ) {
254238 this . current_browser_client = null
255- this . _notify_vscode_clients ( ) // Notify when the browser disconnects
239+ this . _notify_vscode_clients ( )
256240 } else {
257- // Find and remove the disconnected VS Code client
258241 let disconnected_client_id : number | null = null
259242 for ( const [ client_id , client ] of this . vscode_clients . entries ( ) ) {
260243 if ( client . ws === ws ) {
@@ -264,7 +247,6 @@ class WebSocketServer {
264247 }
265248 }
266249
267- // Notify browser when a VSCode client disconnects (if browser is connected)
268250 if (
269251 disconnected_client_id !== null &&
270252 this . current_browser_client &&
@@ -359,7 +341,7 @@ class WebSocketServer {
359341 if ( p1 > p2 ) return true
360342 if ( p1 < p2 ) return false
361343 }
362- return false // Versions are equal or v1 is not newer
344+ return false
363345 }
364346
365347 private _is_version_lower_than (
@@ -377,7 +359,7 @@ class WebSocketServer {
377359 if ( p1 < p2 ) return true
378360 if ( p1 > p2 ) return false
379361 }
380- return false // Versions are equal
362+ return false
381363 }
382364}
383365
0 commit comments