All files / my-screeps-repo utils.memory.js

66.66% Statements 40/60
58.2% Branches 39/67
66.66% Functions 6/9
65.51% Lines 38/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                7x   290x   289x                             289x     7x     4x 2x 2x   2x         1x 1x     2x                 5x 3x     2x 1x     2x 1x     2x         3x 1x     2x 2x   2x         4x 2x     2x 1x             8x 1x     7x 3x     7x 7x 3x     4x 4x         4x                                                                                                                
// Memory Management Utilities
// Inspired by best practices from daily update 2026-02-20
 
/**
 * Security: Validates that a key is safe to use for object access.
 * Prevents Prototype Pollution attacks by blocking special properties.
 * Defined as a local constant to avoid 'this' context issues during destructuring.
 */
const isSafeKey = (key) => {
    // Numbers are always safe keys in JavaScript objects
    if (typeof key === 'number') return true;
    // Only allow safe strings and block dangerous properties
    const dangerousKeys = [
        '__proto__',
        'constructor',
        'prototype',
        '__defineGetter__',
        '__defineSetter__',
        '__lookupGetter__',
        '__lookupSetter__',
        'toString',
        'valueOf',
        'hasOwnProperty',
        'toLocaleString',
        'isPrototypeOf',
        'propertyIsEnumerable',
    ];
    return typeof key === 'string' && !dangerousKeys.includes(key);
};
 
module.exports = {
    // Clean up memory of dead creeps
    cleanMemory: function () {
        if (!Memory.creeps) return 0;
        let cleaned = 0;
        for (const name in Memory.creeps) {
            // Security: Use isSafeKey and hasOwnProperty to prevent prototype pollution during iteration
            if (
                isSafeKey(name) &&
                Object.prototype.hasOwnProperty.call(Memory.creeps, name) &&
                !Game.creeps[name]
            ) {
                delete Memory.creeps[name];
                cleaned++;
            }
        }
        return cleaned;
    },
 
    // Exported version of isSafeKey
    isSafeKey: isSafeKey,
 
    // Safe memory access with default values
    getRoomMemory: function (roomName, key, defaultValue) {
        // Security: Validate roomName and key to prevent prototype pollution
        if (!isSafeKey(roomName) || !isSafeKey(key)) {
            return defaultValue;
        }
 
        if (!Memory.rooms[roomName]) {
            Memory.rooms[roomName] = {};
        }
 
        if (Memory.rooms[roomName][key] === undefined) {
            Memory.rooms[roomName][key] = defaultValue;
        }
 
        return Memory.rooms[roomName][key];
    },
 
    setRoomMemory: function (roomName, key, value) {
        // Security: Validate roomName and key to prevent prototype pollution
        if (!isSafeKey(roomName) || !isSafeKey(key)) {
            return;
        }
 
        Eif (!Memory.rooms[roomName]) {
            Memory.rooms[roomName] = {};
        }
        Memory.rooms[roomName][key] = value;
    },
 
    clearRoomMemory: function (roomName, key) {
        // Security: Validate roomName and key to prevent prototype pollution
        if (!isSafeKey(roomName) || !isSafeKey(key)) {
            return;
        }
 
        if (Memory.rooms[roomName]) {
            delete Memory.rooms[roomName][key];
        }
    },
 
    // Memoization helper for expensive calculations
    memoize: function (fn, cacheKey, ttl = 100) {
        // Security: Validate cacheKey to prevent prototype pollution
        if (!isSafeKey(cacheKey)) {
            return fn();
        }
 
        if (!Memory.cache) {
            Memory.cache = {};
        }
 
        const cached = Memory.cache[cacheKey];
        if (cached && Game.time - cached.timestamp < ttl) {
            return cached.value;
        }
 
        const result = fn();
        Memory.cache[cacheKey] = {
            value: result,
            timestamp: Game.time,
        };
 
        return result;
    },
 
    // Clean up old cache entries
    cleanCache: function (maxAge = 500) {
        if (!Memory.cache) {
            return;
        }
 
        for (const key in Memory.cache) {
            // Security: Use isSafeKey and hasOwnProperty to prevent prototype pollution during iteration
            if (isSafeKey(key) && Object.prototype.hasOwnProperty.call(Memory.cache, key)) {
                if (Game.time - Memory.cache[key].timestamp > maxAge) {
                    delete Memory.cache[key];
                }
            }
        }
    },
 
    // Initialize creep memory with role defaults
    initCreepMemory: function (creep, role, extraData = {}) {
        if (!creep.memory.role) {
            creep.memory.role = role;
        }
 
        if (!creep.memory.working) {
            creep.memory.working = false;
        }
 
        for (const key in extraData) {
            // Security: Use isSafeKey and hasOwnProperty when merging extraData
            if (
                isSafeKey(key) &&
                Object.prototype.hasOwnProperty.call(extraData, key) &&
                creep.memory[key] === undefined
            ) {
                creep.memory[key] = extraData[key];
            }
        }
    },
 
    // Get creep working state
    updateWorkingState: function (creep) {
        if (!creep.memory.working && creep.store.getFreeCapacity() === 0) {
            creep.memory.working = true;
            return true;
        }
 
        if (creep.memory.working && creep.store.getUsedCapacity() === 0) {
            creep.memory.working = false;
            return false;
        }
 
        return creep.memory.working;
    },
};