fix js fetch headers (##589)

This commit is contained in:
wanghongenpin
2025-09-12 00:36:22 +08:00
parent 22eb6a70eb
commit 07ffeb8e0f
5 changed files with 42 additions and 9 deletions

View File

@@ -27,10 +27,11 @@ function fetch(url, options) {
blob: () => Promise.resolve(request.response.body),
clone: response,
headers: {
...headers,
keys: () => keys,
entries: () => all,
get: n => headers[n.toLowerCase()],
has: n => n.toLowerCase() in headers
has: n => n.toLowerCase() in headers,
}
});

View File

@@ -250,14 +250,13 @@ extension JavascriptRuntimeXhrExtension on JavascriptRuntime {
logger.d('Eval Fetch Result: $evalFetchResult');
}
Future<http.Client> createClient(enabledProxy) async {
Future<http.Client> createClient(bool enabledProxy) async {
if (!enabledProxy) {
return http.Client();
}
// ProxyServer.current.isRunning
var httpClient = HttpClient();
print(ProxyServer.current?.isRunning);
String proxy;
if (Platforms.isDesktop()) {
Map? proxyResult = await DesktopMultiWindow.invokeMethod(0, 'getProxyInfo');
@@ -354,20 +353,28 @@ extension JavascriptRuntimeXhrExtension on JavascriptRuntime {
body = response.bodyBytes;
}
// logger.d('RESPONSE TEXT: $responseText');
final xhrResult = XmlHttpRequestResponse(
responseText: responseText,
responseInfo: XhtmlHttpResponseInfo(statusCode: 200, statusText: "OK", body: body),
);
response.headers.forEach((key, value) {
xhrResult.responseInfo?.addResponseHeaders(key, value);
});
final responseInfo = jsonEncode(xhrResult.responseInfo);
//final responseText = xhrResult.responseText; //.replaceAll("\\n", "\\\n");
final safeResponseText = responseText != null ? jsonEncode(responseText) : null;
final error = xhrResult.error;
// logger.d('XHR response for url: ${pendingCall.url}, status: ${xhrResult.responseInfo?.statusCode}');
// send back to the javascript environment the
// response for the http pending callback
this.evaluate(
"globalThis.xhrRequests[${pendingCall.idRequest}].callback($responseInfo, `$responseText`, $error);",
var jsResult = evaluate(
"globalThis.xhrRequests[${pendingCall.idRequest}].callback($responseInfo, $safeResponseText, $error);",
);
if (jsResult.isError) {
logger.e('jsResult error url:${pendingCall.url}, ${jsResult.stringResult}');
}
});
});
@@ -455,7 +462,7 @@ class XhtmlHttpResponseInfo {
"statusCode": statusCode,
"statusText": statusText,
"body": body,
"responseHeaders": jsonEncode(responseHeaders)
"responseHeaders": responseHeaders
};
}
}

View File

@@ -65,7 +65,11 @@ class RequestMapInterceptor extends Interceptor {
response = await executeScript(request, mapRule, item.script!);
}
response?.request = request;
if (response == null) {
return null;
}
response.request = request;
request.response = response;
return response;
}

View File

@@ -112,6 +112,7 @@ class HttpProxyChannelHandler extends ChannelHandler<HttpRequest> {
for (var interceptor in interceptors) {
var response = await interceptor.execute(request!);
if (response != null) {
listener?.onResponse(channelContext, response);
channel.writeAndClose(channelContext, response);
return;
}

20
test/js.js Normal file
View File

@@ -0,0 +1,20 @@
async function onRequest() {
const fetchResponse = await fetch('https://httpbin.org/anything');
console.log(fetchResponse.headers);
console.log(await fetchResponse.text());
console.log( fetchResponse.body);
const response = {
statusCode: 200,
body: fetchResponse.body,
headers: fetchResponse.headers
};
return response;
}
onRequest().then( response => {
console.log('Response:', response);
})