fix(helpers): guard calculateSize against non-finite and negative input - #3194
yfwmaniish wants to merge 1 commit into
Conversation
Console (appwrite/console)Project ID: Sites (1)
Tip MCP server integration brings LLM superpowers to Claude Desktop and Cursor IDE |
Greptile SummaryThe PR hardens
Confidence Score: 5/5The PR appears safe to merge, with no actionable correctness, security, or repository-rule issues identified. The new guard covers the realistic nullish API-size path, the unit-index clamp remains valid for supported bases, and the tests exercise both existing behavior and the reported regressions. Important Files Changed
Reviews (1): Last reviewed commit: "fix(helpers): guard calculateSize agains..." | Re-trigger Greptile |
calculateSize() rendered "NaN undefined" / "500 undefined" for non-finite, nullish, or negative sizes because Math.log() produced an out-of-range unit index into the sizes array. Return "0 Bytes" for non-finite/<=0 input and clamp the unit index to a valid range. Valid inputs are unchanged. Adds sizeConvertion.test.ts (the helper had no tests) covering normal scaling plus the NaN/Infinity/undefined/null/negative/sub-1-byte regression cases.
|
Starting today this repo is archived. Please raise any issues with the new Appwrite console on the main platform repo: https://github.com/appwrite/appwrite |

What
calculateSize()returns broken strings for non-finite, negative, or sub-1-byte input:NaN"NaN undefined""0 Bytes"undefined/null"NaN undefined""0 Bytes"Infinity"NaN undefined""0 Bytes"-5"NaN undefined""0 Bytes"0.5"500 undefined""0.5 Bytes"The cause:
Math.log(bytes)of a non-finite/negative value yields aNaNor negative unit index, sosizes[i]reads back asundefined.Why it matters
Several call sites pass API size fields that can be nullish (e.g. a deployment still building or failed):
calculateSize(deployment.buildSize),deployment.sourceSize,deployment.totalSizecalculateSize(file.sizeOriginal),calculateSize(backup.size)One deployments table already works around exactly this with
calculateSize(deployment?.totalSize ?? 0)— but the sibling call sites don't, so users can see "NaN undefined" in the UI. Handling this gracefully also matches the existing convention of guardingNaNinhelpers/numbers(seenumbers.test.ts).Fix
src/lib/helpers/sizeConvertion.ts:'0 Bytes'for any non-finite or<= 0input (extends the existingbytes === 0guard).[0, sizes.length - 1]so sub-1-byte and very large values map to a real unit instead ofundefined.Valid inputs are unchanged.
Tests
Added
src/lib/helpers/sizeConvertion.test.ts(the helper had no test file):NaN,±Infinity,undefined/null, negative, sub-1-byteRan
format(Prettier clean),test:unit(277 passed; the unrelatedoauth2-cimdsuite fails to load withTypeError: Invalid URLon a clean checkout too, and is untouched by this change).