Merge branch 'staging' into webpack-memory
| @@ -419,30 +419,35 @@ export class SlashCommandHandler { | ||
| 419 | 419 | namedArgumentList: [ |
| 420 | 420 | SlashCommandNamedArgument.fromProps({ |
| 421 | 421 | name: 'set', |
| 422 | 422 | description: 'Name of QR set nameto add the context menu to', |
| 423 | 423 | typeList: [ARGUMENT_TYPE.STRING], |
| 424 | 424 | isRequired: true, |
| 425 | 425 | enumProvider: localEnumProviders.qrSets, |
| 426 | 426 | }), |
| 427 | 427 | SlashCommandNamedArgument.fromProps({ |
| 428 | 428 | name: 'label', |
| 429 | 429 | description: 'Label of Quick Reply labelto add the context menu to', |
| 430 | 430 | typeList: [ARGUMENT_TYPE.STRING], |
| 431 | 431 | enumProvider: localEnumProviders.qrEntries, |
| 432 | 432 | }), |
| 433 | 433 | SlashCommandNamedArgument.fromProps({ |
| 434 | 434 | name: 'id', |
| 435 | 435 | description: 'numericNumeric ID of Quick Reply to add the QRcontext menu to, e.g., id=42', |
| 436 | 436 | typeList: [ARGUMENT_TYPE.NUMBER], |
| 437 | 437 | enumProvider: localEnumProviders.qrIds, |
| 438 | 438 | }), |
| 439 | 439 | new SlashCommandNamedArgument( |
| 440 | - 'chain', 'boolean', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false', | |
| 440 | + 'chain', | |
| 441 | + 'If true, button QR is sent together with (before) the clicked QR from the context menu', | |
| 442 | + [ARGUMENT_TYPE.BOOLEAN], | |
| 443 | + false, | |
| 444 | + false, | |
| 445 | + 'false', | |
| 441 | 446 | ), |
| 442 | 447 | ], |
| 443 | 448 | unnamedArgumentList: [ |
| 444 | 449 | SlashCommandArgument.fromProps({ |
| 445 | 450 | description: 'Name of QR set nameto add as a context menu', |
| 446 | 451 | typeList: [ARGUMENT_TYPE.STRING], |
| 447 | 452 | isRequired: true, |
| 448 | 453 | enumProvider: localEnumProviders.qrSets, |
| @@ -450,13 +455,16 @@ export class SlashCommandHandler { | ||
| 450 | 455 | ], |
| 451 | 456 | helpString: ` |
| 452 | 457 | <div> |
| 453 | 458 | Add a context menu preset to a QR. |
| 459 | + </div> | |
| 460 | + <div> | |
| 461 | + If <code>id</code> and <code>label</code> are both provided, <code>id</code> will be used. | |
| 454 | 462 | </div> |
| 455 | 463 | <div> |
| 456 | 464 | <strong>Example:</strong> |
| 457 | 465 | <ul> |
| 458 | 466 | <li> |
| 459 | 467 | <pre><code>/qr-contextadd set=MyPresetMyQRSetWithTheButton label=MyButton chain=true MyOtherPresetMyQRSetWithContextItems</code></pre> |
| 460 | 468 | </li> |
| 461 | 469 | </ul> |
| 462 | 470 | </div> |
| @@ -470,27 +478,27 @@ export class SlashCommandHandler { | ||
| 470 | 478 | namedArgumentList: [ |
| 471 | 479 | SlashCommandNamedArgument.fromProps({ |
| 472 | 480 | name: 'set', |
| 473 | 481 | description: 'Name of QR set nameto remove the context menu from', |
| 474 | 482 | typeList: [ARGUMENT_TYPE.STRING], |
| 475 | 483 | isRequired: true, |
| 476 | 484 | enumProvider: localEnumProviders.qrSets, |
| 477 | 485 | }), |
| 478 | 486 | SlashCommandNamedArgument.fromProps({ |
| 479 | 487 | name: 'label', |
| 480 | 488 | description: 'Label of Quick Reply labelto remove the context menu from', |
| 481 | 489 | typeList: [ARGUMENT_TYPE.STRING], |
| 482 | 490 | enumProvider: localEnumProviders.qrEntries, |
| 483 | 491 | }), |
| 484 | 492 | SlashCommandNamedArgument.fromProps({ |
| 485 | 493 | name: 'id', |
| 486 | 494 | description: 'numericNumeric ID of Quick Reply to remove the QRcontext menu from, e.g., id=42', |
| 487 | 495 | typeList: [ARGUMENT_TYPE.NUMBER], |
| 488 | 496 | enumProvider: localEnumProviders.qrIds, |
| 489 | 497 | }), |
| 490 | 498 | ], |
| 491 | 499 | unnamedArgumentList: [ |
| 492 | 500 | SlashCommandArgument.fromProps({ |
| 493 | 501 | description: 'Name of QR set nameto remove', |
| 494 | 502 | typeList: [ARGUMENT_TYPE.STRING], |
| 495 | 503 | isRequired: true, |
| 496 | 504 | enumProvider: localEnumProviders.qrSets, |
| @@ -501,6 +509,9 @@ export class SlashCommandHandler { | ||
| 501 | 509 | Remove context menu preset from a QR. |
| 502 | 510 | </div> |
| 503 | 511 | <div> |
| 512 | + If <code>id</code> and <code>label</code> are both provided, <code>id</code> will be used. | |
| 513 | + </div> | |
| 514 | + <div> | |
| 504 | 515 | <strong>Example:</strong> |
| 505 | 516 | <ul> |
| 506 | 517 | <li> |
| @@ -542,6 +553,9 @@ export class SlashCommandHandler { | ||
| 542 | 553 | Remove all context menu presets from a QR. |
| 543 | 554 | </div> |
| 544 | 555 | <div> |
| 556 | + If <code>id</code> and a label are both provided, <code>id</code> will be used. | |
| 557 | + </div> | |
| 558 | + <div> | |
| 545 | 559 | <strong>Example:</strong> |
| 546 | 560 | <ul> |
| 547 | 561 | <li> |
| @@ -908,12 +922,11 @@ export class SlashCommandHandler { | ||
| 908 | 922 | } |
| 909 | 923 | } |
| 910 | 924 | |
| 911 | - | |
| 912 | 925 | createContextItem(args, name) { |
| 913 | 926 | try { |
| 914 | 927 | this.api.createContextItem( |
| 915 | 928 | args.set, |
| 916 | - args.label, | |
| 929 | + args.id !== undefined ? Number(args.id) : args.label, | |
| 917 | 930 | name, |
| 918 | 931 | isTrueBoolean(args.chain), |
| 919 | 932 | ); |
| @@ -923,14 +936,14 @@ export class SlashCommandHandler { | ||
| 923 | 936 | } |
| 924 | 937 | deleteContextItem(args, name) { |
| 925 | 938 | try { |
| 926 | 939 | this.api.deleteContextItem(args.set, args.id !== undefined ? Number(args.id) : args.label, name); |
| 927 | 940 | } catch (ex) { |
| 928 | 941 | toastr.error(ex.message); |
| 929 | 942 | } |
| 930 | 943 | } |
| 931 | 944 | clearContextMenu(args, label) { |
| 932 | 945 | try { |
| 933 | 946 | this.api.clearContextMenu(args.set, args.id !== undefined ? Number(args.id) : args.label ?? label); |
| 934 | 947 | } catch (ex) { |
| 935 | 948 | toastr.error(ex.message); |
| 936 | 949 | } |
| @@ -19,7 +19,7 @@ export class ContextMenu { | ||
| 19 | 19 | this.itemList = this.build(qr).children; |
| 20 | 20 | this.itemList.forEach(item => { |
| 21 | 21 | item.onExpand = () => { |
| 22 | 22 | this.itemList.filter(it => it !== item) |
| 23 | 23 | .forEach(it => it.collapse()); |
| 24 | 24 | }; |
| 25 | 25 | }); |
| @@ -36,7 +36,9 @@ export class ContextMenu { | ||
| 36 | 36 | icon: qr.icon, |
| 37 | 37 | showLabel: qr.showLabel, |
| 38 | 38 | label: qr.label, |
| 39 | + title: qr.title, | |
| 39 | 40 | message: (chainedMessage && qr.message ? `${chainedMessage} | ` : '') + qr.message, |
| 41 | + isHidden: qr.isHidden, | |
| 40 | 42 | children: [], |
| 41 | 43 | }; |
| 42 | 44 | qr.contextList.forEach((cl) => { |
| @@ -51,7 +53,9 @@ export class ContextMenu { | ||
| 51 | 53 | subTree.icon, |
| 52 | 54 | subTree.showLabel, |
| 53 | 55 | subTree.label, |
| 56 | + subTree.title, | |
| 54 | 57 | subTree.message, |
| 58 | + subTree.isHidden, | |
| 55 | 59 | (evt) => { |
| 56 | 60 | evt.stopPropagation(); |
| 57 | 61 | const finalQr = Object.assign(new QuickReply(), subQr); |
| @@ -2,7 +2,7 @@ import { MenuItem } from './MenuItem.js'; | ||
| 2 | 2 | |
| 3 | 3 | export class MenuHeader extends MenuItem { |
| 4 | 4 | constructor(/**@type {String}*/label) { |
| 5 | 5 | super(null, null, label, null, null, false, null, []); |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
| @@ -4,11 +4,12 @@ export class MenuItem { | ||
| 4 | 4 | /**@type {string}*/ icon; |
| 5 | 5 | /**@type {boolean}*/ showLabel; |
| 6 | 6 | /**@type {string}*/ label; |
| 7 | + /**@type {string}*/ title; | |
| 7 | 8 | /**@type {object}*/ value; |
| 9 | + /**@type {boolean}*/ isHidden = false; | |
| 8 | 10 | /**@type {function}*/ callback; |
| 9 | 11 | /**@type {MenuItem[]}*/ childList = []; |
| 10 | 12 | /**@type {SubMenu}*/ subMenu; |
| 11 | - /**@type {boolean}*/ isForceExpanded = false; | |
| 12 | 13 | |
| 13 | 14 | /**@type {HTMLElement}*/ root; |
| 14 | 15 | |
| @@ -19,35 +20,67 @@ export class MenuItem { | ||
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * |
| 22 | 23 | * @param {?string} icon |
| 23 | 24 | * @param {?boolean} showLabel |
| 24 | 25 | * @param {string} label |
| 26 | + * @param {?string} title Tooltip | |
| 25 | 27 | * @param {object} value |
| 28 | + * @param {boolean} isHidden QR is Invisible (auto-execute only) | |
| 26 | 29 | * @param {function} callback |
| 27 | 30 | * @param {MenuItem[]} children |
| 28 | 31 | */ |
| 29 | 32 | constructor(icon, showLabel, label, title, value, isHidden, callback, children = []) { |
| 30 | 33 | this.icon = icon; |
| 31 | 34 | this.showLabel = showLabel; |
| 32 | 35 | this.label = label; |
| 36 | + this.title = title; | |
| 33 | 37 | this.value = value; |
| 38 | + this.isHidden = isHidden; | |
| 34 | 39 | this.callback = callback; |
| 35 | 40 | this.childList = children; |
| 36 | 41 | } |
| 37 | 42 | |
| 38 | 43 | |
| 44 | + /** | |
| 45 | + * Renders the MenuItem | |
| 46 | + * | |
| 47 | + * A .qr--hidden class is added to: | |
| 48 | + * - the item if it is "Invisible (auto-execute only)" | |
| 49 | + * - the icon if no icon is set | |
| 50 | + * - the label if an icon is set and showLabel is false | |
| 51 | + * | |
| 52 | + * There is no .qr--hidden class defined in default CSS, since having items | |
| 53 | + * that are invisible on the QR bar but visible in the context menu, | |
| 54 | + * or icon-only on the QR bar but labelled in the context menu, is a valid use case. | |
| 55 | + * | |
| 56 | + * To hide optional labels when icons are present, add this user CSS: | |
| 57 | + * .ctx-menu .ctx-item .qr--button-label.qr--hidden {display: none;} | |
| 58 | + * To hide icons when no icon is present (removes unwanted padding): | |
| 59 | + * .ctx-menu .ctx-item .qr--button-icon.qr--hidden {display: none;} | |
| 60 | + * To hide items that are set "invisible": | |
| 61 | + * .ctx-menu .ctx-item.qr--hidden {display: none;} | |
| 62 | + * To target submenus only, use .ctx-menu .ctx-sub-menu .qr--hidden {display: none;} | |
| 63 | + * | |
| 64 | + * @returns {HTMLElement} | |
| 65 | + */ | |
| 39 | 66 | render() { |
| 40 | 67 | if (!this.root) { |
| 41 | 68 | const item = document.createElement('li'); { |
| 42 | 69 | this.root = item; |
| 43 | 70 | item.classList.add('list-group-item'); |
| 44 | 71 | item.classList.add('ctx-item'); |
| 45 | - item.title = this.value; | |
| 72 | + | |
| 73 | + // if this item is Invisible, add the hidden class | |
| 74 | + if (this.isHidden) item.classList.add('qr--hidden'); | |
| 75 | + | |
| 76 | + // if a title/tooltip is set, add it, otherwise use the QR content | |
| 77 | + // same as for the main QR list | |
| 78 | + item.title = this.title || this.value; | |
| 79 | + | |
| 46 | 80 | if (this.callback) { |
| 47 | 81 | item.addEventListener('click', (evt) => this.callback(evt, this)); |
| 48 | 82 | } |
| 49 | 83 | const icon = document.createElement('div'); { |
| 50 | - this.domIcon = icon; | |
| 51 | 84 | icon.classList.add('qr--button-icon'); |
| 52 | 85 | icon.classList.add('fa-solid'); |
| 53 | 86 | if (!this.icon) icon.classList.add('qr--hidden'); |
| @@ -55,7 +88,6 @@ export class MenuItem { | ||
| 55 | 88 | item.append(icon); |
| 56 | 89 | } |
| 57 | 90 | const lbl = document.createElement('div'); { |
| 58 | - this.domLabel = lbl; | |
| 59 | 91 | lbl.classList.add('qr--button-label'); |
| 60 | 92 | if (this.icon && !this.showLabel) lbl.classList.add('qr--hidden'); |
| 61 | 93 | lbl.textContent = this.label; |
| @@ -14,7 +14,7 @@ import jimp from 'jimp'; | ||
| 14 | 14 | |
| 15 | 15 | import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; |
| 16 | 16 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 17 | 17 | import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer, MemoryLimitedMap } from '../util.js'; |
| 18 | 18 | import { TavernCardValidator } from '../validator/TavernCardValidator.js'; |
| 19 | 19 | import { parse, write } from '../character-card-parser.js'; |
| 20 | 20 | import { readWorldInfoFile } from './worldinfo.js'; |
| @@ -23,7 +23,8 @@ import { importRisuSprites } from './sprites.js'; | ||
| 23 | 23 | const defaultAvatarPath = './public/img/ai4.png'; |
| 24 | 24 | |
| 25 | 25 | // KV-store for parsed character data |
| 26 | -const characterDataCache = new Map(); | |
| 26 | +// 100 MB limit. Would take roughly 3000 characters to reach this limit | |
| 27 | +const characterDataCache = new MemoryLimitedMap(1024 * 1024 * 100); | |
| 27 | 28 | // Some Android devices require tighter memory management |
| 28 | 29 | const isAndroid = process.platform === 'android'; |
| 29 | 30 | |
| @@ -58,6 +59,9 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u | ||
| 58 | 59 | try { |
| 59 | 60 | // Reset the cache |
| 60 | 61 | for (const key of characterDataCache.keys()) { |
| 62 | + if (Buffer.isBuffer(inputFile)) { | |
| 63 | + break; | |
| 64 | + } | |
| 61 | 65 | if (key.startsWith(inputFile)) { |
| 62 | 66 | characterDataCache.delete(key); |
| 63 | 67 | break; |
| @@ -670,3 +670,182 @@ export function isValidUrl(url) { | ||
| 670 | 670 | return false; |
| 671 | 671 | } |
| 672 | 672 | } |
| 673 | + | |
| 674 | +/** | |
| 675 | + * MemoryLimitedMap class that limits the memory usage of string values. | |
| 676 | + */ | |
| 677 | +export class MemoryLimitedMap { | |
| 678 | + /** | |
| 679 | + * Creates an instance of MemoryLimitedMap. | |
| 680 | + * @param {number} maxMemoryInBytes - The maximum allowed memory in bytes for string values. | |
| 681 | + */ | |
| 682 | + constructor(maxMemoryInBytes) { | |
| 683 | + if (typeof maxMemoryInBytes !== 'number' || maxMemoryInBytes <= 0) { | |
| 684 | + throw new Error('maxMemoryInBytes must be a positive number'); | |
| 685 | + } | |
| 686 | + this.maxMemory = maxMemoryInBytes; | |
| 687 | + this.currentMemory = 0; | |
| 688 | + this.map = new Map(); | |
| 689 | + this.queue = []; | |
| 690 | + } | |
| 691 | + | |
| 692 | + /** | |
| 693 | + * Estimates the memory usage of a string in bytes. | |
| 694 | + * Assumes each character occupies 2 bytes (UTF-16). | |
| 695 | + * @param {string} str | |
| 696 | + * @returns {number} | |
| 697 | + */ | |
| 698 | + static estimateStringSize(str) { | |
| 699 | + return str ? str.length * 2 : 0; | |
| 700 | + } | |
| 701 | + | |
| 702 | + /** | |
| 703 | + * Adds or updates a key-value pair in the map. | |
| 704 | + * If adding the new value exceeds the memory limit, evicts oldest entries. | |
| 705 | + * @param {string} key | |
| 706 | + * @param {string} value | |
| 707 | + */ | |
| 708 | + set(key, value) { | |
| 709 | + if (typeof key !== 'string' || typeof value !== 'string') { | |
| 710 | + return; | |
| 711 | + } | |
| 712 | + | |
| 713 | + const newValueSize = MemoryLimitedMap.estimateStringSize(value); | |
| 714 | + | |
| 715 | + // If the new value itself exceeds the max memory, reject it | |
| 716 | + if (newValueSize > this.maxMemory) { | |
| 717 | + return; | |
| 718 | + } | |
| 719 | + | |
| 720 | + // Check if the key already exists to adjust memory accordingly | |
| 721 | + if (this.map.has(key)) { | |
| 722 | + const oldValue = this.map.get(key); | |
| 723 | + const oldValueSize = MemoryLimitedMap.estimateStringSize(oldValue); | |
| 724 | + this.currentMemory -= oldValueSize; | |
| 725 | + // Remove the key from its current position in the queue | |
| 726 | + const index = this.queue.indexOf(key); | |
| 727 | + if (index > -1) { | |
| 728 | + this.queue.splice(index, 1); | |
| 729 | + } | |
| 730 | + } | |
| 731 | + | |
| 732 | + // Evict oldest entries until there's enough space | |
| 733 | + while (this.currentMemory + newValueSize > this.maxMemory && this.queue.length > 0) { | |
| 734 | + const oldestKey = this.queue.shift(); | |
| 735 | + const oldestValue = this.map.get(oldestKey); | |
| 736 | + const oldestValueSize = MemoryLimitedMap.estimateStringSize(oldestValue); | |
| 737 | + this.map.delete(oldestKey); | |
| 738 | + this.currentMemory -= oldestValueSize; | |
| 739 | + } | |
| 740 | + | |
| 741 | + // After eviction, check again if there's enough space | |
| 742 | + if (this.currentMemory + newValueSize > this.maxMemory) { | |
| 743 | + return; | |
| 744 | + } | |
| 745 | + | |
| 746 | + // Add the new key-value pair | |
| 747 | + this.map.set(key, value); | |
| 748 | + this.queue.push(key); | |
| 749 | + this.currentMemory += newValueSize; | |
| 750 | + } | |
| 751 | + | |
| 752 | + /** | |
| 753 | + * Retrieves the value associated with the given key. | |
| 754 | + * @param {string} key | |
| 755 | + * @returns {string | undefined} | |
| 756 | + */ | |
| 757 | + get(key) { | |
| 758 | + return this.map.get(key); | |
| 759 | + } | |
| 760 | + | |
| 761 | + /** | |
| 762 | + * Checks if the map contains the given key. | |
| 763 | + * @param {string} key | |
| 764 | + * @returns {boolean} | |
| 765 | + */ | |
| 766 | + has(key) { | |
| 767 | + return this.map.has(key); | |
| 768 | + } | |
| 769 | + | |
| 770 | + /** | |
| 771 | + * Deletes the key-value pair associated with the given key. | |
| 772 | + * @param {string} key | |
| 773 | + * @returns {boolean} - Returns true if the key was found and deleted, else false. | |
| 774 | + */ | |
| 775 | + delete(key) { | |
| 776 | + if (!this.map.has(key)) { | |
| 777 | + return false; | |
| 778 | + } | |
| 779 | + const value = this.map.get(key); | |
| 780 | + const valueSize = MemoryLimitedMap.estimateStringSize(value); | |
| 781 | + this.map.delete(key); | |
| 782 | + this.currentMemory -= valueSize; | |
| 783 | + | |
| 784 | + // Remove the key from the queue | |
| 785 | + const index = this.queue.indexOf(key); | |
| 786 | + if (index > -1) { | |
| 787 | + this.queue.splice(index, 1); | |
| 788 | + } | |
| 789 | + | |
| 790 | + return true; | |
| 791 | + } | |
| 792 | + | |
| 793 | + /** | |
| 794 | + * Clears all entries from the map. | |
| 795 | + */ | |
| 796 | + clear() { | |
| 797 | + this.map.clear(); | |
| 798 | + this.queue = []; | |
| 799 | + this.currentMemory = 0; | |
| 800 | + } | |
| 801 | + | |
| 802 | + /** | |
| 803 | + * Returns the number of key-value pairs in the map. | |
| 804 | + * @returns {number} | |
| 805 | + */ | |
| 806 | + size() { | |
| 807 | + return this.map.size; | |
| 808 | + } | |
| 809 | + | |
| 810 | + /** | |
| 811 | + * Returns the current memory usage in bytes. | |
| 812 | + * @returns {number} | |
| 813 | + */ | |
| 814 | + totalMemory() { | |
| 815 | + return this.currentMemory; | |
| 816 | + } | |
| 817 | + | |
| 818 | + /** | |
| 819 | + * Returns an iterator over the keys in the map. | |
| 820 | + * @returns {IterableIterator<string>} | |
| 821 | + */ | |
| 822 | + keys() { | |
| 823 | + return this.map.keys(); | |
| 824 | + } | |
| 825 | + | |
| 826 | + /** | |
| 827 | + * Returns an iterator over the values in the map. | |
| 828 | + * @returns {IterableIterator<string>} | |
| 829 | + */ | |
| 830 | + values() { | |
| 831 | + return this.map.values(); | |
| 832 | + } | |
| 833 | + | |
| 834 | + /** | |
| 835 | + * Iterates over the map in insertion order. | |
| 836 | + * @param {Function} callback - Function to execute for each element. | |
| 837 | + */ | |
| 838 | + forEach(callback) { | |
| 839 | + this.map.forEach((value, key) => { | |
| 840 | + callback(value, key, this); | |
| 841 | + }); | |
| 842 | + } | |
| 843 | + | |
| 844 | + /** | |
| 845 | + * Makes the MemoryLimitedMap iterable. | |
| 846 | + * @returns {Iterator} - Iterator over [key, value] pairs. | |
| 847 | + */ | |
| 848 | + [Symbol.iterator]() { | |
| 849 | + return this.map[Symbol.iterator](); | |
| 850 | + } | |
| 851 | +} | |