@@ -6,21 +6,14 @@ import getRawBody from "raw-body"
6
6
import { APIKeyAuthProvider } from "../../auth/providers/apikey.js"
7
7
import { DEFAULT_AUTH_ERROR } from "../../auth/types.js"
8
8
import { 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"
10
10
import { logger } from "../../core/Logger.js"
11
11
import { getRequestHeader , setResponseHeaders } from "../../utils/headers.js"
12
12
13
13
interface ExtendedIncomingMessage extends IncomingMessage {
14
14
body ?: ClientRequest
15
15
}
16
16
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
-
24
17
const SSE_HEADERS = {
25
18
"Content-Type" : "text/event-stream" ,
26
19
"Cache-Control" : "no-cache" ,
@@ -52,6 +45,31 @@ export class SSEServerTransport extends AbstractTransport {
52
45
} ) } `)
53
46
}
54
47
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
+
55
73
async start ( ) : Promise < void > {
56
74
if ( this . _server ) {
57
75
throw new Error ( "SSE transport already started" )
@@ -88,16 +106,12 @@ export class SSEServerTransport extends AbstractTransport {
88
106
logger . debug ( `Incoming request: ${ req . method } ${ req . url } ` )
89
107
90
108
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 ) )
96
110
res . writeHead ( 204 ) . end ( )
97
111
return
98
112
}
99
113
100
- setResponseHeaders ( res , CORS_HEADERS )
114
+ setResponseHeaders ( res , this . getCorsHeaders ( ) )
101
115
102
116
const url = new URL ( req . url ! , `http://${ req . headers . host } ` )
103
117
const sessionId = url . searchParams . get ( "sessionId" )
@@ -194,7 +208,7 @@ export class SSEServerTransport extends AbstractTransport {
194
208
195
209
const headers = {
196
210
...SSE_HEADERS ,
197
- ...CORS_HEADERS ,
211
+ ...this . getCorsHeaders ( ) ,
198
212
...this . _config . headers
199
213
}
200
214
setResponseHeaders ( res , headers )
@@ -301,12 +315,6 @@ export class SSEServerTransport extends AbstractTransport {
301
315
params : params
302
316
} ) } `) ;
303
317
304
- logger . debug ( `Processing RPC message: ${ JSON . stringify ( {
305
- id : rpcMessage . id ,
306
- method : rpcMessage . method ,
307
- params : rpcMessage . params
308
- } ) } `)
309
-
310
318
if ( ! this . _onmessage ) {
311
319
throw new Error ( "No message handler registered" )
312
320
}
0 commit comments