mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-01-12 09:24:14 +00:00
Implement fetch polyfill and Promise for ES5 backend
Co-authored-by: qaiu <29825328+qaiu@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package cn.qaiu.parser.customjs;
|
||||
|
||||
import cn.qaiu.parser.customjs.JsHttpClient.JsHttpResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JavaScript Fetch API桥接类
|
||||
* 将标准的fetch API调用桥接到现有的JsHttpClient实现
|
||||
*
|
||||
* @author <a href="https://qaiu.top">QAIU</a>
|
||||
* Create at 2025/12/06
|
||||
*/
|
||||
public class JsFetchBridge {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(JsFetchBridge.class);
|
||||
|
||||
private final JsHttpClient httpClient;
|
||||
|
||||
public JsFetchBridge(JsHttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch API实现
|
||||
* 接收fetch API调用并转换为JsHttpClient调用
|
||||
*
|
||||
* @param url 请求URL
|
||||
* @param options 请求选项(包含method、headers、body等)
|
||||
* @return JsHttpResponse响应对象
|
||||
*/
|
||||
public JsHttpResponse fetch(String url, Map<String, Object> options) {
|
||||
try {
|
||||
// 解析请求方法
|
||||
String method = "GET";
|
||||
if (options != null && options.containsKey("method")) {
|
||||
method = options.get("method").toString().toUpperCase();
|
||||
}
|
||||
|
||||
// 解析并设置请求头
|
||||
if (options != null && options.containsKey("headers")) {
|
||||
Object headersObj = options.get("headers");
|
||||
if (headersObj instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> headersMap = (Map<String, Object>) headersObj;
|
||||
for (Map.Entry<String, Object> entry : headersMap.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
httpClient.putHeader(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 解析请求体
|
||||
Object body = null;
|
||||
if (options != null && options.containsKey("body")) {
|
||||
body = options.get("body");
|
||||
}
|
||||
|
||||
// 根据方法执行请求
|
||||
JsHttpResponse response;
|
||||
switch (method) {
|
||||
case "GET":
|
||||
response = httpClient.get(url);
|
||||
break;
|
||||
case "POST":
|
||||
response = httpClient.post(url, body);
|
||||
break;
|
||||
case "PUT":
|
||||
response = httpClient.put(url, body);
|
||||
break;
|
||||
case "DELETE":
|
||||
response = httpClient.delete(url);
|
||||
break;
|
||||
case "PATCH":
|
||||
response = httpClient.patch(url, body);
|
||||
break;
|
||||
case "HEAD":
|
||||
response = httpClient.getNoRedirect(url);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的HTTP方法: " + method);
|
||||
}
|
||||
|
||||
log.debug("Fetch请求完成: {} {} - 状态码: {}", method, url, response.statusCode());
|
||||
return response;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Fetch请求失败: {} - {}", url, e.getMessage());
|
||||
throw new RuntimeException("Fetch请求失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,13 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* JavaScript解析器执行器
|
||||
@@ -30,17 +35,19 @@ public class JsParserExecutor implements IPanTool {
|
||||
|
||||
private static final WorkerExecutor EXECUTOR = WebClientVertxInit.get().createSharedWorkerExecutor("parser-executor", 32);
|
||||
|
||||
private static String FETCH_RUNTIME_JS = null;
|
||||
|
||||
private final CustomParserConfig config;
|
||||
private final ShareLinkInfo shareLinkInfo;
|
||||
private final ScriptEngine engine;
|
||||
private final JsHttpClient httpClient;
|
||||
private final JsLogger jsLogger;
|
||||
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
||||
private final JsFetchBridge fetchBridge;
|
||||
|
||||
public JsParserExecutor(ShareLinkInfo shareLinkInfo, CustomParserConfig config) {
|
||||
this.config = config;
|
||||
this.shareLinkInfo = shareLinkInfo;
|
||||
this.engine = initEngine();
|
||||
|
||||
// 检查是否有代理配置
|
||||
JsonObject proxyConfig = null;
|
||||
@@ -51,6 +58,34 @@ public class JsParserExecutor implements IPanTool {
|
||||
this.httpClient = new JsHttpClient(proxyConfig);
|
||||
this.jsLogger = new JsLogger("JsParser-" + config.getType());
|
||||
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
||||
this.fetchBridge = new JsFetchBridge(httpClient);
|
||||
this.engine = initEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载fetch运行时JS代码
|
||||
* @return fetch运行时代码
|
||||
*/
|
||||
static String loadFetchRuntime() {
|
||||
if (FETCH_RUNTIME_JS != null) {
|
||||
return FETCH_RUNTIME_JS;
|
||||
}
|
||||
|
||||
try (InputStream is = JsParserExecutor.class.getClassLoader().getResourceAsStream("fetch-runtime.js")) {
|
||||
if (is == null) {
|
||||
log.warn("未找到fetch-runtime.js文件,fetch API将不可用");
|
||||
return "";
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||
FETCH_RUNTIME_JS = reader.lines().collect(Collectors.joining("\n"));
|
||||
log.debug("Fetch运行时加载成功,大小: {} 字符", FETCH_RUNTIME_JS.length());
|
||||
return FETCH_RUNTIME_JS;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("加载fetch-runtime.js失败", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +116,7 @@ public class JsParserExecutor implements IPanTool {
|
||||
engine.put("http", httpClient);
|
||||
engine.put("logger", jsLogger);
|
||||
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
||||
engine.put("JavaFetch", fetchBridge);
|
||||
|
||||
// 禁用Java对象访问
|
||||
engine.eval("var Java = undefined;");
|
||||
@@ -90,6 +126,13 @@ public class JsParserExecutor implements IPanTool {
|
||||
engine.eval("var org = undefined;");
|
||||
engine.eval("var com = undefined;");
|
||||
|
||||
// 加载fetch运行时(Promise和fetch API polyfill)
|
||||
String fetchRuntime = loadFetchRuntime();
|
||||
if (!fetchRuntime.isEmpty()) {
|
||||
engine.eval(fetchRuntime);
|
||||
log.debug("✅ Fetch API和Promise polyfill注入成功");
|
||||
}
|
||||
|
||||
log.debug("🔒 安全的JavaScript引擎初始化成功,解析器类型: {}", config.getType());
|
||||
|
||||
// 执行JavaScript代码
|
||||
|
||||
@@ -42,6 +42,7 @@ public class JsPlaygroundExecutor {
|
||||
private final JsHttpClient httpClient;
|
||||
private final JsPlaygroundLogger playgroundLogger;
|
||||
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
||||
private final JsFetchBridge fetchBridge;
|
||||
|
||||
/**
|
||||
* 创建演练场执行器
|
||||
@@ -62,6 +63,7 @@ public class JsPlaygroundExecutor {
|
||||
this.httpClient = new JsHttpClient(proxyConfig);
|
||||
this.playgroundLogger = new JsPlaygroundLogger();
|
||||
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
||||
this.fetchBridge = new JsFetchBridge(httpClient);
|
||||
this.engine = initEngine();
|
||||
}
|
||||
|
||||
@@ -84,6 +86,7 @@ public class JsPlaygroundExecutor {
|
||||
engine.put("http", httpClient);
|
||||
engine.put("logger", playgroundLogger);
|
||||
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
||||
engine.put("JavaFetch", fetchBridge);
|
||||
|
||||
// 禁用Java对象访问
|
||||
engine.eval("var Java = undefined;");
|
||||
@@ -93,6 +96,13 @@ public class JsPlaygroundExecutor {
|
||||
engine.eval("var org = undefined;");
|
||||
engine.eval("var com = undefined;");
|
||||
|
||||
// 加载fetch运行时(Promise和fetch API polyfill)
|
||||
String fetchRuntime = JsParserExecutor.loadFetchRuntime();
|
||||
if (!fetchRuntime.isEmpty()) {
|
||||
engine.eval(fetchRuntime);
|
||||
playgroundLogger.infoJava("✅ Fetch API和Promise polyfill注入成功");
|
||||
}
|
||||
|
||||
playgroundLogger.infoJava("🔒 安全的JavaScript引擎初始化成功(演练场)");
|
||||
|
||||
// 执行JavaScript代码
|
||||
|
||||
Reference in New Issue
Block a user