@@ -12,13 +12,17 @@ export class SSEClientTransport implements Transport {
12
12
private _endpoint ?: URL ;
13
13
private _abortController ?: AbortController ;
14
14
private _url : URL ;
15
+ private _eventSourceInit ?: EventSourceInit ;
16
+ private _requestInit ?: RequestInit ;
15
17
16
18
onclose ?: ( ) => void ;
17
19
onerror ?: ( error : Error ) => void ;
18
20
onmessage ?: ( message : JSONRPCMessage ) => void ;
19
21
20
- constructor ( url : URL ) {
22
+ constructor ( url : URL , opts ?: { eventSourceInit ?: EventSourceInit , requestInit ?: RequestInit } ) {
21
23
this . _url = url ;
24
+ this . _eventSourceInit = opts ?. eventSourceInit ;
25
+ this . _requestInit = opts ?. requestInit ;
22
26
}
23
27
24
28
start ( ) : Promise < void > {
@@ -29,7 +33,7 @@ export class SSEClientTransport implements Transport {
29
33
}
30
34
31
35
return new Promise ( ( resolve , reject ) => {
32
- this . _eventSource = new EventSource ( this . _url . href ) ;
36
+ this . _eventSource = new EventSource ( this . _url . href , this . _eventSourceInit ) ;
33
37
this . _abortController = new AbortController ( ) ;
34
38
35
39
this . _eventSource . onerror = ( event ) => {
@@ -90,14 +94,17 @@ export class SSEClientTransport implements Transport {
90
94
}
91
95
92
96
try {
93
- const response = await fetch ( this . _endpoint , {
97
+ const headers = new Headers ( this . _requestInit ?. headers ) ;
98
+ headers . set ( "content-type" , "application/json" ) ;
99
+ const init = {
100
+ ...this . _requestInit ,
94
101
method : "POST" ,
95
- headers : {
96
- "Content-Type" : "application/json" ,
97
- } ,
102
+ headers,
98
103
body : JSON . stringify ( message ) ,
99
- signal : this . _abortController ?. signal ,
100
- } ) ;
104
+ signal : this . _abortController ?. signal
105
+ } ;
106
+
107
+ const response = await fetch ( this . _endpoint , init ) ;
101
108
102
109
if ( ! response . ok ) {
103
110
const text = await response . text ( ) . catch ( ( ) => null ) ;
0 commit comments