@@ -12,7 +12,7 @@ export function setupWebSocket(server: Server) {
1212 logger . debug ( `Received upgrade request for URL: ${ req . url } ` ) ;
1313 const baseURL = `http://${ req . headers . host } /` ;
1414 const requestURL = new URL ( req . url || '' , baseURL ) ;
15- const { pathname} = requestURL ;
15+ const { pathname } = requestURL ;
1616 logger . debug ( `Parsed pathname: ${ pathname } ` ) ;
1717
1818 // Debug log to verify path handling
@@ -38,7 +38,7 @@ export function setupWebSocket(server: Server) {
3838 wss . on ( 'connection' , ( ws : WebSocket , req ) => {
3939 const baseURL = `http://${ req . headers . host } /` ;
4040 const requestURL = new URL ( req . url || '' , baseURL ) ;
41- const { pathname} = requestURL ;
41+ const { pathname } = requestURL ;
4242
4343 logger . info ( `WebSocket connection established to ${ pathname } ` ) ;
4444
@@ -83,27 +83,22 @@ export function setupWebSocket(server: Server) {
8383 ws . send ( JSON . stringify ( { type : 'log-history' , data : history } ) ) ;
8484
8585 // Watch the log file for changes
86- const watcher = fs . watch ( logPath , ( eventType ) => {
87- if ( eventType === 'change' ) {
88- const newSize = fs . statSync ( logPath ) . size ;
89- if ( newSize > lastSize ) {
90- const stream = fs . createReadStream ( logPath , {
91- start : lastSize ,
92- end : newSize - 1 ,
93- encoding : 'utf-8'
94- } ) ;
95-
96- stream . on ( 'data' , ( chunk ) => {
97- ws . send ( JSON . stringify ( { type : 'log-update' , data : chunk } ) ) ;
98- } ) ;
99-
100- lastSize = newSize ;
101- }
86+ const watcher = fs . watchFile ( logPath , { interval : 1000 } , ( curr , prev ) => {
87+ if ( curr . size > prev . size ) {
88+ const stream = fs . createReadStream ( logPath , {
89+ start : prev . size ,
90+ end : curr . size - 1 ,
91+ encoding : 'utf-8'
92+ } ) ;
93+
94+ stream . on ( 'data' , ( chunk ) => {
95+ ws . send ( JSON . stringify ( { type : 'log-update' , data : chunk } ) ) ;
96+ } ) ;
10297 }
10398 } ) ;
10499
105100 ws . on ( 'close' , ( ) => {
106- watcher . close ( ) ;
101+ watcher . removeAllListeners ( ) ;
107102 logger . info ( 'Closed WebSocket connection for logs' ) ;
108103 } ) ;
109104 } else {
0 commit comments