| 1 | const UNITS: Record<string, number> = { |
| 2 | b: 1, |
| 3 | kb: 1024, |
| 4 | mb: 1024 ** 2, |
| 5 | gb: 1024 ** 3, |
| 6 | tb: 1024 ** 4, |
| 7 | }; |
| 8 | |
| 9 | /** Parses `"1073741824"`, `"1gb"` or `"500 MB"` (binary units) into bytes. Blank means "no limit" and reads as null; anything else unreadable throws, so a typo fails at boot instead of silently lifting a limit. */ |
| 10 | export function parseByteSize(value |
| 11 | const text = |
| 12 | if (!text) |
| 13 | |
| 14 | const match = |
| 15 | if (!match) |
| 16 | |
| 17 | const bytes = |
| 18 | // enough digits overflow to Infinity, or past where a count of bytes is exact |
| 19 | if (!Number.isSafeInteger |
| 20 | throw new Error( |
| 21 | } |
| 22 | return bytes; |
| 23 | } |
| 24 | |
| 25 | /** `1073741824` → `"1 GB"`, for error messages a person reads. */ |
| 26 | export function formatByteSize(bytes |
| 27 | const [unit, |
| 28 | .reverse() |
| 29 | .find(([, size |
| 30 | const value = |
| 31 | return `${Number. |
| 32 | } |