Skip to content

Commit 7bc620d

Browse files
authored
Migrate BrowserClient from blob to arraybuffer (#489)
This allows BrowserClient to be used on Cobalt (which doesn't support blob in XHR). "blob" was used as a workaround for the Dart issue #18542. Since the issue is closed, using "arraybuffer" is more straightforward and efficient.
1 parent a8efbef commit 7bc620d

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This release will be pinned to only allow pre-release sdk versions starting from
99
2.10.0-2.0.dev, which is the first version where this package will appear in the
1010
null safety allow list.
1111

12-
- Added `const` constructor to `ByteStream`.
12+
* Add `const` constructor to `ByteStream`.
13+
* Migrate `BrowserClient` from `blob` to `arraybuffer`.
1314

1415
## 0.12.2
1516

lib/src/browser_client.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BrowserClient extends BaseClient {
4747
_xhrs.add(xhr);
4848
xhr
4949
..open(request.method, '${request.url}', async: true)
50-
..responseType = 'blob'
50+
..responseType = 'arraybuffer'
5151
..withCredentials = withCredentials;
5252
request.headers.forEach(xhr.setRequestHeader);
5353

@@ -56,25 +56,13 @@ class BrowserClient extends BaseClient {
5656
// TODO(kevmoo): Waiting on https://github.com/dart-lang/linter/issues/2185
5757
// ignore: void_checks
5858
unawaited(xhr.onLoad.first.then((_) {
59-
var blob = xhr.response as Blob;
60-
var reader = FileReader();
61-
62-
reader.onLoad.first.then((_) {
63-
var body = reader.result as Uint8List;
64-
completer.complete(StreamedResponse(
65-
ByteStream.fromBytes(body), xhr.status!,
66-
contentLength: body.length,
67-
request: request,
68-
headers: xhr.responseHeaders,
69-
reasonPhrase: xhr.statusText));
70-
});
71-
72-
reader.onError.first.then((error) {
73-
completer.completeError(
74-
ClientException(error.toString(), request.url), StackTrace.current);
75-
});
76-
77-
reader.readAsArrayBuffer(blob);
59+
var body = (xhr.response as ByteBuffer).asUint8List();
60+
completer.complete(StreamedResponse(
61+
ByteStream.fromBytes(body), xhr.status!,
62+
contentLength: body.length,
63+
request: request,
64+
headers: xhr.responseHeaders,
65+
reasonPhrase: xhr.statusText));
7866
}));
7967

8068
// TODO(kevmoo): Waiting on https://github.com/dart-lang/linter/issues/2185

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ homepage: https://github.com/dart-lang/http
44
description: A composable, multi-platform, Future-based API for HTTP requests.
55

66
environment:
7-
sdk: '>=2.10.0-2.0.dev <2.11.0'
7+
sdk: '>=2.12.0-0 <2.13.0'
88

99
# Cannot be published until null safety ships
1010
publish_to: none

0 commit comments

Comments
 (0)