Files
WebDrop/web/src/core-bridge/captcha.ts
T

16 lines
835 B
TypeScript

import { serverApi } from './server';
/** Cap performs challenge solving in its own workers and only returns a one-use token. */
export async function solveCreationCaptcha(): Promise<string | undefined> {
const info = await serverApi.info();
if (!info.captcha?.enabled) return undefined;
if (typeof info.captcha.api_endpoint !== 'string') throw new Error('验证码服务配置无效');
// Cap's official client owns the challenge protocol and WASM worker implementation; bundling it avoids a runtime CDN script.
const { default: Cap } = await import('cap-widget');
const config = { apiEndpoint: info.captcha.api_endpoint, workers: 2 } as ConstructorParameters<typeof Cap>[0] & { workers: number };
const { token } = await new Cap(config).solve();
if (!token) throw new Error('验证码未完成');
return token;
}