@splitsoftware/splitio-commons 1.2.1-rc.7 → 1.2.1-rc.8

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.
@@ -21,8 +21,7 @@ function clientFactory(params) {
21
21
  var wrapUp = function (evaluationResult) {
22
22
  var queue = [];
23
23
  var treatment = processEvaluation(evaluationResult, splitName, key, attributes, withConfig, "getTreatment" + (withConfig ? 'withConfig' : ''), queue);
24
- if (settings.userConsent !== constants_1.CONSENT_DECLINED)
25
- impressionsTracker.track(queue, attributes);
24
+ impressionsTracker.track(queue, attributes);
26
25
  return treatment;
27
26
  };
28
27
  var evaluation = (0, evaluator_1.evaluateFeature)(log, key, splitName, attributes, storage);
@@ -39,8 +38,7 @@ function clientFactory(params) {
39
38
  Object.keys(evaluationResults).forEach(function (splitName) {
40
39
  treatments[splitName] = processEvaluation(evaluationResults[splitName], splitName, key, attributes, withConfig, "getTreatments" + (withConfig ? 'withConfig' : ''), queue);
41
40
  });
42
- if (settings.userConsent !== constants_1.CONSENT_DECLINED)
43
- impressionsTracker.track(queue, attributes);
41
+ impressionsTracker.track(queue, attributes);
44
42
  return treatments;
45
43
  };
46
44
  var evaluations = (0, evaluator_1.evaluateFeatures)(log, key, splitNames, attributes, storage);
@@ -94,10 +92,7 @@ function clientFactory(params) {
94
92
  };
95
93
  // This may be async but we only warn, we don't actually care if it is valid or not in terms of queueing the event.
96
94
  (0, trafficTypeExistance_1.validateTrafficTypeExistance)(log, readinessManager, storage.splits, mode, trafficTypeName, 'track');
97
- if (settings.userConsent !== constants_1.CONSENT_DECLINED)
98
- return eventTracker.track(eventData, size);
99
- else
100
- return false;
95
+ return eventTracker.track(eventData, size);
101
96
  }
