Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kameleoon/javascript-sdk-core

Package Overview
Dependencies
Maintainers
3
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kameleoon/javascript-sdk-core - npm Package Compare versions

Comparing version 2.7.0 to 2.7.1

6

dist/kameleoonClient.js

@@ -7,3 +7,7 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonClient=void 0;var _tsRes=require("ts-res"),_cacheManager=require("./cacheManager"),_campaignConfiguration=require("./campaignConfiguration"),_clientSettings=require("./clientSettings"),_constants=require("./constants"),_kameleoonData=require("./kameleoonData"),_kameleoonError=require("./kameleoonError"),_requester=require("./requester"),_storage=require("./storage"),_externalStorage=require("./storage/externalStorage"),_utilities=require("./utilities"),_variationConfiguration=require("./variationConfiguration");function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c.return||c.return()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */function a(a,b,c){return Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}),a[b]}function b(a,b,c,e){var f=b&&b.prototype instanceof d?b:d,g=Object.create(f.prototype),h=new m(e||[]);return s(g,"_invoke",{value:i(a,c,h)}),g}function c(a,b,c){try{return{type:"normal",arg:a.call(b,c)}}catch(a){return{type:"throw",arg:a}}}function d(){}function e(){}function f(){}function g(b){["next","throw","return"].forEach(function(c){a(b,c,function(a){return this._invoke(c,a)})})}function h(a,b){function d(e,f,g,h){var i=c(a[e],a,f);if("throw"!==i.type){var j=i.arg,k=j.value;return k&&"object"==_typeof(k)&&r.call(k,"__await")?b.resolve(k.__await).then(function(a){d("next",a,g,h)},function(a){d("throw",a,g,h)}):b.resolve(k).then(function(a){j.value=a,g(j)},function(a){return d("throw",a,g,h)})}h(i.arg)}var e;s(this,"_invoke",{value:function value(a,c){function f(){return new b(function(b,e){d(a,c,b,e)})}return e=e?e.then(f,f):f()}})}function i(a,b,d){var e="suspendedStart";return function(f,g){if("executing"==e)throw new Error("Generator is already running");if("completed"==e){if("throw"===f)throw g;return o()}for(d.method=f,d.arg=g;;){var h=d.delegate;if(h){var i=j(h,d);if(i){if(i===x)continue;return i}}if("next"===d.method)d.sent=d._sent=d.arg;else if("throw"===d.method){if("suspendedStart"==e)throw e="completed",d.arg;d.dispatchException(d.arg)}else"return"===d.method&&d.abrupt("return",d.arg);e="executing";var k=c(a,b,d);if("normal"===k.type){if(e=d.done?"completed":"suspendedYield",k.arg===x)continue;return{value:k.arg,done:d.done}}"throw"===k.type&&(e="completed",d.method="throw",d.arg=k.arg)}}}function j(a,b){var d=b.method,e=a.iterator[d];if(void 0===e)return b.delegate=null,"throw"===d&&a.iterator.return&&(b.method="return",b.arg=void 0,j(a,b),"throw"===b.method)||"return"!==d&&(b.method="throw",b.arg=new TypeError("The iterator does not provide a '"+d+"' method")),x;var f=c(e,a.iterator,b.arg);if("throw"===f.type)return b.method="throw",b.arg=f.arg,b.delegate=null,x;var g=f.arg;return g?g.done?(b[a.resultName]=g.value,b.next=a.nextLoc,"return"!==b.method&&(b.method="next",b.arg=void 0),b.delegate=null,x):g:(b.method="throw",b.arg=new TypeError("iterator result is not an object"),b.delegate=null,x)}function k(a){var b={tryLoc:a[0]};1 in a&&(b.catchLoc=a[1]),2 in a&&(b.finallyLoc=a[2],b.afterLoc=a[3]),this.tryEntries.push(b)}function l(a){var b=a.completion||{};b.type="normal",delete b.arg,a.completion=b}function m(a){this.tryEntries=[{tryLoc:"root"}],a.forEach(k,this),this.reset(!0)}function n(a){if(a){var b=a[u];if(b)return b.call(a);if("function"==typeof a.next)return a;if(!isNaN(a.length)){var c=-1,d=function b(){for(;++c<a.length;)if(r.call(a,c))return b.value=a[c],b.done=!1,b;return b.value=void 0,b.done=!0,b};return d.next=d}}return{next:o}}function o(){return{value:void 0,done:!0}}_regeneratorRuntime=function(){return p};var p={},q=Object.prototype,r=q.hasOwnProperty,s=Object.defineProperty||function(a,b,c){a[b]=c.value},t="function"==typeof Symbol?Symbol:{},u=t.iterator||"@@iterator",v=t.asyncIterator||"@@asyncIterator",w=t.toStringTag||"@@toStringTag";try{a({},"")}catch(b){a=function(a,b,c){return a[b]=c}}p.wrap=b;var x={},y={};a(y,u,function(){return this});var z=Object.getPrototypeOf,A=z&&z(z(n([])));A&&A!==q&&r.call(A,u)&&(y=A);var B=f.prototype=d.prototype=Object.create(y);return e.prototype=f,s(B,"constructor",{value:f,configurable:!0}),s(f,"constructor",{value:e,configurable:!0}),e.displayName=a(f,w,"GeneratorFunction"),p.isGeneratorFunction=function(a){var b="function"==typeof a&&a.constructor;return!!b&&(b===e||"GeneratorFunction"===(b.displayName||b.name))},p.mark=function(b){return Object.setPrototypeOf?Object.setPrototypeOf(b,f):(b.__proto__=f,a(b,w,"GeneratorFunction")),b.prototype=Object.create(B),b},p.awrap=function(a){return{__await:a}},g(h.prototype),a(h.prototype,v,function(){return this}),p.AsyncIterator=h,p.async=function(a,c,d,e,f){void 0===f&&(f=Promise);var g=new h(b(a,c,d,e),f);return p.isGeneratorFunction(c)?g:g.next().then(function(a){return a.done?a.value:g.next()})},g(B),a(B,w,"Generator"),a(B,u,function(){return this}),a(B,"toString",function(){return"[object Generator]"}),p.keys=function(a){var b=Object(a),c=[];for(var d in b)c.push(d);return c.reverse(),function a(){for(;c.length;){var d=c.pop();if(d in b)return a.value=d,a.done=!1,a}return a.done=!0,a}},p.values=n,m.prototype={constructor:m,reset:function reset(a){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(l),!a)for(var b in this)"t"===b.charAt(0)&&r.call(this,b)&&!isNaN(+b.slice(1))&&(this[b]=void 0)},stop:function stop(){this.done=!0;var a=this.tryEntries[0].completion;if("throw"===a.type)throw a.arg;return this.rval},dispatchException:function dispatchException(a){function b(b,d){return f.type="throw",f.arg=a,c.next=b,d&&(c.method="next",c.arg=void 0),!!d}if(this.done)throw a;for(var c=this,d=this.tryEntries.length-1;0<=d;--d){var e=this.tryEntries[d],f=e.completion;if("root"===e.tryLoc)return b("end");if(e.tryLoc<=this.prev){var g=r.call(e,"catchLoc"),h=r.call(e,"finallyLoc");if(g&&h){if(this.prev<e.catchLoc)return b(e.catchLoc,!0);if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(!g){if(!h)throw new Error("try statement without catch or finally");if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(this.prev<e.catchLoc)return b(e.catchLoc,!0)}}},abrupt:function abrupt(a,b){for(var c,d=this.tryEntries.length-1;0<=d;--d)if(c=this.tryEntries[d],c.tryLoc<=this.prev&&r.call(c,"finallyLoc")&&this.prev<c.finallyLoc){var e=c;break}e&&("break"===a||"continue"===a)&&e.tryLoc<=b&&b<=e.finallyLoc&&(e=null);var f=e?e.completion:{};return f.type=a,f.arg=b,e?(this.method="next",this.next=e.finallyLoc,x):this.complete(f)},complete:function complete(a,b){if("throw"===a.type)throw a.arg;return"break"===a.type||"continue"===a.type?this.next=a.arg:"return"===a.type?(this.rval=this.arg=a.arg,this.method="return",this.next="end"):"normal"===a.type&&b&&(this.next=b),x},finish:function finish(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.finallyLoc===a)return this.complete(b.completion,b.afterLoc),l(b),x},catch:function _catch(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.tryLoc===a){var d=b.completion;if("throw"===d.type){var e=d.arg;l(b)}return e}throw new Error("illegal catch attempt")},delegateYield:function delegateYield(a,b,c){return this.delegate={iterator:n(a),resultName:b,nextLoc:c},"next"===this.method&&(this.arg=void 0),x}},p}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,_toPropertyKey(c.key),c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _defineProperty(a,b,c){return b=_toPropertyKey(b),b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"===_typeof(b)?b:b+""}function _toPrimitive(a,b){if("object"!==_typeof(a)||null===a)return a;var c=a[Symbol.toPrimitive];if(c!==void 0){var d=c.call(a,b||"default");if("object"!==_typeof(d))return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}/**

* @param {SDKCoreParameters} sdkCoreParameters - parameters for initializing sdk core
*/function a(b){var c=b.siteCode,d=b.configuration,e=b.internalConfiguration;_classCallCheck(this,a),_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"campaignConfiguration",void 0),_defineProperty(this,"variationDataStorage",void 0),_defineProperty(this,"offlineTrackingStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalEventSource",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"isOfflineModeOn",void 0);var f=new _clientSettings.ClientSettings(c,d),g=f.settings,h=e.externalStorage,i=e.externalEventSource,j=e.externalPackageInfo,k=e.externalRequestDispatcher,l=e.externalClientConfiguration,m=new _requester.Requester({siteCode:c,environment:g.environment,packageInfo:j,requestDispatcher:k}),n=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),o=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.ClientData)),p=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.VariationData)),q=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.OfflineTracking)),r=new _campaignConfiguration.CampaignConfiguration({settings:g,storage:o,requester:m,externalClientConfiguration:l,targetingCleanupInterval:null===d||void 0===d?void 0:d.targetingDataCleanupInterval,offlineTrackingStorage:q});this.requester=m,this.trackingCache=n,this.campaignConfiguration=r,this.variationDataStorage=p,this.externalEventSource=i,this.externalPackageInfo=j,this.offlineTrackingStorage=q}return _createClass(a,[{key:"initialize",value:function(){function a(){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d;return _regeneratorRuntime().wrap(function e(b){for(;1;)switch(b.prev=b.next){case 0:return b.next=2,this.campaignConfiguration.initialize(this.externalEventSource,a);case 2:return c=b.sent,c.throw(),d=new _variationConfiguration.VariationConfiguration(this.campaignConfiguration.experiments,this.variationDataStorage),this.variationConfiguration=d,this.isOfflineModeOn=a,b.abrupt("return",c.ok);case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"addData",value:function addData(a){var b;if(_utilities.Utilities.validateVisitorCode(a).throw(),!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);for(var c=arguments.length,d=Array(1<c?c-1:0),e=1;e<c;e++)d[e-1]=arguments[e];(b=this.campaignConfiguration).addTargetingData.apply(b,[a].concat(d)).throw()}},{key:"getRemoteVisitorData",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j=arguments;return _regeneratorRuntime().wrap(function k(b){for(;1;)switch(b.prev=b.next){case 0:return c=!(1<j.length&&void 0!==j[1])||j[1],b.next=3,this.requester.getVisitorData(a);case 3:if(d=b.sent,e=d.throw(),f=_utilities.Utilities.parseVisitorData(e),c){g=_createForOfIteratorHelper(f);try{for(g.s();!(h=g.n()).done;)i=h.value,this.addData(a,i)}catch(a){g.e(a)}finally{g.f()}}return b.abrupt("return",f);case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"triggerExperiment",value:function triggerExperiment(a,b){var c=this;if(_utilities.Utilities.validateVisitorCode(a).throw(),!this.campaignConfiguration||!this.variationConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);var d=this.campaignConfiguration.experiments.find(function(a){return a.id===b+""});if(!d)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.ExperimentConfigurationNotFound,b,a);var e=this.campaignConfiguration.storedTargetingData[a],f=_constants.UNALLOCATED_VARIATION_ID,g=_utilities.Utilities.checkTargeting({visitorCode:a,experimentId:b,targetingData:e,segment:d.segment,campaignConfiguration:this.campaignConfiguration,variationConfiguration:this.variationConfiguration,packageInfo:this.externalPackageInfo}).throw();if(f=this.variationConfiguration.getAssociatedVariationId(a,b).else(function(d){if(d.type===_kameleoonError.KameleoonException.NotAllocated)return _utilities.Utilities.trackExperiment({requester:c.requester,campaignConfiguration:c.campaignConfiguration,isUnallocated:!0,variationId:f,visitorCode:a,experimentId:b,isOfflineModeOn:c.isOfflineModeOn,offlineTrackingStorage:c.offlineTrackingStorage}),_constants.UNALLOCATED_VARIATION_ID;throw d}),_utilities.Utilities.trackExperiment({requester:this.requester,campaignConfiguration:this.campaignConfiguration,isUnallocated:!1,variationId:f,visitorCode:a,experimentId:b,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage}),!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.NotTargeted,a);return _utilities.Utilities.updateCache({cacheManager:this.trackingCache,visitorCode:a,experimentId:b,variationId:f}),f}},{key:"trackConversion",value:function trackConversion(a){var b=a.visitorCode,c=a.goalId,d=a.revenue;this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b)}},{key:"flush",value:function flush(a){if(!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a&&_utilities.Utilities.validateVisitorCode(a).throw(),a)return void _utilities.Utilities.trackData({visitorCode:a,campaignConfiguration:this.campaignConfiguration,requester:this.requester,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage});var b,c=_createForOfIteratorHelper(this.campaignConfiguration.unsentDataVisitors);try{for(c.s();!(b=c.n()).done;){var d=b.value;_utilities.Utilities.trackData({visitorCode:d,campaignConfiguration:this.campaignConfiguration,requester:this.requester,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage})}}catch(a){c.e(a)}finally{c.f()}}},{key:"flushData",value:function flushData(a){this.flush(a)}},{key:"getExperiments",value:function getExperiments(){if(!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);return this.campaignConfiguration.experiments.map(function(a){var b=a.id,c=a.name;return{id:+b,name:c}})}},{key:"getVisitorExperiments",value:function getVisitorExperiments(a){var b=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];if(!this.variationConfiguration||!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);_utilities.Utilities.validateVisitorCode(a).throw();var c,d=this.campaignConfiguration.experiments,e=[],f=this.campaignConfiguration.storedTargetingData[a],g=_createForOfIteratorHelper(d);try{for(g.s();!(c=g.n()).done;){var h=c.value,i=h.id,j=h.name,k=h.segment,l=_utilities.Utilities.checkTargeting({targetingData:f,visitorCode:a,segment:k,variationConfiguration:this.variationConfiguration,campaignConfiguration:this.campaignConfiguration,experimentId:+i,packageInfo:this.externalPackageInfo}).throw();if(l){// -- If `isActive=false` then return every experiment
*/function a(b){var c=b.siteCode,d=b.configuration,e=b.internalConfiguration;_classCallCheck(this,a),_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"campaignConfiguration",void 0),_defineProperty(this,"variationDataStorage",void 0),_defineProperty(this,"offlineTrackingStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalEventSource",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"isOfflineModeOn",void 0);var f=new _clientSettings.ClientSettings(c,d),g=f.settings,h=e.externalStorage,i=e.externalEventSource,j=e.externalPackageInfo,k=e.externalRequestDispatcher,l=e.externalClientConfiguration,m=new _requester.Requester({siteCode:c,environment:g.environment,packageInfo:j,requestDispatcher:k}),n=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),o=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.ClientData)),p=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.VariationData)),q=new _externalStorage.ExternalStorage(h.initialize(_storage.KameleoonStorageKey.OfflineTracking)),r=new _campaignConfiguration.CampaignConfiguration({settings:g,storage:o,requester:m,externalClientConfiguration:l,targetingCleanupInterval:null===d||void 0===d?void 0:d.targetingDataCleanupInterval,offlineTrackingStorage:q});this.requester=m,this.trackingCache=n,this.campaignConfiguration=r,this.variationDataStorage=p,this.externalEventSource=i,this.externalPackageInfo=j,this.offlineTrackingStorage=q}return _createClass(a,[{key:"initialize",value:function(){function a(){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d;return _regeneratorRuntime().wrap(function e(b){for(;1;)switch(b.prev=b.next){case 0:return b.next=2,this.campaignConfiguration.initialize(this.externalEventSource,a);case 2:return c=b.sent,c.throw(),d=new _variationConfiguration.VariationConfiguration(this.campaignConfiguration.experiments,this.variationDataStorage),this.variationConfiguration=d,this.isOfflineModeOn=a,b.abrupt("return",c.ok);case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"addData",value:function addData(a){var b;if(_utilities.Utilities.validateVisitorCode(a).throw(),!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);for(var c=arguments.length,d=Array(1<c?c-1:0),e=1;e<c;e++)d[e-1]=arguments[e];(b=this.campaignConfiguration).addTargetingData.apply(b,[a].concat(d)).throw()}},{key:"getRemoteVisitorData",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j=arguments;return _regeneratorRuntime().wrap(function k(b){for(;1;)switch(b.prev=b.next){case 0:return c=!(1<j.length&&void 0!==j[1])||j[1],b.next=3,this.requester.getVisitorData(a);case 3:if(d=b.sent,e=d.throw(),f=_utilities.Utilities.parseVisitorData(e),c){g=_createForOfIteratorHelper(f);try{for(g.s();!(h=g.n()).done;)i=h.value,this.addData(a,i)}catch(a){g.e(a)}finally{g.f()}}return b.abrupt("return",f);case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"triggerExperiment",value:function triggerExperiment(a,b){var c=this;if(_utilities.Utilities.validateVisitorCode(a).throw(),!this.campaignConfiguration||!this.variationConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);var d=this.campaignConfiguration.experiments.find(function(a){return a.id===b+""});if(!d)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.ExperimentConfigurationNotFound,b,a);var e=this.campaignConfiguration.storedTargetingData[a],f=_constants.UNALLOCATED_VARIATION_ID,g=_utilities.Utilities.checkTargeting({visitorCode:a,experimentId:b,targetingData:e,segment:d.segment,campaignConfiguration:this.campaignConfiguration,variationConfiguration:this.variationConfiguration,packageInfo:this.externalPackageInfo}).throw();if(f=this.variationConfiguration.getAssociatedVariationId(a,b).else(function(d){if(d.type===_kameleoonError.KameleoonException.NotAllocated)return _utilities.Utilities.trackExperiment({requester:c.requester,campaignConfiguration:c.campaignConfiguration,isUnallocated:!0,variationId:f,visitorCode:a,experimentId:b,isOfflineModeOn:c.isOfflineModeOn,offlineTrackingStorage:c.offlineTrackingStorage}),_constants.UNALLOCATED_VARIATION_ID;throw d}),_utilities.Utilities.trackExperiment({requester:this.requester,campaignConfiguration:this.campaignConfiguration,isUnallocated:!1,variationId:f,visitorCode:a,experimentId:b,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage}),!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.NotTargeted,a);return _utilities.Utilities.updateCache({cacheManager:this.trackingCache,visitorCode:a,experimentId:b,variationId:f}),f}},{key:"trackConversion",value:function trackConversion(a){var b=a.visitorCode,c=a.goalId,d=a.revenue;this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b)}},{key:"flush",value:function flush(a){if(!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a&&_utilities.Utilities.validateVisitorCode(a).throw(),a&&this.campaignConfiguration.unsentDataVisitors.includes(a))return void _utilities.Utilities.trackData({visitorCode:a,campaignConfiguration:this.campaignConfiguration,requester:this.requester,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage});// --- Note ---
// `Utilities.trackData` executes `Utilities.sendOfflineTracking` asynchronously at first, but
// we need to execute `Utilities.sendOfflineTracking` even if calling `Utilities.trackData` is skipped
// due to lack of unsent data
if(!this.campaignConfiguration.unsentDataVisitors.length)return void _utilities.Utilities.sendOfflineTracking({requester:this.requester,storage:this.offlineTrackingStorage});var b,c=_createForOfIteratorHelper(this.campaignConfiguration.unsentDataVisitors);try{for(c.s();!(b=c.n()).done;){var d=b.value;_utilities.Utilities.trackData({visitorCode:d,campaignConfiguration:this.campaignConfiguration,requester:this.requester,isOfflineModeOn:this.isOfflineModeOn,offlineTrackingStorage:this.offlineTrackingStorage})}}catch(a){c.e(a)}finally{c.f()}}},{key:"flushData",value:function flushData(a){this.flush(a)}},{key:"getExperiments",value:function getExperiments(){if(!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);return this.campaignConfiguration.experiments.map(function(a){var b=a.id,c=a.name;return{id:+b,name:c}})}},{key:"getVisitorExperiments",value:function getVisitorExperiments(a){var b=!(1<arguments.length&&void 0!==arguments[1])||arguments[1];if(!this.variationConfiguration||!this.campaignConfiguration)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);_utilities.Utilities.validateVisitorCode(a).throw();var c,d=this.campaignConfiguration.experiments,e=[],f=this.campaignConfiguration.storedTargetingData[a],g=_createForOfIteratorHelper(d);try{for(g.s();!(c=g.n()).done;){var h=c.value,i=h.id,j=h.name,k=h.segment,l=_utilities.Utilities.checkTargeting({targetingData:f,visitorCode:a,segment:k,variationConfiguration:this.variationConfiguration,campaignConfiguration:this.campaignConfiguration,experimentId:+i,packageInfo:this.externalPackageInfo}).throw();if(l){// -- If `isActive=false` then return every experiment
if(!b){e.push({id:+i,name:j});continue}// -- Else first check if there are saved variations for the experiment

@@ -10,0 +14,0 @@ // if there is at least one than it's active

2

dist/utilities/utilities.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Utilities=void 0;var _tsRes=require("ts-res"),_kameleoonError=require("../kameleoonError"),_types=require("../types"),_kameleoonData=require("../kameleoonData"),_constants=require("../constants"),_constants2=require("./constants"),_excluded=["timestamp","isExperiment"];function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _objectWithoutProperties(a,b){if(null==a)return{};var c,d,e=_objectWithoutPropertiesLoose(a,b);if(Object.getOwnPropertySymbols){var f=Object.getOwnPropertySymbols(a);for(d=0;d<f.length;d++)c=f[d],0<=b.indexOf(c)||Object.prototype.propertyIsEnumerable.call(a,c)&&(e[c]=a[c])}return e}function _objectWithoutPropertiesLoose(a,b){if(null==a)return{};var c,d,e={},f=Object.keys(a);for(d=0;d<f.length;d++)c=f[d],0<=b.indexOf(c)||(e[c]=a[c]);return e}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return b=_toPropertyKey(b),b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f,g,h=[],i=!0,j=!1;try{if(f=(c=c.call(a)).next,0===b){if(Object(c)!==c)return;i=!1}else for(;!(i=(d=f.call(c)).done)&&(h.push(d.value),h.length!==b);i=!0);}catch(a){j=!0,e=a}finally{try{if(!i&&null!=c.return&&(g=c.return(),Object(g)!==g))return}finally{if(j)throw e}}return h}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _construct(){return _construct=_isNativeReflectConstruct()?Reflect.construct.bind():function(b,c,d){var e=[null];e.push.apply(e,c);var a=Function.bind.apply(b,e),f=new a;return d&&_setPrototypeOf(f,d.prototype),f},_construct.apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(a){return!1}}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */function a(a,b,c){return Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}),a[b]}function b(a,b,c,e){var f=b&&b.prototype instanceof d?b:d,g=Object.create(f.prototype),h=new m(e||[]);return s(g,"_invoke",{value:i(a,c,h)}),g}function c(a,b,c){try{return{type:"normal",arg:a.call(b,c)}}catch(a){return{type:"throw",arg:a}}}function d(){}function e(){}function f(){}function g(b){["next","throw","return"].forEach(function(c){a(b,c,function(a){return this._invoke(c,a)})})}function h(a,b){function d(e,f,g,h){var i=c(a[e],a,f);if("throw"!==i.type){var j=i.arg,k=j.value;return k&&"object"==_typeof(k)&&r.call(k,"__await")?b.resolve(k.__await).then(function(a){d("next",a,g,h)},function(a){d("throw",a,g,h)}):b.resolve(k).then(function(a){j.value=a,g(j)},function(a){return d("throw",a,g,h)})}h(i.arg)}var e;s(this,"_invoke",{value:function value(a,c){function f(){return new b(function(b,e){d(a,c,b,e)})}return e=e?e.then(f,f):f()}})}function i(a,b,d){var e="suspendedStart";return function(f,g){if("executing"==e)throw new Error("Generator is already running");if("completed"==e){if("throw"===f)throw g;return o()}for(d.method=f,d.arg=g;;){var h=d.delegate;if(h){var i=j(h,d);if(i){if(i===x)continue;return i}}if("next"===d.method)d.sent=d._sent=d.arg;else if("throw"===d.method){if("suspendedStart"==e)throw e="completed",d.arg;d.dispatchException(d.arg)}else"return"===d.method&&d.abrupt("return",d.arg);e="executing";var k=c(a,b,d);if("normal"===k.type){if(e=d.done?"completed":"suspendedYield",k.arg===x)continue;return{value:k.arg,done:d.done}}"throw"===k.type&&(e="completed",d.method="throw",d.arg=k.arg)}}}function j(a,b){var d=b.method,e=a.iterator[d];if(void 0===e)return b.delegate=null,"throw"===d&&a.iterator.return&&(b.method="return",b.arg=void 0,j(a,b),"throw"===b.method)||"return"!==d&&(b.method="throw",b.arg=new TypeError("The iterator does not provide a '"+d+"' method")),x;var f=c(e,a.iterator,b.arg);if("throw"===f.type)return b.method="throw",b.arg=f.arg,b.delegate=null,x;var g=f.arg;return g?g.done?(b[a.resultName]=g.value,b.next=a.nextLoc,"return"!==b.method&&(b.method="next",b.arg=void 0),b.delegate=null,x):g:(b.method="throw",b.arg=new TypeError("iterator result is not an object"),b.delegate=null,x)}function k(a){var b={tryLoc:a[0]};1 in a&&(b.catchLoc=a[1]),2 in a&&(b.finallyLoc=a[2],b.afterLoc=a[3]),this.tryEntries.push(b)}function l(a){var b=a.completion||{};b.type="normal",delete b.arg,a.completion=b}function m(a){this.tryEntries=[{tryLoc:"root"}],a.forEach(k,this),this.reset(!0)}function n(a){if(a){var b=a[u];if(b)return b.call(a);if("function"==typeof a.next)return a;if(!isNaN(a.length)){var c=-1,d=function b(){for(;++c<a.length;)if(r.call(a,c))return b.value=a[c],b.done=!1,b;return b.value=void 0,b.done=!0,b};return d.next=d}}return{next:o}}function o(){return{value:void 0,done:!0}}_regeneratorRuntime=function(){return p};var p={},q=Object.prototype,r=q.hasOwnProperty,s=Object.defineProperty||function(a,b,c){a[b]=c.value},t="function"==typeof Symbol?Symbol:{},u=t.iterator||"@@iterator",v=t.asyncIterator||"@@asyncIterator",w=t.toStringTag||"@@toStringTag";try{a({},"")}catch(b){a=function(a,b,c){return a[b]=c}}p.wrap=b;var x={},y={};a(y,u,function(){return this});var z=Object.getPrototypeOf,A=z&&z(z(n([])));A&&A!==q&&r.call(A,u)&&(y=A);var B=f.prototype=d.prototype=Object.create(y);return e.prototype=f,s(B,"constructor",{value:f,configurable:!0}),s(f,"constructor",{value:e,configurable:!0}),e.displayName=a(f,w,"GeneratorFunction"),p.isGeneratorFunction=function(a){var b="function"==typeof a&&a.constructor;return!!b&&(b===e||"GeneratorFunction"===(b.displayName||b.name))},p.mark=function(b){return Object.setPrototypeOf?Object.setPrototypeOf(b,f):(b.__proto__=f,a(b,w,"GeneratorFunction")),b.prototype=Object.create(B),b},p.awrap=function(a){return{__await:a}},g(h.prototype),a(h.prototype,v,function(){return this}),p.AsyncIterator=h,p.async=function(a,c,d,e,f){void 0===f&&(f=Promise);var g=new h(b(a,c,d,e),f);return p.isGeneratorFunction(c)?g:g.next().then(function(a){return a.done?a.value:g.next()})},g(B),a(B,w,"Generator"),a(B,u,function(){return this}),a(B,"toString",function(){return"[object Generator]"}),p.keys=function(a){var b=Object(a),c=[];for(var d in b)c.push(d);return c.reverse(),function a(){for(;c.length;){var d=c.pop();if(d in b)return a.value=d,a.done=!1,a}return a.done=!0,a}},p.values=n,m.prototype={constructor:m,reset:function reset(a){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(l),!a)for(var b in this)"t"===b.charAt(0)&&r.call(this,b)&&!isNaN(+b.slice(1))&&(this[b]=void 0)},stop:function stop(){this.done=!0;var a=this.tryEntries[0].completion;if("throw"===a.type)throw a.arg;return this.rval},dispatchException:function dispatchException(a){function b(b,d){return f.type="throw",f.arg=a,c.next=b,d&&(c.method="next",c.arg=void 0),!!d}if(this.done)throw a;for(var c=this,d=this.tryEntries.length-1;0<=d;--d){var e=this.tryEntries[d],f=e.completion;if("root"===e.tryLoc)return b("end");if(e.tryLoc<=this.prev){var g=r.call(e,"catchLoc"),h=r.call(e,"finallyLoc");if(g&&h){if(this.prev<e.catchLoc)return b(e.catchLoc,!0);if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(!g){if(!h)throw new Error("try statement without catch or finally");if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(this.prev<e.catchLoc)return b(e.catchLoc,!0)}}},abrupt:function abrupt(a,b){for(var c,d=this.tryEntries.length-1;0<=d;--d)if(c=this.tryEntries[d],c.tryLoc<=this.prev&&r.call(c,"finallyLoc")&&this.prev<c.finallyLoc){var e=c;break}e&&("break"===a||"continue"===a)&&e.tryLoc<=b&&b<=e.finallyLoc&&(e=null);var f=e?e.completion:{};return f.type=a,f.arg=b,e?(this.method="next",this.next=e.finallyLoc,x):this.complete(f)},complete:function complete(a,b){if("throw"===a.type)throw a.arg;return"break"===a.type||"continue"===a.type?this.next=a.arg:"return"===a.type?(this.rval=this.arg=a.arg,this.method="return",this.next="end"):"normal"===a.type&&b&&(this.next=b),x},finish:function finish(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.finallyLoc===a)return this.complete(b.completion,b.afterLoc),l(b),x},catch:function _catch(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.tryLoc===a){var d=b.completion;if("throw"===d.type){var e=d.arg;l(b)}return e}throw new Error("illegal catch attempt")},delegateYield:function delegateYield(a,b,c){return this.delegate={iterator:n(a),resultName:b,nextLoc:c},"next"===this.method&&(this.arg=void 0),x}},p}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c.return||c.return()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,_toPropertyKey(c.key),c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"===_typeof(b)?b:b+""}function _toPrimitive(a,b){if("object"!==_typeof(a)||null===a)return a;var c=a[Symbol.toPrimitive];if(c!==void 0){var d=c.call(a,b||"default");if("object"!==_typeof(d))return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}var Utilities=/*#__PURE__*/function(){function a(){_classCallCheck(this,a)}return _createClass(a,null,[{key:"checkTargeting",value:function checkTargeting(a){var b=a.segment,c=a.visitorCode,d=a.targetingData,e=a.experimentId,f=a.variationConfiguration,g=a.campaignConfiguration,h=a.packageInfo;if(!b||!b.conditionsData.firstLevel.length)return(0,_tsRes.Ok)(!0);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));var i={},j=f.getAssignedVariations(c);j.ok&&(i=j.data);var k=g.getTargetingTree(b),l=k.evaluate({variationData:i,sdkInfo:h,targetingData:d,experimentId:e,visitorCode:c});return l}},{key:"validateVisitorCode",value:function validateVisitorCode(a){return 0===a.length?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitorCodeEmpty)):a.length>_constants2.VISITOR_CODE_MAX_LENGTH?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitorCodeMaxLength)):(0,_tsRes.Ok)()}},{key:"getDataUrl",value:function getDataUrl(a,b){var c=b.getUnsentData(a),d="";if(!c||!c.size)return d;var e,f=_createForOfIteratorHelper(c.values());try{for(f.s();!(e=f.n()).done;){var g=e.value;g.url&&(d+=g.url+"\n")}}catch(a){f.e(a)}finally{f.f()}return d}},{key:"trackRule",value:function(){function a(){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;return _regeneratorRuntime().wrap(function r(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.featureFlagVariation,d=a.campaignConfiguration,e=a.visitorCode,f=a.requester,g=a.isOfflineModeOn,h=a.offlineTrackingStorage,i=c.variationId,j=c.rule,k=this.getUserAgent(e,d),l=this.getDataUrl(e,d),m=i||0,n=null===i,o=j?j.experimentId:0,p={visitorCode:e,isUnallocated:n,body:l,experimentId:o,variationId:m,userAgent:k},b.next=10,f.trackExperiment(p);case 10:q=b.sent,q.ok&&d.clearUnsentData(e),!q.ok&&g&&this.addItemToOfflineTracking({item:p,storage:h,isExperiment:!0});case 13:case"end":return b.stop();}},b,this)}));return a}()},{key:"parseFeatureVariable",value:function parseFeatureVariable(a){var b=a.type,c=a.value;switch(b){case _types.VariableType.BOOLEAN:return(0,_tsRes.Ok)({type:b,value:!!c});case _types.VariableType.STRING:return(0,_tsRes.Ok)({type:b,value:c+""});case _types.VariableType.NUMBER:var d=+c;return Number.isNaN(d)?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.NumberParse,c+"")):(0,_tsRes.Ok)({type:b,value:d});case _types.VariableType.JSON:try{var e=JSON.parse(a.value+"");return(0,_tsRes.Ok)({type:b,value:e})}catch(a){return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.JSONParse,a))}default:(0,_kameleoonError.exhaustCheck)(b);}}},{key:"parseVisitorData",value:function parseVisitorData(a){var b,c=a.currentVisit,d=a.previousVisits;if(c?b=[c]:d&&(b=d),!b)return[];var e=new Map,f=[],g=_toConsumableArray(b[0].customDataEvents);g.sort(function(c,a){return a.time-c.time});var h,i=_createForOfIteratorHelper(g);try{for(i.s();!(h=i.n()).done;){var j=h.value,k=j.data.index;if(!e.has(k)){e.set(k,!0);var l=Object.keys(j.data.valuesCountMap);f.push(_construct(_kameleoonData.CustomData,[k].concat(l)))}}}catch(a){i.e(a)}finally{i.f()}return f}},{key:"parseRegExp",value:function parseRegExp(a){if("/"===a[0]){var b=a.split("/"),c=_slicedToArray(b,3),d=c[0],e=c[1],f=c[2];return new RegExp(e,f)}return new RegExp(a)}},{key:"getUserAgent",value:function getUserAgent(a,b){var c=b.storedTargetingData[a];if(c){var d=c[_kameleoonData.KameleoonData.UserAgent];if(d)return d.value}}},{key:"isCustomData",value:function isCustomData(a){return a.data.type===_kameleoonData.KameleoonData.CustomData}},{key:"updateCache",value:function updateCache(a){var b=a.cacheManager,c=a.visitorCode,d=a.experimentId,e=a.variationId,f=b.getAliveItem(c),g=f?_objectSpread({},f):{};g[d]={variationId:e,expirationTime:Date.now()+_constants.CACHE_ITEM_LIFETIME*_types.Milliseconds.Second},b.add({key:c,data:g,lifetime:_constants.CACHE_ITEM_LIFETIME})}},{key:"getTrackingCode",value:function getTrackingCode(a,b){var c="window.kameleoonQueue=window.kameleoonQueue||[];",d=a.getAliveItem(b);if(!d)return c;var e={};return Object.entries(d).forEach(function(a){var b=_slicedToArray(a,2),d=b[0],f=b[1],g=f.variationId,h=f.expirationTime;h>Date.now()&&(c+="window.kameleoonQueue.push(['Experiments.assignVariation',".concat(d,",").concat(g,"]);"),c+="window.kameleoonQueue.push(['Experiments.trigger',".concat(d,",true]);"),e[+d]={variationId:g,expirationTime:h})}),a.add({key:b,data:e,lifetime:_constants.CACHE_ITEM_LIFETIME}),c}},{key:"trackData",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l;return _regeneratorRuntime().wrap(function m(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.visitorCode,d=a.campaignConfiguration,e=a.requester,f=a.isOfflineModeOn,g=a.offlineTrackingStorage,b.next=3,this.sendOfflineTracking({requester:e,storage:g});case 3:return h=this.getDataUrl(c,d),i=this.getUserAgent(c,d),j={visitorCode:c,body:h,userAgent:i},b.next=8,e.trackData(j);case 8:k=b.sent,l=function cleanup(){return d.clearUnsentData(c)},k.ok&&l(),!k.ok&&f&&this.addItemToOfflineTracking({item:j,storage:g,isExperiment:!1,onSuccess:l});case 12:case"end":return b.stop();}},b,this)}));return a}()},{key:"trackExperiment",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l,m,n;return _regeneratorRuntime().wrap(function o(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.variationId,d=a.experimentId,e=a.isUnallocated,f=a.visitorCode,g=a.campaignConfiguration,h=a.requester,i=a.isOfflineModeOn,j=a.offlineTrackingStorage,k=this.getDataUrl(f,g),l=this.getUserAgent(f,g),m={body:k,visitorCode:f,isUnallocated:e,variationId:c,userAgent:l,experimentId:d},b.next=6,h.trackExperiment(m);case 6:n=b.sent,!n.ok&&i&&this.addItemToOfflineTracking({item:m,storage:j,isExperiment:!0});case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"getNonce",value:function getNonce(){for(var a,b=_constants2.HEX_CHARACTERS.length,c="",d=0;d<b;d++)a=Math.floor(Math.random()*b),c+=_constants2.HEX_CHARACTERS.substring(a,a+1);return c}},{key:"sendOfflineTracking",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h;return _regeneratorRuntime().wrap(function i(b){for(;1;)switch(b.prev=b.next){case 0:if(c=a.requester,d=a.storage,e=d.read(),!(e.ok&&e.data.requestParameters)){b.next=9;break}return f=e.data.requestParameters,g=[],h=f.map(/*#__PURE__*/function(){var a=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var d,e,f,h;return _regeneratorRuntime().wrap(function i(b){for(;1;)switch(b.prev=b.next){case 0:if(d=a.timestamp,e=a.isExperiment,f=_objectWithoutProperties(a,_excluded),!e){b.next=7;break}return b.next=4,c.trackExperiment(f);case 4:h=b.sent,b.next=10;break;case 7:return b.next=9,c.trackData(f);case 9:h=b.sent;case 10:h.ok&&h.data&&g.push(d);case 11:case"end":return b.stop();}},b)}));return function(b){return a.apply(this,arguments)}}()),b.next=8,Promise.allSettled(h);case 8:this.deleteFromOfflineTracking({storage:d,timestamps:g});case 9:case"end":return b.stop();}},b,this)}));return a}()},{key:"addItemToOfflineTracking",value:function addItemToOfflineTracking(a){var b=a.item,c=a.isExperiment,d=a.storage,e=a.onSuccess,f=d.read(),g=f.ok?f.data:{},h=g.requestParameters,i=_objectSpread(_objectSpread({},b),{},{timestamp:new Date().getTime(),isExperiment:c}),j=h?[].concat(_toConsumableArray(h),[i]):[i],k=d.write({requestParameters:j});k.ok&&e&&e()}},{key:"deleteFromOfflineTracking",value:function deleteFromOfflineTracking(a){var b=a.timestamps,c=a.storage,d=c.read();if(d.ok){var e=d.data.requestParameters,f=null===e||void 0===e?void 0:e.filter(function(a){return!b.includes(a.timestamp)});c.write({requestParameters:f})}}}]),a}();exports.Utilities=Utilities;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Utilities=void 0;var _tsRes=require("ts-res"),_kameleoonError=require("../kameleoonError"),_types=require("../types"),_kameleoonData=require("../kameleoonData"),_constants=require("../constants"),_constants2=require("./constants"),_excluded=["timestamp","isExperiment"];function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _objectWithoutProperties(a,b){if(null==a)return{};var c,d,e=_objectWithoutPropertiesLoose(a,b);if(Object.getOwnPropertySymbols){var f=Object.getOwnPropertySymbols(a);for(d=0;d<f.length;d++)c=f[d],0<=b.indexOf(c)||Object.prototype.propertyIsEnumerable.call(a,c)&&(e[c]=a[c])}return e}function _objectWithoutPropertiesLoose(a,b){if(null==a)return{};var c,d,e={},f=Object.keys(a);for(d=0;d<f.length;d++)c=f[d],0<=b.indexOf(c)||(e[c]=a[c]);return e}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return b=_toPropertyKey(b),b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f,g,h=[],i=!0,j=!1;try{if(f=(c=c.call(a)).next,0===b){if(Object(c)!==c)return;i=!1}else for(;!(i=(d=f.call(c)).done)&&(h.push(d.value),h.length!==b);i=!0);}catch(a){j=!0,e=a}finally{try{if(!i&&null!=c.return&&(g=c.return(),Object(g)!==g))return}finally{if(j)throw e}}return h}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _construct(){return _construct=_isNativeReflectConstruct()?Reflect.construct.bind():function(b,c,d){var e=[null];e.push.apply(e,c);var a=Function.bind.apply(b,e),f=new a;return d&&_setPrototypeOf(f,d.prototype),f},_construct.apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(a){return!1}}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */function a(a,b,c){return Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}),a[b]}function b(a,b,c,e){var f=b&&b.prototype instanceof d?b:d,g=Object.create(f.prototype),h=new m(e||[]);return s(g,"_invoke",{value:i(a,c,h)}),g}function c(a,b,c){try{return{type:"normal",arg:a.call(b,c)}}catch(a){return{type:"throw",arg:a}}}function d(){}function e(){}function f(){}function g(b){["next","throw","return"].forEach(function(c){a(b,c,function(a){return this._invoke(c,a)})})}function h(a,b){function d(e,f,g,h){var i=c(a[e],a,f);if("throw"!==i.type){var j=i.arg,k=j.value;return k&&"object"==_typeof(k)&&r.call(k,"__await")?b.resolve(k.__await).then(function(a){d("next",a,g,h)},function(a){d("throw",a,g,h)}):b.resolve(k).then(function(a){j.value=a,g(j)},function(a){return d("throw",a,g,h)})}h(i.arg)}var e;s(this,"_invoke",{value:function value(a,c){function f(){return new b(function(b,e){d(a,c,b,e)})}return e=e?e.then(f,f):f()}})}function i(a,b,d){var e="suspendedStart";return function(f,g){if("executing"==e)throw new Error("Generator is already running");if("completed"==e){if("throw"===f)throw g;return o()}for(d.method=f,d.arg=g;;){var h=d.delegate;if(h){var i=j(h,d);if(i){if(i===x)continue;return i}}if("next"===d.method)d.sent=d._sent=d.arg;else if("throw"===d.method){if("suspendedStart"==e)throw e="completed",d.arg;d.dispatchException(d.arg)}else"return"===d.method&&d.abrupt("return",d.arg);e="executing";var k=c(a,b,d);if("normal"===k.type){if(e=d.done?"completed":"suspendedYield",k.arg===x)continue;return{value:k.arg,done:d.done}}"throw"===k.type&&(e="completed",d.method="throw",d.arg=k.arg)}}}function j(a,b){var d=b.method,e=a.iterator[d];if(void 0===e)return b.delegate=null,"throw"===d&&a.iterator.return&&(b.method="return",b.arg=void 0,j(a,b),"throw"===b.method)||"return"!==d&&(b.method="throw",b.arg=new TypeError("The iterator does not provide a '"+d+"' method")),x;var f=c(e,a.iterator,b.arg);if("throw"===f.type)return b.method="throw",b.arg=f.arg,b.delegate=null,x;var g=f.arg;return g?g.done?(b[a.resultName]=g.value,b.next=a.nextLoc,"return"!==b.method&&(b.method="next",b.arg=void 0),b.delegate=null,x):g:(b.method="throw",b.arg=new TypeError("iterator result is not an object"),b.delegate=null,x)}function k(a){var b={tryLoc:a[0]};1 in a&&(b.catchLoc=a[1]),2 in a&&(b.finallyLoc=a[2],b.afterLoc=a[3]),this.tryEntries.push(b)}function l(a){var b=a.completion||{};b.type="normal",delete b.arg,a.completion=b}function m(a){this.tryEntries=[{tryLoc:"root"}],a.forEach(k,this),this.reset(!0)}function n(a){if(a){var b=a[u];if(b)return b.call(a);if("function"==typeof a.next)return a;if(!isNaN(a.length)){var c=-1,d=function b(){for(;++c<a.length;)if(r.call(a,c))return b.value=a[c],b.done=!1,b;return b.value=void 0,b.done=!0,b};return d.next=d}}return{next:o}}function o(){return{value:void 0,done:!0}}_regeneratorRuntime=function(){return p};var p={},q=Object.prototype,r=q.hasOwnProperty,s=Object.defineProperty||function(a,b,c){a[b]=c.value},t="function"==typeof Symbol?Symbol:{},u=t.iterator||"@@iterator",v=t.asyncIterator||"@@asyncIterator",w=t.toStringTag||"@@toStringTag";try{a({},"")}catch(b){a=function(a,b,c){return a[b]=c}}p.wrap=b;var x={},y={};a(y,u,function(){return this});var z=Object.getPrototypeOf,A=z&&z(z(n([])));A&&A!==q&&r.call(A,u)&&(y=A);var B=f.prototype=d.prototype=Object.create(y);return e.prototype=f,s(B,"constructor",{value:f,configurable:!0}),s(f,"constructor",{value:e,configurable:!0}),e.displayName=a(f,w,"GeneratorFunction"),p.isGeneratorFunction=function(a){var b="function"==typeof a&&a.constructor;return!!b&&(b===e||"GeneratorFunction"===(b.displayName||b.name))},p.mark=function(b){return Object.setPrototypeOf?Object.setPrototypeOf(b,f):(b.__proto__=f,a(b,w,"GeneratorFunction")),b.prototype=Object.create(B),b},p.awrap=function(a){return{__await:a}},g(h.prototype),a(h.prototype,v,function(){return this}),p.AsyncIterator=h,p.async=function(a,c,d,e,f){void 0===f&&(f=Promise);var g=new h(b(a,c,d,e),f);return p.isGeneratorFunction(c)?g:g.next().then(function(a){return a.done?a.value:g.next()})},g(B),a(B,w,"Generator"),a(B,u,function(){return this}),a(B,"toString",function(){return"[object Generator]"}),p.keys=function(a){var b=Object(a),c=[];for(var d in b)c.push(d);return c.reverse(),function a(){for(;c.length;){var d=c.pop();if(d in b)return a.value=d,a.done=!1,a}return a.done=!0,a}},p.values=n,m.prototype={constructor:m,reset:function reset(a){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(l),!a)for(var b in this)"t"===b.charAt(0)&&r.call(this,b)&&!isNaN(+b.slice(1))&&(this[b]=void 0)},stop:function stop(){this.done=!0;var a=this.tryEntries[0].completion;if("throw"===a.type)throw a.arg;return this.rval},dispatchException:function dispatchException(a){function b(b,d){return f.type="throw",f.arg=a,c.next=b,d&&(c.method="next",c.arg=void 0),!!d}if(this.done)throw a;for(var c=this,d=this.tryEntries.length-1;0<=d;--d){var e=this.tryEntries[d],f=e.completion;if("root"===e.tryLoc)return b("end");if(e.tryLoc<=this.prev){var g=r.call(e,"catchLoc"),h=r.call(e,"finallyLoc");if(g&&h){if(this.prev<e.catchLoc)return b(e.catchLoc,!0);if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(!g){if(!h)throw new Error("try statement without catch or finally");if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(this.prev<e.catchLoc)return b(e.catchLoc,!0)}}},abrupt:function abrupt(a,b){for(var c,d=this.tryEntries.length-1;0<=d;--d)if(c=this.tryEntries[d],c.tryLoc<=this.prev&&r.call(c,"finallyLoc")&&this.prev<c.finallyLoc){var e=c;break}e&&("break"===a||"continue"===a)&&e.tryLoc<=b&&b<=e.finallyLoc&&(e=null);var f=e?e.completion:{};return f.type=a,f.arg=b,e?(this.method="next",this.next=e.finallyLoc,x):this.complete(f)},complete:function complete(a,b){if("throw"===a.type)throw a.arg;return"break"===a.type||"continue"===a.type?this.next=a.arg:"return"===a.type?(this.rval=this.arg=a.arg,this.method="return",this.next="end"):"normal"===a.type&&b&&(this.next=b),x},finish:function finish(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.finallyLoc===a)return this.complete(b.completion,b.afterLoc),l(b),x},catch:function _catch(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.tryLoc===a){var d=b.completion;if("throw"===d.type){var e=d.arg;l(b)}return e}throw new Error("illegal catch attempt")},delegateYield:function delegateYield(a,b,c){return this.delegate={iterator:n(a),resultName:b,nextLoc:c},"next"===this.method&&(this.arg=void 0),x}},p}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c.return||c.return()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,_toPropertyKey(c.key),c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"===_typeof(b)?b:b+""}function _toPrimitive(a,b){if("object"!==_typeof(a)||null===a)return a;var c=a[Symbol.toPrimitive];if(c!==void 0){var d=c.call(a,b||"default");if("object"!==_typeof(d))return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}var Utilities=/*#__PURE__*/function(){function a(){_classCallCheck(this,a)}return _createClass(a,null,[{key:"checkTargeting",value:function checkTargeting(a){var b=a.segment,c=a.visitorCode,d=a.targetingData,e=a.experimentId,f=a.variationConfiguration,g=a.campaignConfiguration,h=a.packageInfo;if(!b||!b.conditionsData.firstLevel.length)return(0,_tsRes.Ok)(!0);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));var i={},j=f.getAssignedVariations(c);j.ok&&(i=j.data);var k=g.getTargetingTree(b),l=k.evaluate({variationData:i,sdkInfo:h,targetingData:d,experimentId:e,visitorCode:c});return l}},{key:"validateVisitorCode",value:function validateVisitorCode(a){return 0===a.length?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitorCodeEmpty)):a.length>_constants2.VISITOR_CODE_MAX_LENGTH?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitorCodeMaxLength)):(0,_tsRes.Ok)()}},{key:"getDataUrl",value:function getDataUrl(a,b){var c=b.getUnsentData(a),d="";if(!c||!c.size)return d;var e,f=_createForOfIteratorHelper(c.values());try{for(f.s();!(e=f.n()).done;){var g=e.value;g.url&&(d+=g.url+"\n")}}catch(a){f.e(a)}finally{f.f()}return d}},{key:"trackRule",value:function(){function a(){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;return _regeneratorRuntime().wrap(function r(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.featureFlagVariation,d=a.campaignConfiguration,e=a.visitorCode,f=a.requester,g=a.isOfflineModeOn,h=a.offlineTrackingStorage,i=c.variationId,j=c.rule,k=this.getUserAgent(e,d),l=this.getDataUrl(e,d),m=i||0,n=null===i,o=j?j.experimentId:0,p={visitorCode:e,isUnallocated:n,body:l,experimentId:o,variationId:m,userAgent:k},b.next=10,f.trackExperiment(p);case 10:q=b.sent,q.ok&&d.clearUnsentData(e),!q.ok&&g&&this.addItemToOfflineTracking({item:p,storage:h,isExperiment:!0});case 13:case"end":return b.stop();}},b,this)}));return a}()},{key:"parseFeatureVariable",value:function parseFeatureVariable(a){var b=a.type,c=a.value;switch(b){case _types.VariableType.BOOLEAN:return(0,_tsRes.Ok)({type:b,value:!!c});case _types.VariableType.STRING:return(0,_tsRes.Ok)({type:b,value:c+""});case _types.VariableType.NUMBER:var d=+c;return Number.isNaN(d)?(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.NumberParse,c+"")):(0,_tsRes.Ok)({type:b,value:d});case _types.VariableType.JSON:try{var e=JSON.parse(a.value+"");return(0,_tsRes.Ok)({type:b,value:e})}catch(a){return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.JSONParse,a))}default:(0,_kameleoonError.exhaustCheck)(b);}}},{key:"parseVisitorData",value:function parseVisitorData(a){var b,c=a.currentVisit,d=a.previousVisits;if(c?b=[c]:d&&(b=d),!b)return[];var e=new Map,f=[],g=_toConsumableArray(b[0].customDataEvents);g.sort(function(c,a){return a.time-c.time});var h,i=_createForOfIteratorHelper(g);try{for(i.s();!(h=i.n()).done;){var j=h.value,k=j.data.index;if(!e.has(k)){e.set(k,!0);var l=Object.keys(j.data.valuesCountMap);f.push(_construct(_kameleoonData.CustomData,[k].concat(l)))}}}catch(a){i.e(a)}finally{i.f()}return f}},{key:"parseRegExp",value:function parseRegExp(a){if("/"===a[0]){var b=a.split("/"),c=_slicedToArray(b,3),d=c[0],e=c[1],f=c[2];return new RegExp(e,f)}return new RegExp(a)}},{key:"getUserAgent",value:function getUserAgent(a,b){var c=b.storedTargetingData[a];if(c){var d=c[_kameleoonData.KameleoonData.UserAgent];if(d)return d.value}}},{key:"isCustomData",value:function isCustomData(a){return a.data.type===_kameleoonData.KameleoonData.CustomData}},{key:"updateCache",value:function updateCache(a){var b=a.cacheManager,c=a.visitorCode,d=a.experimentId,e=a.variationId,f=b.getAliveItem(c),g=f?_objectSpread({},f):{};g[d]={variationId:e,expirationTime:Date.now()+_constants.CACHE_ITEM_LIFETIME*_types.Milliseconds.Second},b.add({key:c,data:g,lifetime:_constants.CACHE_ITEM_LIFETIME})}},{key:"getTrackingCode",value:function getTrackingCode(a,b){var c="window.kameleoonQueue=window.kameleoonQueue||[];",d=a.getAliveItem(b);if(!d)return c;var e={};return Object.entries(d).forEach(function(a){var b=_slicedToArray(a,2),d=b[0],f=b[1],g=f.variationId,h=f.expirationTime;h>Date.now()&&(c+="window.kameleoonQueue.push(['Experiments.assignVariation',".concat(d,",").concat(g,"]);"),c+="window.kameleoonQueue.push(['Experiments.trigger',".concat(d,",true]);"),e[+d]={variationId:g,expirationTime:h})}),a.add({key:b,data:e,lifetime:_constants.CACHE_ITEM_LIFETIME}),c}},{key:"trackData",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l;return _regeneratorRuntime().wrap(function m(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.visitorCode,d=a.campaignConfiguration,e=a.requester,f=a.isOfflineModeOn,g=a.offlineTrackingStorage,b.next=3,this.sendOfflineTracking({requester:e,storage:g});case 3:return h=this.getDataUrl(c,d),i=this.getUserAgent(c,d),j={visitorCode:c,body:h,userAgent:i},b.next=8,e.trackData(j);case 8:k=b.sent,l=function cleanup(){return d.clearUnsentData(c)},k.ok&&l(),!k.ok&&f&&this.addItemToOfflineTracking({item:j,storage:g,isExperiment:!1,onSuccess:l});case 12:case"end":return b.stop();}},b,this)}));return a}()},{key:"trackExperiment",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l,m,n;return _regeneratorRuntime().wrap(function o(b){for(;1;)switch(b.prev=b.next){case 0:return c=a.variationId,d=a.experimentId,e=a.isUnallocated,f=a.visitorCode,g=a.campaignConfiguration,h=a.requester,i=a.isOfflineModeOn,j=a.offlineTrackingStorage,k=this.getDataUrl(f,g),l=this.getUserAgent(f,g),m={body:k,visitorCode:f,isUnallocated:e,variationId:c,userAgent:l,experimentId:d},b.next=6,h.trackExperiment(m);case 6:n=b.sent,!n.ok&&i&&this.addItemToOfflineTracking({item:m,storage:j,isExperiment:!0});case 8:case"end":return b.stop();}},b,this)}));return a}()},{key:"getNonce",value:function getNonce(){for(var a,b=_constants2.HEX_CHARACTERS.length,c="",d=0;d<b;d++)a=Math.floor(Math.random()*b),c+=_constants2.HEX_CHARACTERS.substring(a,a+1);return c}},{key:"sendOfflineTracking",value:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c,d,e,f,g,h,i,j,k,l,m,n;return _regeneratorRuntime().wrap(function o(b){for(;1;)switch(b.prev=b.next){case 0:if(c=a.requester,d=a.storage,e=d.read(),!(e.ok&&e.data.requestParameters)){b.next=33;break}f=e.data.requestParameters,g=[],h=_createForOfIteratorHelper(f),b.prev=6,h.s();case 8:if((i=h.n()).done){b.next=24;break}if(j=i.value,k=j.timestamp,l=j.isExperiment,m=_objectWithoutProperties(j,_excluded),n=void 0,!l){b.next=18;break}return b.next=15,c.trackExperiment(m);case 15:n=b.sent,b.next=21;break;case 18:return b.next=20,c.trackData(m);case 20:n=b.sent;case 21:n.ok&&n.data&&g.push(k);case 22:b.next=8;break;case 24:b.next=29;break;case 26:b.prev=26,b.t0=b["catch"](6),h.e(b.t0);case 29:return b.prev=29,h.f(),b.finish(29);case 32:this.deleteFromOfflineTracking({storage:d,timestamps:g});case 33:case"end":return b.stop();}},b,this,[[6,26,29,32]])}));return a}()},{key:"addItemToOfflineTracking",value:function addItemToOfflineTracking(a){var b=a.item,c=a.isExperiment,d=a.storage,e=a.onSuccess,f=d.read(),g=f.ok?f.data:{},h=g.requestParameters,i=_objectSpread(_objectSpread({},b),{},{timestamp:new Date().getTime(),isExperiment:c}),j=h?[].concat(_toConsumableArray(h),[i]):[i],k=d.write({requestParameters:j});k.ok&&e&&e()}},{key:"deleteFromOfflineTracking",value:function deleteFromOfflineTracking(a){var b=a.timestamps,c=a.storage,d=c.read();if(d.ok){var e=d.data.requestParameters,f=null===e||void 0===e?void 0:e.filter(function(a){return!b.includes(a.timestamp)});c.write({requestParameters:f})}}}]),a}();exports.Utilities=Utilities;
//# sourceMappingURL=utilities.js.map
{
"name": "@kameleoon/javascript-sdk-core",
"version": "2.7.0",
"version": "2.7.1",
"description": "Kameleoon JS SDK Core",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc