Refactor endpoints/chats.js 1. Move formatBytes utility to util.js 2. Fix type error related to dates sorting 3. Add type to backupFunctions map

5c82ccf4352c3be6465036250823ffa04ce9a5b8

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +26 -16Showing whitespace changes
src/endpoints/chats.js+13 -16
@@ -9,7 +9,14 @@ import { sync as writeFileAtomicSync } from 'write-file-atomic';
99import _ from 'lodash';
1010
1111import { jsonParser, urlencodedParser } from '../express-common.js';
12-import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js';
12+import {
13+ getConfigValue,
14+ humanizedISO8601DateTime,
15+ tryParse,
16+ generateTimestamp,
17+ removeOldBackups,
18+ formatBytes,
19+} from '../util.js';
1320
1421const isBackupDisabled = getConfigValue('disableChatBackup', false);
1522const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1));
@@ -46,6 +53,9 @@ function backupChat(directory, name, chat) {
4653 }
4754}
4855
56+/**
57+ * @type {Map<string, import('lodash').DebouncedFunc<function(string, string, string): void>>}
58+ */
4959const backupFunctions = new Map();
5060
5161/**
@@ -57,20 +67,7 @@ function getBackupFunction(handle) {
5767 if (!backupFunctions.has(handle)) {
5868 backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true }));
5969 }
6070 return backupFunctions.get(handle) || (() => {});
61-}
62-
63-/**
64- * Formats a byte size into a human-readable string with units
65- * @param {number} bytes - The size in bytes to format
66- * @returns {string} The formatted string (e.g., "1.5 MB")
67- */
68-function formatBytes(bytes) {
69- if (bytes === 0) return '0 B';
70- const k = 1024;
71- const sizes = ['B', 'KB', 'MB', 'GB'];
72- const i = Math.floor(Math.log(bytes) / Math.log(k));
73- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
7471}
7572
7673/**
@@ -710,7 +707,7 @@ router.post('/search', jsonParser, function (request, response) {
710707 }
711708
712709 // Sort by last message date descending
713710 results.sort((a, b) => new Date(b.last_mes).getTime() - new Date(a.last_mes).getTime());
714711 return response.send(results);
715712
716713 } catch (error) {
src/util.js+13 -0
@@ -143,6 +143,19 @@ export function getHexString(length) {
143143}
144144
145145/**
146+ * Formats a byte size into a human-readable string with units
147+ * @param {number} bytes - The size in bytes to format
148+ * @returns {string} The formatted string (e.g., "1.5 MB")
149+ */
150+export function formatBytes(bytes) {
151+ if (bytes === 0) return '0 B';
152+ const k = 1024;
153+ const sizes = ['B', 'KB', 'MB', 'GB'];
154+ const i = Math.floor(Math.log(bytes) / Math.log(k));
155+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
156+}
157+
158+/**
146159 * Extracts a file with given extension from an ArrayBuffer containing a ZIP archive.
147160 * @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive
148161 * @param {string} fileExtension File extension to look for