UNPKG

@splitsoftware/splitio-commons

Version:
96 lines (95 loc) 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateCache = void 0; var lang_1 = require("../../utils/lang"); var KeyBuilder_1 = require("../KeyBuilder"); var constants_1 = require("./constants"); var DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10; var MILLIS_IN_A_DAY = 86400000; /** * Validates if cache should be cleared and sets the cache `hash` if needed. * * @returns `true` if cache should be cleared, `false` otherwise */ function validateExpiration(options, storage, settings, keys, currentTimestamp, isThereCache) { var log = settings.log, initialRolloutPlan = settings.initialRolloutPlan; // Check expiration var lastUpdatedTimestamp = parseInt(storage.getItem(keys.buildLastUpdatedKey()), 10); if (!(0, lang_1.isNaNNumber)(lastUpdatedTimestamp)) { var cacheExpirationInDays = (0, lang_1.isFiniteNumber)(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS; var expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays; if (lastUpdatedTimestamp < expirationTimestamp) { log.info(constants_1.LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache'); return true; } } // Check hash var storageHashKey = keys.buildHashKey(); var storageHash = storage.getItem(storageHashKey); var currentStorageHash = (0, KeyBuilder_1.getStorageHash)(settings); if (storageHash !== currentStorageHash) { try { storage.setItem(storageHashKey, currentStorageHash); } catch (e) { log.error(constants_1.LOG_PREFIX + e); } if (isThereCache && !initialRolloutPlan) { log.info(constants_1.LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache'); return true; } return false; // No cache to clear } // Clear on init if (options.clearOnInit) { var lastClearTimestamp = parseInt(storage.getItem(keys.buildLastClear()), 10); if ((0, lang_1.isNaNNumber)(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) { log.info(constants_1.LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache'); return true; } } } /** * Clean cache if: * - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp` * - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified * - `clearOnInit` was set and cache was not cleared in the last 24 hours * * @returns Metadata object with `initialCacheLoad` (true if is fresh install, false if is ready from cache) and `lastUpdateTimestamp` (timestamp of last cache update or undefined) */ function validateCache(options, storage, settings, keys, definitions, rbSegments, segments, largeSegments) { return Promise.resolve(storage.load && storage.load()).then(function () { var currentTimestamp = Date.now(); var isThereCache = definitions.getChangeNumber() > -1; // Get lastUpdateTimestamp from storage var lastUpdatedTimestampStr = storage.getItem(keys.buildLastUpdatedKey()); var lastUpdatedTimestamp = lastUpdatedTimestampStr ? parseInt(lastUpdatedTimestampStr, 10) : undefined; var lastUpdateTimestamp = (!(0, lang_1.isNaNNumber)(lastUpdatedTimestamp) && lastUpdatedTimestamp !== undefined) ? lastUpdatedTimestamp : undefined; if (validateExpiration(options, storage, settings, keys, currentTimestamp, isThereCache)) { definitions.clear(); rbSegments.clear(); segments.clear(); largeSegments.clear(); // Update last clear timestamp try { storage.setItem(keys.buildLastClear(), currentTimestamp + ''); } catch (e) { settings.log.error(constants_1.LOG_PREFIX + e); } // Persist clear if (storage.save) storage.save(); return { initialCacheLoad: true, lastUpdateTimestamp: undefined }; } // Check if ready from cache return { initialCacheLoad: !isThereCache, lastUpdateTimestamp: lastUpdateTimestamp }; }); } exports.validateCache = validateCache;