You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/** 填表后人类节奏延迟(毫秒):降低 WAF/Cloudflare 误触风险 */
export const RPA_HUMAN_PACING_MIN_MS = 100;
export const RPA_HUMAN_PACING_MAX_MS = 300;
function readPacingBounds(): { min: number; max: number } | null {
if (process.env.RPA_HUMAN_PACING === "false") {
return null;
}
const minRaw = process.env.RPA_HUMAN_PACING_MIN_MS?.trim();
const maxRaw = process.env.RPA_HUMAN_PACING_MAX_MS?.trim();
const min =
minRaw && Number.isFinite(Number(minRaw))
? Math.max(0, Number(minRaw))
: RPA_HUMAN_PACING_MIN_MS;
const max =
maxRaw && Number.isFinite(Number(maxRaw))
? Math.max(min, Number(maxRaw))
: RPA_HUMAN_PACING_MAX_MS;
return { min, max };
}
/** 随机人类节奏等待时长RPA_HUMAN_PACING=false 时返回 0 */
export function humanPacingDelayMs(): number {
const bounds = readPacingBounds();
if (!bounds) {
return 0;
}
const span = bounds.max - bounds.min;
return bounds.min + Math.floor(Math.random() * (span + 1));
}
/** 填表/点击后短暂停顿,模拟真人输入节奏 */
export async function awaitHumanPacing(page: {
waitForTimeout: (ms: number) => Promise<void>;
}): Promise<void> {
const delay = humanPacingDelayMs();
if (delay <= 0) {
return;
}
await page.waitForTimeout(delay);
}