@splitsoftware/splitio-commons 2.0.3-rc.0 → 2.1.0-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 +0 -4
- package/README.md +2 -2
- package/cjs/evaluator/index.js +0 -2
- package/cjs/listeners/browser.js +6 -4
- package/cjs/listeners/node.js +2 -2
- package/cjs/sdkClient/client.js +13 -13
- package/cjs/sdkClient/sdkClient.js +1 -1
- package/cjs/sdkFactory/index.js +14 -9
- package/cjs/sdkManager/index.js +1 -2
- package/cjs/storages/AbstractSplitsCacheAsync.js +0 -7
- package/cjs/storages/AbstractSplitsCacheSync.js +0 -7
- package/cjs/storages/KeyBuilderCS.js +3 -0
- package/cjs/storages/dataLoader.js +3 -2
- package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +1 -57
- package/cjs/storages/inLocalStorage/index.js +8 -7
- package/cjs/storages/inLocalStorage/validateCache.js +80 -0
- package/cjs/storages/inMemory/InMemoryStorage.js +3 -3
- package/cjs/storages/inMemory/InMemoryStorageCS.js +3 -4
- package/cjs/storages/inRedis/index.js +13 -9
- package/cjs/storages/pluggable/index.js +21 -16
- package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +1 -10
- package/cjs/sync/streaming/pushManager.js +8 -6
- package/cjs/sync/submitters/impressionCountsSubmitter.js +4 -2
- package/cjs/sync/submitters/submitterManager.js +6 -3
- package/cjs/sync/syncManagerOnline.js +10 -4
- package/cjs/trackers/eventTracker.js +1 -1
- package/cjs/trackers/impressionsTracker.js +19 -18
- package/cjs/trackers/strategy/strategyDebug.js +11 -4
- package/cjs/trackers/strategy/strategyNone.js +16 -11
- package/cjs/trackers/strategy/strategyOptimized.js +21 -11
- package/cjs/utils/settingsValidation/index.js +1 -1
- package/cjs/utils/settingsValidation/storage/storageCS.js +1 -1
- package/esm/evaluator/index.js +0 -2
- package/esm/listeners/browser.js +3 -1
- package/esm/listeners/node.js +2 -2
- package/esm/sdkClient/client.js +13 -13
- package/esm/sdkClient/sdkClient.js +1 -1
- package/esm/sdkFactory/index.js +15 -10
- package/esm/sdkManager/index.js +1 -2
- package/esm/storages/AbstractSplitsCacheAsync.js +0 -7
- package/esm/storages/AbstractSplitsCacheSync.js +0 -7
- package/esm/storages/KeyBuilderCS.js +3 -0
- package/esm/storages/dataLoader.js +2 -1
- package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +1 -57
- package/esm/storages/inLocalStorage/index.js +9 -8
- package/esm/storages/inLocalStorage/validateCache.js +76 -0
- package/esm/storages/inMemory/InMemoryStorage.js +4 -4
- package/esm/storages/inMemory/InMemoryStorageCS.js +4 -5
- package/esm/storages/inRedis/index.js +14 -10
- package/esm/storages/pluggable/index.js +22 -17
- package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
- package/esm/sync/polling/updaters/splitChangesUpdater.js +2 -11
- package/esm/sync/streaming/pushManager.js +8 -6
- package/esm/sync/submitters/impressionCountsSubmitter.js +4 -2
- package/esm/sync/submitters/submitterManager.js +6 -3
- package/esm/sync/syncManagerOnline.js +10 -4
- package/esm/trackers/eventTracker.js +1 -1
- package/esm/trackers/impressionsTracker.js +19 -18
- package/esm/trackers/strategy/strategyDebug.js +11 -4
- package/esm/trackers/strategy/strategyNone.js +16 -11
- package/esm/trackers/strategy/strategyOptimized.js +21 -11
- package/esm/utils/settingsValidation/index.js +1 -1
- package/esm/utils/settingsValidation/storage/storageCS.js +1 -1
- package/package.json +1 -1
- package/src/dtos/types.ts +1 -2
- package/src/evaluator/index.ts +0 -2
- package/src/evaluator/types.ts +1 -1
- package/src/listeners/browser.ts +3 -1
- package/src/listeners/node.ts +2 -2
- package/src/sdkClient/client.ts +11 -11
- package/src/sdkClient/sdkClient.ts +1 -1
- package/src/sdkFactory/index.ts +16 -11
- package/src/sdkFactory/types.ts +1 -1
- package/src/sdkManager/index.ts +1 -2
- package/src/storages/AbstractSplitsCacheAsync.ts +0 -8
- package/src/storages/AbstractSplitsCacheSync.ts +0 -8
- package/src/storages/KeyBuilderCS.ts +4 -0
- package/src/storages/dataLoader.ts +3 -1
- package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +1 -66
- package/src/storages/inLocalStorage/index.ts +12 -13
- package/src/storages/inLocalStorage/validateCache.ts +92 -0
- package/src/storages/inMemory/InMemoryStorage.ts +4 -4
- package/src/storages/inMemory/InMemoryStorageCS.ts +4 -5
- package/src/storages/inRedis/index.ts +10 -10
- package/src/storages/pluggable/index.ts +22 -17
- package/src/storages/types.ts +3 -6
- package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +6 -5
- package/src/sync/polling/updaters/splitChangesUpdater.ts +2 -11
- package/src/sync/streaming/pushManager.ts +8 -6
- package/src/sync/submitters/impressionCountsSubmitter.ts +4 -2
- package/src/sync/submitters/submitterManager.ts +4 -3
- package/src/sync/submitters/uniqueKeysSubmitter.ts +3 -2
- package/src/sync/syncManagerOnline.ts +11 -5
- package/src/trackers/eventTracker.ts +1 -1
- package/src/trackers/impressionsTracker.ts +19 -18
- package/src/trackers/strategy/strategyDebug.ts +11 -4
- package/src/trackers/strategy/strategyNone.ts +17 -11
- package/src/trackers/strategy/strategyOptimized.ts +20 -10
- package/src/trackers/types.ts +8 -2
- package/src/utils/lang/index.ts +1 -1
- package/src/utils/settingsValidation/index.ts +1 -1
- package/src/utils/settingsValidation/storage/storageCS.ts +1 -1
- package/types/index.d.ts +1 -1
- package/types/splitio.d.ts +26 -6
- package/cjs/utils/constants/browser.js +0 -5
- package/esm/utils/constants/browser.js +0 -2
- package/src/utils/constants/browser.ts +0 -2
|
@@ -22,7 +22,7 @@ export function eventTrackerFactory(settings, eventsCache, whenInit, integration
|
|
|
22
22
|
whenInit(function () {
|
|
23
23
|
// Wrap in a timeout because we don't want it to be blocking.
|
|
24
24
|
setTimeout(function () {
|
|
25
|
-
// copy of event, to avoid unexpected
|
|
25
|
+
// copy of event, to avoid unexpected behavior if modified by integrations
|
|
26
26
|
var eventDataCopy = objectAssign({}, eventData);
|
|
27
27
|
if (properties)
|
|
28
28
|
eventDataCopy.properties = objectAssign({}, properties);
|
|
@@ -4,37 +4,38 @@ import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIO
|
|
|
4
4
|
import { CONSENT_DECLINED, DEDUPED, QUEUED } from '../utils/constants';
|
|
5
5
|
/**
|
|
6
6
|
* Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided.
|
|
7
|
+
*
|
|
8
|
+
* @param impressionsCache - cache to save impressions
|
|
9
|
+
* @param metadata - runtime metadata (ip, hostname and version)
|
|
10
|
+
* @param impressionListener - optional impression listener
|
|
11
|
+
* @param integrationsManager - optional integrations manager
|
|
12
|
+
* @param strategy - strategy for impressions tracking.
|
|
7
13
|
*/
|
|
8
|
-
export function impressionsTrackerFactory(settings, impressionsCache,
|
|
14
|
+
export function impressionsTrackerFactory(settings, impressionsCache, strategy, whenInit, integrationsManager, telemetryCache) {
|
|
9
15
|
var log = settings.log, impressionListener = settings.impressionListener, _a = settings.runtime, ip = _a.ip, hostname = _a.hostname, version = settings.version;
|
|
10
16
|
return {
|
|
11
17
|
track: function (impressions, attributes) {
|
|
12
18
|
if (settings.userConsent === CONSENT_DECLINED)
|
|
13
19
|
return;
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
var impressionsLength = impressions.length;
|
|
21
|
-
var impressionsToStoreLength = impressionsToStore.length;
|
|
22
|
-
if (impressionsToStoreLength) {
|
|
23
|
-
var res = impressionsCache.track(impressionsToStore.map(function (item) { return item[0]; }));
|
|
20
|
+
var impressionsCount = impressions.length;
|
|
21
|
+
var _a = strategy.process(impressions), impressionsToStore = _a.impressionsToStore, impressionsToListener = _a.impressionsToListener, deduped = _a.deduped;
|
|
22
|
+
var impressionsToListenerCount = impressionsToListener.length;
|
|
23
|
+
if (impressionsToStore.length > 0) {
|
|
24
|
+
var res = impressionsCache.track(impressionsToStore);
|
|
24
25
|
// If we're on an async storage, handle error and log it.
|
|
25
26
|
if (thenable(res)) {
|
|
26
27
|
res.then(function () {
|
|
27
|
-
log.info(IMPRESSIONS_TRACKER_SUCCESS, [
|
|
28
|
+
log.info(IMPRESSIONS_TRACKER_SUCCESS, [impressionsCount]);
|
|
28
29
|
}).catch(function (err) {
|
|
29
|
-
log.error(ERROR_IMPRESSIONS_TRACKER, [
|
|
30
|
+
log.error(ERROR_IMPRESSIONS_TRACKER, [impressionsCount, err]);
|
|
30
31
|
});
|
|
31
32
|
}
|
|
32
33
|
else {
|
|
33
34
|
// Record when impressionsCache is sync only (standalone mode)
|
|
34
35
|
// @TODO we are not dropping impressions on full queue yet, so DROPPED stats are not recorded
|
|
35
36
|
if (telemetryCache) {
|
|
36
|
-
telemetryCache.recordImpressionStats(QUEUED,
|
|
37
|
-
telemetryCache.recordImpressionStats(DEDUPED,
|
|
37
|
+
telemetryCache.recordImpressionStats(QUEUED, impressionsToStore.length);
|
|
38
|
+
telemetryCache.recordImpressionStats(DEDUPED, deduped);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -42,8 +43,8 @@ export function impressionsTrackerFactory(settings, impressionsCache, noneStrate
|
|
|
42
43
|
if (impressionListener || integrationsManager) {
|
|
43
44
|
var _loop_1 = function (i) {
|
|
44
45
|
var impressionData = {
|
|
45
|
-
// copy of impression, to avoid unexpected
|
|
46
|
-
impression: objectAssign({},
|
|
46
|
+
// copy of impression, to avoid unexpected behavior if modified by integrations or impressionListener
|
|
47
|
+
impression: objectAssign({}, impressionsToListener[i]),
|
|
47
48
|
attributes: attributes,
|
|
48
49
|
ip: ip,
|
|
49
50
|
hostname: hostname,
|
|
@@ -65,7 +66,7 @@ export function impressionsTrackerFactory(settings, impressionsCache, noneStrate
|
|
|
65
66
|
});
|
|
66
67
|
});
|
|
67
68
|
};
|
|
68
|
-
for (var i = 0; i <
|
|
69
|
+
for (var i = 0; i < impressionsToListenerCount; i++) {
|
|
69
70
|
_loop_1(i);
|
|
70
71
|
}
|
|
71
72
|
}
|
|
@@ -2,13 +2,20 @@
|
|
|
2
2
|
* Debug strategy for impressions tracker. Wraps impressions to store and adds previousTime if it corresponds
|
|
3
3
|
*
|
|
4
4
|
* @param impressionsObserver - impression observer. Previous time (pt property) is included in impression instances
|
|
5
|
-
* @returns
|
|
5
|
+
* @returns IStrategyResult
|
|
6
6
|
*/
|
|
7
7
|
export function strategyDebugFactory(impressionsObserver) {
|
|
8
8
|
return {
|
|
9
|
-
process: function (
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
process: function (impressions) {
|
|
10
|
+
impressions.forEach(function (impression) {
|
|
11
|
+
// Adds previous time if it is enabled
|
|
12
|
+
impression.pt = impressionsObserver.testAndSet(impression);
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
impressionsToStore: impressions,
|
|
16
|
+
impressionsToListener: impressions,
|
|
17
|
+
deduped: 0
|
|
18
|
+
};
|
|
12
19
|
}
|
|
13
20
|
};
|
|
14
21
|
}
|
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* None strategy for impressions tracker.
|
|
3
3
|
*
|
|
4
|
-
* @param
|
|
4
|
+
* @param impressionsCounter - cache to save impressions count. impressions will be deduped (OPTIMIZED mode)
|
|
5
5
|
* @param uniqueKeysTracker - unique keys tracker in charge of tracking the unique keys per split.
|
|
6
|
-
* @returns
|
|
6
|
+
* @returns IStrategyResult
|
|
7
7
|
*/
|
|
8
|
-
export function strategyNoneFactory(
|
|
8
|
+
export function strategyNoneFactory(impressionsCounter, uniqueKeysTracker) {
|
|
9
9
|
return {
|
|
10
|
-
process: function (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
process: function (impressions) {
|
|
11
|
+
impressions.forEach(function (impression) {
|
|
12
|
+
var now = Date.now();
|
|
13
|
+
// Increments impression counter per featureName
|
|
14
|
+
impressionsCounter.track(impression.feature, now, 1);
|
|
15
|
+
// Keep track by unique key
|
|
16
|
+
uniqueKeysTracker.track(impression.keyName, impression.feature);
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
impressionsToStore: [],
|
|
20
|
+
impressionsToListener: impressions,
|
|
21
|
+
deduped: 0
|
|
22
|
+
};
|
|
18
23
|
}
|
|
19
24
|
};
|
|
20
25
|
}
|
|
@@ -3,19 +3,29 @@ import { truncateTimeFrame } from '../../utils/time';
|
|
|
3
3
|
* Optimized strategy for impressions tracker. Wraps impressions to store and adds previousTime if it corresponds
|
|
4
4
|
*
|
|
5
5
|
* @param impressionsObserver - impression observer. previous time (pt property) is included in impression instances
|
|
6
|
-
* @param
|
|
7
|
-
* @returns
|
|
6
|
+
* @param impressionsCounter - cache to save impressions count. impressions will be deduped (OPTIMIZED mode)
|
|
7
|
+
* @returns IStrategyResult
|
|
8
8
|
*/
|
|
9
|
-
export function strategyOptimizedFactory(impressionsObserver,
|
|
9
|
+
export function strategyOptimizedFactory(impressionsObserver, impressionsCounter) {
|
|
10
10
|
return {
|
|
11
|
-
process: function (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
process: function (impressions) {
|
|
12
|
+
var impressionsToStore = [];
|
|
13
|
+
impressions.forEach(function (impression) {
|
|
14
|
+
impression.pt = impressionsObserver.testAndSet(impression);
|
|
15
|
+
var now = Date.now();
|
|
16
|
+
// Increments impression counter per featureName
|
|
17
|
+
if (impression.pt)
|
|
18
|
+
impressionsCounter.track(impression.feature, now, 1);
|
|
19
|
+
// Checks if the impression should be added in queue to be sent
|
|
20
|
+
if (!impression.pt || impression.pt < truncateTimeFrame(now)) {
|
|
21
|
+
impressionsToStore.push(impression);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
impressionsToStore: impressionsToStore,
|
|
26
|
+
impressionsToListener: impressions,
|
|
27
|
+
deduped: impressions.length - impressionsToStore.length
|
|
28
|
+
};
|
|
19
29
|
}
|
|
20
30
|
};
|
|
21
31
|
}
|
|
@@ -136,7 +136,7 @@ export function settingsValidation(config, validationParams) {
|
|
|
136
136
|
withDefaults.core.key = 'localhost_key';
|
|
137
137
|
}
|
|
138
138
|
else {
|
|
139
|
-
// Keeping same
|
|
139
|
+
// Keeping same behavior than JS SDK: if settings key or TT are invalid,
|
|
140
140
|
// `false` value is used as bound key/TT of the default client, which leads to some issues.
|
|
141
141
|
// @ts-ignore, @TODO handle invalid keys as a non-recoverable error?
|
|
142
142
|
withDefaults.core.key = validateKey(log, maybeKey, LOG_PREFIX_CLIENT_INSTANTIATION);
|
|
@@ -3,7 +3,7 @@ import { ERROR_STORAGE_INVALID } from '../../../logger/constants';
|
|
|
3
3
|
import { LOCALHOST_MODE, STANDALONE_MODE, STORAGE_PLUGGABLE, STORAGE_LOCALSTORAGE, STORAGE_MEMORY } from '../../../utils/constants';
|
|
4
4
|
export function __InLocalStorageMockFactory(params) {
|
|
5
5
|
var result = InMemoryStorageCSFactory(params);
|
|
6
|
-
result.
|
|
6
|
+
result.validateCache = function () { return true; }; // to emit SDK_READY_FROM_CACHE
|
|
7
7
|
return result;
|
|
8
8
|
}
|
|
9
9
|
__InLocalStorageMockFactory.type = STORAGE_MEMORY;
|
package/package.json
CHANGED
package/src/dtos/types.ts
CHANGED
package/src/evaluator/index.ts
CHANGED
|
@@ -156,14 +156,12 @@ function getEvaluation(
|
|
|
156
156
|
return evaluation.then(result => {
|
|
157
157
|
result.changeNumber = split.getChangeNumber();
|
|
158
158
|
result.config = splitJSON.configurations && splitJSON.configurations[result.treatment] || null;
|
|
159
|
-
result.track = splitJSON.trackImpressions;
|
|
160
159
|
|
|
161
160
|
return result;
|
|
162
161
|
});
|
|
163
162
|
} else {
|
|
164
163
|
evaluation.changeNumber = split.getChangeNumber(); // Always sync and optional
|
|
165
164
|
evaluation.config = splitJSON.configurations && splitJSON.configurations[evaluation.treatment] || null;
|
|
166
|
-
evaluation.track = splitJSON.trackImpressions;
|
|
167
165
|
}
|
|
168
166
|
}
|
|
169
167
|
|
package/src/evaluator/types.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface IEvaluation {
|
|
|
25
25
|
config?: string | null
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export type IEvaluationResult = IEvaluation & { treatment: string
|
|
28
|
+
export type IEvaluationResult = IEvaluation & { treatment: string }
|
|
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
|
|
package/src/listeners/browser.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { IResponse, ISplitApi } from '../services/types';
|
|
|
8
8
|
import { ISettings } from '../types';
|
|
9
9
|
import SplitIO from '../../types/splitio';
|
|
10
10
|
import { ImpressionsPayload } from '../sync/submitters/types';
|
|
11
|
+
import { OPTIMIZED, DEBUG, NONE } from '../utils/constants';
|
|
11
12
|
import { objectAssign } from '../utils/lang/objectAssign';
|
|
12
13
|
import { CLEANUP_REGISTERING, CLEANUP_DEREGISTERING } from '../logger/constants';
|
|
13
14
|
import { ISyncManager } from '../sync/types';
|
|
@@ -77,9 +78,10 @@ export class BrowserSignalListener implements ISignalListener {
|
|
|
77
78
|
|
|
78
79
|
// Flush impressions & events data if there is user consent
|
|
79
80
|
if (isConsentGranted(this.settings)) {
|
|
81
|
+
const sim = this.settings.sync.impressionsMode;
|
|
80
82
|
const extraMetadata = {
|
|
81
83
|
// sim stands for Sync/Split Impressions Mode
|
|
82
|
-
sim:
|
|
84
|
+
sim: sim === OPTIMIZED ? OPTIMIZED : sim === DEBUG ? DEBUG : NONE
|
|
83
85
|
};
|
|
84
86
|
|
|
85
87
|
this._flushData(events + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
|
package/src/listeners/node.ts
CHANGED
|
@@ -56,7 +56,7 @@ export class NodeSignalListener implements ISignalListener {
|
|
|
56
56
|
// Cleaned up, remove handlers.
|
|
57
57
|
this.stop();
|
|
58
58
|
|
|
59
|
-
// This handler prevented the default
|
|
59
|
+
// This handler prevented the default behavior, start again.
|
|
60
60
|
// eslint-disable-next-line no-undef
|
|
61
61
|
process.kill(process.pid, SIGTERM);
|
|
62
62
|
};
|
|
@@ -72,7 +72,7 @@ export class NodeSignalListener implements ISignalListener {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
if (thenable(handlerResult)) {
|
|
75
|
-
// Always exit, even with errors. The promise is returned for UT
|
|
75
|
+
// Always exit, even with errors. The promise is returned for UT purposes.
|
|
76
76
|
return handlerResult.then(wrapUp).catch(wrapUp);
|
|
77
77
|
} else {
|
|
78
78
|
wrapUp();
|
package/src/sdkClient/client.ts
CHANGED
|
@@ -34,11 +34,11 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
|
|
|
34
34
|
const stopTelemetryTracker = telemetryTracker.trackEval(withConfig ? TREATMENT_WITH_CONFIG : TREATMENT);
|
|
35
35
|
|
|
36
36
|
const wrapUp = (evaluationResult: IEvaluationResult) => {
|
|
37
|
-
const queue:
|
|
37
|
+
const queue: SplitIO.ImpressionDTO[] = [];
|
|
38
38
|
const treatment = processEvaluation(evaluationResult, featureFlagName, key, attributes, withConfig, methodName, queue);
|
|
39
39
|
impressionsTracker.track(queue, attributes);
|
|
40
40
|
|
|
41
|
-
stopTelemetryTracker(queue[0] && queue[0]
|
|
41
|
+
stopTelemetryTracker(queue[0] && queue[0].label);
|
|
42
42
|
return treatment;
|
|
43
43
|
};
|
|
44
44
|
|
|
@@ -59,14 +59,14 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
|
|
|
59
59
|
const stopTelemetryTracker = telemetryTracker.trackEval(withConfig ? TREATMENTS_WITH_CONFIG : TREATMENTS);
|
|
60
60
|
|
|
61
61
|
const wrapUp = (evaluationResults: Record<string, IEvaluationResult>) => {
|
|
62
|
-
const queue:
|
|
62
|
+
const queue: SplitIO.ImpressionDTO[] = [];
|
|
63
63
|
const treatments: Record<string, SplitIO.Treatment | SplitIO.TreatmentWithConfig> = {};
|
|
64
64
|
Object.keys(evaluationResults).forEach(featureFlagName => {
|
|
65
65
|
treatments[featureFlagName] = processEvaluation(evaluationResults[featureFlagName], featureFlagName, key, attributes, withConfig, methodName, queue);
|
|
66
66
|
});
|
|
67
67
|
impressionsTracker.track(queue, attributes);
|
|
68
68
|
|
|
69
|
-
stopTelemetryTracker(queue[0] && queue[0]
|
|
69
|
+
stopTelemetryTracker(queue[0] && queue[0].label);
|
|
70
70
|
return treatments;
|
|
71
71
|
};
|
|
72
72
|
|
|
@@ -87,7 +87,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
|
|
|
87
87
|
const stopTelemetryTracker = telemetryTracker.trackEval(method);
|
|
88
88
|
|
|
89
89
|
const wrapUp = (evaluationResults: Record<string, IEvaluationResult>) => {
|
|
90
|
-
const queue:
|
|
90
|
+
const queue: SplitIO.ImpressionDTO[] = [];
|
|
91
91
|
const treatments: Record<string, SplitIO.Treatment | SplitIO.TreatmentWithConfig> = {};
|
|
92
92
|
const evaluations = evaluationResults;
|
|
93
93
|
Object.keys(evaluations).forEach(featureFlagName => {
|
|
@@ -95,7 +95,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
|
|
|
95
95
|
});
|
|
96
96
|
impressionsTracker.track(queue, attributes);
|
|
97
97
|
|
|
98
|
-
stopTelemetryTracker(queue[0] && queue[0]
|
|
98
|
+
stopTelemetryTracker(queue[0] && queue[0].label);
|
|
99
99
|
return treatments;
|
|
100
100
|
};
|
|
101
101
|
|
|
@@ -128,25 +128,25 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
|
|
|
128
128
|
attributes: SplitIO.Attributes | undefined,
|
|
129
129
|
withConfig: boolean,
|
|
130
130
|
invokingMethodName: string,
|
|
131
|
-
queue:
|
|
131
|
+
queue: SplitIO.ImpressionDTO[]
|
|
132
132
|
): SplitIO.Treatment | SplitIO.TreatmentWithConfig {
|
|
133
133
|
const matchingKey = getMatching(key);
|
|
134
134
|
const bucketingKey = getBucketing(key);
|
|
135
135
|
|
|
136
|
-
const { treatment, label, changeNumber, config = null
|
|
136
|
+
const { treatment, label, changeNumber, config = null } = evaluation;
|
|
137
137
|
log.info(IMPRESSION, [featureFlagName, matchingKey, treatment, label]);
|
|
138
138
|
|
|
139
139
|
if (validateSplitExistence(log, readinessManager, featureFlagName, label, invokingMethodName)) {
|
|
140
140
|
log.info(IMPRESSION_QUEUEING);
|
|
141
|
-
queue.push(
|
|
141
|
+
queue.push({
|
|
142
142
|
feature: featureFlagName,
|
|
143
143
|
keyName: matchingKey,
|
|
144
144
|
treatment,
|
|
145
145
|
time: Date.now(),
|
|
146
146
|
bucketingKey,
|
|
147
147
|
label,
|
|
148
|
-
changeNumber: changeNumber as number
|
|
149
|
-
}
|
|
148
|
+
changeNumber: changeNumber as number
|
|
149
|
+
});
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
if (withConfig) {
|
|
@@ -61,7 +61,7 @@ export function sdkClientFactory(params: ISdkFactoryContext, isSharedClient?: bo
|
|
|
61
61
|
releaseApiKey(settings.core.authorizationKey);
|
|
62
62
|
telemetryTracker.sessionLength();
|
|
63
63
|
signalListener && signalListener.stop();
|
|
64
|
-
uniqueKeysTracker.stop();
|
|
64
|
+
uniqueKeysTracker && uniqueKeysTracker.stop();
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// Stop background jobs
|
package/src/sdkFactory/index.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { strategyDebugFactory } from '../trackers/strategy/strategyDebug';
|
|
|
13
13
|
import { strategyOptimizedFactory } from '../trackers/strategy/strategyOptimized';
|
|
14
14
|
import { strategyNoneFactory } from '../trackers/strategy/strategyNone';
|
|
15
15
|
import { uniqueKeysTrackerFactory } from '../trackers/uniqueKeysTracker';
|
|
16
|
-
import {
|
|
16
|
+
import { NONE, OPTIMIZED } from '../utils/constants';
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Modular SDK factory
|
|
@@ -59,16 +59,21 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ISDK | SplitIO.IA
|
|
|
59
59
|
const integrationsManager = integrationsManagerFactory && integrationsManagerFactory({ settings, storage, telemetryTracker });
|
|
60
60
|
|
|
61
61
|
const observer = impressionsObserverFactory();
|
|
62
|
-
const uniqueKeysTracker = uniqueKeysTrackerFactory(log, storage.uniqueKeys
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
const uniqueKeysTracker = impressionsMode === NONE ? uniqueKeysTrackerFactory(log, storage.uniqueKeys!, filterAdapterFactory && filterAdapterFactory()) : undefined;
|
|
63
|
+
|
|
64
|
+
let strategy;
|
|
65
|
+
switch (impressionsMode) {
|
|
66
|
+
case OPTIMIZED:
|
|
67
|
+
strategy = strategyOptimizedFactory(observer, storage.impressionCounts!);
|
|
68
|
+
break;
|
|
69
|
+
case NONE:
|
|
70
|
+
strategy = strategyNoneFactory(storage.impressionCounts!, uniqueKeysTracker!);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
strategy = strategyDebugFactory(observer);
|
|
74
|
+
}
|
|
70
75
|
|
|
71
|
-
const impressionsTracker = impressionsTrackerFactory(settings, storage.impressions,
|
|
76
|
+
const impressionsTracker = impressionsTrackerFactory(settings, storage.impressions, strategy, whenInit, integrationsManager, storage.telemetry);
|
|
72
77
|
const eventTracker = eventTrackerFactory(settings, storage.events, whenInit, integrationsManager, storage.telemetry);
|
|
73
78
|
|
|
74
79
|
// splitApi is used by SyncManager and Browser signal listener
|
|
@@ -94,7 +99,7 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ISDK | SplitIO.IA
|
|
|
94
99
|
// We will just log and allow for the SDK to end up throwing an SDK_TIMEOUT event for devs to handle.
|
|
95
100
|
validateAndTrackApiKey(log, settings.core.authorizationKey);
|
|
96
101
|
readiness.init();
|
|
97
|
-
uniqueKeysTracker.start();
|
|
102
|
+
uniqueKeysTracker && uniqueKeysTracker.start();
|
|
98
103
|
syncManager && syncManager.start();
|
|
99
104
|
signalListener && signalListener.start();
|
|
100
105
|
|
package/src/sdkFactory/types.ts
CHANGED
|
@@ -46,7 +46,7 @@ export interface ISdkFactoryContext {
|
|
|
46
46
|
eventTracker: IEventTracker,
|
|
47
47
|
telemetryTracker: ITelemetryTracker,
|
|
48
48
|
storage: IStorageSync | IStorageAsync,
|
|
49
|
-
uniqueKeysTracker
|
|
49
|
+
uniqueKeysTracker?: IUniqueKeysTracker,
|
|
50
50
|
signalListener?: ISignalListener
|
|
51
51
|
splitApi?: ISplitApi
|
|
52
52
|
syncManager?: ISyncManager,
|
package/src/sdkManager/index.ts
CHANGED
|
@@ -31,8 +31,7 @@ function objectToView(splitObject: ISplit | null): SplitIO.SplitView | null {
|
|
|
31
31
|
treatments: collectTreatments(splitObject),
|
|
32
32
|
configs: splitObject.configurations || {},
|
|
33
33
|
sets: splitObject.sets || [],
|
|
34
|
-
defaultTreatment: splitObject.defaultTreatment
|
|
35
|
-
trackImpressions: splitObject.trackImpressions !== false
|
|
34
|
+
defaultTreatment: splitObject.defaultTreatment
|
|
36
35
|
};
|
|
37
36
|
}
|
|
38
37
|
|
|
@@ -27,14 +27,6 @@ export abstract class AbstractSplitsCacheAsync implements ISplitsCacheAsync {
|
|
|
27
27
|
return Promise.resolve(true);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/**
|
|
31
|
-
* Check if the splits information is already stored in cache.
|
|
32
|
-
* Noop, just keeping the interface. This is used by client-side implementations only.
|
|
33
|
-
*/
|
|
34
|
-
checkCache(): Promise<boolean> {
|
|
35
|
-
return Promise.resolve(false);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
30
|
/**
|
|
39
31
|
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
|
|
40
32
|
* Used for SPLIT_KILL push notifications.
|
|
@@ -47,14 +47,6 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
|
|
|
47
47
|
|
|
48
48
|
abstract clear(): void
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
* Check if the splits information is already stored in cache. This data can be preloaded.
|
|
52
|
-
* It is used as condition to emit SDK_SPLITS_CACHE_LOADED, and then SDK_READY_FROM_CACHE.
|
|
53
|
-
*/
|
|
54
|
-
checkCache(): boolean {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
50
|
/**
|
|
59
51
|
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
|
|
60
52
|
* Used for SPLIT_KILL push notifications.
|
|
@@ -43,6 +43,10 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
|
|
|
43
43
|
buildTillKey() {
|
|
44
44
|
return `${this.prefix}.${this.matchingKey}.segments.till`;
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
buildLastClear() {
|
|
48
|
+
return `${this.prefix}.lastClear`;
|
|
49
|
+
}
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
export function myLargeSegmentsKeyBuilder(prefix: string, matchingKey: string): MySegmentsKeyBuilder {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { PreloadedData } from '../types';
|
|
2
|
-
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../utils/constants/browser';
|
|
3
2
|
import { DataLoader, ISegmentsCacheSync, ISplitsCacheSync } from './types';
|
|
4
3
|
|
|
4
|
+
// This value might be eventually set via a config parameter
|
|
5
|
+
const DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* Factory of client-side storage loader
|
|
7
9
|
*
|
|
@@ -5,7 +5,6 @@ import { KeyBuilderCS } from '../KeyBuilderCS';
|
|
|
5
5
|
import { ILogger } from '../../logger/types';
|
|
6
6
|
import { LOG_PREFIX } from './constants';
|
|
7
7
|
import { ISettings } from '../../types';
|
|
8
|
-
import { getStorageHash } from '../KeyBuilder';
|
|
9
8
|
import { setToArray } from '../../utils/lang/sets';
|
|
10
9
|
|
|
11
10
|
/**
|
|
@@ -15,21 +14,14 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
15
14
|
|
|
16
15
|
private readonly keys: KeyBuilderCS;
|
|
17
16
|
private readonly log: ILogger;
|
|
18
|
-
private readonly storageHash: string;
|
|
19
17
|
private readonly flagSetsFilter: string[];
|
|
20
18
|
private hasSync?: boolean;
|
|
21
|
-
private updateNewFilter?: boolean;
|
|
22
19
|
|
|
23
|
-
constructor(settings: ISettings, keys: KeyBuilderCS
|
|
20
|
+
constructor(settings: ISettings, keys: KeyBuilderCS) {
|
|
24
21
|
super();
|
|
25
22
|
this.keys = keys;
|
|
26
23
|
this.log = settings.log;
|
|
27
|
-
this.storageHash = getStorageHash(settings);
|
|
28
24
|
this.flagSetsFilter = settings.sync.__splitFiltersValidation.groupedFilters.bySet;
|
|
29
|
-
|
|
30
|
-
this._checkExpiration(expirationTimestamp);
|
|
31
|
-
|
|
32
|
-
this._checkFilterQuery();
|
|
33
25
|
}
|
|
34
26
|
|
|
35
27
|
private _decrementCount(key: string) {
|
|
@@ -79,8 +71,6 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
79
71
|
* We cannot simply call `localStorage.clear()` since that implies removing user items from the storage.
|
|
80
72
|
*/
|
|
81
73
|
clear() {
|
|
82
|
-
this.log.info(LOG_PREFIX + 'Flushing Splits data from localStorage');
|
|
83
|
-
|
|
84
74
|
// collect item keys
|
|
85
75
|
const len = localStorage.length;
|
|
86
76
|
const accum = [];
|
|
@@ -138,19 +128,6 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
138
128
|
}
|
|
139
129
|
|
|
140
130
|
setChangeNumber(changeNumber: number): boolean {
|
|
141
|
-
|
|
142
|
-
// when using a new split query, we must update it at the store
|
|
143
|
-
if (this.updateNewFilter) {
|
|
144
|
-
this.log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache');
|
|
145
|
-
const storageHashKey = this.keys.buildHashKey();
|
|
146
|
-
try {
|
|
147
|
-
localStorage.setItem(storageHashKey, this.storageHash);
|
|
148
|
-
} catch (e) {
|
|
149
|
-
this.log.error(LOG_PREFIX + e);
|
|
150
|
-
}
|
|
151
|
-
this.updateNewFilter = false;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
131
|
try {
|
|
155
132
|
localStorage.setItem(this.keys.buildSplitsTillKey(), changeNumber + '');
|
|
156
133
|
// update "last updated" timestamp with current time
|
|
@@ -212,48 +189,6 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
|
|
|
212
189
|
}
|
|
213
190
|
}
|
|
214
191
|
|
|
215
|
-
/**
|
|
216
|
-
* Check if the splits information is already stored in browser LocalStorage.
|
|
217
|
-
* In this function we could add more code to check if the data is valid.
|
|
218
|
-
* @override
|
|
219
|
-
*/
|
|
220
|
-
checkCache(): boolean {
|
|
221
|
-
return this.getChangeNumber() > -1;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`,
|
|
226
|
-
*
|
|
227
|
-
* @param expirationTimestamp - if the value is not a number, data will not be cleaned
|
|
228
|
-
*/
|
|
229
|
-
private _checkExpiration(expirationTimestamp?: number) {
|
|
230
|
-
let value: string | number | null = localStorage.getItem(this.keys.buildLastUpdatedKey());
|
|
231
|
-
if (value !== null) {
|
|
232
|
-
value = parseInt(value, 10);
|
|
233
|
-
if (!isNaNNumber(value) && expirationTimestamp && value < expirationTimestamp) this.clear();
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// @TODO eventually remove `_checkFilterQuery`. Cache should be cleared at the storage level, reusing same logic than PluggableStorage
|
|
238
|
-
private _checkFilterQuery() {
|
|
239
|
-
const storageHashKey = this.keys.buildHashKey();
|
|
240
|
-
const storageHash = localStorage.getItem(storageHashKey);
|
|
241
|
-
|
|
242
|
-
if (storageHash !== this.storageHash) {
|
|
243
|
-
try {
|
|
244
|
-
// mark cache to update the new query filter on first successful splits fetch
|
|
245
|
-
this.updateNewFilter = true;
|
|
246
|
-
|
|
247
|
-
// if there is cache, clear it
|
|
248
|
-
if (this.checkCache()) this.clear();
|
|
249
|
-
|
|
250
|
-
} catch (e) {
|
|
251
|
-
this.log.error(LOG_PREFIX + e);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
// if the filter didn't change, nothing is done
|
|
255
|
-
}
|
|
256
|
-
|
|
257
192
|
getNamesByFlagSets(flagSets: string[]): Set<string>[] {
|
|
258
193
|
return flagSets.map(flagSet => {
|
|
259
194
|
const flagSetKey = this.keys.buildFlagSetKey(flagSet);
|