|
14 | 14 | // limitations under the License. |
15 | 15 |
|
16 | 16 | import 'dart:async'; |
| 17 | +import 'dart:convert'; |
17 | 18 | import 'dart:html'; |
18 | 19 | import 'dart:js_util' as js_util; |
19 | 20 | import 'dart:typed_data'; |
@@ -80,20 +81,25 @@ class FetchHttpRequest { |
80 | 81 | Future send([List<int>? data]) async { |
81 | 82 | final wgs = WorkerGlobalScope.instance; |
82 | 83 | _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); |
97 | 103 |
|
98 | 104 | _response = await operation.value; |
99 | 105 | _setReadyState(HttpRequest.HEADERS_RECEIVED); |
|
0 commit comments