diff --git a/.eslintignore b/.eslintignore index 21447ad..b19caf0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,3 @@ -src/__tests__/customStorage/**/*.js +src/__tests__/pluggableStorage/**/*.js lib/ bin/ diff --git a/README.md b/README.md index 64efdae..f63f525 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,9 @@ const settings = { }, // Mandatory: provide a valid Storage wrapper. storage: { - type: 'CUSTOM', + type: 'PLUGGABLE', prefix: 'storagePrefix', - wrapper: customStorage, + wrapper: storageWrapper, }, synchronizerConfigs: { synchronizerMode: 'MODE_RUN_ALL', diff --git a/e2e/synchronizer.test.ts b/e2e/synchronizer.test.ts index 909eb9a..1c9c8fe 100644 --- a/e2e/synchronizer.test.ts +++ b/e2e/synchronizer.test.ts @@ -1,14 +1,14 @@ /* eslint-disable no-magic-numbers */ // @ts-nocheck import { Synchronizer } from '../src/index'; -import { ICustomStorageWrapper } from '@splitsoftware/splitio-commons/src/storages/types'; +import { IPluggableStorageWrapper } from '@splitsoftware/splitio-commons/src/storages/types'; import { PREFIX, REDIS_PREFIX, REDIS_URL, SERVER_MOCK_URL } from './utils/constants'; import runSDKConsumer from './utils/SDKConsumerMode'; import redisAdapterWrapper from './utils/inRedisService'; import { SynchronizerConfigs } from '../src/types'; // @ts-ignore -let _redisServer: ICustomStorageWrapper; +let _redisWrapper: IPluggableStorageWrapper; /** * Function to create a Synchronizer instance/task. @@ -31,7 +31,7 @@ const createSynchronizer = () => { events: SERVER_MOCK_URL, }, storage: { - type: 'CUSTOM_TEST', + type: 'PLUGGABLE_TEST', prefix: PREFIX, // @ts-ignore wrapper: redisAdapterWrapper({ options: { url: REDIS_URL } }), @@ -54,23 +54,23 @@ const _redisStorage = redisAdapterWrapper({ options: { url: REDIS_URL } }); * Function to flush REDIS db from all keys related to e2e tests. */ const flushRedis = async () => { - const keys = await _redisServer.getKeysByPrefix(PREFIX); + const keys = await _redisWrapper.getKeysByPrefix(PREFIX); for (let i = 0; i < keys.length; i++) { - await _redisServer.del(keys[i]); + await _redisWrapper.del(keys[i]); } }; describe('Synchronizer e2e tests', () => { beforeAll(async (done) => { - _redisServer = _redisStorage; - await _redisServer.connect(); + _redisWrapper = _redisStorage; + await _redisWrapper.connect(); await flushRedis(); done(); }); afterAll(async (done) => { - await _redisServer.close(); + await _redisWrapper.disconnect(); done(); }); @@ -85,24 +85,24 @@ describe('Synchronizer e2e tests', () => { }); it('saves [4] Splits as keys in Redis', async () => { - const splits = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); + const splits = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); expect(splits).toHaveLength(4); }); it('saves [2] Segments as keys in Redis', async () => { - const segments = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.segment.*`); - const segmentsRegistered = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.segments.*`); + const segments = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.segment.*`); + const segmentsRegistered = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.segments.*`); expect(segments.filter(x => !x.match(/.till$/))).toHaveLength(2); expect(segmentsRegistered).toHaveLength(1); }); it('saves [2] Traffic Types keys', async () => { - const trafficTypes = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); - const ttAccount = await _redisServer.get(`${REDIS_PREFIX}.trafficType.account`); - const ttTest = await _redisServer.get(`${REDIS_PREFIX}.trafficType.testTT`); - const ttUser = await _redisServer.get(`${REDIS_PREFIX}.trafficType.user`); + const trafficTypes = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); + const ttAccount = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.account`); + const ttTest = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.testTT`); + const ttUser = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.user`); expect(trafficTypes).toHaveLength(2); expect(Number(ttAccount)).toBe(0); @@ -117,13 +117,13 @@ describe('Synchronizer e2e tests', () => { }); it('checks that [4] Impressions saved in Redis', async () => { - const impressions = await _redisServer.popItems(`${REDIS_PREFIX}.impressions`, 100); + const impressions = await _redisWrapper.popItems(`${REDIS_PREFIX}.impressions`, 100); expect(impressions).toHaveLength(4); }); it('checks that [2] Events are saved in Redis', async () => { - const events = await _redisServer.popItems(`${REDIS_PREFIX}.events`, 100); + const events = await _redisWrapper.popItems(`${REDIS_PREFIX}.events`, 100); expect(events).toHaveLength(2); }); @@ -140,16 +140,16 @@ describe('Synchronizer e2e tests', () => { }); it('saves [4] Splits as keys in Redis', async () => { - const splits = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); + const splits = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); expect(splits).toHaveLength(4); }); it('saves [3] Traffic Types keys', async () => { - const trafficTypes = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); - const ttAccount = await _redisServer.get(`${REDIS_PREFIX}.trafficType.account`); - const ttTest = await _redisServer.get(`${REDIS_PREFIX}.trafficType.testTT`); - const ttUser = await _redisServer.get(`${REDIS_PREFIX}.trafficType.user`); + const trafficTypes = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); + const ttAccount = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.account`); + const ttTest = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.testTT`); + const ttUser = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.user`); expect(trafficTypes).toHaveLength(2); expect(Number(ttAccount)).toBe(2); @@ -158,13 +158,13 @@ describe('Synchronizer e2e tests', () => { }); it('checks that [0] Impressions are saved in Redis', async () => { - const impressions = await _redisServer.popItems(`${REDIS_PREFIX}.impressions`, 100); + const impressions = await _redisWrapper.popItems(`${REDIS_PREFIX}.impressions`, 100); expect(impressions).toHaveLength(0); }); it('checks that [0] Events are saved in Redis', async () => { - const events = await _redisServer.popItems(`${REDIS_PREFIX}.events`, 100); + const events = await _redisWrapper.popItems(`${REDIS_PREFIX}.events`, 100); expect(events).toHaveLength(0); }); @@ -188,7 +188,7 @@ describe('Synchronizer e2e tests - InMemoryOperation - only Splits & Segments mo events: SERVER_MOCK_URL, }, storage: { - type: 'CUSTOM_TEST', + type: 'PLUGGABLE_TEST', prefix: PREFIX, // @ts-ignore wrapper: redisAdapterWrapper({ options: { url: REDIS_URL } }), @@ -205,8 +205,8 @@ describe('Synchronizer e2e tests - InMemoryOperation - only Splits & Segments mo beforeAll(async () => { // @ts-ignore - _redisServer = _redisStorage; - await _redisServer.connect(); + _redisWrapper = _redisStorage; + await _redisWrapper.connect(); await flushRedis(); await _synchronizer.initializeStorages(); @@ -219,31 +219,31 @@ describe('Synchronizer e2e tests - InMemoryOperation - only Splits & Segments mo }); it('saves [4] Splits as keys in Redis', async () => { - const splits = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); + const splits = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); // Check changeNumber(...71) expect(splits).toHaveLength(4); }); it('saves new changeNumber value', async () => { - const till = await _redisServer.getByPrefix(`${REDIS_PREFIX}.splits.till`); + const till = await _redisWrapper.getByPrefix(`${REDIS_PREFIX}.splits.till`); expect(Number(till[0])).toBe(1619720346271); }); it('saves [2] Segments as keys in Redis', async () => { - const segments = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.segment.*`); - const segmentsRegistered = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.segments.*`); + const segments = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.segment.*`); + const segmentsRegistered = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.segments.*`); expect(segments.filter(x => !x.match(/.till$/))).toHaveLength(2); expect(segmentsRegistered).toHaveLength(1); }); it('saves [2] Traffic Types keys', async () => { - const trafficTypes = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); - const ttAccount = await _redisServer.get(`${REDIS_PREFIX}.trafficType.account`); - const ttTest = await _redisServer.get(`${REDIS_PREFIX}.trafficType.testTT`); - const ttUser = await _redisServer.get(`${REDIS_PREFIX}.trafficType.user`); + const trafficTypes = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); + const ttAccount = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.account`); + const ttTest = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.testTT`); + const ttUser = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.user`); expect(trafficTypes).toHaveLength(2); expect(Number(ttAccount)).toBe(0); @@ -258,22 +258,22 @@ describe('Synchronizer e2e tests - InMemoryOperation - only Splits & Segments mo }); it('runs again and saves [17] Splits as keys in Redis', async () => { - const splits = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); + const splits = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.split.*`); expect(splits).toHaveLength(4); }); it('saves new changeNumber value', async () => { - const till = await _redisServer.getByPrefix(`${REDIS_PREFIX}.splits.till`); + const till = await _redisWrapper.getByPrefix(`${REDIS_PREFIX}.splits.till`); expect(Number(till[0])).toBe(1619720346272); }); it('updates [4] Traffic Types keys\' values', async () => { - const trafficTypes = await _redisServer.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); - const ttAccount = await _redisServer.get(`${REDIS_PREFIX}.trafficType.account`); - const ttTest = await _redisServer.get(`${REDIS_PREFIX}.trafficType.testTT`); - const ttUser = await _redisServer.get(`${REDIS_PREFIX}.trafficType.user`); + const trafficTypes = await _redisWrapper.getKeysByPrefix(`${REDIS_PREFIX}.trafficType.*`); + const ttAccount = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.account`); + const ttTest = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.testTT`); + const ttUser = await _redisWrapper.get(`${REDIS_PREFIX}.trafficType.user`); expect(trafficTypes).toHaveLength(2); expect(Number(ttAccount)).toBe(2); @@ -293,7 +293,7 @@ describe('Synchronizer - only Splits & Segments mode', () => { beforeAll(async (done) => { _synchronizer = await createSynchronizer(); // @ts-ignore - _synchronizer._settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_SPLIT_SEGMENTS'; + _synchronizer.settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_SPLIT_SEGMENTS'; await _synchronizer.initializeStorages(); await _synchronizer.initializeSynchronizers(); executeSplitsAndSegmentsCallSpy = jest.spyOn(_synchronizer, 'executeSplitsAndSegments'); @@ -301,8 +301,8 @@ describe('Synchronizer - only Splits & Segments mode', () => { await _synchronizer.execute(); // @ts-ignore - _redisServer = _redisStorage; - await _redisServer.connect(); + _redisWrapper = _redisStorage; + await _redisWrapper.connect(); done(); }); @@ -316,7 +316,7 @@ describe('Synchronizer - only Splits & Segments mode', () => { }); afterAll(async (done) => { - await _redisServer.close(); + await _redisWrapper.disconnect(); done(); }); }); @@ -329,7 +329,7 @@ describe('Synchronizer - only Events & Impressions', () => { beforeAll(async (done) => { _synchronizer = await createSynchronizer(); // @ts-ignore - _synchronizer._settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_EVENTS_IMPRESSIONS'; + _synchronizer.settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_EVENTS_IMPRESSIONS'; await _synchronizer.initializeStorages(); await _synchronizer.initializeSynchronizers(); executeSplitsAndSegmentsCallSpy = jest.spyOn(_synchronizer, 'executeSplitsAndSegments'); @@ -337,8 +337,8 @@ describe('Synchronizer - only Events & Impressions', () => { await _synchronizer.execute(); // @ts-ignore - _redisServer = _redisStorage; - await _redisServer.connect(); + _redisWrapper = _redisStorage; + await _redisWrapper.connect(); done(); }); @@ -352,7 +352,7 @@ describe('Synchronizer - only Events & Impressions', () => { }); afterAll(async (done) => { - await _redisServer.close(); + await _redisWrapper.disconnect(); done(); }); }); diff --git a/e2e/utils/inRedisService.ts b/e2e/utils/inRedisService.ts index cc635b5..2920509 100644 --- a/e2e/utils/inRedisService.ts +++ b/e2e/utils/inRedisService.ts @@ -8,7 +8,7 @@ import { InRedisStorageOptions } from '@splitsoftware/splitio-commons/src/storag * Wrapper for our RedisAdapter. * * @param {Object} redisOptions Redis options with the format expected at `settings.storage.options`. - * @returns {import("@splitsoftware/splitio-commons/types/storages/types").ICustomStorageWrapper} Wrapper for IORedis client. + * @returns {import("@splitsoftware/splitio-commons/types/storages/types").IPluggableStorageWrapper} Wrapper for IORedis client. */ function redisAdapterWrapper(redisOptions: InRedisStorageOptions) { @@ -92,7 +92,7 @@ function redisAdapterWrapper(redisOptions: InRedisStorageOptions) { }); }); }, - close() { + disconnect() { return Promise.resolve(redis && redis.disconnect()); // close the connection }, }; diff --git a/package-lock.json b/package-lock.json index 2b53bb8..85af5e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -937,9 +937,9 @@ } }, "@splitsoftware/splitio-commons": { - "version": "1.0.1-rc.1", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-1.0.1-rc.1.tgz", - "integrity": "sha512-X6uRhmp58VG4zcWJk0MYAIWgX1NdP3cRif8Uxo9/yDfmRedkZC733jm+57V3bQbp2WOOT0Nbzh18pyG9ddR12A==", + "version": "1.0.1-rc.2", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-1.0.1-rc.2.tgz", + "integrity": "sha512-Bya3j+m3qMiiu5s7B8cLOi6QyIUUdZDIald9rFvqf7ul/TQWfGI9dpt3EopV3ZYIm6vY0UeDM9SX96AUNyOEtA==", "requires": { "object-assign": "^4.1.1", "tslib": "^2.1.0" diff --git a/package.json b/package.json index df31aaf..6d09e03 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "test": "npm run test:unit && npm run test:e2e" }, "dependencies": { - "@splitsoftware/splitio-commons": "1.0.1-rc.1", + "@splitsoftware/splitio-commons": "1.0.1-rc.2", "dotenv": "^9.0.1", "node-fetch": "^2.6.1", "yargs": "^17.0.1" diff --git a/src/Synchronizer.ts b/src/Synchronizer.ts index 42a4d2a..2c098cf 100644 --- a/src/Synchronizer.ts +++ b/src/Synchronizer.ts @@ -55,11 +55,11 @@ export class Synchronizer { /** * The local reference to the Synchronizer's settings configurations. */ - _settings: ISettings & { synchronizerConfigs: SynchronizerConfigs }; + settings: ISettings & { synchronizerConfigs: SynchronizerConfigs }; /** * The local reference for the Impression Observer. */ - _observer: ImpressionObserver; + _observer: ImpressionObserver; /** * @param {ISettings} config Configuration object used to instantiates the Synchronizer. @@ -72,12 +72,12 @@ export class Synchronizer { throw new Error('Unable to initialize Synchronizer task: invalid APIKEY.'); } - this._settings = settings; + this.settings = settings; /** * The Split's HTTPclient, required to make the requests to the API. */ this._splitApi = splitApiFactory( - this._settings, + this.settings, { getFetch: Synchronizer._getFetch }, ); } @@ -100,12 +100,12 @@ export class Synchronizer { return new Promise((res, rej) => { // @ts-ignore this._storage = SynchronizerStorageFactory( - this._settings, + this.settings, (error) => error ? rej() : res(true) ); return true; }).catch((error) => { - this._settings.log.error(`Error when initializing Storages: ${error}`); + this.settings.log.error(`Error when initializing Storages: ${error}`); return false; }); } @@ -116,51 +116,51 @@ export class Synchronizer { */ initializeSynchronizers(): Promise { // @todo: Add Cli paramater to define impressionsMode. - const countsCache = this._settings.sync.impressionsMode === 'OPTIMIZED' ? + const countsCache = this.settings.sync.impressionsMode === 'OPTIMIZED' ? new ImpressionCountsCacheInMemory() : undefined; try { this._segmentsSynchronizer = new SegmentsSynchronizer( this._splitApi.fetchSegmentChanges, - this._settings, + this.settings, this._storage.segments, ); this._splitsSynchronizer = new SplitsSynchronizer( this._splitApi.fetchSplitChanges, - this._settings, + this.settings, this._storage.splits, this._storage.segments, // @ts-ignore - InMemoryStorageFactory({ log: this._settings.log }), + InMemoryStorageFactory({ log: this.settings.log }), // @ts-ignore - InMemoryStorageFactory({ log: this._settings.log }) + InMemoryStorageFactory({ log: this.settings.log }) ); this._eventsSynchronizer = new EventsSynchronizer( this._splitApi.postEventsBulk, this._storage.events as IEventsCacheAsync, - this._settings.log, - this._settings.synchronizerConfigs.eventsPerPost, - this._settings.synchronizerConfigs.maxRetries, + this.settings.log, + this.settings.synchronizerConfigs.eventsPerPost, + this.settings.synchronizerConfigs.maxRetries, ); this._impressionsSynchronizer = new ImpressionsSynchronizer( this._splitApi.postTestImpressionsBulk, this._storage.impressions as IImpressionsCacheAsync, this._observer, - this._settings.log, - this._settings.synchronizerConfigs.impressionsPerPost, - this._settings.synchronizerConfigs.maxRetries, + this.settings.log, + this.settings.synchronizerConfigs.impressionsPerPost, + this.settings.synchronizerConfigs.maxRetries, countsCache, ); if (countsCache) { this._impressionsCountSynchronizer = new ImpressionsCountSynchronizer( this._splitApi.postTestImpressionsCount, countsCache, - this._settings.log, + this.settings.log, ); } } catch (error) { - this._settings.log.error(`Error when initializing Synchronizer: ${error}`); + this.settings.log.error(`Error when initializing Synchronizer: ${error}`); return Promise.resolve(false); } return Promise.resolve(true); @@ -185,7 +185,7 @@ export class Synchronizer { const isStorageReady = await this.initializeStorages(); if (!isStorageReady) { // @TODO fix message for programmatic API - console.log('Custom Storage not ready. Run the cli with -d option for debugging information.'); + console.log('Pluggable Storage not ready. Run the cli with -d option for debugging information.'); return false; } console.log(' > Storage setup: Ready'); @@ -209,12 +209,14 @@ export class Synchronizer { * @returns {boolean} */ async execute(): Promise { - const mode = this._settings.synchronizerConfigs?.synchronizerMode || 'MODE_RUN_ALL'; + const mode = this.settings.synchronizerConfigs.synchronizerMode || 'MODE_RUN_ALL'; const hasPreExecutionSucceded = await this.preExecute(); if (!hasPreExecutionSucceded) return false; console.log('# Syncronization tasks'); - if (mode === 'MODE_RUN_ALL' || mode === 'MODE_RUN_SPLIT_SEGMENTS') await this.executeSplitsAndSegments(false); + if (mode === 'MODE_RUN_ALL' || mode === 'MODE_RUN_SPLIT_SEGMENTS') { + await this.executeSplitsAndSegments(false); + } if (mode === 'MODE_RUN_ALL' || mode === 'MODE_RUN_EVENTS_IMPRESSIONS') { await this.executeImpressionsAndEvents(false); } @@ -232,7 +234,7 @@ export class Synchronizer { async executeSplitsAndSegments(standalone = true) { if (standalone) await this.preExecute(); - const isSplitsSyncReady = this._settings.synchronizerConfigs.inMemoryOperation ? + const isSplitsSyncReady = this.settings.synchronizerConfigs.inMemoryOperation ? await this._splitsSynchronizer.getSplitChangesInMemory() : await this._splitsSynchronizer.getSplitChanges(); console.log(` > Splits Synchronizer task: ${isSplitsSyncReady ? 'Successful √' : 'Unsuccessful X'}`); diff --git a/src/__tests__/customStorage/InMemoryStorage.js b/src/__tests__/pluggableStorage/InMemoryStorage.js similarity index 98% rename from src/__tests__/customStorage/InMemoryStorage.js rename to src/__tests__/pluggableStorage/InMemoryStorage.js index 497c7c8..8926026 100644 --- a/src/__tests__/customStorage/InMemoryStorage.js +++ b/src/__tests__/pluggableStorage/InMemoryStorage.js @@ -1,2 +1,2 @@ -var e=require("tslib").__spreadArrays;exports.__esModule=!0;exports.inMemoryStorageFactory=void 0;var r={};Object.defineProperty(r,"__esModule",{value:!0});var t=(n=void 0,i=r.endsWith=n,o=r.find=i,s=r.findIndex=o,u=r.forOwn=s,a=r.get=u,f=r.groupBy=a,c=r.isBoolean=f,l=r.isFiniteNumber=c,v=r.isIntegerNumber=l,m=r.isNaNNumber=v,_=r.isObject=m,h=r.isString=_,g=r.merge=h,d=r.shallowClone=g,b=r.startsWith=d,P=r.toNumber=b,N=r.toString=P,A=r.uniq=N,r.uniqAsStrings=A);r.uniqueId=t;var n=function(e,r,t){return void 0===t&&(t=!1),!(!p(e)||!p(r))&&(t&&(e=e.toLowerCase(),r=r.toLowerCase()),e.slice(e.length-r.length)===r)};r.endsWith=n;var i=function(e,r){var t;if(y(e))for(var n=Object.keys(e),i=0;i { @@ -16,7 +16,7 @@ describe('Synchronizer creation and execution', () => { events: 'https://fake.events.split.io/api', }, storage: { - type: 'CUSTOM', + type: 'PLUGGABLE', prefix: 'InMemoryWrapper', wrapper: InMemoryStorage, }, @@ -42,13 +42,13 @@ describe('Synchronizer creation and execution', () => { }); }); - describe('Custom Storage initialization', () => { - it('Instantiates the Synchronizer and [SUCCESSFULLY] initializes Custom Storage', async () => { + describe('Pluggable Storage initialization', () => { + it('Instantiates the Synchronizer and [SUCCESSFULLY] initializes Pluggable Storage', async () => { const synchronizer = new Synchronizer(config); expect(await synchronizer.initializeStorages()).toBe(true); }); - it('Instantiate the Synchronizer and [FAILS] to initialize Custom Storage', async () => { + it('Instantiate the Synchronizer and [FAILS] to initialize Pluggable Storage', async () => { const synchronizer = new Synchronizer({ ...config, storage: { ...config.storage, wrapper: undefined } }); expect(await synchronizer.initializeStorages()).toBe(false); }); @@ -63,7 +63,9 @@ describe('Synchronizer creation and execution', () => { beforeAll(() => { jest.spyOn(synchronizer, 'preExecute').mockImplementation(() => Promise.resolve(true)); jest.spyOn(synchronizer, 'postExecute').mockImplementation(() => Promise.resolve()); + // @ts-ignore executeSplitsAndSegmentsCallSpy = jest.spyOn(synchronizer, 'executeSplitsAndSegments').mockImplementation(() => Promise.resolve()); + // @ts-ignore executeImpressionsAndEventsCallSpy = jest.spyOn(synchronizer, 'executeImpressionsAndEvents').mockImplementation(() => Promise.resolve()); }); @@ -72,7 +74,7 @@ describe('Synchronizer creation and execution', () => { }); it('runs [ALL] Synchronizer tasks.', async () => { - synchronizer._settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_ALL'; + synchronizer.settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_ALL'; await synchronizer.execute(); expect(executeSplitsAndSegmentsCallSpy).toBeCalledTimes(1); @@ -80,7 +82,7 @@ describe('Synchronizer creation and execution', () => { }); it('runs [SPLITS & SEGMENTS] Synchronizer tasks only.', async () => { - synchronizer._settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_SPLIT_SEGMENTS'; + synchronizer.settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_SPLIT_SEGMENTS'; await synchronizer.execute(); expect(executeSplitsAndSegmentsCallSpy).toBeCalledTimes(1); @@ -88,7 +90,7 @@ describe('Synchronizer creation and execution', () => { }); it('runs [EVENTS & IMPRESSIONS] Synchronizer tasks only.', async () => { - synchronizer._settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_EVENTS_IMPRESSIONS'; + synchronizer.settings.synchronizerConfigs.synchronizerMode = 'MODE_RUN_EVENTS_IMPRESSIONS'; await synchronizer.execute(); expect(executeSplitsAndSegmentsCallSpy).toBeCalledTimes(0); diff --git a/src/cli.ts b/src/cli.ts index 0b2da0d..4cc5dbf 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,7 +4,7 @@ import { Synchronizer } from './Synchronizer'; import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import dotenv from 'dotenv'; -import { ICustomStorageWrapper } from '@splitsoftware/splitio-commons/src/storages/types'; +import { IPluggableStorageWrapper } from '@splitsoftware/splitio-commons/src/storages/types'; import { SynchronizerConfigs } from './types'; type CustomModeOption = 'splitsAndSegments' | 'eventsAndImpressions' | undefined; @@ -26,13 +26,13 @@ let _eventsApiUrl: string | undefined; */ let _apikey: string | undefined; /** - * The Custom Storage's prefix. + * The Pluggable Storage prefix. */ let _storagePrefix: string | undefined; /** - * The reference to the provided Storage's path file. + * The reference to the provided Storage path file. */ -let _customStoragePath: string; +let _pluggableStoragePath: string; /** * The reference for the Impressions Mode configuration. */ @@ -44,9 +44,9 @@ let _debug: boolean; /** * The reference to the provided Storage. */ -let customStorage: ICustomStorageWrapper; +let _storageWrapper: IPluggableStorageWrapper; /** - * Object that contains Synchronizer's specific configs. + * Object that contains Synchronizer specific configs. */ const synchronizerConfigs: SynchronizerConfigs = { synchronizerMode: 'MODE_RUN_ALL', @@ -110,7 +110,7 @@ const yargv = yargs(hideBin(argv)) }, p: { alias: 'prefix', - describe: 'Set the Storage\'s prefix', + describe: 'Set the Storage prefix', type: 'string', nargs: 1, }, @@ -177,7 +177,7 @@ const { IMPRESSIONS_PER_POST, } = yargv; -console.log(` > Synchronizer's configs from: ${mode || 'CLI params'}`); +console.log(` > Synchronizer configs from: ${mode || 'CLI params'}`); /** * Function to set the Synchronizer Execution Mode. * @@ -202,7 +202,7 @@ switch (mode) { _sdkApiUrl = API_URL as string; _eventsApiUrl = EVENTS_API_URL as string; _storagePrefix = STORAGE_PREFIX as string; - _customStoragePath = STORAGE_PATH as string; + _pluggableStoragePath = STORAGE_PATH as string; _impressionsMode = IMPRESSIONS_MODE as string; _debug = DEBUG as unknown as boolean; synchronizerConfigs.eventsPerPost = EVENTS_PER_POST as number; @@ -216,7 +216,7 @@ switch (mode) { _sdkApiUrl = env.API_URL; _eventsApiUrl = env.EVENTS_API_URL; _storagePrefix = env.STORAGE_PREFIX as string; - _customStoragePath = env.STORAGE_PATH as string; + _pluggableStoragePath = env.STORAGE_PATH as string; _impressionsMode = env.IMPRESSIONS_MODE as string; _debug = env.DEBUG as unknown as boolean; synchronizerConfigs.eventsPerPost = env.EVENTS_PER_POST as unknown as number; @@ -230,7 +230,7 @@ switch (mode) { _sdkApiUrl = apiUrl as string; _eventsApiUrl = eventsApiUrl as string; _storagePrefix = prefix as string; - _customStoragePath = storage as string; + _pluggableStoragePath = storage as string; _impressionsMode = impressionsMode as string; _debug = debug as boolean; synchronizerConfigs.eventsPerPost = eventsPerPost as number; @@ -242,7 +242,7 @@ switch (mode) { } try { - customStorage = require(`${process.cwd()}/${_customStoragePath}` as string).default; + _storageWrapper = require(`${process.cwd()}/${_pluggableStoragePath}` as string).default; } catch (error) { // @ts-ignore console.log('Error importing Storage: ', error.message); @@ -266,9 +266,9 @@ const settings = { events: _eventsApiUrl, }, storage: { - type: 'CUSTOM', + type: 'PLUGGABLE', prefix: _storagePrefix, - wrapper: customStorage, + wrapper: _storageWrapper, }, sync: { // @ts-ignore diff --git a/src/storages/SynchronizerStorage.ts b/src/storages/SynchronizerStorage.ts index 08cb5c2..0d2d3cd 100644 --- a/src/storages/SynchronizerStorage.ts +++ b/src/storages/SynchronizerStorage.ts @@ -7,7 +7,7 @@ import { IStorageFactoryParams } from '@splitsoftware/splitio-commons/src/storag * Function to create an InMemory Storage instance. * * @param {ISettings} settings The Synchronizer's settings. - * @param {() => void} onReadyCb The callback to execute when instantiating the custom Storage. + * @param {() => void} onReadyCb The callback to execute when instantiating the pluggable storage. * @returns {IStorageAsync} */ export function SynchronizerStorageFactory(settings: ISettings, onReadyCb: IStorageFactoryParams['onReadyCb']) { diff --git a/src/submitters/__tests__/synchronizerImpressionsSubmitter.spec.ts b/src/submitters/__tests__/synchronizerImpressionsSubmitter.spec.ts index 9092510..ea5fea0 100644 --- a/src/submitters/__tests__/synchronizerImpressionsSubmitter.spec.ts +++ b/src/submitters/__tests__/synchronizerImpressionsSubmitter.spec.ts @@ -16,7 +16,7 @@ describe('Impressions Submitter for Lightweight Synchronizer', () => { }; const _fakeLogger = { error: () => {} }; - let observer: ImpressionObserver; + let observer: ImpressionObserver; let countsCache: ImpressionCountsCacheInMemory; let _impressionsSubmiter: { (): any; (): Promise; }; diff --git a/src/submitters/synchronizerImpressionsSubmitter.ts b/src/submitters/synchronizerImpressionsSubmitter.ts index 679c0d7..4c9d760 100644 --- a/src/submitters/synchronizerImpressionsSubmitter.ts +++ b/src/submitters/synchronizerImpressionsSubmitter.ts @@ -64,7 +64,7 @@ export const impressionWithMetadataToImpressionDTO = (storedImpression: StoredIm export function impressionsSubmitterFactory( postImpressionsBulk: IPostTestImpressionsBulk, impressionsCache: IImpressionsCacheAsync, - observer: ImpressionObserver, + observer: ImpressionObserver, logger: ILogger, impressionsPerPost?: number, maxRetries?: number, diff --git a/src/synchronizers/ImpressionsSynchronizer.ts b/src/synchronizers/ImpressionsSynchronizer.ts index 13b10be..9b53066 100644 --- a/src/synchronizers/ImpressionsSynchronizer.ts +++ b/src/synchronizers/ImpressionsSynchronizer.ts @@ -35,7 +35,7 @@ export class ImpressionsSynchronizer { constructor( postImpressionsBulk: IPostTestImpressionsBulk, impressionsStorage: IImpressionsCacheAsync, - observer: ImpressionObserver, + observer: ImpressionObserver, logger: ILogger, impressionsPerPost?: number, maxRetries?: number,