// UPWARD-ONLY JITTER for every wait that decides when a request reaches a site. // // A source that counts requests sees their rhythm as well as their rate: a job // every 20.0 s, a page every 3.0 s, a sync at the same minute each day is a // machine's beat. So each such number is stretched by a random share of itself // before it is waited — and ONLY stretched. Every pacing number in this repo is // a floor someone measured or was told to keep (Rumble's 120 s, X's 5 min): // jitter may make a wait longer, never shorter, so a floor stays a floor. // // `spread` is the largest share added: 0.25 makes a 20 s gap land anywhere in // [20, 25) s. 0 turns jitter off (the number is returned as is). The operator's // value is `pacing.jitter` in settings.json — the TS wrapper (./jitter.ts) // reads it; plain-JS callers (umtool) pass a spread or take the default. // // Plain JS so umtool's .mjs scripts and common's TS share one implementation. import { refuseInstagramYtdlp } from "./instagram/ytdlpRefusal.mjs"; export const DEFAULT_JITTER_SPREAD = 0.25; export const MAX_JITTER_SPREAD = 1; /** @param {unknown} spread */ export function clampSpread(spread) { const n = typeof spread === "number" && Number.isFinite(spread) ? spread : DEFAULT_JITTER_SPREAD; return Math.min(MAX_JITTER_SPREAD, Math.max(0, n)); } /** * `value` stretched by a random share of itself in [0, spread). Never smaller * than `value`; a non-positive or non-finite value is returned unchanged. * @param {number} value * @param {number} [spread] * @param {() => number} [rand] in [0, 1) * @returns {number} */ export function jitterUp(value, spread = DEFAULT_JITTER_SPREAD, rand = Math.random) { if (!Number.isFinite(value) || value <= 0) return value; const s = clampSpread(spread); if (s === 0) return value; const r = Math.min(1, Math.max(0, rand())); return value * (1 + s * r); } /** * A STABLE draw in [0, 1) for a seed — the same seed always gives the same * share. For a wait that is re-computed on every poll of one wait (the job gap * re-reads its gap each pass): a fresh draw per pass would let the shortest * draw win, which is no jitter at all. FNV-1a over the seed's UTF-16 units. * @param {string} seed * @returns {number} */ export function stableUnit(seed) { let h = 0x811c9dc5; for (let i = 0; i < seed.length; i++) { h ^= seed.charCodeAt(i); h = Math.imul(h, 0x01000193) >>> 0; } // Mix the low bits up so neighbouring seeds don't draw neighbouring shares. h ^= h >>> 15; h = Math.imul(h, 0x2c1b3c6d) >>> 0; h ^= h >>> 12; return (h >>> 0) / 0x100000000; } /** * `jitterUp` with the draw fixed by `seed`. * @param {number} value * @param {string} seed * @param {number} [spread] */ export function jitterUpStable(value, seed, spread = DEFAULT_JITTER_SPREAD) { return jitterUp(value, spread, () => stableUnit(seed)); } /** * Seconds as a yt-dlp/gallery-dl argument: at most two decimals, never rounded * DOWN below the value (a 1.004 s pace is written 1.01, not 1). * @param {number} seconds */ export function secondsArg(seconds) { const up = Math.ceil(seconds * 100) / 100; return String(Number.isInteger(up) ? up : Number(up.toFixed(2))); } /** * `args` with the value after each `--sleep-requests` / `--sleep-subtitles` * (and the other fixed yt-dlp waits) stretched by `jitterUp`. Every occurrence * is stretched because yt-dlp takes the last one. Returns a copy. * * EVERY yt-dlp ARGV PASSES HERE ON ITS WAY TO A SPAWN — common's (through * jitter.ts jitterYtdlpSleeps) and umtool's .mjs scripts' — which is why the * one refusal that holds for all of them is asked here: an instagram.com URL * is never handed to yt-dlp (./instagram/ytdlpRefusal.mjs). It THROWS, and no * process starts. * @param {readonly string[]} args * @param {number} [spread] * @param {() => number} [rand] * @returns {string[]} */ export function jitterSleepArgs(args, spread = DEFAULT_JITTER_SPREAD, rand = Math.random) { refuseInstagramYtdlp(args); const out = [...args]; if (clampSpread(spread) === 0) return out; for (let i = 0; i < out.length - 1; i++) { if (!SLEEP_FLAGS.has(out[i])) continue; const v = Number(out[i + 1]); if (Number.isFinite(v) && v > 0) out[i + 1] = secondsArg(jitterUp(v, spread, rand)); } return out; } // yt-dlp's FIXED waits. `--sleep-interval`/`--max-sleep-interval` are left alone: // yt-dlp already draws that one at random between the two. const SLEEP_FLAGS = new Set(["--sleep-requests", "--sleep-subtitles"]);