From 6f8ec3f855357375b614d592dd34848a20081486 Mon Sep 17 00:00:00 2001 From: FongMi Date: Tue, 13 Jan 2026 01:21:02 +0800 Subject: [PATCH] Fix js proxy --- .../com/fongmi/quickjs/crawler/Spider.java | 13 ++-- .../java/com/fongmi/quickjs/utils/Async.java | 59 +++++++++++++------ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java b/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java index a0f2b7ebe..7cc23c158 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java @@ -115,8 +115,7 @@ public class Spider extends com.github.catvod.crawler.Spider { @Override public Object[] proxy(Map params) throws Exception { - if ("catvod".equals(params.get("from"))) return proxy2(params); - else return submit(() -> proxy1(params)).get(); + return "catvod".equals(params.get("from")) ? proxy2(params) : proxy1(params); } @Override @@ -205,8 +204,10 @@ public class Spider extends com.github.catvod.crawler.Spider { } private Object[] proxy1(Map params) throws Exception { - JSObject obj = JSUtil.toObject(ctx, params); - JSONArray array = new JSONArray(((JSArray) jsObject.getJSFunction("proxy").call(obj)).stringify()); + JSObject obj = submit(() -> JSUtil.toObject(ctx, params)).get(); + JSArray proxy = (JSArray) call("proxy", obj); + String json = submit(proxy::stringify).get(); + JSONArray array = new JSONArray(json); Map headers = array.length() > 3 ? Json.toMap(array.optString(3)) : null; boolean base64 = array.length() > 4 && array.optInt(4) == 1; Object[] result = new Object[4]; @@ -222,8 +223,8 @@ public class Spider extends com.github.catvod.crawler.Spider { String header = params.get("header"); JSArray array = submit(() -> JSUtil.toArray(ctx, Arrays.asList(url.split("/")))).get(); Object object = submit(() -> ctx.parse(header)).get(); - String json = (String) call("proxy", array, object); - Res res = Res.objectFrom(json); + String proxy = (String) call("proxy", array, object); + Res res = Res.objectFrom(proxy); Object[] result = new Object[3]; result[0] = res.getCode(); result[1] = res.getContentType(); diff --git a/quickjs/src/main/java/com/fongmi/quickjs/utils/Async.java b/quickjs/src/main/java/com/fongmi/quickjs/utils/Async.java index 79dedd352..6a617d7fb 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/utils/Async.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/utils/Async.java @@ -8,9 +8,9 @@ import java.util.concurrent.CompletableFuture; public class Async { - private final CompletableFuture future; + private CompletableFuture future; - public static CompletableFuture run(JSObject object, String name, Object[] args) { + public static CompletableFuture run(JSObject object, String name, Object... args) { return new Async().call(object, name, args); } @@ -18,13 +18,10 @@ public class Async { this.future = new CompletableFuture<>(); } - private CompletableFuture call(JSObject object, String name, Object[] args) { - JSFunction function = object.getJSFunction(name); - if (function == null) return empty(); - Object result = function.call(args); - if (result instanceof JSObject) then(result); - else future.complete(result); - function.release(); + private CompletableFuture call(JSObject object, String name, Object... args) { + JSFunction func = object.getJSFunction(name); + if (func == null) return empty(); + call(func, args); return future; } @@ -33,18 +30,44 @@ public class Async { return future; } - private void then(Object result) { - JSObject promise = (JSObject) result; + private void call(JSFunction func, Object... args) { + try { + Object result = func.call(args); + if (result instanceof JSObject) then((JSObject) result); + else future.complete(result); + } catch (Throwable e) { + future.completeExceptionally(e); + } finally { + func.release(); + } + } + + private void then(JSObject promise) { JSFunction then = promise.getJSFunction("then"); - if (then != null) then.call(callback); - if (then != null) then.release(); + if (then == null) { + future.complete(promise); + } else { + consume(then, onSuccess); + consume(promise.getJSFunction("catch"), onError); + } } - private final JSCallFunction callback = new JSCallFunction() { - @Override - public Object call(Object... args) { - future.complete(args[0]); - return null; + private void consume(JSFunction func, JSCallFunction callback) { + if (func == null) return; + try { + func.call(callback); + } finally { + func.release(); } + } + + private final JSCallFunction onSuccess = args -> { + future.complete(args != null && args.length > 0 ? args[0] : null); + return null; + }; + + private final JSCallFunction onError = args -> { + future.complete(null); + return null; }; }