@splitsoftware/splitio-commons 2.2.1-rc.3 → 2.2.1-rc.5

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 (152) hide show
  1. package/CHANGES.txt +5 -2
  2. package/README.md +1 -0
  3. package/cjs/consent/sdkUserConsent.js +5 -3
  4. package/cjs/evaluator/combiners/and.js +2 -6
  5. package/cjs/evaluator/combiners/ifelseif.js +6 -6
  6. package/cjs/evaluator/condition/index.js +6 -5
  7. package/cjs/evaluator/index.js +7 -7
  8. package/cjs/evaluator/matchers/index.js +3 -1
  9. package/cjs/evaluator/matchers/matcherTypes.js +1 -0
  10. package/cjs/evaluator/matchers/rbsegment.js +56 -0
  11. package/cjs/evaluator/matchersTransform/index.js +4 -0
  12. package/cjs/evaluator/parser/index.js +2 -2
  13. package/cjs/evaluator/value/sanitize.js +1 -0
  14. package/cjs/listeners/browser.js +2 -5
  15. package/cjs/logger/constants.js +4 -3
  16. package/cjs/logger/messages/debug.js +3 -2
  17. package/cjs/logger/messages/warn.js +1 -1
  18. package/cjs/services/splitApi.js +3 -4
  19. package/cjs/services/splitHttpClient.js +3 -1
  20. package/cjs/storages/AbstractSplitsCacheSync.js +5 -2
  21. package/cjs/storages/KeyBuilder.js +9 -0
  22. package/cjs/storages/KeyBuilderCS.js +3 -0
  23. package/cjs/storages/KeyBuilderSS.js +3 -0
  24. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +117 -0
  25. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +9 -14
  26. package/cjs/storages/inLocalStorage/index.js +5 -1
  27. package/cjs/storages/inLocalStorage/validateCache.js +2 -1
  28. package/cjs/storages/inMemory/InMemoryStorage.js +3 -0
  29. package/cjs/storages/inMemory/InMemoryStorageCS.js +4 -0
  30. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +61 -0
  31. package/cjs/storages/inMemory/SplitsCacheInMemory.js +1 -0
  32. package/cjs/storages/inRedis/RBSegmentsCacheInRedis.js +64 -0
  33. package/cjs/storages/inRedis/index.js +5 -3
  34. package/cjs/storages/pluggable/RBSegmentsCachePluggable.js +64 -0
  35. package/cjs/storages/pluggable/index.js +2 -0
  36. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +54 -4
  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 +59 -33
  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 +6 -2
  48. package/esm/consent/sdkUserConsent.js +5 -3
  49. package/esm/evaluator/combiners/and.js +2 -6
  50. package/esm/evaluator/combiners/ifelseif.js +7 -7
  51. package/esm/evaluator/condition/index.js +6 -5
  52. package/esm/evaluator/index.js +7 -7
  53. package/esm/evaluator/matchers/index.js +3 -1
  54. package/esm/evaluator/matchers/matcherTypes.js +1 -0
  55. package/esm/evaluator/matchers/rbsegment.js +52 -0
  56. package/esm/evaluator/matchersTransform/index.js +4 -0
  57. package/esm/evaluator/parser/index.js +2 -2
  58. package/esm/evaluator/value/sanitize.js +1 -0
  59. package/esm/listeners/browser.js +2 -5
  60. package/esm/logger/constants.js +1 -0
  61. package/esm/logger/messages/debug.js +3 -2
  62. package/esm/logger/messages/warn.js +1 -1
  63. package/esm/services/splitApi.js +3 -4
  64. package/esm/services/splitHttpClient.js +3 -1
  65. package/esm/storages/AbstractSplitsCacheSync.js +5 -2
  66. package/esm/storages/KeyBuilder.js +9 -0
  67. package/esm/storages/KeyBuilderCS.js +3 -0
  68. package/esm/storages/KeyBuilderSS.js +3 -0
  69. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +114 -0
  70. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +9 -14
  71. package/esm/storages/inLocalStorage/index.js +5 -1
  72. package/esm/storages/inLocalStorage/validateCache.js +2 -1
  73. package/esm/storages/inMemory/InMemoryStorage.js +3 -0
  74. package/esm/storages/inMemory/InMemoryStorageCS.js +4 -0
  75. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +58 -0
  76. package/esm/storages/inMemory/SplitsCacheInMemory.js +1 -0
  77. package/esm/storages/inRedis/RBSegmentsCacheInRedis.js +61 -0
  78. package/esm/storages/inRedis/index.js +5 -3
  79. package/esm/storages/pluggable/RBSegmentsCachePluggable.js +61 -0
  80. package/esm/storages/pluggable/index.js +2 -0
  81. package/esm/sync/polling/fetchers/splitChangesFetcher.js +54 -4
  82. package/esm/sync/polling/pollingManagerCS.js +7 -7
  83. package/esm/sync/polling/syncTasks/splitsSyncTask.js +1 -1
  84. package/esm/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  85. package/esm/sync/polling/updaters/segmentChangesUpdater.js +1 -1
  86. package/esm/sync/polling/updaters/splitChangesUpdater.js +59 -33
  87. package/esm/sync/streaming/SSEHandler/index.js +2 -1
  88. package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +102 -73
  89. package/esm/sync/streaming/constants.js +1 -0
  90. package/esm/sync/streaming/pushManager.js +6 -19
  91. package/esm/sync/syncManagerOnline.js +2 -2
  92. package/esm/utils/constants/index.js +5 -1
  93. package/package.json +1 -1
  94. package/src/consent/sdkUserConsent.ts +3 -2
  95. package/src/dtos/types.ts +37 -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 +74 -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/listeners/browser.ts +2 -3
  111. package/src/logger/constants.ts +1 -0
  112. package/src/logger/messages/debug.ts +3 -2
  113. package/src/logger/messages/warn.ts +1 -1
  114. package/src/sdkManager/index.ts +1 -1
  115. package/src/services/splitApi.ts +3 -4
  116. package/src/services/splitHttpClient.ts +3 -1
  117. package/src/services/types.ts +1 -1
  118. package/src/storages/AbstractSplitsCacheSync.ts +6 -3
  119. package/src/storages/KeyBuilder.ts +12 -0
  120. package/src/storages/KeyBuilderCS.ts +4 -0
  121. package/src/storages/KeyBuilderSS.ts +4 -0
  122. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +136 -0
  123. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +10 -14
  124. package/src/storages/inLocalStorage/index.ts +5 -1
  125. package/src/storages/inLocalStorage/validateCache.ts +3 -1
  126. package/src/storages/inMemory/InMemoryStorage.ts +3 -0
  127. package/src/storages/inMemory/InMemoryStorageCS.ts +4 -0
  128. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +68 -0
  129. package/src/storages/inMemory/SplitsCacheInMemory.ts +1 -0
  130. package/src/storages/inRedis/RBSegmentsCacheInRedis.ts +79 -0
  131. package/src/storages/inRedis/index.ts +5 -3
  132. package/src/storages/pluggable/RBSegmentsCachePluggable.ts +76 -0
  133. package/src/storages/pluggable/index.ts +2 -0
  134. package/src/storages/types.ts +33 -1
  135. package/src/sync/polling/fetchers/splitChangesFetcher.ts +65 -4
  136. package/src/sync/polling/fetchers/types.ts +1 -0
  137. package/src/sync/polling/pollingManagerCS.ts +7 -7
  138. package/src/sync/polling/syncTasks/splitsSyncTask.ts +1 -1
  139. package/src/sync/polling/types.ts +2 -2
  140. package/src/sync/polling/updaters/mySegmentsUpdater.ts +2 -2
  141. package/src/sync/polling/updaters/segmentChangesUpdater.ts +1 -1
  142. package/src/sync/polling/updaters/splitChangesUpdater.ts +70 -43
  143. package/src/sync/streaming/SSEHandler/index.ts +2 -1
  144. package/src/sync/streaming/SSEHandler/types.ts +2 -2
  145. package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +98 -68
  146. package/src/sync/streaming/constants.ts +1 -0
  147. package/src/sync/streaming/parseUtils.ts +2 -2
  148. package/src/sync/streaming/pushManager.ts +6 -18
  149. package/src/sync/streaming/types.ts +3 -2
  150. package/src/sync/syncManagerOnline.ts +2 -2
  151. package/src/utils/constants/index.ts +6 -1
  152. package/src/utils/lang/index.ts +1 -1
