@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Treatment, TreatmentWithConfig } from '../../../../types/splitio';
|
|
1
|
+
import { FallbackTreatmentConfiguration, Treatment, TreatmentWithConfig } from '../../../../types/splitio';
|
|
2
2
|
import { ILogger } from '../../../logger/types';
|
|
3
3
|
import { isObject, isString } from '../../../utils/lang';
|
|
4
4
|
|
|
@@ -7,59 +7,65 @@ enum FallbackDiscardReason {
|
|
|
7
7
|
Treatment = 'Invalid treatment (max 100 chars and must match pattern)',
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const TREATMENT_PATTERN = /^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$/;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return name.length <= 100 && !name.includes(' ');
|
|
16
|
-
}
|
|
12
|
+
export function isValidFlagName(name: string): boolean {
|
|
13
|
+
return name.length <= 100 && !name.includes(' ');
|
|
14
|
+
}
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
export function isValidTreatment(t?: Treatment | TreatmentWithConfig): boolean {
|
|
17
|
+
const treatment = isObject(t) ? (t as TreatmentWithConfig).treatment : t;
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
return FallbacksSanitizer.pattern.test(treatment);
|
|
19
|
+
if (!isString(treatment) || treatment.length > 100) {
|
|
20
|
+
return false;
|
|
25
21
|
}
|
|
22
|
+
return TREATMENT_PATTERN.test(treatment);
|
|
23
|
+
}
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
return treatment;
|
|
25
|
+
function sanitizeGlobal(logger: ILogger, treatment?: Treatment | TreatmentWithConfig): Treatment | TreatmentWithConfig | undefined {
|
|
26
|
+
if (treatment === undefined) return undefined;
|
|
27
|
+
if (!isValidTreatment(treatment)) {
|
|
28
|
+
logger.error(`Fallback treatments - Discarded fallback: ${FallbackDiscardReason.Treatment}`);
|
|
29
|
+
return undefined;
|
|
35
30
|
}
|
|
31
|
+
return treatment;
|
|
32
|
+
}
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
function sanitizeByFlag(
|
|
35
|
+
logger: ILogger,
|
|
36
|
+
byFlagFallbacks?: Record<string, Treatment | TreatmentWithConfig>
|
|
37
|
+
): Record<string, Treatment | TreatmentWithConfig> {
|
|
38
|
+
const sanitizedByFlag: Record<string, Treatment | TreatmentWithConfig> = {};
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
entries.forEach((flag) => {
|
|
45
|
-
const t = byFlagFallbacks[flag];
|
|
46
|
-
if (!this.isValidFlagName(flag)) {
|
|
47
|
-
logger.error(
|
|
48
|
-
`Fallback treatments - Discarded flag '${flag}': ${FallbackDiscardReason.FlagName}`
|
|
49
|
-
);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
40
|
+
if (!isObject(byFlagFallbacks)) return sanitizedByFlag;
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
`Fallback treatments - Discarded treatment for flag '${flag}': ${FallbackDiscardReason.Treatment}`
|
|
56
|
-
);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
42
|
+
Object.keys(byFlagFallbacks!).forEach((flag) => {
|
|
43
|
+
const t = byFlagFallbacks![flag];
|
|
59
44
|
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
if (!isValidFlagName(flag)) {
|
|
46
|
+
logger.error(`Fallback treatments - Discarded flag '${flag}': ${FallbackDiscardReason.FlagName}`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!isValidTreatment(t)) {
|
|
51
|
+
logger.error(`Fallback treatments - Discarded treatment for flag '${flag}': ${FallbackDiscardReason.Treatment}`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
sanitizedByFlag[flag] = t;
|
|
56
|
+
});
|
|
62
57
|
|
|
63
|
-
|
|
58
|
+
return sanitizedByFlag;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function sanitizeFallbacks(logger: ILogger, fallbacks: FallbackTreatmentConfiguration): FallbackTreatmentConfiguration | undefined {
|
|
62
|
+
if (!isObject(fallbacks)) {
|
|
63
|
+
logger.error('Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties');
|
|
64
|
+
return;
|
|
64
65
|
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
global: sanitizeGlobal(logger, fallbacks.global),
|
|
69
|
+
byFlag: sanitizeByFlag(logger, fallbacks.byFlag)
|
|
70
|
+
};
|
|
65
71
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { FallbackTreatmentConfiguration, Treatment, TreatmentWithConfig } from '../../../types/splitio';
|
|
2
|
-
import { FallbacksSanitizer } from './fallbackSanitizer';
|
|
3
2
|
import { CONTROL } from '../../utils/constants';
|
|
4
3
|
import { isString } from '../../utils/lang';
|
|
5
|
-
import { ILogger } from '../../logger/types';
|
|
6
4
|
|
|
7
5
|
export type IFallbackTreatmentsCalculator = {
|
|
8
6
|
resolve(flagName: string, label: string): TreatmentWithConfig & { label: string };
|
|
@@ -13,13 +11,8 @@ export const FALLBACK_PREFIX = 'fallback - ';
|
|
|
13
11
|
export class FallbackTreatmentsCalculator implements IFallbackTreatmentsCalculator {
|
|
14
12
|
private readonly fallbacks: FallbackTreatmentConfiguration;
|
|
15
13
|
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
const sanitizedByFlag = fallbacks?.byFlag ? FallbacksSanitizer.sanitizeByFlag(logger, fallbacks.byFlag) : {};
|
|
19
|
-
this.fallbacks = {
|
|
20
|
-
global: sanitizedGlobal,
|
|
21
|
-
byFlag: sanitizedByFlag
|
|
22
|
-
};
|
|
14
|
+
constructor(fallbacks: FallbackTreatmentConfiguration = {}) {
|
|
15
|
+
this.fallbacks = fallbacks;
|
|
23
16
|
}
|
|
24
17
|
|
|
25
18
|
resolve(flagName: string, label: string): TreatmentWithConfig & { label: string } {
|
package/src/sdkFactory/index.ts
CHANGED
|
@@ -61,7 +61,7 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ISDK | SplitIO.IA
|
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
-
const fallbackTreatmentsCalculator = new FallbackTreatmentsCalculator(settings.
|
|
64
|
+
const fallbackTreatmentsCalculator = new FallbackTreatmentsCalculator(settings.fallbackTreatments);
|
|
65
65
|
|
|
66
66
|
if (initialRolloutPlan) {
|
|
67
67
|
setRolloutPlan(log, initialRolloutPlan, storage as IStorageSync, key && getMatching(key));
|
|
@@ -49,12 +49,10 @@ export abstract class AbstractMySegmentsCacheSync implements ISegmentsCacheSync
|
|
|
49
49
|
* For client-side synchronizer: it resets or updates the cache.
|
|
50
50
|
*/
|
|
51
51
|
resetSegments(segmentsData: MySegmentsData | IMySegmentsResponse): boolean {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
let isDiff = false;
|
|
54
53
|
const { added, removed } = segmentsData as MySegmentsData;
|
|
55
54
|
|
|
56
55
|
if (added && removed) {
|
|
57
|
-
let isDiff = false;
|
|
58
56
|
|
|
59
57
|
added.forEach(segment => {
|
|
60
58
|
isDiff = this.addSegment(segment) || isDiff;
|
|
@@ -63,32 +61,40 @@ export abstract class AbstractMySegmentsCacheSync implements ISegmentsCacheSync
|
|
|
63
61
|
removed.forEach(segment => {
|
|
64
62
|
isDiff = this.removeSegment(segment) || isDiff;
|
|
65
63
|
});
|
|
64
|
+
} else {
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
const names = ((segmentsData as IMySegmentsResponse).k || []).map(s => s.n).sort();
|
|
67
|
+
const storedSegmentKeys = this.getRegisteredSegments().sort();
|
|
69
68
|
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
// Extreme fast => everything is empty
|
|
70
|
+
if (!names.length && !storedSegmentKeys.length) {
|
|
71
|
+
isDiff = false;
|
|
72
|
+
} else {
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
if (!names.length && !storedSegmentKeys.length) return false;
|
|
74
|
+
let index = 0;
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
while (index < names.length && index < storedSegmentKeys.length && names[index] === storedSegmentKeys[index]) index++;
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
// Quick path => no changes
|
|
79
|
+
if (index === names.length && index === storedSegmentKeys.length) {
|
|
80
|
+
isDiff = false;
|
|
81
|
+
} else {
|
|
79
82
|
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
// Slowest path => add and/or remove segments
|
|
84
|
+
for (let removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
|
|
85
|
+
this.removeSegment(storedSegmentKeys[removeIndex]);
|
|
86
|
+
}
|
|
82
87
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
88
|
+
for (let addIndex = index; addIndex < names.length; addIndex++) {
|
|
89
|
+
this.addSegment(names[addIndex]);
|
|
90
|
+
}
|
|
87
91
|
|
|
88
|
-
|
|
89
|
-
|
|
92
|
+
isDiff = true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
90
95
|
}
|
|
91
96
|
|
|
92
|
-
|
|
97
|
+
this.setChangeNumber(segmentsData.cn);
|
|
98
|
+
return isDiff;
|
|
93
99
|
}
|
|
94
100
|
}
|
|
@@ -14,9 +14,10 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
|
|
|
14
14
|
protected abstract setChangeNumber(changeNumber: number): boolean | void
|
|
15
15
|
|
|
16
16
|
update(toAdd: ISplit[], toRemove: ISplit[], changeNumber: number): boolean {
|
|
17
|
+
let updated = toAdd.map(addedFF => this.addSplit(addedFF)).some(result => result);
|
|
18
|
+
updated = toRemove.map(removedFF => this.removeSplit(removedFF.name)).some(result => result) || updated;
|
|
17
19
|
this.setChangeNumber(changeNumber);
|
|
18
|
-
|
|
19
|
-
return toRemove.map(removedFF => this.removeSplit(removedFF.name)).some(result => result) || updated;
|
|
20
|
+
return updated;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
abstract getSplit(name: string): ISplit | null
|
|
@@ -2,7 +2,7 @@ import { ILogger } from '../../logger/types';
|
|
|
2
2
|
import { isNaNNumber } from '../../utils/lang';
|
|
3
3
|
import { AbstractMySegmentsCacheSync } from '../AbstractMySegmentsCacheSync';
|
|
4
4
|
import type { MySegmentsKeyBuilder } from '../KeyBuilderCS';
|
|
5
|
-
import {
|
|
5
|
+
import { DEFINED } from './constants';
|
|
6
6
|
import { StorageAdapter } from '../types';
|
|
7
7
|
|
|
8
8
|
export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
|
|
@@ -16,33 +16,22 @@ export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
|
|
|
16
16
|
this.log = log;
|
|
17
17
|
this.keys = keys;
|
|
18
18
|
this.storage = storage;
|
|
19
|
-
// There is not need to flush segments cache like splits cache, since resetSegments receives the up-to-date list of active segments
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
protected addSegment(name: string): boolean {
|
|
23
22
|
const segmentKey = this.keys.buildSegmentNameKey(name);
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return true;
|
|
29
|
-
} catch (e) {
|
|
30
|
-
this.log.error(LOG_PREFIX + e);
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
24
|
+
if (this.storage.getItem(segmentKey) === DEFINED) return false;
|
|
25
|
+
this.storage.setItem(segmentKey, DEFINED);
|
|
26
|
+
return true;
|
|
33
27
|
}
|
|
34
28
|
|
|
35
29
|
protected removeSegment(name: string): boolean {
|
|
36
30
|
const segmentKey = this.keys.buildSegmentNameKey(name);
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return true;
|
|
42
|
-
} catch (e) {
|
|
43
|
-
this.log.error(LOG_PREFIX + e);
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
32
|
+
if (this.storage.getItem(segmentKey) !== DEFINED) return false;
|
|
33
|
+
this.storage.removeItem(segmentKey);
|
|
34
|
+
return true;
|
|
46
35
|
}
|
|
47
36
|
|
|
48
37
|
isInSegment(name: string): boolean {
|
|
@@ -63,12 +52,8 @@ export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
|
|
|
63
52
|
}
|
|
64
53
|
|
|
65
54
|
protected setChangeNumber(changeNumber?: number) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
else this.storage.removeItem(this.keys.buildTillKey());
|
|
69
|
-
} catch (e) {
|
|
70
|
-
this.log.error(e);
|
|
71
|
-
}
|
|
55
|
+
if (changeNumber) this.storage.setItem(this.keys.buildTillKey(), changeNumber + '');
|
|
56
|
+
else this.storage.removeItem(this.keys.buildTillKey());
|
|
72
57
|
}
|
|
73
58
|
|
|
74
59
|
getChangeNumber() {
|
|
@@ -26,9 +26,10 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
update(toAdd: IRBSegment[], toRemove: IRBSegment[], changeNumber: number): boolean {
|
|
29
|
+
let updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result);
|
|
30
|
+
updated = toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
|
|
29
31
|
this.setChangeNumber(changeNumber);
|
|
30
|
-
|
|
31
|
-
return toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
|
|
32
|
+
return updated;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
private setChangeNumber(changeNumber: number) {
|
|
@@ -48,40 +49,30 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
private add(rbSegment: IRBSegment): boolean {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const previous = rbSegmentFromStorage ? JSON.parse(rbSegmentFromStorage) : null;
|
|
52
|
+
const name = rbSegment.name;
|
|
53
|
+
const rbSegmentKey = this.keys.buildRBSegmentKey(name);
|
|
54
|
+
const rbSegmentFromStorage = this.storage.getItem(rbSegmentKey);
|
|
55
|
+
const previous = rbSegmentFromStorage ? JSON.parse(rbSegmentFromStorage) : null;
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
this.storage.setItem(rbSegmentKey, JSON.stringify(rbSegment));
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
let usesSegmentsDiff = 0;
|
|
60
|
+
if (previous && usesSegments(previous)) usesSegmentsDiff--;
|
|
61
|
+
if (usesSegments(rbSegment)) usesSegmentsDiff++;
|
|
62
|
+
if (usesSegmentsDiff !== 0) this.updateSegmentCount(usesSegmentsDiff);
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
} catch (e) {
|
|
66
|
-
this.log.error(LOG_PREFIX + e);
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
64
|
+
return true;
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
private remove(name: string): boolean {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (!rbSegment) return false;
|
|
68
|
+
const rbSegment = this.get(name);
|
|
69
|
+
if (!rbSegment) return false;
|
|
75
70
|
|
|
76
|
-
|
|
71
|
+
this.storage.removeItem(this.keys.buildRBSegmentKey(name));
|
|
77
72
|
|
|
78
|
-
|
|
73
|
+
if (usesSegments(rbSegment)) this.updateSegmentCount(-1);
|
|
79
74
|
|
|
80
|
-
|
|
81
|
-
} catch (e) {
|
|
82
|
-
this.log.error(LOG_PREFIX + e);
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
75
|
+
return true;
|
|
85
76
|
}
|
|
86
77
|
|
|
87
78
|
private getNames(): string[] {
|
|
@@ -80,44 +80,34 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
addSplit(split: ISplit) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
83
|
+
const name = split.name;
|
|
84
|
+
const splitKey = this.keys.buildSplitKey(name);
|
|
85
|
+
const splitFromStorage = this.storage.getItem(splitKey);
|
|
86
|
+
const previousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : null;
|
|
87
|
+
|
|
88
|
+
if (previousSplit) {
|
|
89
|
+
this._decrementCounts(previousSplit);
|
|
90
|
+
this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
|
|
91
|
+
}
|
|
93
92
|
|
|
94
|
-
|
|
93
|
+
this.storage.setItem(splitKey, JSON.stringify(split));
|
|
95
94
|
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
this._incrementCounts(split);
|
|
96
|
+
this.addToFlagSets(split);
|
|
98
97
|
|
|
99
|
-
|
|
100
|
-
} catch (e) {
|
|
101
|
-
this.log.error(LOG_PREFIX + e);
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
98
|
+
return true;
|
|
104
99
|
}
|
|
105
100
|
|
|
106
101
|
removeSplit(name: string): boolean {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (!split) return false;
|
|
102
|
+
const split = this.getSplit(name);
|
|
103
|
+
if (!split) return false;
|
|
110
104
|
|
|
111
|
-
|
|
105
|
+
this.storage.removeItem(this.keys.buildSplitKey(name));
|
|
112
106
|
|
|
113
|
-
|
|
114
|
-
|
|
107
|
+
this._decrementCounts(split);
|
|
108
|
+
this.removeFromFlagSets(split.name, split.sets);
|
|
115
109
|
|
|
116
|
-
|
|
117
|
-
} catch (e) {
|
|
118
|
-
this.log.error(LOG_PREFIX + e);
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
110
|
+
return true;
|
|
121
111
|
}
|
|
122
112
|
|
|
123
113
|
getSplit(name: string): ISplit | null {
|
|
@@ -206,6 +196,9 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
206
196
|
const flagSetFromStorage = this.storage.getItem(flagSetKey);
|
|
207
197
|
|
|
208
198
|
const flagSetCache = new Set(flagSetFromStorage ? JSON.parse(flagSetFromStorage) : []);
|
|
199
|
+
|
|
200
|
+
if (flagSetCache.has(featureFlag.name)) return;
|
|
201
|
+
|
|
209
202
|
flagSetCache.add(featureFlag.name);
|
|
210
203
|
|
|
211
204
|
this.storage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
|
|
@@ -16,9 +16,10 @@ export class RBSegmentsCacheInMemory implements IRBSegmentsCacheSync {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
update(toAdd: IRBSegment[], toRemove: IRBSegment[], changeNumber: number): boolean {
|
|
19
|
+
let updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result);
|
|
20
|
+
updated = toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
|
|
19
21
|
this.changeNumber = changeNumber;
|
|
20
|
-
|
|
21
|
-
return toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
|
|
22
|
+
return updated;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
private add(rbSegment: IRBSegment): boolean {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ILogger } from '../../logger/types';
|
|
2
2
|
import { isNaNNumber } from '../../utils/lang';
|
|
3
|
-
import { LOG_PREFIX } from '
|
|
3
|
+
import { LOG_PREFIX } from './constants';
|
|
4
4
|
import { KeyBuilderSS } from '../KeyBuilderSS';
|
|
5
5
|
import { ISegmentsCacheAsync } from '../types';
|
|
6
6
|
import type { RedisAdapter } from './RedisAdapter';
|
|
@@ -23,6 +23,8 @@ export function segmentsSyncTaskFactory(
|
|
|
23
23
|
segmentChangesFetcherFactory(fetchSegmentChanges),
|
|
24
24
|
storage.segments,
|
|
25
25
|
readiness,
|
|
26
|
+
settings.startup.requestTimeoutBeforeReady,
|
|
27
|
+
settings.startup.retriesOnFailureBeforeReady,
|
|
26
28
|
),
|
|
27
29
|
settings.scheduler.segmentsRefreshRate,
|
|
28
30
|
'segmentChangesUpdater'
|
|
@@ -66,10 +66,10 @@ export function mySegmentsUpdaterFactory(
|
|
|
66
66
|
new Promise((res) => { updateSegments(segmentsData); res(true); }) :
|
|
67
67
|
// If not provided, fetch mySegments
|
|
68
68
|
mySegmentsFetcher(matchingKey, noCache, till, _promiseDecorator).then(segments => {
|
|
69
|
-
// Only when we have downloaded segments completely, we should not keep retrying anymore
|
|
70
|
-
startingUp = false;
|
|
71
|
-
|
|
72
69
|
updateSegments(segments);
|
|
70
|
+
|
|
71
|
+
// Only when we have downloaded and stored segments completely, we should not keep retrying anymore
|
|
72
|
+
startingUp = false;
|
|
73
73
|
return true;
|
|
74
74
|
});
|
|
75
75
|
|
|
@@ -4,6 +4,7 @@ import { IReadinessManager } from '../../../readiness/types';
|
|
|
4
4
|
import { SDK_SEGMENTS_ARRIVED } from '../../../readiness/constants';
|
|
5
5
|
import { ILogger } from '../../../logger/types';
|
|
6
6
|
import { LOG_PREFIX_INSTANTIATION, LOG_PREFIX_SYNC_SEGMENTS } from '../../../logger/constants';
|
|
7
|
+
import { timeout } from '../../../utils/promise/timeout';
|
|
7
8
|
|
|
8
9
|
type ISegmentChangesUpdater = (fetchOnlyNew?: boolean, segmentName?: string, noCache?: boolean, till?: number) => Promise<boolean>
|
|
9
10
|
|
|
@@ -23,11 +24,18 @@ export function segmentChangesUpdaterFactory(
|
|
|
23
24
|
segmentChangesFetcher: ISegmentChangesFetcher,
|
|
24
25
|
segments: ISegmentsCacheBase,
|
|
25
26
|
readiness?: IReadinessManager,
|
|
27
|
+
requestTimeoutBeforeReady?: number,
|
|
28
|
+
retriesOnFailureBeforeReady?: number,
|
|
26
29
|
): ISegmentChangesUpdater {
|
|
27
30
|
|
|
28
31
|
let readyOnAlreadyExistentState = true;
|
|
29
32
|
|
|
30
|
-
function
|
|
33
|
+
function _promiseDecorator<T>(promise: Promise<T>) {
|
|
34
|
+
if (readyOnAlreadyExistentState && requestTimeoutBeforeReady) promise = timeout(requestTimeoutBeforeReady, promise);
|
|
35
|
+
return promise;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function updateSegment(segmentName: string, noCache?: boolean, till?: number, fetchOnlyNew?: boolean, retries?: number): Promise<boolean> {
|
|
31
39
|
log.debug(`${LOG_PREFIX_SYNC_SEGMENTS}Processing segment ${segmentName}`);
|
|
32
40
|
let sincePromise = Promise.resolve(segments.getChangeNumber(segmentName));
|
|
33
41
|
|
|
@@ -35,13 +43,19 @@ export function segmentChangesUpdaterFactory(
|
|
|
35
43
|
// if fetchOnlyNew flag, avoid processing already fetched segments
|
|
36
44
|
return fetchOnlyNew && since !== undefined ?
|
|
37
45
|
false :
|
|
38
|
-
segmentChangesFetcher(since || -1, segmentName, noCache, till).then((changes) => {
|
|
46
|
+
segmentChangesFetcher(since || -1, segmentName, noCache, till, _promiseDecorator).then((changes) => {
|
|
39
47
|
return Promise.all(changes.map(x => {
|
|
40
48
|
log.debug(`${LOG_PREFIX_SYNC_SEGMENTS}Processing ${segmentName} with till = ${x.till}. Added: ${x.added.length}. Removed: ${x.removed.length}`);
|
|
41
49
|
return segments.update(segmentName, x.added, x.removed, x.till);
|
|
42
50
|
})).then((updates) => {
|
|
43
51
|
return updates.some(update => update);
|
|
44
52
|
});
|
|
53
|
+
}).catch(error => {
|
|
54
|
+
if (retries) {
|
|
55
|
+
log.warn(`${LOG_PREFIX_SYNC_SEGMENTS}Retrying fetch of segment ${segmentName} (attempt #${retries}). Reason: ${error}`);
|
|
56
|
+
return updateSegment(segmentName, noCache, till, fetchOnlyNew, retries - 1);
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
45
59
|
});
|
|
46
60
|
});
|
|
47
61
|
}
|
|
@@ -63,8 +77,7 @@ export function segmentChangesUpdaterFactory(
|
|
|
63
77
|
let segmentsPromise = Promise.resolve(segmentName ? [segmentName] : segments.getRegisteredSegments());
|
|
64
78
|
|
|
65
79
|
return segmentsPromise.then(segmentNames => {
|
|
66
|
-
|
|
67
|
-
const updaters = segmentNames.map(segmentName => updateSegment(segmentName, noCache, till, fetchOnlyNew));
|
|
80
|
+
const updaters = segmentNames.map(segmentName => updateSegment(segmentName, noCache, till, fetchOnlyNew, readyOnAlreadyExistentState ? retriesOnFailureBeforeReady : 0));
|
|
68
81
|
|
|
69
82
|
return Promise.all(updaters).then(shouldUpdateFlags => {
|
|
70
83
|
// if at least one segment fetch succeeded, mark segments ready
|
|
@@ -120,8 +120,8 @@ export function splitChangesUpdaterFactory(
|
|
|
120
120
|
storage: Pick<IStorageBase, 'splits' | 'rbSegments' | 'segments' | 'save'>,
|
|
121
121
|
splitFiltersValidation: ISplitFiltersValidation,
|
|
122
122
|
splitsEventEmitter?: ISplitsEventEmitter,
|
|
123
|
-
requestTimeoutBeforeReady
|
|
124
|
-
retriesOnFailureBeforeReady
|
|
123
|
+
requestTimeoutBeforeReady = 0,
|
|
124
|
+
retriesOnFailureBeforeReady = 0,
|
|
125
125
|
isClientSide?: boolean
|
|
126
126
|
): SplitChangesUpdater {
|
|
127
127
|
const { splits, rbSegments, segments } = storage;
|
|
@@ -163,8 +163,6 @@ export function splitChangesUpdaterFactory(
|
|
|
163
163
|
splitChangesFetcher(since, noCache, till, rbSince, _promiseDecorator)
|
|
164
164
|
)
|
|
165
165
|
.then((splitChanges: ISplitChangesResponse) => {
|
|
166
|
-
startingUp = false;
|
|
167
|
-
|
|
168
166
|
const usedSegments = new Set<string>();
|
|
169
167
|
|
|
170
168
|
let ffUpdate: MaybeThenable<boolean> = false;
|
|
@@ -187,6 +185,8 @@ export function splitChangesUpdaterFactory(
|
|
|
187
185
|
]).then(([ffChanged, rbsChanged]) => {
|
|
188
186
|
if (storage.save) storage.save();
|
|
189
187
|
|
|
188
|
+
startingUp = false;
|
|
189
|
+
|
|
190
190
|
if (splitsEventEmitter) {
|
|
191
191
|
// To emit SDK_SPLITS_ARRIVED for server-side SDK, we must check that all registered segments have been fetched
|
|
192
192
|
return Promise.resolve(!splitsEventEmitter.splitsArrived || ((ffChanged || rbsChanged) && (isClientSide || checkAllSegmentsExist(segments))))
|
|
@@ -201,14 +201,13 @@ export function splitChangesUpdaterFactory(
|
|
|
201
201
|
});
|
|
202
202
|
})
|
|
203
203
|
.catch(error => {
|
|
204
|
-
log.warn(SYNC_SPLITS_FETCH_FAILS, [error]);
|
|
205
|
-
|
|
206
204
|
if (startingUp && retriesOnFailureBeforeReady > retry) {
|
|
207
205
|
retry += 1;
|
|
208
|
-
log.
|
|
206
|
+
log.warn(SYNC_SPLITS_FETCH_RETRY, [retry, error]);
|
|
209
207
|
return _splitChangesUpdater(sinces, retry);
|
|
210
208
|
} else {
|
|
211
209
|
startingUp = false;
|
|
210
|
+
log.warn(SYNC_SPLITS_FETCH_FAILS, [error]);
|
|
212
211
|
}
|
|
213
212
|
return false;
|
|
214
213
|
});
|
|
@@ -8,6 +8,7 @@ import { ISettings } from '../../types';
|
|
|
8
8
|
import { validateKey } from '../inputValidation/key';
|
|
9
9
|
import { ERROR_MIN_CONFIG_PARAM, LOG_PREFIX_CLIENT_INSTANTIATION } from '../../logger/constants';
|
|
10
10
|
import { validateRolloutPlan } from '../../storages/setRolloutPlan';
|
|
11
|
+
import { sanitizeFallbacks } from '../../evaluator/fallbackTreatmentsCalculator/fallbackSanitizer';
|
|
11
12
|
|
|
12
13
|
// Exported for telemetry
|
|
13
14
|
export const base = {
|
|
@@ -207,5 +208,8 @@ export function settingsValidation(config: unknown, validationParams: ISettingsV
|
|
|
207
208
|
// @ts-ignore, modify readonly prop
|
|
208
209
|
withDefaults.userConsent = consent ? consent(withDefaults) : undefined;
|
|
209
210
|
|
|
211
|
+
// @ts-ignore, modify readonly prop
|
|
212
|
+
withDefaults.fallbackTreatments = withDefaults.fallbackTreatments ? sanitizeFallbacks(log, withDefaults.fallbackTreatments) : undefined;
|
|
213
|
+
|
|
210
214
|
return withDefaults;
|
|
211
215
|
}
|
package/types/splitio.d.ts
CHANGED
|
@@ -98,6 +98,8 @@ interface ISharedSettings {
|
|
|
98
98
|
logger?: SplitIO.Logger;
|
|
99
99
|
/**
|
|
100
100
|
* Fallback treatments to be used when the SDK is not ready or the flag is not found.
|
|
101
|
+
*
|
|
102
|
+
* @defaultValue `undefined`
|
|
101
103
|
*/
|
|
102
104
|
fallbackTreatments?: SplitIO.FallbackTreatmentConfiguration;
|
|
103
105
|
}
|
|
@@ -717,7 +719,7 @@ declare namespace SplitIO {
|
|
|
717
719
|
isReadyFromCache: boolean;
|
|
718
720
|
|
|
719
721
|
/**
|
|
720
|
-
* `isTimedout` indicates if the client has triggered an `SDK_READY_TIMED_OUT` event and is not ready
|
|
722
|
+
* `isTimedout` indicates if the client has triggered an `SDK_READY_TIMED_OUT` event and is not ready.
|
|
721
723
|
* In other words, `isTimedout` is equivalent to `hasTimedout && !isReady`.
|
|
722
724
|
*/
|
|
723
725
|
isTimedout: boolean;
|
|
@@ -1307,7 +1309,13 @@ declare namespace SplitIO {
|
|
|
1307
1309
|
* Fallback treatments to be used when the SDK is not ready or the flag is not found.
|
|
1308
1310
|
*/
|
|
1309
1311
|
type FallbackTreatmentConfiguration = {
|
|
1312
|
+
/**
|
|
1313
|
+
* Fallback treatment for all flags.
|
|
1314
|
+
*/
|
|
1310
1315
|
global?: Treatment | TreatmentWithConfig,
|
|
1316
|
+
/**
|
|
1317
|
+
* Fallback treatments for specific flags. It takes precedence over the global fallback treatment.
|
|
1318
|
+
*/
|
|
1311
1319
|
byFlag?: {
|
|
1312
1320
|
[featureFlagName: string]: Treatment | TreatmentWithConfig
|
|
1313
1321
|
}
|
|
@@ -1327,8 +1335,6 @@ declare namespace SplitIO {
|
|
|
1327
1335
|
/**
|
|
1328
1336
|
* Defines the factory function to instantiate the storage. If not provided, the default in-memory storage is used.
|
|
1329
1337
|
*
|
|
1330
|
-
* NOTE: Currently, there is no persistent storage option available for the React Native SDK; only `InLocalStorage` for the Browser SDK.
|
|
1331
|
-
*
|
|
1332
1338
|
* Example:
|
|
1333
1339
|
* ```
|
|
1334
1340
|
* SplitFactory({
|