// The operator's jitter (settings.json `pacing.jitter`) over ./jitter.mjs — see // there for why every request-pacing wait is stretched, and only stretched. import { getSettings } from "./settings"; import { clampSpread, jitterSleepArgs, jitterUp, jitterUpStable, } from "./jitter.mjs"; export { secondsArg, stableUnit } from "./jitter.mjs"; // The spread in force. A settings read that throws (no settings file in a // bare test) falls back to the default rather than to no jitter. export function pacingJitter(): number { // TEST-ONLY: E2E_PACING_JITTER replaces the operator's share (the unit tests // that assert an exact gap set 0). Unset everywhere else. const raw = process.env.E2E_PACING_JITTER; if (raw !== undefined && raw.trim() !== "" && Number.isFinite(Number(raw))) { return clampSpread(Number(raw)); } try { return clampSpread(getSettings().pacing?.jitter); } catch { return clampSpread(undefined); } } // `value` (any unit) stretched by up to `pacing.jitter` of itself. export function jitter(value: number, rand: () => number = Math.random): number { return jitterUp(value, pacingJitter(), rand); } // Milliseconds, stretched and rounded UP to a whole millisecond. export function jitterMs(ms: number, rand: () => number = Math.random): number { return Math.ceil(jitter(ms, rand)); } // The same stretch every time for one `seed` — for a wait re-computed on each // poll of itself (see stableUnit). export function jitterMsStable(ms: number, seed: string): number { return Math.ceil(jitterUpStable(ms, seed, pacingJitter())); } // yt-dlp args with their fixed waits (`--sleep-requests`, `--sleep-subtitles`) // stretched — once per spawn, since yt-dlp itself waits exactly what it is told. // // EVERY yt-dlp ARGV PASSES HERE ON ITS WAY TO A SPAWN. The one refusal that // holds for all of them is asked inside jitterSleepArgs (jitter.mjs, shared // with umtool's scripts): an instagram.com URL is never handed to yt-dlp // (lib/instagram/ytdlpRefusal.mjs) — it throws, and no process starts. export function jitterYtdlpSleeps( args: readonly string[], rand: () => number = Math.random, ): string[] { return jitterSleepArgs(args, pacingJitter(), rand); }