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
0
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 5.1.0 to 5.1.1

9

CHANGELOG.md
# Change Log
## 5.1.1
### Patch Changes
- Fixed the issue where Real-Time Update server failures could lead to SDK configuration update issues. If the Real-Time Update server fails SDK will now temporarily switch to polling mode until the Real-Time Update server is available again.
- Added an optional `onError` handler for [`IExternalEventSource`][iextrnaleventsource] interface. If the `onError` handler is not provided the SDK would not be able to handle Real-Time Update server failures. Note: no action is required when using default Kameleoon event source solutions.
[iextrnaleventsource]: https://developers.kameleoon.com/feature-management-and-experimentation/web-sdks/js-sdk/#eventsource
## 5.1.0 (2024-11-14)

@@ -4,0 +13,0 @@

2

dist/clientConfiguration/clientConfiguration.js

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ClientConfiguration=void 0;var _tsRes=require("ts-res"),_types=require("./types"),_eventManager=require("../eventManager"),_constants=require("./constants"),_logging=require("../logging");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))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||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){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)}class ClientConfiguration{constructor(a){let{updateInterval:b,urlProvider:c,storage:d,requester:e,dataManager:f,eventSource:g,externalVisitorCodeManager:h,eventManager:i}=a;_defineProperty(this,"updateConfigurationIntervalId",null),_defineProperty(this,"updateType",_types.UpdateType.Polling),_defineProperty(this,"configurationData",_constants.DEFAULT_CLIENT_CONFIGURATION),_defineProperty(this,"featureFlagsData",new Map),_defineProperty(this,"isTargetedDeliveryRule",null),_defineProperty(this,"segmentsData",null),_defineProperty(this,"updateInterval",void 0),_defineProperty(this,"lastUpdate",void 0),_defineProperty(this,"storage",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"eventSource",void 0),_defineProperty(this,"mappedRules",null),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"urlProvider",void 0),this.urlProvider=c,this.requester=e,this.updateInterval=b,this.lastUpdate=new Date,this.storage=d,this.dataManager=f,this.eventManager=i,this.visitorCodeManager=h,this.eventSource=g}async initialize(){_logging.KameleoonLogger.debug("CALL: ClientConfiguration.initialize()"),this.readStorageData();const a=this.checkShouldUpdate();if(a){const a=await this.updateClientConfiguration();if(!a.ok)return _logging.KameleoonLogger.error("Error occurred during client configuration update"),a;const b=a.data;if(b)return _logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)()}return this.updateType!==_types.UpdateType.RealTime||this.eventSource.isOpen?(this.updateType===_types.UpdateType.Polling&&this.handlePollingUpdates(),_logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)()):(this.updateType=_types.UpdateType.Polling,_logging.KameleoonLogger.info("Configuration update type was toggled to Polling"),this.handleRealTimeUpdates(),_logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)())}cleanupHandlers(){this.eventSource.isOpen&&this.eventSource.close(),this.updateConfigurationIntervalId&&clearInterval(this.updateConfigurationIntervalId)}handleRealTimeUpdates(){this.cleanupHandlers();const a=a=>{this.updateClientConfiguration(a).then(b=>{b.ok&&this.eventManager.fireEvent(_eventManager.EventType.ConfigurationUpdate,{timestamp:a})})};this.eventSource.open(a)}handlePollingUpdates(){this.cleanupHandlers();try{this.updateConfigurationIntervalId=setInterval(this.updateClientConfiguration.bind(this),this.updateInterval)}catch(a){throw this.updateConfigurationIntervalId&&clearInterval(this.updateConfigurationIntervalId),a}}get featureFlags(){return this.featureFlagsData}get configuration(){return this.configurationData.configuration}get segments(){if(this.segmentsData)return this.segmentsData;const a=new Map;return this.featureFlagsData.forEach(b=>{b.rules.forEach(b=>{b.segment&&!a.has(b.segment.id)&&a.set(b.segment.id,b.segment)})}),this.segmentsData=a,a}get ruleMap(){var a;if(null!==(a=this.mappedRules)&&void 0!==a&&a.size)return this.mappedRules;const b=new Map;for(const a of this.featureFlags.values())for(const c of a.rules){const{id:d,experimentId:e,variationByExposition:f}=c,g=new Map;f.forEach(a=>{let{variationKey:b,variationId:c}=a;"number"==typeof c&&g.set(b,c)}),b.set(d,{featureId:a.id,experimentId:e,variations:g})}return this.mappedRules=b,b}get isConsentRequired(){return this.configuration.consentType===_types.ConsentType.Required}get hasAnyTargetedDeliveryRule(){if(null!==this.isTargetedDeliveryRule)return this.isTargetedDeliveryRule;for(const a of this.featureFlags.values())if(a.environmentEnabled&&a.rules.some(a=>a.type===_types.RuleType.TARGETED_DELIVERY))return!0;return!1}checkShouldUpdate(){const{configuration:a}=this.configurationData;if(this.updateType=a.realTimeUpdate?_types.UpdateType.RealTime:_types.UpdateType.Polling,!this.featureFlags.size)return!0;const b=this.lastUpdate.getTime(),c=Date.now()-this.updateInterval;return b<c}readStorageData(){_logging.KameleoonLogger.debug("CALL: ClientConfiguration.readStorageData()");const a=this.storage.read();if(a.ok){const{data:b,lastUpdate:c}=a.data;this.urlProvider.dataApiDomain=b.configuration.dataApiDomain;const{featureFlags:d,customData:e}=b;null!==e&&void 0!==e&&e.length&&(this.dataManager.customDataIndexes=e);for(const a of d)this.featureFlagsData.set(a.featureKey,a);this.configurationData=b,this.lastUpdate=new Date(Date.parse(c))}_logging.KameleoonLogger.debug`RETURN: ClientConfiguration.readStorageData() -> (result: ${a.ok})`}async updateClientConfiguration(a){var b;const c=await this.requester.getClientConfiguration(a);if(!c.ok)return this.featureFlags.size?(_logging.KameleoonLogger.error("Error occurred while updating configuration. Existing configuration will be used as a fallback."),(0,_tsRes.Ok)(!0)):c;const{configuration:d,customData:e,featureFlags:f}=c.data;this.configurationData={configuration:d,customData:e};for(const b of f)this.featureFlagsData.set(b.featureKey,b);null!==e&&void 0!==e&&e.length&&(this.dataManager.customDataIndexes=e),this.dataManager.clearTrees(),null===(b=this.mappedRules)||void 0===b?void 0:b.clear(),this.isTargetedDeliveryRule=null,this.segmentsData=null;const g=this.updateStorageData();if(!g.ok)return g;const h=d.realTimeUpdate?_types.UpdateType.RealTime:_types.UpdateType.Polling,i=h!==this.updateType;i&&(this.updateType=h,_logging.KameleoonLogger.debug`Configuration update type was toggled to ${h}`,await this.initialize());const j=this.isConsentRequired&&!this.hasAnyTargetedDeliveryRule;return this.visitorCodeManager.consentRequired=j,(0,_tsRes.Ok)(i)}updateStorageData(){this.lastUpdate=new Date;const a=_objectSpread(_objectSpread({},this.configurationData),{},{featureFlags:Array.from(this.featureFlags.values())}),b={data:a,lastUpdate:this.lastUpdate.toString()};return this.urlProvider.dataApiDomain=this.configurationData.configuration.dataApiDomain,this.storage.write(b)}}exports.ClientConfiguration=ClientConfiguration;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ClientConfiguration=void 0;var _tsRes=require("ts-res"),_types=require("./types"),_eventManager=require("../eventManager"),_constants=require("./constants"),_logging=require("../logging");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))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||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){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)}class ClientConfiguration{constructor(a){let{updateInterval:b,urlProvider:c,storage:d,requester:e,dataManager:f,eventSource:g,externalVisitorCodeManager:h,eventManager:i}=a;_defineProperty(this,"updateConfigurationIntervalId",null),_defineProperty(this,"updateType",_types.UpdateType.Polling),_defineProperty(this,"configurationData",_constants.DEFAULT_CLIENT_CONFIGURATION),_defineProperty(this,"featureFlagsData",new Map),_defineProperty(this,"isTargetedDeliveryRule",null),_defineProperty(this,"segmentsData",null),_defineProperty(this,"updateInterval",void 0),_defineProperty(this,"lastUpdate",void 0),_defineProperty(this,"storage",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"eventSource",void 0),_defineProperty(this,"mappedRules",null),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"urlProvider",void 0),this.urlProvider=c,this.requester=e,this.updateInterval=b,this.lastUpdate=new Date,this.storage=d,this.dataManager=f,this.eventManager=i,this.visitorCodeManager=h,this.eventSource=g}async initialize(){_logging.KameleoonLogger.debug("CALL: ClientConfiguration.initialize()"),this.readStorageData();const a=this.checkShouldUpdate();if(a){const a=await this.updateClientConfiguration();if(!a.ok)return _logging.KameleoonLogger.error("Error occurred during client configuration update"),a;const b=a.data;if(b)return _logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)()}return this.updateType!==_types.UpdateType.RealTime||this.eventSource.isOpen?(this.updateType===_types.UpdateType.Polling&&this.handlePollingUpdates(),_logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)()):(_logging.KameleoonLogger.info("Configuration update type was toggled to Polling"),this.handleRealTimeUpdates(),_logging.KameleoonLogger.debug("RETURN: ClientConfiguration.initialize()"),(0,_tsRes.Ok)())}cleanupHandlers(){this.eventSource.isOpen&&this.eventSource.close(),this.updateConfigurationIntervalId&&clearInterval(this.updateConfigurationIntervalId)}handleRealTimeUpdates(){this.cleanupHandlers();const a=a=>{this.updateClientConfiguration(a).then(b=>{b.ok&&this.eventManager.fireEvent(_eventManager.EventType.ConfigurationUpdate,{timestamp:a})})};this.eventSource.open(a),this.eventSource.onError(a=>{_logging.KameleoonLogger.error`Error occurred during real-time update: ${a}. Switching to polling updates while the connection is not restored.`,this.updateType=_types.UpdateType.Polling,this.handlePollingUpdates()})}handlePollingUpdates(){this.cleanupHandlers();try{this.updateConfigurationIntervalId=setInterval(this.updateClientConfiguration.bind(this),this.updateInterval)}catch(a){throw this.updateConfigurationIntervalId&&clearInterval(this.updateConfigurationIntervalId),a}}get featureFlags(){return this.featureFlagsData}get configuration(){return this.configurationData.configuration}get segments(){if(this.segmentsData)return this.segmentsData;const a=new Map;return this.featureFlagsData.forEach(b=>{b.rules.forEach(b=>{b.segment&&!a.has(b.segment.id)&&a.set(b.segment.id,b.segment)})}),this.segmentsData=a,a}get ruleMap(){var a;if(null!==(a=this.mappedRules)&&void 0!==a&&a.size)return this.mappedRules;const b=new Map;for(const a of this.featureFlags.values())for(const c of a.rules){const{id:d,experimentId:e,variationByExposition:f}=c,g=new Map;f.forEach(a=>{let{variationKey:b,variationId:c}=a;"number"==typeof c&&g.set(b,c)}),b.set(d,{featureId:a.id,experimentId:e,variations:g})}return this.mappedRules=b,b}get isConsentRequired(){return this.configuration.consentType===_types.ConsentType.Required}get hasAnyTargetedDeliveryRule(){if(null!==this.isTargetedDeliveryRule)return this.isTargetedDeliveryRule;for(const a of this.featureFlags.values())if(a.environmentEnabled&&a.rules.some(a=>a.type===_types.RuleType.TARGETED_DELIVERY))return!0;return!1}checkShouldUpdate(){const{configuration:a}=this.configurationData;if(this.updateType=a.realTimeUpdate?_types.UpdateType.RealTime:_types.UpdateType.Polling,!this.featureFlags.size)return!0;const b=this.lastUpdate.getTime(),c=Date.now()-this.updateInterval;return b<c}readStorageData(){_logging.KameleoonLogger.debug("CALL: ClientConfiguration.readStorageData()");const a=this.storage.read();if(a.ok){const{data:b,lastUpdate:c}=a.data;this.urlProvider.dataApiDomain=b.configuration.dataApiDomain;const{featureFlags:d,customData:e}=b;null!==e&&void 0!==e&&e.length&&(this.dataManager.customDataIndexes=e);for(const a of d)this.featureFlagsData.set(a.featureKey,a);this.configurationData=b,this.lastUpdate=new Date(Date.parse(c))}_logging.KameleoonLogger.debug`RETURN: ClientConfiguration.readStorageData() -> (result: ${a.ok})`}async updateClientConfiguration(a){var b;const c=await this.requester.getClientConfiguration(a);if(!c.ok)return this.featureFlags.size?(_logging.KameleoonLogger.error("Error occurred while updating configuration. Existing configuration will be used as a fallback."),(0,_tsRes.Ok)(!0)):c;const{configuration:d,customData:e,featureFlags:f}=c.data;this.configurationData={configuration:d,customData:e};for(const b of f)this.featureFlagsData.set(b.featureKey,b);null!==e&&void 0!==e&&e.length&&(this.dataManager.customDataIndexes=e),this.dataManager.clearTrees(),null===(b=this.mappedRules)||void 0===b?void 0:b.clear(),this.isTargetedDeliveryRule=null,this.segmentsData=null;const g=this.updateStorageData();if(!g.ok)return g;const h=d.realTimeUpdate?_types.UpdateType.RealTime:_types.UpdateType.Polling,i=h!==this.updateType;i&&(this.updateType=h,_logging.KameleoonLogger.debug`Configuration update type was toggled to ${h}`,await this.initialize());const j=this.isConsentRequired&&!this.hasAnyTargetedDeliveryRule;return this.visitorCodeManager.consentRequired=j,(0,_tsRes.Ok)(i)}updateStorageData(){this.lastUpdate=new Date;const a=_objectSpread(_objectSpread({},this.configurationData),{},{featureFlags:Array.from(this.featureFlags.values())}),b={data:a,lastUpdate:this.lastUpdate.toString()};return this.urlProvider.dataApiDomain=this.configurationData.configuration.dataApiDomain,this.storage.write(b)}}exports.ClientConfiguration=ClientConfiguration;

