UNPKG

@splitsoftware/splitio-commons

Version:
74 lines (73 loc) 4.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InRedisStorage = void 0; var KeyBuilder_1 = require("../KeyBuilder"); var KeyBuilderSS_1 = require("../KeyBuilderSS"); var SplitsCacheInRedis_1 = require("./SplitsCacheInRedis"); var SegmentsCacheInRedis_1 = require("./SegmentsCacheInRedis"); var ImpressionsCacheInRedis_1 = require("./ImpressionsCacheInRedis"); var EventsCacheInRedis_1 = require("./EventsCacheInRedis"); var constants_1 = require("../../utils/constants"); var TelemetryCacheInRedis_1 = require("./TelemetryCacheInRedis"); var UniqueKeysCacheInRedis_1 = require("./UniqueKeysCacheInRedis"); var ImpressionCountsCacheInRedis_1 = require("./ImpressionCountsCacheInRedis"); var utils_1 = require("../utils"); var RBSegmentsCacheInRedis_1 = require("./RBSegmentsCacheInRedis"); var RD; try { // Using `require` to prevent error when bundling or importing the SDK in a .mjs file, since ioredis is a CommonJS module. // Redis storage is not supported with .mjs files. RD = require('./RedisAdapter').RedisAdapter; } catch (error) { /* empty */ } /** * InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.js * @see {@link https://www.npmjs.com/package/ioredis} */ function InRedisStorage(options) { if (options === void 0) { options = {}; } var prefix = (0, KeyBuilder_1.validatePrefix)(options.prefix); function InRedisStorageFactory(params) { if (!RD) throw new Error('The SDK Redis storage is unavailable. Make sure your runtime environment supports CommonJS (`require`) so the `ioredis` dependency can be imported.'); var onReadyFromCacheCb = params.onReadyFromCacheCb, onReadyCb = params.onReadyCb, settings = params.settings, log = params.settings.log; var metadata = (0, utils_1.metadataBuilder)(settings); var keys = new KeyBuilderSS_1.KeyBuilderSS(prefix, metadata); var redisClient = new RD(log, options.options || {}); var telemetry = new TelemetryCacheInRedis_1.TelemetryCacheInRedis(log, keys, redisClient); var impressionCountsCache = new ImpressionCountsCacheInRedis_1.ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient); var uniqueKeysCache = new UniqueKeysCacheInRedis_1.UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient); // RedisAdapter lets queue operations before connected onReadyFromCacheCb(); // Subscription to Redis connect event in order to emit SDK_READY event on consumer mode redisClient.on('connect', function () { onReadyCb(); impressionCountsCache.start(); uniqueKeysCache.start(); // Synchronize config telemetry.recordConfig(); }); return { splits: new SplitsCacheInRedis_1.SplitsCacheInRedis(log, keys, redisClient, settings.sync.__splitFiltersValidation), rbSegments: new RBSegmentsCacheInRedis_1.RBSegmentsCacheInRedis(log, keys, redisClient), segments: new SegmentsCacheInRedis_1.SegmentsCacheInRedis(log, keys, redisClient), impressions: new ImpressionsCacheInRedis_1.ImpressionsCacheInRedis(log, keys.buildImpressionsKey(), redisClient, metadata), impressionCounts: impressionCountsCache, events: new EventsCacheInRedis_1.EventsCacheInRedis(log, keys.buildEventsKey(), redisClient, metadata), telemetry: telemetry, uniqueKeys: uniqueKeysCache, // When using REDIS we should: // 1- Disconnect from the storage destroy: function () { return Promise.all([ impressionCountsCache.stop(), uniqueKeysCache.stop() ]).then(function () { redisClient.disconnect(); }); // @TODO check that caches works as expected when redisClient is disconnected } }; } InRedisStorageFactory.type = constants_1.STORAGE_REDIS; return InRedisStorageFactory; } exports.InRedisStorage = InRedisStorage;