Rename LimitedMap => MemoryLimitedMap
| @@ -14,7 +14,7 @@ import jimp from 'jimp'; | |||
| 14 | 14 | ||
| 15 | import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; | 15 | import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; |
| 16 | import { jsonParser, urlencodedParser } from '../express-common.js'; | 16 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 17 | import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, LimitedMap } from '../util.js'; | 17 | import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, MemoryLimitedMap } from '../util.js'; |
| 18 | import { TavernCardValidator } from '../validator/TavernCardValidator.js'; | 18 | import { TavernCardValidator } from '../validator/TavernCardValidator.js'; |
| 19 | import { parse, write } from '../character-card-parser.js'; | 19 | import { parse, write } from '../character-card-parser.js'; |
| 20 | import { readWorldInfoFile } from './worldinfo.js'; | 20 | import { readWorldInfoFile } from './worldinfo.js'; |
| @@ -24,7 +24,7 @@ const defaultAvatarPath = './public/img/ai4.png'; | |||
| 24 | 24 | ||
| 25 | // KV-store for parsed character data | 25 | // KV-store for parsed character data |
| 26 | // 100 MB limit. Would take roughly 3000 characters to reach this limit | 26 | // 100 MB limit. Would take roughly 3000 characters to reach this limit |
| 27 | const characterDataCache = new LimitedMap(1024 * 1024 * 100); | 27 | const characterDataCache = new MemoryLimitedMap(1024 * 1024 * 100); |
| 28 | // Some Android devices require tighter memory management | 28 | // Some Android devices require tighter memory management |
| 29 | const isAndroid = process.platform === 'android'; | 29 | const isAndroid = process.platform === 'android'; |
| 30 | 30 | ||
| @@ -672,11 +672,11 @@ export function isValidUrl(url) { | |||
| 672 | } | 672 | } |
| 673 | 673 | ||
| 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 | */ |
| 677 | export class LimitedMap { | 677 | export 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 | } |
| 712 | 712 | ||
| 713 | const newValueSize = LimitedMap.estimateStringSize(value); | 713 | const newValueSize = MemoryLimitedMap.estimateStringSize(value); |
| 714 | 714 | ||
| 715 | // If the new value itself exceeds the max memory, reject it | 715 | // 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 accordingly | 720 | // 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 queue | 725 | // 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; |
| 783 | 783 | ||
| @@ -842,7 +842,7 @@ export class LimitedMap { | |||
| 842 | } | 842 | } |
| 843 | 843 | ||
| 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]() { |