@@ -53,7 +53,7 @@ export class SSEClientTransport implements Transport {
5353 private _endpoint ?: URL ;
5454 private _abortController ?: AbortController ;
5555 private _url : URL ;
56- private _eventSourceInit ? : EventSourceInit ;
56+ private _eventSourceInit : EventSourceInit ;
5757 private _requestInit ?: RequestInit ;
5858 private _authProvider ?: OAuthClientProvider ;
5959
@@ -66,7 +66,19 @@ export class SSEClientTransport implements Transport {
6666 opts ?: SSEClientTransportOptions ,
6767 ) {
6868 this . _url = url ;
69- this . _eventSourceInit = opts ?. eventSourceInit ;
69+
70+ const actualFetch = opts ?. eventSourceInit ?. fetch ?? fetch ;
71+ this . _eventSourceInit = {
72+ ...( opts ?. eventSourceInit ?? { } ) ,
73+ fetch : ( url , init ) => this . _commonHeaders ( ) . then ( ( headers ) => actualFetch ( url , {
74+ ...init ,
75+ headers : {
76+ ...headers ,
77+ Accept : "text/event-stream"
78+ }
79+ } ) ) ,
80+ } ;
81+
7082 this . _requestInit = opts ?. requestInit ;
7183 this . _authProvider = opts ?. authProvider ;
7284 }
@@ -105,7 +117,7 @@ export class SSEClientTransport implements Transport {
105117
106118 private _startOrAuth ( ) : Promise < void > {
107119 return new Promise ( ( resolve , reject ) => {
108- this . _eventSource = new EventSource ( this . _url . href , this . _getEventSourceInit ( ) ) ;
120+ this . _eventSource = new EventSource ( this . _url . href , this . _eventSourceInit ) ;
109121 this . _abortController = new AbortController ( ) ;
110122
111123 this . _eventSource . onerror = ( event ) => {
@@ -159,44 +171,6 @@ export class SSEClientTransport implements Transport {
159171 } ) ;
160172 }
161173
162- private _getEventSourceInit ( ) : EventSourceInit {
163- let eventSourceInit : EventSourceInit ;
164-
165- if ( this . _eventSourceInit ) {
166- const originalFetch = this . _eventSourceInit . fetch ;
167-
168- if ( originalFetch && this . _authProvider ) {
169- // merge the new headers with the existing headers
170- eventSourceInit = {
171- ...this . _eventSourceInit ,
172- fetch : async ( url , init ) => {
173- const newHeaders : Record < string , string > = await this . _commonHeaders ( ) ;
174- return originalFetch ( url , {
175- ...init ,
176- headers : {
177- ...newHeaders ,
178- ...init ?. headers
179- }
180- } ) ;
181- }
182- } ;
183- } else {
184- eventSourceInit = this . _eventSourceInit ;
185- }
186- } else {
187- eventSourceInit = {
188- fetch : ( url , init ) => this . _commonHeaders ( ) . then ( ( headers ) => fetch ( url , {
189- ...init ,
190- headers : {
191- ...headers ,
192- Accept : "text/event-stream"
193- }
194- } ) ) ,
195- } ;
196- }
197- return eventSourceInit ;
198- }
199-
200174 async start ( ) {
201175 if ( this . _eventSource ) {
202176 throw new Error (
0 commit comments