-
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 3 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; | ||
|
||
|
@@ -96,7 +91,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 +105,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._getEventSourceInit()); | ||
this._abortController = new AbortController(); | ||
|
||
this._eventSource.onerror = (event) => { | ||
|
@@ -175,6 +159,44 @@ export class SSEClientTransport implements Transport { | |
}); | ||
} | ||
|
||
private _getEventSourceInit(): EventSourceInit { | ||
let eventSourceInit: EventSourceInit; | ||
|
||
if (this._eventSourceInit) { | ||
const originalFetch = this._eventSourceInit.fetch; | ||
|
||
if (originalFetch && this._authProvider) { | ||
// merge the new headers with the existing headers | ||
eventSourceInit = { | ||
...this._eventSourceInit, | ||
fetch: async (url, init) => { | ||
const newHeaders: Record<string, string> = await this._commonHeaders(); | ||
return originalFetch(url, { | ||
...init, | ||
headers: { | ||
...newHeaders, | ||
...init?.headers | ||
} | ||
}); | ||
} | ||
}; | ||
} else { | ||
eventSourceInit = this._eventSourceInit; | ||
} | ||
} else { | ||
eventSourceInit = { | ||
fetch: (url, init) => this._commonHeaders().then((headers) => fetch(url, { | ||
...init, | ||
headers: { | ||
...headers, | ||
Accept: "text/event-stream" | ||
} | ||
})), | ||
}; | ||
} | ||
return eventSourceInit; | ||
} | ||
|
||
async start() { | ||
if (this._eventSource) { | ||
throw new Error( | ||
|
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.