Files
rea/src/process/capture/ProcessTimer.ts
T
morluto 8443930bd3 refactor(limits): declare the Node timer bound and duration schema once (#10640)
* refactor(limits): declare the Node timer bound and duration schema once

Fifteen modules re-declared 2_147_483_647 as their own timer maximum and
eleven built the same 1..2^31-1 millisecond schema. They now use
MAX_NODE_TIMER_DELAY_MS and timerDurationMsSchema from
src/domain/analysisResourceLimits.ts, and an architecture guard rejects the
literal outside that owner and five non-timer int32 bounds. The generated
MCP tool catalog is byte-identical.

* refactor(limits): keep the JVM processor-count bound separate from the timer bound

REA_JADX_ACTIVE_PROCESSOR_COUNT is a Java int count, not a timer delay, so it keeps its own named bound and a documented exception in the signed 32-bit guard. Remove the timer-bound and zod imports the duration-schema migration left unused.

* refactor(limits): keep the TShark packet-count bound separate from the timer bound

packet_limit is passed to TShark's -c as a C int packet count, not a timer delay, so it keeps its own named bound and a documented exception in the signed 32-bit guard.
2026-11-15 16:40:03 +08:00

58 lines
1.7 KiB
TypeScript

import { MAX_NODE_TIMER_DELAY_MS } from "../../domain/analysisResourceLimits.js";
/** A timer owned by one process capture or replay endpoint. */
export interface ProcessTimer {
cancel(): void;
}
/** Injectable timer primitive for deterministic long-delay tests. */
export type ProcessTimerScheduler = (
callback: () => void,
delayMs: number,
) => ProcessTimer;
const systemTimerScheduler: ProcessTimerScheduler = (callback, delayMs) => {
const timer = setTimeout(callback, delayMs);
return { cancel: () => clearTimeout(timer) };
};
/** Schedule a delay using consecutive Node timers without shortening long waits. */
export const scheduleProcessDelay = (
delayMs: number,
callback: () => void,
schedule: ProcessTimerScheduler = systemTimerScheduler,
): ProcessTimer => {
if (!Number.isSafeInteger(delayMs) || delayMs < 0)
throw new RangeError("Process delay must be a nonnegative safe integer");
let remaining = delayMs;
let timer: ProcessTimer | undefined;
let cancelled = false;
const scheduleNext = (): void => {
if (cancelled) return;
const interval = Math.min(remaining, MAX_NODE_TIMER_DELAY_MS);
timer = schedule(() => {
if (cancelled) return;
remaining -= interval;
if (remaining === 0) callback();
else scheduleNext();
}, interval);
};
scheduleNext();
return {
cancel: () => {
cancelled = true;
timer?.cancel();
},
};
};
/** Create an owned interval that shares process-capture cancellation handling. */
export const scheduleProcessInterval = (
callback: () => void,
intervalMs: number,
): ProcessTimer => {
const timer = setInterval(callback, intervalMs);
return { cancel: () => clearInterval(timer) };
};