@@ -0,0 +1,74 @@
1
+ import { IExcludedSegment, 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
+ import { STANDARD_SEGMENT, RULE_BASED_SEGMENT, LARGE_SEGMENT } from '../../utils/constants';
9
+
10
+
11
+ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: IStorageSync | IStorageAsync, log: ILogger) {
12
+
13
+ return function ruleBasedSegmentMatcher({ key, attributes }: IDependencyMatcherValue, splitEvaluator: ISplitEvaluator): MaybeThenable<boolean> {
14
+ const matchingKey = getMatching(key);
15
+
16
+ function matchConditions(rbsegment: IRBSegment) {
17
+ const conditions = rbsegment.conditions || [];
18
+
19
+ if (!conditions.length) return false;
20
+
21
+ const evaluator = parser(log, conditions, storage);
22
+
23
+ const evaluation = evaluator(
24
+ keyParser(key),
25
+ undefined,
26
+ undefined,
27
+ undefined,
28
+ attributes,
29
+ splitEvaluator
30
+ );
31
+
32
+ return thenable(evaluation) ?
33
+ evaluation.then(evaluation => evaluation ? true : false) :
34
+ evaluation ? true : false;
35
+ }
36
+
37
+ function isInExcludedSegment({ type, name }: IExcludedSegment) {
38
+ return type === STANDARD_SEGMENT ?
39
+ storage.segments.isInSegment(name, matchingKey) :
40
+ type === RULE_BASED_SEGMENT ?
41
+ ruleBasedSegmentMatcherContext(name, storage, log)({ key, attributes }, splitEvaluator) :
42
+ type === LARGE_SEGMENT && (storage as IStorageSync).largeSegments ?
43
+ (storage as IStorageSync).largeSegments!.isInSegment(name, matchingKey) :
44
+ false;
45
+ }
46
+
47
+ function isExcluded(rbSegment: IRBSegment) {
48
+ const excluded = rbSegment.excluded || {};
49
+
50
+ if (excluded.keys && excluded.keys.indexOf(matchingKey) !== -1) return true;
51
+
52
+ return (excluded.segments || []).reduce<MaybeThenable<boolean>>((result, excludedSegment) => {
53
+ return thenable(result) ?
54
+ result.then(result => result || isInExcludedSegment(excludedSegment)) :
55
+ result || isInExcludedSegment(excludedSegment);
56
+ }, false);
57
+ }
58
+
59
+ function isInRBSegment(rbSegment: IRBSegment | null) {
60
+ if (!rbSegment) return false;
61
+ const excluded = isExcluded(rbSegment);
62
+
63
+ return thenable(excluded) ?
64
+ excluded.then(excluded => excluded ? false : matchConditions(rbSegment)) :
65
+ excluded ? false : matchConditions(rbSegment);
66
+ }
67
+
68
+ const rbSegment = storage.rbSegments.get(segmentName);
69
+
70
+ return thenable(rbSegment) ?
71
+ rbSegment.then(isInRBSegment) :
72
+ isInRBSegment(rbSegment);
73
+ };
74
+ }
@@ -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;
@@ -84,9 +84,8 @@ export class BrowserSignalListener implements ISignalListener {
84
84
 
85
85
  this._flushData(events + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
86
86
  this._flushData(events + '/events/beacon', this.storage.events, this.serviceApi.postEventsBulk);
87
- if (this.storage.impressionCounts) this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
88
- // @ts-ignore
89
- if (this.storage.uniqueKeys) this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
87
+ this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
88
+ this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
90
89
  }
91
90
 
92
91
  // Flush telemetry data
@@ -21,6 +21,7 @@ export const RETRIEVE_MANAGER = 29;
21
21
  export const SYNC_OFFLINE_DATA = 30;
22
22
  export const SYNC_SPLITS_FETCH = 31;
23
23
  export const SYNC_SPLITS_UPDATE = 32;
24
+ export const SYNC_RBS_UPDATE = 33;
24
25
  export const STREAMING_NEW_MESSAGE = 35;
25
26
  export const SYNC_TASK_START = 36;
26
27
  export const SYNC_TASK_EXECUTE = 37;
@@ -20,8 +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_UPDATE, c.LOG_PREFIX_SYNC_SPLITS + 'New feature flags %s. Removed feature flags %s. 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.'],
25
26
  [c.STREAMING_NEW_MESSAGE, c.LOG_PREFIX_SYNC_STREAMING + 'New SSE message received, with data: %s.'],
