@splitsoftware/splitio-commons 1.2.1-rc.3 → 1.2.1-rc.6

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 (104) hide show
  1. package/cjs/listeners/browser.js +14 -10
  2. package/cjs/logger/constants.js +7 -4
  3. package/cjs/logger/messages/debug.js +3 -3
  4. package/cjs/logger/messages/error.js +3 -2
  5. package/cjs/logger/messages/info.js +5 -3
  6. package/cjs/sdkClient/client.js +10 -4
  7. package/cjs/sdkFactory/index.js +6 -4
  8. package/cjs/sdkFactory/userConsentProps.js +37 -0
  9. package/cjs/storages/KeyBuilder.js +1 -5
  10. package/cjs/storages/KeyBuilderCS.js +11 -1
  11. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +23 -3
  12. package/cjs/storages/inLocalStorage/index.js +1 -1
  13. package/cjs/storages/inMemory/ImpressionsCacheInMemory.js +15 -1
  14. package/cjs/storages/inMemory/InMemoryStorage.js +1 -1
  15. package/cjs/storages/inMemory/InMemoryStorageCS.js +1 -1
  16. package/cjs/storages/pluggable/index.js +2 -2
  17. package/cjs/sync/submitters/eventsSyncTask.js +17 -5
  18. package/cjs/sync/submitters/impressionsSyncTask.js +13 -1
  19. package/cjs/sync/syncManagerOnline.js +11 -7
  20. package/cjs/utils/consent.js +10 -0
  21. package/cjs/utils/constants/index.js +5 -1
  22. package/cjs/utils/lang/index.js +8 -1
  23. package/cjs/utils/settingsValidation/consent.js +16 -0
  24. package/cjs/utils/settingsValidation/impressionsMode.js +6 -6
  25. package/cjs/utils/settingsValidation/index.js +6 -1
  26. package/cjs/utils/settingsValidation/{runtime/browser.js → runtime.js} +1 -0
  27. package/esm/listeners/browser.js +14 -10
  28. package/esm/logger/constants.js +5 -2
  29. package/esm/logger/messages/debug.js +3 -3
  30. package/esm/logger/messages/error.js +3 -2
  31. package/esm/logger/messages/info.js +5 -3
  32. package/esm/sdkClient/client.js +11 -5
  33. package/esm/sdkFactory/index.js +6 -4
  34. package/esm/sdkFactory/userConsentProps.js +33 -0
  35. package/esm/storages/KeyBuilder.js +2 -6
  36. package/esm/storages/KeyBuilderCS.js +11 -1
  37. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +23 -3
  38. package/esm/storages/inLocalStorage/index.js +1 -1
  39. package/esm/storages/inMemory/ImpressionsCacheInMemory.js +15 -1
  40. package/esm/storages/inMemory/InMemoryStorage.js +1 -1
  41. package/esm/storages/inMemory/InMemoryStorageCS.js +1 -1
  42. package/esm/storages/pluggable/index.js +2 -2
  43. package/esm/sync/submitters/eventsSyncTask.js +18 -6
  44. package/esm/sync/submitters/impressionsSyncTask.js +13 -1
  45. package/esm/sync/syncManagerOnline.js +11 -7
  46. package/esm/utils/consent.js +6 -0
  47. package/esm/utils/constants/index.js +4 -0
  48. package/esm/utils/lang/index.js +6 -0
  49. package/esm/utils/settingsValidation/consent.js +12 -0
  50. package/esm/utils/settingsValidation/impressionsMode.js +7 -7
  51. package/esm/utils/settingsValidation/index.js +6 -1
  52. package/esm/utils/settingsValidation/{runtime/browser.js → runtime.js} +1 -0
  53. package/package.json +1 -2
  54. package/src/listeners/browser.ts +13 -9
  55. package/src/logger/constants.ts +5 -2
  56. package/src/logger/messages/debug.ts +3 -3
  57. package/src/logger/messages/error.ts +3 -2
  58. package/src/logger/messages/info.ts +5 -3
  59. package/src/sdkClient/client.ts +7 -5
  60. package/src/sdkFactory/index.ts +6 -4
  61. package/src/sdkFactory/types.ts +2 -0
  62. package/src/sdkFactory/userConsentProps.ts +40 -0
  63. package/src/storages/KeyBuilder.ts +2 -6
  64. package/src/storages/KeyBuilderCS.ts +13 -1
  65. package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +23 -3
  66. package/src/storages/inLocalStorage/index.ts +1 -1
  67. package/src/storages/inMemory/ImpressionsCacheInMemory.ts +22 -1
  68. package/src/storages/inMemory/InMemoryStorage.ts +1 -1
  69. package/src/storages/inMemory/InMemoryStorageCS.ts +1 -1
  70. package/src/storages/pluggable/index.ts +2 -2
  71. package/src/storages/types.ts +5 -1
  72. package/src/sync/submitters/eventsSyncTask.ts +19 -6
  73. package/src/sync/submitters/impressionsSyncTask.ts +16 -1
  74. package/src/sync/syncManagerOnline.ts +13 -7
  75. package/src/sync/types.ts +4 -1
  76. package/src/types.ts +21 -0
  77. package/src/utils/consent.ts +8 -0
  78. package/src/utils/constants/index.ts +5 -0
  79. package/src/utils/lang/index.ts +8 -1
  80. package/src/utils/settingsValidation/consent.ts +15 -0
  81. package/src/utils/settingsValidation/impressionsMode.ts +8 -8
  82. package/src/utils/settingsValidation/index.ts +7 -1
  83. package/src/utils/settingsValidation/runtime.ts +9 -0
  84. package/src/utils/settingsValidation/types.ts +2 -0
  85. package/types/logger/constants.d.ts +5 -2
  86. package/types/sdkFactory/types.d.ts +1 -0
  87. package/types/sdkFactory/userConsentProps.d.ts +6 -0
  88. package/types/storages/KeyBuilderCS.d.ts +2 -0
  89. package/types/storages/inMemory/ImpressionsCacheInMemory.d.ts +9 -0
  90. package/types/storages/types.d.ts +2 -0
  91. package/types/sync/types.d.ts +3 -0
  92. package/types/types.d.ts +21 -0
  93. package/types/utils/consent.d.ts +2 -0
  94. package/types/utils/constants/index.d.ts +3 -0
  95. package/types/utils/lang/index.d.ts +4 -0
  96. package/types/utils/settingsValidation/consent.d.ts +5 -0
  97. package/types/utils/settingsValidation/impressionsMode.d.ts +1 -1
  98. package/types/utils/settingsValidation/runtime.d.ts +2 -0
  99. package/types/utils/settingsValidation/types.d.ts +2 -0
  100. package/types/utils/settingsValidation/userConsent.d.ts +5 -0
  101. package/cjs/utils/settingsValidation/runtime/node.js +0 -22
  102. package/esm/utils/settingsValidation/runtime/node.js +0 -17
  103. package/src/utils/settingsValidation/runtime/browser.ts +0 -8
  104. package/src/utils/settingsValidation/runtime/node.ts +0 -22
