@splitsoftware/splitio-commons 2.1.0 → 2.1.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.
Files changed (154) hide show
  1. package/README.md +1 -0
  2. package/cjs/evaluator/combiners/and.js +2 -6
  3. package/cjs/evaluator/combiners/ifelseif.js +6 -6
  4. package/cjs/evaluator/condition/index.js +6 -5
  5. package/cjs/evaluator/index.js +7 -7
  6. package/cjs/evaluator/matchers/index.js +3 -1
  7. package/cjs/evaluator/matchers/matcherTypes.js +1 -0
  8. package/cjs/evaluator/matchers/rbsegment.js +43 -0
  9. package/cjs/evaluator/matchersTransform/index.js +4 -0
  10. package/cjs/evaluator/parser/index.js +2 -2
  11. package/cjs/evaluator/value/sanitize.js +1 -0
  12. package/cjs/logger/constants.js +5 -6
  13. package/cjs/logger/messages/debug.js +3 -4
  14. package/cjs/logger/messages/warn.js +1 -1
  15. package/cjs/services/splitApi.js +2 -2
  16. package/cjs/storages/AbstractSplitsCacheAsync.js +12 -1
  17. package/cjs/storages/AbstractSplitsCacheSync.js +10 -9
  18. package/cjs/storages/KeyBuilder.js +8 -15
  19. package/cjs/storages/KeyBuilderCS.js +12 -3
  20. package/cjs/storages/KeyBuilderSS.js +3 -0
  21. package/cjs/storages/dataLoader.js +1 -2
  22. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +117 -0
  23. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +14 -16
  24. package/cjs/storages/inLocalStorage/index.js +4 -0
  25. package/cjs/storages/inMemory/InMemoryStorage.js +3 -0
  26. package/cjs/storages/inMemory/InMemoryStorageCS.js +4 -0
  27. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +61 -0
  28. package/cjs/storages/inMemory/SplitsCacheInMemory.js +24 -31
  29. package/cjs/storages/inRedis/RBSegmentsCacheInRedis.js +64 -0
  30. package/cjs/storages/inRedis/SplitsCacheInRedis.js +4 -21
  31. package/cjs/storages/inRedis/index.js +2 -0
  32. package/cjs/storages/pluggable/RBSegmentsCachePluggable.js +64 -0
  33. package/cjs/storages/pluggable/SplitsCachePluggable.js +2 -19
  34. package/cjs/storages/pluggable/index.js +2 -0
  35. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +12 -13
  36. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  37. package/cjs/sync/polling/pollingManagerCS.js +7 -7
  38. package/cjs/sync/polling/syncTasks/splitsSyncTask.js +1 -1
  39. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  40. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +1 -1
  41. package/cjs/sync/polling/updaters/splitChangesUpdater.js +53 -51
  42. package/cjs/sync/streaming/SSEHandler/index.js +1 -0
  43. package/cjs/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +106 -77
  44. package/cjs/sync/streaming/constants.js +2 -1
  45. package/cjs/sync/streaming/pushManager.js +3 -16
  46. package/cjs/sync/syncManagerOnline.js +2 -2
  47. package/cjs/utils/constants/index.js +3 -2
  48. package/esm/evaluator/combiners/and.js +2 -6
  49. package/esm/evaluator/combiners/ifelseif.js +7 -7
  50. package/esm/evaluator/condition/index.js +6 -5
  51. package/esm/evaluator/index.js +7 -7
  52. package/esm/evaluator/matchers/index.js +3 -1
  53. package/esm/evaluator/matchers/matcherTypes.js +1 -0
  54. package/esm/evaluator/matchers/rbsegment.js +39 -0
  55. package/esm/evaluator/matchersTransform/index.js +4 -0
  56. package/esm/evaluator/parser/index.js +2 -2
  57. package/esm/evaluator/value/sanitize.js +1 -0
  58. package/esm/logger/constants.js +2 -3
  59. package/esm/logger/messages/debug.js +3 -4
  60. package/esm/logger/messages/warn.js +1 -1
  61. package/esm/services/splitApi.js +2 -2
  62. package/esm/storages/AbstractSplitsCacheAsync.js +12 -1
  63. package/esm/storages/AbstractSplitsCacheSync.js +10 -9
  64. package/esm/storages/KeyBuilder.js +8 -15
  65. package/esm/storages/KeyBuilderCS.js +12 -3
  66. package/esm/storages/KeyBuilderSS.js +3 -0
  67. package/esm/storages/dataLoader.js +1 -2
  68. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +114 -0
  69. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +14 -16
  70. package/esm/storages/inLocalStorage/index.js +4 -0
  71. package/esm/storages/inMemory/InMemoryStorage.js +3 -0
  72. package/esm/storages/inMemory/InMemoryStorageCS.js +4 -0
  73. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +58 -0
  74. package/esm/storages/inMemory/SplitsCacheInMemory.js +24 -31
  75. package/esm/storages/inRedis/RBSegmentsCacheInRedis.js +61 -0
  76. package/esm/storages/inRedis/SplitsCacheInRedis.js +4 -21
  77. package/esm/storages/inRedis/index.js +2 -0
  78. package/esm/storages/pluggable/RBSegmentsCachePluggable.js +61 -0
  79. package/esm/storages/pluggable/SplitsCachePluggable.js +2 -19
  80. package/esm/storages/pluggable/index.js +2 -0
  81. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +12 -13
  82. package/esm/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  83. package/esm/sync/polling/pollingManagerCS.js +7 -7
  84. package/esm/sync/polling/syncTasks/splitsSyncTask.js +1 -1
  85. package/esm/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  86. package/esm/sync/polling/updaters/segmentChangesUpdater.js +1 -1
  87. package/esm/sync/polling/updaters/splitChangesUpdater.js +53 -51
  88. package/esm/sync/streaming/SSEHandler/index.js +2 -1
  89. package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +102 -73
  90. package/esm/sync/streaming/constants.js +1 -0
  91. package/esm/sync/streaming/pushManager.js +6 -19
  92. package/esm/sync/syncManagerOnline.js +2 -2
  93. package/esm/utils/constants/index.js +2 -1
  94. package/package.json +1 -1
  95. package/src/dtos/types.ts +32 -8
  96. package/src/evaluator/Engine.ts +1 -1
  97. package/src/evaluator/combiners/and.ts +5 -4
  98. package/src/evaluator/combiners/ifelseif.ts +7 -9
  99. package/src/evaluator/condition/engineUtils.ts +1 -1
  100. package/src/evaluator/condition/index.ts +12 -12
  101. package/src/evaluator/index.ts +7 -7
  102. package/src/evaluator/matchers/index.ts +3 -1
  103. package/src/evaluator/matchers/matcherTypes.ts +1 -0
  104. package/src/evaluator/matchers/rbsegment.ts +61 -0
  105. package/src/evaluator/matchersTransform/index.ts +3 -0
  106. package/src/evaluator/parser/index.ts +3 -3
  107. package/src/evaluator/types.ts +2 -2
  108. package/src/evaluator/value/index.ts +2 -2
  109. package/src/evaluator/value/sanitize.ts +5 -4
  110. package/src/logger/constants.ts +2 -3
  111. package/src/logger/messages/debug.ts +3 -4
  112. package/src/logger/messages/warn.ts +1 -1
  113. package/src/sdkManager/index.ts +1 -1
  114. package/src/services/splitApi.ts +2 -2
  115. package/src/services/types.ts +1 -1
  116. package/src/storages/AbstractSplitsCacheAsync.ts +15 -5
  117. package/src/storages/AbstractSplitsCacheSync.ts +14 -15
  118. package/src/storages/KeyBuilder.ts +9 -17
  119. package/src/storages/KeyBuilderCS.ts +15 -4
  120. package/src/storages/KeyBuilderSS.ts +4 -0
  121. package/src/storages/dataLoader.ts +1 -2
  122. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +136 -0
  123. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +15 -16
  124. package/src/storages/inLocalStorage/index.ts +4 -0
  125. package/src/storages/inMemory/InMemoryStorage.ts +3 -0
  126. package/src/storages/inMemory/InMemoryStorageCS.ts +4 -0
  127. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +68 -0
  128. package/src/storages/inMemory/SplitsCacheInMemory.ts +22 -27
  129. package/src/storages/inRedis/RBSegmentsCacheInRedis.ts +79 -0
  130. package/src/storages/inRedis/SplitsCacheInRedis.ts +4 -21
  131. package/src/storages/inRedis/index.ts +2 -0
  132. package/src/storages/pluggable/RBSegmentsCachePluggable.ts +76 -0
  133. package/src/storages/pluggable/SplitsCachePluggable.ts +2 -19
  134. package/src/storages/pluggable/index.ts +2 -0
  135. package/src/storages/types.ts +43 -17
  136. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +14 -15
  137. package/src/sync/polling/fetchers/splitChangesFetcher.ts +2 -1
  138. package/src/sync/polling/fetchers/types.ts +1 -0
  139. package/src/sync/polling/pollingManagerCS.ts +7 -7
  140. package/src/sync/polling/syncTasks/splitsSyncTask.ts +1 -2
  141. package/src/sync/polling/types.ts +2 -2
  142. package/src/sync/polling/updaters/mySegmentsUpdater.ts +2 -2
  143. package/src/sync/polling/updaters/segmentChangesUpdater.ts +1 -1
  144. package/src/sync/polling/updaters/splitChangesUpdater.ts +64 -62
  145. package/src/sync/streaming/SSEHandler/index.ts +2 -1
  146. package/src/sync/streaming/SSEHandler/types.ts +2 -2
  147. package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +98 -68
  148. package/src/sync/streaming/constants.ts +1 -0
  149. package/src/sync/streaming/parseUtils.ts +2 -2
  150. package/src/sync/streaming/pushManager.ts +6 -18
  151. package/src/sync/streaming/types.ts +3 -2
  152. package/src/sync/syncManagerOnline.ts +2 -2
  153. package/src/utils/constants/index.ts +2 -1
  154. package/src/utils/lang/index.ts +1 -1
