-
Notifications
You must be signed in to change notification settings - Fork 1.1k
optimization: When using custom SSE request,Authorization
header can still be automatically attached to the SSE request.
#478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
f64601b
d93cfca
05bc65c
a179c12
dbf6fdd
f6a748b
b247c39
056ce81
1b3874e
b08ad9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,6 @@ export type SSEClientTransportOptions = { | |
|
||
/** | ||
* Customizes the initial SSE request to the server (the request that begins the stream). | ||
* | ||
* NOTE: Setting this property will prevent an `Authorization` header from | ||
* being automatically attached to the SSE request, if an `authProvider` is | ||
* also given. This can be worked around by setting the `Authorization` header | ||
* manually. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused as to whether this note is indeed obsolete. |
||
*/ | ||
eventSourceInit?: EventSourceInit; | ||
|
||
|
@@ -58,7 +53,7 @@ export class SSEClientTransport implements Transport { | |
private _endpoint?: URL; | ||
private _abortController?: AbortController; | ||
private _url: URL; | ||
private _eventSourceInit?: EventSourceInit; | ||
private _eventSourceInit: EventSourceInit; | ||
private _requestInit?: RequestInit; | ||
private _authProvider?: OAuthClientProvider; | ||
|
||
|
@@ -71,7 +66,19 @@ export class SSEClientTransport implements Transport { | |
opts?: SSEClientTransportOptions, | ||
) { | ||
this._url = url; | ||
this._eventSourceInit = opts?.eventSourceInit; | ||
|
||
const actualFetch = opts?.eventSourceInit?.fetch ?? fetch; | ||
this._eventSourceInit = { | ||
chenxi-null marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...(opts?.eventSourceInit ?? {}), | ||
fetch: (url, init) => this._commonHeaders().then((headers) => actualFetch(url, { | ||
...init, | ||
headers: { | ||
...headers, | ||
Accept: "text/event-stream" | ||
} | ||
})), | ||
}; | ||
|
||
this._requestInit = opts?.requestInit; | ||
this._authProvider = opts?.authProvider; | ||
} | ||
|
@@ -96,7 +103,7 @@ export class SSEClientTransport implements Transport { | |
return await this._startOrAuth(); | ||
} | ||
|
||
private async _commonHeaders(): Promise<HeadersInit> { | ||
private async _commonHeaders(): Promise<Record<string, string>> { | ||
const headers: HeadersInit = {}; | ||
if (this._authProvider) { | ||
const tokens = await this._authProvider.tokens(); | ||
|
@@ -110,18 +117,7 @@ export class SSEClientTransport implements Transport { | |
|
||
private _startOrAuth(): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this._eventSource = new EventSource( | ||
this._url.href, | ||
this._eventSourceInit ?? { | ||
fetch: (url, init) => this._commonHeaders().then((headers) => fetch(url, { | ||
...init, | ||
headers: { | ||
...headers, | ||
Accept: "text/event-stream" | ||
} | ||
})), | ||
}, | ||
); | ||
this._eventSource = new EventSource(this._url.href, this._eventSourceInit); | ||
this._abortController = new AbortController(); | ||
|
||
this._eventSource.onerror = (event) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this belongs here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to make the .vscode folder untracked by Git.