{"version":3,"file":"StorageCache.mjs","sources":["../../../src/Cache/StorageCache.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '../Logger';\nimport { KeyValueStorage } from '../storage/KeyValueStorage';\nimport { getLocalStorageWithFallback } from '../storage/utils';\nimport { defaultConfig } from './constants';\nimport { StorageCacheCommon } from './StorageCacheCommon';\nimport { getCurrentSizeKey, getCurrentTime } from './utils';\nconst logger = new ConsoleLogger('StorageCache');\n/**\n * Customized storage based on the SessionStorage or LocalStorage with LRU implemented\n */\nexport class StorageCache extends StorageCacheCommon {\n    /**\n     * initialize the cache\n     * @param config - the configuration of the cache\n     */\n    constructor(config) {\n        const storage = getLocalStorageWithFallback();\n        super({ config, keyValueStorage: new KeyValueStorage(storage) });\n        this.storage = storage;\n        this.getItem = this.getItem.bind(this);\n        this.setItem = this.setItem.bind(this);\n        this.removeItem = this.removeItem.bind(this);\n    }\n    async getAllCacheKeys(options) {\n        const { omitSizeKey } = options ?? {};\n        const keys = [];\n        for (let i = 0; i < this.storage.length; i++) {\n            const key = this.storage.key(i);\n            if (omitSizeKey && key === getCurrentSizeKey(this.config.keyPrefix)) {\n                continue;\n            }\n            if (key?.startsWith(this.config.keyPrefix)) {\n                keys.push(key.substring(this.config.keyPrefix.length));\n            }\n        }\n        return keys;\n    }\n    /**\n     * Return a new instance of cache with customized configuration.\n     * @param {Object} config - the customized configuration\n     * @return {Object} - the new instance of Cache\n     */\n    createInstance(config) {\n        if (!config.keyPrefix || config.keyPrefix === defaultConfig.keyPrefix) {\n            logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');\n            config.keyPrefix = getCurrentTime.toString();\n        }\n        return new StorageCache(config);\n    }\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAOA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,cAAc,CAAC;AAChD;AACA;AACA;AACO,MAAM,YAAY,SAAS,kBAA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