@@ -0,0 +1,61 @@
1
+ import { IRBSegment, MaybeThenable } from '../../dtos/types';
2
+ import { IStorageAsync, IStorageSync } from '../../storages/types';
3
+ import { ILogger } from '../../logger/types';
4
+ import { IDependencyMatcherValue, ISplitEvaluator } from '../types';
5
+ import { thenable } from '../../utils/promise/thenable';
6
+ import { getMatching, keyParser } from '../../utils/key';
7
+ import { parser } from '../parser';
8
+
9
+
10
+ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: IStorageSync | IStorageAsync, log: ILogger) {
11
+
12
+ return function ruleBasedSegmentMatcher({ key, attributes }: IDependencyMatcherValue, splitEvaluator: ISplitEvaluator): MaybeThenable<boolean> {
13
+
14
+ function matchConditions(rbsegment: IRBSegment) {
15
+ const conditions = rbsegment.conditions;
16
+ const evaluator = parser(log, conditions, storage);
17
+
18
+ const evaluation = evaluator(
19
+ keyParser(key),
20
+ undefined,
21
+ undefined,
22
+ undefined,
23
+ attributes,
24
+ splitEvaluator
25
+ );
26
+
27
+ return thenable(evaluation) ?
28
+ evaluation.then(evaluation => evaluation ? true : false) :
29
+ evaluation ? true : false;
30
+ }
31
+
32
+ function isExcluded(rbSegment: IRBSegment) {
33
+ const matchingKey = getMatching(key);
34
+
35
+ if (rbSegment.excluded.keys.indexOf(matchingKey) !== -1) return true;
36
+
37
+ const isInSegment = rbSegment.excluded.segments.map(segmentName => {
38
+ return storage.segments.isInSegment(segmentName, matchingKey);
39
+ });
40
+
41
+ return isInSegment.length && thenable(isInSegment[0]) ?
42
+ Promise.all(isInSegment).then(results => results.some(result => result)) :
43
+ isInSegment.some(result => result);
44
+ }
45
+
46
+ function isInSegment(rbSegment: IRBSegment | null) {
47
+ if (!rbSegment) return false;
48
+ const excluded = isExcluded(rbSegment);
49
+
50
+ return thenable(excluded) ?
51
+ excluded.then(excluded => excluded ? false : matchConditions(rbSegment)) :
52
+ excluded ? false : matchConditions(rbSegment);
53
+ }
54
+
55
+ const rbSegment = storage.rbSegments.get(segmentName);
56
+
57
+ return thenable(rbSegment) ?
58
+ rbSegment.then(isInSegment) :
59
+ isInSegment(rbSegment);
60
+ };
61
+ }
@@ -95,6 +95,9 @@ export function matchersTransform(matchers: ISplitMatcher[]): IMatcherDto[] {
95
95
  type === matcherTypes.LESS_THAN_OR_EQUAL_TO_SEMVER
96
96
  ) {
97
97
  value = stringMatcherData;
98
+ } else if (type === matcherTypes.IN_RULE_BASED_SEGMENT) {
99
+ value = segmentTransform(userDefinedSegmentMatcherData as IInSegmentMatcherData);
100
+ dataType = matcherDataTypes.NOT_SPECIFIED;
98
101
  }
