@@ -6,21 +6,14 @@ import getRawBody from "raw-body"
66import { APIKeyAuthProvider } from "../../auth/providers/apikey.js"
77import { DEFAULT_AUTH_ERROR } from "../../auth/types.js"
88import { AbstractTransport } from "../base.js"
9- import { DEFAULT_SSE_CONFIG , SSETransportConfig , SSETransportConfigInternal } from "./types.js"
9+ import { DEFAULT_SSE_CONFIG , SSETransportConfig , SSETransportConfigInternal , DEFAULT_CORS_CONFIG , CORSConfig } from "./types.js"
1010import { logger } from "../../core/Logger.js"
1111import { getRequestHeader , setResponseHeaders } from "../../utils/headers.js"
1212
1313interface ExtendedIncomingMessage extends IncomingMessage {
1414 body ?: ClientRequest
1515}
1616
17- const CORS_HEADERS = {
18- "Access-Control-Allow-Origin" : "*" ,
19- "Access-Control-Allow-Methods" : "GET, POST, OPTIONS" ,
20- "Access-Control-Allow-Headers" : "Content-Type, Authorization, x-api-key" ,
21- "Access-Control-Expose-Headers" : "Content-Type, Authorization, x-api-key"
22- }
23-
2417const SSE_HEADERS = {
2518 "Content-Type" : "text/event-stream" ,
2619 "Cache-Control" : "no-cache" ,
@@ -52,6 +45,31 @@ export class SSEServerTransport extends AbstractTransport {
5245 } ) } `)
5346 }
5447
48+ private getCorsHeaders ( includeMaxAge : boolean = false ) : Record < string , string > {
49+ // Ensure all CORS properties are present by merging with defaults
50+ const corsConfig = {
51+ allowOrigin : DEFAULT_CORS_CONFIG . allowOrigin ,
52+ allowMethods : DEFAULT_CORS_CONFIG . allowMethods ,
53+ allowHeaders : DEFAULT_CORS_CONFIG . allowHeaders ,
54+ exposeHeaders : DEFAULT_CORS_CONFIG . exposeHeaders ,
55+ maxAge : DEFAULT_CORS_CONFIG . maxAge ,
56+ ...this . _config . cors
57+ } as Required < CORSConfig >
58+
59+ const headers : Record < string , string > = {
60+ "Access-Control-Allow-Origin" : corsConfig . allowOrigin ,
61+ "Access-Control-Allow-Methods" : corsConfig . allowMethods ,
62+ "Access-Control-Allow-Headers" : corsConfig . allowHeaders ,
63+ "Access-Control-Expose-Headers" : corsConfig . exposeHeaders
64+ }
65+
66+ if ( includeMaxAge ) {
67+ headers [ "Access-Control-Max-Age" ] = corsConfig . maxAge
68+ }
69+
70+ return headers
71+ }
72+
5573 async start ( ) : Promise < void > {
5674 if ( this . _server ) {
5775 throw new Error ( "SSE transport already started" )
@@ -88,16 +106,12 @@ export class SSEServerTransport extends AbstractTransport {
88106 logger . debug ( `Incoming request: ${ req . method } ${ req . url } ` )
89107
90108 if ( req . method === "OPTIONS" ) {
91- const preflightHeaders = {
92- ...CORS_HEADERS ,
93- "Access-Control-Max-Age" : "86400"
94- }
95- setResponseHeaders ( res , preflightHeaders )
109+ setResponseHeaders ( res , this . getCorsHeaders ( true ) )
96110 res . writeHead ( 204 ) . end ( )
97111 return
98112 }
99113
100- setResponseHeaders ( res , CORS_HEADERS )
114+ setResponseHeaders ( res , this . getCorsHeaders ( ) )
101115
102116 const url = new URL ( req . url ! , `http://${ req . headers . host } ` )
103117 const sessionId = url . searchParams . get ( "sessionId" )
@@ -194,7 +208,7 @@ export class SSEServerTransport extends AbstractTransport {
194208
195209 const headers = {
196210 ...SSE_HEADERS ,
197- ...CORS_HEADERS ,
211+ ...this . getCorsHeaders ( ) ,
198212 ...this . _config . headers
199213 }
200214 setResponseHeaders ( res , headers )
@@ -301,12 +315,6 @@ export class SSEServerTransport extends AbstractTransport {
301315 params : params
302316 } ) } `) ;
303317
304- logger . debug ( `Processing RPC message: ${ JSON . stringify ( {
305- id : rpcMessage . id ,
306- method : rpcMessage . method ,
307- params : rpcMessage . params
308- } ) } `)
309-
310318 if ( ! this . _onmessage ) {
311319 throw new Error ( "No message handler registered" )
312320 }
0 commit comments