Repository navigation
feat: Add an HTTP-based SSE client that works on web #357
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
Open
+131
−13
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7fa73c6
feat: Add an HTTP-based SSE client that works on web
joker23 2d6ef48
fix: align web runtime behavior more with js-client-sdk
joker23 608b1a9
refactor: Drop the customMethod and httpErrors SSE capabilities
joker23 873dad1
fix: Prevent a browser reconnect from replaying a cached stream response
joker23 25bd0b4
chore: Refresh the SSE contract service lock file
joker23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/event_source_client/lib/src/http_sse_headers.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import '../launchdarkly_event_source_client.dart'; | ||
| import 'http_consts.dart'; | ||
|
|
||
| /// True when compiled for a browser. | ||
| const bool _isWeb = bool.fromEnvironment('dart.library.js_interop'); | ||
|
|
||
| /// The default headers of [SSEClient.http] on web. | ||
| /// | ||
| /// A browser forbids the `user-agent` header, so it is left out. The | ||
| /// `cache-control` header stops a browser from serving a cached stream | ||
| /// response on a reconnect. It is not CORS-safelisted, so the browser sends | ||
| /// a preflight request, which the server must allow. The LaunchDarkly | ||
| /// streaming service allows it. The LaunchDarkly browser SDK sends the same | ||
| /// headers. | ||
| const Map<String, String> webDefaultHeaders = { | ||
| HttpHeaders.acceptHeader: MimeTypes.textEventStream, | ||
| HttpHeaders.cacheControlHeader: HttpHeaders.noCacheHeaderValue, | ||
| }; | ||
|
|
||
| /// Returns the request headers of [SSEClient.http]: the default headers of | ||
| /// the platform, with [headers] on top. | ||
| Map<String, String> httpSseClientHeaders(Map<String, String> headers) => { | ||
| ...(_isWeb ? webDefaultHeaders : SSEClient.defaultHeaders), | ||
| ...headers, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/event_source_client/test/http_sse_headers_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import 'package:launchdarkly_event_source_client/launchdarkly_event_source_client.dart'; | ||
| import 'package:launchdarkly_event_source_client/src/http_sse_headers.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| // The web case runs with `dart test test/http_sse_headers_test.dart -p chrome`. | ||
| void main() { | ||
| test('on web, sends accept and cache-control and no user-agent', () { | ||
| final headers = httpSseClientHeaders({'authorization': 'key'}); | ||
|
|
||
| expect(headers, { | ||
| 'accept': 'text/event-stream', | ||
| 'cache-control': 'no-cache', | ||
| 'authorization': 'key', | ||
| }); | ||
| }, testOn: 'browser'); | ||
|
|
||
| test('on other platforms, sends the default headers', () { | ||
| final headers = httpSseClientHeaders({'authorization': 'key'}); | ||
|
|
||
| expect(headers, {...SSEClient.defaultHeaders, 'authorization': 'key'}); | ||
| }, testOn: 'vm'); | ||
|
|
||
| test('a caller header replaces a default header', () { | ||
| final headers = httpSseClientHeaders({'accept': 'application/json'}); | ||
|
|
||
| expect(headers['accept'], 'application/json'); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
NOTE: updating this to the latest in main