102
97
  return {
103
98
  getTreatment: getTreatment,
@@ -5,12 +5,14 @@ var objectAssign_1 = require("../utils/lang/objectAssign");
5
5
  var inputValidation_1 = require("../utils/inputValidation");
6
6
  var lang_1 = require("../utils/lang");
7
7
  var constants_1 = require("../utils/constants");
8
+ var utils_1 = require("../trackers/impressionObserver/utils");
8
9
  /**
9
10
  * Decorator that validates the input before actually executing the client methods.
10
11
  * We should "guard" the client here, while not polluting the "real" implementation of those methods.
11
12
  */
12
- function clientInputValidationDecorator(log, client, readinessManager, isStorageSync) {
13
- if (isStorageSync === void 0) { isStorageSync = false; }
13
+ function clientInputValidationDecorator(settings, client, readinessManager) {
14
+ var log = settings.log;
15
+ var isSync = (0, utils_1.isStorageSync)(settings);
14
16
  /**
15
17
  * Avoid repeating this validations code
16
18
  */
@@ -30,9 +32,7 @@ function clientInputValidationDecorator(log, client, readinessManager, isStorage
30
32
  };
31
33
  }
32
34
  function wrapResult(value) {
33
- if (isStorageSync)
34
- return value;
35
- return Promise.resolve(value);
35
+ return isSync ? value : Promise.resolve(value);
36
36
  }
37
37
  function getTreatment(maybeKey, maybeSplit, maybeAttributes) {
38
38
  var params = validateEvaluationParams(maybeKey, maybeSplit, maybeAttributes, 'getTreatment');
@@ -87,9 +87,7 @@ function clientInputValidationDecorator(log, client, readinessManager, isStorage
87
87
  return client.track(key, tt, event, eventValue, properties, size);
88
88
  }
89
89
  else {
90
- if (isStorageSync)
91
- return false;
92
- return Promise.resolve(false);
90
+ return isSync ? false : Promise.resolve(false);
93
91
  }
94
92
  }
95
93
  return {
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sdkClientFactory = void 0;
4
4
  var objectAssign_1 = require("../utils/lang/objectAssign");
5
- var constants_1 = require("../utils/constants");
6
5
  var apiKey_1 = require("../utils/inputValidation/apiKey");
7
6
  var client_1 = require("./client");
8
7
  var clientInputValidation_1 = require("./clientInputValidation");
@@ -15,9 +14,7 @@ function sdkClientFactory(params) {
15
14
  // Proto-linkage of the readiness Event Emitter
16
15
  Object.create(sdkReadinessManager.sdkStatus),
17
16
  // Client API (getTreatment* & track methods)
18
- (0, clientInputValidation_1.clientInputValidationDecorator)(settings.log, (0, client_1.clientFactory)(params), sdkReadinessManager.readinessManager,
19
- // storage is async if and only if mode is consumer or partial consumer
20
- [constants_1.CONSUMER_MODE, constants_1.CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false),
17
+ (0, clientInputValidation_1.clientInputValidationDecorator)(settings, (0, client_1.clientFactory)(params), sdkReadinessManager.readinessManager),
21
18
  // Sdk destroy
22
19
  {
23
20
  destroy: function () {
@@ -16,7 +16,7 @@ var objectAssign_1 = require("../utils/lang/objectAssign");
16
16
  * Modular SDK factory
17
17
  */
18
18
  function sdkFactory(params) {
19
- var settings = params.settings, platform = params.platform, storageFactory = params.storageFactory, splitApiFactory = params.splitApiFactory, extraProps = params.extraProps, syncManagerFactory = params.syncManagerFactory, SignalListener = params.SignalListener, impressionsObserverFactory = params.impressionsObserverFactory, impressionListener = params.impressionListener, integrationsManagerFactory = params.integrationsManagerFactory, sdkManagerFactory = params.sdkManagerFactory, sdkClientMethodFactory = params.sdkClientMethodFactory;
19
+ var settings = params.settings, platform = params.platform, storageFactory = params.storageFactory, splitApiFactory = params.splitApiFactory, extraProps = params.extraProps, syncManagerFactory = params.syncManagerFactory, SignalListener = params.SignalListener, impressionsObserverFactory = params.impressionsObserverFactory, integrationsManagerFactory = params.integrationsManagerFactory, sdkManagerFactory = params.sdkManagerFactory, sdkClientMethodFactory = params.sdkClientMethodFactory;
20
20
  var log = settings.log;
21
21
  // @TODO handle non-recoverable errors: not start sync, mark the SDK as destroyed, etc.
22
22
  // We will just log and allow for the SDK to end up throwing an SDK_TIMEOUT event for devs to handle.
@@ -59,8 +59,8 @@ function sdkFactory(params) {
59
59
  var integrationsManager = integrationsManagerFactory && integrationsManagerFactory({ settings: settings, storage: storage });
60
60
  // trackers
61
61
  var observer = impressionsObserverFactory && impressionsObserverFactory();
62
- var impressionsTracker = (0, impressionsTracker_1.impressionsTrackerFactory)(log, storage.impressions, settings, impressionListener, integrationsManager, observer, storage.impressionCounts);
63
- var eventTracker = (0, eventTracker_1.eventTrackerFactory)(log, storage.events, integrationsManager);
62
+ var impressionsTracker = (0, impressionsTracker_1.impressionsTrackerFactory)(settings, storage.impressions, integrationsManager, observer, storage.impressionCounts);
63
+ var eventTracker = (0, eventTracker_1.eventTrackerFactory)(settings, storage.events, integrationsManager);
64
64
  // signal listener
65
65
  var signalListener = SignalListener && new SignalListener(syncManager, settings, storage, splitApi);
66
66
  // Sdk client and manager
@@ -4,13 +4,17 @@ exports.eventTrackerFactory = void 0;
4
4
  var objectAssign_1 = require("../utils/lang/objectAssign");
5
5
  var thenable_1 = require("../utils/promise/thenable");
6
6
  var constants_1 = require("../logger/constants");
7
+ var constants_2 = require("../utils/constants");
8
+ var utils_1 = require("./impressionObserver/utils");
7
9
  /**
8
10
  * Event tracker stores events in cache and pass them to the integrations manager if provided.
9
11
  *
10
12
  * @param eventsCache cache to save events
11
13
  * @param integrationsManager optional event handler used for integrations
12
14
  */
13
- function eventTrackerFactory(log, eventsCache, integrationsManager) {
15
+ function eventTrackerFactory(settings, eventsCache, integrationsManager) {
16
+ var log = settings.log;
17
+ var isSync = (0, utils_1.isStorageSync)(settings);
14
18
  function queueEventsCallback(eventData, tracked) {
15
19
  var eventTypeId = eventData.eventTypeId, trafficTypeName = eventData.trafficTypeName, key = eventData.key, value = eventData.value, timestamp = eventData.timestamp, properties = eventData.properties;
16
20
  // Logging every prop would be too much.
@@ -36,6 +40,9 @@ function eventTrackerFactory(log, eventsCache, integrationsManager) {
36
40
  }
37
41
  return {
38
42
  track: function (eventData, size) {
43
+ if (settings.userConsent === constants_2.CONSENT_DECLINED) {
44
+ return isSync ? false : Promise.resolve(false);
45
+ }
39
46
  var tracked = eventsCache.track(eventData, size);
40
47
  if ((0, thenable_1.thenable)(tracked)) {
41
48
  return tracked.then(queueEventsCallback.bind(null, eventData));
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.shouldBeOptimized = exports.shouldAddPt = void 0;
3
+ exports.isStorageSync = exports.shouldBeOptimized = exports.shouldAddPt = void 0;
4
4
  var constants_1 = require("../../utils/constants");
5
5
  /**
6
6
  * Checks if impressions previous time should be added or not.
@@ -18,3 +18,10 @@ function shouldBeOptimized(settings) {
18
18
  return settings.sync.impressionsMode === constants_1.OPTIMIZED ? true : false;
19
19
  }
20
20
  exports.shouldBeOptimized = shouldBeOptimized;
21
+ /**
22
+ * Storage is async if mode is consumer or partial consumer
23
+ */
24
+ function isStorageSync(settings) {
25
+ return [constants_1.CONSUMER_MODE, constants_1.CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false;
26
+ }
27
+ exports.isStorageSync = isStorageSync;
@@ -5,6 +5,7 @@ var objectAssign_1 = require("../utils/lang/objectAssign");
5
5
  var thenable_1 = require("../utils/promise/thenable");
6
6
  var time_1 = require("../utils/time");
7
7
  var constants_1 = require("../logger/constants");
8
+ var constants_2 = require("../utils/constants");
8
9
  /**
9
10
  * Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided.
10
11
  *
@@ -15,16 +16,16 @@ var constants_1 = require("../logger/constants");
15
16
  * @param observer optional impression observer. If provided, previous time (pt property) is included in impression instances
16
17
  * @param countsCache optional cache to save impressions count. If provided, impressions will be deduped (OPTIMIZED mode)
17
18
  */
18
- function impressionsTrackerFactory(log, impressionsCache,
19
- // @TODO consider passing only an optional integrationsManager to handle impressions
20
- _a, impressionListener, integrationsManager,
19
+ function impressionsTrackerFactory(settings, impressionsCache, integrationsManager,
21
20
  // if observer is provided, it implies `shouldAddPreviousTime` flag (i.e., if impressions previous time should be added or not)
22
21
  observer,
23
22
  // if countsCache is provided, it implies `isOptimized` flag (i.e., if impressions should be deduped or not)
24
23
  countsCache) {
25
- var _b = _a.runtime, ip = _b.ip, hostname = _b.hostname, version = _a.version;
24
+ var log = settings.log, impressionListener = settings.impressionListener, _a = settings.runtime, ip = _a.ip, hostname = _a.hostname, version = settings.version;
26
25
  return {
27
26
  track: function (impressions, attributes) {
27
+ if (settings.userConsent === constants_2.CONSENT_DECLINED)
28
+ return;
28
29
  var impressionsCount = impressions.length;
29
30
  var impressionsToStore = []; // Track only the impressions that are going to be stored
30
31
  // Wraps impressions to store and adds previousTime if it corresponds
@@ -68,7 +69,7 @@ countsCache) {
68
69
  // integrationsManager.handleImpression does not throw errors
69
70
  if (integrationsManager)
70
71
  integrationsManager.handleImpression(impressionData);
71
- try { // An exception on the listeners should not break the SDK.
72
+ try { // @ts-ignore. An exception on the listeners should not break the SDK.
72
73
  if (impressionListener)
73
74
  impressionListener.logImpression(impressionData);
74
75
  }
@@ -4,7 +4,7 @@ import { getMatching, getBucketing } from '../utils/key';
4
4
  import { validateSplitExistance } from '../utils/inputValidation/splitExistance';
5
5
  import { validateTrafficTypeExistance } from '../utils/inputValidation/trafficTypeExistance';
6
6
  import { SDK_NOT_READY } from '../utils/labels';
7
- import { CONSENT_DECLINED, CONTROL } from '../utils/constants';
7
+ import { CONTROL } from '../utils/constants';
8
8
  import { IMPRESSION, IMPRESSION_QUEUEING } from '../logger/constants';
9
9
  /**
10
10
  * Creator of base client with getTreatments and track methods.
@@ -18,8 +18,7 @@ export function clientFactory(params) {
18
18
  var wrapUp = function (evaluationResult) {
19
19
  var queue = [];
20
20
  var treatment = processEvaluation(evaluationResult, splitName, key, attributes, withConfig, "getTreatment" + (withConfig ? 'withConfig' : ''), queue);
21
- if (settings.userConsent !== CONSENT_DECLINED)
22
- impressionsTracker.track(queue, attributes);
21
+ impressionsTracker.track(queue, attributes);
23
22
  return treatment;
24
23
  };
25
24
  var evaluation = evaluateFeature(log, key, splitName, attributes, storage);
@@ -36,8 +35,7 @@ export function clientFactory(params) {
36
35
  Object.keys(evaluationResults).forEach(function (splitName) {
37
36
  treatments[splitName] = processEvaluation(evaluationResults[splitName], splitName, key, attributes, withConfig, "getTreatments" + (withConfig ? 'withConfig' : ''), queue);
38
37
  });
39
- if (settings.userConsent !== CONSENT_DECLINED)
40
- impressionsTracker.track(queue, attributes);
38
+ impressionsTracker.track(queue, attributes);
41
39
  return treatments;
42
40
  };
43
41
  var evaluations = evaluateFeatures(log, key, splitNames, attributes, storage);
@@ -91,10 +89,7 @@ export function clientFactory(params) {
91
89
  };
92
90
  // This may be async but we only warn, we don't actually care if it is valid or not in terms of queueing the event.
93
91
  validateTrafficTypeExistance(log, readinessManager, storage.splits, mode, trafficTypeName, 'track');
94
- if (settings.userConsent !== CONSENT_DECLINED)
95
- return eventTracker.track(eventData, size);
96
- else
97
- return false;
92
+ return eventTracker.track(eventData, size);
98
93
  }
99
94
  return {
100
95
  getTreatment: getTreatment,
@@ -2,12 +2,14 @@ import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { validateAttributes, validateEvent, validateEventValue, validateEventProperties, validateKey, validateSplit, validateSplits, validateTrafficType, validateIfNotDestroyed, validateIfOperational } from '../utils/inputValidation';
3
3
  import { startsWith } from '../utils/lang';
4
4
  import { CONTROL, CONTROL_WITH_CONFIG } from '../utils/constants';
5
+ import { isStorageSync } from '../trackers/impressionObserver/utils';
5
6
  /**
6
7
  * Decorator that validates the input before actually executing the client methods.
7
8
  * We should "guard" the client here, while not polluting the "real" implementation of those methods.
8
9
  */
9
- export function clientInputValidationDecorator(log, client, readinessManager, isStorageSync) {
10
- if (isStorageSync === void 0) { isStorageSync = false; }
10
+ export function clientInputValidationDecorator(settings, client, readinessManager) {
11
+ var log = settings.log;
12
+ var isSync = isStorageSync(settings);
11
13
  /**
12
14
  * Avoid repeating this validations code
13
15
  */
@@ -27,9 +29,7 @@ export function clientInputValidationDecorator(log, client, readinessManager, is
27
29
  };
28
30
  }
29
31
  function wrapResult(value) {
30
- if (isStorageSync)
31
- return value;
32
- return Promise.resolve(value);
32
+ return isSync ? value : Promise.resolve(value);
33
33
  }
34
34
  function getTreatment(maybeKey, maybeSplit, maybeAttributes) {
35
35
  var params = validateEvaluationParams(maybeKey, maybeSplit, maybeAttributes, 'getTreatment');
@@ -84,9 +84,7 @@ export function clientInputValidationDecorator(log, client, readinessManager, is
84
84
  return client.track(key, tt, event, eventValue, properties, size);
85
85
  }
86
86
  else {
87
- if (isStorageSync)
88
- return false;
89
- return Promise.resolve(false);
87
+ return isSync ? false : Promise.resolve(false);
90
88
  }
91
89
  }
92
90
  return {
@@ -1,5 +1,4 @@
1
1
  import { objectAssign } from '../utils/lang/objectAssign';
2
- import { CONSUMER_MODE, CONSUMER_PARTIAL_MODE } from '../utils/constants';
3
2
  import { releaseApiKey } from '../utils/inputValidation/apiKey';
4
3
  import { clientFactory } from './client';
5
4
  import { clientInputValidationDecorator } from './clientInputValidation';
@@ -12,9 +11,7 @@ export function sdkClientFactory(params) {
12
11
  // Proto-linkage of the readiness Event Emitter
13
12
  Object.create(sdkReadinessManager.sdkStatus),
14
13
  // Client API (getTreatment* & track methods)
15
- clientInputValidationDecorator(settings.log, clientFactory(params), sdkReadinessManager.readinessManager,
16
- // storage is async if and only if mode is consumer or partial consumer
17
- [CONSUMER_MODE, CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false),
14
+ clientInputValidationDecorator(settings, clientFactory(params), sdkReadinessManager.readinessManager),
18
15
  // Sdk destroy
19
16
  {
20
17
  destroy: function () {
@@ -13,7 +13,7 @@ import { objectAssign } from '../utils/lang/objectAssign';
13
13
  * Modular SDK factory
14
14
  */
15
15
  export function sdkFactory(params) {
16
- var settings = params.settings, platform = params.platform, storageFactory = params.storageFactory, splitApiFactory = params.splitApiFactory, extraProps = params.extraProps, syncManagerFactory = params.syncManagerFactory, SignalListener = params.SignalListener, impressionsObserverFactory = params.impressionsObserverFactory, impressionListener = params.impressionListener, integrationsManagerFactory = params.integrationsManagerFactory, sdkManagerFactory = params.sdkManagerFactory, sdkClientMethodFactory = params.sdkClientMethodFactory;
16
+ var settings = params.settings, platform = params.platform, storageFactory = params.storageFactory, splitApiFactory = params.splitApiFactory, extraProps = params.extraProps, syncManagerFactory = params.syncManagerFactory, SignalListener = params.SignalListener, impressionsObserverFactory = params.impressionsObserverFactory, integrationsManagerFactory = params.integrationsManagerFactory, sdkManagerFactory = params.sdkManagerFactory, sdkClientMethodFactory = params.sdkClientMethodFactory;
17
17
  var log = settings.log;
18
18
  // @TODO handle non-recoverable errors: not start sync, mark the SDK as destroyed, etc.
19
19
  // We will just log and allow for the SDK to end up throwing an SDK_TIMEOUT event for devs to handle.
@@ -56,8 +56,8 @@ export function sdkFactory(params) {
56
56
  var integrationsManager = integrationsManagerFactory && integrationsManagerFactory({ settings: settings, storage: storage });
57
57
  // trackers
58
58
  var observer = impressionsObserverFactory && impressionsObserverFactory();
59
- var impressionsTracker = impressionsTrackerFactory(log, storage.impressions, settings, impressionListener, integrationsManager, observer, storage.impressionCounts);
60
- var eventTracker = eventTrackerFactory(log, storage.events, integrationsManager);
59
+ var impressionsTracker = impressionsTrackerFactory(settings, storage.impressions, integrationsManager, observer, storage.impressionCounts);
60
+ var eventTracker = eventTrackerFactory(settings, storage.events, integrationsManager);
61
61
  // signal listener
62
62
  var signalListener = SignalListener && new SignalListener(syncManager, settings, storage, splitApi);
63
63
  // Sdk client and manager
@@ -1,13 +1,17 @@
1
1
  import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { thenable } from '../utils/promise/thenable';
3
3
  import { EVENTS_TRACKER_SUCCESS, ERROR_EVENTS_TRACKER } from '../logger/constants';
4
+ import { CONSENT_DECLINED } from '../utils/constants';
5
+ import { isStorageSync } from './impressionObserver/utils';
4
6
  /**
5
7
  * Event tracker stores events in cache and pass them to the integrations manager if provided.
6
8
  *
7
9
  * @param eventsCache cache to save events
8
10
  * @param integrationsManager optional event handler used for integrations
9
11
  */
10
- export function eventTrackerFactory(log, eventsCache, integrationsManager) {
12
+ export function eventTrackerFactory(settings, eventsCache, integrationsManager) {
13
+ var log = settings.log;
14
+ var isSync = isStorageSync(settings);
11
15
  function queueEventsCallback(eventData, tracked) {
12
16
  var eventTypeId = eventData.eventTypeId, trafficTypeName = eventData.trafficTypeName, key = eventData.key, value = eventData.value, timestamp = eventData.timestamp, properties = eventData.properties;
13
17
  // Logging every prop would be too much.
@@ -33,6 +37,9 @@ export function eventTrackerFactory(log, eventsCache, integrationsManager) {
33
37
  }
34
38
  return {
35
39
  track: function (eventData, size) {
40
+ if (settings.userConsent === CONSENT_DECLINED) {
41
+ return isSync ? false : Promise.resolve(false);
42
+ }
36
43
  var tracked = eventsCache.track(eventData, size);
37
44
  if (thenable(tracked)) {
38
45
  return tracked.then(queueEventsCallback.bind(null, eventData));
@@ -1,4 +1,4 @@
1
- import { CONSUMER_PARTIAL_MODE, OPTIMIZED, PRODUCER_MODE, STANDALONE_MODE } from '../../utils/constants';
1
+ import { CONSUMER_MODE, CONSUMER_PARTIAL_MODE, OPTIMIZED, PRODUCER_MODE, STANDALONE_MODE } from '../../utils/constants';
2
2
  /**
3
3
  * Checks if impressions previous time should be added or not.
4
4
  */
@@ -13,3 +13,9 @@ export function shouldBeOptimized(settings) {
13
13
  return false;
14
14
  return settings.sync.impressionsMode === OPTIMIZED ? true : false;
15
15
  }
16
+ /**
17
+ * Storage is async if mode is consumer or partial consumer
18
+ */
19
+ export function isStorageSync(settings) {
20
+ return [CONSUMER_MODE, CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false;
21
+ }
@@ -2,6 +2,7 @@ import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { thenable } from '../utils/promise/thenable';
3
3
  import { truncateTimeFrame } from '../utils/time';
4
4
  import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIONS_LISTENER } from '../logger/constants';
5
+ import { CONSENT_DECLINED } from '../utils/constants';
5
6
  /**
6
7
  * Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided.
7
8
  *
@@ -12,16 +13,16 @@ import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIO
12
13
  * @param observer optional impression observer. If provided, previous time (pt property) is included in impression instances
13
14
  * @param countsCache optional cache to save impressions count. If provided, impressions will be deduped (OPTIMIZED mode)
14
15
  */
15
- export function impressionsTrackerFactory(log, impressionsCache,
16
- // @TODO consider passing only an optional integrationsManager to handle impressions
17
- _a, impressionListener, integrationsManager,
16
+ export function impressionsTrackerFactory(settings, impressionsCache, integrationsManager,
18
17
  // if observer is provided, it implies `shouldAddPreviousTime` flag (i.e., if impressions previous time should be added or not)
19
18
  observer,
20
19
  // if countsCache is provided, it implies `isOptimized` flag (i.e., if impressions should be deduped or not)
21
20
  countsCache) {
22
- var _b = _a.runtime, ip = _b.ip, hostname = _b.hostname, version = _a.version;
21
+ var log = settings.log, impressionListener = settings.impressionListener, _a = settings.runtime, ip = _a.ip, hostname = _a.hostname, version = settings.version;
23
22
  return {
24
23
  track: function (impressions, attributes) {
24
+ if (settings.userConsent === CONSENT_DECLINED)
25
+ return;
25
26
  var impressionsCount = impressions.length;
26
27
  var impressionsToStore = []; // Track only the impressions that are going to be stored
27
28
  // Wraps impressions to store and adds previousTime if it corresponds
@@ -65,7 +66,7 @@ countsCache) {
65
66
  // integrationsManager.handleImpression does not throw errors
66
67
  if (integrationsManager)
67
68
  integrationsManager.handleImpression(impressionData);
68
- try { // An exception on the listeners should not break the SDK.
69
+ try { // @ts-ignore. An exception on the listeners should not break the SDK.
69
70
  if (impressionListener)
70
71
  impressionListener.logImpression(impressionData);
71
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.2.1-rc.7",
3
+ "version": "1.2.1-rc.8",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -4,7 +4,7 @@ import { getMatching, getBucketing } from '../utils/key';
4
4
  import { validateSplitExistance } from '../utils/inputValidation/splitExistance';
5
5
  import { validateTrafficTypeExistance } from '../utils/inputValidation/trafficTypeExistance';
6
6
  import { SDK_NOT_READY } from '../utils/labels';
7
- import { CONSENT_DECLINED, CONTROL } from '../utils/constants';
7
+ import { CONTROL } from '../utils/constants';
8
8
  import { IClientFactoryParams } from './types';
9
9
  import { IEvaluationResult } from '../evaluator/types';
10
10
  import { SplitIO, ImpressionDTO } from '../types';
@@ -23,7 +23,7 @@ export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | S
23
23
  const wrapUp = (evaluationResult: IEvaluationResult) => {
24
24
  const queue: ImpressionDTO[] = [];
25
25
  const treatment = processEvaluation(evaluationResult, splitName, key, attributes, withConfig, `getTreatment${withConfig ? 'withConfig' : ''}`, queue);
26
- if (settings.userConsent !== CONSENT_DECLINED) impressionsTracker.track(queue, attributes);
26
+ impressionsTracker.track(queue, attributes);
27
27
  return treatment;
28
28
  };
29
29
 
@@ -43,7 +43,7 @@ export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | S
43
43
  Object.keys(evaluationResults).forEach(splitName => {
44
44
  treatments[splitName] = processEvaluation(evaluationResults[splitName], splitName, key, attributes, withConfig, `getTreatments${withConfig ? 'withConfig' : ''}`, queue);
45
45
  });
46
- if (settings.userConsent !== CONSENT_DECLINED) impressionsTracker.track(queue, attributes);
46
+ impressionsTracker.track(queue, attributes);
47
47
  return treatments;
48
48
  };
49
49
 
@@ -116,8 +116,7 @@ export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | S
116
116
  // This may be async but we only warn, we don't actually care if it is valid or not in terms of queueing the event.
117
117
  validateTrafficTypeExistance(log, readinessManager, storage.splits, mode, trafficTypeName, 'track');
118
118
 
119
- if (settings.userConsent !== CONSENT_DECLINED) return eventTracker.track(eventData, size);
120
- else return false;
119
+ return eventTracker.track(eventData, size);
121
120
  }
122
121
 
123
122
  return {
@@ -15,14 +15,17 @@ import { startsWith } from '../utils/lang';
15
15
  import { CONTROL, CONTROL_WITH_CONFIG } from '../utils/constants';
16
16
  import { IReadinessManager } from '../readiness/types';
17
17
  import { MaybeThenable } from '../dtos/types';
18
- import { SplitIO } from '../types';
19
- import { ILogger } from '../logger/types';
18
+ import { ISettings, SplitIO } from '../types';
19
+ import { isStorageSync } from '../trackers/impressionObserver/utils';
20
20
 
21
21
  /**
22
22
  * Decorator that validates the input before actually executing the client methods.
23
23
  * We should "guard" the client here, while not polluting the "real" implementation of those methods.
24
24
  */
25
- export function clientInputValidationDecorator<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(log: ILogger, client: TClient, readinessManager: IReadinessManager, isStorageSync = false): TClient {
25
+ export function clientInputValidationDecorator<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(settings: ISettings, client: TClient, readinessManager: IReadinessManager): TClient {
26
+
27
+ const log = settings.log;
28
+ const isSync = isStorageSync(settings);
26
29
 
27
30
  /**
28
31
  * Avoid repeating this validations code
@@ -47,8 +50,7 @@ export function clientInputValidationDecorator<TClient extends SplitIO.IClient |
47
50
  }
48
51
 
49
52
  function wrapResult<T>(value: T): MaybeThenable<T> {
50
- if (isStorageSync) return value;
51
- return Promise.resolve(value);
53
+ return isSync ? value : Promise.resolve(value);
52
54
  }
53
55
 
54
56
  function getTreatment(maybeKey: SplitIO.SplitKey, maybeSplit: string, maybeAttributes?: SplitIO.Attributes) {
@@ -108,8 +110,7 @@ export function clientInputValidationDecorator<TClient extends SplitIO.IClient |
108
110
  if (isOperational && key && tt && event && eventValue !== false && properties !== false) { // @ts-expect-error
109
111
  return client.track(key, tt, event, eventValue, properties, size);
110
112
  } else {
111
- if (isStorageSync) return false;
112
- return Promise.resolve(false);
113
+ return isSync ? false : Promise.resolve(false);
113
114
  }
114
115
  }
115
116
 
@@ -1,6 +1,5 @@
1
1
  import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { IStatusInterface, SplitIO } from '../types';
3
- import { CONSUMER_MODE, CONSUMER_PARTIAL_MODE } from '../utils/constants';
4
3
  import { releaseApiKey } from '../utils/inputValidation/apiKey';
5
4
  import { clientFactory } from './client';
6
5
  import { clientInputValidationDecorator } from './clientInputValidation';
@@ -18,11 +17,9 @@ export function sdkClientFactory(params: ISdkClientFactoryParams): SplitIO.IClie
18
17
 
19
18
  // Client API (getTreatment* & track methods)
20
19
  clientInputValidationDecorator(
21
- settings.log,
20
+ settings,
22
21
  clientFactory(params),
23
- sdkReadinessManager.readinessManager,
24
- // storage is async if and only if mode is consumer or partial consumer
25
- [CONSUMER_MODE, CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false,
22
+ sdkReadinessManager.readinessManager
26
23
  ),
27
24
 
28
25
  // Sdk destroy
@@ -20,7 +20,7 @@ import { objectAssign } from '../utils/lang/objectAssign';
20
20
  export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.ISDK | SplitIO.IAsyncSDK {
21
21
 
22
22
  const { settings, platform, storageFactory, splitApiFactory, extraProps,
23
- syncManagerFactory, SignalListener, impressionsObserverFactory, impressionListener,
23
+ syncManagerFactory, SignalListener, impressionsObserverFactory,
24
24
  integrationsManagerFactory, sdkManagerFactory, sdkClientMethodFactory } = params;
25
25
  const log = settings.log;
26
26
 
@@ -74,8 +74,8 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
74
74
 
75
75
  // trackers
76
76
  const observer = impressionsObserverFactory && impressionsObserverFactory();
77
- const impressionsTracker = impressionsTrackerFactory(log, storage.impressions, settings, impressionListener, integrationsManager, observer, storage.impressionCounts);
78
- const eventTracker = eventTrackerFactory(log, storage.events, integrationsManager);
77
+ const impressionsTracker = impressionsTrackerFactory(settings, storage.impressions, integrationsManager, observer, storage.impressionCounts);
78
+ const eventTracker = eventTrackerFactory(settings, storage.events, integrationsManager);
79
79
 
80
80
  // signal listener
81
81
  const signalListener = SignalListener && new SignalListener(syncManager, settings, storage, splitApi);
@@ -64,7 +64,6 @@ export interface ISdkFactoryParams {
64
64
  serviceApi: ISplitApi | undefined) => ISignalListener, // Used by BrowserSignalListener
65
65
 
66
66
  // @TODO review impressionListener and integrations interfaces. What about handling impressionListener as an integration ?
67
- impressionListener?: SplitIO.IImpressionListener,
68
67
  integrationsManagerFactory?: (params: IIntegrationFactoryParams) => IIntegrationManager | undefined,
69
68
 
70
69
  // Impression observer factory. If provided, will be used for impressions dedupe
@@ -2,9 +2,10 @@ import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { thenable } from '../utils/promise/thenable';
3
3
  import { IEventsCacheBase } from '../storages/types';
4
4
  import { IEventsHandler, IEventTracker } from './types';
5
- import { SplitIO } from '../types';
6
- import { ILogger } from '../logger/types';
5
+ import { ISettings, SplitIO } from '../types';
7
6
  import { EVENTS_TRACKER_SUCCESS, ERROR_EVENTS_TRACKER } from '../logger/constants';
7
+ import { CONSENT_DECLINED } from '../utils/constants';
8
+ import { isStorageSync } from './impressionObserver/utils';
8
9
 
9
10
  /**
10
11
  * Event tracker stores events in cache and pass them to the integrations manager if provided.
@@ -13,11 +14,14 @@ import { EVENTS_TRACKER_SUCCESS, ERROR_EVENTS_TRACKER } from '../logger/constant
13
14
  * @param integrationsManager optional event handler used for integrations
14
15
  */
15
16
  export function eventTrackerFactory(
16
- log: ILogger,
17
+ settings: ISettings,
17
18
  eventsCache: IEventsCacheBase,
18
19
  integrationsManager?: IEventsHandler
19
20
  ): IEventTracker {
20
21
 
22
+ const log = settings.log;
23
+ const isSync = isStorageSync(settings);
24
+
21
25
  function queueEventsCallback(eventData: SplitIO.EventData, tracked: boolean) {
22
26
  const { eventTypeId, trafficTypeName, key, value, timestamp, properties } = eventData;
23
27
  // Logging every prop would be too much.
@@ -44,6 +48,10 @@ export function eventTrackerFactory(
44
48
 
45
49
  return {
46
50
  track(eventData: SplitIO.EventData, size?: number) {
51
+ if (settings.userConsent === CONSENT_DECLINED) {
52
+ return isSync ? false : Promise.resolve(false);
53
+ }
54
+
47
55
  const tracked = eventsCache.track(eventData, size);
48
56
 
49
57
  if (thenable(tracked)) {
@@ -1,4 +1,4 @@
1
- import { CONSUMER_PARTIAL_MODE, OPTIMIZED, PRODUCER_MODE, STANDALONE_MODE } from '../../utils/constants';
1
+ import { CONSUMER_MODE, CONSUMER_PARTIAL_MODE, OPTIMIZED, PRODUCER_MODE, STANDALONE_MODE } from '../../utils/constants';
2
2
  import { ISettings } from '../../types';
3
3
 
4
4
  /**
@@ -15,3 +15,10 @@ export function shouldBeOptimized(settings: ISettings) {
15
15
  if (!shouldAddPt(settings)) return false;
16
16
  return settings.sync.impressionsMode === OPTIMIZED ? true : false;
17
17
  }
18
+
19
+ /**
20
+ * Storage is async if mode is consumer or partial consumer
21
+ */
22
+ export function isStorageSync(settings: ISettings) {
23
+ return [CONSUMER_MODE, CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false;
24
+ }
@@ -5,8 +5,8 @@ import { IImpressionCountsCacheSync, IImpressionsCacheBase } from '../storages/t
5
5
  import { IImpressionsHandler, IImpressionsTracker } from './types';
6
6
  import { SplitIO, ImpressionDTO, ISettings } from '../types';
7
7
  import { IImpressionObserver } from './impressionObserver/types';
8
- import { ILogger } from '../logger/types';
9
8
  import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIONS_LISTENER } from '../logger/constants';
9
+ import { CONSENT_DECLINED } from '../utils/constants';
10
10
 
11
11
  /**
12
12
  * Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided.
@@ -19,22 +19,21 @@ import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIO
19
19
  * @param countsCache optional cache to save impressions count. If provided, impressions will be deduped (OPTIMIZED mode)
20
20
  */
21
21
  export function impressionsTrackerFactory(
22
- log: ILogger,
22
+ settings: ISettings,
23
23
  impressionsCache: IImpressionsCacheBase,
24
-
25
- // @TODO consider passing only an optional integrationsManager to handle impressions
26
- { runtime: { ip, hostname }, version }: Pick<ISettings, 'version' | 'runtime'>,
27
- impressionListener?: SplitIO.IImpressionListener,
28
24
  integrationsManager?: IImpressionsHandler,
29
-
30
25
  // if observer is provided, it implies `shouldAddPreviousTime` flag (i.e., if impressions previous time should be added or not)
31
26
  observer?: IImpressionObserver,
32
27
  // if countsCache is provided, it implies `isOptimized` flag (i.e., if impressions should be deduped or not)
33
28
  countsCache?: IImpressionCountsCacheSync
34
29
  ): IImpressionsTracker {
35
30
 
31
+ const { log, impressionListener, runtime: { ip, hostname }, version } = settings;
32
+
36
33
  return {
37
34
  track(impressions: ImpressionDTO[], attributes?: SplitIO.Attributes) {
35
+ if (settings.userConsent === CONSENT_DECLINED) return;
36
+
38
37
  const impressionsCount = impressions.length;
39
38
 
40
39
  const impressionsToStore: ImpressionDTO[] = []; // Track only the impressions that are going to be stored
@@ -85,7 +84,7 @@ export function impressionsTrackerFactory(
85
84
  // integrationsManager.handleImpression does not throw errors
86
85
  if (integrationsManager) integrationsManager.handleImpression(impressionData);
87
86
 
88
- try { // An exception on the listeners should not break the SDK.
87
+ try { // @ts-ignore. An exception on the listeners should not break the SDK.
89
88
  if (impressionListener) impressionListener.logImpression(impressionData);
90
89
  } catch (err) {
91
90
  log.error(ERROR_IMPRESSIONS_LISTENER, [err]);
@@ -1,8 +1,7 @@
1
1
  import { IReadinessManager } from '../readiness/types';
2
- import { SplitIO } from '../types';
3
- import { ILogger } from '../logger/types';
2
+ import { ISettings, SplitIO } from '../types';
4
3
  /**
5
4
  * Decorator that validates the input before actually executing the client methods.
6
5
  * We should "guard" the client here, while not polluting the "real" implementation of those methods.
7
6
  */
8
- export declare function clientInputValidationDecorator<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(log: ILogger, client: TClient, readinessManager: IReadinessManager, isStorageSync?: boolean): TClient;
7
+ export declare function clientInputValidationDecorator<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(settings: ISettings, client: TClient, readinessManager: IReadinessManager): TClient;
@@ -36,7 +36,6 @@ export interface ISdkFactoryParams {
36
36
  settings: ISettings, // Used by BrowserSignalListener
37
37
  storage: IStorageSync | IStorageAsync, // Used by BrowserSignalListener
38
38
  serviceApi: ISplitApi | undefined) => ISignalListener;
39
- impressionListener?: SplitIO.IImpressionListener;
40
39
  integrationsManagerFactory?: (params: IIntegrationFactoryParams) => IIntegrationManager | undefined;
41
40
  impressionsObserverFactory?: () => IImpressionObserver;
42
41
  extraProps?: (settings: ISettings, syncManager?: ISyncManager) => object;
@@ -1,10 +1,10 @@
1
1
  import { IEventsCacheBase } from '../storages/types';
2
2
  import { IEventsHandler, IEventTracker } from './types';
3
- import { ILogger } from '../logger/types';
3
+ import { ISettings } from '../types';
4
4
  /**
5
5
  * Event tracker stores events in cache and pass them to the integrations manager if provided.
6
6
  *
7
7
  * @param eventsCache cache to save events
8
8
  * @param integrationsManager optional event handler used for integrations
9
9
  */
10
- export declare function eventTrackerFactory(log: ILogger, eventsCache: IEventsCacheBase, integrationsManager?: IEventsHandler): IEventTracker;
10
+ export declare function eventTrackerFactory(settings: ISettings, eventsCache: IEventsCacheBase, integrationsManager?: IEventsHandler): IEventTracker;
@@ -7,3 +7,7 @@ export declare function shouldAddPt(settings: ISettings): boolean;
7
7
  * Checks if it should dedupe impressions or not.
8
8
  */
9
9
  export declare function shouldBeOptimized(settings: ISettings): boolean;
10
+ /**
11
+ * Storage is async if mode is consumer or partial consumer
12
+ */
13
+ export declare function isStorageSync(settings: ISettings): boolean;
@@ -1,8 +1,7 @@
1
1
  import { IImpressionCountsCacheSync, IImpressionsCacheBase } from '../storages/types';
2
2
  import { IImpressionsHandler, IImpressionsTracker } from './types';
3
- import { SplitIO, ISettings } from '../types';
3
+ import { ISettings } from '../types';
4
4
  import { IImpressionObserver } from './impressionObserver/types';
5
- import { ILogger } from '../logger/types';
6
5
  /**
7
6
  * Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided.
8
7
  *
@@ -13,4 +12,4 @@ import { ILogger } from '../logger/types';
13
12
  * @param observer optional impression observer. If provided, previous time (pt property) is included in impression instances
14
13
  * @param countsCache optional cache to save impressions count. If provided, impressions will be deduped (OPTIMIZED mode)
15
14
  */
16
- export declare function impressionsTrackerFactory(log: ILogger, impressionsCache: IImpressionsCacheBase, { runtime: { ip, hostname }, version }: Pick<ISettings, 'version' | 'runtime'>, impressionListener?: SplitIO.IImpressionListener, integrationsManager?: IImpressionsHandler, observer?: IImpressionObserver, countsCache?: IImpressionCountsCacheSync): IImpressionsTracker;
15
+ export declare function impressionsTrackerFactory(settings: ISettings, impressionsCache: IImpressionsCacheBase, integrationsManager?: IImpressionsHandler, observer?: IImpressionObserver, countsCache?: IImpressionCountsCacheSync): IImpressionsTracker;