@amplitude/analytics-core 2.41.3 → 2.41.4-SR-2728.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/remote-config/remote-config.d.ts +6 -0
- package/lib/cjs/remote-config/remote-config.d.ts.map +1 -1
- package/lib/cjs/remote-config/remote-config.js +36 -1
- package/lib/cjs/remote-config/remote-config.js.map +1 -1
- package/lib/esm/remote-config/remote-config.d.ts +6 -0
- package/lib/esm/remote-config/remote-config.d.ts.map +1 -1
- package/lib/esm/remote-config/remote-config.js +36 -1
- package/lib/esm/remote-config/remote-config.js.map +1 -1
- package/package.json +1 -1
|
@@ -91,6 +91,7 @@ export declare class RemoteConfigClient implements IRemoteConfigClient {
|
|
|
91
91
|
callbackInfos: CallbackInfo[];
|
|
92
92
|
lastSuccessfulFetch: number | null;
|
|
93
93
|
fetchPromise: Promise<RemoteConfigInfo> | null;
|
|
94
|
+
isLastFetchInvalidApiKey: boolean;
|
|
94
95
|
constructor(apiKey: string, logger: ILogger, serverZone?: ServerZoneType, serverUrl?: string);
|
|
95
96
|
subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;
|
|
96
97
|
unsubscribe(id: string): boolean;
|
|
@@ -125,6 +126,11 @@ export declare class RemoteConfigClient implements IRemoteConfigClient {
|
|
|
125
126
|
* and the attempt is considered failed (and may be retried if retries remain).
|
|
126
127
|
* 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,
|
|
127
128
|
* so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).
|
|
129
|
+
* Retry behavior by status code:
|
|
130
|
+
* - 401: invalid API key (stop retries and disable future updateConfigs calls).
|
|
131
|
+
* - 429: retry up to max retries.
|
|
132
|
+
* - other 4xx: no retry.
|
|
133
|
+
* - 5xx and network failures: retry up to max retries.
|
|
128
134
|
* @returns the remote config info. null if failed to fetch or the response is not valid JSON.
|
|
129
135
|
*/
|
|
130
136
|
fetch(retries?: number, timeout?: number): Promise<RemoteConfigInfo>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-config.d.ts","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAExC,eAAO,MAAM,aAAa,+CAA+C,CAAC;AAC1E,eAAO,MAAM,aAAa,kDAAkD,CAAC;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"remote-config.d.ts","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAExC,eAAO,MAAM,aAAa,+CAA+C,CAAC;AAC1E,eAAO,MAAM,aAAa,kDAAkD,CAAC;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAkBrC,MAAM,WAAW,YAAY;IAC3B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAElC,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,mBAAmB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,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAE,cAAqB,EAAE,SAAS,CAAC,EAAE,MAAM;IAOlG,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,oBAAoB,GAAG,MAAM;IAmBtG,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAY1B,aAAa;IAiBnB;;;OAGG;IACH,uBAAuB,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAgCpD;;;;OAIG;IACG,YAAY,CAAC,YAAY,EAAE,YAAY;IA2B7C;;OAEG;IACG,sBAAsB,CAAC,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM;IA+BxE;;;OAGG;IACH,YAAY,CAAC,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM;IAuB3F;;;;;;;;;;;;;;;;OAgBG;IACG,KAAK,CAAC,OAAO,GAAE,MAA4B,EAAE,OAAO,GAAE,MAAwB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAuEhH;;OAEG;IACH,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAIzC,YAAY,IAAI,MAAM;CASvB"}
|
|
@@ -7,6 +7,10 @@ var uuid_1 = require("../utils/uuid");
|
|
|
7
7
|
exports.US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';
|
|
8
8
|
exports.EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';
|
|
9
9
|
exports.DEFAULT_MAX_RETRIES = 3;
|
|
10
|
+
var CODE_STATUS = {
|
|
11
|
+
INVALID_API_KEY: 401,
|
|
12
|
+
RATE_LIMIT: 429,
|
|
13
|
+
};
|
|
10
14
|
/**
|
|
11
15
|
* The default timeout for fetch in milliseconds.
|
|
12
16
|
* Linear backoff policy: timeout / retry times is the interval between fetch retry.
|
|
@@ -26,6 +30,8 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
26
30
|
this.lastSuccessfulFetch = null;
|
|
27
31
|
// Store the in-flight fetch promise for deduplication.
|
|
28
32
|
this.fetchPromise = null;
|
|
33
|
+
// Used to skip periodic updateConfigs calls when API key is invalid.
|
|
34
|
+
this.isLastFetchInvalidApiKey = false;
|
|
29
35
|
this.apiKey = apiKey;
|
|
30
36
|
this.serverUrl = serverUrl || (serverZone === 'US' ? exports.US_SERVER_URL : exports.EU_SERVER_URL);
|
|
31
37
|
this.logger = logger;
|
|
@@ -94,6 +100,16 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
94
100
|
if (this.fetchPromise) {
|
|
95
101
|
return this.fetchPromise;
|
|
96
102
|
}
|
|
103
|
+
if (this.isLastFetchInvalidApiKey) {
|
|
104
|
+
this.logger.debug('Remote config client skipping fetch: Invalid API key');
|
|
105
|
+
this.fetchPromise = Promise.resolve({
|
|
106
|
+
remoteConfig: null,
|
|
107
|
+
lastFetch: new Date(),
|
|
108
|
+
}).finally(function () {
|
|
109
|
+
_this.fetchPromise = null;
|
|
110
|
+
});
|
|
111
|
+
return this.fetchPromise;
|
|
112
|
+
}
|
|
97
113
|
this.fetchPromise = this.fetch()
|
|
98
114
|
.then(function (result) {
|
|
99
115
|
// Update last successful fetch time if we got a valid config
|
|
@@ -231,6 +247,11 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
231
247
|
* and the attempt is considered failed (and may be retried if retries remain).
|
|
232
248
|
* 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,
|
|
233
249
|
* so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).
|
|
250
|
+
* Retry behavior by status code:
|
|
251
|
+
* - 401: invalid API key (stop retries and disable future updateConfigs calls).
|
|
252
|
+
* - 429: retry up to max retries.
|
|
253
|
+
* - other 4xx: no retry.
|
|
254
|
+
* - 5xx and network failures: retry up to max retries.
|
|
234
255
|
* @returns the remote config info. null if failed to fetch or the response is not valid JSON.
|
|
235
256
|
*/
|
|
236
257
|
RemoteConfigClient.prototype.fetch = function (retries, timeout) {
|
|
@@ -248,10 +269,11 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
248
269
|
lastFetch: new Date(),
|
|
249
270
|
};
|
|
250
271
|
_loop_1 = function (attempt) {
|
|
251
|
-
var abortController, timeoutId, res, body, remoteConfig, error_2;
|
|
272
|
+
var shouldRetry, abortController, timeoutId, res, body, remoteConfig, error_2;
|
|
252
273
|
return tslib_1.__generator(this, function (_b) {
|
|
253
274
|
switch (_b.label) {
|
|
254
275
|
case 0:
|
|
276
|
+
shouldRetry = true;
|
|
255
277
|
abortController = new AbortController();
|
|
256
278
|
timeoutId = setTimeout(function () { return abortController.abort(); }, timeout);
|
|
257
279
|
_b.label = 1;
|
|
@@ -271,6 +293,14 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
271
293
|
case 3:
|
|
272
294
|
body = _b.sent();
|
|
273
295
|
this_1.logger.debug("Remote config client fetch with retry time ".concat(retries, " failed with ").concat(res.status, ": ").concat(body));
|
|
296
|
+
if (res.status === CODE_STATUS.INVALID_API_KEY) {
|
|
297
|
+
this_1.logger.error("Remote config client fetch failed with ".concat(CODE_STATUS.INVALID_API_KEY, ". Invalid API key; future fetches will be skipped."));
|
|
298
|
+
this_1.isLastFetchInvalidApiKey = true;
|
|
299
|
+
shouldRetry = false;
|
|
300
|
+
}
|
|
301
|
+
else if (res.status >= 400 && res.status < 500 && res.status !== CODE_STATUS.RATE_LIMIT) {
|
|
302
|
+
shouldRetry = false;
|
|
303
|
+
}
|
|
274
304
|
return [3 /*break*/, 6];
|
|
275
305
|
case 4: return [4 /*yield*/, res.json()];
|
|
276
306
|
case 5:
|
|
@@ -295,6 +325,9 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
295
325
|
clearTimeout(timeoutId);
|
|
296
326
|
return [7 /*endfinally*/];
|
|
297
327
|
case 9:
|
|
328
|
+
if (!shouldRetry) {
|
|
329
|
+
return [2 /*return*/, "break"];
|
|
330
|
+
}
|
|
298
331
|
if (!(attempt < retries - 1)) return [3 /*break*/, 11];
|
|
299
332
|
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, _this.getJitterDelay(interval)); })];
|
|
300
333
|
case 10:
|
|
@@ -314,6 +347,8 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
314
347
|
state_1 = _a.sent();
|
|
315
348
|
if (typeof state_1 === "object")
|
|
316
349
|
return [2 /*return*/, state_1.value];
|
|
350
|
+
if (state_1 === "break")
|
|
351
|
+
return [3 /*break*/, 4];
|
|
317
352
|
_a.label = 3;
|
|
318
353
|
case 3:
|
|
319
354
|
attempt++;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-config.js","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":";;;;AAEA,2EAAwE;AACxE,sCAAqC;AAqBxB,QAAA,aAAa,GAAG,4CAA4C,CAAC;AAC7D,QAAA,aAAa,GAAG,+CAA+C,CAAC;AAChE,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACH,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;GAGG;AACH,IAAM,gCAAgC,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;AAwEpE;IAcE,4BAAY,MAAc,EAAE,MAAe,EAAE,UAAiC,EAAE,SAAkB;QAArD,2BAAA,EAAA,iBAAiC;QAP9E,2CAA2C;QAC3C,kBAAa,GAAmB,EAAE,CAAC;QACnC,mFAAmF;QACnF,wBAAmB,GAAkB,IAAI,CAAC;QAC1C,uDAAuD;QACvD,iBAAY,GAAqC,IAAI,CAAC;QAGpD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,qBAAa,CAAC,CAAC,CAAC,qBAAa,CAAC,CAAC;QACpF,IAAI,CAAC,M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AC,YAAY,CAAC,CAAC;QAElE,OAAO,UAAG,IAAI,CAAC,SAAS,cAAI,aAAa,cAAI,SAAS,CAAC,QAAQ,EAAE,CAAE,CAAC;IACtE,CAAC;IA7Qe,+BAAY,GAAG,SAAS,CAAC;IA8Q3C,yBAAC;CAAA,AA/QD,IA+QC;AA/QY,gDAAkB","sourcesContent":["import { ServerZoneType } from '../types/server-zone';\nimport { ILogger } from '../logger';\nimport { RemoteConfigLocalStorage } from './remote-config-localstorage';\nimport { UUID } from '../utils/uuid';\n\n/**\n * Modes for receiving remote config updates:\n * - `'all'` – Optimized for both speed and freshness. Returns the fastest response first\n * (cache or remote), then always waits for and returns the remote response to ensure\n * the most up-to-date config. Callback may be called once (if remote wins) or twice\n * (cache first, then remote).\n * - `{ timeout: number }` – Prefers remote data but with a fallback strategy. Waits for\n * a remote response until the specified timeout (in milliseconds), then falls back to\n * cached data if available. Callback is called exactly once.\n */\nexport type DeliveryMode = 'all' | { timeout: number };\n\n/**\n * Sources of returned remote config:\n * - `cache` - Fetched from local storage.\n * - `remote` - Fetched from remote.\n */\nexport type Source = 'cache' | 'remote';\n\nexport const US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';\nexport const EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The default timeout for fetch in milliseconds.\n * Linear backoff policy: timeout / retry times is the interval between fetch retry.\n */\nconst DEFAULT_TIMEOUT = 1000;\n\n/**\n * The minimum time between fetches in milliseconds.\n * This prevents too many requests from being sent in a short period of time.\n */\nconst DEFAULT_MIN_TIME_BETWEEN_FETCHES = 5 * 60 * 1000; // 5 minutes\n\nexport interface RemoteConfig {\n [key: string]: any;\n}\n\nexport interface RemoteConfigInfo {\n remoteConfig: RemoteConfig | null;\n // Timestamp of when the remote config was fetched.\n lastFetch: Date;\n}\n\nexport interface RemoteConfigStorage {\n /**\n * Fetch remote config from storage asynchronously.\n */\n fetchConfig(): Promise<RemoteConfigInfo>;\n\n /**\n * Set remote config to storage asynchronously.\n */\n setConfig(config: RemoteConfigInfo): Promise<boolean>;\n}\n\n/**\n * Information about each callback registered by `RemoteConfigClient.subscribe()`,\n * managed internally by `RemoteConfigClient`.\n */\nexport interface CallbackInfo {\n id: string;\n key?: string;\n deliveryMode: DeliveryMode;\n callback: RemoteConfigCallback;\n lastCallback?: Date;\n}\n\n/**\n * Callback used in `RemoteConfigClient.subscribe()`.\n * This function is called when the remote config is fetched.\n */\ntype RemoteConfigCallback = (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => void;\n\nexport interface IRemoteConfigClient {\n /**\n * Subscribe for updates to remote config.\n * Callback is guaranteed to be called at least once,\n * Whether we are able to fetch a config or not.\n *\n * @param key - a string containing a series of period delimited keys to filter the returned config.\n * Ie, {a: {b: {c: ...}}} would return {b: {c: ...}} for \"a\" or {c: ...} for \"a.b\".\n * Set to `undefined` to subscribe all keys.\n * @param deliveryMode - how the initial callback is sent.\n * @param callback - a block that will be called when remote config is fetched.\n * @return id - identification of the subscribe and can be used to unsubscribe from updates.\n */\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;\n\n /**\n * Unsubscribe a callback from receiving future updates.\n *\n * @param id - identification of the callback that you want to unsubscribe.\n * It's the return value of subscribe().\n * @return boolean - whether the callback is removed.\n */\n unsubscribe(id: string): boolean;\n\n /**\n * Request the remote config client to fetch from remote, update cache, and callback.\n */\n updateConfigs(): void;\n}\n\nexport class RemoteConfigClient implements IRemoteConfigClient {\n static readonly CONFIG_GROUP = 'browser';\n\n readonly apiKey: string;\n readonly serverUrl: string;\n readonly logger: ILogger;\n readonly storage: RemoteConfigStorage;\n // Registered callbackInfos by subscribe().\n callbackInfos: CallbackInfo[] = [];\n // Track the last successful fetch time for throttling (timestamp in milliseconds).\n lastSuccessfulFetch: number | null = null;\n // Store the in-flight fetch promise for deduplication.\n fetchPromise: Promise<RemoteConfigInfo> | null = null;\n\n constructor(apiKey: string, logger: ILogger, serverZone: ServerZoneType = 'US', serverUrl?: string) {\n this.apiKey = apiKey;\n this.serverUrl = serverUrl || (serverZone === 'US' ? US_SERVER_URL : EU_SERVER_URL);\n this.logger = logger;\n this.storage = new RemoteConfigLocalStorage(apiKey, logger);\n }\n\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string {\n const id = UUID();\n const callbackInfo = {\n id: id,\n key: key,\n deliveryMode: deliveryMode,\n callback: callback,\n };\n this.callbackInfos.push(callbackInfo);\n\n if (deliveryMode === 'all') {\n void this.subscribeAll(callbackInfo);\n } else {\n void this.subscribeWaitForRemote(callbackInfo, deliveryMode.timeout);\n }\n\n return id;\n }\n\n unsubscribe(id: string): boolean {\n const index = this.callbackInfos.findIndex((callbackInfo) => callbackInfo.id === id);\n if (index === -1) {\n this.logger.debug(`Remote config client unsubscribe failed because callback with id ${id} doesn't exist.`);\n return false;\n }\n\n this.callbackInfos.splice(index, 1);\n this.logger.debug(`Remote config client unsubscribe succeeded removing callback with id ${id}.`);\n return true;\n }\n\n async updateConfigs() {\n // Check if we need to throttle based on last successful fetch time\n if (this.lastSuccessfulFetch) {\n const timeSinceLastFetch = Date.now() - this.lastSuccessfulFetch;\n if (timeSinceLastFetch < DEFAULT_MIN_TIME_BETWEEN_FETCHES) {\n this.logger.debug('Remote config client skipping updateConfigs: Too recent');\n return;\n }\n }\n\n const result = await this.getOrCreateFetchPromise();\n void this.storage.setConfig(result);\n this.callbackInfos.forEach((callbackInfo) => {\n this.sendCallback(callbackInfo, result, 'remote');\n });\n }\n\n /**\n * Get the in-flight fetch promise or create a new one.\n * This ensures multiple subscribe calls share the same network request.\n */\n getOrCreateFetchPromise(): Promise<RemoteConfigInfo> {\n if (this.fetchPromise) {\n return this.fetchPromise;\n }\n\n this.fetchPromise = this.fetch()\n .then((result) => {\n // Update last successful fetch time if we got a valid config\n if (result.remoteConfig !== null) {\n this.lastSuccessfulFetch = Date.now();\n }\n return result;\n })\n .finally(() => {\n // Clear the promise after it settles (success or failure)\n this.fetchPromise = null;\n });\n\n return this.fetchPromise;\n }\n\n /**\n * Send remote first. If it's already complete, we can skip the cached response.\n * - if remote is fetched first, no cache fetch.\n * - if cache is fetched first, still fetching remote.\n */\n async subscribeAll(callbackInfo: CallbackInfo) {\n const remotePromise = this.getOrCreateFetchPromise().then((result) => {\n this.logger.debug(`Remote config client subscription all mode fetched from remote: ${JSON.stringify(result)}`);\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n });\n\n const cachePromise = this.storage.fetchConfig().then((result) => {\n return result;\n });\n\n // Wait for the first result to resolve\n const result = await Promise.race([remotePromise, cachePromise]);\n\n // If cache is fetched first, wait for remote.\n if (result !== undefined) {\n this.logger.debug(`Remote config client subscription all mode fetched from cache: ${JSON.stringify(result)}`);\n // Skip sending callback if cache is empty (first time user).\n if (result.remoteConfig !== null) {\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client skips sending callback because cache is empty (first time user).');\n }\n }\n await remotePromise;\n }\n\n /**\n * Waits for a remote response until the given timeout, then return a cached copy, if available.\n */\n async subscribeWaitForRemote(callbackInfo: CallbackInfo, timeout: number) {\n const timeoutPromise = new Promise((_, reject) => {\n setTimeout(() => {\n reject('Timeout exceeded');\n }, timeout);\n });\n\n try {\n const result: RemoteConfigInfo = (await Promise.race([\n this.getOrCreateFetchPromise(),\n timeoutPromise,\n ])) as RemoteConfigInfo;\n\n this.logger.debug('Remote config client subscription wait for remote mode returns from remote.');\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n } catch (error) {\n this.logger.debug(\n 'Remote config client subscription wait for remote mode exceeded timeout. Try to fetch from cache.',\n );\n const result = await this.storage.fetchConfig();\n if (result.remoteConfig !== null) {\n this.logger.debug('Remote config client subscription wait for remote mode returns a cached copy.');\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client subscription wait for remote mode failed to fetch cache.');\n this.sendCallback(callbackInfo, result, 'remote');\n }\n }\n }\n\n /**\n * Call the callback with filtered remote config based on key.\n * @param remoteConfigInfo - the whole remote config object without filtering by key.\n */\n sendCallback(callbackInfo: CallbackInfo, remoteConfigInfo: RemoteConfigInfo, source: Source) {\n callbackInfo.lastCallback = new Date();\n\n let filteredConfig: RemoteConfig | null;\n if (callbackInfo.key) {\n // Filter remote config by key.\n // For example, if remote config is {a: {b: {c: 1}}},\n // if key = 'a', filter result is {b: {c: 1}};\n // if key = 'a.b', filter result is {c: 1}\n filteredConfig = callbackInfo.key.split('.').reduce((config, key) => {\n if (config === null) {\n return config;\n }\n\n return key in config ? (config[key] as RemoteConfig) : null;\n }, remoteConfigInfo.remoteConfig);\n } else {\n filteredConfig = remoteConfigInfo.remoteConfig;\n }\n\n callbackInfo.callback(filteredConfig, source, remoteConfigInfo.lastFetch);\n }\n\n /**\n * Fetch remote config from remote.\n * @param retries - the number of retries. default is 3.\n * @param timeout - the timeout in milliseconds. Default is 1000.\n * This timeout serves two purposes:\n * 1. It determines how long to wait for each remote config fetch request before aborting it.\n * If the fetch does not complete within the specified timeout, the request is cancelled using AbortController,\n * and the attempt is considered failed (and may be retried if retries remain).\n * 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,\n * so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).\n * @returns the remote config info. null if failed to fetch or the response is not valid JSON.\n */\n async fetch(retries: number = DEFAULT_MAX_RETRIES, timeout: number = DEFAULT_TIMEOUT): Promise<RemoteConfigInfo> {\n const interval = timeout / retries;\n const failedRemoteConfigInfo: RemoteConfigInfo = {\n remoteConfig: null,\n lastFetch: new Date(),\n };\n\n for (let attempt = 0; attempt < retries; attempt++) {\n // Create AbortController for request timeout\n const abortController = new AbortController();\n const timeoutId = setTimeout(() => abortController.abort(), timeout);\n\n try {\n const res = await fetch(this.getUrlParams(), {\n method: 'GET',\n headers: {\n Accept: '*/*',\n },\n signal: abortController.signal,\n });\n\n // Handle unsuccessful fetch\n if (!res.ok) {\n const body = await res.text();\n this.logger.debug(`Remote config client fetch with retry time ${retries} failed with ${res.status}: ${body}`);\n } else {\n // Handle successful fetch\n const remoteConfig: RemoteConfig = (await res.json()) as RemoteConfig;\n return {\n remoteConfig: remoteConfig,\n lastFetch: new Date(),\n };\n }\n } catch (error) {\n // Handle rejects when the request fails, for example, a network error or timeout\n if (error instanceof Error && error.name === 'AbortError') {\n this.logger.debug(`Remote config client fetch with retry time ${retries} timed out after ${timeout}ms`);\n } else {\n this.logger.debug(`Remote config client fetch with retry time ${retries} is rejected because: `, error);\n }\n } finally {\n // Clear the timeout since request completed or failed\n clearTimeout(timeoutId);\n }\n\n // Linear backoff:\n // wait for the specified interval before the next attempt\n // except after the last attempt.\n if (attempt < retries - 1) {\n await new Promise((resolve) => setTimeout(resolve, this.getJitterDelay(interval)));\n }\n }\n\n return failedRemoteConfigInfo;\n }\n\n /**\n * Return jitter in the bound of [0,baseDelay) and then floor round.\n */\n getJitterDelay(baseDelay: number): number {\n return Math.floor(Math.random() * baseDelay);\n }\n\n getUrlParams(): string {\n // URL encode the API key to handle special characters\n const encodedApiKey = encodeURIComponent(this.apiKey);\n\n const urlParams = new URLSearchParams();\n urlParams.append('config_group', RemoteConfigClient.CONFIG_GROUP);\n\n return `${this.serverUrl}/${encodedApiKey}?${urlParams.toString()}`;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"remote-config.js","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":";;;;AAEA,2EAAwE;AACxE,sCAAqC;AAqBxB,QAAA,aAAa,GAAG,4CAA4C,CAAC;AAC7D,QAAA,aAAa,GAAG,+CAA+C,CAAC;AAChE,QAAA,mBAAmB,GAAG,CAAC,CAAC;AACrC,IAAM,WAAW,GAAG;IAClB,eAAe,EAAE,GAAG;IACpB,UAAU,EAAE,GAAG;CACP,CAAC;AAEX;;;GAGG;AACH,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;GAGG;AACH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,GAAG,kBAAkB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAEtD,IAAM,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;QACxC,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAElE,OAAO,UAAG,IAAI,CAAC,SAAS,cAAI,aAAa,cAAI,SAAS,CAAC,QAAQ,EAAE,CAAE,CAAC;IACtE,CAAC;IA9Se,+BAAY,GAAG,SAAS,CAAC;IA+S3C,yBAAC;CAAA,AAhTD,IAgTC;AAhTY,gDAAkB","sourcesContent":["import { ServerZoneType } from '../types/server-zone';\nimport { ILogger } from '../logger';\nimport { RemoteConfigLocalStorage } from './remote-config-localstorage';\nimport { UUID } from '../utils/uuid';\n\n/**\n * Modes for receiving remote config updates:\n * - `'all'` – Optimized for both speed and freshness. Returns the fastest response first\n * (cache or remote), then always waits for and returns the remote response to ensure\n * the most up-to-date config. Callback may be called once (if remote wins) or twice\n * (cache first, then remote).\n * - `{ timeout: number }` – Prefers remote data but with a fallback strategy. Waits for\n * a remote response until the specified timeout (in milliseconds), then falls back to\n * cached data if available. Callback is called exactly once.\n */\nexport type DeliveryMode = 'all' | { timeout: number };\n\n/**\n * Sources of returned remote config:\n * - `cache` - Fetched from local storage.\n * - `remote` - Fetched from remote.\n */\nexport type Source = 'cache' | 'remote';\n\nexport const US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';\nexport const EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';\nexport const DEFAULT_MAX_RETRIES = 3;\nconst CODE_STATUS = {\n INVALID_API_KEY: 401,\n RATE_LIMIT: 429,\n} as const;\n\n/**\n * The default timeout for fetch in milliseconds.\n * Linear backoff policy: timeout / retry times is the interval between fetch retry.\n */\nconst DEFAULT_TIMEOUT = 1000;\n\n/**\n * The minimum time between fetches in milliseconds.\n * This prevents too many requests from being sent in a short period of time.\n */\nconst DEFAULT_MIN_TIME_BETWEEN_FETCHES = 5 * 60 * 1000; // 5 minutes\n\nexport interface RemoteConfig {\n [key: string]: any;\n}\n\nexport interface RemoteConfigInfo {\n remoteConfig: RemoteConfig | null;\n // Timestamp of when the remote config was fetched.\n lastFetch: Date;\n}\n\nexport interface RemoteConfigStorage {\n /**\n * Fetch remote config from storage asynchronously.\n */\n fetchConfig(): Promise<RemoteConfigInfo>;\n\n /**\n * Set remote config to storage asynchronously.\n */\n setConfig(config: RemoteConfigInfo): Promise<boolean>;\n}\n\n/**\n * Information about each callback registered by `RemoteConfigClient.subscribe()`,\n * managed internally by `RemoteConfigClient`.\n */\nexport interface CallbackInfo {\n id: string;\n key?: string;\n deliveryMode: DeliveryMode;\n callback: RemoteConfigCallback;\n lastCallback?: Date;\n}\n\n/**\n * Callback used in `RemoteConfigClient.subscribe()`.\n * This function is called when the remote config is fetched.\n */\ntype RemoteConfigCallback = (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => void;\n\nexport interface IRemoteConfigClient {\n /**\n * Subscribe for updates to remote config.\n * Callback is guaranteed to be called at least once,\n * Whether we are able to fetch a config or not.\n *\n * @param key - a string containing a series of period delimited keys to filter the returned config.\n * Ie, {a: {b: {c: ...}}} would return {b: {c: ...}} for \"a\" or {c: ...} for \"a.b\".\n * Set to `undefined` to subscribe all keys.\n * @param deliveryMode - how the initial callback is sent.\n * @param callback - a block that will be called when remote config is fetched.\n * @return id - identification of the subscribe and can be used to unsubscribe from updates.\n */\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;\n\n /**\n * Unsubscribe a callback from receiving future updates.\n *\n * @param id - identification of the callback that you want to unsubscribe.\n * It's the return value of subscribe().\n * @return boolean - whether the callback is removed.\n */\n unsubscribe(id: string): boolean;\n\n /**\n * Request the remote config client to fetch from remote, update cache, and callback.\n */\n updateConfigs(): void;\n}\n\nexport class RemoteConfigClient implements IRemoteConfigClient {\n static readonly CONFIG_GROUP = 'browser';\n\n readonly apiKey: string;\n readonly serverUrl: string;\n readonly logger: ILogger;\n readonly storage: RemoteConfigStorage;\n // Registered callbackInfos by subscribe().\n callbackInfos: CallbackInfo[] = [];\n // Track the last successful fetch time for throttling (timestamp in milliseconds).\n lastSuccessfulFetch: number | null = null;\n // Store the in-flight fetch promise for deduplication.\n fetchPromise: Promise<RemoteConfigInfo> | null = null;\n // Used to skip periodic updateConfigs calls when API key is invalid.\n isLastFetchInvalidApiKey = false;\n\n constructor(apiKey: string, logger: ILogger, serverZone: ServerZoneType = 'US', serverUrl?: string) {\n this.apiKey = apiKey;\n this.serverUrl = serverUrl || (serverZone === 'US' ? US_SERVER_URL : EU_SERVER_URL);\n this.logger = logger;\n this.storage = new RemoteConfigLocalStorage(apiKey, logger);\n }\n\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string {\n const id = UUID();\n const callbackInfo = {\n id: id,\n key: key,\n deliveryMode: deliveryMode,\n callback: callback,\n };\n this.callbackInfos.push(callbackInfo);\n\n if (deliveryMode === 'all') {\n void this.subscribeAll(callbackInfo);\n } else {\n void this.subscribeWaitForRemote(callbackInfo, deliveryMode.timeout);\n }\n\n return id;\n }\n\n unsubscribe(id: string): boolean {\n const index = this.callbackInfos.findIndex((callbackInfo) => callbackInfo.id === id);\n if (index === -1) {\n this.logger.debug(`Remote config client unsubscribe failed because callback with id ${id} doesn't exist.`);\n return false;\n }\n\n this.callbackInfos.splice(index, 1);\n this.logger.debug(`Remote config client unsubscribe succeeded removing callback with id ${id}.`);\n return true;\n }\n\n async updateConfigs() {\n // Check if we need to throttle based on last successful fetch time\n if (this.lastSuccessfulFetch) {\n const timeSinceLastFetch = Date.now() - this.lastSuccessfulFetch;\n if (timeSinceLastFetch < DEFAULT_MIN_TIME_BETWEEN_FETCHES) {\n this.logger.debug('Remote config client skipping updateConfigs: Too recent');\n return;\n }\n }\n\n const result = await this.getOrCreateFetchPromise();\n void this.storage.setConfig(result);\n this.callbackInfos.forEach((callbackInfo) => {\n this.sendCallback(callbackInfo, result, 'remote');\n });\n }\n\n /**\n * Get the in-flight fetch promise or create a new one.\n * This ensures multiple subscribe calls share the same network request.\n */\n getOrCreateFetchPromise(): Promise<RemoteConfigInfo> {\n if (this.fetchPromise) {\n return this.fetchPromise;\n }\n\n if (this.isLastFetchInvalidApiKey) {\n this.logger.debug('Remote config client skipping fetch: Invalid API key');\n this.fetchPromise = Promise.resolve({\n remoteConfig: null,\n lastFetch: new Date(),\n }).finally(() => {\n this.fetchPromise = null;\n });\n return this.fetchPromise;\n }\n\n this.fetchPromise = this.fetch()\n .then((result) => {\n // Update last successful fetch time if we got a valid config\n if (result.remoteConfig !== null) {\n this.lastSuccessfulFetch = Date.now();\n }\n return result;\n })\n .finally(() => {\n // Clear the promise after it settles (success or failure)\n this.fetchPromise = null;\n });\n\n return this.fetchPromise;\n }\n\n /**\n * Send remote first. If it's already complete, we can skip the cached response.\n * - if remote is fetched first, no cache fetch.\n * - if cache is fetched first, still fetching remote.\n */\n async subscribeAll(callbackInfo: CallbackInfo) {\n const remotePromise = this.getOrCreateFetchPromise().then((result) => {\n this.logger.debug(`Remote config client subscription all mode fetched from remote: ${JSON.stringify(result)}`);\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n });\n\n const cachePromise = this.storage.fetchConfig().then((result) => {\n return result;\n });\n\n // Wait for the first result to resolve\n const result = await Promise.race([remotePromise, cachePromise]);\n\n // If cache is fetched first, wait for remote.\n if (result !== undefined) {\n this.logger.debug(`Remote config client subscription all mode fetched from cache: ${JSON.stringify(result)}`);\n // Skip sending callback if cache is empty (first time user).\n if (result.remoteConfig !== null) {\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client skips sending callback because cache is empty (first time user).');\n }\n }\n await remotePromise;\n }\n\n /**\n * Waits for a remote response until the given timeout, then return a cached copy, if available.\n */\n async subscribeWaitForRemote(callbackInfo: CallbackInfo, timeout: number) {\n const timeoutPromise = new Promise((_, reject) => {\n setTimeout(() => {\n reject('Timeout exceeded');\n }, timeout);\n });\n\n try {\n const result: RemoteConfigInfo = (await Promise.race([\n this.getOrCreateFetchPromise(),\n timeoutPromise,\n ])) as RemoteConfigInfo;\n\n this.logger.debug('Remote config client subscription wait for remote mode returns from remote.');\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n } catch (error) {\n this.logger.debug(\n 'Remote config client subscription wait for remote mode exceeded timeout. Try to fetch from cache.',\n );\n const result = await this.storage.fetchConfig();\n if (result.remoteConfig !== null) {\n this.logger.debug('Remote config client subscription wait for remote mode returns a cached copy.');\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client subscription wait for remote mode failed to fetch cache.');\n this.sendCallback(callbackInfo, result, 'remote');\n }\n }\n }\n\n /**\n * Call the callback with filtered remote config based on key.\n * @param remoteConfigInfo - the whole remote config object without filtering by key.\n */\n sendCallback(callbackInfo: CallbackInfo, remoteConfigInfo: RemoteConfigInfo, source: Source) {\n callbackInfo.lastCallback = new Date();\n\n let filteredConfig: RemoteConfig | null;\n if (callbackInfo.key) {\n // Filter remote config by key.\n // For example, if remote config is {a: {b: {c: 1}}},\n // if key = 'a', filter result is {b: {c: 1}};\n // if key = 'a.b', filter result is {c: 1}\n filteredConfig = callbackInfo.key.split('.').reduce((config, key) => {\n if (config === null) {\n return config;\n }\n\n return key in config ? (config[key] as RemoteConfig) : null;\n }, remoteConfigInfo.remoteConfig);\n } else {\n filteredConfig = remoteConfigInfo.remoteConfig;\n }\n\n callbackInfo.callback(filteredConfig, source, remoteConfigInfo.lastFetch);\n }\n\n /**\n * Fetch remote config from remote.\n * @param retries - the number of retries. default is 3.\n * @param timeout - the timeout in milliseconds. Default is 1000.\n * This timeout serves two purposes:\n * 1. It determines how long to wait for each remote config fetch request before aborting it.\n * If the fetch does not complete within the specified timeout, the request is cancelled using AbortController,\n * and the attempt is considered failed (and may be retried if retries remain).\n * 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,\n * so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).\n * Retry behavior by status code:\n * - 401: invalid API key (stop retries and disable future updateConfigs calls).\n * - 429: retry up to max retries.\n * - other 4xx: no retry.\n * - 5xx and network failures: retry up to max retries.\n * @returns the remote config info. null if failed to fetch or the response is not valid JSON.\n */\n async fetch(retries: number = DEFAULT_MAX_RETRIES, timeout: number = DEFAULT_TIMEOUT): Promise<RemoteConfigInfo> {\n const interval = timeout / retries;\n const failedRemoteConfigInfo: RemoteConfigInfo = {\n remoteConfig: null,\n lastFetch: new Date(),\n };\n\n for (let attempt = 0; attempt < retries; attempt++) {\n let shouldRetry = true;\n // Create AbortController for request timeout\n const abortController = new AbortController();\n const timeoutId = setTimeout(() => abortController.abort(), timeout);\n\n try {\n const res = await fetch(this.getUrlParams(), {\n method: 'GET',\n headers: {\n Accept: '*/*',\n },\n signal: abortController.signal,\n });\n\n // Handle unsuccessful fetch\n if (!res.ok) {\n const body = await res.text();\n this.logger.debug(`Remote config client fetch with retry time ${retries} failed with ${res.status}: ${body}`);\n\n if (res.status === CODE_STATUS.INVALID_API_KEY) {\n this.logger.error(\n `Remote config client fetch failed with ${CODE_STATUS.INVALID_API_KEY}. Invalid API key; future fetches will be skipped.`,\n );\n this.isLastFetchInvalidApiKey = true;\n shouldRetry = false;\n } else if (res.status >= 400 && res.status < 500 && res.status !== CODE_STATUS.RATE_LIMIT) {\n shouldRetry = false;\n }\n } else {\n // Handle successful fetch\n const remoteConfig: RemoteConfig = (await res.json()) as RemoteConfig;\n return {\n remoteConfig: remoteConfig,\n lastFetch: new Date(),\n };\n }\n } catch (error) {\n // Handle rejects when the request fails, for example, a network error or timeout\n if (error instanceof Error && error.name === 'AbortError') {\n this.logger.debug(`Remote config client fetch with retry time ${retries} timed out after ${timeout}ms`);\n } else {\n this.logger.debug(`Remote config client fetch with retry time ${retries} is rejected because: `, error);\n }\n } finally {\n // Clear the timeout since request completed or failed\n clearTimeout(timeoutId);\n }\n\n if (!shouldRetry) {\n break;\n }\n\n // Linear backoff:\n // wait for the specified interval before the next attempt\n // except after the last attempt.\n if (attempt < retries - 1) {\n await new Promise((resolve) => setTimeout(resolve, this.getJitterDelay(interval)));\n }\n }\n\n return failedRemoteConfigInfo;\n }\n\n /**\n * Return jitter in the bound of [0,baseDelay) and then floor round.\n */\n getJitterDelay(baseDelay: number): number {\n return Math.floor(Math.random() * baseDelay);\n }\n\n getUrlParams(): string {\n // URL encode the API key to handle special characters\n const encodedApiKey = encodeURIComponent(this.apiKey);\n\n const urlParams = new URLSearchParams();\n urlParams.append('config_group', RemoteConfigClient.CONFIG_GROUP);\n\n return `${this.serverUrl}/${encodedApiKey}?${urlParams.toString()}`;\n }\n}\n"]}
|
|
@@ -91,6 +91,7 @@ export declare class RemoteConfigClient implements IRemoteConfigClient {
|
|
|
91
91
|
callbackInfos: CallbackInfo[];
|
|
92
92
|
lastSuccessfulFetch: number | null;
|
|
93
93
|
fetchPromise: Promise<RemoteConfigInfo> | null;
|
|
94
|
+
isLastFetchInvalidApiKey: boolean;
|
|
94
95
|
constructor(apiKey: string, logger: ILogger, serverZone?: ServerZoneType, serverUrl?: string);
|
|
95
96
|
subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;
|
|
96
97
|
unsubscribe(id: string): boolean;
|
|
@@ -125,6 +126,11 @@ export declare class RemoteConfigClient implements IRemoteConfigClient {
|
|
|
125
126
|
* and the attempt is considered failed (and may be retried if retries remain).
|
|
126
127
|
* 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,
|
|
127
128
|
* so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).
|
|
129
|
+
* Retry behavior by status code:
|
|
130
|
+
* - 401: invalid API key (stop retries and disable future updateConfigs calls).
|
|
131
|
+
* - 429: retry up to max retries.
|
|
132
|
+
* - other 4xx: no retry.
|
|
133
|
+
* - 5xx and network failures: retry up to max retries.
|
|
128
134
|
* @returns the remote config info. null if failed to fetch or the response is not valid JSON.
|
|
129
135
|
*/
|
|
130
136
|
fetch(retries?: number, timeout?: number): Promise<RemoteConfigInfo>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-config.d.ts","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAExC,eAAO,MAAM,aAAa,+CAA+C,CAAC;AAC1E,eAAO,MAAM,aAAa,kDAAkD,CAAC;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"remote-config.d.ts","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAExC,eAAO,MAAM,aAAa,+CAA+C,CAAC;AAC1E,eAAO,MAAM,aAAa,kDAAkD,CAAC;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAkBrC,MAAM,WAAW,YAAY;IAC3B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAElC,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAA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wB,UAAS;gBAErB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAE,cAAqB,EAAE,SAAS,CAAC,EAAE,MAAM;IAOlG,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,oBAAoB,GAAG,MAAM;IAmBtG,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAY1B,aAAa;IAiBnB;;;OAGG;IACH,uBAAuB,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAgCpD;;;;OAIG;IACG,YAAY,CAAC,YAAY,EAAE,YAAY;IA2B7C;;OAEG;IACG,sBAAsB,CAAC,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM;IA+BxE;;;OAGG;IACH,YAAY,CAAC,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM;IAuB3F;;;;;;;;;;;;;;;;OAgBG;IACG,KAAK,CAAC,OAAO,GAAE,MAA4B,EAAE,OAAO,GAAE,MAAwB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAuEhH;;OAEG;IACH,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAIzC,YAAY,IAAI,MAAM;CASvB"}
|
|
@@ -4,6 +4,10 @@ import { UUID } from '../utils/uuid';
|
|
|
4
4
|
export var US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';
|
|
5
5
|
export var EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';
|
|
6
6
|
export var DEFAULT_MAX_RETRIES = 3;
|
|
7
|
+
var CODE_STATUS = {
|
|
8
|
+
INVALID_API_KEY: 401,
|
|
9
|
+
RATE_LIMIT: 429,
|
|
10
|
+
};
|
|
7
11
|
/**
|
|
8
12
|
* The default timeout for fetch in milliseconds.
|
|
9
13
|
* Linear backoff policy: timeout / retry times is the interval between fetch retry.
|
|
@@ -23,6 +27,8 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
23
27
|
this.lastSuccessfulFetch = null;
|
|
24
28
|
// Store the in-flight fetch promise for deduplication.
|
|
25
29
|
this.fetchPromise = null;
|
|
30
|
+
// Used to skip periodic updateConfigs calls when API key is invalid.
|
|
31
|
+
this.isLastFetchInvalidApiKey = false;
|
|
26
32
|
this.apiKey = apiKey;
|
|
27
33
|
this.serverUrl = serverUrl || (serverZone === 'US' ? US_SERVER_URL : EU_SERVER_URL);
|
|
28
34
|
this.logger = logger;
|
|
@@ -91,6 +97,16 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
91
97
|
if (this.fetchPromise) {
|
|
92
98
|
return this.fetchPromise;
|
|
93
99
|
}
|
|
100
|
+
if (this.isLastFetchInvalidApiKey) {
|
|
101
|
+
this.logger.debug('Remote config client skipping fetch: Invalid API key');
|
|
102
|
+
this.fetchPromise = Promise.resolve({
|
|
103
|
+
remoteConfig: null,
|
|
104
|
+
lastFetch: new Date(),
|
|
105
|
+
}).finally(function () {
|
|
106
|
+
_this.fetchPromise = null;
|
|
107
|
+
});
|
|
108
|
+
return this.fetchPromise;
|
|
109
|
+
}
|
|
94
110
|
this.fetchPromise = this.fetch()
|
|
95
111
|
.then(function (result) {
|
|
96
112
|
// Update last successful fetch time if we got a valid config
|
|
@@ -228,6 +244,11 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
228
244
|
* and the attempt is considered failed (and may be retried if retries remain).
|
|
229
245
|
* 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,
|
|
230
246
|
* so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).
|
|
247
|
+
* Retry behavior by status code:
|
|
248
|
+
* - 401: invalid API key (stop retries and disable future updateConfigs calls).
|
|
249
|
+
* - 429: retry up to max retries.
|
|
250
|
+
* - other 4xx: no retry.
|
|
251
|
+
* - 5xx and network failures: retry up to max retries.
|
|
231
252
|
* @returns the remote config info. null if failed to fetch or the response is not valid JSON.
|
|
232
253
|
*/
|
|
233
254
|
RemoteConfigClient.prototype.fetch = function (retries, timeout) {
|
|
@@ -245,10 +266,11 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
245
266
|
lastFetch: new Date(),
|
|
246
267
|
};
|
|
247
268
|
_loop_1 = function (attempt) {
|
|
248
|
-
var abortController, timeoutId, res, body, remoteConfig, error_2;
|
|
269
|
+
var shouldRetry, abortController, timeoutId, res, body, remoteConfig, error_2;
|
|
249
270
|
return __generator(this, function (_b) {
|
|
250
271
|
switch (_b.label) {
|
|
251
272
|
case 0:
|
|
273
|
+
shouldRetry = true;
|
|
252
274
|
abortController = new AbortController();
|
|
253
275
|
timeoutId = setTimeout(function () { return abortController.abort(); }, timeout);
|
|
254
276
|
_b.label = 1;
|
|
@@ -268,6 +290,14 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
268
290
|
case 3:
|
|
269
291
|
body = _b.sent();
|
|
270
292
|
this_1.logger.debug("Remote config client fetch with retry time ".concat(retries, " failed with ").concat(res.status, ": ").concat(body));
|
|
293
|
+
if (res.status === CODE_STATUS.INVALID_API_KEY) {
|
|
294
|
+
this_1.logger.error("Remote config client fetch failed with ".concat(CODE_STATUS.INVALID_API_KEY, ". Invalid API key; future fetches will be skipped."));
|
|
295
|
+
this_1.isLastFetchInvalidApiKey = true;
|
|
296
|
+
shouldRetry = false;
|
|
297
|
+
}
|
|
298
|
+
else if (res.status >= 400 && res.status < 500 && res.status !== CODE_STATUS.RATE_LIMIT) {
|
|
299
|
+
shouldRetry = false;
|
|
300
|
+
}
|
|
271
301
|
return [3 /*break*/, 6];
|
|
272
302
|
case 4: return [4 /*yield*/, res.json()];
|
|
273
303
|
case 5:
|
|
@@ -292,6 +322,9 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
292
322
|
clearTimeout(timeoutId);
|
|
293
323
|
return [7 /*endfinally*/];
|
|
294
324
|
case 9:
|
|
325
|
+
if (!shouldRetry) {
|
|
326
|
+
return [2 /*return*/, "break"];
|
|
327
|
+
}
|
|
295
328
|
if (!(attempt < retries - 1)) return [3 /*break*/, 11];
|
|
296
329
|
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, _this.getJitterDelay(interval)); })];
|
|
297
330
|
case 10:
|
|
@@ -311,6 +344,8 @@ var RemoteConfigClient = /** @class */ (function () {
|
|
|
311
344
|
state_1 = _a.sent();
|
|
312
345
|
if (typeof state_1 === "object")
|
|
313
346
|
return [2 /*return*/, state_1.value];
|
|
347
|
+
if (state_1 === "break")
|
|
348
|
+
return [3 /*break*/, 4];
|
|
314
349
|
_a.label = 3;
|
|
315
350
|
case 3:
|
|
316
351
|
attempt++;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-config.js","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAqBrC,MAAM,CAAC,IAAM,aAAa,GAAG,4CAA4C,CAAC;AAC1E,MAAM,CAAC,IAAM,aAAa,GAAG,+CAA+C,CAAC;AAC7E,MAAM,CAAC,IAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACH,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;GAGG;AACH,IAAM,gCAAgC,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;AAwEpE;IAcE,4BAAY,MAAc,EAAE,MAAe,EAAE,UAAiC,EAAE,SAAkB;QAArD,2BAAA,EAAA,iBAAiC;QAP9E,2CAA2C;QAC3C,kBAAa,GAAmB,EAAE,CAAC;QACnC,mFAAmF;QACnF,wBAAmB,GAAkB,IAAI,CAAC;QAC1C,uDAAuD;QACvD,iBAAY,GAAqC,IAAI,CAAC;QAGpD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,CAAC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S,GAAG,IAAI,eAAe,EAAE,CAAC;QACxC,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAElE,OAAO,UAAG,IAAI,CAAC,SAAS,cAAI,aAAa,cAAI,SAAS,CAAC,QAAQ,EAAE,CAAE,CAAC;IACtE,CAAC;IA7Qe,+BAAY,GAAG,SAAS,CAAC;IA8Q3C,yBAAC;CAAA,AA/QD,IA+QC;SA/QY,kBAAkB","sourcesContent":["import { ServerZoneType } from '../types/server-zone';\nimport { ILogger } from '../logger';\nimport { RemoteConfigLocalStorage } from './remote-config-localstorage';\nimport { UUID } from '../utils/uuid';\n\n/**\n * Modes for receiving remote config updates:\n * - `'all'` – Optimized for both speed and freshness. Returns the fastest response first\n * (cache or remote), then always waits for and returns the remote response to ensure\n * the most up-to-date config. Callback may be called once (if remote wins) or twice\n * (cache first, then remote).\n * - `{ timeout: number }` – Prefers remote data but with a fallback strategy. Waits for\n * a remote response until the specified timeout (in milliseconds), then falls back to\n * cached data if available. Callback is called exactly once.\n */\nexport type DeliveryMode = 'all' | { timeout: number };\n\n/**\n * Sources of returned remote config:\n * - `cache` - Fetched from local storage.\n * - `remote` - Fetched from remote.\n */\nexport type Source = 'cache' | 'remote';\n\nexport const US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';\nexport const EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The default timeout for fetch in milliseconds.\n * Linear backoff policy: timeout / retry times is the interval between fetch retry.\n */\nconst DEFAULT_TIMEOUT = 1000;\n\n/**\n * The minimum time between fetches in milliseconds.\n * This prevents too many requests from being sent in a short period of time.\n */\nconst DEFAULT_MIN_TIME_BETWEEN_FETCHES = 5 * 60 * 1000; // 5 minutes\n\nexport interface RemoteConfig {\n [key: string]: any;\n}\n\nexport interface RemoteConfigInfo {\n remoteConfig: RemoteConfig | null;\n // Timestamp of when the remote config was fetched.\n lastFetch: Date;\n}\n\nexport interface RemoteConfigStorage {\n /**\n * Fetch remote config from storage asynchronously.\n */\n fetchConfig(): Promise<RemoteConfigInfo>;\n\n /**\n * Set remote config to storage asynchronously.\n */\n setConfig(config: RemoteConfigInfo): Promise<boolean>;\n}\n\n/**\n * Information about each callback registered by `RemoteConfigClient.subscribe()`,\n * managed internally by `RemoteConfigClient`.\n */\nexport interface CallbackInfo {\n id: string;\n key?: string;\n deliveryMode: DeliveryMode;\n callback: RemoteConfigCallback;\n lastCallback?: Date;\n}\n\n/**\n * Callback used in `RemoteConfigClient.subscribe()`.\n * This function is called when the remote config is fetched.\n */\ntype RemoteConfigCallback = (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => void;\n\nexport interface IRemoteConfigClient {\n /**\n * Subscribe for updates to remote config.\n * Callback is guaranteed to be called at least once,\n * Whether we are able to fetch a config or not.\n *\n * @param key - a string containing a series of period delimited keys to filter the returned config.\n * Ie, {a: {b: {c: ...}}} would return {b: {c: ...}} for \"a\" or {c: ...} for \"a.b\".\n * Set to `undefined` to subscribe all keys.\n * @param deliveryMode - how the initial callback is sent.\n * @param callback - a block that will be called when remote config is fetched.\n * @return id - identification of the subscribe and can be used to unsubscribe from updates.\n */\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;\n\n /**\n * Unsubscribe a callback from receiving future updates.\n *\n * @param id - identification of the callback that you want to unsubscribe.\n * It's the return value of subscribe().\n * @return boolean - whether the callback is removed.\n */\n unsubscribe(id: string): boolean;\n\n /**\n * Request the remote config client to fetch from remote, update cache, and callback.\n */\n updateConfigs(): void;\n}\n\nexport class RemoteConfigClient implements IRemoteConfigClient {\n static readonly CONFIG_GROUP = 'browser';\n\n readonly apiKey: string;\n readonly serverUrl: string;\n readonly logger: ILogger;\n readonly storage: RemoteConfigStorage;\n // Registered callbackInfos by subscribe().\n callbackInfos: CallbackInfo[] = [];\n // Track the last successful fetch time for throttling (timestamp in milliseconds).\n lastSuccessfulFetch: number | null = null;\n // Store the in-flight fetch promise for deduplication.\n fetchPromise: Promise<RemoteConfigInfo> | null = null;\n\n constructor(apiKey: string, logger: ILogger, serverZone: ServerZoneType = 'US', serverUrl?: string) {\n this.apiKey = apiKey;\n this.serverUrl = serverUrl || (serverZone === 'US' ? US_SERVER_URL : EU_SERVER_URL);\n this.logger = logger;\n this.storage = new RemoteConfigLocalStorage(apiKey, logger);\n }\n\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string {\n const id = UUID();\n const callbackInfo = {\n id: id,\n key: key,\n deliveryMode: deliveryMode,\n callback: callback,\n };\n this.callbackInfos.push(callbackInfo);\n\n if (deliveryMode === 'all') {\n void this.subscribeAll(callbackInfo);\n } else {\n void this.subscribeWaitForRemote(callbackInfo, deliveryMode.timeout);\n }\n\n return id;\n }\n\n unsubscribe(id: string): boolean {\n const index = this.callbackInfos.findIndex((callbackInfo) => callbackInfo.id === id);\n if (index === -1) {\n this.logger.debug(`Remote config client unsubscribe failed because callback with id ${id} doesn't exist.`);\n return false;\n }\n\n this.callbackInfos.splice(index, 1);\n this.logger.debug(`Remote config client unsubscribe succeeded removing callback with id ${id}.`);\n return true;\n }\n\n async updateConfigs() {\n // Check if we need to throttle based on last successful fetch time\n if (this.lastSuccessfulFetch) {\n const timeSinceLastFetch = Date.now() - this.lastSuccessfulFetch;\n if (timeSinceLastFetch < DEFAULT_MIN_TIME_BETWEEN_FETCHES) {\n this.logger.debug('Remote config client skipping updateConfigs: Too recent');\n return;\n }\n }\n\n const result = await this.getOrCreateFetchPromise();\n void this.storage.setConfig(result);\n this.callbackInfos.forEach((callbackInfo) => {\n this.sendCallback(callbackInfo, result, 'remote');\n });\n }\n\n /**\n * Get the in-flight fetch promise or create a new one.\n * This ensures multiple subscribe calls share the same network request.\n */\n getOrCreateFetchPromise(): Promise<RemoteConfigInfo> {\n if (this.fetchPromise) {\n return this.fetchPromise;\n }\n\n this.fetchPromise = this.fetch()\n .then((result) => {\n // Update last successful fetch time if we got a valid config\n if (result.remoteConfig !== null) {\n this.lastSuccessfulFetch = Date.now();\n }\n return result;\n })\n .finally(() => {\n // Clear the promise after it settles (success or failure)\n this.fetchPromise = null;\n });\n\n return this.fetchPromise;\n }\n\n /**\n * Send remote first. If it's already complete, we can skip the cached response.\n * - if remote is fetched first, no cache fetch.\n * - if cache is fetched first, still fetching remote.\n */\n async subscribeAll(callbackInfo: CallbackInfo) {\n const remotePromise = this.getOrCreateFetchPromise().then((result) => {\n this.logger.debug(`Remote config client subscription all mode fetched from remote: ${JSON.stringify(result)}`);\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n });\n\n const cachePromise = this.storage.fetchConfig().then((result) => {\n return result;\n });\n\n // Wait for the first result to resolve\n const result = await Promise.race([remotePromise, cachePromise]);\n\n // If cache is fetched first, wait for remote.\n if (result !== undefined) {\n this.logger.debug(`Remote config client subscription all mode fetched from cache: ${JSON.stringify(result)}`);\n // Skip sending callback if cache is empty (first time user).\n if (result.remoteConfig !== null) {\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client skips sending callback because cache is empty (first time user).');\n }\n }\n await remotePromise;\n }\n\n /**\n * Waits for a remote response until the given timeout, then return a cached copy, if available.\n */\n async subscribeWaitForRemote(callbackInfo: CallbackInfo, timeout: number) {\n const timeoutPromise = new Promise((_, reject) => {\n setTimeout(() => {\n reject('Timeout exceeded');\n }, timeout);\n });\n\n try {\n const result: RemoteConfigInfo = (await Promise.race([\n this.getOrCreateFetchPromise(),\n timeoutPromise,\n ])) as RemoteConfigInfo;\n\n this.logger.debug('Remote config client subscription wait for remote mode returns from remote.');\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n } catch (error) {\n this.logger.debug(\n 'Remote config client subscription wait for remote mode exceeded timeout. Try to fetch from cache.',\n );\n const result = await this.storage.fetchConfig();\n if (result.remoteConfig !== null) {\n this.logger.debug('Remote config client subscription wait for remote mode returns a cached copy.');\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client subscription wait for remote mode failed to fetch cache.');\n this.sendCallback(callbackInfo, result, 'remote');\n }\n }\n }\n\n /**\n * Call the callback with filtered remote config based on key.\n * @param remoteConfigInfo - the whole remote config object without filtering by key.\n */\n sendCallback(callbackInfo: CallbackInfo, remoteConfigInfo: RemoteConfigInfo, source: Source) {\n callbackInfo.lastCallback = new Date();\n\n let filteredConfig: RemoteConfig | null;\n if (callbackInfo.key) {\n // Filter remote config by key.\n // For example, if remote config is {a: {b: {c: 1}}},\n // if key = 'a', filter result is {b: {c: 1}};\n // if key = 'a.b', filter result is {c: 1}\n filteredConfig = callbackInfo.key.split('.').reduce((config, key) => {\n if (config === null) {\n return config;\n }\n\n return key in config ? (config[key] as RemoteConfig) : null;\n }, remoteConfigInfo.remoteConfig);\n } else {\n filteredConfig = remoteConfigInfo.remoteConfig;\n }\n\n callbackInfo.callback(filteredConfig, source, remoteConfigInfo.lastFetch);\n }\n\n /**\n * Fetch remote config from remote.\n * @param retries - the number of retries. default is 3.\n * @param timeout - the timeout in milliseconds. Default is 1000.\n * This timeout serves two purposes:\n * 1. It determines how long to wait for each remote config fetch request before aborting it.\n * If the fetch does not complete within the specified timeout, the request is cancelled using AbortController,\n * and the attempt is considered failed (and may be retried if retries remain).\n * 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,\n * so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).\n * @returns the remote config info. null if failed to fetch or the response is not valid JSON.\n */\n async fetch(retries: number = DEFAULT_MAX_RETRIES, timeout: number = DEFAULT_TIMEOUT): Promise<RemoteConfigInfo> {\n const interval = timeout / retries;\n const failedRemoteConfigInfo: RemoteConfigInfo = {\n remoteConfig: null,\n lastFetch: new Date(),\n };\n\n for (let attempt = 0; attempt < retries; attempt++) {\n // Create AbortController for request timeout\n const abortController = new AbortController();\n const timeoutId = setTimeout(() => abortController.abort(), timeout);\n\n try {\n const res = await fetch(this.getUrlParams(), {\n method: 'GET',\n headers: {\n Accept: '*/*',\n },\n signal: abortController.signal,\n });\n\n // Handle unsuccessful fetch\n if (!res.ok) {\n const body = await res.text();\n this.logger.debug(`Remote config client fetch with retry time ${retries} failed with ${res.status}: ${body}`);\n } else {\n // Handle successful fetch\n const remoteConfig: RemoteConfig = (await res.json()) as RemoteConfig;\n return {\n remoteConfig: remoteConfig,\n lastFetch: new Date(),\n };\n }\n } catch (error) {\n // Handle rejects when the request fails, for example, a network error or timeout\n if (error instanceof Error && error.name === 'AbortError') {\n this.logger.debug(`Remote config client fetch with retry time ${retries} timed out after ${timeout}ms`);\n } else {\n this.logger.debug(`Remote config client fetch with retry time ${retries} is rejected because: `, error);\n }\n } finally {\n // Clear the timeout since request completed or failed\n clearTimeout(timeoutId);\n }\n\n // Linear backoff:\n // wait for the specified interval before the next attempt\n // except after the last attempt.\n if (attempt < retries - 1) {\n await new Promise((resolve) => setTimeout(resolve, this.getJitterDelay(interval)));\n }\n }\n\n return failedRemoteConfigInfo;\n }\n\n /**\n * Return jitter in the bound of [0,baseDelay) and then floor round.\n */\n getJitterDelay(baseDelay: number): number {\n return Math.floor(Math.random() * baseDelay);\n }\n\n getUrlParams(): string {\n // URL encode the API key to handle special characters\n const encodedApiKey = encodeURIComponent(this.apiKey);\n\n const urlParams = new URLSearchParams();\n urlParams.append('config_group', RemoteConfigClient.CONFIG_GROUP);\n\n return `${this.serverUrl}/${encodedApiKey}?${urlParams.toString()}`;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"remote-config.js","sourceRoot":"","sources":["../../../src/remote-config/remote-config.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAqBrC,MAAM,CAAC,IAAM,aAAa,GAAG,4CAA4C,CAAC;AAC1E,MAAM,CAAC,IAAM,aAAa,GAAG,+CAA+C,CAAC;AAC7E,MAAM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,2CAAc,GAAd,UAAe,SAAiB;QAC9B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IAC/C,CAAC;IAED,yCAAY,GAAZ;QACE,sDAAsD;QACtD,IAAM,aAAa,GAAG,kBAAkB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAEtD,IAAM,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;QACxC,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAElE,OAAO,UAAG,IAAI,CAAC,SAAS,cAAI,aAAa,cAAI,SAAS,CAAC,QAAQ,EAAE,CAAE,CAAC;IACtE,CAAC;IA9Se,+BAAY,GAAG,SAAS,CAAC;IA+S3C,yBAAC;CAAA,AAhTD,IAgTC;SAhTY,kBAAkB","sourcesContent":["import { ServerZoneType } from '../types/server-zone';\nimport { ILogger } from '../logger';\nimport { RemoteConfigLocalStorage } from './remote-config-localstorage';\nimport { UUID } from '../utils/uuid';\n\n/**\n * Modes for receiving remote config updates:\n * - `'all'` – Optimized for both speed and freshness. Returns the fastest response first\n * (cache or remote), then always waits for and returns the remote response to ensure\n * the most up-to-date config. Callback may be called once (if remote wins) or twice\n * (cache first, then remote).\n * - `{ timeout: number }` – Prefers remote data but with a fallback strategy. Waits for\n * a remote response until the specified timeout (in milliseconds), then falls back to\n * cached data if available. Callback is called exactly once.\n */\nexport type DeliveryMode = 'all' | { timeout: number };\n\n/**\n * Sources of returned remote config:\n * - `cache` - Fetched from local storage.\n * - `remote` - Fetched from remote.\n */\nexport type Source = 'cache' | 'remote';\n\nexport const US_SERVER_URL = 'https://sr-client-cfg.amplitude.com/config';\nexport const EU_SERVER_URL = 'https://sr-client-cfg.eu.amplitude.com/config';\nexport const DEFAULT_MAX_RETRIES = 3;\nconst CODE_STATUS = {\n INVALID_API_KEY: 401,\n RATE_LIMIT: 429,\n} as const;\n\n/**\n * The default timeout for fetch in milliseconds.\n * Linear backoff policy: timeout / retry times is the interval between fetch retry.\n */\nconst DEFAULT_TIMEOUT = 1000;\n\n/**\n * The minimum time between fetches in milliseconds.\n * This prevents too many requests from being sent in a short period of time.\n */\nconst DEFAULT_MIN_TIME_BETWEEN_FETCHES = 5 * 60 * 1000; // 5 minutes\n\nexport interface RemoteConfig {\n [key: string]: any;\n}\n\nexport interface RemoteConfigInfo {\n remoteConfig: RemoteConfig | null;\n // Timestamp of when the remote config was fetched.\n lastFetch: Date;\n}\n\nexport interface RemoteConfigStorage {\n /**\n * Fetch remote config from storage asynchronously.\n */\n fetchConfig(): Promise<RemoteConfigInfo>;\n\n /**\n * Set remote config to storage asynchronously.\n */\n setConfig(config: RemoteConfigInfo): Promise<boolean>;\n}\n\n/**\n * Information about each callback registered by `RemoteConfigClient.subscribe()`,\n * managed internally by `RemoteConfigClient`.\n */\nexport interface CallbackInfo {\n id: string;\n key?: string;\n deliveryMode: DeliveryMode;\n callback: RemoteConfigCallback;\n lastCallback?: Date;\n}\n\n/**\n * Callback used in `RemoteConfigClient.subscribe()`.\n * This function is called when the remote config is fetched.\n */\ntype RemoteConfigCallback = (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => void;\n\nexport interface IRemoteConfigClient {\n /**\n * Subscribe for updates to remote config.\n * Callback is guaranteed to be called at least once,\n * Whether we are able to fetch a config or not.\n *\n * @param key - a string containing a series of period delimited keys to filter the returned config.\n * Ie, {a: {b: {c: ...}}} would return {b: {c: ...}} for \"a\" or {c: ...} for \"a.b\".\n * Set to `undefined` to subscribe all keys.\n * @param deliveryMode - how the initial callback is sent.\n * @param callback - a block that will be called when remote config is fetched.\n * @return id - identification of the subscribe and can be used to unsubscribe from updates.\n */\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string;\n\n /**\n * Unsubscribe a callback from receiving future updates.\n *\n * @param id - identification of the callback that you want to unsubscribe.\n * It's the return value of subscribe().\n * @return boolean - whether the callback is removed.\n */\n unsubscribe(id: string): boolean;\n\n /**\n * Request the remote config client to fetch from remote, update cache, and callback.\n */\n updateConfigs(): void;\n}\n\nexport class RemoteConfigClient implements IRemoteConfigClient {\n static readonly CONFIG_GROUP = 'browser';\n\n readonly apiKey: string;\n readonly serverUrl: string;\n readonly logger: ILogger;\n readonly storage: RemoteConfigStorage;\n // Registered callbackInfos by subscribe().\n callbackInfos: CallbackInfo[] = [];\n // Track the last successful fetch time for throttling (timestamp in milliseconds).\n lastSuccessfulFetch: number | null = null;\n // Store the in-flight fetch promise for deduplication.\n fetchPromise: Promise<RemoteConfigInfo> | null = null;\n // Used to skip periodic updateConfigs calls when API key is invalid.\n isLastFetchInvalidApiKey = false;\n\n constructor(apiKey: string, logger: ILogger, serverZone: ServerZoneType = 'US', serverUrl?: string) {\n this.apiKey = apiKey;\n this.serverUrl = serverUrl || (serverZone === 'US' ? US_SERVER_URL : EU_SERVER_URL);\n this.logger = logger;\n this.storage = new RemoteConfigLocalStorage(apiKey, logger);\n }\n\n subscribe(key: string | undefined, deliveryMode: DeliveryMode, callback: RemoteConfigCallback): string {\n const id = UUID();\n const callbackInfo = {\n id: id,\n key: key,\n deliveryMode: deliveryMode,\n callback: callback,\n };\n this.callbackInfos.push(callbackInfo);\n\n if (deliveryMode === 'all') {\n void this.subscribeAll(callbackInfo);\n } else {\n void this.subscribeWaitForRemote(callbackInfo, deliveryMode.timeout);\n }\n\n return id;\n }\n\n unsubscribe(id: string): boolean {\n const index = this.callbackInfos.findIndex((callbackInfo) => callbackInfo.id === id);\n if (index === -1) {\n this.logger.debug(`Remote config client unsubscribe failed because callback with id ${id} doesn't exist.`);\n return false;\n }\n\n this.callbackInfos.splice(index, 1);\n this.logger.debug(`Remote config client unsubscribe succeeded removing callback with id ${id}.`);\n return true;\n }\n\n async updateConfigs() {\n // Check if we need to throttle based on last successful fetch time\n if (this.lastSuccessfulFetch) {\n const timeSinceLastFetch = Date.now() - this.lastSuccessfulFetch;\n if (timeSinceLastFetch < DEFAULT_MIN_TIME_BETWEEN_FETCHES) {\n this.logger.debug('Remote config client skipping updateConfigs: Too recent');\n return;\n }\n }\n\n const result = await this.getOrCreateFetchPromise();\n void this.storage.setConfig(result);\n this.callbackInfos.forEach((callbackInfo) => {\n this.sendCallback(callbackInfo, result, 'remote');\n });\n }\n\n /**\n * Get the in-flight fetch promise or create a new one.\n * This ensures multiple subscribe calls share the same network request.\n */\n getOrCreateFetchPromise(): Promise<RemoteConfigInfo> {\n if (this.fetchPromise) {\n return this.fetchPromise;\n }\n\n if (this.isLastFetchInvalidApiKey) {\n this.logger.debug('Remote config client skipping fetch: Invalid API key');\n this.fetchPromise = Promise.resolve({\n remoteConfig: null,\n lastFetch: new Date(),\n }).finally(() => {\n this.fetchPromise = null;\n });\n return this.fetchPromise;\n }\n\n this.fetchPromise = this.fetch()\n .then((result) => {\n // Update last successful fetch time if we got a valid config\n if (result.remoteConfig !== null) {\n this.lastSuccessfulFetch = Date.now();\n }\n return result;\n })\n .finally(() => {\n // Clear the promise after it settles (success or failure)\n this.fetchPromise = null;\n });\n\n return this.fetchPromise;\n }\n\n /**\n * Send remote first. If it's already complete, we can skip the cached response.\n * - if remote is fetched first, no cache fetch.\n * - if cache is fetched first, still fetching remote.\n */\n async subscribeAll(callbackInfo: CallbackInfo) {\n const remotePromise = this.getOrCreateFetchPromise().then((result) => {\n this.logger.debug(`Remote config client subscription all mode fetched from remote: ${JSON.stringify(result)}`);\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n });\n\n const cachePromise = this.storage.fetchConfig().then((result) => {\n return result;\n });\n\n // Wait for the first result to resolve\n const result = await Promise.race([remotePromise, cachePromise]);\n\n // If cache is fetched first, wait for remote.\n if (result !== undefined) {\n this.logger.debug(`Remote config client subscription all mode fetched from cache: ${JSON.stringify(result)}`);\n // Skip sending callback if cache is empty (first time user).\n if (result.remoteConfig !== null) {\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client skips sending callback because cache is empty (first time user).');\n }\n }\n await remotePromise;\n }\n\n /**\n * Waits for a remote response until the given timeout, then return a cached copy, if available.\n */\n async subscribeWaitForRemote(callbackInfo: CallbackInfo, timeout: number) {\n const timeoutPromise = new Promise((_, reject) => {\n setTimeout(() => {\n reject('Timeout exceeded');\n }, timeout);\n });\n\n try {\n const result: RemoteConfigInfo = (await Promise.race([\n this.getOrCreateFetchPromise(),\n timeoutPromise,\n ])) as RemoteConfigInfo;\n\n this.logger.debug('Remote config client subscription wait for remote mode returns from remote.');\n this.sendCallback(callbackInfo, result, 'remote');\n void this.storage.setConfig(result);\n } catch (error) {\n this.logger.debug(\n 'Remote config client subscription wait for remote mode exceeded timeout. Try to fetch from cache.',\n );\n const result = await this.storage.fetchConfig();\n if (result.remoteConfig !== null) {\n this.logger.debug('Remote config client subscription wait for remote mode returns a cached copy.');\n this.sendCallback(callbackInfo, result, 'cache');\n } else {\n this.logger.debug('Remote config client subscription wait for remote mode failed to fetch cache.');\n this.sendCallback(callbackInfo, result, 'remote');\n }\n }\n }\n\n /**\n * Call the callback with filtered remote config based on key.\n * @param remoteConfigInfo - the whole remote config object without filtering by key.\n */\n sendCallback(callbackInfo: CallbackInfo, remoteConfigInfo: RemoteConfigInfo, source: Source) {\n callbackInfo.lastCallback = new Date();\n\n let filteredConfig: RemoteConfig | null;\n if (callbackInfo.key) {\n // Filter remote config by key.\n // For example, if remote config is {a: {b: {c: 1}}},\n // if key = 'a', filter result is {b: {c: 1}};\n // if key = 'a.b', filter result is {c: 1}\n filteredConfig = callbackInfo.key.split('.').reduce((config, key) => {\n if (config === null) {\n return config;\n }\n\n return key in config ? (config[key] as RemoteConfig) : null;\n }, remoteConfigInfo.remoteConfig);\n } else {\n filteredConfig = remoteConfigInfo.remoteConfig;\n }\n\n callbackInfo.callback(filteredConfig, source, remoteConfigInfo.lastFetch);\n }\n\n /**\n * Fetch remote config from remote.\n * @param retries - the number of retries. default is 3.\n * @param timeout - the timeout in milliseconds. Default is 1000.\n * This timeout serves two purposes:\n * 1. It determines how long to wait for each remote config fetch request before aborting it.\n * If the fetch does not complete within the specified timeout, the request is cancelled using AbortController,\n * and the attempt is considered failed (and may be retried if retries remain).\n * 2. It is also used to calculate the interval between retries. The total timeout is divided by the number of retries,\n * so each retry waits for (timeout / retries) milliseconds before the next attempt (linear backoff).\n * Retry behavior by status code:\n * - 401: invalid API key (stop retries and disable future updateConfigs calls).\n * - 429: retry up to max retries.\n * - other 4xx: no retry.\n * - 5xx and network failures: retry up to max retries.\n * @returns the remote config info. null if failed to fetch or the response is not valid JSON.\n */\n async fetch(retries: number = DEFAULT_MAX_RETRIES, timeout: number = DEFAULT_TIMEOUT): Promise<RemoteConfigInfo> {\n const interval = timeout / retries;\n const failedRemoteConfigInfo: RemoteConfigInfo = {\n remoteConfig: null,\n lastFetch: new Date(),\n };\n\n for (let attempt = 0; attempt < retries; attempt++) {\n let shouldRetry = true;\n // Create AbortController for request timeout\n const abortController = new AbortController();\n const timeoutId = setTimeout(() => abortController.abort(), timeout);\n\n try {\n const res = await fetch(this.getUrlParams(), {\n method: 'GET',\n headers: {\n Accept: '*/*',\n },\n signal: abortController.signal,\n });\n\n // Handle unsuccessful fetch\n if (!res.ok) {\n const body = await res.text();\n this.logger.debug(`Remote config client fetch with retry time ${retries} failed with ${res.status}: ${body}`);\n\n if (res.status === CODE_STATUS.INVALID_API_KEY) {\n this.logger.error(\n `Remote config client fetch failed with ${CODE_STATUS.INVALID_API_KEY}. Invalid API key; future fetches will be skipped.`,\n );\n this.isLastFetchInvalidApiKey = true;\n shouldRetry = false;\n } else if (res.status >= 400 && res.status < 500 && res.status !== CODE_STATUS.RATE_LIMIT) {\n shouldRetry = false;\n }\n } else {\n // Handle successful fetch\n const remoteConfig: RemoteConfig = (await res.json()) as RemoteConfig;\n return {\n remoteConfig: remoteConfig,\n lastFetch: new Date(),\n };\n }\n } catch (error) {\n // Handle rejects when the request fails, for example, a network error or timeout\n if (error instanceof Error && error.name === 'AbortError') {\n this.logger.debug(`Remote config client fetch with retry time ${retries} timed out after ${timeout}ms`);\n } else {\n this.logger.debug(`Remote config client fetch with retry time ${retries} is rejected because: `, error);\n }\n } finally {\n // Clear the timeout since request completed or failed\n clearTimeout(timeoutId);\n }\n\n if (!shouldRetry) {\n break;\n }\n\n // Linear backoff:\n // wait for the specified interval before the next attempt\n // except after the last attempt.\n if (attempt < retries - 1) {\n await new Promise((resolve) => setTimeout(resolve, this.getJitterDelay(interval)));\n }\n }\n\n return failedRemoteConfigInfo;\n }\n\n /**\n * Return jitter in the bound of [0,baseDelay) and then floor round.\n */\n getJitterDelay(baseDelay: number): number {\n return Math.floor(Math.random() * baseDelay);\n }\n\n getUrlParams(): string {\n // URL encode the API key to handle special characters\n const encodedApiKey = encodeURIComponent(this.apiKey);\n\n const urlParams = new URLSearchParams();\n urlParams.append('config_group', RemoteConfigClient.CONFIG_GROUP);\n\n return `${this.serverUrl}/${encodedApiKey}?${urlParams.toString()}`;\n }\n}\n"]}
|