Skip to content

Commit da178b3

Browse files
author
Steve Browne
committed
Implemented a hack using eval to call fetch so we can pass the body as a Uint8Array to support values larger than 127 which string encoding was messing up.
1 parent 26b5dbb commit da178b3

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/src/client/transport/fetch_transport.dart

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515

1616
import 'dart:async';
17+
import 'dart:convert';
1718
import 'dart:html';
1819
import 'dart:js_util' as js_util;
1920
import 'dart:typed_data';
@@ -80,20 +81,25 @@ class FetchHttpRequest {
8081
Future send([List<int>? data]) async {
8182
final wgs = WorkerGlobalScope.instance;
8283
_setReadyState(HttpRequest.LOADING);
83-
final init = <String, dynamic>{
84-
'method': method,
85-
'referrerPolicy': referrerPolicy,
86-
'mode': mode,
87-
'credentials': credentials,
88-
'cache': cache,
89-
'redirect': redirect,
90-
'integrity': integrity,
91-
'keepalive': keepAlive,
92-
if (headers.isNotEmpty) 'headers': headers,
93-
if (data != null) 'body': String.fromCharCodes(data),
94-
};
95-
final operation =
96-
_cancelable = CancelableOperation.fromFuture(wgs.fetch(uri, init));
84+
final headersStr =
85+
headers.isNotEmpty ? '"headers": ${json.encode(headers)},' : '';
86+
final bodyStr = data != null ? '"body": Uint8Array.from($data),' : '';
87+
final initStr = '''{
88+
$headersStr
89+
$bodyStr
90+
"method": "$method",
91+
"referrerPolicy": "$referrerPolicy",
92+
"mode": "$mode",
93+
"credentials": "$credentials",
94+
"cache": "$cache",
95+
"redirect": "$redirect",
96+
"integrity": "$integrity",
97+
"keepalive": $keepAlive
98+
}''';
99+
100+
final promise = js_util.promiseToFuture(
101+
js_util.callMethod(wgs, 'eval', ['fetch("$uri", $initStr)']));
102+
final operation = _cancelable = CancelableOperation.fromFuture(promise);
97103

98104
_response = await operation.value;
99105
_setReadyState(HttpRequest.HEADERS_RECEIVED);

0 commit comments

Comments
 (0)