New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

unleash-proxy-client

Package Overview
Dependencies
Maintainers
3
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unleash-proxy-client - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

src/events-handler.ts

5

build/index.d.ts

@@ -31,2 +31,3 @@ import { TinyEmitter } from 'tiny-emitter';

headerName?: string;
customHeaders?: Record<string, string>;
}

@@ -71,3 +72,4 @@ interface IVariant {

private eventsHandler;
constructor({ storageProvider, url, clientKey, disableRefresh, refreshInterval, metricsInterval, disableMetrics, appName, environment, context, fetch, bootstrap, bootstrapOverride, headerName, }: IConfig);
private customHeaders;
constructor({ storageProvider, url, clientKey, disableRefresh, refreshInterval, metricsInterval, disableMetrics, appName, environment, context, fetch, bootstrap, bootstrapOverride, headerName, customHeaders, }: IConfig);
getAllToggles(): IToggle[];

@@ -92,2 +94,3 @@ isEnabled(toggleName: string): boolean;

private resolveSessionId;
private getHeaders;
private storeToggles;

@@ -94,0 +97,0 @@ private fetchToggles;

38

build/index.js

@@ -115,3 +115,3 @@ "use strict";

function UnleashClient(_a) {
var storageProvider = _a.storageProvider, url = _a.url, clientKey = _a.clientKey, _b = _a.disableRefresh, disableRefresh = _b === void 0 ? false : _b, _c = _a.refreshInterval, refreshInterval = _c === void 0 ? 30 : _c, _d = _a.metricsInterval, metricsInterval = _d === void 0 ? 30 : _d, _e = _a.disableMetrics, disableMetrics = _e === void 0 ? false : _e, appName = _a.appName, _f = _a.environment, environment = _f === void 0 ? 'default' : _f, context = _a.context, _g = _a.fetch, fetch = _g === void 0 ? (0, exports.resolveFetch)() : _g, bootstrap = _a.bootstrap, _h = _a.bootstrapOverride, bootstrapOverride = _h === void 0 ? true : _h, _j = _a.headerName, headerName = _j === void 0 ? 'Authorization' : _j;
var storageProvider = _a.storageProvider, url = _a.url, clientKey = _a.clientKey, _b = _a.disableRefresh, disableRefresh = _b === void 0 ? false : _b, _c = _a.refreshInterval, refreshInterval = _c === void 0 ? 30 : _c, _d = _a.metricsInterval, metricsInterval = _d === void 0 ? 30 : _d, _e = _a.disableMetrics, disableMetrics = _e === void 0 ? false : _e, appName = _a.appName, _f = _a.environment, environment = _f === void 0 ? 'default' : _f, context = _a.context, _g = _a.fetch, fetch = _g === void 0 ? (0, exports.resolveFetch)() : _g, bootstrap = _a.bootstrap, _h = _a.bootstrapOverride, bootstrapOverride = _h === void 0 ? true : _h, _j = _a.headerName, headerName = _j === void 0 ? 'Authorization' : _j, _k = _a.customHeaders, customHeaders = _k === void 0 ? {} : _k;
var _this = _super.call(this) || this;

@@ -135,2 +135,3 @@ _this.toggles = [];

_this.headerName = headerName;
_this.customHeaders = customHeaders;
_this.storage = storageProvider || new storage_provider_local_1.default();

@@ -337,2 +338,11 @@ _this.refreshInterval = disableRefresh ? 0 : refreshInterval * 1000;

};
UnleashClient.prototype.getHeaders = function () {
var _a;
var headers = (_a = {}, _a[this.headerName] = this.clientKey, _a['Accept'] = 'application/json', _a['Content-Type'] = 'application/json', _a['If-None-Match'] = this.etag, _a);
Object.entries(this.customHeaders).filter(util_1.notNullOrUndefined).forEach(function (_a) {
var name = _a[0], value = _a[1];
return headers[name] = value;
});
return headers;
};
UnleashClient.prototype.storeToggles = function (toggles) {

@@ -356,10 +366,9 @@ return __awaiter(this, void 0, void 0, function () {

var context, urlWithQuery_1, response, data, e_1;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.fetch) return [3 /*break*/, 7];
_b.label = 1;
_a.label = 1;
case 1:
_b.trys.push([1, 6, , 7]);
_a.trys.push([1, 6, , 7]);
context = this.context;

@@ -388,11 +397,6 @@ urlWithQuery_1 = new URL(this.url.toString());

cache: 'no-cache',
headers: (_a = {},
_a[this.headerName] = this.clientKey,
_a.Accept = 'application/json',
_a['Content-Type'] = 'application/json',
_a['If-None-Match'] = this.etag,
_a),
headers: this.getHeaders(),
})];
case 2:
response = _b.sent();
response = _a.sent();
if (!(response.ok && response.status !== 304)) return [3 /*break*/, 5];

