@amplitude/analytics-remote-config
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -114,2 +114,3 @@ var _this = this; | ||
case analytics_types_1.Status.Success: | ||
this.attempts = 0; | ||
return [2 /*return*/, this.parseAndStoreConfig(res, sessionId)]; | ||
@@ -116,0 +117,0 @@ case analytics_types_1.Status.Failed: |
@@ -111,2 +111,3 @@ import { __assign, __awaiter, __generator } from "tslib"; | ||
case Status.Success: | ||
this.attempts = 0; | ||
return [2 /*return*/, this.parseAndStoreConfig(res, sessionId)]; | ||
@@ -113,0 +114,0 @@ case Status.Failed: |
@@ -1,1 +0,1 @@ | ||
var remoteConfig=function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function s(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}!function(e){e.US="US",e.EU="EU",e.STAGING="STAGING"}(t||(t={})),function(e){e.Unknown="unknown",e.Skipped="skipped",e.Success="success",e.RateLimit="rate_limit",e.PayloadTooLarge="payload_too_large",e.Invalid="invalid",e.Failed="failed",e.Timeout="Timeout",e.SystemError="SystemError"}(n||(n={}));var a=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,r,o,i,s,c,a,u,l,d,f,h,v,g,p,m,y,b,w,I,S,D;if("object"!=typeof e)return null;var C=e.code||0,_=this.buildStatus(C);switch(_){case n.Success:return{status:_,statusCode:C,body:{eventsIngested:null!==(t=e.events_ingested)&&void 0!==t?t:0,payloadSizeBytes:null!==(r=e.payload_size_bytes)&&void 0!==r?r:0,serverUploadTime:null!==(o=e.server_upload_time)&&void 0!==o?o:0}};case n.Invalid:return{status:_,statusCode:C,body:{error:null!==(i=e.error)&&void 0!==i?i:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(c=e.events_with_invalid_fields)&&void 0!==c?c:{},eventsWithMissingFields:null!==(a=e.events_with_missing_fields)&&void 0!==a?a:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(l=e.eps_threshold)&&void 0!==l?l:0,exceededDailyQuotaDevices:null!==(d=e.exceeded_daily_quota_devices)&&void 0!==d?d:{},silencedDevices:null!==(f=e.silenced_devices)&&void 0!==f?f:[],silencedEvents:null!==(h=e.silenced_events)&&void 0!==h?h:[],throttledDevices:null!==(v=e.throttled_devices)&&void 0!==v?v:{},throttledEvents:null!==(g=e.throttled_events)&&void 0!==g?g:[]}};case n.PayloadTooLarge:return{status:_,statusCode:C,body:{error:null!==(p=e.error)&&void 0!==p?p:""}};case n.RateLimit:return{status:_,statusCode:C,body:{error:null!==(m=e.error)&&void 0!==m?m:"",epsThreshold:null!==(y=e.eps_threshold)&&void 0!==y?y:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(w=e.throttled_users)&&void 0!==w?w:{},exceededDailyQuotaDevices:null!==(I=e.exceeded_daily_quota_devices)&&void 0!==I?I:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(D=e.throttled_events)&&void 0!==D?D:[]}};case n.Timeout:default:return{status:_,statusCode:C}}},e.prototype.buildStatus=function(e){return e>=200&&e<300?n.Success:429===e?n.RateLimit:413===e?n.PayloadTooLarge:408===e?n.Timeout:e>=400&&e<500?n.Invalid:e>=500?n.Failed:n.Unknown},e}();const u=(e,t)=>t.some((t=>e instanceof t));let l,d;const f=new WeakMap,h=new WeakMap,v=new WeakMap;let g={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return f.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return b(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function p(e){g=e(g)}function m(e){return(d||(d=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(w(this),t),b(this.request)}:function(...t){return b(e.apply(w(this),t))}}function y(e){return"function"==typeof e?m(e):(e instanceof IDBTransaction&&function(e){if(f.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",i),e.removeEventListener("abort",i)},o=()=>{t(),r()},i=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",i),e.addEventListener("abort",i)}));f.set(e,t)}(e),u(e,l||(l=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,g):e)}function b(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",i)},o=()=>{t(b(e.result)),r()},i=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",i)}));return v.set(t,e),t}(e);if(h.has(e))return h.get(e);const t=y(e);return t!==e&&(h.set(e,t),v.set(t,e)),t}const w=e=>v.get(e);function I(e,t,{blocked:n,upgrade:r,blocking:o,terminated:i}={}){const s=indexedDB.open(e,t),c=b(s);return r&&s.addEventListener("upgradeneeded",(e=>{r(b(s.result),e.oldVersion,e.newVersion,b(s.transaction),e)})),n&&s.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),c.then((e=>{i&&e.addEventListener("close",(()=>i())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}function S(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),b(n).then((()=>{}))}const D=["get","getKey","getAll","getAllKeys","count"],C=["put","add","delete","clear"],_=new Map;function T(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(_.get(t))return _.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=C.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.includes(n))return;const i=async function(e,...t){const i=this.transaction(e,o?"readwrite":"readonly");let s=i.store;return r&&(s=s.index(t.shift())),(await Promise.all([s[n](...t),o&&i.done]))[0]};return _.set(t,i),i}p((e=>({...e,get:(t,n,r)=>T(t,n)||e.get(t,n,r),has:(t,n)=>!!T(t,n)||e.has(t,n)})));const B=["continue","continuePrimaryKey","advance"],E={},R=new WeakMap,x=new WeakMap,F={get(e,t){if(!B.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){R.set(this,x.get(this)[t](...e))}),n}};async function*L(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,F);for(x.set(n,t),v.set(n,w(t));t;)yield n,t=await(R.get(n)||t.continue()),R.delete(n)}function k(e,t){return t===Symbol.asyncIterator&&u(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===t&&u(e,[IDBIndex,IDBObjectStore])}p((e=>({...e,get:(t,n,r)=>k(t,n)?L:e.get(t,n,r),has:(t,n)=>k(t,n)||e.has(t,n)})));var j=function(e,t){return o(void 0,void 0,void 0,(function(){return i(this,(function(n){switch(n.label){case 0:return[4,I(e,1,{upgrade:function(e){t.forEach((function(t){e.objectStoreNames.contains(t)||e.createObjectStore(t)}))}})];case 1:return[2,n.sent()]}}))}))},P=function(e){var t=e.loggerProvider,n=e.apiKey,a=e.configKeys;return o(void 0,void 0,void 0,(function(){var e,u,l,d,f,h,v,g,p,m;return i(this,(function(y){switch(y.label){case 0:return e="".concat(n.substring(0,10),"_amp_config"),[4,j(e,a)];case 1:return u=y.sent(),l="".concat(n.substring(0,10),"_amp_config_meta"),[4,(b=l,o(void 0,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return[4,I(b,1,{upgrade:function(e){e.objectStoreNames.contains("lastFetchedSessionId")||e.createObjectStore("lastFetchedSessionId")}})];case 1:return[2,e.sent()]}}))})))];case 2:d=y.sent(),y.label=3;case 3:return y.trys.push([3,8,,9]),[4,d.get("lastFetchedSessionId","sessionId")];case 4:return(f=y.sent())&&Date.now()-f>=2592e5?(u.close(),[4,S(e)]):[3,7];case 5:return y.sent(),[4,j(n,a)];case 6:u=y.sent(),y.label=7;case 7:return[3,9];case 8:return h=y.sent(),t.warn("Failed to reset store: ".concat(h)),[3,9];case 9:return v=function(e,n){return o(void 0,void 0,void 0,(function(){var o,a,l,f,h,v;return i(this,(function(g){switch(g.label){case 0:return g.trys.push([0,7,,8]),n?[4,d.put("lastFetchedSessionId",n,"sessionId")]:[3,2];case 1:g.sent(),g.label=2;case 2:for(f in o=function(t){var n,o;return i(this,(function(i){switch(i.label){case 0:return n=e.configs[t],o=u.transaction(t,"readwrite"),[4,Promise.all(c(c([],s(Object.keys(n).map((function(e){return o.store.put(r({},n[e]),e)}))),!1),[o.done],!1))];case 1:return i.sent(),[2]}}))},a=e.configs,l=[],a)l.push(f);h=0,g.label=3;case 3:return h<l.length?(f=l[h])in a?[5,o(f)]:[3,5]:[3,6];case 4:g.sent(),g.label=5;case 5:return h++,[3,3];case 6:return[3,8];case 7:return v=g.sent(),t.warn("Failed to store remote config: ".concat(v)),[3,8];case 8:return[2]}}))}))},g=function(){return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,d.get("lastFetchedSessionId","sessionId")];case 1:return[2,n.sent()];case 2:return e=n.sent(),t.warn("Failed to fetch lastFetchedSessionId: ".concat(e)),[3,3];case 3:return[2,void 0]}}))}))},p=function(e,n){return o(void 0,void 0,void 0,(function(){var r;return i(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,u.get(e,n)];case 1:return[2,o.sent()];case 2:return r=o.sent(),t.warn("Failed to fetch remote config: ".concat(r)),[3,3];case 3:return[2,void 0]}}))}))},m=function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,u.getAll(e)];case 1:return[2,!!r.sent().length];case 2:return n=r.sent(),t.warn("Failed to fetch remote config: ".concat(n)),[3,3];case 3:return[2,!1]}}))}))},[2,{storeRemoteConfig:v,getRemoteConfig:p,getLastFetchedSessionId:g,remoteConfigHasValues:m}]}var b}))}))},K="Remote config fetch rejected due to timeout after 5 seconds",A=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.fetchTime=0,this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a,u;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.remoteConfigHasValues(e)]:[3,4];case 1:return o=i.sent(),[4,this.remoteConfigIDBStore.getLastFetchedSessionId()];case 2:return s=i.sent(),o&&s===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,4];case 3:return c=i.sent(),this.fetchTime=Date.now()-r,[2,c];case 4:return[4,this.fetchWithTimeout(n)];case 5:return(a=i.sent())&&(u=a.configs&&a.configs[e])?(this.fetchTime=Date.now()-r,[2,u[t]]):(this.fetchTime=Date.now()-r,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return o(c,void 0,void 0,(function(){var t,n,r;return i(this,(function(o){switch(o.label){case 0:return t=new AbortController,n=setTimeout((function(){return t.abort()}),5e3),[4,this.fetchRemoteConfig(t.signal,e)];case 1:return r=o.sent(),clearTimeout(n),[2,r]}}))}))},this.fetchRemoteConfig=function(e,t){return o(c,void 0,void 0,(function(){var o,s,c,u,l,d;return i(this,(function(i){switch(i.label){case 0:if(t===this.lastFetchedSessionId&&this.attempts>=this.localConfig.flushMaxRetries)return[2,this.completeRequest({err:"Remote config fetch rejected due to exceeded retry count"})];if(e.aborted)return[2,this.completeRequest({err:K})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),i.label=1;case 1:return i.trys.push([1,3,,4]),o=new URLSearchParams({api_key:this.localConfig.apiKey,config_keys:this.configKeys.join(",")}),s={headers:{"Content-Type":"application/json",Accept:"*/*"},method:"GET"},c="".concat(this.getServerUrl(),"?").concat(o.toString()),this.attempts+=1,[4,fetch(c,r(r({},s),{signal:e}))];case 2:if(null===(u=i.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new a).buildStatus(u.status)){case n.Success:return[2,this.parseAndStoreConfig(u,t)];case n.Failed:return[2,this.retryFetch(e,t)];default:return[2,this.completeRequest({err:"Network error occurred, remote config fetch failed"})]}case 3:return l=i.sent(),d=l,e.aborted?[2,this.completeRequest({err:K})]:[2,this.completeRequest({err:d.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return o(c,void 0,void 0,(function(){var n=this;return i(this,(function(r){switch(r.label){case 0:return[4,new Promise((function(e){return setTimeout(e,n.attempts*n.retryTimeout)}))];case 1:return r.sent(),[2,this.fetchRemoteConfig(e,t)]}}))}))},this.parseAndStoreConfig=function(e,t){return o(c,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return[4,e.json()];case 1:return n=r.sent(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.storeRemoteConfig(n,t)]:[3,3];case 2:r.sent(),r.label=3;case 3:return this.completeRequest({success:"Remote config successfully fetched"}),[2,n]}}))}))},this.localConfig=t,this.configKeys=s}return e.prototype.initialize=function(){return o(this,void 0,void 0,(function(){var e;return i(this,(function(t){switch(t.label){case 0:return e=this,[4,P({apiKey:this.localConfig.apiKey,loggerProvider:this.localConfig.loggerProvider,configKeys:this.configKeys})];case 1:return e.remoteConfigIDBStore=t.sent(),[2]}}))}))},e.prototype.getServerUrl=function(){return this.localConfig.serverZone===t.STAGING?"https://sr-client-cfg.stag2.amplitude.com/config":this.localConfig.serverZone===t.EU?"https://sr-client-cfg.eu.amplitude.com/config":"https://sr-client-cfg.amplitude.com/config"},e.prototype.completeRequest=function(e){var t=e.err,n=e.success;if(t)throw new Error(t);n&&this.localConfig.loggerProvider.log(n)},e}(),U=function(e){var t=e.localConfig,n=e.configKeys;return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return[4,(e=new A({localConfig:t,configKeys:n})).initialize()];case 1:return r.sent(),[2,e]}}))}))};return e.createRemoteConfigFetch=U,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); | ||
var remoteConfig=function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function s(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}!function(e){e.US="US",e.EU="EU",e.STAGING="STAGING"}(t||(t={})),function(e){e.Unknown="unknown",e.Skipped="skipped",e.Success="success",e.RateLimit="rate_limit",e.PayloadTooLarge="payload_too_large",e.Invalid="invalid",e.Failed="failed",e.Timeout="Timeout",e.SystemError="SystemError"}(n||(n={}));var a=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,r,o,i,s,c,a,u,l,d,f,h,v,g,p,m,y,b,w,I,S,D;if("object"!=typeof e)return null;var C=e.code||0,_=this.buildStatus(C);switch(_){case n.Success:return{status:_,statusCode:C,body:{eventsIngested:null!==(t=e.events_ingested)&&void 0!==t?t:0,payloadSizeBytes:null!==(r=e.payload_size_bytes)&&void 0!==r?r:0,serverUploadTime:null!==(o=e.server_upload_time)&&void 0!==o?o:0}};case n.Invalid:return{status:_,statusCode:C,body:{error:null!==(i=e.error)&&void 0!==i?i:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(c=e.events_with_invalid_fields)&&void 0!==c?c:{},eventsWithMissingFields:null!==(a=e.events_with_missing_fields)&&void 0!==a?a:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(l=e.eps_threshold)&&void 0!==l?l:0,exceededDailyQuotaDevices:null!==(d=e.exceeded_daily_quota_devices)&&void 0!==d?d:{},silencedDevices:null!==(f=e.silenced_devices)&&void 0!==f?f:[],silencedEvents:null!==(h=e.silenced_events)&&void 0!==h?h:[],throttledDevices:null!==(v=e.throttled_devices)&&void 0!==v?v:{},throttledEvents:null!==(g=e.throttled_events)&&void 0!==g?g:[]}};case n.PayloadTooLarge:return{status:_,statusCode:C,body:{error:null!==(p=e.error)&&void 0!==p?p:""}};case n.RateLimit:return{status:_,statusCode:C,body:{error:null!==(m=e.error)&&void 0!==m?m:"",epsThreshold:null!==(y=e.eps_threshold)&&void 0!==y?y:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(w=e.throttled_users)&&void 0!==w?w:{},exceededDailyQuotaDevices:null!==(I=e.exceeded_daily_quota_devices)&&void 0!==I?I:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(D=e.throttled_events)&&void 0!==D?D:[]}};case n.Timeout:default:return{status:_,statusCode:C}}},e.prototype.buildStatus=function(e){return e>=200&&e<300?n.Success:429===e?n.RateLimit:413===e?n.PayloadTooLarge:408===e?n.Timeout:e>=400&&e<500?n.Invalid:e>=500?n.Failed:n.Unknown},e}();const u=(e,t)=>t.some((t=>e instanceof t));let l,d;const f=new WeakMap,h=new WeakMap,v=new WeakMap;let g={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return f.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return b(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function p(e){g=e(g)}function m(e){return(d||(d=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(w(this),t),b(this.request)}:function(...t){return b(e.apply(w(this),t))}}function y(e){return"function"==typeof e?m(e):(e instanceof IDBTransaction&&function(e){if(f.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",i),e.removeEventListener("abort",i)},o=()=>{t(),r()},i=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",i),e.addEventListener("abort",i)}));f.set(e,t)}(e),u(e,l||(l=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,g):e)}function b(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",i)},o=()=>{t(b(e.result)),r()},i=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",i)}));return v.set(t,e),t}(e);if(h.has(e))return h.get(e);const t=y(e);return t!==e&&(h.set(e,t),v.set(t,e)),t}const w=e=>v.get(e);function I(e,t,{blocked:n,upgrade:r,blocking:o,terminated:i}={}){const s=indexedDB.open(e,t),c=b(s);return r&&s.addEventListener("upgradeneeded",(e=>{r(b(s.result),e.oldVersion,e.newVersion,b(s.transaction),e)})),n&&s.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),c.then((e=>{i&&e.addEventListener("close",(()=>i())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}function S(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),b(n).then((()=>{}))}const D=["get","getKey","getAll","getAllKeys","count"],C=["put","add","delete","clear"],_=new Map;function T(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(_.get(t))return _.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=C.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.includes(n))return;const i=async function(e,...t){const i=this.transaction(e,o?"readwrite":"readonly");let s=i.store;return r&&(s=s.index(t.shift())),(await Promise.all([s[n](...t),o&&i.done]))[0]};return _.set(t,i),i}p((e=>({...e,get:(t,n,r)=>T(t,n)||e.get(t,n,r),has:(t,n)=>!!T(t,n)||e.has(t,n)})));const B=["continue","continuePrimaryKey","advance"],E={},R=new WeakMap,x=new WeakMap,F={get(e,t){if(!B.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){R.set(this,x.get(this)[t](...e))}),n}};async function*L(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,F);for(x.set(n,t),v.set(n,w(t));t;)yield n,t=await(R.get(n)||t.continue()),R.delete(n)}function k(e,t){return t===Symbol.asyncIterator&&u(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===t&&u(e,[IDBIndex,IDBObjectStore])}p((e=>({...e,get:(t,n,r)=>k(t,n)?L:e.get(t,n,r),has:(t,n)=>k(t,n)||e.has(t,n)})));var j=function(e,t){return o(void 0,void 0,void 0,(function(){return i(this,(function(n){switch(n.label){case 0:return[4,I(e,1,{upgrade:function(e){t.forEach((function(t){e.objectStoreNames.contains(t)||e.createObjectStore(t)}))}})];case 1:return[2,n.sent()]}}))}))},P=function(e){var t=e.loggerProvider,n=e.apiKey,a=e.configKeys;return o(void 0,void 0,void 0,(function(){var e,u,l,d,f,h,v,g,p,m;return i(this,(function(y){switch(y.label){case 0:return e="".concat(n.substring(0,10),"_amp_config"),[4,j(e,a)];case 1:return u=y.sent(),l="".concat(n.substring(0,10),"_amp_config_meta"),[4,(b=l,o(void 0,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return[4,I(b,1,{upgrade:function(e){e.objectStoreNames.contains("lastFetchedSessionId")||e.createObjectStore("lastFetchedSessionId")}})];case 1:return[2,e.sent()]}}))})))];case 2:d=y.sent(),y.label=3;case 3:return y.trys.push([3,8,,9]),[4,d.get("lastFetchedSessionId","sessionId")];case 4:return(f=y.sent())&&Date.now()-f>=2592e5?(u.close(),[4,S(e)]):[3,7];case 5:return y.sent(),[4,j(n,a)];case 6:u=y.sent(),y.label=7;case 7:return[3,9];case 8:return h=y.sent(),t.warn("Failed to reset store: ".concat(h)),[3,9];case 9:return v=function(e,n){return o(void 0,void 0,void 0,(function(){var o,a,l,f,h,v;return i(this,(function(g){switch(g.label){case 0:return g.trys.push([0,7,,8]),n?[4,d.put("lastFetchedSessionId",n,"sessionId")]:[3,2];case 1:g.sent(),g.label=2;case 2:for(f in o=function(t){var n,o;return i(this,(function(i){switch(i.label){case 0:return n=e.configs[t],o=u.transaction(t,"readwrite"),[4,Promise.all(c(c([],s(Object.keys(n).map((function(e){return o.store.put(r({},n[e]),e)}))),!1),[o.done],!1))];case 1:return i.sent(),[2]}}))},a=e.configs,l=[],a)l.push(f);h=0,g.label=3;case 3:return h<l.length?(f=l[h])in a?[5,o(f)]:[3,5]:[3,6];case 4:g.sent(),g.label=5;case 5:return h++,[3,3];case 6:return[3,8];case 7:return v=g.sent(),t.warn("Failed to store remote config: ".concat(v)),[3,8];case 8:return[2]}}))}))},g=function(){return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,d.get("lastFetchedSessionId","sessionId")];case 1:return[2,n.sent()];case 2:return e=n.sent(),t.warn("Failed to fetch lastFetchedSessionId: ".concat(e)),[3,3];case 3:return[2,void 0]}}))}))},p=function(e,n){return o(void 0,void 0,void 0,(function(){var r;return i(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,u.get(e,n)];case 1:return[2,o.sent()];case 2:return r=o.sent(),t.warn("Failed to fetch remote config: ".concat(r)),[3,3];case 3:return[2,void 0]}}))}))},m=function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,u.getAll(e)];case 1:return[2,!!r.sent().length];case 2:return n=r.sent(),t.warn("Failed to fetch remote config: ".concat(n)),[3,3];case 3:return[2,!1]}}))}))},[2,{storeRemoteConfig:v,getRemoteConfig:p,getLastFetchedSessionId:g,remoteConfigHasValues:m}]}var b}))}))},K="Remote config fetch rejected due to timeout after 5 seconds",A=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.fetchTime=0,this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a,u;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.remoteConfigHasValues(e)]:[3,4];case 1:return o=i.sent(),[4,this.remoteConfigIDBStore.getLastFetchedSessionId()];case 2:return s=i.sent(),o&&s===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,4];case 3:return c=i.sent(),this.fetchTime=Date.now()-r,[2,c];case 4:return[4,this.fetchWithTimeout(n)];case 5:return(a=i.sent())&&(u=a.configs&&a.configs[e])?(this.fetchTime=Date.now()-r,[2,u[t]]):(this.fetchTime=Date.now()-r,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return o(c,void 0,void 0,(function(){var t,n,r;return i(this,(function(o){switch(o.label){case 0:return t=new AbortController,n=setTimeout((function(){return t.abort()}),5e3),[4,this.fetchRemoteConfig(t.signal,e)];case 1:return r=o.sent(),clearTimeout(n),[2,r]}}))}))},this.fetchRemoteConfig=function(e,t){return o(c,void 0,void 0,(function(){var o,s,c,u,l,d;return i(this,(function(i){switch(i.label){case 0:if(t===this.lastFetchedSessionId&&this.attempts>=this.localConfig.flushMaxRetries)return[2,this.completeRequest({err:"Remote config fetch rejected due to exceeded retry count"})];if(e.aborted)return[2,this.completeRequest({err:K})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),i.label=1;case 1:return i.trys.push([1,3,,4]),o=new URLSearchParams({api_key:this.localConfig.apiKey,config_keys:this.configKeys.join(",")}),s={headers:{"Content-Type":"application/json",Accept:"*/*"},method:"GET"},c="".concat(this.getServerUrl(),"?").concat(o.toString()),this.attempts+=1,[4,fetch(c,r(r({},s),{signal:e}))];case 2:if(null===(u=i.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new a).buildStatus(u.status)){case n.Success:return this.attempts=0,[2,this.parseAndStoreConfig(u,t)];case n.Failed:return[2,this.retryFetch(e,t)];default:return[2,this.completeRequest({err:"Network error occurred, remote config fetch failed"})]}case 3:return l=i.sent(),d=l,e.aborted?[2,this.completeRequest({err:K})]:[2,this.completeRequest({err:d.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return o(c,void 0,void 0,(function(){var n=this;return i(this,(function(r){switch(r.label){case 0:return[4,new Promise((function(e){return setTimeout(e,n.attempts*n.retryTimeout)}))];case 1:return r.sent(),[2,this.fetchRemoteConfig(e,t)]}}))}))},this.parseAndStoreConfig=function(e,t){return o(c,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return[4,e.json()];case 1:return n=r.sent(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.storeRemoteConfig(n,t)]:[3,3];case 2:r.sent(),r.label=3;case 3:return this.completeRequest({success:"Remote config successfully fetched"}),[2,n]}}))}))},this.localConfig=t,this.configKeys=s}return e.prototype.initialize=function(){return o(this,void 0,void 0,(function(){var e;return i(this,(function(t){switch(t.label){case 0:return e=this,[4,P({apiKey:this.localConfig.apiKey,loggerProvider:this.localConfig.loggerProvider,configKeys:this.configKeys})];case 1:return e.remoteConfigIDBStore=t.sent(),[2]}}))}))},e.prototype.getServerUrl=function(){return this.localConfig.serverZone===t.STAGING?"https://sr-client-cfg.stag2.amplitude.com/config":this.localConfig.serverZone===t.EU?"https://sr-client-cfg.eu.amplitude.com/config":"https://sr-client-cfg.amplitude.com/config"},e.prototype.completeRequest=function(e){var t=e.err,n=e.success;if(t)throw new Error(t);n&&this.localConfig.loggerProvider.log(n)},e}(),U=function(e){var t=e.localConfig,n=e.configKeys;return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return[4,(e=new A({localConfig:t,configKeys:n})).initialize()];case 1:return r.sent(),[2,e]}}))}))};return e.createRemoteConfigFetch=U,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).amplitude={})}(this,(function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function s(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}!function(e){e.US="US",e.EU="EU",e.STAGING="STAGING"}(t||(t={})),function(e){e.Unknown="unknown",e.Skipped="skipped",e.Success="success",e.RateLimit="rate_limit",e.PayloadTooLarge="payload_too_large",e.Invalid="invalid",e.Failed="failed",e.Timeout="Timeout",e.SystemError="SystemError"}(n||(n={}));var a=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,r,o,i,s,c,a,u,l,d,f,h,v,p,g,y,m,b,w,I,S,D;if("object"!=typeof e)return null;var _=e.code||0,C=this.buildStatus(_);switch(C){case n.Success:return{status:C,statusCode:_,body:{eventsIngested:null!==(t=e.events_ingested)&&void 0!==t?t:0,payloadSizeBytes:null!==(r=e.payload_size_bytes)&&void 0!==r?r:0,serverUploadTime:null!==(o=e.server_upload_time)&&void 0!==o?o:0}};case n.Invalid:return{status:C,statusCode:_,body:{error:null!==(i=e.error)&&void 0!==i?i:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(c=e.events_with_invalid_fields)&&void 0!==c?c:{},eventsWithMissingFields:null!==(a=e.events_with_missing_fields)&&void 0!==a?a:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(l=e.eps_threshold)&&void 0!==l?l:0,exceededDailyQuotaDevices:null!==(d=e.exceeded_daily_quota_devices)&&void 0!==d?d:{},silencedDevices:null!==(f=e.silenced_devices)&&void 0!==f?f:[],silencedEvents:null!==(h=e.silenced_events)&&void 0!==h?h:[],throttledDevices:null!==(v=e.throttled_devices)&&void 0!==v?v:{},throttledEvents:null!==(p=e.throttled_events)&&void 0!==p?p:[]}};case n.PayloadTooLarge:return{status:C,statusCode:_,body:{error:null!==(g=e.error)&&void 0!==g?g:""}};case n.RateLimit:return{status:C,statusCode:_,body:{error:null!==(y=e.error)&&void 0!==y?y:"",epsThreshold:null!==(m=e.eps_threshold)&&void 0!==m?m:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(w=e.throttled_users)&&void 0!==w?w:{},exceededDailyQuotaDevices:null!==(I=e.exceeded_daily_quota_devices)&&void 0!==I?I:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(D=e.throttled_events)&&void 0!==D?D:[]}};case n.Timeout:default:return{status:C,statusCode:_}}},e.prototype.buildStatus=function(e){return e>=200&&e<300?n.Success:429===e?n.RateLimit:413===e?n.PayloadTooLarge:408===e?n.Timeout:e>=400&&e<500?n.Invalid:e>=500?n.Failed:n.Unknown},e}();const u=(e,t)=>t.some((t=>e instanceof t));let l,d;const f=new WeakMap,h=new WeakMap,v=new WeakMap;let p={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return f.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return b(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function g(e){p=e(p)}function y(e){return(d||(d=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(w(this),t),b(this.request)}:function(...t){return b(e.apply(w(this),t))}}function m(e){return"function"==typeof e?y(e):(e instanceof IDBTransaction&&function(e){if(f.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",i),e.removeEventListener("abort",i)},o=()=>{t(),r()},i=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",i),e.addEventListener("abort",i)}));f.set(e,t)}(e),u(e,l||(l=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,p):e)}function b(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",i)},o=()=>{t(b(e.result)),r()},i=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",i)}));return v.set(t,e),t}(e);if(h.has(e))return h.get(e);const t=m(e);return t!==e&&(h.set(e,t),v.set(t,e)),t}const w=e=>v.get(e);function I(e,t,{blocked:n,upgrade:r,blocking:o,terminated:i}={}){const s=indexedDB.open(e,t),c=b(s);return r&&s.addEventListener("upgradeneeded",(e=>{r(b(s.result),e.oldVersion,e.newVersion,b(s.transaction),e)})),n&&s.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),c.then((e=>{i&&e.addEventListener("close",(()=>i())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}function S(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),b(n).then((()=>{}))}const D=["get","getKey","getAll","getAllKeys","count"],_=["put","add","delete","clear"],C=new Map;function T(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(C.get(t))return C.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=_.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.includes(n))return;const i=async function(e,...t){const i=this.transaction(e,o?"readwrite":"readonly");let s=i.store;return r&&(s=s.index(t.shift())),(await Promise.all([s[n](...t),o&&i.done]))[0]};return C.set(t,i),i}g((e=>({...e,get:(t,n,r)=>T(t,n)||e.get(t,n,r),has:(t,n)=>!!T(t,n)||e.has(t,n)})));const B=["continue","continuePrimaryKey","advance"],E={},x=new WeakMap,R=new WeakMap,F={get(e,t){if(!B.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){x.set(this,R.get(this)[t](...e))}),n}};async function*L(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,F);for(R.set(n,t),v.set(n,w(t));t;)yield n,t=await(x.get(n)||t.continue()),x.delete(n)}function k(e,t){return t===Symbol.asyncIterator&&u(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===t&&u(e,[IDBIndex,IDBObjectStore])}g((e=>({...e,get:(t,n,r)=>k(t,n)?L:e.get(t,n,r),has:(t,n)=>k(t,n)||e.has(t,n)})));var j=function(e,t){return o(void 0,void 0,void 0,(function(){return i(this,(function(n){switch(n.label){case 0:return[4,I(e,1,{upgrade:function(e){t.forEach((function(t){e.objectStoreNames.contains(t)||e.createObjectStore(t)}))}})];case 1:return[2,n.sent()]}}))}))},P=function(e){var t=e.loggerProvider,n=e.apiKey,a=e.configKeys;return o(void 0,void 0,void 0,(function(){var e,u,l,d,f,h,v,p,g,y;return i(this,(function(m){switch(m.label){case 0:return e="".concat(n.substring(0,10),"_amp_config"),[4,j(e,a)];case 1:return u=m.sent(),l="".concat(n.substring(0,10),"_amp_config_meta"),[4,(b=l,o(void 0,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return[4,I(b,1,{upgrade:function(e){e.objectStoreNames.contains("lastFetchedSessionId")||e.createObjectStore("lastFetchedSessionId")}})];case 1:return[2,e.sent()]}}))})))];case 2:d=m.sent(),m.label=3;case 3:return m.trys.push([3,8,,9]),[4,d.get("lastFetchedSessionId","sessionId")];case 4:return(f=m.sent())&&Date.now()-f>=2592e5?(u.close(),[4,S(e)]):[3,7];case 5:return m.sent(),[4,j(n,a)];case 6:u=m.sent(),m.label=7;case 7:return[3,9];case 8:return h=m.sent(),t.warn("Failed to reset store: ".concat(h)),[3,9];case 9:return v=function(e,n){return o(void 0,void 0,void 0,(function(){var o,a,l,f,h,v;return i(this,(function(p){switch(p.label){case 0:return p.trys.push([0,7,,8]),n?[4,d.put("lastFetchedSessionId",n,"sessionId")]:[3,2];case 1:p.sent(),p.label=2;case 2:for(f in o=function(t){var n,o;return i(this,(function(i){switch(i.label){case 0:return n=e.configs[t],o=u.transaction(t,"readwrite"),[4,Promise.all(c(c([],s(Object.keys(n).map((function(e){return o.store.put(r({},n[e]),e)}))),!1),[o.done],!1))];case 1:return i.sent(),[2]}}))},a=e.configs,l=[],a)l.push(f);h=0,p.label=3;case 3:return h<l.length?(f=l[h])in a?[5,o(f)]:[3,5]:[3,6];case 4:p.sent(),p.label=5;case 5:return h++,[3,3];case 6:return[3,8];case 7:return v=p.sent(),t.warn("Failed to store remote config: ".concat(v)),[3,8];case 8:return[2]}}))}))},p=function(){return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,d.get("lastFetchedSessionId","sessionId")];case 1:return[2,n.sent()];case 2:return e=n.sent(),t.warn("Failed to fetch lastFetchedSessionId: ".concat(e)),[3,3];case 3:return[2,void 0]}}))}))},g=function(e,n){return o(void 0,void 0,void 0,(function(){var r;return i(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,u.get(e,n)];case 1:return[2,o.sent()];case 2:return r=o.sent(),t.warn("Failed to fetch remote config: ".concat(r)),[3,3];case 3:return[2,void 0]}}))}))},y=function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,u.getAll(e)];case 1:return[2,!!r.sent().length];case 2:return n=r.sent(),t.warn("Failed to fetch remote config: ".concat(n)),[3,3];case 3:return[2,!1]}}))}))},[2,{storeRemoteConfig:v,getRemoteConfig:g,getLastFetchedSessionId:p,remoteConfigHasValues:y}]}var b}))}))},K="Remote config fetch rejected due to timeout after 5 seconds",A=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.fetchTime=0,this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a,u;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.remoteConfigHasValues(e)]:[3,4];case 1:return o=i.sent(),[4,this.remoteConfigIDBStore.getLastFetchedSessionId()];case 2:return s=i.sent(),o&&s===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,4];case 3:return c=i.sent(),this.fetchTime=Date.now()-r,[2,c];case 4:return[4,this.fetchWithTimeout(n)];case 5:return(a=i.sent())&&(u=a.configs&&a.configs[e])?(this.fetchTime=Date.now()-r,[2,u[t]]):(this.fetchTime=Date.now()-r,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return o(c,void 0,void 0,(function(){var t,n,r;return i(this,(function(o){switch(o.label){case 0:return t=new AbortController,n=setTimeout((function(){return t.abort()}),5e3),[4,this.fetchRemoteConfig(t.signal,e)];case 1:return r=o.sent(),clearTimeout(n),[2,r]}}))}))},this.fetchRemoteConfig=function(e,t){return o(c,void 0,void 0,(function(){var o,s,c,u,l,d;return i(this,(function(i){switch(i.label){case 0:if(t===this.lastFetchedSessionId&&this.attempts>=this.localConfig.flushMaxRetries)return[2,this.completeRequest({err:"Remote config fetch rejected due to exceeded retry count"})];if(e.aborted)return[2,this.completeRequest({err:K})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),i.label=1;case 1:return i.trys.push([1,3,,4]),o=new URLSearchParams({api_key:this.localConfig.apiKey,config_keys:this.configKeys.join(",")}),s={headers:{"Content-Type":"application/json",Accept:"*/*"},method:"GET"},c="".concat(this.getServerUrl(),"?").concat(o.toString()),this.attempts+=1,[4,fetch(c,r(r({},s),{signal:e}))];case 2:if(null===(u=i.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new a).buildStatus(u.status)){case n.Success:return[2,this.parseAndStoreConfig(u,t)];case n.Failed:return[2,this.retryFetch(e,t)];default:return[2,this.completeRequest({err:"Network error occurred, remote config fetch failed"})]}case 3:return l=i.sent(),d=l,e.aborted?[2,this.completeRequest({err:K})]:[2,this.completeRequest({err:d.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return o(c,void 0,void 0,(function(){var n=this;return i(this,(function(r){switch(r.label){case 0:return[4,new Promise((function(e){return setTimeout(e,n.attempts*n.retryTimeout)}))];case 1:return r.sent(),[2,this.fetchRemoteConfig(e,t)]}}))}))},this.parseAndStoreConfig=function(e,t){return o(c,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return[4,e.json()];case 1:return n=r.sent(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.storeRemoteConfig(n,t)]:[3,3];case 2:r.sent(),r.label=3;case 3:return this.completeRequest({success:"Remote config successfully fetched"}),[2,n]}}))}))},this.localConfig=t,this.configKeys=s}return e.prototype.initialize=function(){return o(this,void 0,void 0,(function(){var e;return i(this,(function(t){switch(t.label){case 0:return e=this,[4,P({apiKey:this.localConfig.apiKey,loggerProvider:this.localConfig.loggerProvider,configKeys:this.configKeys})];case 1:return e.remoteConfigIDBStore=t.sent(),[2]}}))}))},e.prototype.getServerUrl=function(){return this.localConfig.serverZone===t.STAGING?"https://sr-client-cfg.stag2.amplitude.com/config":this.localConfig.serverZone===t.EU?"https://sr-client-cfg.eu.amplitude.com/config":"https://sr-client-cfg.amplitude.com/config"},e.prototype.completeRequest=function(e){var t=e.err,n=e.success;if(t)throw new Error(t);n&&this.localConfig.loggerProvider.log(n)},e}(),U=function(e){var t=e.localConfig,n=e.configKeys;return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return[4,(e=new A({localConfig:t,configKeys:n})).initialize()];case 1:return r.sent(),[2,e]}}))}))};e.createRemoteConfigFetch=U,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).amplitude={})}(this,(function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function s(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}!function(e){e.US="US",e.EU="EU",e.STAGING="STAGING"}(t||(t={})),function(e){e.Unknown="unknown",e.Skipped="skipped",e.Success="success",e.RateLimit="rate_limit",e.PayloadTooLarge="payload_too_large",e.Invalid="invalid",e.Failed="failed",e.Timeout="Timeout",e.SystemError="SystemError"}(n||(n={}));var a=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,r,o,i,s,c,a,u,l,d,f,h,v,p,g,m,y,b,w,I,S,D;if("object"!=typeof e)return null;var _=e.code||0,C=this.buildStatus(_);switch(C){case n.Success:return{status:C,statusCode:_,body:{eventsIngested:null!==(t=e.events_ingested)&&void 0!==t?t:0,payloadSizeBytes:null!==(r=e.payload_size_bytes)&&void 0!==r?r:0,serverUploadTime:null!==(o=e.server_upload_time)&&void 0!==o?o:0}};case n.Invalid:return{status:C,statusCode:_,body:{error:null!==(i=e.error)&&void 0!==i?i:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(c=e.events_with_invalid_fields)&&void 0!==c?c:{},eventsWithMissingFields:null!==(a=e.events_with_missing_fields)&&void 0!==a?a:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(l=e.eps_threshold)&&void 0!==l?l:0,exceededDailyQuotaDevices:null!==(d=e.exceeded_daily_quota_devices)&&void 0!==d?d:{},silencedDevices:null!==(f=e.silenced_devices)&&void 0!==f?f:[],silencedEvents:null!==(h=e.silenced_events)&&void 0!==h?h:[],throttledDevices:null!==(v=e.throttled_devices)&&void 0!==v?v:{},throttledEvents:null!==(p=e.throttled_events)&&void 0!==p?p:[]}};case n.PayloadTooLarge:return{status:C,statusCode:_,body:{error:null!==(g=e.error)&&void 0!==g?g:""}};case n.RateLimit:return{status:C,statusCode:_,body:{error:null!==(m=e.error)&&void 0!==m?m:"",epsThreshold:null!==(y=e.eps_threshold)&&void 0!==y?y:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(w=e.throttled_users)&&void 0!==w?w:{},exceededDailyQuotaDevices:null!==(I=e.exceeded_daily_quota_devices)&&void 0!==I?I:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(D=e.throttled_events)&&void 0!==D?D:[]}};case n.Timeout:default:return{status:C,statusCode:_}}},e.prototype.buildStatus=function(e){return e>=200&&e<300?n.Success:429===e?n.RateLimit:413===e?n.PayloadTooLarge:408===e?n.Timeout:e>=400&&e<500?n.Invalid:e>=500?n.Failed:n.Unknown},e}();const u=(e,t)=>t.some((t=>e instanceof t));let l,d;const f=new WeakMap,h=new WeakMap,v=new WeakMap;let p={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return f.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return b(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function g(e){p=e(p)}function m(e){return(d||(d=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(w(this),t),b(this.request)}:function(...t){return b(e.apply(w(this),t))}}function y(e){return"function"==typeof e?m(e):(e instanceof IDBTransaction&&function(e){if(f.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",i),e.removeEventListener("abort",i)},o=()=>{t(),r()},i=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",i),e.addEventListener("abort",i)}));f.set(e,t)}(e),u(e,l||(l=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,p):e)}function b(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",i)},o=()=>{t(b(e.result)),r()},i=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",i)}));return v.set(t,e),t}(e);if(h.has(e))return h.get(e);const t=y(e);return t!==e&&(h.set(e,t),v.set(t,e)),t}const w=e=>v.get(e);function I(e,t,{blocked:n,upgrade:r,blocking:o,terminated:i}={}){const s=indexedDB.open(e,t),c=b(s);return r&&s.addEventListener("upgradeneeded",(e=>{r(b(s.result),e.oldVersion,e.newVersion,b(s.transaction),e)})),n&&s.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),c.then((e=>{i&&e.addEventListener("close",(()=>i())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}function S(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),b(n).then((()=>{}))}const D=["get","getKey","getAll","getAllKeys","count"],_=["put","add","delete","clear"],C=new Map;function T(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(C.get(t))return C.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=_.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.includes(n))return;const i=async function(e,...t){const i=this.transaction(e,o?"readwrite":"readonly");let s=i.store;return r&&(s=s.index(t.shift())),(await Promise.all([s[n](...t),o&&i.done]))[0]};return C.set(t,i),i}g((e=>({...e,get:(t,n,r)=>T(t,n)||e.get(t,n,r),has:(t,n)=>!!T(t,n)||e.has(t,n)})));const B=["continue","continuePrimaryKey","advance"],E={},x=new WeakMap,R=new WeakMap,F={get(e,t){if(!B.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){x.set(this,R.get(this)[t](...e))}),n}};async function*L(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,F);for(R.set(n,t),v.set(n,w(t));t;)yield n,t=await(x.get(n)||t.continue()),x.delete(n)}function k(e,t){return t===Symbol.asyncIterator&&u(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===t&&u(e,[IDBIndex,IDBObjectStore])}g((e=>({...e,get:(t,n,r)=>k(t,n)?L:e.get(t,n,r),has:(t,n)=>k(t,n)||e.has(t,n)})));var j=function(e,t){return o(void 0,void 0,void 0,(function(){return i(this,(function(n){switch(n.label){case 0:return[4,I(e,1,{upgrade:function(e){t.forEach((function(t){e.objectStoreNames.contains(t)||e.createObjectStore(t)}))}})];case 1:return[2,n.sent()]}}))}))},P=function(e){var t=e.loggerProvider,n=e.apiKey,a=e.configKeys;return o(void 0,void 0,void 0,(function(){var e,u,l,d,f,h,v,p,g,m;return i(this,(function(y){switch(y.label){case 0:return e="".concat(n.substring(0,10),"_amp_config"),[4,j(e,a)];case 1:return u=y.sent(),l="".concat(n.substring(0,10),"_amp_config_meta"),[4,(b=l,o(void 0,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return[4,I(b,1,{upgrade:function(e){e.objectStoreNames.contains("lastFetchedSessionId")||e.createObjectStore("lastFetchedSessionId")}})];case 1:return[2,e.sent()]}}))})))];case 2:d=y.sent(),y.label=3;case 3:return y.trys.push([3,8,,9]),[4,d.get("lastFetchedSessionId","sessionId")];case 4:return(f=y.sent())&&Date.now()-f>=2592e5?(u.close(),[4,S(e)]):[3,7];case 5:return y.sent(),[4,j(n,a)];case 6:u=y.sent(),y.label=7;case 7:return[3,9];case 8:return h=y.sent(),t.warn("Failed to reset store: ".concat(h)),[3,9];case 9:return v=function(e,n){return o(void 0,void 0,void 0,(function(){var o,a,l,f,h,v;return i(this,(function(p){switch(p.label){case 0:return p.trys.push([0,7,,8]),n?[4,d.put("lastFetchedSessionId",n,"sessionId")]:[3,2];case 1:p.sent(),p.label=2;case 2:for(f in o=function(t){var n,o;return i(this,(function(i){switch(i.label){case 0:return n=e.configs[t],o=u.transaction(t,"readwrite"),[4,Promise.all(c(c([],s(Object.keys(n).map((function(e){return o.store.put(r({},n[e]),e)}))),!1),[o.done],!1))];case 1:return i.sent(),[2]}}))},a=e.configs,l=[],a)l.push(f);h=0,p.label=3;case 3:return h<l.length?(f=l[h])in a?[5,o(f)]:[3,5]:[3,6];case 4:p.sent(),p.label=5;case 5:return h++,[3,3];case 6:return[3,8];case 7:return v=p.sent(),t.warn("Failed to store remote config: ".concat(v)),[3,8];case 8:return[2]}}))}))},p=function(){return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,d.get("lastFetchedSessionId","sessionId")];case 1:return[2,n.sent()];case 2:return e=n.sent(),t.warn("Failed to fetch lastFetchedSessionId: ".concat(e)),[3,3];case 3:return[2,void 0]}}))}))},g=function(e,n){return o(void 0,void 0,void 0,(function(){var r;return i(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,u.get(e,n)];case 1:return[2,o.sent()];case 2:return r=o.sent(),t.warn("Failed to fetch remote config: ".concat(r)),[3,3];case 3:return[2,void 0]}}))}))},m=function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,u.getAll(e)];case 1:return[2,!!r.sent().length];case 2:return n=r.sent(),t.warn("Failed to fetch remote config: ".concat(n)),[3,3];case 3:return[2,!1]}}))}))},[2,{storeRemoteConfig:v,getRemoteConfig:g,getLastFetchedSessionId:p,remoteConfigHasValues:m}]}var b}))}))},K="Remote config fetch rejected due to timeout after 5 seconds",A=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.fetchTime=0,this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a,u;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.remoteConfigHasValues(e)]:[3,4];case 1:return o=i.sent(),[4,this.remoteConfigIDBStore.getLastFetchedSessionId()];case 2:return s=i.sent(),o&&s===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,4];case 3:return c=i.sent(),this.fetchTime=Date.now()-r,[2,c];case 4:return[4,this.fetchWithTimeout(n)];case 5:return(a=i.sent())&&(u=a.configs&&a.configs[e])?(this.fetchTime=Date.now()-r,[2,u[t]]):(this.fetchTime=Date.now()-r,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return o(c,void 0,void 0,(function(){var t,n,r;return i(this,(function(o){switch(o.label){case 0:return t=new AbortController,n=setTimeout((function(){return t.abort()}),5e3),[4,this.fetchRemoteConfig(t.signal,e)];case 1:return r=o.sent(),clearTimeout(n),[2,r]}}))}))},this.fetchRemoteConfig=function(e,t){return o(c,void 0,void 0,(function(){var o,s,c,u,l,d;return i(this,(function(i){switch(i.label){case 0:if(t===this.lastFetchedSessionId&&this.attempts>=this.localConfig.flushMaxRetries)return[2,this.completeRequest({err:"Remote config fetch rejected due to exceeded retry count"})];if(e.aborted)return[2,this.completeRequest({err:K})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),i.label=1;case 1:return i.trys.push([1,3,,4]),o=new URLSearchParams({api_key:this.localConfig.apiKey,config_keys:this.configKeys.join(",")}),s={headers:{"Content-Type":"application/json",Accept:"*/*"},method:"GET"},c="".concat(this.getServerUrl(),"?").concat(o.toString()),this.attempts+=1,[4,fetch(c,r(r({},s),{signal:e}))];case 2:if(null===(u=i.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new a).buildStatus(u.status)){case n.Success:return this.attempts=0,[2,this.parseAndStoreConfig(u,t)];case n.Failed:return[2,this.retryFetch(e,t)];default:return[2,this.completeRequest({err:"Network error occurred, remote config fetch failed"})]}case 3:return l=i.sent(),d=l,e.aborted?[2,this.completeRequest({err:K})]:[2,this.completeRequest({err:d.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return o(c,void 0,void 0,(function(){var n=this;return i(this,(function(r){switch(r.label){case 0:return[4,new Promise((function(e){return setTimeout(e,n.attempts*n.retryTimeout)}))];case 1:return r.sent(),[2,this.fetchRemoteConfig(e,t)]}}))}))},this.parseAndStoreConfig=function(e,t){return o(c,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:return[4,e.json()];case 1:return n=r.sent(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.storeRemoteConfig(n,t)]:[3,3];case 2:r.sent(),r.label=3;case 3:return this.completeRequest({success:"Remote config successfully fetched"}),[2,n]}}))}))},this.localConfig=t,this.configKeys=s}return e.prototype.initialize=function(){return o(this,void 0,void 0,(function(){var e;return i(this,(function(t){switch(t.label){case 0:return e=this,[4,P({apiKey:this.localConfig.apiKey,loggerProvider:this.localConfig.loggerProvider,configKeys:this.configKeys})];case 1:return e.remoteConfigIDBStore=t.sent(),[2]}}))}))},e.prototype.getServerUrl=function(){return this.localConfig.serverZone===t.STAGING?"https://sr-client-cfg.stag2.amplitude.com/config":this.localConfig.serverZone===t.EU?"https://sr-client-cfg.eu.amplitude.com/config":"https://sr-client-cfg.amplitude.com/config"},e.prototype.completeRequest=function(e){var t=e.err,n=e.success;if(t)throw new Error(t);n&&this.localConfig.loggerProvider.log(n)},e}(),U=function(e){var t=e.localConfig,n=e.configKeys;return o(void 0,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return[4,(e=new A({localConfig:t,configKeys:n})).initialize()];case 1:return r.sent(),[2,e]}}))}))};e.createRemoteConfigFetch=U,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@amplitude/analytics-remote-config", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "", | ||
@@ -61,3 +61,3 @@ "author": "Amplitude Inc", | ||
], | ||
"gitHead": "7b525f83f34f0a03e92795492bc0a08182e4359e" | ||
"gitHead": "a009dfa144d0b616326c9a26a8348ee5678f581a" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155158
1187