@@ -53,7 +53,7 @@ export class SSEClientTransport implements Transport {
53
53
private _endpoint ?: URL ;
54
54
private _abortController ?: AbortController ;
55
55
private _url : URL ;
56
- private _eventSourceInit ? : EventSourceInit ;
56
+ private _eventSourceInit : EventSourceInit ;
57
57
private _requestInit ?: RequestInit ;
58
58
private _authProvider ?: OAuthClientProvider ;
59
59
@@ -66,7 +66,19 @@ export class SSEClientTransport implements Transport {
66
66
opts ?: SSEClientTransportOptions ,
67
67
) {
68
68
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
+
70
82
this . _requestInit = opts ?. requestInit ;
71
83
this . _authProvider = opts ?. authProvider ;
72
84
}
@@ -105,7 +117,7 @@ export class SSEClientTransport implements Transport {
105
117
106
118
private _startOrAuth ( ) : Promise < void > {
107
119
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 ) ;
109
121
this . _abortController = new AbortController ( ) ;
110
122
111
123
this . _eventSource . onerror = ( event ) => {
@@ -159,44 +171,6 @@ export class SSEClientTransport implements Transport {
159
171
} ) ;
160
172
}
161
173
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
-
200
174
async start ( ) {
201
175
if ( this . _eventSource ) {
202
176
throw new Error (
0 commit comments