@@ -402,10 +406,10 @@ this.etag = response.headers.get('ETag') || '';

case 3:
data = _b.sent();
data = _a.sent();
return [4 /*yield*/, this.storeToggles(data.toggles)];
case 4:
_b.sent();
_b.label = 5;
_a.sent();
_a.label = 5;
case 5: return [3 /*break*/, 7];
case 6:
e_1 = _b.sent();
e_1 = _a.sent();
// tslint:disable-next-line

@@ -412,0 +416,0 @@ console.error('Unleash: unable to fetch feature toggles', e_1);

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

var unleash=function(a){"use strict";function b(a,c){function b(){this.constructor=a}if("function"!=typeof c&&null!==c)throw new TypeError("Class extends value "+(c+" is not a constructor or null"));k(a,c),a.prototype=null===c?Object.create(c):(b.prototype=c.prototype,new b)}function c(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})}function d(a,b){function c(a){return function(b){return d([a,b])}}function d(c){if(e)throw new TypeError("Generator is already executing.");for(;k;)try{if(e=1,h&&(i=2&c[0]?h["return"]:c[0]?h["throw"]||((i=h["return"])&&i.call(h),0):h.next)&&!(i=i.call(h,c[1])).done)return i;switch((h=0,i)&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return k.label++,{value:c[1],done:!1};case 5:k.label++,h=c[1],c=[0];continue;case 7:c=k.ops.pop(),k.trys.pop();continue;default:if((i=k.trys,!(i=0<i.length&&i[i.length-1]))&&(6===c[0]||2===c[0])){k=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){k.label=c[1];break}if(6===c[0]&&k.label<i[1]){k.label=i[1],i=c;break}if(i&&k.label<i[2]){k.label=i[2],k.ops.push(c);break}i[2]&&k.ops.pop(),k.trys.pop();continue;}c=b.call(a,k)}catch(a){c=[6,a],h=0}finally{e=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}var e,h,i,j,k={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return j={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(j[Symbol.iterator]=function(){return this}),j}function e(a,b,c){if(c||2===arguments.length)for(var d,e=0,f=b.length;e<f;e++)(d||!(e in b))&&(d||(d=Array.prototype.slice.call(b,0,e)),d[e]=b[e]);return a.concat(d||Array.prototype.slice.call(b))}function f(){}function g(){return s>r.length-16&&(q.randomFillSync(r),s=0),r.slice(s,s+=16)}function h(a){return"string"==typeof a&&t.test(a)}function i(a,b=0){const c=(u[a[b+0]]+u[a[b+1]]+u[a[b+2]]+u[a[b+3]]+"-"+u[a[b+4]]+u[a[b+5]]+"-"+u[a[b+6]]+u[a[b+7]]+"-"+u[a[b+8]]+u[a[b+9]]+"-"+u[a[b+10]]+u[a[b+11]]+u[a[b+12]]+u[a[b+13]]+u[a[b+14]]+u[a[b+15]]).toLowerCase();if(!h(c))throw TypeError("Stringified UUID is invalid");return c}function j(a,b,c){a=a||{};const d=a.random||(a.rng||g)();if(d[6]=64|15&d[6],d[8]=128|63&d[8],b){c=c||0;for(let a=0;16>a;++a)b[c+a]=d[a];return b}return i(d)}var k=function(a,c){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)Object.prototype.hasOwnProperty.call(c,b)&&(a[b]=c[b])},k(a,c)},l=function(){return l=Object.assign||function(a){for(var b,c=1,d=arguments.length;c<d;c++)for(var e in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,e)&&(a[e]=b[e]);return a},l.apply(this,arguments)};f.prototype={on:function(a,b,c){var d=this.e||(this.e={});return(d[a]||(d[a]=[])).push({fn:b,ctx:c}),this},once:function(a,b,c){function d(){e.off(a,d),b.apply(c,arguments)}var e=this;return d._=b,this.on(a,d,c)},emit:function(a){var b=[].slice.call(arguments,1),c=((this.e||(this.e={}))[a]||[]).slice(),d=0,e=c.length;for(d;d<e;d++)c[d].fn.apply(c[d].ctx,b);return this},off:function(a,b){var c=this.e||(this.e={}),d=c[a],e=[];if(d&&b)for(var f=0,g=d.length;f<g;f++)d[f].fn!==b&&d[f].fn._!==b&&e.push(d[f]);return e.length?c[a]=e:delete c[a],this}};var m=f;f.TinyEmitter=m;var n=function(){function a(a){var b=a.appName,c=a.metricsInterval,d=a.disableMetrics,e=a.url,f=a.clientKey,g=a.fetch,h=a.headerName;this.disabled=void 0!==d&&d,this.metricsInterval=1e3*c,this.appName=b,this.url=e,this.clientKey=f,this.bucket=this.createEmptyBucket(),this.fetch=g,this.headerName=h}return a.prototype.start=function(){var a=this;return!this.disabled&&void("number"==typeof this.metricsInterval&&0<this.metricsInterval&&setTimeout(function(){a.startTimer(),a.sendMetrics()},2e3))},a.prototype.stop=function(){this.timer&&(clearTimeout(this.timer),delete this.timer)},a.prototype.createEmptyBucket=function(){return{start:new Date,stop:null,toggles:{}}},a.prototype.sendMetrics=function(){return c(this,void 0,void 0,function(){var a,b,c;return d(this,function(d){switch(d.label){case 0:return a="".concat(this.url,"/client/metrics"),b=this.getPayload(),this.bucketIsEmpty(b)?[2]:[4,this.fetch(a,{cache:"no-cache",method:"POST",headers:(c={},c[this.headerName]=this.clientKey,c.Accept="application/json",c["Content-Type"]="application/json",c),body:JSON.stringify(b)})];case 1:return d.sent(),[2];}})})},a.prototype.count=function(a,b){return!this.disabled&&this.bucket&&(this.assertBucket(a),this.bucket.toggles[a][b?"yes":"no"]++,!0)},a.prototype.assertBucket=function(a){return!this.disabled&&this.bucket&&void(!this.bucket.toggles[a]&&(this.bucket.toggles[a]={yes:0,no:0}))},a.prototype.startTimer=function(){var a=this;this.timer=setInterval(function(){a.sendMetrics()},this.metricsInterval)},a.prototype.bucketIsEmpty=function(a){return 0===Object.keys(a.bucket.toggles).length},a.prototype.getPayload=function(){var a=l(l({},this.bucket),{stop:new Date});return this.bucket=this.createEmptyBucket(),{bucket:a,appName:this.appName,instanceId:"browser"}},a}(),o=function(){function a(){this.store=new Map}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){return d(this,function(){return this.store.set(a,b),[2]})})},a.prototype.get=function(a){return c(this,void 0,void 0,function(){return d(this,function(){return[2,this.store.get(a)]})})},a}(),p=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){var c,e;return d(this,function(){c=JSON.stringify(b),e="".concat(this.prefix,":").concat(a);try{window.localStorage.setItem(e,c)}catch(a){console.error(a)}return[2]})})},a.prototype.get=function(a){try{var b="".concat(this.prefix,":").concat(a),c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),q={};const r=new Uint8Array(256);let s=r.length;var t=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const u=[];for(let b=0;256>b;++b)u.push((b+256).toString(16).substr(1));var v=function(){function a(){}return a.prototype.generateEventId=function(){return j()},a.prototype.createImpressionEvent=function(a,b,c,d,e){var f=this.createBaseEvent(a,b,c,d);return e?l(l({},f),{variant:e}):f},a.prototype.createBaseEvent=function(a,b,c,d){return{eventType:d,eventId:this.generateEventId(),context:a,enabled:b,featureName:c}},a}(),w=function(a){var b=a[1];return void 0!==b&&null!==b},x=["userId","sessionId","remoteAddress"],y={INIT:"initialized",ERROR:"error",READY:"ready",UPDATE:"update",IMPRESSION:"impression"},z={IS_ENABLED:"isEnabled",GET_VARIANT:"getVariant"},A={name:"disabled",enabled:!1},B="repo",C=function(){try{if("fetch"in window)return fetch.bind(window);if("fetch"in globalThis)return fetch.bind(globalThis)}catch(a){console.error("Unleash failed to resolve \"fetch\"",a)}},D=function(a){function f(b){var e=b.storageProvider,f=b.url,g=b.clientKey,h=b.disableRefresh,i=b.refreshInterval,j=void 0===i?30:i,k=b.metricsInterval,m=void 0===k?30:k,o=b.disableMetrics,q=b.appName,r=b.environment,s=void 0===r?"default":r,t=b.context,u=b.fetch,w=void 0===u?C():u,x=b.bootstrap,z=b.bootstrapOverride,A=b.headerName,B=void 0===A?"Authorization":A,D=a.call(this)||this;if(D.toggles=[],D.etag="",!f)throw new Error("url is required");if(!g)throw new Error("clientKey is required");if(!q)throw new Error("appName is required.");return D.eventsHandler=new v,D.toggles=x&&0<x.length?x:[],D.url=new URL("".concat(f)),D.clientKey=g,D.headerName=B,D.storage=e||new p,D.refreshInterval=void 0!==h&&h?0:1e3*j,D.context=l({appName:q,environment:s},t),D.ready=new Promise(function(a){return c(D,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,this.init()];case 1:return c.sent(),[3,3];case 2:return b=c.sent(),console.error(b),this.emit(y.ERROR,b),[3,3];case 3:return a(),[2];}})})}),w||console.error("Unleash: You must either provide your own \"fetch\" implementation or run in an environment where \"fetch\" is available."),D.fetch=w,D.bootstrap=x&&0<x.length?x:void 0,D.bootstrapOverride=void 0===z||z,D.metrics=new n({appName:q,metricsInterval:m,disableMetrics:void 0!==o&&o,url:f,clientKey:g,fetch:w,headerName:B}),D}return b(f,a),f.prototype.getAllToggles=function(){return e([],this.toggles,!0)},f.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a}),c=!!b&&b.enabled;if(this.metrics.count(a,c),null===b||void 0===b?void 0:b.impressionData){var d=this.eventsHandler.createImpressionEvent(this.context,c,a,z.IS_ENABLED);this.emit(y.IMPRESSION,d)}return c},f.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});if(b){if(this.metrics.count(a,!0),b.impressionData){var c=this.eventsHandler.createImpressionEvent(this.context,b.enabled,a,z.GET_VARIANT,b.variant.name);this.emit(y.IMPRESSION,c)}return b.variant}return this.metrics.count(a,!1),A},f.prototype.updateContext=function(a){return c(this,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return(a.appName||a.environment)&&console.warn("appName and environment are static. They can't be updated with updateContext."),b={environment:this.context.environment,appName:this.context.appName},this.context=l(l({},b),a),this.timerRef?[4,this.fetchToggles()]:[3,2];case 1:c.sent(),c.label=2;case 2:return[2];}})})},f.prototype.getContext=function(){return l({},this.context)},f.prototype.setContextField=function(a,b){var c,d;if(x.includes(a))this.context=l(l({},this.context),(c={},c[a]=b,c));else{var e=l(l({},this.context.properties),(d={},d[a]=b,d));this.context=l(l({},this.context),{properties:e})}this.timerRef&&this.fetchToggles()},f.prototype.init=function(){return c(this,void 0,void 0,function(){var a,b;return d(this,function(c){switch(c.label){case 0:return[4,this.resolveSessionId()];case 1:return a=c.sent(),this.context=l({sessionId:a},this.context),b=this,[4,this.storage.get(B)];case 2:return b.toggles=c.sent()||[],this.bootstrap&&(this.bootstrapOverride||0===this.toggles.length)?[4,this.storage.save(B,this.bootstrap)]:[3,4];case 3:c.sent(),this.toggles=this.bootstrap,this.emit(y.READY),c.label=4;case 4:return this.emit(y.INIT),[2];}})})},f.prototype.start=function(){return c(this,void 0,void 0,function(){var a,b=this;return d(this,function(c){switch(c.label){case 0:return this.timerRef?(console.error("Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again."),[2]):[4,this.ready];case 1:return c.sent(),this.metrics.start(),a=this.refreshInterval,[4,this.fetchToggles()];case 2:return c.sent(),this.bootstrap||this.emit(y.READY),0<a&&(this.timerRef=setInterval(function(){return b.fetchToggles()},a)),[2];}})})},f.prototype.stop=function(){this.timerRef&&(clearInterval(this.timerRef),this.timerRef=void 0),this.metrics.stop()},f.prototype.resolveSessionId=function(){return c(this,void 0,void 0,function(){var a;return d(this,function(b){var c=Math.floor;switch(b.label){case 0:return this.context.sessionId?[2,this.context.sessionId]:[3,1];case 1:return[4,this.storage.get("sessionId")];case 2:return(a=b.sent(),!!a)?[3,4]:(a=c(1e9*Math.random()),[4,this.storage.save("sessionId",a)]);case 3:b.sent(),b.label=4;case 4:return[2,a];}})})},f.prototype.storeToggles=function(a){return c(this,void 0,void 0,function(){return d(this,function(b){switch(b.label){case 0:return this.toggles=a,this.emit(y.UPDATE),[4,this.storage.save(B,a)];case 1:return b.sent(),[2];}})})},f.prototype.fetchToggles=function(){return c(this,void 0,void 0,function(){var a,b,c,e,f,g;return d(this,function(d){switch(d.label){case 0:if(!this.fetch)return[3,7];d.label=1;case 1:return d.trys.push([1,6,,7]),a=this.context,b=new URL(this.url.toString()),Object.entries(a).filter(w).forEach(function(a){var c=a[0],d=a[1];"properties"===c&&d?Object.entries(d).filter(w).forEach(function(a){var c=a[0],d=a[1];return b.searchParams.append("properties[".concat(c,"]"),d)}):b.searchParams.append(c,d)}),[4,this.fetch(b.toString(),{cache:"no-cache",headers:(g={},g[this.headerName]=this.clientKey,g.Accept="application/json",g["Content-Type"]="application/json",g["If-None-Match"]=this.etag,g)})];case 2:return(c=d.sent(),!(c.ok&&304!==c.status))?[3,5]:(this.etag=c.headers.get("ETag")||"",[4,c.json()]);case 3:return e=d.sent(),[4,this.storeToggles(e.toggles)];case 4:d.sent(),d.label=5;case 5:return[3,7];case 6:return f=d.sent(),console.error("Unleash: unable to fetch feature toggles",f),this.emit(y.ERROR,f),[3,7];case 7:return[2];}})})},f}(m);return a.EVENTS=y,a.InMemoryStorageProvider=o,a.LocalStorageProvider=p,a.UnleashClient=D,a.resolveFetch=C,a}({});
var unleash=function(a){"use strict";function b(a,c){function b(){this.constructor=a}if("function"!=typeof c&&null!==c)throw new TypeError("Class extends value "+(c+" is not a constructor or null"));k(a,c),a.prototype=null===c?Object.create(c):(b.prototype=c.prototype,new b)}function c(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})}function d(a,b){function c(a){return function(b){return d([a,b])}}function d(c){if(e)throw new TypeError("Generator is already executing.");for(;k;)try{if(e=1,h&&(i=2&c[0]?h["return"]:c[0]?h["throw"]||((i=h["return"])&&i.call(h),0):h.next)&&!(i=i.call(h,c[1])).done)return i;switch((h=0,i)&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return k.label++,{value:c[1],done:!1};case 5:k.label++,h=c[1],c=[0];continue;case 7:c=k.ops.pop(),k.trys.pop();continue;default:if((i=k.trys,!(i=0<i.length&&i[i.length-1]))&&(6===c[0]||2===c[0])){k=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){k.label=c[1];break}if(6===c[0]&&k.label<i[1]){k.label=i[1],i=c;break}if(i&&k.label<i[2]){k.label=i[2],k.ops.push(c);break}i[2]&&k.ops.pop(),k.trys.pop();continue;}c=b.call(a,k)}catch(a){c=[6,a],h=0}finally{e=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}var e,h,i,j,k={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return j={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(j[Symbol.iterator]=function(){return this}),j}function e(a,b,c){if(c||2===arguments.length)for(var d,e=0,f=b.length;e<f;e++)(d||!(e in b))&&(d||(d=Array.prototype.slice.call(b,0,e)),d[e]=b[e]);return a.concat(d||Array.prototype.slice.call(b))}function f(){}function g(){return s>r.length-16&&(q.randomFillSync(r),s=0),r.slice(s,s+=16)}function h(a){return"string"==typeof a&&t.test(a)}function i(a,b=0){const c=(u[a[b+0]]+u[a[b+1]]+u[a[b+2]]+u[a[b+3]]+"-"+u[a[b+4]]+u[a[b+5]]+"-"+u[a[b+6]]+u[a[b+7]]+"-"+u[a[b+8]]+u[a[b+9]]+"-"+u[a[b+10]]+u[a[b+11]]+u[a[b+12]]+u[a[b+13]]+u[a[b+14]]+u[a[b+15]]).toLowerCase();if(!h(c))throw TypeError("Stringified UUID is invalid");return c}function j(a,b,c){a=a||{};const d=a.random||(a.rng||g)();if(d[6]=64|15&d[6],d[8]=128|63&d[8],b){c=c||0;for(let a=0;16>a;++a)b[c+a]=d[a];return b}return i(d)}var k=function(a,c){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)Object.prototype.hasOwnProperty.call(c,b)&&(a[b]=c[b])},k(a,c)},l=function(){return l=Object.assign||function(a){for(var b,c=1,d=arguments.length;c<d;c++)for(var e in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,e)&&(a[e]=b[e]);return a},l.apply(this,arguments)};f.prototype={on:function(a,b,c){var d=this.e||(this.e={});return(d[a]||(d[a]=[])).push({fn:b,ctx:c}),this},once:function(a,b,c){function d(){e.off(a,d),b.apply(c,arguments)}var e=this;return d._=b,this.on(a,d,c)},emit:function(a){var b=[].slice.call(arguments,1),c=((this.e||(this.e={}))[a]||[]).slice(),d=0,e=c.length;for(d;d<e;d++)c[d].fn.apply(c[d].ctx,b);return this},off:function(a,b){var c=this.e||(this.e={}),d=c[a],e=[];if(d&&b)for(var f=0,g=d.length;f<g;f++)d[f].fn!==b&&d[f].fn._!==b&&e.push(d[f]);return e.length?c[a]=e:delete c[a],this}};var m=f;f.TinyEmitter=m;var n=function(){function a(a){var b=a.appName,c=a.metricsInterval,d=a.disableMetrics,e=a.url,f=a.clientKey,g=a.fetch,h=a.headerName;this.disabled=void 0!==d&&d,this.metricsInterval=1e3*c,this.appName=b,this.url=e,this.clientKey=f,this.bucket=this.createEmptyBucket(),this.fetch=g,this.headerName=h}return a.prototype.start=function(){var a=this;return!this.disabled&&void("number"==typeof this.metricsInterval&&0<this.metricsInterval&&setTimeout(function(){a.startTimer(),a.sendMetrics()},2e3))},a.prototype.stop=function(){this.timer&&(clearTimeout(this.timer),delete this.timer)},a.prototype.createEmptyBucket=function(){return{start:new Date,stop:null,toggles:{}}},a.prototype.sendMetrics=function(){return c(this,void 0,void 0,function(){var a,b,c;return d(this,function(d){switch(d.label){case 0:return a="".concat(this.url,"/client/metrics"),b=this.getPayload(),this.bucketIsEmpty(b)?[2]:[4,this.fetch(a,{cache:"no-cache",method:"POST",headers:(c={},c[this.headerName]=this.clientKey,c.Accept="application/json",c["Content-Type"]="application/json",c),body:JSON.stringify(b)})];case 1:return d.sent(),[2];}})})},a.prototype.count=function(a,b){return!this.disabled&&this.bucket&&(this.assertBucket(a),this.bucket.toggles[a][b?"yes":"no"]++,!0)},a.prototype.assertBucket=function(a){return!this.disabled&&this.bucket&&void(!this.bucket.toggles[a]&&(this.bucket.toggles[a]={yes:0,no:0}))},a.prototype.startTimer=function(){var a=this;this.timer=setInterval(function(){a.sendMetrics()},this.metricsInterval)},a.prototype.bucketIsEmpty=function(a){return 0===Object.keys(a.bucket.toggles).length},a.prototype.getPayload=function(){var a=l(l({},this.bucket),{stop:new Date});return this.bucket=this.createEmptyBucket(),{bucket:a,appName:this.appName,instanceId:"browser"}},a}(),o=function(){function a(){this.store=new Map}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){return d(this,function(){return this.store.set(a,b),[2]})})},a.prototype.get=function(a){return c(this,void 0,void 0,function(){return d(this,function(){return[2,this.store.get(a)]})})},a}(),p=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){var c,e;return d(this,function(){c=JSON.stringify(b),e="".concat(this.prefix,":").concat(a);try{window.localStorage.setItem(e,c)}catch(a){console.error(a)}return[2]})})},a.prototype.get=function(a){try{var b="".concat(this.prefix,":").concat(a),c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),q={};const r=new Uint8Array(256);let s=r.length;var t=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const u=[];for(let b=0;256>b;++b)u.push((b+256).toString(16).substr(1));var v=function(){function a(){}return a.prototype.generateEventId=function(){return j()},a.prototype.createImpressionEvent=function(a,b,c,d,e){var f=this.createBaseEvent(a,b,c,d);return e?l(l({},f),{variant:e}):f},a.prototype.createBaseEvent=function(a,b,c,d){return{eventType:d,eventId:this.generateEventId(),context:a,enabled:b,featureName:c}},a}(),w=function(a){var b=a[1];return void 0!==b&&null!==b},x=["userId","sessionId","remoteAddress"],y={INIT:"initialized",ERROR:"error",READY:"ready",UPDATE:"update",IMPRESSION:"impression"},z={IS_ENABLED:"isEnabled",GET_VARIANT:"getVariant"},A={name:"disabled",enabled:!1},B="repo",C=function(){try{if("fetch"in window)return fetch.bind(window);if("fetch"in globalThis)return fetch.bind(globalThis)}catch(a){console.error("Unleash failed to resolve \"fetch\"",a)}},D=function(a){function f(b){var e=b.storageProvider,f=b.url,g=b.clientKey,h=b.disableRefresh,i=b.refreshInterval,j=void 0===i?30:i,k=b.metricsInterval,m=void 0===k?30:k,o=b.disableMetrics,q=b.appName,r=b.environment,s=void 0===r?"default":r,t=b.context,u=b.fetch,w=void 0===u?C():u,x=b.bootstrap,z=b.bootstrapOverride,A=b.headerName,B=void 0===A?"Authorization":A,D=b.customHeaders,E=void 0===D?{}:D,F=a.call(this)||this;if(F.toggles=[],F.etag="",!f)throw new Error("url is required");if(!g)throw new Error("clientKey is required");if(!q)throw new Error("appName is required.");return F.eventsHandler=new v,F.toggles=x&&0<x.length?x:[],F.url=new URL("".concat(f)),F.clientKey=g,F.headerName=B,F.customHeaders=E,F.storage=e||new p,F.refreshInterval=void 0!==h&&h?0:1e3*j,F.context=l({appName:q,environment:s},t),F.ready=new Promise(function(a){return c(F,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,this.init()];case 1:return c.sent(),[3,3];case 2:return b=c.sent(),console.error(b),this.emit(y.ERROR,b),[3,3];case 3:return a(),[2];}})})}),w||console.error("Unleash: You must either provide your own \"fetch\" implementation or run in an environment where \"fetch\" is available."),F.fetch=w,F.bootstrap=x&&0<x.length?x:void 0,F.bootstrapOverride=void 0===z||z,F.metrics=new n({appName:q,metricsInterval:m,disableMetrics:void 0!==o&&o,url:f,clientKey:g,fetch:w,headerName:B}),F}return b(f,a),f.prototype.getAllToggles=function(){return e([],this.toggles,!0)},f.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a}),c=!!b&&b.enabled;if(this.metrics.count(a,c),null===b||void 0===b?void 0:b.impressionData){var d=this.eventsHandler.createImpressionEvent(this.context,c,a,z.IS_ENABLED);this.emit(y.IMPRESSION,d)}return c},f.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});if(b){if(this.metrics.count(a,!0),b.impressionData){var c=this.eventsHandler.createImpressionEvent(this.context,b.enabled,a,z.GET_VARIANT,b.variant.name);this.emit(y.IMPRESSION,c)}return b.variant}return this.metrics.count(a,!1),A},f.prototype.updateContext=function(a){return c(this,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return(a.appName||a.environment)&&console.warn("appName and environment are static. They can't be updated with updateContext."),b={environment:this.context.environment,appName:this.context.appName},this.context=l(l({},b),a),this.timerRef?[4,this.fetchToggles()]:[3,2];case 1:c.sent(),c.label=2;case 2:return[2];}})})},f.prototype.getContext=function(){return l({},this.context)},f.prototype.setContextField=function(a,b){var c,d;if(x.includes(a))this.context=l(l({},this.context),(c={},c[a]=b,c));else{var e=l(l({},this.context.properties),(d={},d[a]=b,d));this.context=l(l({},this.context),{properties:e})}this.timerRef&&this.fetchToggles()},f.prototype.init=function(){return c(this,void 0,void 0,function(){var a,b;return d(this,function(c){switch(c.label){case 0:return[4,this.resolveSessionId()];case 1:return a=c.sent(),this.context=l({sessionId:a},this.context),b=this,[4,this.storage.get(B)];case 2:return b.toggles=c.sent()||[],this.bootstrap&&(this.bootstrapOverride||0===this.toggles.length)?[4,this.storage.save(B,this.bootstrap)]:[3,4];case 3:c.sent(),this.toggles=this.bootstrap,this.emit(y.READY),c.label=4;case 4:return this.emit(y.INIT),[2];}})})},f.prototype.start=function(){return c(this,void 0,void 0,function(){var a,b=this;return d(this,function(c){switch(c.label){case 0:return this.timerRef?(console.error("Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again."),[2]):[4,this.ready];case 1:return c.sent(),this.metrics.start(),a=this.refreshInterval,[4,this.fetchToggles()];case 2:return c.sent(),this.bootstrap||this.emit(y.READY),0<a&&(this.timerRef=setInterval(function(){return b.fetchToggles()},a)),[2];}})})},f.prototype.stop=function(){this.timerRef&&(clearInterval(this.timerRef),this.timerRef=void 0),this.metrics.stop()},f.prototype.resolveSessionId=function(){return c(this,void 0,void 0,function(){var a;return d(this,function(b){var c=Math.floor;switch(b.label){case 0:return this.context.sessionId?[2,this.context.sessionId]:[3,1];case 1:return[4,this.storage.get("sessionId")];case 2:return(a=b.sent(),!!a)?[3,4]:(a=c(1e9*Math.random()),[4,this.storage.save("sessionId",a)]);case 3:b.sent(),b.label=4;case 4:return[2,a];}})})},f.prototype.getHeaders=function(){var a,b=(a={},a[this.headerName]=this.clientKey,a.Accept="application/json",a["Content-Type"]="application/json",a["If-None-Match"]=this.etag,a);return Object.entries(this.customHeaders).filter(w).forEach(function(a){var c=a[0],d=a[1];return b[c]=d}),b},f.prototype.storeToggles=function(a){return c(this,void 0,void 0,function(){return d(this,function(b){switch(b.label){case 0:return this.toggles=a,this.emit(y.UPDATE),[4,this.storage.save(B,a)];case 1:return b.sent(),[2];}})})},f.prototype.fetchToggles=function(){return c(this,void 0,void 0,function(){var a,b,c,e,f;return d(this,function(d){switch(d.label){case 0:if(!this.fetch)return[3,7];d.label=1;case 1:return d.trys.push([1,6,,7]),a=this.context,b=new URL(this.url.toString()),Object.entries(a).filter(w).forEach(function(a){var c=a[0],d=a[1];"properties"===c&&d?Object.entries(d).filter(w).forEach(function(a){var c=a[0],d=a[1];return b.searchParams.append("properties[".concat(c,"]"),d)}):b.searchParams.append(c,d)}),[4,this.fetch(b.toString(),{cache:"no-cache",headers:this.getHeaders()})];case 2:return(c=d.sent(),!(c.ok&&304!==c.status))?[3,5]:(this.etag=c.headers.get("ETag")||"",[4,c.json()]);case 3:return e=d.sent(),[4,this.storeToggles(e.toggles)];case 4:d.sent(),d.label=5;case 5:return[3,7];case 6:return f=d.sent(),console.error("Unleash: unable to fetch feature toggles",f),this.emit(y.ERROR,f),[3,7];case 7:return[2];}})})},f}(m);return a.EVENTS=y,a.InMemoryStorageProvider=o,a.LocalStorageProvider=p,a.UnleashClient=D,a.resolveFetch=C,a}({});
{
"name": "unleash-proxy-client",
"version": "2.0.0",
"version": "2.0.1",
"description": "A browser client that can be used together with the unleash-proxy.",

@@ -9,3 +9,4 @@ "main": "./build/index.js",

"build",
"examples"
"examples",
"src"
],

@@ -12,0 +13,0 @@ "scripts": {

@@ -109,2 +109,3 @@ # Unleash Proxy Client for the browser (JS)

| headerName | no| `Authorization` | Provides possiblity to specify custom header that is passed to Unleash / Unleash Proxy with the `clientKey` |
| customHeaders | no| `{}` | Additional headers to use when making HTTP requests to the Unleash proxy. In case of name collisions with the default headers, the `customHeaders` value will be used. |

@@ -111,0 +112,0 @@

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