@amplitude/analytics-remote-config
Advanced tools
Comparing version 0.3.5 to 0.4.0
import { Config } from '@amplitude/analytics-types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigIDBStore, RemoteConfigMetric } from './types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigMetric } from './types'; | ||
export declare const REMOTE_CONFIG_SERVER_URL = "https://sr-client-cfg.amplitude.com/config"; | ||
@@ -10,3 +10,2 @@ export declare const REMOTE_CONFIG_SERVER_URL_STAGING = "https://sr-client-cfg.stag2.amplitude.com/config"; | ||
localConfig: Config; | ||
remoteConfigIDBStore: RemoteConfigIDBStore<RemoteConfig> | undefined; | ||
retryTimeout: number; | ||
@@ -22,3 +21,2 @@ attempts: number; | ||
}); | ||
initialize(): Promise<void>; | ||
getRemoteConfig: <K extends keyof RemoteConfig>(configNamespace: string, key: K, sessionId?: number) => Promise<RemoteConfig[K] | undefined>; | ||
@@ -29,3 +27,3 @@ getServerUrl(): "https://sr-client-cfg.amplitude.com/config" | "https://sr-client-cfg.stag2.amplitude.com/config" | "https://sr-client-cfg.eu.amplitude.com/config"; | ||
retryFetch: (signal: AbortController['signal'], sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig> | void>; | ||
parseAndStoreConfig: (res: Response, sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
parseAndStoreConfig: (res: Response) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
completeRequest({ err, success }: { | ||
@@ -32,0 +30,0 @@ err?: string; |
@@ -7,3 +7,2 @@ var _this = this; | ||
var analytics_types_1 = require("@amplitude/analytics-types"); | ||
var RemoteConfigAPIStore = tslib_1.__importStar(require("./remote-config-idb-store")); | ||
var UNEXPECTED_NETWORK_ERROR_MESSAGE = 'Network error occurred, remote config fetch failed'; | ||
@@ -26,3 +25,3 @@ var SUCCESS_REMOTE_CONFIG = 'Remote config successfully fetched'; | ||
this.getRemoteConfig = function (configNamespace, key, sessionId) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var fetchStartTime, lastFetchedSessionId, idbRemoteConfig, configAPIResponse, remoteConfig; | ||
var fetchStartTime, configAPIResponse, remoteConfig; | ||
return tslib_1.__generator(this, function (_a) { | ||
@@ -32,14 +31,4 @@ switch (_a.label) { | ||
fetchStartTime = Date.now(); | ||
if (!this.remoteConfigIDBStore) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.getLastFetchedSessionId()]; | ||
return [4 /*yield*/, this.fetchWithTimeout(sessionId)]; | ||
case 1: | ||
lastFetchedSessionId = _a.sent(); | ||
if (!(!!lastFetchedSessionId && !!sessionId && lastFetchedSessionId === sessionId)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.getRemoteConfig(configNamespace, key)]; | ||
case 2: | ||
idbRemoteConfig = _a.sent(); | ||
this.metrics.fetchTimeIDB = Date.now() - fetchStartTime; | ||
return [2 /*return*/, idbRemoteConfig]; | ||
case 3: return [4 /*yield*/, this.fetchWithTimeout(sessionId)]; | ||
case 4: | ||
configAPIResponse = _a.sent(); | ||
@@ -74,5 +63,6 @@ if (configAPIResponse) { | ||
this.fetchRemoteConfig = function (signal, sessionId) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var urlParams, options, serverUrl, res, parsedStatus, e_1, knownError; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
var urlParams, _a, _b, configKey, options, serverUrl, res, parsedStatus, e_1, knownError; | ||
var e_2, _c; | ||
return tslib_1.__generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
@@ -89,12 +79,26 @@ if (sessionId === this.lastFetchedSessionId && this.attempts >= this.localConfig.flushMaxRetries) { | ||
} | ||
_a.label = 1; | ||
_d.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
_d.trys.push([1, 3, , 4]); | ||
urlParams = new URLSearchParams({ | ||
api_key: this.localConfig.apiKey, | ||
config_keys: this.configKeys.join(','), | ||
}); | ||
try { | ||
for (_a = tslib_1.__values(this.configKeys), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
configKey = _b.value; | ||
urlParams.append('config_keys', configKey); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
if (sessionId) { | ||
urlParams.set('session_id', String(sessionId)); | ||
} | ||
options = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: '*/*', | ||
@@ -108,3 +112,3 @@ }, | ||
case 2: | ||
res = _a.sent(); | ||
res = _d.sent(); | ||
if (res === null) { | ||
@@ -117,3 +121,3 @@ return [2 /*return*/, this.completeRequest({ err: UNEXPECTED_ERROR_MESSAGE })]; | ||
this.attempts = 0; | ||
return [2 /*return*/, this.parseAndStoreConfig(res, sessionId)]; | ||
return [2 /*return*/, this.parseAndStoreConfig(res)]; | ||
case analytics_types_1.Status.Failed: | ||
@@ -126,3 +130,3 @@ return [2 /*return*/, this.retryFetch(signal, sessionId)]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
e_1 = _d.sent(); | ||
knownError = e_1; | ||
@@ -148,17 +152,9 @@ if (signal.aborted) { | ||
}); }; | ||
this.parseAndStoreConfig = function (res, sessionId) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var remoteConfig, _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
this.parseAndStoreConfig = function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var remoteConfig; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, res.json()]; | ||
case 1: | ||
remoteConfig = (_b.sent()); | ||
_a = this.remoteConfigIDBStore; | ||
if (!_a) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.storeRemoteConfig(remoteConfig, sessionId)]; | ||
case 2: | ||
_a = (_b.sent()); | ||
_b.label = 3; | ||
case 3: | ||
_a; | ||
remoteConfig = (_a.sent()); | ||
this.completeRequest({ success: SUCCESS_REMOTE_CONFIG }); | ||
@@ -172,21 +168,2 @@ return [2 /*return*/, remoteConfig]; | ||
} | ||
RemoteConfigFetch.prototype.initialize = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = this; | ||
return [4 /*yield*/, RemoteConfigAPIStore.createRemoteConfigIDBStore({ | ||
apiKey: this.localConfig.apiKey, | ||
loggerProvider: this.localConfig.loggerProvider, | ||
configKeys: this.configKeys, | ||
})]; | ||
case 1: | ||
_a.remoteConfigIDBStore = _b.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RemoteConfigFetch.prototype.getServerUrl = function () { | ||
@@ -216,12 +193,4 @@ if (this.localConfig.serverZone === analytics_types_1.ServerZone.STAGING) { | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var remoteConfigFetch; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
remoteConfigFetch = new RemoteConfigFetch({ localConfig: localConfig, configKeys: configKeys }); | ||
return [4 /*yield*/, remoteConfigFetch.initialize()]; | ||
case 1: | ||
_b.sent(); | ||
return [2 /*return*/, remoteConfigFetch]; | ||
} | ||
return [2 /*return*/, new RemoteConfigFetch({ localConfig: localConfig, configKeys: configKeys })]; | ||
}); | ||
@@ -228,0 +197,0 @@ }); |
@@ -16,3 +16,2 @@ import { Config } from '@amplitude/analytics-types'; | ||
export interface RemoteConfigMetric { | ||
fetchTimeIDB?: number; | ||
fetchTimeAPISuccess?: number; | ||
@@ -19,0 +18,0 @@ fetchTimeAPIFail?: number; |
import { Config } from '@amplitude/analytics-types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigIDBStore, RemoteConfigMetric } from './types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigMetric } from './types'; | ||
export declare const REMOTE_CONFIG_SERVER_URL = "https://sr-client-cfg.amplitude.com/config"; | ||
@@ -10,3 +10,2 @@ export declare const REMOTE_CONFIG_SERVER_URL_STAGING = "https://sr-client-cfg.stag2.amplitude.com/config"; | ||
localConfig: Config; | ||
remoteConfigIDBStore: RemoteConfigIDBStore<RemoteConfig> | undefined; | ||
retryTimeout: number; | ||
@@ -22,3 +21,2 @@ attempts: number; | ||
}); | ||
initialize(): Promise<void>; | ||
getRemoteConfig: <K extends keyof RemoteConfig>(configNamespace: string, key: K, sessionId?: number) => Promise<RemoteConfig[K] | undefined>; | ||
@@ -29,3 +27,3 @@ getServerUrl(): "https://sr-client-cfg.amplitude.com/config" | "https://sr-client-cfg.stag2.amplitude.com/config" | "https://sr-client-cfg.eu.amplitude.com/config"; | ||
retryFetch: (signal: AbortController['signal'], sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig> | void>; | ||
parseAndStoreConfig: (res: Response, sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
parseAndStoreConfig: (res: Response) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
completeRequest({ err, success }: { | ||
@@ -32,0 +30,0 @@ err?: string; |
@@ -1,5 +0,4 @@ | ||
import { __assign, __awaiter, __generator } from "tslib"; | ||
import { __assign, __awaiter, __generator, __values } from "tslib"; | ||
import { BaseTransport } from '@amplitude/analytics-core'; | ||
import { ServerZone, Status } from '@amplitude/analytics-types'; | ||
import * as RemoteConfigAPIStore from './remote-config-idb-store'; | ||
var UNEXPECTED_NETWORK_ERROR_MESSAGE = 'Network error occurred, remote config fetch failed'; | ||
@@ -22,3 +21,3 @@ var SUCCESS_REMOTE_CONFIG = 'Remote config successfully fetched'; | ||
this.getRemoteConfig = function (configNamespace, key, sessionId) { return __awaiter(_this, void 0, void 0, function () { | ||
var fetchStartTime, lastFetchedSessionId, idbRemoteConfig, configAPIResponse, remoteConfig; | ||
var fetchStartTime, configAPIResponse, remoteConfig; | ||
return __generator(this, function (_a) { | ||
@@ -28,14 +27,4 @@ switch (_a.label) { | ||
fetchStartTime = Date.now(); | ||
if (!this.remoteConfigIDBStore) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.getLastFetchedSessionId()]; | ||
return [4 /*yield*/, this.fetchWithTimeout(sessionId)]; | ||
case 1: | ||
lastFetchedSessionId = _a.sent(); | ||
if (!(!!lastFetchedSessionId && !!sessionId && lastFetchedSessionId === sessionId)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.getRemoteConfig(configNamespace, key)]; | ||
case 2: | ||
idbRemoteConfig = _a.sent(); | ||
this.metrics.fetchTimeIDB = Date.now() - fetchStartTime; | ||
return [2 /*return*/, idbRemoteConfig]; | ||
case 3: return [4 /*yield*/, this.fetchWithTimeout(sessionId)]; | ||
case 4: | ||
configAPIResponse = _a.sent(); | ||
@@ -70,5 +59,6 @@ if (configAPIResponse) { | ||
this.fetchRemoteConfig = function (signal, sessionId) { return __awaiter(_this, void 0, void 0, function () { | ||
var urlParams, options, serverUrl, res, parsedStatus, e_1, knownError; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var urlParams, _a, _b, configKey, options, serverUrl, res, parsedStatus, e_1, knownError; | ||
var e_2, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
@@ -85,12 +75,26 @@ if (sessionId === this.lastFetchedSessionId && this.attempts >= this.localConfig.flushMaxRetries) { | ||
} | ||
_a.label = 1; | ||
_d.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
_d.trys.push([1, 3, , 4]); | ||
urlParams = new URLSearchParams({ | ||
api_key: this.localConfig.apiKey, | ||
config_keys: this.configKeys.join(','), | ||
}); | ||
try { | ||
for (_a = __values(this.configKeys), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
configKey = _b.value; | ||
urlParams.append('config_keys', configKey); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
if (sessionId) { | ||
urlParams.set('session_id', String(sessionId)); | ||
} | ||
options = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: '*/*', | ||
@@ -104,3 +108,3 @@ }, | ||
case 2: | ||
res = _a.sent(); | ||
res = _d.sent(); | ||
if (res === null) { | ||
@@ -113,3 +117,3 @@ return [2 /*return*/, this.completeRequest({ err: UNEXPECTED_ERROR_MESSAGE })]; | ||
this.attempts = 0; | ||
return [2 /*return*/, this.parseAndStoreConfig(res, sessionId)]; | ||
return [2 /*return*/, this.parseAndStoreConfig(res)]; | ||
case Status.Failed: | ||
@@ -122,3 +126,3 @@ return [2 /*return*/, this.retryFetch(signal, sessionId)]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
e_1 = _d.sent(); | ||
knownError = e_1; | ||
@@ -144,17 +148,9 @@ if (signal.aborted) { | ||
}); }; | ||
this.parseAndStoreConfig = function (res, sessionId) { return __awaiter(_this, void 0, void 0, function () { | ||
var remoteConfig, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
this.parseAndStoreConfig = function (res) { return __awaiter(_this, void 0, void 0, function () { | ||
var remoteConfig; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, res.json()]; | ||
case 1: | ||
remoteConfig = (_b.sent()); | ||
_a = this.remoteConfigIDBStore; | ||
if (!_a) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.remoteConfigIDBStore.storeRemoteConfig(remoteConfig, sessionId)]; | ||
case 2: | ||
_a = (_b.sent()); | ||
_b.label = 3; | ||
case 3: | ||
_a; | ||
remoteConfig = (_a.sent()); | ||
this.completeRequest({ success: SUCCESS_REMOTE_CONFIG }); | ||
@@ -168,21 +164,2 @@ return [2 /*return*/, remoteConfig]; | ||
} | ||
RemoteConfigFetch.prototype.initialize = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = this; | ||
return [4 /*yield*/, RemoteConfigAPIStore.createRemoteConfigIDBStore({ | ||
apiKey: this.localConfig.apiKey, | ||
loggerProvider: this.localConfig.loggerProvider, | ||
configKeys: this.configKeys, | ||
})]; | ||
case 1: | ||
_a.remoteConfigIDBStore = _b.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RemoteConfigFetch.prototype.getServerUrl = function () { | ||
@@ -212,12 +189,4 @@ if (this.localConfig.serverZone === ServerZone.STAGING) { | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var remoteConfigFetch; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
remoteConfigFetch = new RemoteConfigFetch({ localConfig: localConfig, configKeys: configKeys }); | ||
return [4 /*yield*/, remoteConfigFetch.initialize()]; | ||
case 1: | ||
_b.sent(); | ||
return [2 /*return*/, remoteConfigFetch]; | ||
} | ||
return [2 /*return*/, new RemoteConfigFetch({ localConfig: localConfig, configKeys: configKeys })]; | ||
}); | ||
@@ -224,0 +193,0 @@ }); |
@@ -16,3 +16,2 @@ import { Config } from '@amplitude/analytics-types'; | ||
export interface RemoteConfigMetric { | ||
fetchTimeIDB?: number; | ||
fetchTimeAPISuccess?: number; | ||
@@ -19,0 +18,0 @@ fetchTimeAPIFail?: number; |
@@ -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 _=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!==(g=e.throttled_events)&&void 0!==g?g:[]}};case n.PayloadTooLarge:return{status:C,statusCode:_,body:{error:null!==(p=e.error)&&void 0!==p?p:""}};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 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"],_=["put","add","delete","clear"],C=new Map;function B(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}p((e=>({...e,get:(t,n,r)=>B(t,n)||e.get(t,n,r),has:(t,n)=>!!B(t,n)||e.has(t,n)})));const T=["continue","continuePrimaryKey","advance"],E={},F=new WeakMap,R=new WeakMap,x={get(e,t){if(!T.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){F.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,x);for(R.set(n,t),v.set(n,w(t));t;)yield n,t=await(F.get(n)||t.continue()),F.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 P=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()]}}))}))},j=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,P(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,P(e,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}))}))},A="Remote config fetch rejected due to timeout after 5 seconds",K=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.metrics={},this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.getLastFetchedSessionId()]:[3,3];case 1:return(o=i.sent())&&n&&o===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,3];case 2:return s=i.sent(),this.metrics.fetchTimeIDB=Date.now()-r,[2,s];case 3:return[4,this.fetchWithTimeout(n)];case 4:return(c=i.sent())&&(a=c.configs&&c.configs[e])?(this.metrics.fetchTimeAPISuccess=Date.now()-r,[2,a[t]]):(this.metrics.fetchTimeAPIFail=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:A})];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:A})]:[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,j({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 K({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 i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},r.apply(this,arguments)};function i(e,t,n,r){return new(n||(n=Promise))((function(i,o){function s(e){try{l(r.next(e))}catch(e){o(e)}}function c(e){try{l(r.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}l((r=r.apply(e,t||[])).next())}))}function o(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(l){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=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(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){s.label=c[1];break}if(6===c[0]&&s.label<i[1]){s.label=i[1],i=c;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(c);break}i[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,l])}}}!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 s=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,r,i,o,s,c,l,u,a,d,f,h,v,p,m,y,g,b,_,w,S,T;if("object"!=typeof e)return null;var C=e.code||0,R=this.buildStatus(C);switch(R){case n.Success:return{status:R,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!==(i=e.server_upload_time)&&void 0!==i?i:0}};case n.Invalid:return{status:R,statusCode:C,body:{error:null!==(o=e.error)&&void 0!==o?o:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(c=e.events_with_invalid_fields)&&void 0!==c?c:{},eventsWithMissingFields:null!==(l=e.events_with_missing_fields)&&void 0!==l?l:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(a=e.eps_threshold)&&void 0!==a?a: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:R,statusCode:C,body:{error:null!==(m=e.error)&&void 0!==m?m:""}};case n.RateLimit:return{status:R,statusCode:C,body:{error:null!==(y=e.error)&&void 0!==y?y:"",epsThreshold:null!==(g=e.eps_threshold)&&void 0!==g?g:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(_=e.throttled_users)&&void 0!==_?_:{},exceededDailyQuotaDevices:null!==(w=e.exceeded_daily_quota_devices)&&void 0!==w?w:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(T=e.throttled_events)&&void 0!==T?T:[]}};case n.Timeout:default:return{status:R,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}(),c="Remote config fetch rejected due to timeout after 5 seconds",l=function(){function e(e){var t=e.localConfig,l=e.configKeys,u=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.metrics={},this.getRemoteConfig=function(e,t,n){return i(u,void 0,void 0,(function(){var r,i,s;return o(this,(function(o){switch(o.label){case 0:return r=Date.now(),[4,this.fetchWithTimeout(n)];case 1:return(i=o.sent())&&(s=i.configs&&i.configs[e])?(this.metrics.fetchTimeAPISuccess=Date.now()-r,[2,s[t]]):(this.metrics.fetchTimeAPIFail=Date.now()-r,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return i(u,void 0,void 0,(function(){var t,n,r;return o(this,(function(i){switch(i.label){case 0:return t=new AbortController,n=setTimeout((function(){return t.abort()}),5e3),[4,this.fetchRemoteConfig(t.signal,e)];case 1:return r=i.sent(),clearTimeout(n),[2,r]}}))}))},this.fetchRemoteConfig=function(e,t){return i(u,void 0,void 0,(function(){var i,l,u,a,d,f,h,v,p,m,y;return o(this,(function(o){switch(o.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:c})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),o.label=1;case 1:o.trys.push([1,3,,4]),i=new URLSearchParams({api_key:this.localConfig.apiKey});try{for(l=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(this.configKeys),u=l.next();!u.done;u=l.next())a=u.value,i.append("config_keys",a)}catch(e){m={error:e}}finally{try{u&&!u.done&&(y=l.return)&&y.call(l)}finally{if(m)throw m.error}}return t&&i.set("session_id",String(t)),d={headers:{Accept:"*/*"},method:"GET"},f="".concat(this.getServerUrl(),"?").concat(i.toString()),this.attempts+=1,[4,fetch(f,r(r({},d),{signal:e}))];case 2:if(null===(h=o.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new s).buildStatus(h.status)){case n.Success:return this.attempts=0,[2,this.parseAndStoreConfig(h)];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 v=o.sent(),p=v,e.aborted?[2,this.completeRequest({err:c})]:[2,this.completeRequest({err:p.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return i(u,void 0,void 0,(function(){var n=this;return o(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){return i(u,void 0,void 0,(function(){var t;return o(this,(function(n){switch(n.label){case 0:return[4,e.json()];case 1:return t=n.sent(),this.completeRequest({success:"Remote config successfully fetched"}),[2,t]}}))}))},this.localConfig=t,this.configKeys=l}return 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 i(void 0,void 0,void 0,(function(){return o(this,(function(e){return[2,new l({localConfig:t,configKeys:n})]}))}))};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,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,F=new WeakMap,R={get(e,t){if(!B.includes(t))return e[t];let n=E[t];return n||(n=E[t]=function(...e){x.set(this,F.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,R);for(F.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 P=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()]}}))}))},j=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,P(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,P(e,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}))}))},A="Remote config fetch rejected due to timeout after 5 seconds",K=function(){function e(e){var t=e.localConfig,s=e.configKeys,c=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.metrics={},this.getRemoteConfig=function(e,t,n){return o(c,void 0,void 0,(function(){var r,o,s,c,a;return i(this,(function(i){switch(i.label){case 0:return r=Date.now(),this.remoteConfigIDBStore?[4,this.remoteConfigIDBStore.getLastFetchedSessionId()]:[3,3];case 1:return(o=i.sent())&&n&&o===n?[4,this.remoteConfigIDBStore.getRemoteConfig(e,t)]:[3,3];case 2:return s=i.sent(),this.metrics.fetchTimeIDB=Date.now()-r,[2,s];case 3:return[4,this.fetchWithTimeout(n)];case 4:return(c=i.sent())&&(a=c.configs&&c.configs[e])?(this.metrics.fetchTimeAPISuccess=Date.now()-r,[2,a[t]]):(this.metrics.fetchTimeAPIFail=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:A})];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:A})]:[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,j({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 K({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,i=function(){return i=Object.assign||function(e){for(var t,n=1,i=arguments.length;n<i;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},i.apply(this,arguments)};function o(e,t,n,i){return new(n||(n=Promise))((function(o,r){function s(e){try{c(i.next(e))}catch(e){r(e)}}function l(e){try{c(i.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,l)}c((i=i.apply(e,t||[])).next())}))}function r(e,t){var n,i,o,r,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return r={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function l(l){return function(c){return function(l){if(n)throw new TypeError("Generator is already executing.");for(;r&&(r=0,l[0]&&(s=0)),s;)try{if(n=1,i&&(o=2&l[0]?i.return:l[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,l[1])).done)return o;switch(i=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return s.label++,{value:l[1],done:!1};case 5:s.label++,i=l[1],l=[0];continue;case 7:l=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==l[0]&&2!==l[0])){s=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){s.label=l[1];break}if(6===l[0]&&s.label<o[1]){s.label=o[1],o=l;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(l);break}o[2]&&s.ops.pop(),s.trys.pop();continue}l=t.call(e,s)}catch(e){l=[6,e],i=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,c])}}}!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 s=function(){function e(){}return e.prototype.send=function(e,t){return Promise.resolve(null)},e.prototype.buildResponse=function(e){var t,i,o,r,s,l,c,u,a,d,f,h,v,p,y,m,g,b,_,w,S,T;if("object"!=typeof e)return null;var C=e.code||0,R=this.buildStatus(C);switch(R){case n.Success:return{status:R,statusCode:C,body:{eventsIngested:null!==(t=e.events_ingested)&&void 0!==t?t:0,payloadSizeBytes:null!==(i=e.payload_size_bytes)&&void 0!==i?i:0,serverUploadTime:null!==(o=e.server_upload_time)&&void 0!==o?o:0}};case n.Invalid:return{status:R,statusCode:C,body:{error:null!==(r=e.error)&&void 0!==r?r:"",missingField:null!==(s=e.missing_field)&&void 0!==s?s:"",eventsWithInvalidFields:null!==(l=e.events_with_invalid_fields)&&void 0!==l?l:{},eventsWithMissingFields:null!==(c=e.events_with_missing_fields)&&void 0!==c?c:{},eventsWithInvalidIdLengths:null!==(u=e.events_with_invalid_id_lengths)&&void 0!==u?u:{},epsThreshold:null!==(a=e.eps_threshold)&&void 0!==a?a: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:R,statusCode:C,body:{error:null!==(y=e.error)&&void 0!==y?y:""}};case n.RateLimit:return{status:R,statusCode:C,body:{error:null!==(m=e.error)&&void 0!==m?m:"",epsThreshold:null!==(g=e.eps_threshold)&&void 0!==g?g:0,throttledDevices:null!==(b=e.throttled_devices)&&void 0!==b?b:{},throttledUsers:null!==(_=e.throttled_users)&&void 0!==_?_:{},exceededDailyQuotaDevices:null!==(w=e.exceeded_daily_quota_devices)&&void 0!==w?w:{},exceededDailyQuotaUsers:null!==(S=e.exceeded_daily_quota_users)&&void 0!==S?S:{},throttledEvents:null!==(T=e.throttled_events)&&void 0!==T?T:[]}};case n.Timeout:default:return{status:R,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}(),l="Remote config fetch rejected due to timeout after 5 seconds",c=function(){function e(e){var t=e.localConfig,c=e.configKeys,u=this;this.retryTimeout=1e3,this.attempts=0,this.sessionTargetingMatch=!1,this.metrics={},this.getRemoteConfig=function(e,t,n){return o(u,void 0,void 0,(function(){var i,o,s;return r(this,(function(r){switch(r.label){case 0:return i=Date.now(),[4,this.fetchWithTimeout(n)];case 1:return(o=r.sent())&&(s=o.configs&&o.configs[e])?(this.metrics.fetchTimeAPISuccess=Date.now()-i,[2,s[t]]):(this.metrics.fetchTimeAPIFail=Date.now()-i,[2,void 0])}}))}))},this.fetchWithTimeout=function(e){return o(u,void 0,void 0,(function(){var t,n,i;return r(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 i=o.sent(),clearTimeout(n),[2,i]}}))}))},this.fetchRemoteConfig=function(e,t){return o(u,void 0,void 0,(function(){var o,c,u,a,d,f,h,v,p,y,m;return r(this,(function(r){switch(r.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:l})];t!==this.lastFetchedSessionId&&(this.lastFetchedSessionId=t,this.attempts=0),r.label=1;case 1:r.trys.push([1,3,,4]),o=new URLSearchParams({api_key:this.localConfig.apiKey});try{for(c=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],i=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&i>=e.length&&(e=void 0),{value:e&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(this.configKeys),u=c.next();!u.done;u=c.next())a=u.value,o.append("config_keys",a)}catch(e){y={error:e}}finally{try{u&&!u.done&&(m=c.return)&&m.call(c)}finally{if(y)throw y.error}}return t&&o.set("session_id",String(t)),d={headers:{Accept:"*/*"},method:"GET"},f="".concat(this.getServerUrl(),"?").concat(o.toString()),this.attempts+=1,[4,fetch(f,i(i({},d),{signal:e}))];case 2:if(null===(h=r.sent()))return[2,this.completeRequest({err:"Unexpected error occurred"})];switch((new s).buildStatus(h.status)){case n.Success:return this.attempts=0,[2,this.parseAndStoreConfig(h)];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 v=r.sent(),p=v,e.aborted?[2,this.completeRequest({err:l})]:[2,this.completeRequest({err:p.message})];case 4:return[2]}}))}))},this.retryFetch=function(e,t){return o(u,void 0,void 0,(function(){var n=this;return r(this,(function(i){switch(i.label){case 0:return[4,new Promise((function(e){return setTimeout(e,n.attempts*n.retryTimeout)}))];case 1:return i.sent(),[2,this.fetchRemoteConfig(e,t)]}}))}))},this.parseAndStoreConfig=function(e){return o(u,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,e.json()];case 1:return t=n.sent(),this.completeRequest({success:"Remote config successfully fetched"}),[2,t]}}))}))},this.localConfig=t,this.configKeys=c}return 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(){return r(this,(function(e){return[2,new c({localConfig:t,configKeys:n})]}))}))};e.createRemoteConfigFetch=u,Object.defineProperty(e,"__esModule",{value:!0})})); |
import { Config } from '@amplitude/analytics-types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigIDBStore, RemoteConfigMetric } from './types'; | ||
import { CreateRemoteConfigFetch, RemoteConfigFetch as IRemoteConfigFetch, RemoteConfigAPIResponse, RemoteConfigMetric } from './types'; | ||
export declare const REMOTE_CONFIG_SERVER_URL = "https://sr-client-cfg.amplitude.com/config"; | ||
@@ -10,3 +10,2 @@ export declare const REMOTE_CONFIG_SERVER_URL_STAGING = "https://sr-client-cfg.stag2.amplitude.com/config"; | ||
localConfig: Config; | ||
remoteConfigIDBStore: RemoteConfigIDBStore<RemoteConfig> | undefined; | ||
retryTimeout: number; | ||
@@ -22,3 +21,2 @@ attempts: number; | ||
}); | ||
initialize(): Promise<void>; | ||
getRemoteConfig: <K extends keyof RemoteConfig>(configNamespace: string, key: K, sessionId?: number) => Promise<RemoteConfig[K] | undefined>; | ||
@@ -29,3 +27,3 @@ getServerUrl(): "https://sr-client-cfg.amplitude.com/config" | "https://sr-client-cfg.stag2.amplitude.com/config" | "https://sr-client-cfg.eu.amplitude.com/config"; | ||
retryFetch: (signal: AbortController['signal'], sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig> | void>; | ||
parseAndStoreConfig: (res: Response, sessionId?: number) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
parseAndStoreConfig: (res: Response) => Promise<RemoteConfigAPIResponse<RemoteConfig>>; | ||
completeRequest({ err, success }: { | ||
@@ -32,0 +30,0 @@ err?: string; |
@@ -16,3 +16,2 @@ import { Config } from '@amplitude/analytics-types'; | ||
export interface RemoteConfigMetric { | ||
fetchTimeIDB?: number; | ||
fetchTimeAPISuccess?: number; | ||
@@ -19,0 +18,0 @@ fetchTimeAPIFail?: number; |
{ | ||
"name": "@amplitude/analytics-remote-config", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "", | ||
@@ -44,3 +44,2 @@ "author": "Amplitude Inc", | ||
"@amplitude/analytics-types": ">=1 <3", | ||
"idb": "^8.0.0", | ||
"tslib": "^2.4.1" | ||
@@ -53,3 +52,2 @@ }, | ||
"@ungap/structured-clone": "^1.2.0", | ||
"fake-indexeddb": "4.0.2", | ||
"rollup": "^2.79.1", | ||
@@ -63,3 +61,3 @@ "rollup-plugin-execute": "^1.1.1", | ||
], | ||
"gitHead": "bec7a72759f8830aaad99256dc521d2f9fd2e0a7" | ||
"gitHead": "4890acf99990ab6698520f2b83c6d3b053443ef9" | ||
} |
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
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
4
8
89525
37
635
- Removedidb@^8.0.0
- Removedidb@8.0.1(transitive)