@splitsoftware/splitio-commons 2.1.0-rc.2 → 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 +2 -7
- package/cjs/readiness/readinessManager.js +0 -6
- 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 +3 -5
- package/cjs/storages/inRedis/constants.js +1 -1
- package/cjs/storages/pluggable/index.js +1 -2
- package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +10 -1
- package/cjs/sync/syncManagerOnline.js +3 -8
- 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/readiness/readinessManager.js +0 -6
- 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 +3 -5
- package/esm/storages/inRedis/constants.js +1 -1
- package/esm/storages/pluggable/index.js +1 -2
- package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/esm/sync/polling/updaters/splitChangesUpdater.js +11 -2
- package/esm/sync/syncManagerOnline.js +3 -8
- 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/readiness/readinessManager.ts +0 -5
- 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 +8 -8
- package/src/storages/inRedis/constants.ts +1 -1
- package/src/storages/pluggable/index.ts +1 -2
- package/src/storages/types.ts +4 -1
- package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
- package/src/sync/polling/updaters/splitChangesUpdater.ts +11 -2
- package/src/sync/syncManagerOnline.ts +3 -9
- 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 +1 -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
|
@@ -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
|
-
}
|