26
27
  [c.SYNC_TASK_START, c.LOG_PREFIX_SYNC + ': Starting %s. Running each %s millis'],
27
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 {
@@ -29,7 +29,6 @@ export function splitApiFactory(
29
29
  const urls = settings.urls;
30
30
  const filterQueryString = settings.sync.__splitFiltersValidation && settings.sync.__splitFiltersValidation.queryString;
31
31
  const SplitSDKImpressionsMode = settings.sync.impressionsMode;
32
- const flagSpecVersion = settings.sync.flagSpecVersion;
33
32
  const splitHttpClient = splitHttpClientFactory(settings, platform);
34
33
 
35
34
  return {
@@ -45,7 +44,7 @@ export function splitApiFactory(
45
44
  },
46
45
 
47
46
  fetchAuth(userMatchingKeys?: string[]) {
48
- let url = `${urls.auth}/v2/auth?s=${flagSpecVersion}`;
47
+ let url = `${urls.auth}/v2/auth?s=${settings.sync.flagSpecVersion}`;
49
48
  if (userMatchingKeys) { // `userMatchingKeys` is undefined in server-side
50
49
  const queryParams = userMatchingKeys.map(userKeyToQueryParam).join('&');
51
50
  if (queryParams) url += '&' + queryParams;
@@ -53,8 +52,8 @@ export function splitApiFactory(
53
52
  return splitHttpClient(url, undefined, telemetryTracker.trackHttp(TOKEN));
54
53
  },
55
54
 
56
- fetchSplitChanges(since: number, noCache?: boolean, till?: number) {
57
- const url = `${urls.sdk}/splitChanges?s=${flagSpecVersion}&since=${since}${filterQueryString || ''}${till ? '&till=' + till : ''}`;
55
+ fetchSplitChanges(since: number, noCache?: boolean, till?: number, rbSince?: number) {
56
+ const url = `${urls.sdk}/splitChanges?s=${settings.sync.flagSpecVersion}&since=${since}${rbSince ? '&rbSince=' + rbSince : ''}${filterQueryString || ''}${till ? '&till=' + till : ''}`;
58
57
  return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SPLITS))
59
58
  .catch((err) => {
60
59
  if (err.statusCode === 414) settings.log.error(ERROR_TOO_MANY_SETS);
@@ -4,6 +4,7 @@ import { ERROR_HTTP, ERROR_CLIENT_CANNOT_GET_READY } from '../logger/constants';
4
4
  import { ISettings } from '../types';
5
5
  import { IPlatform } from '../sdkFactory/types';
6
6
  import { decorateHeaders, removeNonISO88591 } from './decorateHeaders';
7
+ import { timeout } from '../utils/promise/timeout';
7
8
 
8
9
  const messageNoFetch = 'Global fetch API is not available.';
9
10
 
@@ -45,7 +46,8 @@ export function splitHttpClientFactory(settings: ISettings, { getOptions, getFet
45
46
  // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful
46
47
  .then(response => {
47
48
  if (!response.ok) {
48
- return response.text().then(message => Promise.reject({ response, message }));
49
+ // timeout after 100ms because `text()` promise doesn't settle in some implementations and cases (e.g. no content)
50
+ return timeout(100, response.text()).then(message => Promise.reject({ response, message }), () => Promise.reject({ response }));
49
51
  }
50
52
  latencyTracker();
51
53
  return response;
@@ -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
 
@@ -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
 
@@ -72,8 +72,8 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
72
72
  * Given a parsed split, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
73
73
  * This util is intended to simplify the implementation of `splitsCache::usesSegments` method
74
74
  */
75
- export function usesSegments(split: ISplit) {
76
- const conditions = split.conditions || [];
75
+ export function usesSegments(ruleEntity: ISplit | IRBSegment) {
76
+ const conditions = ruleEntity.conditions || [];
77
77
  for (let i = 0; i < conditions.length; i++) {
78
78
  const matchers = conditions[i].matcherGroup.matchers;
79
79
 
@@ -83,5 +83,8 @@ export function usesSegments(split: ISplit) {
83
83
  }
84
84
  }
85
85
 
86
+ const excluded = (ruleEntity as IRBSegment).excluded;
87
+ if (excluded && excluded.segments && excluded.segments.length > 0) return true;
88
+
86
89
  return false;
87
90
  }
@@ -37,6 +37,18 @@ export class KeyBuilder {
37
37
  return `${this.prefix}.split.`;
38
38
  }
39
39
 
40
+ buildRBSegmentKey(rbsegmentName: string) {
41
+ return `${this.prefix}.rbsegment.${rbsegmentName}`;
42
+ }
43
+
44
+ buildRBSegmentsTillKey() {
45
+ return `${this.prefix}.rbsegments.till`;
46
+ }
47
+
48
+ buildRBSegmentKeyPrefix() {
49
+ return `${this.prefix}.rbsegment.`;
50
+ }
51
+
40
52
  buildSegmentNameKey(segmentName: string) {
41
53
  return `${this.prefix}.segment.${segmentName}`;
42
54
  }
@@ -47,6 +47,10 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
47
47
  return startsWith(key, `${this.prefix}.split.`);
48
48
  }
49
49
 
50
+ isRBSegmentKey(key: string) {
51
+ return startsWith(key, `${this.prefix}.rbsegment.`);
52
+ }
53
+
50
54
  buildSplitsWithSegmentCountKey() {
51
55
  return `${this.prefix}.splits.usingSegments`;
52
56
  }
@@ -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) {
@@ -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
+ }
@@ -47,16 +47,14 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
47
47
 
48
48
  private _incrementCounts(split: ISplit) {
49
49
  try {
50
- if (split) {
51
- const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
52
- // @ts-expect-error
53
- localStorage.setItem(ttKey, toNumber(localStorage.getItem(ttKey)) + 1);
50
+ const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
51
+ // @ts-expect-error
52
+ localStorage.setItem(ttKey, toNumber(localStorage.getItem(ttKey)) + 1);
54
53
 
55
- if (usesSegments(split)) {
56
- const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
57
- // @ts-expect-error
58
- localStorage.setItem(segmentsCountKey, toNumber(localStorage.getItem(segmentsCountKey)) + 1);
59
- }
54
+ if (usesSegments(split)) {
55
+ const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
56
+ // @ts-expect-error
57
+ localStorage.setItem(segmentsCountKey, toNumber(localStorage.getItem(segmentsCountKey)) + 1);
60
58
  }
61
59
  } catch (e) {
62
60
  this.log.error(LOG_PREFIX + e);
@@ -185,11 +183,9 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
185
183
  const storedCount = localStorage.getItem(this.keys.buildSplitsWithSegmentCountKey());
186
184
  const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount);
187
185
 
188
- if (isFiniteNumber(splitsWithSegmentsCount)) {
189
- return splitsWithSegmentsCount > 0;
190
- } else {
191
- return true;
192
- }
186
+ return isFiniteNumber(splitsWithSegmentsCount) ?
187
+ splitsWithSegmentsCount > 0 :
188
+ true;
193
189
  }
194
190
 
195
191
  getNamesByFlagSets(flagSets: string[]): Set<string>[] {
@@ -6,6 +6,7 @@ import { validatePrefix } from '../KeyBuilder';
6
6
  import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
7
7
  import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
8
8
  import { SplitsCacheInLocal } from './SplitsCacheInLocal';
9
+ import { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
9
10
  import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
10
11
  import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
11
12
  import { LOG_PREFIX } from './constants';
@@ -36,11 +37,13 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
36
37
  const keys = new KeyBuilderCS(prefix, matchingKey);
37
38
 
38
39
  const splits = new SplitsCacheInLocal(settings, keys);
40
+ const rbSegments = new RBSegmentsCacheInLocal(settings, keys);
39
41
  const segments = new MySegmentsCacheInLocal(log, keys);
40
42
  const largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
41
43
 
42
44
  return {
43
45
  splits,
46
+ rbSegments,
44
47
  segments,
45
48
  largeSegments,
46
49
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
@@ -50,7 +53,7 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
50
53
  uniqueKeys: new UniqueKeysCacheInMemoryCS(),
51
54
 
52
55
  validateCache() {
53
- return validateCache(options, settings, keys, splits, segments, largeSegments);
56
+ return validateCache(options, settings, keys, splits, rbSegments, segments, largeSegments);
54
57
  },
55
58
 
56
59
  destroy() { },
@@ -60,6 +63,7 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
60
63
 
61
64
  return {
62
65
  splits: this.splits,
66
+ rbSegments: this.rbSegments,
63
67
  segments: new MySegmentsCacheInLocal(log, new KeyBuilderCS(prefix, matchingKey)),
64
68
  largeSegments: new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey)),
65
69
  impressions: this.impressions,
@@ -3,6 +3,7 @@ import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
3
3
  import { getStorageHash } from '../KeyBuilder';
4
4
  import { LOG_PREFIX } from './constants';
5
5
  import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
6
+ import type { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
6
7
  import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
7
8
  import { KeyBuilderCS } from '../KeyBuilderCS';
8
9
  import SplitIO from '../../../types/splitio';
@@ -66,13 +67,14 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
66
67
  *
67
68
  * @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
68
69
  */
69
- export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
70
+ export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, rbSegments: RBSegmentsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
70
71
 
71
72
  const currentTimestamp = Date.now();
72
73
  const isThereCache = splits.getChangeNumber() > -1;
73
74
 
74
75
  if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
75
76
  splits.clear();
77
+ rbSegments.clear();
76
78
  segments.clear();
77
79
  largeSegments.clear();
78
80
 
@@ -7,6 +7,7 @@ import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
7
7
  import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
8
8
  import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
9
9
  import { UniqueKeysCacheInMemory } from './UniqueKeysCacheInMemory';
10
+ import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
10
11
 
11
12
  /**
12
13
  * InMemory storage factory for standalone server-side SplitFactory
@@ -17,10 +18,12 @@ export function InMemoryStorageFactory(params: IStorageFactoryParams): IStorageS
17
18
  const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { __splitFiltersValidation } } } = params;
18
19
 
19
20
  const splits = new SplitsCacheInMemory(__splitFiltersValidation);
21
+ const rbSegments = new RBSegmentsCacheInMemory();
20
22
  const segments = new SegmentsCacheInMemory();
21
23
 
22
24
  const storage = {
23
25
  splits,
26
+ rbSegments,
24
27
  segments,
25
28
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
26
29
  impressionCounts: new ImpressionCountsCacheInMemory(),
@@ -7,6 +7,7 @@ import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
7
7
  import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
8
8
  import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
9
9
  import { UniqueKeysCacheInMemoryCS } from './UniqueKeysCacheInMemoryCS';
10
+ import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
10
11
 
11
12
  /**
12
13
  * InMemory storage factory for standalone client-side SplitFactory
@@ -17,11 +18,13 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
17
18
  const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize }, sync: { __splitFiltersValidation } } } = params;
18
19
 
19
20
  const splits = new SplitsCacheInMemory(__splitFiltersValidation);
21
+ const rbSegments = new RBSegmentsCacheInMemory();
20
22
  const segments = new MySegmentsCacheInMemory();
21
23
  const largeSegments = new MySegmentsCacheInMemory();
22
24
 
23
25
  const storage = {
24
26
  splits,
27
+ rbSegments,
25
28
  segments,
26
29
  largeSegments,
27
30
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
@@ -36,6 +39,7 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
36
39
  shared() {
37
40
  return {
38
41
  splits: this.splits,
42
+ rbSegments: this.rbSegments,
39
43
  segments: new MySegmentsCacheInMemory(),
40
44
  largeSegments: new MySegmentsCacheInMemory(),
41
45
  impressions: this.impressions,