Skip to content

Commit

Permalink
Fix issues with PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mraleph committed Nov 8, 2024
1 parent 1eef312 commit 00e87f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
3 changes: 2 additions & 1 deletion pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.2.3-wip

* Fixed unintended HTML tags in doc comments.
* Fixed unintended HTML tags in doc comments.
* Switched `BrowserClient` to use Fetch API instead of `XMLHttpRequest`.

## 1.2.2

Expand Down
94 changes: 48 additions & 46 deletions pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:web/web.dart'
AbortController,
HeadersInit,
ReadableStreamDefaultReader,
Response,
RequestInit,
window;

Expand All @@ -30,9 +31,9 @@ BaseClient createClient() {
}

/// A `package:web`-based HTTP client that runs in the browser and is backed by
/// [fetch].
/// `window.fetch`.
///
/// This client inherits some limitations of [fetch].
/// This client inherits some limitations of `window.fetch`.
/// - [BaseRequest.persistentConnection];
/// - Setting [BaseRequest.followRedirects] to `false` will cause
/// `ClientException` when redirect is encountered;
Expand Down Expand Up @@ -95,56 +96,14 @@ class BrowserClient extends BaseClient {
);
}

final bodyStreamReader =
response.body?.getReader() as ReadableStreamDefaultReader?;
Stream<List<int>> _readLoop() async* {
if (bodyStreamReader == null) {
return;
}

var isDone = false, isError = false;
try {
while (true) {
final chunk = await bodyStreamReader.read().toDart;
if (chunk.done) {
isDone = true;
break;
}
yield (chunk.value! as JSUint8Array).toDart;
}
} catch (e, st) {
isError = true;
_rethrowAsClientException(e, st, request);
} finally {
if (!isDone) {
try {
// catchError here is a temporary workaround for
// http://dartbug.com/57046: an exception from cancel() will
// clobber an exception which is currently in flight.
await bodyStreamReader
.cancel()
.toDart
.catchError((_) => null, test: (_) => isError);
} catch (e, st) {
// If we have already encountered an error swallow the
// error from cancel and simply let the original error to be
// rethrown.
if (!isError) {
_rethrowAsClientException(e, st, request);
}
}
}
}
}

final headers = <String, String>{};
(response.headers as _IterableHeaders)
.forEach((String value, String header) {
headers[header.toLowerCase()] = value;
}.toJS);

return StreamedResponseV2(
_readLoop(),
_readBody(request, response),
response.status,
headers: headers,
request: request,
Expand Down Expand Up @@ -178,7 +137,50 @@ Never _rethrowAsClientException(Object e, StackTrace st, BaseRequest request) {
Error.throwWithStackTrace(e, st);
}

/// Workaround for [Headers] not providing a way to iterate the headers.
Stream<List<int>> _readBody(BaseRequest request, Response response) async* {
final bodyStreamReader =
response.body?.getReader() as ReadableStreamDefaultReader?;

if (bodyStreamReader == null) {
return;
}

var isDone = false, isError = false;
try {
while (true) {
final chunk = await bodyStreamReader.read().toDart;
if (chunk.done) {
isDone = true;
break;
}
yield (chunk.value! as JSUint8Array).toDart;
}
} catch (e, st) {
isError = true;
_rethrowAsClientException(e, st, request);
} finally {
if (!isDone) {
try {
// catchError here is a temporary workaround for
// http://dartbug.com/57046: an exception from cancel() will
// clobber an exception which is currently in flight.
await bodyStreamReader
.cancel()
.toDart
.catchError((_) => null, test: (_) => isError);
} catch (e, st) {
// If we have already encountered an error swallow the
// error from cancel and simply let the original error to be
// rethrown.
if (!isError) {
_rethrowAsClientException(e, st, request);
}
}
}
}
}

/// Workaround for `Headers` not providing a way to iterate the headers.
@JS()
extension type _IterableHeaders._(JSObject _) implements JSObject {
external void forEach(JSFunction fn);
Expand Down

0 comments on commit 00e87f0

Please sign in to comment.