@splitsoftware/splitio-commons 2.7.9-rc.3 → 2.8.1-rc.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 +5 -1
- package/cjs/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +48 -39
- package/cjs/evaluator/fallbackTreatmentsCalculator/index.js +3 -8
- package/cjs/sdkFactory/index.js +1 -1
- package/cjs/storages/AbstractMySegmentsCacheSync.js +31 -23
- package/cjs/storages/AbstractSplitsCacheSync.js +3 -2
- package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +10 -28
- package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +22 -33
- package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +19 -29
- package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +3 -2
- package/cjs/storages/inRedis/SegmentsCacheInRedis.js +1 -1
- package/cjs/sync/polling/syncTasks/segmentsSyncTask.js +1 -1
- package/cjs/sync/polling/updaters/mySegmentsUpdater.js +2 -2
- package/cjs/sync/polling/updaters/segmentChangesUpdater.js +16 -5
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +3 -3
- package/cjs/utils/settingsValidation/index.js +3 -0
- package/esm/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +44 -38
- package/esm/evaluator/fallbackTreatmentsCalculator/index.js +3 -8
- package/esm/sdkFactory/index.js +1 -1
- package/esm/storages/AbstractMySegmentsCacheSync.js +31 -23
- package/esm/storages/AbstractSplitsCacheSync.js +3 -2
- package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +11 -29
- package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +22 -33
- package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +19 -29
- package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +3 -2
- package/esm/storages/inRedis/SegmentsCacheInRedis.js +1 -1
- package/esm/sync/polling/syncTasks/segmentsSyncTask.js +1 -1
- package/esm/sync/polling/updaters/mySegmentsUpdater.js +2 -2
- package/esm/sync/polling/updaters/segmentChangesUpdater.js +16 -5
- package/esm/sync/polling/updaters/splitChangesUpdater.js +3 -3
- package/esm/utils/settingsValidation/index.js +3 -0
- package/package.json +1 -1
- package/src/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.ts +50 -44
- package/src/evaluator/fallbackTreatmentsCalculator/index.ts +2 -9
- package/src/sdkFactory/index.ts +1 -1
- package/src/storages/AbstractMySegmentsCacheSync.ts +26 -20
- package/src/storages/AbstractSplitsCacheSync.ts +3 -2
- package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +9 -24
- package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +18 -27
- package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +22 -29
- package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +3 -2
- package/src/storages/inRedis/SegmentsCacheInRedis.ts +1 -1
- package/src/sync/polling/syncTasks/segmentsSyncTask.ts +2 -0
- package/src/sync/polling/updaters/mySegmentsUpdater.ts +3 -3
- package/src/sync/polling/updaters/segmentChangesUpdater.ts +17 -4
- package/src/sync/polling/updaters/splitChangesUpdater.ts +6 -7
- package/src/utils/settingsValidation/index.ts +4 -0
- package/types/splitio.d.ts +9 -3
|
@@ -4,45 +4,51 @@ var FallbackDiscardReason;
|
|
|
4
4
|
FallbackDiscardReason["FlagName"] = "Invalid flag name (max 100 chars, no spaces)";
|
|
5
5
|
FallbackDiscardReason["Treatment"] = "Invalid treatment (max 100 chars and must match pattern)";
|
|
6
6
|
})(FallbackDiscardReason || (FallbackDiscardReason = {}));
|
|
7
|
-
var
|
|
8
|
-
|
|
7
|
+
var TREATMENT_PATTERN = /^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$/;
|
|
8
|
+
export function isValidFlagName(name) {
|
|
9
|
+
return name.length <= 100 && !name.includes(' ');
|
|
10
|
+
}
|
|
11
|
+
export function isValidTreatment(t) {
|
|
12
|
+
var treatment = isObject(t) ? t.treatment : t;
|
|
13
|
+
if (!isString(treatment) || treatment.length > 100) {
|
|
14
|
+
return false;
|
|
9
15
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
return TREATMENT_PATTERN.test(treatment);
|
|
17
|
+
}
|
|
18
|
+
function sanitizeGlobal(logger, treatment) {
|
|
19
|
+
if (treatment === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (!isValidTreatment(treatment)) {
|
|
22
|
+
logger.error("Fallback treatments - Discarded fallback: " + FallbackDiscardReason.Treatment);
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return treatment;
|
|
26
|
+
}
|
|
27
|
+
function sanitizeByFlag(logger, byFlagFallbacks) {
|
|
28
|
+
var sanitizedByFlag = {};
|
|
29
|
+
if (!isObject(byFlagFallbacks))
|
|
30
|
+
return sanitizedByFlag;
|
|
31
|
+
Object.keys(byFlagFallbacks).forEach(function (flag) {
|
|
32
|
+
var t = byFlagFallbacks[flag];
|
|
33
|
+
if (!isValidFlagName(flag)) {
|
|
34
|
+
logger.error("Fallback treatments - Discarded flag '" + flag + "': " + FallbackDiscardReason.FlagName);
|
|
35
|
+
return;
|
|
17
36
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (!this.isValidTreatment(treatment)) {
|
|
22
|
-
logger.error("Fallback treatments - Discarded fallback: " + FallbackDiscardReason.Treatment);
|
|
23
|
-
return undefined;
|
|
37
|
+
if (!isValidTreatment(t)) {
|
|
38
|
+
logger.error("Fallback treatments - Discarded treatment for flag '" + flag + "': " + FallbackDiscardReason.Treatment);
|
|
39
|
+
return;
|
|
24
40
|
}
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!_this.isValidTreatment(t)) {
|
|
38
|
-
logger.error("Fallback treatments - Discarded treatment for flag '" + flag + "': " + FallbackDiscardReason.Treatment);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
sanitizedByFlag[flag] = t;
|
|
42
|
-
});
|
|
43
|
-
return sanitizedByFlag;
|
|
41
|
+
sanitizedByFlag[flag] = t;
|
|
42
|
+
});
|
|
43
|
+
return sanitizedByFlag;
|
|
44
|
+
}
|
|
45
|
+
export function sanitizeFallbacks(logger, fallbacks) {
|
|
46
|
+
if (!isObject(fallbacks)) {
|
|
47
|
+
logger.error('Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
global: sanitizeGlobal(logger, fallbacks.global),
|
|
52
|
+
byFlag: sanitizeByFlag(logger, fallbacks.byFlag)
|
|
44
53
|
};
|
|
45
|
-
|
|
46
|
-
return FallbacksSanitizer;
|
|
47
|
-
}());
|
|
48
|
-
export { FallbacksSanitizer };
|
|
54
|
+
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import { FallbacksSanitizer } from './fallbackSanitizer';
|
|
2
1
|
import { CONTROL } from '../../utils/constants';
|
|
3
2
|
import { isString } from '../../utils/lang';
|
|
4
3
|
export var FALLBACK_PREFIX = 'fallback - ';
|
|
5
4
|
var FallbackTreatmentsCalculator = /** @class */ (function () {
|
|
6
|
-
function FallbackTreatmentsCalculator(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.fallbacks = {
|
|
10
|
-
global: sanitizedGlobal,
|
|
11
|
-
byFlag: sanitizedByFlag
|
|
12
|
-
};
|
|
5
|
+
function FallbackTreatmentsCalculator(fallbacks) {
|
|
6
|
+
if (fallbacks === void 0) { fallbacks = {}; }
|
|
7
|
+
this.fallbacks = fallbacks;
|
|
13
8
|
}
|
|
14
9
|
FallbackTreatmentsCalculator.prototype.resolve = function (flagName, label) {
|
|
15
10
|
var _a;
|
package/esm/sdkFactory/index.js
CHANGED
|
@@ -49,7 +49,7 @@ export function sdkFactory(params) {
|
|
|
49
49
|
readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
|
|
50
50
|
}
|
|
51
51
|
});
|
|
52
|
-
var fallbackTreatmentsCalculator = new FallbackTreatmentsCalculator(settings.
|
|
52
|
+
var fallbackTreatmentsCalculator = new FallbackTreatmentsCalculator(settings.fallbackTreatments);
|
|
53
53
|
if (initialRolloutPlan) {
|
|
54
54
|
setRolloutPlan(log, initialRolloutPlan, storage, key && getMatching(key));
|
|
55
55
|
if (storage.splits.getChangeNumber() > -1)
|
|
@@ -20,37 +20,45 @@ var AbstractMySegmentsCacheSync = /** @class */ (function () {
|
|
|
20
20
|
*/
|
|
21
21
|
AbstractMySegmentsCacheSync.prototype.resetSegments = function (segmentsData) {
|
|
22
22
|
var _this = this;
|
|
23
|
-
|
|
23
|
+
var isDiff = false;
|
|
24
24
|
var _a = segmentsData, added = _a.added, removed = _a.removed;
|
|
25
25
|
if (added && removed) {
|
|
26
|
-
var isDiff_1 = false;
|
|
27
26
|
added.forEach(function (segment) {
|
|
28
|
-
|
|
27
|
+
isDiff = _this.addSegment(segment) || isDiff;
|
|
29
28
|
});
|
|
30
29
|
removed.forEach(function (segment) {
|
|
31
|
-
|
|
30
|
+
isDiff = _this.removeSegment(segment) || isDiff;
|
|
32
31
|
});
|
|
33
|
-
return isDiff_1;
|
|
34
|
-
}
|
|
35
|
-
var names = (segmentsData.k || []).map(function (s) { return s.n; }).sort();
|
|
36
|
-
var storedSegmentKeys = this.getRegisteredSegments().sort();
|
|
37
|
-
// Extreme fast => everything is empty
|
|
38
|
-
if (!names.length && !storedSegmentKeys.length)
|
|
39
|
-
return false;
|
|
40
|
-
var index = 0;
|
|
41
|
-
while (index < names.length && index < storedSegmentKeys.length && names[index] === storedSegmentKeys[index])
|
|
42
|
-
index++;
|
|
43
|
-
// Quick path => no changes
|
|
44
|
-
if (index === names.length && index === storedSegmentKeys.length)
|
|
45
|
-
return false;
|
|
46
|
-
// Slowest path => add and/or remove segments
|
|
47
|
-
for (var removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
|
|
48
|
-
this.removeSegment(storedSegmentKeys[removeIndex]);
|
|
49
32
|
}
|
|
50
|
-
|
|
51
|
-
|
|
33
|
+
else {
|
|
34
|
+
var names = (segmentsData.k || []).map(function (s) { return s.n; }).sort();
|
|
35
|
+
var storedSegmentKeys = this.getRegisteredSegments().sort();
|
|
36
|
+
// Extreme fast => everything is empty
|
|
37
|
+
if (!names.length && !storedSegmentKeys.length) {
|
|
38
|
+
isDiff = false;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
var index = 0;
|
|
42
|
+
while (index < names.length && index < storedSegmentKeys.length && names[index] === storedSegmentKeys[index])
|
|
43
|
+
index++;
|
|
44
|
+
// Quick path => no changes
|
|
45
|
+
if (index === names.length && index === storedSegmentKeys.length) {
|
|
46
|
+
isDiff = false;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Slowest path => add and/or remove segments
|
|
50
|
+
for (var removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
|
|
51
|
+
this.removeSegment(storedSegmentKeys[removeIndex]);
|
|
52
|
+
}
|
|
53
|
+
for (var addIndex = index; addIndex < names.length; addIndex++) {
|
|
54
|
+
this.addSegment(names[addIndex]);
|
|
55
|
+
}
|
|
56
|
+
isDiff = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
52
59
|
}
|
|
53
|
-
|
|
60
|
+
this.setChangeNumber(segmentsData.cn);
|
|
61
|
+
return isDiff;
|
|
54
62
|
};
|
|
55
63
|
return AbstractMySegmentsCacheSync;
|
|
56
64
|
}());
|
|
@@ -9,9 +9,10 @@ var AbstractSplitsCacheSync = /** @class */ (function () {
|
|
|
9
9
|
}
|
|
10
10
|
AbstractSplitsCacheSync.prototype.update = function (toAdd, toRemove, changeNumber) {
|
|
11
11
|
var _this = this;
|
|
12
|
-
this.setChangeNumber(changeNumber);
|
|
13
12
|
var updated = toAdd.map(function (addedFF) { return _this.addSplit(addedFF); }).some(function (result) { return result; });
|
|
14
|
-
|
|
13
|
+
updated = toRemove.map(function (removedFF) { return _this.removeSplit(removedFF.name); }).some(function (result) { return result; }) || updated;
|
|
14
|
+
this.setChangeNumber(changeNumber);
|
|
15
|
+
return updated;
|
|
15
16
|
};
|
|
16
17
|
AbstractSplitsCacheSync.prototype.getSplits = function (names) {
|
|
17
18
|
var _this = this;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __extends } from "tslib";
|
|
2
2
|
import { isNaNNumber } from '../../utils/lang';
|
|
3
3
|
import { AbstractMySegmentsCacheSync } from '../AbstractMySegmentsCacheSync';
|
|
4
|
-
import {
|
|
4
|
+
import { DEFINED } from './constants';
|
|
5
5
|
var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
6
6
|
__extends(MySegmentsCacheInLocal, _super);
|
|
7
7
|
function MySegmentsCacheInLocal(log, keys, storage) {
|
|
@@ -10,33 +10,20 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
10
10
|
_this.keys = keys;
|
|
11
11
|
_this.storage = storage;
|
|
12
12
|
return _this;
|
|
13
|
-
// There is not need to flush segments cache like splits cache, since resetSegments receives the up-to-date list of active segments
|
|
14
13
|
}
|
|
15
14
|
MySegmentsCacheInLocal.prototype.addSegment = function (name) {
|
|
16
15
|
var segmentKey = this.keys.buildSegmentNameKey(name);
|
|
17
|
-
|
|
18
|
-
if (this.storage.getItem(segmentKey) === DEFINED)
|
|
19
|
-
return false;
|
|
20
|
-
this.storage.setItem(segmentKey, DEFINED);
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
this.log.error(LOG_PREFIX + e);
|
|
16
|
+
if (this.storage.getItem(segmentKey) === DEFINED)
|
|
25
17
|
return false;
|
|
26
|
-
|
|
18
|
+
this.storage.setItem(segmentKey, DEFINED);
|
|
19
|
+
return true;
|
|
27
20
|
};
|
|
28
21
|
MySegmentsCacheInLocal.prototype.removeSegment = function (name) {
|
|
29
22
|
var segmentKey = this.keys.buildSegmentNameKey(name);
|
|
30
|
-
|
|
31
|
-
if (this.storage.getItem(segmentKey) !== DEFINED)
|
|
32
|
-
return false;
|
|
33
|
-
this.storage.removeItem(segmentKey);
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
this.log.error(LOG_PREFIX + e);
|
|
23
|
+
if (this.storage.getItem(segmentKey) !== DEFINED)
|
|
38
24
|
return false;
|
|
39
|
-
|
|
25
|
+
this.storage.removeItem(segmentKey);
|
|
26
|
+
return true;
|
|
40
27
|
};
|
|
41
28
|
MySegmentsCacheInLocal.prototype.isInSegment = function (name) {
|
|
42
29
|
return this.storage.getItem(this.keys.buildSegmentNameKey(name)) === DEFINED;
|
|
@@ -54,15 +41,10 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
54
41
|
return 1;
|
|
55
42
|
};
|
|
56
43
|
MySegmentsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.storage.removeItem(this.keys.buildTillKey());
|
|
62
|
-
}
|
|
63
|
-
catch (e) {
|
|
64
|
-
this.log.error(e);
|
|
65
|
-
}
|
|
44
|
+
if (changeNumber)
|
|
45
|
+
this.storage.setItem(this.keys.buildTillKey(), changeNumber + '');
|
|
46
|
+
else
|
|
47
|
+
this.storage.removeItem(this.keys.buildTillKey());
|
|
66
48
|
};
|
|
67
49
|
MySegmentsCacheInLocal.prototype.getChangeNumber = function () {
|
|
68
50
|
var n = -1;
|
|
@@ -15,9 +15,10 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
15
15
|
};
|
|
16
16
|
RBSegmentsCacheInLocal.prototype.update = function (toAdd, toRemove, changeNumber) {
|
|
17
17
|
var _this = this;
|
|
18
|
-
this.setChangeNumber(changeNumber);
|
|
19
18
|
var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
|
|
20
|
-
|
|
19
|
+
updated = toRemove.map(function (toRemove) { return _this.remove(toRemove.name); }).some(function (result) { return result; }) || updated;
|
|
20
|
+
this.setChangeNumber(changeNumber);
|
|
21
|
+
return updated;
|
|
21
22
|
};
|
|
22
23
|
RBSegmentsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
|
|
23
24
|
try {
|
|
@@ -37,40 +38,28 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
37
38
|
this.storage.removeItem(segmentsCountKey);
|
|
38
39
|
};
|
|
39
40
|
RBSegmentsCacheInLocal.prototype.add = function (rbSegment) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
catch (e) {
|
|
56
|
-
this.log.error(LOG_PREFIX + e);
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
41
|
+
var name = rbSegment.name;
|
|
42
|
+
var rbSegmentKey = this.keys.buildRBSegmentKey(name);
|
|
43
|
+
var rbSegmentFromStorage = this.storage.getItem(rbSegmentKey);
|
|
44
|
+
var previous = rbSegmentFromStorage ? JSON.parse(rbSegmentFromStorage) : null;
|
|
45
|
+
this.storage.setItem(rbSegmentKey, JSON.stringify(rbSegment));
|
|
46
|
+
var usesSegmentsDiff = 0;
|
|
47
|
+
if (previous && usesSegments(previous))
|
|
48
|
+
usesSegmentsDiff--;
|
|
49
|
+
if (usesSegments(rbSegment))
|
|
50
|
+
usesSegmentsDiff++;
|
|
51
|
+
if (usesSegmentsDiff !== 0)
|
|
52
|
+
this.updateSegmentCount(usesSegmentsDiff);
|
|
53
|
+
return true;
|
|
59
54
|
};
|
|
60
55
|
RBSegmentsCacheInLocal.prototype.remove = function (name) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (!rbSegment)
|
|
64
|
-
return false;
|
|
65
|
-
this.storage.removeItem(this.keys.buildRBSegmentKey(name));
|
|
66
|
-
if (usesSegments(rbSegment))
|
|
67
|
-
this.updateSegmentCount(-1);
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
catch (e) {
|
|
71
|
-
this.log.error(LOG_PREFIX + e);
|
|
56
|
+
var rbSegment = this.get(name);
|
|
57
|
+
if (!rbSegment)
|
|
72
58
|
return false;
|
|
73
|
-
|
|
59
|
+
this.storage.removeItem(this.keys.buildRBSegmentKey(name));
|
|
60
|
+
if (usesSegments(rbSegment))
|
|
61
|
+
this.updateSegmentCount(-1);
|
|
62
|
+
return true;
|
|
74
63
|
};
|
|
75
64
|
RBSegmentsCacheInLocal.prototype.getNames = function () {
|
|
76
65
|
var len = this.storage.length;
|
|
@@ -67,39 +67,27 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
67
67
|
this.hasSync = false;
|
|
68
68
|
};
|
|
69
69
|
SplitsCacheInLocal.prototype.addSplit = function (split) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
|
|
78
|
-
}
|
|
79
|
-
this.storage.setItem(splitKey, JSON.stringify(split));
|
|
80
|
-
this._incrementCounts(split);
|
|
81
|
-
this.addToFlagSets(split);
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
catch (e) {
|
|
85
|
-
this.log.error(LOG_PREFIX + e);
|
|
86
|
-
return false;
|
|
70
|
+
var name = split.name;
|
|
71
|
+
var splitKey = this.keys.buildSplitKey(name);
|
|
72
|
+
var splitFromStorage = this.storage.getItem(splitKey);
|
|
73
|
+
var previousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : null;
|
|
74
|
+
if (previousSplit) {
|
|
75
|
+
this._decrementCounts(previousSplit);
|
|
76
|
+
this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
|
|
87
77
|
}
|
|
78
|
+
this.storage.setItem(splitKey, JSON.stringify(split));
|
|
79
|
+
this._incrementCounts(split);
|
|
80
|
+
this.addToFlagSets(split);
|
|
81
|
+
return true;
|
|
88
82
|
};
|
|
89
83
|
SplitsCacheInLocal.prototype.removeSplit = function (name) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (!split)
|
|
93
|
-
return false;
|
|
94
|
-
this.storage.removeItem(this.keys.buildSplitKey(name));
|
|
95
|
-
this._decrementCounts(split);
|
|
96
|
-
this.removeFromFlagSets(split.name, split.sets);
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
catch (e) {
|
|
100
|
-
this.log.error(LOG_PREFIX + e);
|
|
84
|
+
var split = this.getSplit(name);
|
|
85
|
+
if (!split)
|
|
101
86
|
return false;
|
|
102
|
-
|
|
87
|
+
this.storage.removeItem(this.keys.buildSplitKey(name));
|
|
88
|
+
this._decrementCounts(split);
|
|
89
|
+
this.removeFromFlagSets(split.name, split.sets);
|
|
90
|
+
return true;
|
|
103
91
|
};
|
|
104
92
|
SplitsCacheInLocal.prototype.getSplit = function (name) {
|
|
105
93
|
var item = this.storage.getItem(this.keys.buildSplitKey(name));
|
|
@@ -171,6 +159,8 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
171
159
|
var flagSetKey = _this.keys.buildFlagSetKey(featureFlagSet);
|
|
172
160
|
var flagSetFromStorage = _this.storage.getItem(flagSetKey);
|
|
173
161
|
var flagSetCache = new Set(flagSetFromStorage ? JSON.parse(flagSetFromStorage) : []);
|
|
162
|
+
if (flagSetCache.has(featureFlag.name))
|
|
163
|
+
return;
|
|
174
164
|
flagSetCache.add(featureFlag.name);
|
|
175
165
|
_this.storage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
|
|
176
166
|
});
|
|
@@ -13,9 +13,10 @@ var RBSegmentsCacheInMemory = /** @class */ (function () {
|
|
|
13
13
|
};
|
|
14
14
|
RBSegmentsCacheInMemory.prototype.update = function (toAdd, toRemove, changeNumber) {
|
|
15
15
|
var _this = this;
|
|
16
|
-
this.changeNumber = changeNumber;
|
|
17
16
|
var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
|
|
18
|
-
|
|
17
|
+
updated = toRemove.map(function (toRemove) { return _this.remove(toRemove.name); }).some(function (result) { return result; }) || updated;
|
|
18
|
+
this.changeNumber = changeNumber;
|
|
19
|
+
return updated;
|
|
19
20
|
};
|
|
20
21
|
RBSegmentsCacheInMemory.prototype.add = function (rbSegment) {
|
|
21
22
|
var name = rbSegment.name;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isNaNNumber } from '../../utils/lang';
|
|
2
|
-
import { LOG_PREFIX } from '
|
|
2
|
+
import { LOG_PREFIX } from './constants';
|
|
3
3
|
var SegmentsCacheInRedis = /** @class */ (function () {
|
|
4
4
|
function SegmentsCacheInRedis(log, keys, redis) {
|
|
5
5
|
this.log = log;
|
|
@@ -5,5 +5,5 @@ import { segmentChangesUpdaterFactory } from '../updaters/segmentChangesUpdater'
|
|
|
5
5
|
* Creates a sync task that periodically executes a `segmentChangesUpdater` task
|
|
6
6
|
*/
|
|
7
7
|
export function segmentsSyncTaskFactory(fetchSegmentChanges, storage, readiness, settings) {
|
|
8
|
-
return syncTaskFactory(settings.log, segmentChangesUpdaterFactory(settings.log, segmentChangesFetcherFactory(fetchSegmentChanges), storage.segments, readiness), settings.scheduler.segmentsRefreshRate, 'segmentChangesUpdater');
|
|
8
|
+
return syncTaskFactory(settings.log, segmentChangesUpdaterFactory(settings.log, segmentChangesFetcherFactory(fetchSegmentChanges), storage.segments, readiness, settings.startup.requestTimeoutBeforeReady, settings.startup.retriesOnFailureBeforeReady), settings.scheduler.segmentsRefreshRate, 'segmentChangesUpdater');
|
|
9
9
|
}
|
|
@@ -45,9 +45,9 @@ export function mySegmentsUpdaterFactory(log, mySegmentsFetcher, storage, segmen
|
|
|
45
45
|
new Promise(function (res) { updateSegments(segmentsData); res(true); }) :
|
|
46
46
|
// If not provided, fetch mySegments
|
|
47
47
|
mySegmentsFetcher(matchingKey, noCache, till, _promiseDecorator).then(function (segments) {
|
|
48
|
-
// Only when we have downloaded segments completely, we should not keep retrying anymore
|
|
49
|
-
startingUp = false;
|
|
50
48
|
updateSegments(segments);
|
|
49
|
+
// Only when we have downloaded and stored segments completely, we should not keep retrying anymore
|
|
50
|
+
startingUp = false;
|
|
51
51
|
return true;
|
|
52
52
|
});
|
|
53
53
|
return updaterPromise.catch(function (error) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SDK_SEGMENTS_ARRIVED } from '../../../readiness/constants';
|
|
2
2
|
import { LOG_PREFIX_INSTANTIATION, LOG_PREFIX_SYNC_SEGMENTS } from '../../../logger/constants';
|
|
3
|
+
import { timeout } from '../../../utils/promise/timeout';
|
|
3
4
|
/**
|
|
4
5
|
* Factory of SegmentChanges updater, a task that:
|
|
5
6
|
* - fetches segment changes using `segmentChangesFetcher`
|
|
@@ -11,22 +12,33 @@ import { LOG_PREFIX_INSTANTIATION, LOG_PREFIX_SYNC_SEGMENTS } from '../../../log
|
|
|
11
12
|
* @param segments - segments storage, with sync or async methods
|
|
12
13
|
* @param readiness - optional readiness manager. Not required for synchronizer or producer mode.
|
|
13
14
|
*/
|
|
14
|
-
export function segmentChangesUpdaterFactory(log, segmentChangesFetcher, segments, readiness) {
|
|
15
|
+
export function segmentChangesUpdaterFactory(log, segmentChangesFetcher, segments, readiness, requestTimeoutBeforeReady, retriesOnFailureBeforeReady) {
|
|
15
16
|
var readyOnAlreadyExistentState = true;
|
|
16
|
-
function
|
|
17
|
+
function _promiseDecorator(promise) {
|
|
18
|
+
if (readyOnAlreadyExistentState && requestTimeoutBeforeReady)
|
|
19
|
+
promise = timeout(requestTimeoutBeforeReady, promise);
|
|
20
|
+
return promise;
|
|
21
|
+
}
|
|
22
|
+
function updateSegment(segmentName, noCache, till, fetchOnlyNew, retries) {
|
|
17
23
|
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processing segment " + segmentName);
|
|
18
24
|
var sincePromise = Promise.resolve(segments.getChangeNumber(segmentName));
|
|
19
25
|
return sincePromise.then(function (since) {
|
|
20
26
|
// if fetchOnlyNew flag, avoid processing already fetched segments
|
|
21
27
|
return fetchOnlyNew && since !== undefined ?
|
|
22
28
|
false :
|
|
23
|
-
segmentChangesFetcher(since || -1, segmentName, noCache, till).then(function (changes) {
|
|
29
|
+
segmentChangesFetcher(since || -1, segmentName, noCache, till, _promiseDecorator).then(function (changes) {
|
|
24
30
|
return Promise.all(changes.map(function (x) {
|
|
25
31
|
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processing " + segmentName + " with till = " + x.till + ". Added: " + x.added.length + ". Removed: " + x.removed.length);
|
|
26
32
|
return segments.update(segmentName, x.added, x.removed, x.till);
|
|
27
33
|
})).then(function (updates) {
|
|
28
34
|
return updates.some(function (update) { return update; });
|
|
29
35
|
});
|
|
36
|
+
}).catch(function (error) {
|
|
37
|
+
if (retries) {
|
|
38
|
+
log.warn(LOG_PREFIX_SYNC_SEGMENTS + "Retrying fetch of segment " + segmentName + " (attempt #" + retries + "). Reason: " + error);
|
|
39
|
+
return updateSegment(segmentName, noCache, till, fetchOnlyNew, retries - 1);
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
30
42
|
});
|
|
31
43
|
});
|
|
32
44
|
}
|
|
@@ -46,8 +58,7 @@ export function segmentChangesUpdaterFactory(log, segmentChangesFetcher, segment
|
|
|
46
58
|
// If not a segment name provided, read list of available segments names to be updated.
|
|
47
59
|
var segmentsPromise = Promise.resolve(segmentName ? [segmentName] : segments.getRegisteredSegments());
|
|
48
60
|
return segmentsPromise.then(function (segmentNames) {
|
|
49
|
-
|
|
50
|
-
var updaters = segmentNames.map(function (segmentName) { return updateSegment(segmentName, noCache, till, fetchOnlyNew); });
|
|
61
|
+
var updaters = segmentNames.map(function (segmentName) { return updateSegment(segmentName, noCache, till, fetchOnlyNew, readyOnAlreadyExistentState ? retriesOnFailureBeforeReady : 0); });
|
|
51
62
|
return Promise.all(updaters).then(function (shouldUpdateFlags) {
|
|
52
63
|
// if at least one segment fetch succeeded, mark segments ready
|
|
53
64
|
if (shouldUpdateFlags.some(function (update) { return update; }) || readyOnAlreadyExistentState) {
|
|
@@ -130,7 +130,6 @@ export function splitChangesUpdaterFactory(log, splitChangesFetcher, storage, sp
|
|
|
130
130
|
{ rbs: { d: [instantUpdate.payload], t: instantUpdate.changeNumber } } :
|
|
131
131
|
splitChangesFetcher(since, noCache, till, rbSince, _promiseDecorator))
|
|
132
132
|
.then(function (splitChanges) {
|
|
133
|
-
startingUp = false;
|
|
134
133
|
var usedSegments = new Set();
|
|
135
134
|
var ffUpdate = false;
|
|
136
135
|
if (splitChanges.ff) {
|
|
@@ -151,6 +150,7 @@ export function splitChangesUpdaterFactory(log, splitChangesFetcher, storage, sp
|
|
|
151
150
|
var ffChanged = _a[0], rbsChanged = _a[1];
|
|
152
151
|
if (storage.save)
|
|
153
152
|
storage.save();
|
|
153
|
+
startingUp = false;
|
|
154
154
|
if (splitsEventEmitter) {
|
|
155
155
|
// To emit SDK_SPLITS_ARRIVED for server-side SDK, we must check that all registered segments have been fetched
|
|
156
156
|
return Promise.resolve(!splitsEventEmitter.splitsArrived || ((ffChanged || rbsChanged) && (isClientSide || checkAllSegmentsExist(segments))))
|
|
@@ -166,14 +166,14 @@ export function splitChangesUpdaterFactory(log, splitChangesFetcher, storage, sp
|
|
|
166
166
|
});
|
|
167
167
|
})
|
|
168
168
|
.catch(function (error) {
|
|
169
|
-
log.warn(SYNC_SPLITS_FETCH_FAILS, [error]);
|
|
170
169
|
if (startingUp && retriesOnFailureBeforeReady > retry) {
|
|
171
170
|
retry += 1;
|
|
172
|
-
log.
|
|
171
|
+
log.warn(SYNC_SPLITS_FETCH_RETRY, [retry, error]);
|
|
173
172
|
return _splitChangesUpdater(sinces, retry);
|
|
174
173
|
}
|
|
175
174
|
else {
|
|
176
175
|
startingUp = false;
|
|
176
|
+
log.warn(SYNC_SPLITS_FETCH_FAILS, [error]);
|
|
177
177
|
}
|
|
178
178
|
return false;
|
|
179
179
|
});
|
|
@@ -6,6 +6,7 @@ import { validImpressionsMode } from './impressionsMode';
|
|
|
6
6
|
import { validateKey } from '../inputValidation/key';
|
|
7
7
|
import { ERROR_MIN_CONFIG_PARAM, LOG_PREFIX_CLIENT_INSTANTIATION } from '../../logger/constants';
|
|
8
8
|
import { validateRolloutPlan } from '../../storages/setRolloutPlan';
|
|
9
|
+
import { sanitizeFallbacks } from '../../evaluator/fallbackTreatmentsCalculator/fallbackSanitizer';
|
|
9
10
|
// Exported for telemetry
|
|
10
11
|
export var base = {
|
|
11
12
|
// Define which kind of object you want to retrieve from SplitFactory
|
|
@@ -180,5 +181,7 @@ export function settingsValidation(config, validationParams) {
|
|
|
180
181
|
// ensure a valid user consent value
|
|
181
182
|
// @ts-ignore, modify readonly prop
|
|
182
183
|
withDefaults.userConsent = consent ? consent(withDefaults) : undefined;
|
|
184
|
+
// @ts-ignore, modify readonly prop
|
|
185
|
+
withDefaults.fallbackTreatments = withDefaults.fallbackTreatments ? sanitizeFallbacks(log, withDefaults.fallbackTreatments) : undefined;
|
|
183
186
|
return withDefaults;
|
|
184
187
|
}
|