santifer/career-ops · error · RangeError
capacity must be a finite number >= 1, got
Error message
capacity must be a finite number >= 1, got ${capacity} What it means
createTokenBucket in providers/_dns-cache.mjs throws a RangeError when capacity is not a finite number >= 1. The bucket's token count is initialized to capacity, so a capacity of 0, negative, or non-finite would produce a bucket that can never hold tokens and would stall all queued lookups. Like ratePerMin, this is caller-supplied and validated at construction time.
Solutions
- Pass an integer capacity >= 1 (the maximum burst size the bucket should hold).
- Coerce and validate with Number() + Number.isFinite before construction.
- Fix the config/env key that supplied the bad capacity value.
- Use Math.max(1, Math.floor(computedCapacity)) if the capacity is derived from a formula that can round to 0.
Example fix
// before const bucket = createTokenBucket(rate, config.dnsBurst) // after const capacity = Math.max(1, Math.floor(Number(config.dnsBurst) || 1)) const bucket = createTokenBucket(rate, capacity)
Defensive patterns
Strategy: validation
Validate before calling
const cap = Math.max(1, Math.floor(Number(rawCapacity) || 0));
if (!Number.isFinite(cap) || cap < 1) {
throw new RangeError(`capacity must be a finite number >= 1, got ${rawCapacity}`);
} Type guard
function isValidCapacity(n) {
return typeof n === 'number' && Number.isFinite(n) && n >= 1;
} Try / catch
let bucket;
try {
bucket = createTokenBucket(rate, capacity);
} catch (e) {
if (e instanceof RangeError && /capacity/.test(e.message)) {
console.error(`Bad capacity config: ${e.message}; using default 10`);
bucket = createTokenBucket(rate, 10);
} else throw e;
} Prevention
- Coerce capacity config values with Number() and validate at startup.
- Use Math.max(1, ...) on derived capacities that can floor to 0.
- Validate all limiter settings (rate + capacity) together in one config check.
- Test bucket construction with 0 and fractional capacities.
When it happens
Trigger: Calling createTokenBucket(rate, capacity) with capacity = 0, a negative number, NaN, Infinity, or a non-numeric config value.
Common situations: A burst-capacity setting read from config as 0 or a string; a computation like Math.floor(x/y) yielding 0 or NaN; YAML/env values not coerced to numbers.
Related errors
- ratePerMin must be a finite number > 0, got
- H1B_INDEX_PATH is set but empty. Unset it to use the…
- 4dayweek: invalid URL
- 4dayweek: URL must use HTTPS
- a 40-hex commit --sha is required
AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16).
Data as JSON: /api/errors/de551d39e531a3ab.
Report an issue: GitHub.
Appendix: source
Thrown at providers/_dns-cache.mjs:139
* @param {number} [options.ratePerMin] - Sustained calls per minute. Must be > 0.
* @param {number} [options.capacity] - Burst size, in tokens.
* @param {() => number} [options.now] - Clock source, injectable for tests.
* @param {(fn: Function, ms: number) => void} [options.setTimer] - Timer, injectable for tests.
* @returns {{ take: (fn: Function) => void, pending: number, stats: () => { delayed: number, waitedMs: number } }}
*/
export function createTokenBucket(options = {}) {
const ratePerMin = options.ratePerMin ?? DEFAULT_LOOKUPS_PER_MIN;
const capacity = options.capacity ?? DEFAULT_BURST;
const now = options.now ?? Date.now;
const setTimer = options.setTimer ?? setTimeout;
// Caller-supplied, so validate rather than assert: a zero or negative rate
// would make the refill interval Infinity and hang every queued lookup.
if (!Number.isFinite(ratePerMin) || ratePerMin <= 0) {
throw new RangeError(`ratePerMin must be a finite number > 0, got ${ratePerMin}`);
}
if (!Number.isFinite(capacity) || capacity < 1) {
throw new RangeError(`capacity must be a finite number >= 1, got ${capacity}`);
}
const tokensPerMs = ratePerMin / 60_000;
let tokens = capacity;
let lastRefill = now();
/** @type {{ fn: Function, queuedAt: number }[]} */
const queue = [];
let timerPending = false;
let delayed = 0;
let waitedMs = 0;
function refill() {
const t = now();
tokens = Math.min(capacity, tokens + (t - lastRefill) * tokensPerMs);
lastRefill = t;
}
function schedule() {View on GitHub (pinned to aac998c7ed)