jackwener/OpenCLI · error · ArgumentError
step must be a non-negative number, got ${JSON.stringify(ste
Error message
step must be a non-negative number, got ${JSON.stringify(step)} What it means
buildScrollHarvestJs validates step as a finite number >= 0 (default DEFAULT_HARVEST_STEP via options.step) and throws this ArgumentError otherwise. step is the scroll distance per harvest round in the injected script; a negative or non-finite value would break viewport scrolling and is rejected at build time.
Source
Thrown at clis/xiaohongshu/search.js:637
})()
`;
}
export function buildScrollHarvestJs(webHost, targetCount, options = {}) {
const maxRounds = options.maxRounds ?? 30;
const budgetMs = options.budgetMs ?? 30_000;
const step = options.step ?? DEFAULT_HARVEST_STEP;
if (!Number.isSafeInteger(targetCount) || targetCount < 1) {
throw new ArgumentError(`targetCount must be a positive integer, got ${JSON.stringify(targetCount)}`);
}
if (!Number.isSafeInteger(maxRounds) || maxRounds < 1) {
throw new ArgumentError(`maxRounds must be a positive integer, got ${JSON.stringify(maxRounds)}`);
}
if (!Number.isFinite(budgetMs) || budgetMs <= 0) {
throw new ArgumentError(`budgetMs must be a positive number, got ${JSON.stringify(budgetMs)}`);
}
if (!Number.isFinite(step) || step < 0) {
throw new ArgumentError(`step must be a non-negative number, got ${JSON.stringify(step)}`);
}
return `
(async () => {
const targetCount = ${targetCount};
const maxRounds = ${maxRounds};
const budgetMs = ${budgetMs};
const configuredStep = ${step};
const webHost = ${JSON.stringify(webHost)};
const noteUrlInfo = ${noteUrlInfo.toString()};
const mergeHarvestedRow = ${mergeHarvestedRow.toString()};
const stripXhsAuthorDateSuffix = ${stripXhsAuthorDateSuffix.toString()};
const extractSearchRows = ${extractSearchRows.toString()};
const usableRowCount = ${usableRowCount.toString()};
const shouldStopScrolling = ${shouldStopScrolling.toString()};
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const rootScroller = document.scrollingElement || document.documentElement || document.body;
const rootScrollHeight = () => Math.max(
rootScroller?.scrollHeight || 0,View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a finite non-negative number, e.g. { step: 800 }.
- Omit options.step to use the library default.
- Clamp with Math.max(0, computedStep) and check Number.isFinite.
- Re-derive the step from window.innerHeight if it was computed from a stale value.
Example fix
// before
buildScrollHarvestJs(host, target, { step: viewport - header });
// after
const step = Math.max(0, viewport - header);
buildScrollHarvestJs(host, target, { step: Number.isFinite(step) ? step : undefined }); Defensive patterns
Strategy: validation
Validate before calling
function assertStep(v) {
if (!Number.isFinite(v) || v < 0) {
throw new TypeError(`step must be a non-negative finite number, got ${JSON.stringify(v)}`);
}
}
assertStep(options.step ?? DEFAULT_HARVEST_STEP); Type guard
const isValidStep = (v) => v === undefined || (Number.isFinite(v) && v >= 0);
Prevention
- Clamp computed scroll distances with Math.max(0, v) before passing.
- Omit step to use the library default for typical viewports.
- Check Number.isFinite to catch NaN from subtraction of undefined values.
- Never pass negative values to 'scroll up'; the API only accepts >= 0.
- Keep scroll-step math in one helper with its own unit test.
When it happens
Trigger: Passing options { step: -500 } (or NaN, Infinity, a string, a non-plain number) to buildScrollHarvestJs.
Common situations: Config supplying a negative 'scroll back up' value; a calculation like height - header producing a negative; parsing errors yielding NaN; unit confusion between px and vh.
Related errors
- targetCount must be a positive integer, got ${JSON.stringify
- maxScrolls must be a positive integer, got ${JSON.stringify(
- maxRounds must be a positive integer, got ${JSON.stringify(m
- budgetMs must be a positive number, got ${JSON.stringify(bud
- Omni Reference accepts exactly one local image
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/1616ddb9f9b63577.
Report an issue: GitHub.