Rename LimitedMap => MemoryLimitedMap

095d19cda73742ee23daedb3af8c7b55615b4789

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

2 files changed, +10 -10Ignore whitespace
src/endpoints/characters.js+2 -2
@@ -14,7 +14,7 @@ import jimp from 'jimp';
1414
15import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';15import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';
16import { jsonParser, urlencodedParser } from '../express-common.js';16import { jsonParser, urlencodedParser } from '../express-common.js';
17import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, LimitedMap } from '../util.js';17import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, MemoryLimitedMap } from '../util.js';
18import { TavernCardValidator } from '../validator/TavernCardValidator.js';18import { TavernCardValidator } from '../validator/TavernCardValidator.js';
19import { parse, write } from '../character-card-parser.js';19import { parse, write } from '../character-card-parser.js';
20import { readWorldInfoFile } from './worldinfo.js';20import { readWorldInfoFile } from './worldinfo.js';
@@ -24,7 +24,7 @@ const defaultAvatarPath = './public/img/ai4.png';
2424
25// KV-store for parsed character data25// KV-store for parsed character data
26// 100 MB limit. Would take roughly 3000 characters to reach this limit26// 100 MB limit. Would take roughly 3000 characters to reach this limit
27const characterDataCache = new LimitedMap(1024 * 1024 * 100);27const characterDataCache = new MemoryLimitedMap(1024 * 1024 * 100);
28// Some Android devices require tighter memory management28// Some Android devices require tighter memory management
29const isAndroid = process.platform === 'android';29const isAndroid = process.platform === 'android';
3030
src/util.js+8 -8
@@ -672,11 +672,11 @@ export function isValidUrl(url) {
672}672}
673673
674/**674/**
675 * LimitedMap class that limits the memory usage of string values.675 * MemoryLimitedMap class that limits the memory usage of string values.
676 */676 */
677export class LimitedMap {677export class MemoryLimitedMap {
678 /**678 /**
679 * Creates an instance of LimitedMap.679 * Creates an instance of MemoryLimitedMap.
680 * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values.680 * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values.
681 */681 */
682 constructor(maxMemoryInBytes) {682 constructor(maxMemoryInBytes) {
@@ -710,7 +710,7 @@ export class LimitedMap {
710 return;710 return;
711 }711 }
712712
713 const newValueSize = LimitedMap.estimateStringSize(value);713 const newValueSize = MemoryLimitedMap.estimateStringSize(value);
714714
715 // If the new value itself exceeds the max memory, reject it715 // If the new value itself exceeds the max memory, reject it
716 if (newValueSize > this.maxMemory) {716 if (newValueSize > this.maxMemory) {
@@ -720,7 +720,7 @@ export class LimitedMap {
720 // Check if the key already exists to adjust memory accordingly720 // Check if the key already exists to adjust memory accordingly
721 if (this.map.has(key)) {721 if (this.map.has(key)) {
722 const oldValue = this.map.get(key);722 const oldValue = this.map.get(key);
723 const oldValueSize = LimitedMap.estimateStringSize(oldValue);723 const oldValueSize = MemoryLimitedMap.estimateStringSize(oldValue);
724 this.currentMemory -= oldValueSize;724 this.currentMemory -= oldValueSize;
725 // Remove the key from its current position in the queue725 // Remove the key from its current position in the queue
726 const index = this.queue.indexOf(key);726 const index = this.queue.indexOf(key);
@@ -733,7 +733,7 @@ export class LimitedMap {
733 while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) {733 while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) {
734 const oldestKey = this.queue.shift();734 const oldestKey = this.queue.shift();
735 const oldestValue = this.map.get(oldestKey);735 const oldestValue = this.map.get(oldestKey);
736 const oldestValueSize = LimitedMap.estimateStringSize(oldestValue);736 const oldestValueSize = MemoryLimitedMap.estimateStringSize(oldestValue);
737 this.map.delete(oldestKey);737 this.map.delete(oldestKey);
738 this.currentMemory -= oldestValueSize;738 this.currentMemory -= oldestValueSize;
739 }739 }
@@ -777,7 +777,7 @@ export class LimitedMap {
777 return false;777 return false;
778 }778 }
779 const value = this.map.get(key);779 const value = this.map.get(key);
780 const valueSize = LimitedMap.estimateStringSize(value);780 const valueSize = MemoryLimitedMap.estimateStringSize(value);
781 this.map.delete(key);781 this.map.delete(key);
782 this.currentMemory -= valueSize;782 this.currentMemory -= valueSize;
783783
@@ -842,7 +842,7 @@ export class LimitedMap {
842 }842 }
843843
844 /**844 /**
845 * Makes the LimitedMap iterable.845 * Makes the MemoryLimitedMap iterable.
846 * @returns {Iterator} - Iterator over [key, value] pairs.846 * @returns {Iterator} - Iterator over [key, value] pairs.
847 */847 */
848 [Symbol.iterator]() {848 [Symbol.iterator]() {