Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor(js): extract shared formatBytes util; dedup delete_manager + pdf_upload (#4329)

LearningCircuitf6e8484d64ea
diff --git a/src/local_deep_research/web/static/js/deletion/delete_manager.js b/src/local_deep_research/web/static/js/deletion/delete_manager.js
index 4078d3da7b..c781ad33f4 100644
--- a/src/local_deep_research/web/static/js/deletion/delete_manager.js
+++ b/src/local_deep_research/web/static/js/deletion/delete_manager.js
@@ -58,14 +58,13 @@ async function postRequest(url, body) {
}
/**
- * Format bytes to human readable size
+ * Format bytes to human readable size.
+ * Delegates to the shared window.formatBytes (utils/format-bytes.js, loaded
+ * globally in base.html). Kept as a thin local wrapper so the internal call
+ * sites and the DeleteManager.formatBytes export stay unchanged.
*/
function formatBytes(bytes) {
- if (bytes === 0) return '0 Bytes';
- const k = 1024;
- const sizes = ['Bytes', 'KB', 'MB', 'GB'];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return parseFloat((bytes / k ** i).toFixed(2)) + ' ' + sizes[i];
+ return window.formatBytes(bytes);
}
// =============================================================================
diff --git a/src/local_deep_research/web/static/js/pdf_upload_handler.js b/src/local_deep_research/web/static/js/pdf_upload_handler.js
index f71a02ddea..36f1774733 100644
--- a/src/local_deep_research/web/static/js/pdf_upload_handler.js
+++ b/src/local_deep_research/web/static/js/pdf_upload_handler.js
@@ -30,7 +30,7 @@ class PDFUploadHandler {
this.maxFileSize = limits.max_file_size;
this.maxFiles = limits.max_files;
this.limitsLoaded = true;
- SafeLogger.log(`PDF Upload Handler: Loaded limits from API - maxFileSize: ${this.formatFileSize(this.maxFileSize)}, maxFiles: ${this.maxFiles}`);
+ SafeLogger.log(`PDF Upload Handler: Loaded limits from API - maxFileSize: ${window.formatBytes(this.maxFileSize)}, maxFiles: ${this.maxFiles}`);
} else {
SafeLogger.warn('PDF Upload Handler: Could not fetch limits from API, using defaults');
}
@@ -269,7 +269,7 @@ class PDFUploadHandler {
// Check file sizes
const oversizedFiles = pdfFiles.filter(file => file.size > this.maxFileSize);
if (oversizedFiles.length > 0) {
- this.showError(`PDF files must be smaller than ${this.formatFileSize(this.maxFileSize)}`);
+ this.showError(`PDF files must be smaller than ${window.formatBytes(this.maxFileSize)}`);
return;
}
@@ -549,17 +549,6 @@ class PDFUploadHandler {
this.updatePlaceholder();
}
- /**
- * Format file size for display
- */
- formatFileSize(bytes) {
- if (bytes === 0) return '0 Bytes';
- const k = 1024;
- const sizes = ['Bytes', 'KB', 'MB', 'GB'];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return parseFloat((bytes / k ** i).toFixed(2)) + ' ' + sizes[i];
- }
-
/**
* Get list of uploaded PDFs
*/
diff --git a/src/local_deep_research/web/static/js/utils/format-bytes.js b/src/local_deep_research/web/static/js/utils/format-bytes.js
new file mode 100644
index 0000000000..3c8a614db9
--- /dev/null
+++ b/src/local_deep_research/web/static/js/utils/format-bytes.js
@@ -0,0 +1,26 @@
+/**
+ * Shared byte-size formatter.
+ *
+ * Single source of truth for "1536 -> 1.5 KB" style formatting, previously
+ * duplicated as formatBytes in deletion/delete_manager.js and formatFileSize
+ * in pdf_upload_handler.js. Loaded globally from base.html so both consumers
+ * (and any future ones) can call window.formatBytes.
+ */
+(function() {
+ 'use strict';
+
+ /**
+ * Format a byte count as a human-readable size.
+ * @param {number} bytes - Non-negative byte count.
+ * @returns {string} e.g. "0 Bytes", "1.5 KB", "1 MB".
+ */
+ function formatBytes(bytes) {
+ if (bytes === 0) return '0 Bytes';
+ const k = 1024;
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
+ return parseFloat((bytes / k ** i).toFixed(2)) + ' ' + sizes[i];
+ }
+
+ window.formatBytes = formatBytes;
+})();
diff --git a/src/local_deep_research/web/templates/base.html b/src/local_deep_research/web/templates/base.html
index c9216ea046..cc35fe0289 100644
--- a/src/local_deep_research/web/templates/base.html
+++ b/src/local_deep_research/web/templates/base.html
@@ -189,6 +189,7 @@
<script defer src="/static/js/utils/sse-completion.js"></script>
<script defer src="/static/js/utils/value-helpers.js"></script>
<script defer src="/static/js/utils/alert-helpers.js"></script>
+ <script defer src="/static/js/utils/format-bytes.js"></script>
<!-- Shared Components -->
<script defer src="/static/js/utils/log-helpers.js"></script>
diff --git a/tests/js/deletion/delete-modules.test.js b/tests/js/deletion/delete-modules.test.js
index 8ac2be0439..925c3a4a39 100644
--- a/tests/js/deletion/delete-modules.test.js
+++ b/tests/js/deletion/delete-modules.test.js
@@ -6,6 +6,9 @@
* and the formatBytes utility. Skips trivial "API shape" assertions.
*/
+// delete_manager.js's formatBytes delegates to the shared window.formatBytes;
+// load it first so DeleteManager.formatBytes works under test.
+import '@js/utils/format-bytes.js';
import '@js/deletion/confirmation_modal.js';
describe('DeleteConfirmation tooltip lookup', () => {
diff --git a/tests/js/utils/format-bytes.test.js b/tests/js/utils/format-bytes.test.js
new file mode 100644
index 0000000000..30ee4b64bf
--- /dev/null
+++ b/tests/js/utils/format-bytes.test.js
@@ -0,0 +1,44 @@
+/**
+ * Tests for utils/format-bytes.js — window.formatBytes.
+ *
+ * Single source of truth for human-readable byte sizes, shared by
+ * delete_manager.js and pdf_upload_handler.js. The unit-boundary
+ * rounding (1023 -> Bytes, 1024 -> KB) is the interesting part.
+ */
+
+import '@js/utils/format-bytes.js';
+
+const formatBytes = window.formatBytes;
+
+describe('formatBytes', () => {
+ it('returns "0 Bytes" for 0', () => {
+ expect(formatBytes(0)).toBe('0 Bytes');
+ });
+
+ it('formats sub-kilobyte values in Bytes', () => {
+ expect(formatBytes(500)).toBe('500 Bytes');
+ expect(formatBytes(1023)).toContain('Bytes');
+ });
+
+ it('formats kilobytes, including fractional', () => {
+ expect(formatBytes(1024)).toBe('1 KB');
+ expect(formatBytes(1536)).toBe('1.5 KB');
+ });
+
+ it('formats megabytes and gigabytes', () => {
+ expect(formatBytes(1048576)).toBe('1 MB');
+ expect(formatBytes(1073741824)).toBe('1 GB');
+ });
+
+ it('rounds to 2 decimal places', () => {
+ // 1234567 bytes ≈ 1.18 MB
+ expect(formatBytes(1234567)).toBe('1.18 MB');
+ });
+
+ it('picks the unit by power-of-1024 boundary (1023->Bytes, 1024->KB)', () => {
+ expect(formatBytes(1023)).toContain('Bytes');
+ expect(formatBytes(1024)).toContain('KB');
+ expect(formatBytes(1024 * 1024 - 1)).toContain('KB');
+ expect(formatBytes(1024 * 1024)).toContain('MB');
+ });
+});