@@ -46,7 +46,6 @@ export class SSEServerTransport extends AbstractTransport {
46
46
}
47
47
48
48
private getCorsHeaders ( includeMaxAge : boolean = false ) : Record < string , string > {
49
- // Ensure all CORS properties are present by merging with defaults
50
49
const corsConfig = {
51
50
allowOrigin : DEFAULT_CORS_CONFIG . allowOrigin ,
52
51
allowMethods : DEFAULT_CORS_CONFIG . allowMethods ,
@@ -125,13 +124,13 @@ export class SSEServerTransport extends AbstractTransport {
125
124
if ( this . _sseResponse ?. writableEnded ) {
126
125
this . _sseResponse = undefined
127
126
}
128
-
127
+
129
128
if ( this . _sseResponse ) {
130
- logger . warn ( "SSE connection already established" )
131
- res . writeHead ( 409 ) . end ( "SSE connection already established" )
132
- return
129
+ logger . warn ( "SSE connection already established; closing the old connection to allow a new one. " )
130
+ this . _sseResponse . end ( )
131
+ this . cleanupConnection ( )
133
132
}
134
-
133
+
135
134
this . setupSSEConnection ( res )
136
135
return
137
136
}
@@ -204,48 +203,48 @@ export class SSEServerTransport extends AbstractTransport {
204
203
}
205
204
206
205
private setupSSEConnection ( res : ServerResponse ) : void {
207
- logger . debug ( `Setting up SSE connection for session: ${ this . _sessionId } ` ) ;
206
+ logger . debug ( `Setting up SSE connection for session: ${ this . _sessionId } ` )
208
207
209
208
const headers = {
210
209
...SSE_HEADERS ,
211
210
...this . getCorsHeaders ( ) ,
212
211
...this . _config . headers
213
212
}
214
213
setResponseHeaders ( res , headers )
215
- logger . debug ( `SSE headers set: ${ JSON . stringify ( headers ) } ` ) ;
214
+ logger . debug ( `SSE headers set: ${ JSON . stringify ( headers ) } ` )
216
215
217
216
if ( res . socket ) {
218
- res . socket . setNoDelay ( true )
219
- res . socket . setTimeout ( 0 )
220
- res . socket . setKeepAlive ( true , 1000 )
221
- logger . debug ( 'Socket optimized for SSE connection' ) ;
217
+ res . socket . setNoDelay ( true )
218
+ res . socket . setTimeout ( 0 )
219
+ res . socket . setKeepAlive ( true , 1000 )
220
+ logger . debug ( 'Socket optimized for SSE connection' )
222
221
}
223
222
224
- const endpointUrl = `${ this . _config . messageEndpoint } ?sessionId=${ this . _sessionId } ` ;
225
- logger . debug ( `Sending endpoint URL: ${ endpointUrl } ` ) ;
226
- res . write ( `event: endpoint\ndata: ${ endpointUrl } \n\n` ) ;
223
+ const endpointUrl = `${ this . _config . messageEndpoint } ?sessionId=${ this . _sessionId } `
224
+ logger . debug ( `Sending endpoint URL: ${ endpointUrl } ` )
225
+ res . write ( `event: endpoint\ndata: ${ endpointUrl } \n\n` )
227
226
228
- logger . debug ( 'Sending initial keep-alive' ) ;
229
- res . write ( ": keep-alive\n\n" ) ;
227
+ logger . debug ( 'Sending initial keep-alive' )
228
+ res . write ( ": keep-alive\n\n" )
230
229
231
230
this . _keepAliveInterval = setInterval ( ( ) => {
232
231
if ( this . _sseResponse && ! this . _sseResponse . writableEnded ) {
233
232
try {
234
- logger . debug ( 'Sending keep-alive ping' ) ;
235
- this . _sseResponse . write ( ": keep-alive\n\n" ) ;
233
+ logger . debug ( 'Sending keep-alive ping' )
234
+ this . _sseResponse . write ( ": keep-alive\n\n" )
236
235
237
236
const pingMessage = {
238
237
jsonrpc : "2.0" ,
239
238
method : "ping" ,
240
239
params : { timestamp : Date . now ( ) }
241
- } ;
242
- this . _sseResponse . write ( `data: ${ JSON . stringify ( pingMessage ) } \n\n` ) ;
240
+ }
241
+ this . _sseResponse . write ( `data: ${ JSON . stringify ( pingMessage ) } \n\n` )
243
242
} catch ( error ) {
244
- logger . error ( `Error sending keep-alive: ${ error } ` ) ;
245
- this . cleanupConnection ( ) ;
243
+ logger . error ( `Error sending keep-alive: ${ error } ` )
244
+ this . cleanupConnection ( )
246
245
}
247
246
}
248
- } , 15000 )
247
+ } , 15000 )
249
248
250
249
this . _sseResponse = res
251
250
@@ -294,26 +293,26 @@ export class SSEServerTransport extends AbstractTransport {
294
293
return parsed
295
294
} ) ( )
296
295
297
- const { id, method, params } = rawMessage as any ;
298
- logger . debug ( `Parsed message - ID: ${ id } , Method: ${ method } ` ) ;
296
+ const { id, method, params } = rawMessage as any
297
+ logger . debug ( `Parsed message - ID: ${ id } , Method: ${ method } ` )
299
298
300
299
const rpcMessage : JSONRPCMessage = {
301
300
jsonrpc : "2.0" ,
302
301
id : id ,
303
302
method : method ,
304
303
params : params
305
- } ;
304
+ }
306
305
307
306
currentMessage = {
308
307
id : id ,
309
308
method : method
310
- } ;
309
+ }
311
310
312
311
logger . debug ( `Processing RPC message: ${ JSON . stringify ( {
313
312
id : id ,
314
313
method : method ,
315
314
params : params
316
- } ) } `) ;
315
+ } ) } `)
317
316
318
317
if ( ! this . _onmessage ) {
319
318
throw new Error ( "No message handler registered" )
@@ -382,9 +381,6 @@ export class SSEServerTransport extends AbstractTransport {
382
381
} )
383
382
}
384
383
385
- /**
386
- * Clean up SSE connection resources
387
- */
388
384
private cleanupConnection ( ) : void {
389
385
if ( this . _keepAliveInterval ) {
390
386
clearInterval ( this . _keepAliveInterval )
0 commit comments