@splitsoftware/splitio-commons 2.1.0-rc.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.txt +4 -5
- package/LICENSE +1 -1
- package/cjs/evaluator/index.js +2 -0
- package/cjs/listeners/browser.js +4 -6
- package/cjs/readiness/readinessManager.js +0 -6
- package/cjs/sdkClient/client.js +14 -11
- package/cjs/sdkClient/sdkClient.js +1 -1
- package/cjs/sdkFactory/index.js +9 -14
- package/cjs/sdkManager/index.js +2 -1
- package/cjs/storages/AbstractSplitsCacheAsync.js +7 -0
- package/cjs/storages/AbstractSplitsCacheSync.js +7 -0
- package/cjs/storages/KeyBuilderCS.js +0 -3
- package/cjs/storages/dataLoader.js +2 -3
- package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +57 -1
- package/cjs/storages/inLocalStorage/index.js +7 -8
- package/cjs/storages/inMemory/InMemoryStorage.js +5 -7
- package/cjs/storages/inMemory/InMemoryStorageCS.js +6 -7
- package/cjs/storages/inRedis/constants.js +1 -1
- package/cjs/storages/inRedis/index.js +9 -13
- package/cjs/storages/pluggable/index.js +16 -21
- package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +10 -1
- package/cjs/sync/submitters/impressionCountsSubmitter.js +2 -4
- package/cjs/sync/submitters/submitterManager.js +3 -6
- package/cjs/sync/syncManagerOnline.js +3 -8
- package/cjs/trackers/impressionsTracker.js +17 -18
- package/cjs/trackers/strategy/strategyDebug.js +4 -11
- package/cjs/trackers/strategy/strategyNone.js +11 -16
- package/cjs/trackers/strategy/strategyOptimized.js +11 -21
- package/cjs/trackers/uniqueKeysTracker.js +1 -1
- package/cjs/utils/constants/browser.js +5 -0
- package/cjs/utils/settingsValidation/storage/storageCS.js +1 -1
- package/esm/evaluator/index.js +2 -0
- package/esm/listeners/browser.js +1 -3
- package/esm/readiness/readinessManager.js +0 -6
- package/esm/sdkClient/client.js +14 -11
- package/esm/sdkClient/sdkClient.js +1 -1
- package/esm/sdkFactory/index.js +10 -15
- package/esm/sdkManager/index.js +2 -1
- package/esm/storages/AbstractSplitsCacheAsync.js +7 -0
- package/esm/storages/AbstractSplitsCacheSync.js +7 -0
- package/esm/storages/KeyBuilderCS.js +0 -3
- package/esm/storages/dataLoader.js +1 -2
- package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +57 -1
- package/esm/storages/inLocalStorage/index.js +8 -9
- package/esm/storages/inMemory/InMemoryStorage.js +6 -8
- package/esm/storages/inMemory/InMemoryStorageCS.js +7 -8
- package/esm/storages/inRedis/constants.js +1 -1
- package/esm/storages/inRedis/index.js +10 -14
- package/esm/storages/pluggable/index.js +17 -22
- package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/esm/sync/polling/updaters/splitChangesUpdater.js +11 -2
- package/esm/sync/submitters/impressionCountsSubmitter.js +2 -4
- package/esm/sync/submitters/submitterManager.js +3 -6
- package/esm/sync/syncManagerOnline.js +3 -8
- package/esm/trackers/impressionsTracker.js +17 -18
- package/esm/trackers/strategy/strategyDebug.js +4 -11
- package/esm/trackers/strategy/strategyNone.js +11 -16
- package/esm/trackers/strategy/strategyOptimized.js +11 -21
- package/esm/trackers/uniqueKeysTracker.js +1 -1
- package/esm/utils/constants/browser.js +2 -0
- package/esm/utils/settingsValidation/storage/storageCS.js +1 -1
- package/package.json +1 -1
- package/src/dtos/types.ts +2 -1
- package/src/evaluator/index.ts +2 -0
- package/src/evaluator/types.ts +1 -1
- package/src/listeners/browser.ts +1 -3
- package/src/readiness/readinessManager.ts +0 -5
- package/src/sdkClient/client.ts +19 -15
- package/src/sdkClient/sdkClient.ts +1 -1
- package/src/sdkFactory/index.ts +11 -16
- package/src/sdkFactory/types.ts +1 -1
- package/src/sdkManager/index.ts +2 -1
- package/src/storages/AbstractSplitsCacheAsync.ts +8 -0
- package/src/storages/AbstractSplitsCacheSync.ts +8 -0
- package/src/storages/KeyBuilderCS.ts +0 -4
- package/src/storages/dataLoader.ts +1 -3
- package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +66 -1
- package/src/storages/inLocalStorage/index.ts +13 -12
- package/src/storages/inMemory/InMemoryStorage.ts +6 -6
- package/src/storages/inMemory/InMemoryStorageCS.ts +7 -6
- package/src/storages/inRedis/constants.ts +1 -1
- package/src/storages/inRedis/index.ts +10 -10
- package/src/storages/pluggable/index.ts +17 -22
- package/src/storages/types.ts +6 -3
- package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
- package/src/sync/polling/updaters/splitChangesUpdater.ts +11 -2
- package/src/sync/submitters/impressionCountsSubmitter.ts +2 -4
- package/src/sync/submitters/submitterManager.ts +3 -4
- package/src/sync/submitters/uniqueKeysSubmitter.ts +2 -3
- package/src/sync/syncManagerOnline.ts +3 -9
- package/src/trackers/impressionsTracker.ts +18 -19
- package/src/trackers/strategy/strategyDebug.ts +4 -11
- package/src/trackers/strategy/strategyNone.ts +11 -17
- package/src/trackers/strategy/strategyOptimized.ts +10 -20
- package/src/trackers/types.ts +13 -8
- package/src/trackers/uniqueKeysTracker.ts +1 -1
- package/src/utils/constants/browser.ts +2 -0
- package/src/utils/lang/index.ts +1 -1
- package/src/utils/settingsValidation/storage/storageCS.ts +1 -1
- package/types/splitio.d.ts +5 -25
- package/cjs/storages/inLocalStorage/validateCache.js +0 -79
- package/esm/storages/inLocalStorage/validateCache.js +0 -75
- package/src/storages/inLocalStorage/validateCache.ts +0 -91
|
@@ -10,7 +10,7 @@ const noopFilterAdapter = {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Tracks uniques keys
|
|
14
14
|
* Unique Keys Tracker will be in charge of checking if the MTK was already sent to the BE in the last period
|
|
15
15
|
* or schedule to be sent; if not it will be added in an internal cache and sent in the next post.
|
|
16
16
|
*
|
package/src/utils/lang/index.ts
CHANGED
|
@@ -120,7 +120,7 @@ export function isBoolean(val: any): boolean {
|
|
|
120
120
|
* Unlike `Number.isFinite`, it also tests Number object instances.
|
|
121
121
|
* Unlike global `isFinite`, it returns false if the value is not a number or Number object instance.
|
|
122
122
|
*/
|
|
123
|
-
export function isFiniteNumber(val: any):
|
|
123
|
+
export function isFiniteNumber(val: any): boolean {
|
|
124
124
|
if (val instanceof Number) val = val.valueOf();
|
|
125
125
|
return typeof val === 'number' ?
|
|
126
126
|
Number.isFinite ? Number.isFinite(val) : isFinite(val) :
|
|
@@ -8,7 +8,7 @@ import { IStorageFactoryParams, IStorageSync } from '../../../storages/types';
|
|
|
8
8
|
|
|
9
9
|
export function __InLocalStorageMockFactory(params: IStorageFactoryParams): IStorageSync {
|
|
10
10
|
const result = InMemoryStorageCSFactory(params);
|
|
11
|
-
result.
|
|
11
|
+
result.splits.checkCache = () => true; // to emit SDK_READY_FROM_CACHE
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
14
|
__InLocalStorageMockFactory.type = STORAGE_MEMORY;
|
package/types/splitio.d.ts
CHANGED
|
@@ -862,6 +862,10 @@ declare namespace SplitIO {
|
|
|
862
862
|
* The default treatment of the feature flag.
|
|
863
863
|
*/
|
|
864
864
|
defaultTreatment: string;
|
|
865
|
+
/**
|
|
866
|
+
* Whether the feature flag has impressions tracking disabled or not.
|
|
867
|
+
*/
|
|
868
|
+
impressionsDisabled: boolean;
|
|
865
869
|
};
|
|
866
870
|
/**
|
|
867
871
|
* A promise that resolves to a feature flag view or null if the feature flag is not found.
|
|
@@ -906,18 +910,6 @@ declare namespace SplitIO {
|
|
|
906
910
|
* @defaultValue `'SPLITIO'`
|
|
907
911
|
*/
|
|
908
912
|
prefix?: string;
|
|
909
|
-
/**
|
|
910
|
-
* Number of days before cached data expires if it was not updated. If cache expires, it is cleared on initialization.
|
|
911
|
-
*
|
|
912
|
-
* @defaultValue `10`
|
|
913
|
-
*/
|
|
914
|
-
expirationDays?: number;
|
|
915
|
-
/**
|
|
916
|
-
* Optional settings to clear the cache. If set to `true`, the SDK clears the cached data on initialization, unless the cache was cleared within the last 24 hours.
|
|
917
|
-
*
|
|
918
|
-
* @defaultValue `false`
|
|
919
|
-
*/
|
|
920
|
-
clearOnInit?: boolean;
|
|
921
913
|
}
|
|
922
914
|
/**
|
|
923
915
|
* Storage for asynchronous (consumer) SDK.
|
|
@@ -1241,23 +1233,11 @@ declare namespace SplitIO {
|
|
|
1241
1233
|
*/
|
|
1242
1234
|
type?: BrowserStorage;
|
|
1243
1235
|
/**
|
|
1244
|
-
* Optional prefix to prevent any kind of data collision between SDK versions
|
|
1236
|
+
* Optional prefix to prevent any kind of data collision between SDK versions.
|
|
1245
1237
|
*
|
|
1246
1238
|
* @defaultValue `'SPLITIO'`
|
|
1247
1239
|
*/
|
|
1248
1240
|
prefix?: string;
|
|
1249
|
-
/**
|
|
1250
|
-
* Optional settings for the 'LOCALSTORAGE' storage type. It specifies the number of days before cached data expires if it was not updated. If cache expires, it is cleared on initialization.
|
|
1251
|
-
*
|
|
1252
|
-
* @defaultValue `10`
|
|
1253
|
-
*/
|
|
1254
|
-
expirationDays?: number;
|
|
1255
|
-
/**
|
|
1256
|
-
* Optional settings for the 'LOCALSTORAGE' storage type. If set to `true`, the SDK clears the cached data on initialization, unless the cache was cleared within the last 24 hours.
|
|
1257
|
-
*
|
|
1258
|
-
* @defaultValue `false`
|
|
1259
|
-
*/
|
|
1260
|
-
clearOnInit?: boolean;
|
|
1261
1241
|
};
|
|
1262
1242
|
}
|
|
1263
1243
|
/**
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateCache = void 0;
|
|
4
|
-
var lang_1 = require("../../utils/lang");
|
|
5
|
-
var KeyBuilder_1 = require("../KeyBuilder");
|
|
6
|
-
var constants_1 = require("./constants");
|
|
7
|
-
var DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
|
|
8
|
-
var MILLIS_IN_A_DAY = 86400000;
|
|
9
|
-
/**
|
|
10
|
-
* Validates if cache should be cleared and sets the cache `hash` if needed.
|
|
11
|
-
*
|
|
12
|
-
* @returns `true` if cache should be cleared, `false` otherwise
|
|
13
|
-
*/
|
|
14
|
-
function validateExpiration(options, settings, keys, currentTimestamp, isThereCache) {
|
|
15
|
-
var log = settings.log;
|
|
16
|
-
// Check expiration
|
|
17
|
-
var lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()), 10);
|
|
18
|
-
if (!(0, lang_1.isNaNNumber)(lastUpdatedTimestamp)) {
|
|
19
|
-
var cacheExpirationInDays = (0, lang_1.isFiniteNumber)(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
|
|
20
|
-
var expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
|
|
21
|
-
if (lastUpdatedTimestamp < expirationTimestamp) {
|
|
22
|
-
log.info(constants_1.LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache');
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
// Check hash
|
|
27
|
-
var storageHashKey = keys.buildHashKey();
|
|
28
|
-
var storageHash = localStorage.getItem(storageHashKey);
|
|
29
|
-
var currentStorageHash = (0, KeyBuilder_1.getStorageHash)(settings);
|
|
30
|
-
if (storageHash !== currentStorageHash) {
|
|
31
|
-
try {
|
|
32
|
-
localStorage.setItem(storageHashKey, currentStorageHash);
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
log.error(constants_1.LOG_PREFIX + e);
|
|
36
|
-
}
|
|
37
|
-
if (isThereCache) {
|
|
38
|
-
log.info(constants_1.LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache');
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
return false; // No cache to clear
|
|
42
|
-
}
|
|
43
|
-
// Clear on init
|
|
44
|
-
if (options.clearOnInit) {
|
|
45
|
-
var lastClearTimestamp = parseInt(localStorage.getItem(keys.buildLastClear()), 10);
|
|
46
|
-
if ((0, lang_1.isNaNNumber)(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
|
|
47
|
-
log.info(constants_1.LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache');
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Clean cache if:
|
|
54
|
-
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
|
|
55
|
-
* - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
|
|
56
|
-
* - `clearOnInit` was set and cache was not cleared in the last 24 hours
|
|
57
|
-
*
|
|
58
|
-
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
|
|
59
|
-
*/
|
|
60
|
-
function validateCache(options, settings, keys, splits, segments, largeSegments) {
|
|
61
|
-
var currentTimestamp = Date.now();
|
|
62
|
-
var isThereCache = splits.getChangeNumber() > -1;
|
|
63
|
-
if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
|
|
64
|
-
splits.clear();
|
|
65
|
-
segments.clear();
|
|
66
|
-
largeSegments.clear();
|
|
67
|
-
// Update last clear timestamp
|
|
68
|
-
try {
|
|
69
|
-
localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
|
|
70
|
-
}
|
|
71
|
-
catch (e) {
|
|
72
|
-
settings.log.error(constants_1.LOG_PREFIX + e);
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
// Check if ready from cache
|
|
77
|
-
return isThereCache;
|
|
78
|
-
}
|
|
79
|
-
exports.validateCache = validateCache;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
|
|
2
|
-
import { getStorageHash } from '../KeyBuilder';
|
|
3
|
-
import { LOG_PREFIX } from './constants';
|
|
4
|
-
var DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
|
|
5
|
-
var MILLIS_IN_A_DAY = 86400000;
|
|
6
|
-
/**
|
|
7
|
-
* Validates if cache should be cleared and sets the cache `hash` if needed.
|
|
8
|
-
*
|
|
9
|
-
* @returns `true` if cache should be cleared, `false` otherwise
|
|
10
|
-
*/
|
|
11
|
-
function validateExpiration(options, settings, keys, currentTimestamp, isThereCache) {
|
|
12
|
-
var log = settings.log;
|
|
13
|
-
// Check expiration
|
|
14
|
-
var lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()), 10);
|
|
15
|
-
if (!isNaNNumber(lastUpdatedTimestamp)) {
|
|
16
|
-
var cacheExpirationInDays = isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
|
|
17
|
-
var expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
|
|
18
|
-
if (lastUpdatedTimestamp < expirationTimestamp) {
|
|
19
|
-
log.info(LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache');
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
// Check hash
|
|
24
|
-
var storageHashKey = keys.buildHashKey();
|
|
25
|
-
var storageHash = localStorage.getItem(storageHashKey);
|
|
26
|
-
var currentStorageHash = getStorageHash(settings);
|
|
27
|
-
if (storageHash !== currentStorageHash) {
|
|
28
|
-
try {
|
|
29
|
-
localStorage.setItem(storageHashKey, currentStorageHash);
|
|
30
|
-
}
|
|
31
|
-
catch (e) {
|
|
32
|
-
log.error(LOG_PREFIX + e);
|
|
33
|
-
}
|
|
34
|
-
if (isThereCache) {
|
|
35
|
-
log.info(LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache');
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
return false; // No cache to clear
|
|
39
|
-
}
|
|
40
|
-
// Clear on init
|
|
41
|
-
if (options.clearOnInit) {
|
|
42
|
-
var lastClearTimestamp = parseInt(localStorage.getItem(keys.buildLastClear()), 10);
|
|
43
|
-
if (isNaNNumber(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
|
|
44
|
-
log.info(LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache');
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Clean cache if:
|
|
51
|
-
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
|
|
52
|
-
* - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
|
|
53
|
-
* - `clearOnInit` was set and cache was not cleared in the last 24 hours
|
|
54
|
-
*
|
|
55
|
-
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
|
|
56
|
-
*/
|
|
57
|
-
export function validateCache(options, settings, keys, splits, segments, largeSegments) {
|
|
58
|
-
var currentTimestamp = Date.now();
|
|
59
|
-
var isThereCache = splits.getChangeNumber() > -1;
|
|
60
|
-
if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
|
|
61
|
-
splits.clear();
|
|
62
|
-
segments.clear();
|
|
63
|
-
largeSegments.clear();
|
|
64
|
-
// Update last clear timestamp
|
|
65
|
-
try {
|
|
66
|
-
localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
settings.log.error(LOG_PREFIX + e);
|
|
70
|
-
}
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
// Check if ready from cache
|
|
74
|
-
return isThereCache;
|
|
75
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { ISettings } from '../../types';
|
|
2
|
-
import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
|
|
3
|
-
import { getStorageHash } from '../KeyBuilder';
|
|
4
|
-
import { LOG_PREFIX } from './constants';
|
|
5
|
-
import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
|
|
6
|
-
import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
|
|
7
|
-
import { KeyBuilderCS } from '../KeyBuilderCS';
|
|
8
|
-
import SplitIO from '../../../types/splitio';
|
|
9
|
-
|
|
10
|
-
const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
|
|
11
|
-
const MILLIS_IN_A_DAY = 86400000;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Validates if cache should be cleared and sets the cache `hash` if needed.
|
|
15
|
-
*
|
|
16
|
-
* @returns `true` if cache should be cleared, `false` otherwise
|
|
17
|
-
*/
|
|
18
|
-
function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, currentTimestamp: number, isThereCache: boolean) {
|
|
19
|
-
const { log } = settings;
|
|
20
|
-
|
|
21
|
-
// Check expiration
|
|
22
|
-
const lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()) as string, 10);
|
|
23
|
-
if (!isNaNNumber(lastUpdatedTimestamp)) {
|
|
24
|
-
const cacheExpirationInDays = isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
|
|
25
|
-
const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
|
|
26
|
-
if (lastUpdatedTimestamp < expirationTimestamp) {
|
|
27
|
-
log.info(LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache');
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Check hash
|
|
33
|
-
const storageHashKey = keys.buildHashKey();
|
|
34
|
-
const storageHash = localStorage.getItem(storageHashKey);
|
|
35
|
-
const currentStorageHash = getStorageHash(settings);
|
|
36
|
-
|
|
37
|
-
if (storageHash !== currentStorageHash) {
|
|
38
|
-
try {
|
|
39
|
-
localStorage.setItem(storageHashKey, currentStorageHash);
|
|
40
|
-
} catch (e) {
|
|
41
|
-
log.error(LOG_PREFIX + e);
|
|
42
|
-
}
|
|
43
|
-
if (isThereCache) {
|
|
44
|
-
log.info(LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache');
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return false; // No cache to clear
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Clear on init
|
|
51
|
-
if (options.clearOnInit) {
|
|
52
|
-
const lastClearTimestamp = parseInt(localStorage.getItem(keys.buildLastClear()) as string, 10);
|
|
53
|
-
|
|
54
|
-
if (isNaNNumber(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
|
|
55
|
-
log.info(LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache');
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Clean cache if:
|
|
63
|
-
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
|
|
64
|
-
* - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
|
|
65
|
-
* - `clearOnInit` was set and cache was not cleared in the last 24 hours
|
|
66
|
-
*
|
|
67
|
-
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
|
|
68
|
-
*/
|
|
69
|
-
export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
|
|
70
|
-
|
|
71
|
-
const currentTimestamp = Date.now();
|
|
72
|
-
const isThereCache = splits.getChangeNumber() > -1;
|
|
73
|
-
|
|
74
|
-
if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
|
|
75
|
-
splits.clear();
|
|
76
|
-
segments.clear();
|
|
77
|
-
largeSegments.clear();
|
|
78
|
-
|
|
79
|
-
// Update last clear timestamp
|
|
80
|
-
try {
|
|
81
|
-
localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
|
|
82
|
-
} catch (e) {
|
|
83
|
-
settings.log.error(LOG_PREFIX + e);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Check if ready from cache
|
|
90
|
-
return isThereCache;
|
|
91
|
-
}
|