UNPKG

@splitsoftware/splitio-commons

Version:
50 lines (49 loc) 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateStorageCS = exports.__InLocalStorageMockFactory = void 0; var InMemoryStorageCS_1 = require("../../../storages/inMemory/InMemoryStorageCS"); var constants_1 = require("../../../logger/constants"); var constants_2 = require("../../../utils/constants"); function __InLocalStorageMockFactory(params) { var result = (0, InMemoryStorageCS_1.InMemoryStorageCSFactory)(params); result.validateCache = function () { return true; }; // to emit SDK_READY_FROM_CACHE return result; } exports.__InLocalStorageMockFactory = __InLocalStorageMockFactory; __InLocalStorageMockFactory.type = constants_2.STORAGE_MEMORY; /** * This function validates `settings.storage` object * * @param settings - config object provided by the user to initialize the sdk * * @returns valid storage factory. Default to `InMemoryStorageCSFactory` if the provided storage is invalid or not compatible with the sdk mode if mode is standalone or localhost * * @throws error if mode is consumer and the provided storage is not compatible */ function validateStorageCS(settings) { var _a = settings.storage, storage = _a === void 0 ? InMemoryStorageCS_1.InMemoryStorageCSFactory : _a, log = settings.log, mode = settings.mode; // If an invalid storage is provided, fallback into MEMORY if (typeof storage !== 'function' || [constants_2.STORAGE_MEMORY, constants_2.STORAGE_LOCALSTORAGE, constants_2.STORAGE_PLUGGABLE].indexOf(storage.type) === -1) { storage = InMemoryStorageCS_1.InMemoryStorageCSFactory; log.error(constants_1.ERROR_STORAGE_INVALID); } // In localhost mode with InLocalStorage, fallback to a mock InLocalStorage to emit SDK_READY_FROM_CACHE if (mode === constants_2.LOCALHOST_MODE && storage.type === constants_2.STORAGE_LOCALSTORAGE) { return __InLocalStorageMockFactory; } if ([constants_2.LOCALHOST_MODE, constants_2.STANDALONE_MODE].indexOf(mode) === -1) { // Consumer modes require an async storage if (storage.type !== constants_2.STORAGE_PLUGGABLE) throw new Error('A PluggableStorage instance is required on consumer mode'); } else { // Standalone and localhost modes require a sync storage if (storage.type === constants_2.STORAGE_PLUGGABLE) { storage = InMemoryStorageCS_1.InMemoryStorageCSFactory; log.error(constants_1.ERROR_STORAGE_INVALID, [' It requires consumer mode.']); } } // return default InMemory storage if provided one is not valid return storage; } exports.validateStorageCS = validateStorageCS;