Skip to content

Commit 900da9f

Browse files
authored
Fix browser clients in workers (#1715)
1 parent e28f9f5 commit 900da9f

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

Diff for: pkgs/http/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
* Fixed default encoding for application/json without a charset
44
to use utf8 instead of latin1, ensuring proper JSON decoding.
5+
* Avoid references to `window` in `BrowserClient`, restoring support for web
6+
workers and NodeJS.
57

6-
## 1.3.0-wip
8+
## 1.3.0
79

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

Diff for: pkgs/http/lib/src/browser_client.dart

+24-20
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import 'package:web/web.dart'
1010
AbortController,
1111
HeadersInit,
1212
ReadableStreamDefaultReader,
13+
RequestInfo,
1314
RequestInit,
14-
Response,
15-
window;
15+
Response;
1616

1717
import 'base_client.dart';
1818
import 'base_request.dart';
@@ -30,6 +30,12 @@ BaseClient createClient() {
3030
return BrowserClient();
3131
}
3232

33+
@JS('fetch')
34+
external JSPromise<Response> _fetch(
35+
RequestInfo input, [
36+
RequestInit init,
37+
]);
38+
3339
/// A `package:web`-based HTTP client that runs in the browser and is backed by
3440
/// [`window.fetch`](https://fetch.spec.whatwg.org/).
3541
///
@@ -63,24 +69,22 @@ class BrowserClient extends BaseClient {
6369

6470
final bodyBytes = await request.finalize().toBytes();
6571
try {
66-
final response = await window
67-
.fetch(
68-
'${request.url}'.toJS,
69-
RequestInit(
70-
method: request.method,
71-
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
72-
credentials: withCredentials ? 'include' : 'same-origin',
73-
headers: {
74-
if (request.contentLength case final contentLength?)
75-
'content-length': contentLength,
76-
for (var header in request.headers.entries)
77-
header.key: header.value,
78-
}.jsify()! as HeadersInit,
79-
signal: _abortController.signal,
80-
redirect: request.followRedirects ? 'follow' : 'error',
81-
),
82-
)
83-
.toDart;
72+
final response = await _fetch(
73+
'${request.url}'.toJS,
74+
RequestInit(
75+
method: request.method,
76+
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
77+
credentials: withCredentials ? 'include' : 'same-origin',
78+
headers: {
79+
if (request.contentLength case final contentLength?)
80+
'content-length': contentLength,
81+
for (var header in request.headers.entries)
82+
header.key: header.value,
83+
}.jsify()! as HeadersInit,
84+
signal: _abortController.signal,
85+
redirect: request.followRedirects ? 'follow' : 'error',
86+
),
87+
).toDart;
8488

8589
final contentLengthHeader = response.headers.get('content-length');
8690

0 commit comments

Comments
 (0)