@@ -6,2 +6,3 @@ import { EventHandlerType, EventSourceParametersType } from './types';

close: () => void;
onError: (callback: (error: Event) => void) => void;
}

@@ -17,3 +18,4 @@ export declare class KameleoonEventSource implements IEventSource {

close(): void;
onError(callback: (error: Event) => void): void;
private handleMessage;
}

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

"use strict";var _constants=require("./constants");Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonEventSource=void 0;function _defineProperty(a,b,c){return(b=_toPropertyKey(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||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){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)}class KameleoonEventSource{constructor(a){let{siteCode:b,externalEventSource:c,urlProvider:d}=a;_defineProperty(this,"eventSource",void 0),_defineProperty(this,"siteCode",void 0),_defineProperty(this,"url",void 0),_defineProperty(this,"isConnectionOpen",void 0),this.url=d.getEventSourceUrl(),this.siteCode=b,this.eventSource=c,this.isConnectionOpen=!1}get isOpen(){return this.isConnectionOpen}open(a){this.eventSource.open({onEvent:this.handleMessage(a),url:this.url,eventType:_constants.REAL_TIME_EVENT}),this.isConnectionOpen=!0}close(){this.eventSource.close(),this.isConnectionOpen=!1}handleMessage(a){const b=b=>{const c=JSON.parse(b.data),{siteCode:d,ts:e}=c;this.siteCode===d&&a(e)};return b}}exports.KameleoonEventSource=KameleoonEventSource;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonEventSource=void 0;var _=require(".."),_constants=require("./constants");function _defineProperty(a,b,c){return(b=_toPropertyKey(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||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){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)}class KameleoonEventSource{constructor(a){let{siteCode:b,externalEventSource:c,urlProvider:d}=a;_defineProperty(this,"eventSource",void 0),_defineProperty(this,"siteCode",void 0),_defineProperty(this,"url",void 0),_defineProperty(this,"isConnectionOpen",void 0),this.url=d.getEventSourceUrl(),this.siteCode=b,this.eventSource=c,this.isConnectionOpen=!1}get isOpen(){return this.isConnectionOpen}open(a){this.eventSource.open({onEvent:this.handleMessage(a),url:this.url,eventType:_constants.REAL_TIME_EVENT}),this.isConnectionOpen=!0}close(){this.eventSource.close(),this.isConnectionOpen=!1}onError(a){return this.eventSource.onError?void this.eventSource.onError(a):void _.KameleoonLogger.warning("EventSource does not have `onError` method provided. Real-time errors won't be handled.")}handleMessage(a){const b=b=>{const c=JSON.parse(b.data),{siteCode:d,ts:e}=c;this.siteCode===d&&a(e)};return b}}exports.KameleoonEventSource=KameleoonEventSource;

@@ -29,2 +29,6 @@ import { IUrlProvider } from '../requester';

close: () => void;
/**
* @method onError - optional error handler - sets error handler for event source
* */
onError?: (callback: (error: Event) => void) => void;
}

@@ -31,0 +35,0 @@ /**

{
"name": "@kameleoon/javascript-sdk-core",
"version": "5.1.0",
"version": "5.1.1",
"description": "Kameleoon JS SDK Core",

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

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