fix: make calculate() robust to omitted optional inputs#32
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
The pure calculate() API crashed with a TypeError when compliance_needs
was omitted, and produced NaN scan_window_utilization (plus a bogus
'poor' efficiency rating) when scan_window was omitted — even though
index.d.ts marks both fields optional and the README shows
`calculate({ /* inputs */ })`.
Normalize inputs up front so omitted fields fall back to documented
defaults (matching the web component's form), while preserving explicit
0 / [] values via nullish coalescing. Also guards against a null/undefined
argument and a non-array scanning_tools.
- Add 5 tests covering omitted compliance_needs/scan_window, minimal
input, omitted scanning_tools, and zero/empty-array preservation.
- Document per-field defaults in the README Input Schema and fix the
Quick Start snippet so it isn't a crashing example.
- Record the fix in CHANGELOG.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HPMrQUfv9Q592yhYXKGCGo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The pure
calculate()API — the documented DOM-free entry point — breaks when called with the input shape its own type declarations and README advertise:TypeError: Cannot read properties of undefined (reading 'length')whencompliance_needsis omitted, even thoughindex.d.tsmarks itcompliance_needs?(optional). It's read unconditionally incalcAsmResources()(.length) andcalcTiming()(.length).NaNforscan_window_utilizationand silently falls through to a bogus'poor'efficiency rating whenscan_window(also typed optional) is omitted —scan_window * 60isNaN, every comparison isfalse, so the rating ladder lands onpoor.The README's own Quick Start (
calculate({ /* inputs */ })) and thecalculate({...})usage inREADME.mdwould crash as written.Reproduction (before this PR)
Fix
Normalize inputs once at the top of
calculateResources()so omitted fields fall back to documented defaults that mirror the web component's form (target_hosts=1000,medium/weekly,8hwindow, empty tool/compliance lists). Nullish coalescing (??) is used so explicit0/[]are preserved, and the helper also guards against anull/undefinedargument and a non-arrayscanning_tools.This brings the runtime behaviour in line with the existing
index.d.tscontract rather than changing the public type surface.Changes
hailbytes-vuln-calculator.js— addnormalizeInputs()and apply it incalculateResources().test/calculate.test.mjs— 5 new tests: omittedcompliance_needs, omittedscan_window(finite utilization), minimal-input call, omittedscanning_tools(traditional path), and preservation of explicit0/[].README.md— add a Default column to the Input Schema, note that omitted fields fall back to defaults, and fix the Quick Start snippet so it's a runnable example.CHANGELOG.md— record under Unreleased → Fixed.Verification
npm test→ 70 passing, 0 failing (5 new). The two repro cases above now return finite, well-formed results.Scope / blast radius
Localized to input handling in the calculation engine. No change to formulas, output shape, or the existing TypeScript types; all prior tests pass unchanged. Distinct from the open formula-accuracy issues (#24, #31).
🤖 Generated with Claude Code
Generated by Claude Code