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

Adapt dart2wasm #2215

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion dio/lib/src/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:typed_data';
import 'package:meta/meta.dart';

import 'adapters/io_adapter.dart'
if (dart.library.html) 'adapters/browser_adapter.dart' as adapter;
if (dart.library.js_util) 'adapters/browser_adapter.dart' as adapter;
import 'headers.dart';
import 'options.dart';
import 'redirect_record.dart';
Expand Down
87 changes: 60 additions & 27 deletions dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:web/web.dart' as web;

import '../adapter.dart';
import '../dio_exception.dart';
Expand All @@ -19,7 +20,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {

/// These are aborted if the client is closed.
@visibleForTesting
final xhrs = <HttpRequest>{};
final xhrs = <web.XMLHttpRequest>{};

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
Expand All @@ -36,7 +37,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) async {
final xhr = HttpRequest();
final xhr = web.XMLHttpRequest();
xhrs.add(xhr);
xhr
..open(options.method, '${options.uri}')
Expand Down Expand Up @@ -65,18 +66,20 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
xhr.timeout = xhrTimeout;

final completer = Completer<ResponseBody>();

xhr.onLoad.first.then((_) {
final Uint8List body = (xhr.response as ByteBuffer).asUint8List();
final Uint8List body =
(xhr.response as JSArrayBuffer).toDart.asUint8List();
completer.complete(
ResponseBody.fromBytes(
body,
xhr.status!,
headers: xhr.responseHeaders.map((k, v) => MapEntry(k, v.split(','))),
xhr.status,
headers:
_convertResponseHeadersString2Map(xhr.getAllResponseHeaders())
.map((k, v) => MapEntry(k, v.split(','))),
statusMessage: xhr.statusText,
isRedirect: xhr.status == 302 ||
xhr.status == 301 ||
options.uri.toString() != xhr.responseUrl,
options.uri.toString() != xhr.responseURL,
),
);
});
Expand Down Expand Up @@ -111,15 +114,17 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
// so we can check it beforehand.
if (requestStream != null) {
if (connectTimeoutTimer != null) {
xhr.upload.onProgress.listen((event) {
void cancelConnectTimeoutTimer(web.ProgressEvent _) {
connectTimeoutTimer?.cancel();
connectTimeoutTimer = null;
});
}

xhr.upload.onprogress = cancelConnectTimeoutTimer.toJS;
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
}

if (sendTimeout > Duration.zero) {
final uploadStopwatch = Stopwatch();
xhr.upload.onProgress.listen((event) {
void startStopwatch(web.ProgressEvent _) {
if (!uploadStopwatch.isRunning) {
uploadStopwatch.start();
}
Expand All @@ -135,16 +140,18 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
);
xhr.abort();
}
});
}

xhr.upload.onprogress = startStopwatch.toJS;
}

final onSendProgress = options.onSendProgress;
if (onSendProgress != null) {
xhr.upload.onProgress.listen((event) {
if (event.loaded != null && event.total != null) {
onSendProgress(event.loaded!, event.total!);
}
});
void onSendProgressWrap(web.ProgressEvent event) {
onSendProgress(event.loaded, event.total);
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
}

xhr.upload.onprogress = onSendProgressWrap.toJS;
}
} else {
if (sendTimeout > Duration.zero) {
Expand Down Expand Up @@ -195,16 +202,14 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
}

xhr.onProgress.listen(
(ProgressEvent event) {
(web.ProgressEvent event) {
if (connectTimeoutTimer != null) {
connectTimeoutTimer!.cancel();
connectTimeoutTimer = null;
}
watchReceiveTimeout();
if (options.onReceiveProgress != null &&
event.loaded != null &&
event.total != null) {
options.onReceiveProgress!(event.loaded!, event.total!);
if (options.onReceiveProgress != null) {
options.onReceiveProgress!(event.loaded, event.total);
}
},
onDone: () => stopWatchReceiveTimeout(),
Expand All @@ -225,7 +230,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
);
});

xhr.onTimeout.first.then((_) {
void onTimeout() {
final isConnectTimeout = connectTimeoutTimer != null;
if (connectTimeoutTimer != null) {
connectTimeoutTimer?.cancel();
Expand All @@ -248,11 +253,13 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
);
}
}
});
}

xhr.ontimeout = onTimeout.toJS;

cancelFuture?.then((_) {
if (xhr.readyState < HttpRequest.DONE &&
xhr.readyState > HttpRequest.UNSENT) {
if (xhr.readyState < web.XMLHttpRequest.DONE &&
xhr.readyState > web.XMLHttpRequest.UNSENT) {
connectTimeoutTimer?.cancel();
try {
xhr.abort();
Expand Down Expand Up @@ -289,7 +296,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
cancelOnError: true,
);
final bytes = await completer.future;
xhr.send(bytes);
xhr.send(bytes.toJS);
} else {
xhr.send();
}
Expand All @@ -311,3 +318,29 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
xhrs.clear();
}
}

Map<String, String> _convertResponseHeadersString2Map(String headersString) {
final headers = <String, String>{};
if (headersString.isEmpty) {
return headers;
}
final headersList = headersString.split('\r\n');
for (final header in headersList) {
if (header.isEmpty) {
continue;
}

final splitIdx = header.indexOf(': ');
if (splitIdx == -1) {
continue;
}
final key = header.substring(0, splitIdx).toLowerCase();
final value = header.substring(splitIdx + 2);
if (headers.containsKey(key)) {
headers[key] = '${headers[key]}, $value';
} else {
headers[key] = value;
}
}
return headers;
}
2 changes: 1 addition & 1 deletion dio/lib/src/compute/compute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import 'dart:async';

import 'compute_io.dart' if (dart.library.html) 'compute_web.dart' as _c;
import 'compute_io.dart' if (dart.library.js_util) 'compute_web.dart' as _c;

/// Signature for the callback passed to [compute].
///
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'adapter.dart';
import 'cancel_token.dart';
import 'dio/dio_for_native.dart'
if (dart.library.html) 'dio/dio_for_browser.dart';
if (dart.library.js_util) 'dio/dio_for_browser.dart';
import 'dio_mixin.dart';
import 'headers.dart';
import 'options.dart';
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'headers.dart';
import 'interceptors/imply_content_type.dart';
import 'options.dart';
import 'progress_stream/io_progress_stream.dart'
if (dart.library.html) 'progress_stream/browser_progress_stream.dart';
if (dart.library.js_util) 'progress_stream/browser_progress_stream.dart';
import 'response.dart';
import 'response/response_stream_handler.dart';
import 'transformer.dart';
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/multipart_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:convert';
import 'package:http_parser/http_parser.dart';

import 'multipart_file/io_multipart_file.dart'
if (dart.library.html) 'multipart_file/browser_multipart_file.dart';
if (dart.library.js_util) 'multipart_file/browser_multipart_file.dart';
import 'utils.dart';

/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
Expand Down
1 change: 1 addition & 0 deletions dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
http_parser: ^4.0.0
meta: ^1.5.0
path: ^1.8.0
web: ^0.5.1
Copy link
Member

Choose a reason for hiding this comment

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

What is the min SDK requirement to use the web package?

Copy link
Member

Choose a reason for hiding this comment

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

Seems to be 3.1.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to be 3.1.0

Maybe 3.3.0?


dev_dependencies:
lints: any
Expand Down
Loading