UNPKG

@splitsoftware/splitio-commons

Version:
95 lines (94 loc) 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InLocalStorage = void 0; var ImpressionsCacheInMemory_1 = require("../inMemory/ImpressionsCacheInMemory"); var ImpressionCountsCacheInMemory_1 = require("../inMemory/ImpressionCountsCacheInMemory"); var EventsCacheInMemory_1 = require("../inMemory/EventsCacheInMemory"); var KeyBuilder_1 = require("../KeyBuilder"); var KeyBuilderCS_1 = require("../KeyBuilderCS"); var isLocalStorageAvailable_1 = require("../../utils/env/isLocalStorageAvailable"); var DefinitionsCacheInLocal_1 = require("./DefinitionsCacheInLocal"); var RBSegmentsCacheInLocal_1 = require("./RBSegmentsCacheInLocal"); var MySegmentsCacheInLocal_1 = require("./MySegmentsCacheInLocal"); var InMemoryStorageCS_1 = require("../inMemory/InMemoryStorageCS"); var constants_1 = require("./constants"); var constants_2 = require("../../utils/constants"); var TelemetryCacheInMemory_1 = require("../inMemory/TelemetryCacheInMemory"); var UniqueKeysCacheInMemoryCS_1 = require("../inMemory/UniqueKeysCacheInMemoryCS"); var key_1 = require("../../utils/key"); var validateCache_1 = require("./validateCache"); var storageAdapter_1 = require("./storageAdapter"); function validateStorage(log, prefix, wrapper) { if (wrapper) { if ((0, isLocalStorageAvailable_1.isValidStorageWrapper)(wrapper)) { return (0, isLocalStorageAvailable_1.isWebStorage)(wrapper) ? wrapper : // localStorage and sessionStorage don't need adapter (0, storageAdapter_1.storageAdapter)(log, prefix, wrapper); } log.warn(constants_1.LOG_PREFIX + 'Invalid storage provided. Falling back to LocalStorage API'); } if ((0, isLocalStorageAvailable_1.isLocalStorageAvailable)()) return localStorage; log.warn(constants_1.LOG_PREFIX + 'LocalStorage API is unavailable. Falling back to default MEMORY storage'); } /** * InLocal storage factory for standalone client-side SplitFactory */ function InLocalStorage(options) { if (options === void 0) { options = {}; } var prefix = (0, KeyBuilder_1.validatePrefix)(options.prefix); function InLocalStorageCSFactory(params) { var settings = params.settings, _a = params.settings, log = _a.log, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize; var storage = validateStorage(log, prefix, options.wrapper); if (!storage) return (0, InMemoryStorageCS_1.InMemoryStorageCSFactory)(params); var matchingKey = (0, key_1.getMatching)(settings.core.key); var keys = new KeyBuilderCS_1.KeyBuilderCS(prefix, matchingKey); var definitions = new DefinitionsCacheInLocal_1.DefinitionsCacheInLocal(settings, keys, storage); var rbSegments = new RBSegmentsCacheInLocal_1.RBSegmentsCacheInLocal(settings, keys, storage); var segments = new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, keys, storage); var largeSegments = new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, (0, KeyBuilderCS_1.myLargeSegmentsKeyBuilder)(prefix, matchingKey), storage); var validateCachePromise; return { definitions: definitions, rbSegments: rbSegments, segments: segments, largeSegments: largeSegments, impressions: new ImpressionsCacheInMemory_1.ImpressionsCacheInMemory(impressionsQueueSize), impressionCounts: new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory(), events: new EventsCacheInMemory_1.EventsCacheInMemory(eventsQueueSize), telemetry: (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)(params) ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory(definitions, segments) : undefined, uniqueKeys: new UniqueKeysCacheInMemoryCS_1.UniqueKeysCacheInMemoryCS(), validateCache: function () { if (!validateCachePromise) { validateCachePromise = (0, validateCache_1.validateCache)(options, storage, settings, keys, definitions, rbSegments, segments, largeSegments); } return validateCachePromise; }, save: function () { return storage.save && storage.save(); }, destroy: function () { return storage.whenSaved && storage.whenSaved(); }, // When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key). shared: function (matchingKey) { return { definitions: this.definitions, rbSegments: this.rbSegments, segments: new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, new KeyBuilderCS_1.KeyBuilderCS(prefix, matchingKey), storage), largeSegments: new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, (0, KeyBuilderCS_1.myLargeSegmentsKeyBuilder)(prefix, matchingKey), storage), impressions: this.impressions, impressionCounts: this.impressionCounts, events: this.events, telemetry: this.telemetry, uniqueKeys: this.uniqueKeys, destroy: function () { } }; }, }; } InLocalStorageCSFactory.type = constants_2.STORAGE_LOCALSTORAGE; return InLocalStorageCSFactory; } exports.InLocalStorage = InLocalStorage;