Affected Component
src/utils/formatBytes.ts
User Impact
When displaying download progress, cache sizes, or file size measurements in the UI (e.g. progress bars, settings panels, model downloads), negative byte values, non-finite values (like NaN), or sub-1 byte values cause formatBytes to return invalid strings such as "NaN undefined" or "512 undefined".
Exact Reproduction
import { formatBytes } from "./src/utils/formatBytes.ts";
formatBytes(-100); // returns "NaN undefined"
formatBytes(0.5); // returns "512 undefined"
formatBytes(NaN); // returns "NaN undefined"
Actual Behavior
formatBytes(-100) returns "NaN undefined"
formatBytes(0.5) returns "512 undefined"
formatBytes(NaN) returns "NaN undefined"
Expected Behavior
formatBytes should validate that inputs are positive finite numbers (!Number.isFinite(bytes) || bytes <= 0 returns "0 Bytes").
- Sub-kilobyte fractional values (
0 < bytes < 1024) should format cleanly under the "Bytes" unit (e.g., "0.5 Bytes").
- The calculated index
i should be clamped safely within array bounds [0, sizes.length - 1].
Root Cause
formatBytes only checks bytes === 0 and calculates the log index i = Math.floor(Math.log(bytes) / Math.log(k)). For bytes <= 0 or non-finite inputs, Math.log returns NaN. For 0 < bytes < 1, Math.log is negative so i becomes -1. In both cases, sizes[i] evaluates to undefined.
Proposed Scope
Update formatBytes in src/utils/formatBytes.ts to validate inputs and clamp the unit index, and add unit tests covering these boundary conditions in test/helpers/formatBytes.test.js.
I intend to work on this issue and open a pull request.
Affected Component
src/utils/formatBytes.tsUser Impact
When displaying download progress, cache sizes, or file size measurements in the UI (e.g. progress bars, settings panels, model downloads), negative byte values, non-finite values (like
NaN), or sub-1 byte values causeformatBytesto return invalid strings such as"NaN undefined"or"512 undefined".Exact Reproduction
Actual Behavior
formatBytes(-100)returns"NaN undefined"formatBytes(0.5)returns"512 undefined"formatBytes(NaN)returns"NaN undefined"Expected Behavior
formatBytesshould validate that inputs are positive finite numbers (!Number.isFinite(bytes) || bytes <= 0returns"0 Bytes").0 < bytes < 1024) should format cleanly under the"Bytes"unit (e.g.,"0.5 Bytes").ishould be clamped safely within array bounds[0, sizes.length - 1].Root Cause
formatBytesonly checksbytes === 0and calculates the log indexi = Math.floor(Math.log(bytes) / Math.log(k)). Forbytes <= 0or non-finite inputs,Math.logreturnsNaN. For0 < bytes < 1,Math.logis negative soibecomes-1. In both cases,sizes[i]evaluates toundefined.Proposed Scope
Update
formatBytesinsrc/utils/formatBytes.tsto validate inputs and clamp the unit index, and add unit tests covering these boundary conditions intest/helpers/formatBytes.test.js.I intend to work on this issue and open a pull request.