UNPKG

@embrace-io/web-sdk

Version:
94 lines (93 loc) 3.6 kB
import { DEFAULT_CONFIG, LOCAL_STORAGE_REMOTE_CONFIG_KEY } from "./constants.js"; import { getConfigURL } from "./utils.js"; import { diag } from "@opentelemetry/api"; //#region src/managers/EmbraceConfigManager/EmbraceDynamicConfigManager.ts const parseRemoteConfig = (remoteConfig) => { const parsed = { samplingPct: remoteConfig.threshold }; if (remoteConfig.network_span_forwarding !== void 0) parsed.networkSpansForwardingThreshold = remoteConfig.network_span_forwarding.pct_enabled; if (remoteConfig.user_session?.max_duration_seconds !== void 0) parsed.userSessionMaxDurationSeconds = remoteConfig.user_session.max_duration_seconds; if (remoteConfig.user_session?.inactivity_timeout_seconds !== void 0) parsed.userSessionInactivityTimeoutSeconds = remoteConfig.user_session.inactivity_timeout_seconds; if (remoteConfig.user_session?.web_foreground_inactivity_timeout_seconds !== void 0) parsed.userSessionForegroundInactivityTimeoutSeconds = remoteConfig.user_session.web_foreground_inactivity_timeout_seconds; return parsed; }; var EmbraceDynamicConfigManager = class { _remoteConfigURL = null; _diag; _storage; _baseConfig; _sdkConfig; _etag = null; constructor({ appID, appVersion, deviceId, diag: diagParam = diag.createComponentLogger({ namespace: "embrace-config-manager" }), storage, defaultConfig = {}, embraceConfigURL }) { if (appID && appVersion && deviceId) this._remoteConfigURL = getConfigURL(appID, { appVersion, deviceId, osVersion: "1" }, embraceConfigURL); this._diag = diagParam; this._storage = storage; const storedRemoteConfig = this._getRemoteConfigFromStorage(); if (storedRemoteConfig) this._etag = storedRemoteConfig.etag; this._baseConfig = { ...DEFAULT_CONFIG, ...defaultConfig }; this._sdkConfig = { ...this._baseConfig, ...storedRemoteConfig ? parseRemoteConfig(storedRemoteConfig.config) : {} }; } setConfig(config) { this._sdkConfig = { ...this._sdkConfig, ...config }; } getConfig() { return this._sdkConfig; } async refreshRemoteConfig() { if (!this._remoteConfigURL) return; try { const remoteConfigResponse = await this._fetchRemoteConfig(this._remoteConfigURL); if (!remoteConfigResponse) { this._diag.debug("No changes in remote config, skipping update"); return; } const [remoteConfig, etag] = remoteConfigResponse; this._storage.setItem(LOCAL_STORAGE_REMOTE_CONFIG_KEY, JSON.stringify({ config: remoteConfig, etag })); this._sdkConfig = { ...this._baseConfig, ...parseRemoteConfig(remoteConfig) }; this._etag = etag; } catch (error) { this._diag.warn(`Failed to refresh remote config: ${error instanceof Error ? error.message : String(error)}`); } } _getRemoteConfigFromStorage() { const configString = this._storage.getItem(LOCAL_STORAGE_REMOTE_CONFIG_KEY); if (!configString) return null; try { return JSON.parse(configString); } catch (error) { this._diag.warn(`Failed to parse remote config from storage: ${error instanceof Error ? error.message : String(error)}`); return null; } } async _fetchRemoteConfig(url) { const response = await fetch(url, { headers: this._etag ? { "If-None-Match": this._etag } : {} }); const etag = response.headers.get("etag"); if (response.status === 304) return null; if (!response.ok) { this._diag.warn(`Failed to fetch remote config from ${url}: ${response.statusText}`); return null; } return [await response.json(), etag]; } }; //#endregion export { EmbraceDynamicConfigManager }; //# sourceMappingURL=EmbraceDynamicConfigManager.js.map