{"record":{"id":"de551d39e531a3ab","repo":"santifer/career-ops","slug":"capacity-must-be-a-finite-number-1-got-capac","errorCode":null,"errorMessage":"capacity must be a finite number >= 1, got ${capacity}","messagePattern":"capacity must be a finite number >= 1, got (.+?)","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"providers/_dns-cache.mjs","lineNumber":139,"sourceCode":" * @param {number} [options.ratePerMin] - Sustained calls per minute. Must be > 0.\n * @param {number} [options.capacity] - Burst size, in tokens.\n * @param {() => number} [options.now] - Clock source, injectable for tests.\n * @param {(fn: Function, ms: number) => void} [options.setTimer] - Timer, injectable for tests.\n * @returns {{ take: (fn: Function) => void, pending: number, stats: () => { delayed: number, waitedMs: number } }}\n */\nexport function createTokenBucket(options = {}) {\n  const ratePerMin = options.ratePerMin ?? DEFAULT_LOOKUPS_PER_MIN;\n  const capacity = options.capacity ?? DEFAULT_BURST;\n  const now = options.now ?? Date.now;\n  const setTimer = options.setTimer ?? setTimeout;\n\n  // Caller-supplied, so validate rather than assert: a zero or negative rate\n  // would make the refill interval Infinity and hang every queued lookup.\n  if (!Number.isFinite(ratePerMin) || ratePerMin <= 0) {\n    throw new RangeError(`ratePerMin must be a finite number > 0, got ${ratePerMin}`);\n  }\n  if (!Number.isFinite(capacity) || capacity < 1) {\n    throw new RangeError(`capacity must be a finite number >= 1, got ${capacity}`);\n  }\n\n  const tokensPerMs = ratePerMin / 60_000;\n  let tokens = capacity;\n  let lastRefill = now();\n  /** @type {{ fn: Function, queuedAt: number }[]} */\n  const queue = [];\n  let timerPending = false;\n  let delayed = 0;\n  let waitedMs = 0;\n\n  function refill() {\n    const t = now();\n    tokens = Math.min(capacity, tokens + (t - lastRefill) * tokensPerMs);\n    lastRefill = t;\n  }\n\n  function schedule() {","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/providers/_dns-cache.mjs#L121-L157","documentation":"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.","triggerScenarios":"Calling createTokenBucket(rate, capacity) with capacity = 0, a negative number, NaN, Infinity, or a non-numeric config value.","commonSituations":"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.","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."],"exampleFix":"// before\nconst bucket = createTokenBucket(rate, config.dnsBurst)\n// after\nconst capacity = Math.max(1, Math.floor(Number(config.dnsBurst) || 1))\nconst bucket = createTokenBucket(rate, capacity)","handlingStrategy":"validation","validationCode":"const cap = Math.max(1, Math.floor(Number(rawCapacity) || 0));\nif (!Number.isFinite(cap) || cap < 1) {\n  throw new RangeError(`capacity must be a finite number >= 1, got ${rawCapacity}`);\n}","typeGuard":"function isValidCapacity(n) {\n  return typeof n === 'number' && Number.isFinite(n) && n >= 1;\n}","tryCatchPattern":"let bucket;\ntry {\n  bucket = createTokenBucket(rate, capacity);\n} catch (e) {\n  if (e instanceof RangeError && /capacity/.test(e.message)) {\n    console.error(`Bad capacity config: ${e.message}; using default 10`);\n    bucket = createTokenBucket(rate, 10);\n  } else throw e;\n}","preventionTips":["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."],"tags":["rate-limiting","validation","dns","configuration"],"backgroundTag":"argument-out-of-range","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}