zcode-daili / src /proxy /captcha-worker-entry.ts
bolikoto's picture
chore(zcode): 升级上游至 v4.8.5 并保留多账号兼容
488aedf verified
Raw History Blame Contribute Delete
1.33 kB
/**
* Captcha solver worker entry.
*
* Runs the happy-dom solver (captcha-happy.ts) inside a worker thread so the
* proxy's main event loop NEVER blocks on Atomics.wait sync XHRs. One solve
* per worker at a time: the module's global browser-frame/cookie state is
* per-worker, which also removes the cross-solve global races of in-process
* parallel solving.
*
* Protocol: {id, scene, region, prefix} in -> {id, ok, param|error} out.
* Spawned by captcha-worker-dispatch.ts via the captcha-worker-asset.ts
* file asset (`with { type: "file" }`) -- the only worker mechanism that
* survives `bun build --compile` single-file binaries (verified on Bun 1.4).
*/
import { parentPort } from "node:worker_threads";
import { solveTraceless } from "./captcha-happy.js";
type SolveMsg = { id: number; scene: string; region: string; prefix: string };
const port = parentPort;
if (!port) throw new Error("captcha worker entry requires a worker_threads parent");
port.on("message", (m: SolveMsg) => {
void (async () => {
try {
const param = await solveTraceless({ scene: m.scene, region: m.region, prefix: m.prefix });
port.postMessage({ id: m.id, ok: true, param });
} catch (err) {
port.postMessage({ id: m.id, ok: false, error: String((err as Error)?.message ?? err) });
}
})();
});