@@ -82,6 +82,7 @@ export class HttpStreamTransport extends AbstractTransport {
82
82
cors : { ...DEFAULT_CORS_CONFIG , ...( config . cors || { } ) } as Required < NonNullable < HttpStreamTransportConfig [ 'cors' ] > > ,
83
83
auth : config . auth ?? DEFAULT_HTTP_STREAM_CONFIG . auth ,
84
84
headers : config . headers ?? DEFAULT_HTTP_STREAM_CONFIG . headers ,
85
+ enableGetSse : config . enableGetSse ?? DEFAULT_HTTP_STREAM_CONFIG . enableGetSse ,
85
86
} ;
86
87
87
88
if ( this . _config . auth ?. endpoints ) {
@@ -95,6 +96,7 @@ export class HttpStreamTransport extends AbstractTransport {
95
96
sessionEnabled : this . _config . session . enabled ,
96
97
resumabilityEnabled : this . _config . resumability . enabled ,
97
98
resumabilityStore : this . _config . resumability . messageStoreType ,
99
+ enableGetSse : this . _config . enableGetSse ,
98
100
authEnabled : ! ! this . _config . auth ,
99
101
corsOrigin : this . _config . cors . allowOrigin
100
102
} , null , 2 ) } `) ;
@@ -108,7 +110,9 @@ export class HttpStreamTransport extends AbstractTransport {
108
110
const corsConfig = this . _config . cors ;
109
111
const headers : Record < string , string > = {
110
112
"Access-Control-Allow-Origin" : corsConfig . allowOrigin || req . headers . origin || '*' ,
111
- "Access-Control-Allow-Methods" : corsConfig . allowMethods ,
113
+ "Access-Control-Allow-Methods" : this . _config . enableGetSse ?
114
+ corsConfig . allowMethods :
115
+ corsConfig . allowMethods . replace ( / G E T , ? \s * / , '' ) ,
112
116
"Access-Control-Allow-Headers" : corsConfig . allowHeaders ,
113
117
"Access-Control-Expose-Headers" : [ corsConfig . exposeHeaders , this . _config . session . enabled ? this . _config . session . headerName : null ] . filter ( Boolean ) . join ( ', ' ) ,
114
118
"Access-Control-Allow-Credentials" : "true" ,
@@ -181,8 +185,13 @@ export class HttpStreamTransport extends AbstractTransport {
181
185
case "GET" : await this . handleGet ( req , res ) ; break ;
182
186
case "DELETE" : await this . handleDelete ( req , res ) ; break ;
183
187
default :
184
- res . writeHead ( 405 , { 'Content-Type' : 'text/plain' , 'Allow' : 'GET, POST, DELETE, OPTIONS' } ) ; res . end ( "Method Not Allowed" ) ;
185
- logger . warn ( `Unsupported method: ${ req . method } ` ) ; break ;
188
+ const allowHeader = this . _config . enableGetSse ?
189
+ 'GET, POST, DELETE, OPTIONS' :
190
+ 'POST, DELETE, OPTIONS' ;
191
+ res . writeHead ( 405 , { 'Content-Type' : 'text/plain' , 'Allow' : allowHeader } ) ;
192
+ res . end ( "Method Not Allowed" ) ;
193
+ logger . warn ( `Unsupported method: ${ req . method } ` ) ;
194
+ break ;
186
195
}
187
196
} catch ( error : any ) {
188
197
logger . error ( `Error processing ${ req . method } ${ url . pathname } : ${ error . message } ` ) ;
@@ -375,10 +384,25 @@ export class HttpStreamTransport extends AbstractTransport {
375
384
376
385
private async handleGet ( req : IncomingMessage , res : ServerResponse ) : Promise < void > {
377
386
logger . debug ( `Handling GET request to ${ this . _config . endpoint } ` ) ;
387
+
388
+ if ( ! this . _config . enableGetSse ) {
389
+ logger . debug ( `GET SSE is disabled. Returning 405 Method Not Allowed.` ) ;
390
+ res . writeHead ( 405 , {
391
+ 'Content-Type' : 'text/plain' ,
392
+ 'Allow' : 'POST, DELETE, OPTIONS'
393
+ } ) ;
394
+ res . end ( "Method Not Allowed: GET-based SSE is disabled on this server." ) ;
395
+ return ;
396
+ }
397
+
378
398
const acceptHeader = req . headers . accept || '' ;
379
- if ( ! acceptHeader . includes ( SSE_CONTENT_TYPE ) && ! acceptHeader . includes ( '*/*' ) ) throw this . httpError ( 406 , `Not Acceptable: GET requires Accept header including ${ SSE_CONTENT_TYPE } ` ) ;
399
+ if ( ! acceptHeader . includes ( SSE_CONTENT_TYPE ) && ! acceptHeader . includes ( '*/*' ) ) {
400
+ throw this . httpError ( 406 , `Not Acceptable: GET requires Accept header including ${ SSE_CONTENT_TYPE } ` ) ;
401
+ }
380
402
403
+ const lastEventId = getRequestHeader ( req . headers , "Last-Event-ID" ) ;
381
404
const sessionIdHeader = getRequestHeader ( req . headers , this . _config . session . headerName ) ;
405
+
382
406
let session : SessionData | undefined ;
383
407
384
408
if ( this . _config . session . enabled && sessionIdHeader ) {
@@ -393,7 +417,6 @@ export class HttpStreamTransport extends AbstractTransport {
393
417
await this . handleAuthentication ( req , res , `GET (sessions disabled)` , undefined ) ;
394
418
}
395
419
396
- const lastEventId = getRequestHeader ( req . headers , "Last-Event-ID" ) ;
397
420
if ( lastEventId && ! this . _config . resumability . enabled ) {
398
421
logger . warn ( `Client sent Last-Event-ID (${ lastEventId } ) but resumability is disabled.` ) ;
399
422
}
0 commit comments