99
102
 
100
103
  return {
@@ -37,7 +37,7 @@ export function parser(log: ILogger, conditions: ISplitCondition[], storage: ISt
37
37
  }
38
38
 
39
39
  // Evaluator function.
40
- return (key: string, attributes: SplitIO.Attributes | undefined, splitEvaluator: ISplitEvaluator) => {
40
+ return (key: SplitIO.SplitKey, attributes?: SplitIO.Attributes, splitEvaluator?: ISplitEvaluator) => {
41
41
  const value = sanitizeValue(log, key, matcherDto, attributes);
42
42
  let result: MaybeThenable<boolean> = false;
43
43
 
@@ -71,12 +71,12 @@ export function parser(log: ILogger, conditions: ISplitCondition[], storage: ISt
71
71
  predicates.push(conditionContext(
72
72
  log,
73
73
  andCombinerContext(log, expressions),
74
- Treatments.parse(partitions),
74
+ partitions && Treatments.parse(partitions),
75
75
  label,
76
76
  conditionType
77
77
  ));
78
78
  }
79
79
 
80
- // Instanciate evaluator given the set of conditions using if else if logic
80
+ // Instantiate evaluator given the set of conditions using if else if logic
81
81
  return ifElseIfCombinerContext(log, predicates);
82
82
  }
@@ -29,6 +29,6 @@ export type IEvaluationResult = IEvaluation & { treatment: string; impressionsDi
29
29
 
30
30
  export type ISplitEvaluator = (log: ILogger, key: SplitIO.SplitKey, splitName: string, attributes: SplitIO.Attributes | undefined, storage: IStorageSync | IStorageAsync) => MaybeThenable<IEvaluation>
31
31
 
32
- export type IEvaluator = (key: SplitIO.SplitKey, seed: number, trafficAllocation?: number, trafficAllocationSeed?: number, attributes?: SplitIO.Attributes, splitEvaluator?: ISplitEvaluator) => MaybeThenable<IEvaluation | undefined>
32
+ export type IEvaluator = (key: SplitIO.SplitKeyObject, seed?: number, trafficAllocation?: number, trafficAllocationSeed?: number, attributes?: SplitIO.Attributes, splitEvaluator?: ISplitEvaluator) => MaybeThenable<IEvaluation | boolean | undefined>
33
33
 
34
- export type IMatcher = (...args: any) => MaybeThenable<boolean>
34
+ export type IMatcher = (value: string | number | boolean | string[] | IDependencyMatcherValue, splitEvaluator?: ISplitEvaluator) => MaybeThenable<boolean>
@@ -4,7 +4,7 @@ import { ILogger } from '../../logger/types';
4
4
  import { sanitize } from './sanitize';
5
5
  import { ENGINE_VALUE, ENGINE_VALUE_NO_ATTRIBUTES, ENGINE_VALUE_INVALID } from '../../logger/constants';
6
6
 
7
- function parseValue(log: ILogger, key: string, attributeName: string | null, attributes?: SplitIO.Attributes) {
7
+ function parseValue(log: ILogger, key: SplitIO.SplitKey, attributeName: string | null, attributes?: SplitIO.Attributes) {
8
8
  let value = undefined;
9
9
  if (attributeName) {
10
10
  if (attributes) {
@@ -23,7 +23,7 @@ function parseValue(log: ILogger, key: string, attributeName: string | null, att
23
23
  /**
24
24
  * Defines value to be matched (key / attribute).
25
25
  */
26
- export function sanitizeValue(log: ILogger, key: string, matcherDto: IMatcherDto, attributes?: SplitIO.Attributes) {
26
+ export function sanitizeValue(log: ILogger, key: SplitIO.SplitKey, matcherDto: IMatcherDto, attributes?: SplitIO.Attributes) {
27
27
  const attributeName = matcherDto.attribute;
28
28
  const valueToMatch = parseValue(log, key, attributeName, attributes);
29
29
  const sanitizedValue = sanitize(log, matcherDto.type, valueToMatch, matcherDto.dataType, attributes);
@@ -41,7 +41,7 @@ function sanitizeBoolean(val: any): boolean | undefined {
41
41
  return undefined;
42
42
  }
43
43
 
44
- function dependencyProcessor(sanitizedValue: string, attributes?: SplitIO.Attributes): IDependencyMatcherValue {
44
+ function dependencyProcessor(sanitizedValue: SplitIO.SplitKey, attributes?: SplitIO.Attributes): IDependencyMatcherValue {
45
45
  return {
46
46
  key: sanitizedValue,
47
47
  attributes
@@ -60,6 +60,7 @@ function getProcessingFunction(matcherTypeID: number, dataType: string) {
60
60
  case matcherTypes.BETWEEN:
61
61
  return dataType === 'DATETIME' ? zeroSinceSS : undefined;
62
62
  case matcherTypes.IN_SPLIT_TREATMENT:
63
+ case matcherTypes.IN_RULE_BASED_SEGMENT:
63
64
  return dependencyProcessor;
64
65
  default:
65
66
  return undefined;
@@ -69,9 +70,9 @@ function getProcessingFunction(matcherTypeID: number, dataType: string) {
69
70
  /**
70
71
  * Sanitize matcher value
71
72
  */
72
- export function sanitize(log: ILogger, matcherTypeID: number, value: string | number | boolean | Array<string | number> | undefined, dataType: string, attributes?: SplitIO.Attributes) {
73
+ export function sanitize(log: ILogger, matcherTypeID: number, value: string | number | boolean | Array<string | number> | SplitIO.SplitKey | undefined, dataType: string, attributes?: SplitIO.Attributes) {
73
74
  const processor = getProcessingFunction(matcherTypeID, dataType);
74
- let sanitizedValue: string | number | boolean | Array<string | number> | IDependencyMatcherValue | undefined;
75
+ let sanitizedValue: string | number | boolean | Array<string> | IDependencyMatcherValue | undefined;
75
76
 
76
77
  switch (dataType) {
77
78
  case matcherDataTypes.NUMBER:
@@ -88,7 +89,7 @@ export function sanitize(log: ILogger, matcherTypeID: number, value: string | nu
88
89
  sanitizedValue = sanitizeBoolean(value);
89
90
  break;
90
91
  case matcherDataTypes.NOT_SPECIFIED:
91
- sanitizedValue = value;
92
+ sanitizedValue = value as any;
92
93
  break;
93
94
  default:
94
95
  sanitizedValue = undefined;
@@ -20,9 +20,8 @@ export const RETRIEVE_CLIENT_EXISTING = 28;
20
20
  export const RETRIEVE_MANAGER = 29;
21
21
  export const SYNC_OFFLINE_DATA = 30;
22
22
  export const SYNC_SPLITS_FETCH = 31;
23
- export const SYNC_SPLITS_NEW = 32;
24
- export const SYNC_SPLITS_REMOVED = 33;
25
- export const SYNC_SPLITS_SEGMENTS = 34;
23
+ export const SYNC_SPLITS_UPDATE = 32;
24
+ export const SYNC_RBS_UPDATE = 33;
26
25
  export const STREAMING_NEW_MESSAGE = 35;
27
26
  export const SYNC_TASK_START = 36;
28
27
  export const SYNC_TASK_EXECUTE = 37;
@@ -20,10 +20,9 @@ export const codesDebug: [number, string][] = codesInfo.concat([
20
20
  [c.RETRIEVE_MANAGER, 'Retrieving manager instance.'],
21
21
  // synchronizer
22
22
  [c.SYNC_OFFLINE_DATA, c.LOG_PREFIX_SYNC_OFFLINE + 'Feature flags data: \n%s'],
23
- [c.SYNC_SPLITS_FETCH, c.LOG_PREFIX_SYNC_SPLITS + 'Spin up feature flags update using since = %s'],
24
- [c.SYNC_SPLITS_NEW, c.LOG_PREFIX_SYNC_SPLITS + 'New feature flags %s'],
25
- [c.SYNC_SPLITS_REMOVED, c.LOG_PREFIX_SYNC_SPLITS + 'Removed feature flags %s'],
26
- [c.SYNC_SPLITS_SEGMENTS, c.LOG_PREFIX_SYNC_SPLITS + 'Segment names collected %s'],
23
+ [c.SYNC_SPLITS_FETCH, c.LOG_PREFIX_SYNC_SPLITS + 'Spin up feature flags update using since = %s and rbSince = %s.'],
24
+ [c.SYNC_SPLITS_UPDATE, c.LOG_PREFIX_SYNC_SPLITS + 'New feature flags %s. Removed feature flags %s.'],
25
+ [c.SYNC_RBS_UPDATE, c.LOG_PREFIX_SYNC_SPLITS + 'New rule-based segments %s. Removed rule-based segments %s.'],
27
26
  [c.STREAMING_NEW_MESSAGE, c.LOG_PREFIX_SYNC_STREAMING + 'New SSE message received, with data: %s.'],
28
27
  [c.SYNC_TASK_START, c.LOG_PREFIX_SYNC + ': Starting %s. Running each %s millis'],
29
28
  [c.SYNC_TASK_EXECUTE, c.LOG_PREFIX_SYNC + ': Running %s'],
@@ -33,7 +33,7 @@ export const codesWarn: [number, string][] = codesError.concat([
33
33
  [c.WARN_SDK_KEY, c.LOG_PREFIX_SETTINGS + ': You already have %s. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application'],
34
34
 
35
35
  [c.STREAMING_PARSING_MEMBERSHIPS_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching Memberships due to an error processing %s notification: %s'],
36
- [c.STREAMING_PARSING_SPLIT_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching SplitChanges due to an error processing SPLIT_UPDATE notification: %s'],
36
+ [c.STREAMING_PARSING_SPLIT_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching SplitChanges due to an error processing %s notification: %s'],
37
37
  [c.WARN_INVALID_FLAGSET, '%s: you passed %s, flag set must adhere to the regular expressions %s. This means a flag set must start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.'],
38
38
  [c.WARN_LOWERCASE_FLAGSET, '%s: flag set %s should be all lowercase - converting string to lowercase.'],
39
39
  [c.WARN_FLAGSET_WITHOUT_FLAGS, '%s: you passed %s flag set that does not contain cached feature flag names. Please double check what flag sets are in use in the Split user interface.'],
@@ -17,7 +17,7 @@ function collectTreatments(splitObject: ISplit) {
17
17
  // Localstorage mode could fall into a no rollout conditions state. Take the first condition in that case.
18
18
  if (!allTreatmentsCondition) allTreatmentsCondition = conditions[0];
19
19
  // Then extract the treatments from the partitions
20
- return allTreatmentsCondition ? allTreatmentsCondition.partitions.map(v => v.treatment) : [];
20
+ return allTreatmentsCondition ? allTreatmentsCondition.partitions!.map(v => v.treatment) : [];
21
21
  }
22
22
 
23
23
  function objectToView(splitObject: ISplit | null): SplitIO.SplitView | null {
@@ -53,8 +53,8 @@ export function splitApiFactory(
53
53
  return splitHttpClient(url, undefined, telemetryTracker.trackHttp(TOKEN));
54
54
  },
55
55
 
56
- fetchSplitChanges(since: number, noCache?: boolean, till?: number) {
57
- const url = `${urls.sdk}/splitChanges?s=${flagSpecVersion}&since=${since}${filterQueryString || ''}${till ? '&till=' + till : ''}`;
56
+ fetchSplitChanges(since: number, noCache?: boolean, till?: number, rbSince?: number) {
57
+ const url = `${urls.sdk}/splitChanges?s=${flagSpecVersion}&since=${since}${rbSince ? '&rbSince=' + rbSince : ''}${filterQueryString || ''}${till ? '&till=' + till : ''}`;
58
58
  return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SPLITS))
59
59
  .catch((err) => {
60
60
  if (err.statusCode === 414) settings.log.error(ERROR_TOO_MANY_SETS);
@@ -35,7 +35,7 @@ export type ISplitHttpClient = (url: string, options?: IRequestOptions, latencyT
35
35
 
36
36
  export type IFetchAuth = (userKeys?: string[]) => Promise<IResponse>
37
37
 
38
- export type IFetchSplitChanges = (since: number, noCache?: boolean, till?: number) => Promise<IResponse>
38
+ export type IFetchSplitChanges = (since: number, noCache?: boolean, till?: number, rbSince?: number) => Promise<IResponse>
39
39
 
40
40
  export type IFetchSegmentChanges = (since: number, segmentName: string, noCache?: boolean, till?: number) => Promise<IResponse>
41
41
 
@@ -8,12 +8,22 @@ import { objectAssign } from '../utils/lang/objectAssign';
8
8
  */
9
9
  export abstract class AbstractSplitsCacheAsync implements ISplitsCacheAsync {
10
10
 
11
- abstract addSplit(name: string, split: ISplit): Promise<boolean>
12
- abstract addSplits(entries: [string, ISplit][]): Promise<boolean[] | void>
13
- abstract removeSplits(names: string[]): Promise<boolean[] | void>
11
+ protected abstract addSplit(split: ISplit): Promise<boolean>
12
+ protected abstract removeSplit(name: string): Promise<boolean>
13
+ protected abstract setChangeNumber(changeNumber: number): Promise<boolean | void>
14
+
15
+ update(toAdd: ISplit[], toRemove: ISplit[], changeNumber: number): Promise<boolean> {
16
+ return Promise.all([
17
+ this.setChangeNumber(changeNumber),
18
+ Promise.all(toAdd.map(addedFF => this.addSplit(addedFF))),
19
+ Promise.all(toRemove.map(removedFF => this.removeSplit(removedFF.name)))
20
+ ]).then(([, added, removed]) => {
21
+ return added.some(result => result) || removed.some(result => result);
22
+ });
23
+ }
24
+
14
25
  abstract getSplit(name: string): Promise<ISplit | null>
15
26
  abstract getSplits(names: string[]): Promise<Record<string, ISplit | null>>
16
- abstract setChangeNumber(changeNumber: number): Promise<boolean | void>
17
27
  abstract getChangeNumber(): Promise<number>
18
28
  abstract getAll(): Promise<ISplit[]>
19
29
  abstract getSplitNames(): Promise<string[]>
@@ -52,7 +62,7 @@ export abstract class AbstractSplitsCacheAsync implements ISplitsCacheAsync {
52
62
  newSplit.defaultTreatment = defaultTreatment;
53
63
  newSplit.changeNumber = changeNumber;
54
64
 
55
- return this.addSplit(name, newSplit);
65
+ return this.addSplit(newSplit);
56
66
  }
57
67
  return false;
58
68
  }).catch(() => false);
@@ -1,5 +1,5 @@
1
1
  import { ISplitsCacheSync } from './types';
2
- import { ISplit } from '../dtos/types';
2
+ import { IRBSegment, ISplit } from '../dtos/types';
3
3
  import { objectAssign } from '../utils/lang/objectAssign';
4
4
  import { IN_SEGMENT, IN_LARGE_SEGMENT } from '../utils/constants';
5
5
 
@@ -9,16 +9,14 @@ import { IN_SEGMENT, IN_LARGE_SEGMENT } from '../utils/constants';
9
9
  */
10
10
  export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
11
11
 
12
- abstract addSplit(name: string, split: ISplit): boolean
12
+ protected abstract addSplit(split: ISplit): boolean
13
+ protected abstract removeSplit(name: string): boolean
14
+ protected abstract setChangeNumber(changeNumber: number): boolean | void
13
15
 
14
- addSplits(entries: [string, ISplit][]): boolean[] {
15
- return entries.map(keyValuePair => this.addSplit(keyValuePair[0], keyValuePair[1]));
16
- }
17
-
18
- abstract removeSplit(name: string): boolean
19
-
20
- removeSplits(names: string[]): boolean[] {
21
- return names.map(name => this.removeSplit(name));
16
+ update(toAdd: ISplit[], toRemove: ISplit[], changeNumber: number): boolean {
17
+ this.setChangeNumber(changeNumber);
18
+ const updated = toAdd.map(addedFF => this.addSplit(addedFF)).some(result => result);
19
+ return toRemove.map(removedFF => this.removeSplit(removedFF.name)).some(result => result) || updated;
22
20
  }
23
21
 
24
22
  abstract getSplit(name: string): ISplit | null
@@ -31,8 +29,6 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
31
29
  return splits;
32
30
  }
33
31
 
34
- abstract setChangeNumber(changeNumber: number): boolean | void
35
-
36
32
  abstract getChangeNumber(): number
37
33
 
38
34
  getAll(): ISplit[] {
@@ -71,7 +67,7 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
71
67
  newSplit.defaultTreatment = defaultTreatment;
72
68
  newSplit.changeNumber = changeNumber;
73
69
 
74
- return this.addSplit(name, newSplit);
70
+ return this.addSplit(newSplit);
75
71
  }
76
72
  return false;
77
73
  }
@@ -84,8 +80,8 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
84
80
  * Given a parsed split, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
85
81
  * This util is intended to simplify the implementation of `splitsCache::usesSegments` method
86
82
  */
87
- export function usesSegments(split: ISplit) {
88
- const conditions = split.conditions || [];
83
+ export function usesSegments(ruleEntity: ISplit | IRBSegment) {
84
+ const conditions = ruleEntity.conditions || [];
89
85
  for (let i = 0; i < conditions.length; i++) {
90
86
  const matchers = conditions[i].matcherGroup.matchers;
91
87
 
@@ -95,5 +91,8 @@ export function usesSegments(split: ISplit) {
95
91
  }
96
92
  }
97
93
 
94
+ const excluded = (ruleEntity as IRBSegment).excluded;
95
+ if (excluded && excluded.segments && excluded.segments.length > 0) return true;
96
+
98
97
  return false;
99
98
  }
@@ -1,5 +1,4 @@
1
1
  import { ISettings } from '../types';
2
- import { startsWith } from '../utils/lang';
3
2
  import { hash } from '../utils/murmur3/murmur3';
4
3
 
5
4
  const everythingAtTheEnd = /[^.]+$/;
@@ -34,22 +33,20 @@ export class KeyBuilder {
34
33
  return `${this.prefix}.splits.till`;
35
34
  }
36
35
 
37
- // NOT USED
38
- // buildSplitsReady() {
39
- // return `${this.prefix}.splits.ready`;
40
- // }
36
+ buildSplitKeyPrefix() {
37
+ return `${this.prefix}.split.`;
38
+ }
41
39
 
42
- isSplitKey(key: string) {
43
- return startsWith(key, `${this.prefix}.split.`);
40
+ buildRBSegmentKey(rbsegmentName: string) {
41
+ return `${this.prefix}.rbsegment.${rbsegmentName}`;
44
42
  }
45
43
 
46
- buildSplitKeyPrefix() {
47
- return `${this.prefix}.split.`;
44
+ buildRBSegmentsTillKey() {
45
+ return `${this.prefix}.rbsegments.till`;
48
46
  }
49
47
 
50
- // Only used by InLocalStorage.
51
- buildSplitsWithSegmentCountKey() {
52
- return `${this.prefix}.splits.usingSegments`;
48
+ buildRBSegmentKeyPrefix() {
49
+ return `${this.prefix}.rbsegment.`;
53
50
  }
54
51
 
55
52
  buildSegmentNameKey(segmentName: string) {
@@ -60,11 +57,6 @@ export class KeyBuilder {
60
57
  return `${this.prefix}.segment.${segmentName}.till`;
61
58
  }
62
59
 
63
- // NOT USED
64
- // buildSegmentsReady() {
65
- // return `${this.prefix}.segments.ready`;
66
- // }
67
-
68
60
  extractKey(builtKey: string) {
69
61
  const s = builtKey.match(everythingAtTheEnd);
70
62
 
@@ -15,7 +15,7 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
15
15
  constructor(prefix: string, matchingKey: string) {
16
16
  super(prefix);
17
17
  this.matchingKey = matchingKey;
18
- this.regexSplitsCacheKey = new RegExp(`^${prefix}\\.(splits?|trafficType|flagSet)\\.`);
18
+ this.regexSplitsCacheKey = new RegExp(`^${prefix}\\.(splits?|trafficType|flagSet|rbsegment)\\.`);
19
19
  }
20
20
 
21
21
  /**
@@ -28,8 +28,7 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
28
28
  extractSegmentName(builtSegmentKeyName: string) {
29
29
  const prefix = `${this.prefix}.${this.matchingKey}.segment.`;
30
30
 
31
- if (startsWith(builtSegmentKeyName, prefix))
32
- return builtSegmentKeyName.substr(prefix.length);
31
+ if (startsWith(builtSegmentKeyName, prefix)) return builtSegmentKeyName.slice(prefix.length);
33
32
  }
34
33
 
35
34
  buildLastUpdatedKey() {
@@ -43,6 +42,18 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
43
42
  buildTillKey() {
44
43
  return `${this.prefix}.${this.matchingKey}.segments.till`;
45
44
  }
45
+
46
+ isSplitKey(key: string) {
47
+ return startsWith(key, `${this.prefix}.split.`);
48
+ }
49
+
50
+ isRBSegmentKey(key: string) {
51
+ return startsWith(key, `${this.prefix}.rbsegment.`);
52
+ }
53
+
54
+ buildSplitsWithSegmentCountKey() {
55
+ return `${this.prefix}.splits.usingSegments`;
56
+ }
46
57
  }
47
58
 
48
59
  export function myLargeSegmentsKeyBuilder(prefix: string, matchingKey: string): MySegmentsKeyBuilder {
@@ -54,7 +65,7 @@ export function myLargeSegmentsKeyBuilder(prefix: string, matchingKey: string):
54
65
  extractSegmentName(builtSegmentKeyName: string) {
55
66
  const p = `${prefix}.${matchingKey}.largeSegment.`;
56
67
 
57
- if (startsWith(builtSegmentKeyName, p)) return builtSegmentKeyName.substr(p.length);
68
+ if (startsWith(builtSegmentKeyName, p)) return builtSegmentKeyName.slice(p.length);
58
69
  },
59
70
 
60
71
  buildTillKey() {
@@ -53,6 +53,10 @@ export class KeyBuilderSS extends KeyBuilder {
53
53
  return `${this.buildSplitKeyPrefix()}*`;
54
54
  }
55
55
 
56
+ searchPatternForRBSegmentKeys() {
57
+ return `${this.buildRBSegmentKeyPrefix()}*`;
58
+ }
59
+
56
60
  /* Telemetry keys */
57
61
 
58
62
  buildLatencyKey(method: Method, bucket: number) {
@@ -35,10 +35,9 @@ export function dataLoaderFactory(preloadedData: PreloadedData): DataLoader {
35
35
 
36
36
  // cleaning up the localStorage data, since some cached splits might need be part of the preloaded data
37
37
  storage.splits.clear();
38
- storage.splits.setChangeNumber(since);
39
38
 
40
39
  // splitsData in an object where the property is the split name and the pertaining value is a stringified json of its data
41
- storage.splits.addSplits(Object.keys(splitsData).map(splitName => JSON.parse(splitsData[splitName])));
40
+ storage.splits.update(Object.keys(splitsData).map(splitName => JSON.parse(splitsData[splitName])), [], since);
42
41
 
43
42
  // add mySegments data
44
43
  let mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[userId];
@@ -0,0 +1,136 @@
1
+ import { IRBSegment } from '../../dtos/types';
2
+ import { ILogger } from '../../logger/types';
3
+ import { ISettings } from '../../types';
4
+ import { isFiniteNumber, isNaNNumber, toNumber } from '../../utils/lang';
5
+ import { setToArray } from '../../utils/lang/sets';
6
+ import { usesSegments } from '../AbstractSplitsCacheSync';
7
+ import { KeyBuilderCS } from '../KeyBuilderCS';
8
+ import { IRBSegmentsCacheSync } from '../types';
9
+ import { LOG_PREFIX } from './constants';
10
+
11
+ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
12
+
13
+ private readonly keys: KeyBuilderCS;
14
+ private readonly log: ILogger;
15
+
16
+ constructor(settings: ISettings, keys: KeyBuilderCS) {
17
+ this.keys = keys;
18
+ this.log = settings.log;
19
+ }
20
+
21
+ clear() {
22
+ this.getNames().forEach(name => this.remove(name));
23
+ localStorage.removeItem(this.keys.buildRBSegmentsTillKey());
24
+ }
25
+
26
+ update(toAdd: IRBSegment[], toRemove: IRBSegment[], changeNumber: number): boolean {
27
+ this.setChangeNumber(changeNumber);
28
+ const updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result);
29
+ return toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
30
+ }
31
+
32
+ private setChangeNumber(changeNumber: number) {
33
+ try {
34
+ localStorage.setItem(this.keys.buildRBSegmentsTillKey(), changeNumber + '');
35
+ localStorage.setItem(this.keys.buildLastUpdatedKey(), Date.now() + '');
36
+ } catch (e) {
37
+ this.log.error(LOG_PREFIX + e);
38
+ }
39
+ }
40
+
41
+ private updateSegmentCount(diff: number) {
42
+ const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
43
+ const count = toNumber(localStorage.getItem(segmentsCountKey)) + diff;
44
+ // @ts-expect-error
45
+ if (count > 0) localStorage.setItem(segmentsCountKey, count);
46
+ else localStorage.removeItem(segmentsCountKey);
47
+ }
48
+
49
+ private add(rbSegment: IRBSegment): boolean {
50
+ try {
51
+ const name = rbSegment.name;
52
+ const rbSegmentKey = this.keys.buildRBSegmentKey(name);
53
+ const rbSegmentFromLocalStorage = localStorage.getItem(rbSegmentKey);
54
+ const previous = rbSegmentFromLocalStorage ? JSON.parse(rbSegmentFromLocalStorage) : null;
55
+
56
+ localStorage.setItem(rbSegmentKey, JSON.stringify(rbSegment));
57
+
58
+ let usesSegmentsDiff = 0;
59
+ if (previous && usesSegments(previous)) usesSegmentsDiff--;
60
+ if (usesSegments(rbSegment)) usesSegmentsDiff++;
61
+ if (usesSegmentsDiff !== 0) this.updateSegmentCount(usesSegmentsDiff);
62
+
63
+ return true;
64
+ } catch (e) {
65
+ this.log.error(LOG_PREFIX + e);
66
+ return false;
67
+ }
68
+ }
69
+
70
+ private remove(name: string): boolean {
71
+ try {
72
+ const rbSegment = this.get(name);
73
+ if (!rbSegment) return false;
74
+
75
+ localStorage.removeItem(this.keys.buildRBSegmentKey(name));
76
+
77
+ if (usesSegments(rbSegment)) this.updateSegmentCount(-1);
78
+
79
+ return true;
80
+ } catch (e) {
81
+ this.log.error(LOG_PREFIX + e);
82
+ return false;
83
+ }
84
+ }
85
+
86
+ private getNames(): string[] {
87
+ const len = localStorage.length;
88
+ const accum = [];
89
+
90
+ let cur = 0;
91
+
92
+ while (cur < len) {
93
+ const key = localStorage.key(cur);
94
+
95
+ if (key != null && this.keys.isRBSegmentKey(key)) accum.push(this.keys.extractKey(key));
96
+
97
+ cur++;
98
+ }
99
+
100
+ return accum;
101
+ }
102
+
103
+ get(name: string): IRBSegment | null {
104
+ const item = localStorage.getItem(this.keys.buildRBSegmentKey(name));
105
+ return item && JSON.parse(item);
106
+ }
107
+
108
+ contains(names: Set<string>): boolean {
109
+ const namesArray = setToArray(names);
110
+ const namesInStorage = this.getNames();
111
+ return namesArray.every(name => namesInStorage.indexOf(name) !== -1);
112
+ }
113
+
114
+ getChangeNumber(): number {
115
+ const n = -1;
116
+ let value: string | number | null = localStorage.getItem(this.keys.buildRBSegmentsTillKey());
117
+
118
+ if (value !== null) {
119
+ value = parseInt(value, 10);
120
+
121
+ return isNaNNumber(value) ? n : value;
122
+ }
123
+
124
+ return n;
125
+ }
126
+
127
+ usesSegments(): boolean {
128
+ const storedCount = localStorage.getItem(this.keys.buildSplitsWithSegmentCountKey());
129
+ const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount);
130
+
131
+ return isFiniteNumber(splitsWithSegmentsCount) ?
132
+ splitsWithSegmentsCount > 0 :
133
+ true;
134
+ }
135
+
136
+ }