Rename LimitedMap => MemoryLimitedMap

095d19cda73742ee23daedb3af8c7b55615b4789

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

2 files changed, +10 -10Showing whitespace changes
src/endpoints/characters.js+2 -2
@@ -14,7 +14,7 @@ import jimp from 'jimp';
1414
1515import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';
1616import { jsonParser, urlencodedParser } from '../express-common.js';
1717import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, LimitedMapMemoryLimitedMap } from '../util.js';
1818import { TavernCardValidator } from '../validator/TavernCardValidator.js';
1919import { parse, write } from '../character-card-parser.js';
2020import { readWorldInfoFile } from './worldinfo.js';
@@ -24,7 +24,7 @@ const defaultAvatarPath = './public/img/ai4.png';
2424
2525// KV-store for parsed character data
2626// 100 MB limit. Would take roughly 3000 characters to reach this limit
2727const characterDataCache = new LimitedMapMemoryLimitedMap(1024 * 1024 * 100);
2828// Some Android devices require tighter memory management
2929const isAndroid = process.platform === 'android';
3030
src/util.js+8 -8
@@ -672,11 +672,11 @@ export function isValidUrl(url) {
672672}
673673
674674/**
675675 * LimitedMapMemoryLimitedMap class that limits the memory usage of string values.
676676 */
677677export class LimitedMapMemoryLimitedMap {
678678 /**
679679 * Creates an instance of LimitedMapMemoryLimitedMap.
680680 * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values.
681681 */
682682 constructor(maxMemoryInBytes) {
@@ -710,7 +710,7 @@ export class LimitedMap {
710710 return;
711711 }
712712
713713 const newValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(value);
714714
715715 // If the new value itself exceeds the max memory, reject it
716716 if (newValueSize > this.maxMemory) {
@@ -720,7 +720,7 @@ export class LimitedMap {
720720 // Check if the key already exists to adjust memory accordingly
721721 if (this.map.has(key)) {
722722 const oldValue = this.map.get(key);
723723 const oldValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(oldValue);
724724 this.currentMemory -= oldValueSize;
725725 // Remove the key from its current position in the queue
726726 const index = this.queue.indexOf(key);
@@ -733,7 +733,7 @@ export class LimitedMap {
733733 while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) {
734734 const oldestKey = this.queue.shift();
735735 const oldestValue = this.map.get(oldestKey);
736736 const oldestValueSize = LimitedMapMemoryLimitedMap.estimateStringSize(oldestValue);
737737 this.map.delete(oldestKey);
738738 this.currentMemory -= oldestValueSize;
739739 }
@@ -777,7 +777,7 @@ export class LimitedMap {
777777 return false;
778778 }
779779 const value = this.map.get(key);
780780 const valueSize = LimitedMapMemoryLimitedMap.estimateStringSize(value);
781781 this.map.delete(key);
782782 this.currentMemory -= valueSize;
783783
@@ -842,7 +842,7 @@ export class LimitedMap {
842842 }
843843
844844 /**
845845 * Makes the LimitedMapMemoryLimitedMap iterable.
846846 * @returns {Iterator} - Iterator over [key, value] pairs.
847847 */
848848 [Symbol.iterator]() {