@@ -1,6 +1,7 @@
1
1
  import { submitterManagerFactory } from './submitters/submitterManager';
2
2
  import { PUSH_SUBSYSTEM_UP, PUSH_SUBSYSTEM_DOWN } from './streaming/constants';
3
3
  import { SYNC_START_POLLING, SYNC_CONTINUE_POLLING, SYNC_STOP_POLLING } from '../logger/constants';
4
+ import { isConsentGranted } from '../utils/consent';
4
5
  /**
5
6
  * Online SyncManager factory.
6
7
  * Can be used for server-side API, and client-side API with or without multiple clients.
@@ -14,7 +15,7 @@ export function syncManagerOnlineFactory(pollingManagerFactory, pushManagerFacto
14
15
  * SyncManager factory for modular SDK
15
16
  */
16
17
  return function (params) {
17
- var _a = params.settings, log = _a.log, streamingEnabled = _a.streamingEnabled;
18
+ var settings = params.settings, _a = params.settings, log = _a.log, streamingEnabled = _a.streamingEnabled;
18
19
  /** Polling Manager */
19
20
  var pollingManager = pollingManagerFactory && pollingManagerFactory(params);
20
21
  /** Push Manager */
@@ -49,11 +50,16 @@ export function syncManagerOnlineFactory(pollingManagerFactory, pushManagerFacto
49
50
  var running = false; // flag that indicates whether the syncManager has been started (true) or stopped (false)
50
51
  var startFirstTime = true; // flag to distinguish calling the `start` method for the first time, to support pausing and resuming the synchronization
51
52
  return {
53
+ // Exposed for fine-grained control of synchronization.
54
+ // E.g.: user consent, app state changes (Page hide, Foreground/Background, Online/Offline).
55
+ pollingManager: pollingManager,
52
56
  pushManager: pushManager,
57
+ submitter: submitter,
53
58
  /**
54
59
  * Method used to start the syncManager for the first time, or resume it after being stopped.
55
60
  */
56
61
  start: function () {
62
+ running = true;
57
63
  // start syncing splits and segments
58
64
  if (pollingManager) {
59
65
  if (pushManager) {
@@ -69,29 +75,27 @@ export function syncManagerOnlineFactory(pollingManagerFactory, pushManagerFacto
69
75
  }
70
76
  }
71
77
  // start periodic data recording (events, impressions, telemetry).
72
- if (submitter)
78
+ if (isConsentGranted(settings))
73
79
  submitter.start();
74
- running = true;
75
80
  },
76
81
  /**
77
82
  * Method used to stop/pause the syncManager.
78
83
  */
79
84
  stop: function () {
85
+ running = false;
80
86
  // stop syncing
81
87
  if (pushManager)
82
88
  pushManager.stop();
83
89
  if (pollingManager && pollingManager.isRunning())
84
90
  pollingManager.stop();
85
91
  // stop periodic data recording (events, impressions, telemetry).
86
- if (submitter)
87
- submitter.stop();
88
- running = false;
92
+ submitter.stop();
89
93
  },
90
94
  isRunning: function () {
91
95
  return running;
92
96
  },
93
97
  flush: function () {
94
- if (submitter)
98
+ if (isConsentGranted(settings))
95
99
  return submitter.execute();
96
100
  else
97
101
  return Promise.resolve();
@@ -0,0 +1,6 @@
1
+ import { CONSENT_GRANTED } from './constants';
2
+ export function isConsentGranted(settings) {
3
+ var userConsent = settings.userConsent;
4
+ // undefined userConsent is handled as granted (default)
5
+ return !userConsent || userConsent === CONSENT_GRANTED;
6
+ }
@@ -24,3 +24,7 @@ export var STORAGE_MEMORY = 'MEMORY';
24
24
  export var STORAGE_LOCALSTORAGE = 'LOCALSTORAGE';
25
25
  export var STORAGE_REDIS = 'REDIS';
26
26
  export var STORAGE_PLUGGABLE = 'PLUGGABLE';
27
+ // User consent
28
+ export var CONSENT_GRANTED = 'GRANTED'; // The user has granted consent for tracking events and impressions
29
+ export var CONSENT_DECLINED = 'DECLINED'; // The user has declined consent for tracking events and impressions
30
+ export var CONSENT_UNKNOWN = 'UNKNOWN'; // The user has neither granted nor declined consent for tracking events and impressions
@@ -152,6 +152,12 @@ export function isObject(obj) {
152
152
  export function isString(val) {
153
153
  return typeof val === 'string' || val instanceof String;
154
154
  }
155
+ /**
156
+ * String sanitizer. Returns the provided value converted to uppercase if it is a string.
157
+ */
158
+ export function stringToUpperCase(val) {
159
+ return isString(val) ? val.toUpperCase() : val;
160
+ }
155
161
  /**
156
162
  * Deep copy version of Object.assign using recursion.
157
163
  * There are some assumptions here. It's for internal use and we don't need verbose errors
@@ -0,0 +1,12 @@
1
+ import { ERROR_INVALID_CONFIG_PARAM } from '../../logger/constants';
2
+ import { CONSENT_DECLINED, CONSENT_GRANTED, CONSENT_UNKNOWN } from '../constants';
3
+ import { stringToUpperCase } from '../lang';
4
+ var userConsentValues = [CONSENT_DECLINED, CONSENT_GRANTED, CONSENT_UNKNOWN];
5
+ export function validateConsent(_a) {
6
+ var userConsent = _a.userConsent, log = _a.log;
7
+ userConsent = stringToUpperCase(userConsent);
8
+ if (userConsentValues.indexOf(userConsent) > -1)
9
+ return userConsent;
10
+ log.error(ERROR_INVALID_CONFIG_PARAM, ['userConsent', userConsentValues, CONSENT_GRANTED]);
11
+ return CONSENT_GRANTED;
12
+ }
@@ -1,10 +1,10 @@
1
- import { ERROR_INVALID_IMPRESSIONS_MODE } from '../../logger/constants';
1
+ import { ERROR_INVALID_CONFIG_PARAM } from '../../logger/constants';
2
2
  import { DEBUG, OPTIMIZED } from '../constants';
3
+ import { stringToUpperCase } from '../lang';
3
4
  export function validImpressionsMode(log, impressionsMode) {
4
- impressionsMode = impressionsMode.toUpperCase();
5
- if ([DEBUG, OPTIMIZED].indexOf(impressionsMode) === -1) {
6
- log.error(ERROR_INVALID_IMPRESSIONS_MODE, [[DEBUG, OPTIMIZED], OPTIMIZED]);
7
- impressionsMode = OPTIMIZED;
8
- }
9
- return impressionsMode;
5
+ impressionsMode = stringToUpperCase(impressionsMode);
6
+ if ([DEBUG, OPTIMIZED].indexOf(impressionsMode) > -1)
7
+ return impressionsMode;
8
+ log.error(ERROR_INVALID_CONFIG_PARAM, ['impressionsMode', [DEBUG, OPTIMIZED], OPTIMIZED]);
9
+ return OPTIMIZED;
10
10
  }
@@ -33,6 +33,8 @@ var base = {
33
33
  eventsPushRate: 60,
34
34
  // how many events will be queued before flushing
35
35
  eventsQueueSize: 500,
36
+ // how many impressions will be queued before flushing
37
+ impressionsQueueSize: 30000,
36
38
  // backoff base seconds to wait before re attempting to connect to push notifications
37
39
  pushRetryBackoffBase: 1,
38
40
  },
@@ -78,7 +80,7 @@ function fromSecondsToMillis(n) {
78
80
  * @param validationParams defaults and fields validators used to validate and creates a settings object from a given config
79
81
  */
80
82
  export function settingsValidation(config, validationParams) {
81
- var defaults = validationParams.defaults, runtime = validationParams.runtime, storage = validationParams.storage, integrations = validationParams.integrations, logger = validationParams.logger, localhost = validationParams.localhost;
83
+ var defaults = validationParams.defaults, runtime = validationParams.runtime, storage = validationParams.storage, integrations = validationParams.integrations, logger = validationParams.logger, localhost = validationParams.localhost, consent = validationParams.consent;
82
84
  // creates a settings object merging base, defaults and config objects.
83
85
  var withDefaults = merge({}, base, defaults, config);
84
86
  // ensure a valid logger.
@@ -131,5 +133,8 @@ export function settingsValidation(config, validationParams) {
131
133
  withDefaults.sync.__splitFiltersValidation = splitFiltersValidation;
132
134
  // ensure a valid impressionsMode
133
135
  withDefaults.sync.impressionsMode = validImpressionsMode(log, withDefaults.sync.impressionsMode);
136
+ // ensure a valid user consent value
137
+ // @ts-ignore, modify readonly prop
138
+ withDefaults.userConsent = consent(withDefaults);
134
139
  return withDefaults;
135
140
  }
@@ -1,3 +1,4 @@
1
+ // For client-side SDKs, machine IP and Hostname are not captured and sent to Split backend.
1
2
  export function validateRuntime() {
2
3
  return {
3
4
  ip: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.2.1-rc.3",
3
+ "version": "1.2.1-rc.6",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -49,7 +49,6 @@
49
49
  "devDependencies": {
50
50
  "@types/google.analytics": "0.0.40",
51
51
  "@types/ioredis": "^4.28.0",
52
- "@types/ip": "^1.1.0",
53
52
  "@types/jest": "^27.0.0",
54
53
  "@types/lodash": "^4.14.162",
55
54
  "@typescript-eslint/eslint-plugin": "^4.2.0",
@@ -11,6 +11,7 @@ import { OPTIMIZED, DEBUG } from '../utils/constants';
11
11
  import { objectAssign } from '../utils/lang/objectAssign';
12
12
  import { CLEANUP_REGISTERING, CLEANUP_DEREGISTERING } from '../logger/constants';
13
13
  import { ISyncManager } from '../sync/types';
14
+ import { isConsentGranted } from '../utils/consent';
14
15
 
15
16
  // 'unload' event is used instead of 'beforeunload', since 'unload' is not a cancelable event, so no other listeners can stop the event from occurring.
16
17
  const UNLOAD_DOM_EVENT = 'unload';
@@ -65,15 +66,18 @@ export class BrowserSignalListener implements ISignalListener {
65
66
  flushData() {
66
67
  if (!this.syncManager) return; // In consumer mode there is not sync manager and data to flush
67
68
 
68
- const eventsUrl = this.settings.urls.events;
69
- const extraMetadata = {
70
- // sim stands for Sync/Split Impressions Mode
71
- sim: this.settings.sync.impressionsMode === OPTIMIZED ? OPTIMIZED : DEBUG
72
- };
69
+ // Flush data if there is user consent
70
+ if (isConsentGranted(this.settings)) {
71
+ const eventsUrl = this.settings.urls.events;
72
+ const extraMetadata = {
73
+ // sim stands for Sync/Split Impressions Mode
74
+ sim: this.settings.sync.impressionsMode === OPTIMIZED ? OPTIMIZED : DEBUG
75
+ };
73
76
 
74
- this._flushData(eventsUrl + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
75
- this._flushData(eventsUrl + '/events/beacon', this.storage.events, this.serviceApi.postEventsBulk);
76
- if (this.storage.impressionCounts) this._flushData(eventsUrl + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
77
+ this._flushData(eventsUrl + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
78
+ this._flushData(eventsUrl + '/events/beacon', this.storage.events, this.serviceApi.postEventsBulk);
79
+ if (this.storage.impressionCounts) this._flushData(eventsUrl + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
80
+ }
77
81
 
78
82
  // Close streaming connection
79
83
  if (this.syncManager.pushManager) this.syncManager.pushManager.stop();
@@ -84,7 +88,7 @@ export class BrowserSignalListener implements ISignalListener {
84
88
  if (!cache.isEmpty()) {
85
89
  const dataPayload = fromCacheToPayload ? fromCacheToPayload(cache.state()) : cache.state();
86
90
  if (!this._sendBeacon(url, dataPayload, extraMetadata)) {
87
- postService(JSON.stringify(dataPayload)).catch(() => { }); // no-op just to catch a possible exceptions
91
+ postService(JSON.stringify(dataPayload)).catch(() => { }); // no-op just to catch a possible exception
88
92
  }
89
93
  cache.clear();
90
94
  }
@@ -61,13 +61,15 @@ export const STREAMING_RECONNECT = 111;
61
61
  export const STREAMING_CONNECTING = 112;
62
62
  export const STREAMING_DISABLED = 113;
63
63
  export const STREAMING_DISCONNECTING = 114;
64
- export const SUBMITTERS_PUSH_FULL_EVENTS_QUEUE = 115;
64
+ export const SUBMITTERS_PUSH_FULL_QUEUE = 115;
65
65
  export const SUBMITTERS_PUSH = 116;
66
66
  export const SYNC_START_POLLING = 117;
67
67
  export const SYNC_CONTINUE_POLLING = 118;
68
68
  export const SYNC_STOP_POLLING = 119;
69
69
  export const EVENTS_TRACKER_SUCCESS = 120;
70
70
  export const IMPRESSIONS_TRACKER_SUCCESS = 121;
71
+ export const USER_CONSENT_UPDATED = 122;
72
+ export const USER_CONSENT_NOT_UPDATED = 123;
71
73
 
72
74
  export const ENGINE_VALUE_INVALID = 200;
73
75
  export const ENGINE_VALUE_NO_ATTRIBUTES = 201;
@@ -115,10 +117,11 @@ export const ERROR_INVALID_KEY_OBJECT = 317;
115
117
  export const ERROR_INVALID = 318;
116
118
  export const ERROR_EMPTY = 319;
117
119
  export const ERROR_EMPTY_ARRAY = 320;
118
- export const ERROR_INVALID_IMPRESSIONS_MODE = 321;
120
+ export const ERROR_INVALID_CONFIG_PARAM = 321;
119
121
  export const ERROR_HTTP = 322;
120
122
  export const ERROR_LOCALHOST_MODULE_REQUIRED = 323;
121
123
  export const ERROR_STORAGE_INVALID = 324;
124
+ export const ERROR_NOT_BOOLEAN = 325;
122
125
 
123
126
  // Log prefixes (a.k.a. tags or categories)
124
127
  export const LOG_PREFIX_SETTINGS = 'settings';
@@ -31,9 +31,9 @@ export const codesDebug: [number, string][] = codesInfo.concat([
31
31
  // SDK
32
32
  [c.CLEANUP_REGISTERING, c.LOG_PREFIX_CLEANUP + 'Registering cleanup handler %s'],
33
33
  [c.CLEANUP_DEREGISTERING, c.LOG_PREFIX_CLEANUP + 'Deregistering cleanup handler %s'],
34
- [c.RETRIEVE_CLIENT_DEFAULT, ' Retrieving default SDK client.'],
35
- [c.RETRIEVE_CLIENT_EXISTING, ' Retrieving existing SDK client.'],
36
- [c.RETRIEVE_MANAGER, ' Retrieving manager instance.'],
34
+ [c.RETRIEVE_CLIENT_DEFAULT, 'Retrieving default SDK client.'],
35
+ [c.RETRIEVE_CLIENT_EXISTING, 'Retrieving existing SDK client.'],
36
+ [c.RETRIEVE_MANAGER, 'Retrieving manager instance.'],
37
37
  // synchronizer
38
38
  [c.SYNC_OFFLINE_DATA, c.LOG_PREFIX_SYNC_OFFLINE + 'Splits data: \n%s'],
39
39
  [c.SYNC_SPLITS_FETCH, c.LOG_PREFIX_SYNC_SPLITS + 'Spin up split update using since = %s'],
@@ -5,7 +5,7 @@ export const codesError: [number, string][] = [
5
5
  [c.ERROR_ENGINE_COMBINER_IFELSEIF, c.LOG_PREFIX_ENGINE_COMBINER + 'Invalid Split, no valid rules found'],
6
6
  // SDK
7
7
  [c.ERROR_LOGLEVEL_INVALID, 'logger: Invalid Log Level - No changes to the logs will be applied.'],
8
- [c.ERROR_CLIENT_CANNOT_GET_READY, ' The SDK will not get ready. Reason: %s'],
8
+ [c.ERROR_CLIENT_CANNOT_GET_READY, 'The SDK will not get ready. Reason: %s'],
9
9
  [c.ERROR_IMPRESSIONS_TRACKER, c.LOG_PREFIX_IMPRESSIONS_TRACKER + 'Could not store impressions bulk with %s impression(s). Error: %s'],
10
10
  [c.ERROR_IMPRESSIONS_LISTENER, c.LOG_PREFIX_IMPRESSIONS_TRACKER + 'Impression listener logImpression method threw: %s.'],
11
11
  [c.ERROR_EVENTS_TRACKER, c.LOG_PREFIX_EVENTS_TRACKER + 'Failed to queue %s'],
@@ -28,8 +28,9 @@ export const codesError: [number, string][] = [
28
28
  [c.ERROR_INVALID, '%s: you passed an invalid %s. It must be a non-empty string.'],
29
29
  [c.ERROR_EMPTY, '%s: you passed an empty %s. It must be a non-empty string.'],
30
30
  [c.ERROR_EMPTY_ARRAY, '%s: %s must be a non-empty array.'],
31
+ [c.ERROR_NOT_BOOLEAN, '%s: provided param must be a boolean value.'],
31
32
  // initialization / settings validation
32
- [c.ERROR_INVALID_IMPRESSIONS_MODE, c.LOG_PREFIX_SETTINGS + ': you passed an invalid "impressionsMode". It should be one of the following values: %s. Defaulting to "%s" mode.'],
33
+ [c.ERROR_INVALID_CONFIG_PARAM, c.LOG_PREFIX_SETTINGS + ': you passed an invalid "%s" config param. It should be one of the following values: %s. Defaulting to "%s".'],
33
34
  [c.ERROR_LOCALHOST_MODULE_REQUIRED, c.LOG_PREFIX_SETTINGS + ': an invalid value was received for "sync.localhostMode" config. A valid entity should be provided for localhost mode.'],
34
35
  [c.ERROR_STORAGE_INVALID, c.LOG_PREFIX_SETTINGS+': The provided storage is invalid.%s Fallbacking into default MEMORY storage'],
35
36
  ];
@@ -10,17 +10,19 @@ export const codesInfo: [number, string][] = codesWarn.concat([
10
10
  // SDK
11
11
  [c.IMPRESSION, c.LOG_PREFIX_IMPRESSIONS_TRACKER +'Split: %s. Key: %s. Evaluation: %s. Label: %s'],
12
12
  [c.IMPRESSION_QUEUEING, c.LOG_PREFIX_IMPRESSIONS_TRACKER +'Queueing corresponding impression.'],
13
- [c.NEW_SHARED_CLIENT, ' New shared client instance created.'],
14
- [c.NEW_FACTORY, ' New Split SDK instance created.'],
13
+ [c.NEW_SHARED_CLIENT, 'New shared client instance created.'],
14
+ [c.NEW_FACTORY, 'New Split SDK instance created.'],
15
15
  [c.EVENTS_TRACKER_SUCCESS, c.LOG_PREFIX_EVENTS_TRACKER + 'Successfully queued %s'],
16
16
  [c.IMPRESSIONS_TRACKER_SUCCESS, c.LOG_PREFIX_IMPRESSIONS_TRACKER + 'Successfully stored %s impression(s).'],
17
+ [c.USER_CONSENT_UPDATED, 'setUserConsent: consent status changed from %s to %s.'],
18
+ [c.USER_CONSENT_NOT_UPDATED, 'setUserConsent: call had no effect because it was the current consent status (%s).'],
17
19
 
18
20
  // synchronizer
19
21
  [c.POLLING_SMART_PAUSING, c.LOG_PREFIX_SYNC_POLLING + 'Turning segments data polling %s.'],
20
22
  [c.POLLING_START, c.LOG_PREFIX_SYNC_POLLING + 'Starting polling'],
21
23
  [c.POLLING_STOP, c.LOG_PREFIX_SYNC_POLLING + 'Stopping polling'],
22
24
  [c.SYNC_SPLITS_FETCH_RETRY, c.LOG_PREFIX_SYNC_SPLITS + 'Retrying download of splits #%s. Reason: %s'],
23
- [c.SUBMITTERS_PUSH_FULL_EVENTS_QUEUE, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Flushing full events queue and reseting timer.'],
25
+ [c.SUBMITTERS_PUSH_FULL_QUEUE, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Flushing full %s queue and reseting timer.'],
24
26
  [c.SUBMITTERS_PUSH, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Pushing %s %s.'],
25
27
  [c.STREAMING_REFRESH_TOKEN, c.LOG_PREFIX_SYNC_STREAMING + 'Refreshing streaming token in %s seconds, and connecting streaming in %s seconds.'],
26
28
  [c.STREAMING_RECONNECT, c.LOG_PREFIX_SYNC_STREAMING + 'Attempting to reconnect streaming in %s seconds.'],
@@ -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 { CONTROL } from '../utils/constants';
7
+ import { CONSENT_DECLINED, CONTROL } from '../utils/constants';
8
8
  import { IClientFactoryParams } from './types';
9
9
  import { IEvaluationResult } from '../evaluator/types';
10
10
  import { SplitIO, ImpressionDTO } from '../types';
@@ -16,13 +16,14 @@ import { IMPRESSION, IMPRESSION_QUEUEING } from '../logger/constants';
16
16
  */
17
17
  // @TODO missing time tracking to collect telemetry
18
18
  export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | SplitIO.IAsyncClient {
19
- const { sdkReadinessManager: { readinessManager }, storage, settings: { log, mode }, impressionsTracker, eventTracker } = params;
19
+ const { sdkReadinessManager: { readinessManager }, storage, settings, impressionsTracker, eventTracker } = params;
20
+ const { log, mode } = settings;
20
21
 
21
22
  function getTreatment(key: SplitIO.SplitKey, splitName: string, attributes: SplitIO.Attributes | undefined, withConfig = false) {
22
23
  const wrapUp = (evaluationResult: IEvaluationResult) => {
23
24
  const queue: ImpressionDTO[] = [];
24
25
  const treatment = processEvaluation(evaluationResult, splitName, key, attributes, withConfig, `getTreatment${withConfig ? 'withConfig' : ''}`, queue);
25
- impressionsTracker.track(queue, attributes);
26
+ if (settings.userConsent !== CONSENT_DECLINED) impressionsTracker.track(queue, attributes);
26
27
  return treatment;
27
28
  };
28
29
 
@@ -42,7 +43,7 @@ export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | S
42
43
  Object.keys(evaluationResults).forEach(splitName => {
43
44
  treatments[splitName] = processEvaluation(evaluationResults[splitName], splitName, key, attributes, withConfig, `getTreatments${withConfig ? 'withConfig' : ''}`, queue);
44
45
  });
45
- impressionsTracker.track(queue, attributes);
46
+ if (settings.userConsent !== CONSENT_DECLINED) impressionsTracker.track(queue, attributes);
46
47
  return treatments;
47
48
  };
48
49
 
@@ -115,7 +116,8 @@ export function clientFactory(params: IClientFactoryParams): SplitIO.IClient | S
115
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.
116
117
  validateTrafficTypeExistance(log, readinessManager, storage.splits, mode, trafficTypeName, 'track');
117
118
 
118
- return eventTracker.track(eventData, size);
119
+ if (settings.userConsent !== CONSENT_DECLINED) return eventTracker.track(eventData, size);
120
+ else return false;
119
121
  }
120
122
 
121
123
  return {
@@ -12,13 +12,14 @@ import { createLoggerAPI } from '../logger/sdkLogger';
12
12
  import { NEW_FACTORY, RETRIEVE_MANAGER } from '../logger/constants';
13
13
  import { metadataBuilder } from '../storages/metadataBuilder';
14
14
  import { SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
15
+ import { objectAssign } from '../utils/lang/objectAssign';
15
16
 
16
17
  /**
17
18
  * Modular SDK factory
18
19
  */
19
20
  export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.ISDK | SplitIO.IAsyncSDK {
20
21
 
21
- const { settings, platform, storageFactory, splitApiFactory,
22
+ const { settings, platform, storageFactory, splitApiFactory, extraProps,
22
23
  syncManagerFactory, SignalListener, impressionsObserverFactory, impressionListener,
23
24
  integrationsManagerFactory, sdkManagerFactory, sdkClientMethodFactory } = params;
24
25
  const log = settings.log;
@@ -33,6 +34,7 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
33
34
 
34
35
  // @TODO consider passing the settings object, so that each storage access only what it needs
35
36
  const storageFactoryParams: IStorageFactoryParams = {
37
+ impressionsQueueSize: settings.scheduler.impressionsQueueSize,
36
38
  eventsQueueSize: settings.scheduler.eventsQueueSize,
37
39
  optimize: shouldBeOptimized(settings),
38
40
 
@@ -87,12 +89,12 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
87
89
 
88
90
  log.info(NEW_FACTORY);
89
91
 
90
- return {
92
+ // @ts-ignore
93
+ return objectAssign({
91
94
  // Split evaluation and event tracking engine
92
95
  client: clientMethod,
93
96
 
94
97
  // Manager API to explore available information
95
- // @ts-ignore
96
98
  manager() {
97
99
  log.debug(RETRIEVE_MANAGER);
98
100
  return managerInstance;
@@ -102,5 +104,5 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
102
104
  Logger: createLoggerAPI(settings.log),
103
105
 
104
106
  settings,
105
- };
107
+ }, extraProps && extraProps(settings, syncManager));
106
108
  }
@@ -70,4 +70,6 @@ export interface ISdkFactoryParams {
70
70
  // Impression observer factory. If provided, will be used for impressions dedupe
71
71
  impressionsObserverFactory?: () => IImpressionObserver
72
72
 
73
+ // Optional function to assign additional properties to the factory instance
74
+ extraProps?: (settings: ISettings, syncManager?: ISyncManager) => object
73
75
  }
@@ -0,0 +1,40 @@
1
+ import { ERROR_NOT_BOOLEAN, USER_CONSENT_UPDATED, USER_CONSENT_NOT_UPDATED } from '../logger/constants';
2
+ import { ISyncManager } from '../sync/types';
3
+ import { ISettings } from '../types';
4
+ import { CONSENT_GRANTED, CONSENT_DECLINED } from '../utils/constants';
5
+ import { isBoolean } from '../utils/lang';
6
+
7
+ // Extend client-side factory instances with user consent getter/setter
8
+ export function userConsentProps(settings: ISettings, syncManager?: ISyncManager) {
9
+
10
+ const log = settings.log;
11
+
12
+ return {
13
+ setUserConsent(consent: unknown) {
14
+ // validate input param
15
+ if (!isBoolean(consent)) {
16
+ log.warn(ERROR_NOT_BOOLEAN, ['setUserConsent']);
17
+ return false;
18
+ }
19
+
20
+ const newConsentStatus = consent ? CONSENT_GRANTED : CONSENT_DECLINED;
21
+
22
+ if (settings.userConsent !== newConsentStatus) { // @ts-ignore, modify readonly prop
23
+ settings.userConsent = newConsentStatus;
24
+
25
+ if (consent) syncManager?.submitter?.start(); // resumes submitters if transitioning to GRANTED
26
+ else syncManager?.submitter?.stop(); // pauses submitters if transitioning to DECLINED
27
+
28
+ log.info(USER_CONSENT_UPDATED, [settings.userConsent, newConsentStatus]);
29
+ } else {
30
+ log.info(USER_CONSENT_NOT_UPDATED, [newConsentStatus]);
31
+ }
32
+
33
+ return true;
34
+ },
35
+
36
+ getUserConsent() {
37
+ return settings.userConsent;
38
+ }
39
+ };
40
+ }
@@ -1,15 +1,11 @@
1
- import { endsWith, startsWith } from '../utils/lang';
1
+ import { startsWith } from '../utils/lang';
2
2
 
3
3
  const everythingAtTheEnd = /[^.]+$/;
4
4
 
5
5
  const DEFAULT_PREFIX = 'SPLITIO';
6
6
 
7
7
  export function validatePrefix(prefix: unknown) {
8
- return prefix && typeof prefix === 'string' ?
9
- endsWith(prefix, '.' + DEFAULT_PREFIX) ?
10
- prefix : // suffix already appended
11
- prefix + '.' + DEFAULT_PREFIX : // append suffix
12
- DEFAULT_PREFIX; // use default prefix if none is provided
8
+ return prefix ? prefix + '.SPLITIO' : 'SPLITIO';
13
9
  }
14
10
 
15
11
  export class KeyBuilder {
@@ -16,10 +16,22 @@ export class KeyBuilderCS extends KeyBuilder {
16
16
  * @override
17
17
  */
18
18
  buildSegmentNameKey(segmentName: string) {
19
- return `${this.matchingKey}.${this.prefix}.segment.${segmentName}`;
19
+ return `${this.prefix}.${this.matchingKey}.segment.${segmentName}`;
20
20
  }
21
21
 
22
22
  extractSegmentName(builtSegmentKeyName: string) {
23
+ const prefix = `${this.prefix}.${this.matchingKey}.segment.`;
24
+
25
+ if (startsWith(builtSegmentKeyName, prefix))
26
+ return builtSegmentKeyName.substr(prefix.length);
27
+ }
28
+
29
+ // @BREAKING: The key used to start with the matching key instead of the prefix, this was changed on version 10.17.3
30
+ buildOldSegmentNameKey(segmentName: string) {
31
+ return `${this.matchingKey}.${this.prefix}.segment.${segmentName}`;
32
+ }
33
+ // @BREAKING: The key used to start with the matching key instead of the prefix, this was changed on version 10.17.3
34
+ extractOldSegmentKey(builtSegmentKeyName: string) {
23
35
  const prefix = `${this.matchingKey}.${this.prefix}.segment.`;
24
36
 
25
37
  if (startsWith(builtSegmentKeyName, prefix))
@@ -67,9 +67,29 @@ export class MySegmentsCacheInLocal extends AbstractSegmentsCacheSync {
67
67
 
68
68
  // Scan current values from localStorage
69
69
  const storedSegmentNames = Object.keys(localStorage).reduce((accum, key) => {
70
- const name = this.keys.extractSegmentName(key);
71
-
72
- if (name) accum.push(name);
70
+ let segmentName = this.keys.extractSegmentName(key);
71
+
72
+ if (segmentName) {
73
+ accum.push(segmentName);
74
+ } else {
75
+ // @BREAKING: This is only to clean up "old" keys. Remove this whole else code block.
76
+ segmentName = this.keys.extractOldSegmentKey(key);
77
+
78
+ if (segmentName) { // this was an old segment key, let's clean up.
79
+ const newSegmentKey = this.keys.buildSegmentNameKey(segmentName);
80
+ try {
81
+ // If the new format key is not there, create it.
82
+ if (!localStorage.getItem(newSegmentKey) && names.indexOf(segmentName) > -1) {
83
+ localStorage.setItem(newSegmentKey, DEFINED);
84
+ // we are migrating a segment, let's track it.
85
+ accum.push(segmentName);
86
+ }
87
+ localStorage.removeItem(key); // we migrated the current key, let's delete it.
88
+ } catch (e) {
89
+ this.log.error(e);
90
+ }
91
+ }
92
+ }
73
93
 
74
94
  return accum;
75
95
  }, [] as string[]);
@@ -40,7 +40,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
40
40
  return {
41
41
  splits: new SplitsCacheInLocal(log, keys, expirationTimestamp, params.splitFiltersValidation),
42
42
  segments: new MySegmentsCacheInLocal(log, keys),
43
- impressions: new ImpressionsCacheInMemory(),
43
+ impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
44
44
  impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory() : undefined,
45
45
  events: new EventsCacheInMemory(params.eventsQueueSize),
46
46
 
@@ -3,13 +3,34 @@ import { ImpressionDTO } from '../../types';
3
3
 
4
4
  export class ImpressionsCacheInMemory implements IImpressionsCacheSync {
5
5
 
6
- private queue: ImpressionDTO[] = [];
6
+ private onFullQueue?: () => void;
7
+ private readonly maxQueue: number;
8
+ private queue: ImpressionDTO[];
9
+
10
+ /**
11
+ *
12
+ * @param impressionsQueueSize number of queued impressions to call onFullQueueCb.
13
+ * Default value is 0, that means no maximum value, in case we want to avoid this being triggered.
14
+ */
15
+ constructor(impressionsQueueSize: number = 0) {
16
+ this.maxQueue = impressionsQueueSize;
17
+ this.queue = [];
18
+ }
19
+
20
+ setOnFullQueueCb(cb: () => void) {
21
+ this.onFullQueue = cb;
22
+ }
7
23
 
8
24
  /**
9
25
  * Store impressions in sequential order
10
26
  */
11
27
  track(data: ImpressionDTO[]) {
12
28
  this.queue.push(...data);
29
+
30
+ // Check if the cache queue is full and we need to flush it.
31
+ if (this.maxQueue > 0 && this.queue.length >= this.maxQueue && this.onFullQueue) {
32
+ this.onFullQueue();
33
+ }
13
34
  }
14
35
 
15
36
  /**
@@ -16,7 +16,7 @@ export function InMemoryStorageFactory(params: IStorageFactoryParams): IStorageS
16
16
  return {
17
17
  splits: new SplitsCacheInMemory(),
18
18
  segments: new SegmentsCacheInMemory(),
19
- impressions: new ImpressionsCacheInMemory(),
19
+ impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
20
20
  impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory() : undefined,
21
21
  events: new EventsCacheInMemory(params.eventsQueueSize),
22
22
 
@@ -16,7 +16,7 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
16
16
  return {
17
17
  splits: new SplitsCacheInMemory(),
18
18
  segments: new MySegmentsCacheInMemory(),
19
- impressions: new ImpressionsCacheInMemory(),
19
+ impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
20
20
  impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory() : undefined,
21
21
  events: new EventsCacheInMemory(params.eventsQueueSize),
22
22
 
@@ -63,7 +63,7 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
63
63
 
64
64
  const prefix = validatePrefix(options.prefix);
65
65
 
66
- function PluggableStorageFactory({ log, metadata, onReadyCb, mode, eventsQueueSize, optimize }: IStorageFactoryParams): IStorageAsync {
66
+ function PluggableStorageFactory({ log, metadata, onReadyCb, mode, eventsQueueSize, impressionsQueueSize, optimize }: IStorageFactoryParams): IStorageAsync {
67
67
  const keys = new KeyBuilderSS(prefix, metadata);
68
68
  const wrapper = wrapperAdapter(log, options.wrapper);
69
69
  const isPartialConsumer = mode === CONSUMER_PARTIAL_MODE;
@@ -74,7 +74,7 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
74
74
  return {
75
75
  splits: new SplitsCachePluggable(log, keys, wrapper),
76
76
  segments: new SegmentsCachePluggable(log, keys, wrapper),
77
- impressions: isPartialConsumer ? new ImpressionsCacheInMemory() : new ImpressionsCachePluggable(log, keys.buildImpressionsKey(), wrapper, metadata),
77
+ impressions: isPartialConsumer ? new ImpressionsCacheInMemory(impressionsQueueSize) : new ImpressionsCachePluggable(log, keys.buildImpressionsKey(), wrapper, metadata),
78
78
  impressionCounts: optimize ? new ImpressionCountsCacheInMemory() : undefined,
79
79
  events: isPartialConsumer ? promisifyEventsTrack(new EventsCacheInMemory(eventsQueueSize)) : new EventsCachePluggable(log, keys.buildEventsKey(), wrapper, metadata),
80
80
  // @TODO add telemetry cache when required