Skip to content
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

Use Fetch API in SSE Client #66

Merged
merged 4 commits into from
Nov 23, 2022
Merged

Use Fetch API in SSE Client #66

merged 4 commits into from
Nov 23, 2022

Conversation

elliette
Copy link
Contributor

Use fetch requests instead of XHR requests in sse_client.

Manifest V3 Chrome Extensions no longer supports XHR (see dart-lang/http#595 (comment)). Therefore, this PR switches sse_client over to using fetch requests to unblock the Dart Debug Extension.

I'm also now closing the connection with an error when something goes wrong with the request. This makes it easier for clients to figure out why their connection was closed, eg:

background.dart.js:9165 SSE client failed to send ["DevToolsRequest","appId","L0KWP9nZ5N1ioobPczhOEQ==","instanceId","c35c5e80-6b5b-11ed-8f31-a1a736009ce5","contextId",1,"tabUrl","https://b2607f8b04800100000144d90c0a823621f90000000000000000001.proxy.googlers.com/dart/angular/codelab/example/app/web/index.ddc.html","uriOnly",true]:
 ReferenceError: XMLHttpRequest is not defined

@elliette elliette requested review from grouma and natebosch November 23, 2022 18:41
@@ -5,7 +5,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:js_util' as js_util;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this import in addition to the import on line 11?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no longer need either after addressing your other suggestions!

@JS()
@anonymous
class FetchOptions {
external String get method; // e.g., 'GET', 'POST'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I don't think these getters are used in Dart. It shouldn't be necessary to define them if they aren't used - the constructor is the important part of this class definition

class FetchOptions {
  external factory FetchOptions({
    String method, // e.g., 'GET', 'POST'
    CredentialsOptions credentialsOptions,
    String? body,
  });
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, removed the getters


@JS()
@anonymous
class FetchOptions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to move these to lib/src, or make them private. I don't think we want to expose JS interop types from this library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made them private

// Custom implementation of Fetch API until Dart supports GET vs. POST,
// credentials, etc. See https://github.com/dart-lang/http/issues/595.
Future<dynamic> _fetch(String resourceUrl, FetchOptions options) {
return js_util.promiseToFuture(js_util.callMethod(globalThis, 'fetch', [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work to do

@JS('fetch')
external Object _fetchPromise(String resourceUrl, FetchOptions options);

Future<dynamic> _fetch(String resourceUrl, FetchOptions options) =>
    promiseToFuture(_fetchPromise(resourceUrl, options));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does! Switched to that

@elliette elliette merged commit eaee6a8 into master Nov 23, 2022
@elliette elliette deleted the done-event branch November 23, 2022 21:19
mosuem pushed a commit to dart-lang/tools that referenced this pull request Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants