@kameleoon/javascript-sdk-core 1.1.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/dist/cacheManager/cacheManager.d.ts +38 -0
  2. package/dist/cacheManager/cacheManager.js +74 -0
  3. package/dist/cacheManager/cacheManager.js.map +1 -0
  4. package/dist/cacheManager/index.d.ts +2 -0
  5. package/dist/cacheManager/index.js +20 -0
  6. package/dist/cacheManager/index.js.map +1 -0
  7. package/dist/cacheManager/types.d.ts +14 -0
  8. package/dist/cacheManager/types.js +6 -0
  9. package/dist/cacheManager/types.js.map +1 -0
  10. package/dist/campaignConfiguration/campaignConfiguration.d.ts +2 -1
  11. package/dist/campaignConfiguration/campaignConfiguration.js +243 -196
  12. package/dist/campaignConfiguration/campaignConfiguration.js.map +1 -1
  13. package/dist/campaignConfiguration/constants.d.ts +3 -3
  14. package/dist/campaignConfiguration/constants.js +22 -16
  15. package/dist/campaignConfiguration/constants.js.map +1 -1
  16. package/dist/campaignConfiguration/index.d.ts +1 -0
  17. package/dist/campaignConfiguration/index.js +85 -6
  18. package/dist/campaignConfiguration/index.js.map +1 -1
  19. package/dist/campaignConfiguration/types.d.ts +7 -3
  20. package/dist/campaignConfiguration/types.js +53 -45
  21. package/dist/campaignConfiguration/types.js.map +1 -1
  22. package/dist/clientSettings/clientSettings.d.ts +1 -1
  23. package/dist/clientSettings/clientSettings.js +28 -21
  24. package/dist/clientSettings/clientSettings.js.map +1 -1
  25. package/dist/clientSettings/index.js +18 -4
  26. package/dist/clientSettings/index.js.map +1 -1
  27. package/dist/clientSettings/types.d.ts +1 -1
  28. package/dist/clientSettings/types.js +4 -1
  29. package/dist/clientSettings/types.js.map +1 -1
  30. package/dist/constants.d.ts +11 -0
  31. package/dist/constants.js +37 -14
  32. package/dist/constants.js.map +1 -1
  33. package/dist/eventSource/constants.js +6 -2
  34. package/dist/eventSource/constants.js.map +1 -1
  35. package/dist/eventSource/externalEventSource.js +28 -20
  36. package/dist/eventSource/externalEventSource.js.map +1 -1
  37. package/dist/eventSource/index.js +49 -6
  38. package/dist/eventSource/index.js.map +1 -1
  39. package/dist/eventSource/types.d.ts +1 -1
  40. package/dist/eventSource/types.js +4 -1
  41. package/dist/eventSource/types.js.map +1 -1
  42. package/dist/hasher/hasher.d.ts +1 -1
  43. package/dist/hasher/hasher.js +37 -26
  44. package/dist/hasher/hasher.js.map +1 -1
  45. package/dist/hasher/index.js +11 -4
  46. package/dist/hasher/index.js.map +1 -1
  47. package/dist/hasher/types.d.ts +1 -1
  48. package/dist/hasher/types.js +4 -1
  49. package/dist/hasher/types.js.map +1 -1
  50. package/dist/index.d.ts +4 -2
  51. package/dist/index.js +309 -28
  52. package/dist/index.js.map +1 -1
  53. package/dist/kameleoonClient.d.ts +32 -19
  54. package/dist/kameleoonClient.js +353 -235
  55. package/dist/kameleoonClient.js.map +1 -1
  56. package/dist/kameleoonData/browser.js +37 -32
  57. package/dist/kameleoonData/browser.js.map +1 -1
  58. package/dist/kameleoonData/constants.js +6 -2
  59. package/dist/kameleoonData/constants.js.map +1 -1
  60. package/dist/kameleoonData/conversion.js +47 -40
  61. package/dist/kameleoonData/conversion.js.map +1 -1
  62. package/dist/kameleoonData/customData.js +62 -58
  63. package/dist/kameleoonData/customData.js.map +1 -1
  64. package/dist/kameleoonData/device.js +37 -32
  65. package/dist/kameleoonData/device.js.map +1 -1
  66. package/dist/kameleoonData/index.js +101 -18
  67. package/dist/kameleoonData/index.js.map +1 -1
  68. package/dist/kameleoonData/nonce.js +25 -13
  69. package/dist/kameleoonData/nonce.js.map +1 -1
  70. package/dist/kameleoonData/pageView.js +52 -44
  71. package/dist/kameleoonData/pageView.js.map +1 -1
  72. package/dist/kameleoonData/types.js +41 -26
  73. package/dist/kameleoonData/types.js.map +1 -1
  74. package/dist/kameleoonData/userAgent.js +33 -24
  75. package/dist/kameleoonData/userAgent.js.map +1 -1
  76. package/dist/kameleoonError/constants.d.ts +1 -1
  77. package/dist/kameleoonError/constants.js +25 -21
  78. package/dist/kameleoonError/constants.js.map +1 -1
  79. package/dist/kameleoonError/helpers.js +6 -4
  80. package/dist/kameleoonError/helpers.js.map +1 -1
  81. package/dist/kameleoonError/index.js +25 -8
  82. package/dist/kameleoonError/index.js.map +1 -1
  83. package/dist/kameleoonError/kameleoonError.d.ts +1 -1
  84. package/dist/kameleoonError/kameleoonError.js +64 -53
  85. package/dist/kameleoonError/kameleoonError.js.map +1 -1
  86. package/dist/kameleoonError/types.d.ts +1 -1
  87. package/dist/kameleoonError/types.js +26 -22
  88. package/dist/kameleoonError/types.js.map +1 -1
  89. package/dist/kameleoonUtils.js +34 -33
  90. package/dist/kameleoonUtils.js.map +1 -1
  91. package/dist/requester/constants.d.ts +6 -7
  92. package/dist/requester/constants.js +58 -45
  93. package/dist/requester/constants.js.map +1 -1
  94. package/dist/requester/index.d.ts +2 -2
  95. package/dist/requester/index.js +67 -8
  96. package/dist/requester/index.js.map +1 -1
  97. package/dist/requester/requester.d.ts +7 -7
  98. package/dist/requester/requester.js +90 -113
  99. package/dist/requester/requester.js.map +1 -1
  100. package/dist/requester/types.d.ts +38 -10
  101. package/dist/requester/types.js +14 -36
  102. package/dist/requester/types.js.map +1 -1
  103. package/dist/storage/constants.js +11 -7
  104. package/dist/storage/constants.js.map +1 -1
  105. package/dist/storage/externalStorage.js +33 -26
  106. package/dist/storage/externalStorage.js.map +1 -1
  107. package/dist/storage/index.js +55 -6
  108. package/dist/storage/index.js.map +1 -1
  109. package/dist/storage/types.js +4 -1
  110. package/dist/storage/types.js.map +1 -1
  111. package/dist/targeting/conditions/conditionFactory.js +24 -19
  112. package/dist/targeting/conditions/conditionFactory.js.map +1 -1
  113. package/dist/targeting/conditions/customDataCondition.js +99 -88
  114. package/dist/targeting/conditions/customDataCondition.js.map +1 -1
  115. package/dist/targeting/conditions/exclusiveExperimentCondition.js +27 -15
  116. package/dist/targeting/conditions/exclusiveExperimentCondition.js.map +1 -1
  117. package/dist/targeting/conditions/index.js +45 -10
  118. package/dist/targeting/conditions/index.js.map +1 -1
  119. package/dist/targeting/conditions/targetExperimentCondition.js +54 -32
  120. package/dist/targeting/conditions/targetExperimentCondition.js.map +1 -1
  121. package/dist/targeting/conditions/types.js +4 -1
  122. package/dist/targeting/conditions/types.js.map +1 -1
  123. package/dist/targeting/index.js +79 -13
  124. package/dist/targeting/index.js.map +1 -1
  125. package/dist/targeting/targetingNode.js +41 -33
  126. package/dist/targeting/targetingNode.js.map +1 -1
  127. package/dist/targeting/targetingTree.js +125 -104
  128. package/dist/targeting/targetingTree.js.map +1 -1
  129. package/dist/targeting/types.js +99 -91
  130. package/dist/targeting/types.js.map +1 -1
  131. package/dist/types.d.ts +34 -2
  132. package/dist/types.js +58 -21
  133. package/dist/types.js.map +1 -1
  134. package/dist/utilities/constants.js +6 -2
  135. package/dist/utilities/constants.js.map +1 -1
  136. package/dist/utilities/index.js +11 -4
  137. package/dist/utilities/index.js.map +1 -1
  138. package/dist/utilities/types.d.ts +9 -1
  139. package/dist/utilities/types.js +4 -1
  140. package/dist/utilities/types.js.map +1 -1
  141. package/dist/utilities/utilities.d.ts +6 -3
  142. package/dist/utilities/utilities.js +171 -105
  143. package/dist/utilities/utilities.js.map +1 -1
  144. package/dist/variationConfiguration/index.js +24 -4
  145. package/dist/variationConfiguration/index.js.map +1 -1
  146. package/dist/variationConfiguration/types.d.ts +1 -1
  147. package/dist/variationConfiguration/types.js +4 -1
  148. package/dist/variationConfiguration/types.js.map +1 -1
  149. package/dist/variationConfiguration/variation.js +35 -28
  150. package/dist/variationConfiguration/variation.js.map +1 -1
  151. package/dist/variationConfiguration/variationConfiguration.d.ts +5 -6
  152. package/dist/variationConfiguration/variationConfiguration.js +263 -230
  153. package/dist/variationConfiguration/variationConfiguration.js.map +1 -1
  154. package/package.json +6 -4
  155. package/dist/hasher/constants.d.ts +0 -1
  156. package/dist/hasher/constants.js +0 -5
  157. package/dist/hasher/constants.js.map +0 -1
@@ -0,0 +1,38 @@
1
+ import { AddParametersType } from './types';
2
+ interface ICacheManager<T> {
3
+ /**
4
+ * Add item to cache
5
+ * @param {AddParametersType<T>} parameters - parameters for adding new item cache
6
+ * @returns {void}
7
+ */
8
+ add({ key, data, lifetime }: AddParametersType<T>): void;
9
+ /**
10
+ * Get item that is not expired from cache
11
+ * @param {string} key - key of item
12
+ * @returns {T} an item that is not expired
13
+ */
14
+ getAliveItem(key: string): T | null;
15
+ }
16
+ /**
17
+ * @class
18
+ * CacheManager - a class for managing cache
19
+ */
20
+ export declare class CacheManager<T> implements ICacheManager<T> {
21
+ private cacheMap;
22
+ private intervalId;
23
+ private cleanupTimeout;
24
+ /**
25
+ * @param {number} cleanupTimeout - timeout for cleaning cache in seconds
26
+ * generic type `T` is a type of data that will be stored in cache
27
+ * @example
28
+ * ```typescript
29
+ * const cacheManager = new CacheManager<number>(60);
30
+ * ```
31
+ */
32
+ constructor(cleanupTimeout: number);
33
+ add({ key, data, lifetime }: AddParametersType<T>): void;
34
+ getAliveItem(key: string): T | null;
35
+ private activate;
36
+ private cleanupInterval;
37
+ }
38
+ export {};
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CacheManager = void 0;
7
+ var _types = require("../types");
8
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
10
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
11
+ /**
12
+ * @class
13
+ * CacheManager - a class for managing cache
14
+ */
15
+ class CacheManager {
16
+ /**
17
+ * @param {number} cleanupTimeout - timeout for cleaning cache in seconds
18
+ * generic type `T` is a type of data that will be stored in cache
19
+ * @example
20
+ * ```typescript
21
+ * const cacheManager = new CacheManager<number>(60);
22
+ * ```
23
+ */
24
+ constructor(cleanupTimeout) {
25
+ _defineProperty(this, "cacheMap", new Map());
26
+ _defineProperty(this, "intervalId", null);
27
+ _defineProperty(this, "cleanupTimeout", void 0);
28
+ this.cleanupTimeout = cleanupTimeout * _types.Milliseconds.Second;
29
+ }
30
+ add({
31
+ key,
32
+ data,
33
+ lifetime
34
+ }) {
35
+ if (this.intervalId === null) {
36
+ this.activate();
37
+ }
38
+ const expirationTime = lifetime * _types.Milliseconds.Second;
39
+ const cacheItem = {
40
+ data,
41
+ expirationTime: Date.now() + expirationTime
42
+ };
43
+ this.cacheMap.set(key, cacheItem);
44
+ }
45
+ getAliveItem(key) {
46
+ const cacheItem = this.cacheMap.get(key);
47
+ if (cacheItem && cacheItem.expirationTime >= Date.now()) {
48
+ return cacheItem.data;
49
+ }
50
+ cacheItem && this.cacheMap.delete(key);
51
+ return null;
52
+ }
53
+ activate() {
54
+ this.intervalId = setInterval(() => {
55
+ const now = Date.now();
56
+ for (const [key, item] of this.cacheMap) {
57
+ if (item.expirationTime < now) {
58
+ this.cacheMap.delete(key);
59
+ }
60
+ }
61
+ if (!this.cacheMap.size) {
62
+ this.cleanupInterval();
63
+ }
64
+ }, this.cleanupTimeout);
65
+ }
66
+ cleanupInterval() {
67
+ if (this.intervalId) {
68
+ clearInterval(this.intervalId);
69
+ this.intervalId = null;
70
+ }
71
+ }
72
+ }
73
+ exports.CacheManager = CacheManager;
74
+ //# sourceMappingURL=cacheManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cacheManager.js","names":["_types","require","_defineProperty","obj","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","arg","_toPrimitive","String","input","hint","prim","Symbol","toPrimitive","undefined","res","call","TypeError","Number","CacheManager","constructor","cleanupTimeout","Map","Milliseconds","Second","add","data","lifetime","intervalId","activate","expirationTime","cacheItem","Date","now","cacheMap","set","getAliveItem","get","delete","setInterval","item","size","cleanupInterval","clearInterval","exports"],"sources":["../../src/cacheManager/cacheManager.ts"],"sourcesContent":["import { AddParametersType, CacheItemType } from './types';\nimport { Milliseconds } from '../types';\n\ninterface ICacheManager<T> {\n /**\n * Add item to cache\n * @param {AddParametersType<T>} parameters - parameters for adding new item cache\n * @returns {void}\n */\n add({ key, data, lifetime }: AddParametersType<T>): void;\n /**\n * Get item that is not expired from cache\n * @param {string} key - key of item\n * @returns {T} an item that is not expired\n */\n getAliveItem(key: string): T | null;\n}\n\n/**\n * @class\n * CacheManager - a class for managing cache\n */\nexport class CacheManager<T> implements ICacheManager<T> {\n private cacheMap = new Map<string, CacheItemType<T>>();\n private intervalId: NodeJS.Timeout | null = null;\n private cleanupTimeout: number;\n\n /**\n * @param {number} cleanupTimeout - timeout for cleaning cache in seconds\n * generic type `T` is a type of data that will be stored in cache\n * @example\n * ```typescript\n * const cacheManager = new CacheManager<number>(60);\n * ```\n */\n constructor(cleanupTimeout: number) {\n this.cleanupTimeout = cleanupTimeout * Milliseconds.Second;\n }\n\n public add({ key, data, lifetime }: AddParametersType<T>): void {\n if (this.intervalId === null) {\n this.activate();\n }\n\n const expirationTime = lifetime * Milliseconds.Second;\n\n const cacheItem: CacheItemType<T> = {\n data,\n expirationTime: Date.now() + expirationTime,\n };\n\n this.cacheMap.set(key, cacheItem);\n }\n\n public getAliveItem(key: string): T | null {\n const cacheItem = this.cacheMap.get(key);\n\n if (cacheItem && cacheItem.expirationTime >= Date.now()) {\n return cacheItem.data;\n }\n\n cacheItem && this.cacheMap.delete(key);\n\n return null;\n }\n\n private activate(): void {\n this.intervalId = setInterval(() => {\n const now = Date.now();\n\n for (const [key, item] of this.cacheMap) {\n if (item.expirationTime < now) {\n this.cacheMap.delete(key);\n }\n }\n\n if (!this.cacheMap.size) {\n this.cleanupInterval();\n }\n }, this.cleanupTimeout);\n }\n\n private cleanupInterval(): void {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n }\n}\n"],"mappings":"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"}
@@ -0,0 +1,2 @@
1
+ export { CacheManager } from './cacheManager';
2
+ export { AddParametersType } from './types';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "AddParametersType", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _types.AddParametersType;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "CacheManager", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _cacheManager.CacheManager;
16
+ }
17
+ });
18
+ var _cacheManager = require("./cacheManager");
19
+ var _types = require("./types");
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["_cacheManager","require","_types"],"sources":["../../src/cacheManager/index.ts"],"sourcesContent":["export { CacheManager } from './cacheManager';\nexport { AddParametersType } from './types';\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @param {string} key - key for adding new item cache, key must be unique otherwise the item will be overwritten
3
+ * @param {T} data - data for adding new item cache
4
+ * @param {number} lifetime - lifetime for adding new item cache in seconds
5
+ */
6
+ export type AddParametersType<T> = {
7
+ key: string;
8
+ data: T;
9
+ lifetime: number;
10
+ };
11
+ export type CacheItemType<T> = {
12
+ data: T;
13
+ expirationTime: number;
14
+ };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../src/cacheManager/types.ts"],"sourcesContent":["/**\n * @param {string} key - key for adding new item cache, key must be unique otherwise the item will be overwritten\n * @param {T} data - data for adding new item cache\n * @param {number} lifetime - lifetime for adding new item cache in seconds\n */\nexport type AddParametersType<T> = {\n key: string;\n data: T;\n lifetime: number;\n};\n\nexport type CacheItemType<T> = {\n data: T;\n expirationTime: number;\n};\n"],"mappings":""}
@@ -27,7 +27,8 @@ export declare class CampaignConfiguration implements ICampaignConfiguration {
27
27
  private storage;
28
28
  private requester;
29
29
  private eventSource?;
30
- constructor({ settings, storage, requester, }: CampaignConfigurationParametersType);
30
+ private externalClientConfiguration?;
31
+ constructor({ settings, storage, requester, externalClientConfiguration, targetingCleanupInterval, }: CampaignConfigurationParametersType);
31
32
  initialize(externalEventSource: IExternalEventSourceConstructor): Promise<Result<void, KameleoonError>>;
32
33
  addTargetingData(visitorCode: string, ...data: KameleoonDataType[]): Result<void, KameleoonError>;
33
34
  getUnsentData(visitorCode: string): KameleoonDataType[];
@@ -1,217 +1,264 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
12
6
  exports.CampaignConfiguration = void 0;
13
- const ts_res_1 = require("ts-res");
14
- const constants_1 = require("./constants");
15
- const eventSource_1 = require("../eventSource");
7
+ var _tsRes = require("ts-res");
8
+ var _constants = require("./constants");
9
+ var _eventSource = require("../eventSource");
10
+ var _types = require("../types");
11
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
15
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
16
16
  class CampaignConfiguration {
17
- constructor({ settings, storage, requester, }) {
18
- this.updateConfigurationIntervalId = null;
19
- this.targetingDataIntervalId = null;
20
- this.unsentTargetingData = {};
21
- this.targetingData = constants_1.DEFAULT_CAMPAIGN_CONFIGURATION.targetingData;
22
- this.configurationData = constants_1.DEFAULT_CAMPAIGN_CONFIGURATION.configurationData;
23
- this.requester = requester;
24
- this.settings = settings;
25
- this.lastUpdate = new Date();
26
- this.storage = storage;
27
- this.readStorageData();
28
- // --- Note ---
29
- // We can not make delayed code execution recoverable, as it might lead
30
- // to targeting data storage size overflow
31
- try {
32
- this.targetingDataIntervalId = setInterval(() => this.cleanupOutdatedTargetingData.call(this, this.targetingData), constants_1.CLEANUP_TARGETING_DATA_INTERVAL);
33
- }
34
- catch (err) {
35
- if (this.targetingDataIntervalId) {
36
- clearInterval(this.targetingDataIntervalId);
37
- }
38
- throw err;
17
+ constructor({
18
+ settings,
19
+ storage,
20
+ requester,
21
+ externalClientConfiguration,
22
+ targetingCleanupInterval
23
+ }) {
24
+ _defineProperty(this, "configurationUpdateCallback", void 0);
25
+ _defineProperty(this, "updateConfigurationIntervalId", null);
26
+ _defineProperty(this, "targetingDataIntervalId", null);
27
+ _defineProperty(this, "unsentTargetingData", {});
28
+ _defineProperty(this, "targetingData", _constants.DEFAULT_CAMPAIGN_CONFIGURATION.targetingData);
29
+ _defineProperty(this, "configurationData", _constants.DEFAULT_CAMPAIGN_CONFIGURATION.configurationData);
30
+ _defineProperty(this, "settings", void 0);
31
+ _defineProperty(this, "lastUpdate", void 0);
32
+ _defineProperty(this, "storage", void 0);
33
+ _defineProperty(this, "requester", void 0);
34
+ _defineProperty(this, "eventSource", void 0);
35
+ _defineProperty(this, "externalClientConfiguration", void 0);
36
+ this.requester = requester;
37
+ this.settings = settings;
38
+ this.lastUpdate = new Date();
39
+ this.storage = storage;
40
+ this.readStorageData();
41
+ this.externalClientConfiguration = externalClientConfiguration;
42
+ if (targetingCleanupInterval) {
43
+ const cleanupIntervalMinutes = targetingCleanupInterval < 0 ? 1 : targetingCleanupInterval;
44
+
45
+ // --- Note ---
46
+ // We can not make delayed code execution recoverable, as it might lead
47
+ // to targeting data storage size overflow
48
+ try {
49
+ this.targetingDataIntervalId = setInterval(() => this.cleanupOutdatedTargetingData.call(this, this.targetingData), cleanupIntervalMinutes * _types.Milliseconds.Minute);
50
+ } catch (err) {
51
+ if (this.targetingDataIntervalId) {
52
+ clearInterval(this.targetingDataIntervalId);
39
53
  }
54
+ throw err;
55
+ }
40
56
  }
41
- initialize(externalEventSource) {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- const shouldUpdate = this.checkShouldUpdate();
44
- // --- Initial data fetch ---
45
- if (shouldUpdate) {
46
- const updateResult = yield this.updateClientConfiguration();
47
- if (!updateResult.ok) {
48
- return updateResult;
49
- }
50
- }
51
- // --- Note ---
52
- // Interval is cleaned up in case real time update
53
- // was activated while initialized client already exists
54
- if (this.configurationData.configuration.realTimeUpdate) {
55
- if (this.updateConfigurationIntervalId) {
56
- clearInterval(this.updateConfigurationIntervalId);
57
- }
58
- this.eventSource = new eventSource_1.ExternalEventSource(this.settings.siteCode, externalEventSource);
59
- const updateEventCallback = (_timeStamp) => {
60
- this.updateClientConfiguration();
61
- if (this.configurationUpdateCallback) {
62
- this.configurationUpdateCallback();
63
- }
64
- };
65
- this.eventSource.open(updateEventCallback);
66
- }
67
- else {
68
- // --- Note ---
69
- // Event Source is cleaned up in case real time update
70
- // was disabled while initialized client already exists
71
- if (this.eventSource) {
72
- this.eventSource.close();
73
- }
74
- if (this.updateConfigurationIntervalId) {
75
- clearInterval(this.updateConfigurationIntervalId);
76
- }
77
- // --- Note ---
78
- // Same as targeting data interval we don't want to make failed configuration
79
- // retrieval recoverable
80
- try {
81
- this.updateConfigurationIntervalId = setInterval(this.updateClientConfiguration.bind(this), this.settings.updateInterval);
82
- }
83
- catch (err) {
84
- if (this.updateConfigurationIntervalId) {
85
- clearInterval(this.updateConfigurationIntervalId);
86
- }
87
- throw err;
88
- }
89
- }
90
- return (0, ts_res_1.Ok)();
91
- });
57
+ }
58
+ async initialize(externalEventSource) {
59
+ const shouldUpdate = this.checkShouldUpdate();
60
+
61
+ // --- Initial data fetch ---
62
+ if (shouldUpdate) {
63
+ const updateResult = await this.updateClientConfiguration();
64
+ if (!updateResult.ok) {
65
+ return updateResult;
66
+ }
92
67
  }
93
- addTargetingData(visitorCode, ...data) {
94
- if (!(visitorCode in this.targetingData)) {
95
- this.targetingData[visitorCode] = [];
96
- }
97
- data.forEach((dataItem) => {
98
- // --- Note ---
99
- // Previous check excludes the failure possibility
100
- this.targetingData[visitorCode].push({
101
- data: dataItem.data,
102
- lastActivityTime: Date.now(),
103
- });
104
- });
105
- // --- Note ---
106
- // `targetingData` should not refresh `lastUpdate` time
107
- const result = this.updateStorageData(this.lastUpdate);
108
- if (result.ok) {
109
- data.forEach((dataItem) => {
110
- if (!this.unsentTargetingData[visitorCode]) {
111
- this.unsentTargetingData[visitorCode] = [];
112
- }
113
- this.unsentTargetingData[visitorCode].push(dataItem);
114
- });
68
+
69
+ // --- Note ---
70
+ // Interval is cleaned up in case real time update
71
+ // was activated while initialized client already exists
72
+ if (this.configurationData.configuration.realTimeUpdate && !this.externalClientConfiguration) {
73
+ if (this.updateConfigurationIntervalId) {
74
+ clearInterval(this.updateConfigurationIntervalId);
75
+ }
76
+ this.eventSource = new _eventSource.ExternalEventSource(this.settings.siteCode, externalEventSource);
77
+ const updateEventCallback = _timeStamp => {
78
+ this.updateClientConfiguration();
79
+ if (this.configurationUpdateCallback) {
80
+ this.configurationUpdateCallback();
115
81
  }
116
- return result;
117
- }
118
- getUnsentData(visitorCode) {
119
- const unsentDataItem = this.unsentTargetingData[visitorCode];
120
- if (!unsentDataItem) {
121
- return [];
82
+ };
83
+ this.eventSource.open(updateEventCallback);
84
+ } else {
85
+ // --- Note ---
86
+ // Event Source is cleaned up in case real time update
87
+ // was disabled while initialized client already exists
88
+ if (this.eventSource) {
89
+ this.eventSource.close();
90
+ }
91
+ if (this.updateConfigurationIntervalId) {
92
+ clearInterval(this.updateConfigurationIntervalId);
93
+ }
94
+
95
+ // --- Note ---
96
+ // Same as targeting data interval we don't want to make failed configuration
97
+ // retrieval recoverable
98
+ try {
99
+ this.updateConfigurationIntervalId = setInterval(this.updateClientConfiguration.bind(this), this.settings.updateInterval);
100
+ } catch (err) {
101
+ if (this.updateConfigurationIntervalId) {
102
+ clearInterval(this.updateConfigurationIntervalId);
122
103
  }
123
- return unsentDataItem;
124
- }
125
- clearUnsentData(visitorCode) {
126
- this.unsentTargetingData[visitorCode] = [];
127
- }
128
- onConfigurationUpdate(callback) {
129
- this.configurationUpdateCallback = callback;
104
+ throw err;
105
+ }
130
106
  }
131
- get storedTargetingData() {
132
- return this.targetingData;
107
+ return (0, _tsRes.Ok)();
108
+ }
109
+ addTargetingData(visitorCode, ...data) {
110
+ if (!(visitorCode in this.targetingData)) {
111
+ this.targetingData[visitorCode] = [];
133
112
  }
134
- get experiments() {
135
- return this.configurationData.experiments;
113
+ data.forEach(dataItem => {
114
+ // --- Note ---
115
+ // Previous check excludes the failure possibility
116
+ this.targetingData[visitorCode].push({
117
+ data: dataItem.data,
118
+ lastActivityTime: Date.now()
119
+ });
120
+ });
121
+
122
+ // --- Note ---
123
+ // `targetingData` should not refresh `lastUpdate` time
124
+ const result = this.updateStorageData(this.lastUpdate);
125
+ if (result.ok) {
126
+ data.forEach(dataItem => {
127
+ if (!this.unsentTargetingData[visitorCode]) {
128
+ this.unsentTargetingData[visitorCode] = [];
129
+ }
130
+ this.unsentTargetingData[visitorCode].push(dataItem);
131
+ });
136
132
  }
137
- get featureFlags() {
138
- return this.configurationData.featureFlags;
133
+ return result;
134
+ }
135
+ getUnsentData(visitorCode) {
136
+ const unsentDataItem = this.unsentTargetingData[visitorCode];
137
+ if (!unsentDataItem) {
138
+ return [];
139
139
  }
140
- get configuration() {
141
- return this.configurationData.configuration;
140
+ return unsentDataItem;
141
+ }
142
+ clearUnsentData(visitorCode) {
143
+ this.unsentTargetingData[visitorCode] = [];
144
+ }
145
+ onConfigurationUpdate(callback) {
146
+ this.configurationUpdateCallback = callback;
147
+ }
148
+ get storedTargetingData() {
149
+ return this.targetingData;
150
+ }
151
+ get experiments() {
152
+ return this.configurationData.experiments;
153
+ }
154
+ get featureFlags() {
155
+ return this.configurationData.featureFlags;
156
+ }
157
+ get configuration() {
158
+ return this.configurationData.configuration;
159
+ }
160
+ checkShouldUpdate() {
161
+ const {
162
+ featureFlags,
163
+ experiments,
164
+ configuration
165
+ } = this.configurationData;
166
+ const {
167
+ updateInterval
168
+ } = this.settings;
169
+ if (!featureFlags.length && !experiments.length) {
170
+ return true;
142
171
  }
143
- checkShouldUpdate() {
144
- const { featureFlags, experiments, configuration } = this.configurationData;
145
- const { updateInterval } = this.settings;
146
- if (!featureFlags.length && !experiments.length) {
147
- return true;
148
- }
149
- if (configuration.realTimeUpdate) {
150
- return true;
151
- }
152
- const lastUpdateTime = this.lastUpdate.getTime();
153
- const updateWindowEdge = Date.now() - updateInterval;
154
- return lastUpdateTime < updateWindowEdge;
172
+ if (configuration.realTimeUpdate) {
173
+ return true;
155
174
  }
156
- cleanupOutdatedTargetingData(data) {
157
- const currentTime = Date.now();
158
- const resultTargetingData = Object.assign({}, data);
159
- for (const [key, value] of Object.entries(resultTargetingData)) {
160
- if (value) {
161
- const resultValue = value.filter((item) => {
162
- const isDataExpired = item.lastActivityTime + constants_1.SESSION_DURATION < currentTime;
163
- return isDataExpired;
164
- });
165
- resultTargetingData[key] = resultValue;
166
- }
167
- else {
168
- resultTargetingData[key] = [];
169
- }
170
- }
171
- this.targetingData = resultTargetingData;
172
- // --- Note ---
173
- // `targetingData` should not refresh `lastUpdate` time
174
- this.updateStorageData(this.lastUpdate).throw();
175
+ const lastUpdateTime = this.lastUpdate.getTime();
176
+ const updateWindowEdge = Date.now() - updateInterval;
177
+ return lastUpdateTime < updateWindowEdge;
178
+ }
179
+ cleanupOutdatedTargetingData(data) {
180
+ const currentTime = Date.now();
181
+ const resultTargetingData = _objectSpread({}, data);
182
+ for (const [key, value] of Object.entries(resultTargetingData)) {
183
+ if (value) {
184
+ const resultValue = value.filter(item => {
185
+ const isDataExpired = item.lastActivityTime + _constants.SESSION_DURATION < currentTime;
186
+ return isDataExpired;
187
+ });
188
+ resultTargetingData[key] = resultValue;
189
+ } else {
190
+ resultTargetingData[key] = [];
191
+ }
175
192
  }
176
- readStorageData() {
177
- const result = this.storage.read();
178
- if (result.ok) {
179
- const { kameleoonTargetingData, kameleoonConfiguration, lastUpdate } = result.data;
180
- this.configurationData = kameleoonConfiguration;
181
- this.targetingData = kameleoonTargetingData;
182
- this.lastUpdate = new Date(Date.parse(lastUpdate));
183
- }
193
+ this.targetingData = resultTargetingData;
194
+ // --- Note ---
195
+ // `targetingData` should not refresh `lastUpdate` time
196
+ this.updateStorageData(this.lastUpdate).throw();
197
+ }
198
+ readStorageData() {
199
+ const result = this.storage.read();
200
+ if (result.ok) {
201
+ const {
202
+ kameleoonTargetingData,
203
+ kameleoonConfiguration,
204
+ lastUpdate
205
+ } = result.data;
206
+ this.configurationData = kameleoonConfiguration;
207
+ this.targetingData = kameleoonTargetingData;
208
+ this.lastUpdate = new Date(Date.parse(lastUpdate));
184
209
  }
185
- updateClientConfiguration(timeStamp) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- const clientConfigurationResult = yield this.requester.getClientConfiguration(timeStamp);
188
- if (!clientConfigurationResult.ok) {
189
- return clientConfigurationResult;
190
- }
191
- // --- Note ---
192
- // `featureFlags` field is ommited (v1)
193
- // instead `featureFlagConfigurations` is used (v2)
194
- const { configuration, experiments, featureFlagConfigurations } = clientConfigurationResult.data;
195
- this.configurationData = {
196
- configuration,
197
- experiments,
198
- featureFlags: featureFlagConfigurations,
199
- };
200
- const result = this.updateStorageData(new Date());
201
- if (!result.ok) {
202
- return result;
203
- }
204
- return (0, ts_res_1.Ok)(true);
205
- });
210
+ }
211
+ async updateClientConfiguration(timeStamp) {
212
+ // --- Note ---
213
+ // if `externalClientConfiguration` is passed, fetching is disabled and passed config is used instead
214
+ if (this.externalClientConfiguration) {
215
+ // --- Note ---
216
+ // `featureFlags` field is omitted (v1)
217
+ // instead `featureFlagConfigurations` is used (v2)
218
+ const {
219
+ configuration,
220
+ experiments,
221
+ featureFlagConfigurations
222
+ } = this.externalClientConfiguration;
223
+ this.configurationData = {
224
+ configuration,
225
+ experiments,
226
+ featureFlags: featureFlagConfigurations
227
+ };
228
+ } else {
229
+ const clientConfigurationResult = await this.requester.getClientConfiguration(timeStamp);
230
+ if (!clientConfigurationResult.ok) {
231
+ return clientConfigurationResult;
232
+ }
233
+
234
+ // --- Note ---
235
+ // `featureFlags` field is omitted (v1)
236
+ // instead `featureFlagConfigurations` is used (v2)
237
+ const {
238
+ configuration,
239
+ experiments,
240
+ featureFlagConfigurations
241
+ } = clientConfigurationResult.data;
242
+ this.configurationData = {
243
+ configuration,
244
+ experiments,
245
+ featureFlags: featureFlagConfigurations
246
+ };
206
247
  }
207
- updateStorageData(lastUpdate) {
208
- const data = {
209
- kameleoonTargetingData: this.targetingData,
210
- kameleoonConfiguration: this.configurationData,
211
- lastUpdate: lastUpdate.toString(),
212
- };
213
- return this.storage.write(data);
248
+ const result = this.updateStorageData(new Date());
249
+ if (!result.ok) {
250
+ return result;
214
251
  }
252
+ return (0, _tsRes.Ok)(true);
253
+ }
254
+ updateStorageData(lastUpdate) {
255
+ const data = {
256
+ kameleoonTargetingData: this.targetingData,
257
+ kameleoonConfiguration: this.configurationData,
258
+ lastUpdate: lastUpdate.toString()
259
+ };
260
+ return this.storage.write(data);
261
+ }
215
262
  }
216
263
  exports.CampaignConfiguration = CampaignConfiguration;
217
264
  //# sourceMappingURL=campaignConfiguration.js.map