@statsig/client-core 3.4.0 → 3.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statsig/client-core",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -32,6 +32,7 @@ export type Spec = {
32
32
  hasSharedParams: boolean;
33
33
  isActive?: boolean;
34
34
  targetAppIDs?: string[];
35
+ version?: number;
35
36
  };
36
37
  export type DownloadConfigSpecsResponse = {
37
38
  feature_gates: Spec[];
@@ -7,6 +7,7 @@ type EvaluationBase<T> = {
7
7
  rule_id: string;
8
8
  secondary_exposures: SecondaryExposure[];
9
9
  value: T;
10
+ version?: string;
10
11
  };
11
12
  export type SecondaryExposure = {
12
13
  gate: string;
@@ -21,6 +22,7 @@ export type ExperimentEvaluation = Flatten<EvaluationBase<Record<string, unknown
21
22
  is_device_based: boolean;
22
23
  is_experiment_active?: boolean;
23
24
  is_user_in_experiment?: boolean;
25
+ passed?: boolean;
24
26
  }>;
25
27
  export type DynamicConfigEvaluation = ExperimentEvaluation;
26
28
  export type LayerEvaluation = Flatten<Omit<ExperimentEvaluation, 'id_type'> & {
@@ -15,7 +15,7 @@ export declare class EventLogger {
15
15
  private _hasRunQuickFlush;
16
16
  private _creationTime;
17
17
  private _isLoggingDisabled;
18
- private _logEventUrl;
18
+ private _logEventUrlConfig;
19
19
  private static _safeFlushAndForget;
20
20
  private static _safeRetryFailedLogs;
21
21
  constructor(_sdkKey: string, _emitter: StatsigClientEmitEventFunc, _network: NetworkCore, _options: StatsigOptionsCommon<NetworkConfigCommon> | null);
@@ -17,7 +17,7 @@ const NetworkConfig_1 = require("./NetworkConfig");
17
17
  const SafeJs_1 = require("./SafeJs");
18
18
  const StatsigEvent_1 = require("./StatsigEvent");
19
19
  const StorageProvider_1 = require("./StorageProvider");
20
- const UrlOverrides_1 = require("./UrlOverrides");
20
+ const UrlConfiguration_1 = require("./UrlConfiguration");
21
21
  const VisibilityObserving_1 = require("./VisibilityObserving");
22
22
  const DEFAULT_QUEUE_SIZE = 50;
23
23
  const DEFAULT_FLUSH_INTERVAL_MS = 10000;
@@ -55,7 +55,7 @@ class EventLogger {
55
55
  this._isLoggingDisabled = (_options === null || _options === void 0 ? void 0 : _options.disableLogging) === true;
56
56
  this._maxQueueSize = (_a = _options === null || _options === void 0 ? void 0 : _options.loggingBufferMaxSize) !== null && _a !== void 0 ? _a : DEFAULT_QUEUE_SIZE;
57
57
  const config = _options === null || _options === void 0 ? void 0 : _options.networkConfig;
58
- this._logEventUrl = (0, UrlOverrides_1._getOverridableUrl)(config === null || config === void 0 ? void 0 : config.logEventUrl, config === null || config === void 0 ? void 0 : config.api, '/rgstr', NetworkConfig_1.NetworkDefault.eventsApi);
58
+ this._logEventUrlConfig = new UrlConfiguration_1.UrlConfiguration(NetworkConfig_1.Endpoint._rgstr, config === null || config === void 0 ? void 0 : config.logEventUrl, config === null || config === void 0 ? void 0 : config.api, config === null || config === void 0 ? void 0 : config.logEventFallbackUrls);
59
59
  }
60
60
  setLoggingDisabled(isDisabled) {
61
61
  this._isLoggingDisabled = isDisabled;
@@ -212,7 +212,7 @@ class EventLogger {
212
212
  data: {
213
213
  events,
214
214
  },
215
- url: this._logEventUrl,
215
+ urlConfig: this._logEventUrlConfig,
216
216
  retries: 3,
217
217
  isCompressable: true,
218
218
  params: {
@@ -1,7 +1,13 @@
1
+ export declare const Endpoint: {
2
+ readonly _initialize: "initialize";
3
+ readonly _rgstr: "rgstr";
4
+ readonly _download_config_specs: "download_config_specs";
5
+ };
6
+ export type Endpoint = (typeof Endpoint)[keyof typeof Endpoint];
1
7
  export declare const NetworkDefault: {
2
- eventsApi: "https://prodregistryv2.org/v1";
3
- initializeApi: "https://featureassets.org/v1";
4
- specsApi: "https://assetsconfigcdn.org/v1";
8
+ rgstr: "https://prodregistryv2.org/v1";
9
+ initialize: "https://featureassets.org/v1";
10
+ download_config_specs: "https://assetsconfigcdn.org/v1";
5
11
  };
6
12
  export type NetworkPriority = 'high' | 'low' | 'auto';
7
13
  export type NetworkArgs = RequestInit & {
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NetworkParam = exports.NetworkDefault = void 0;
3
+ exports.NetworkParam = exports.NetworkDefault = exports.Endpoint = void 0;
4
+ exports.Endpoint = {
5
+ _initialize: 'initialize',
6
+ _rgstr: 'rgstr',
7
+ _download_config_specs: 'download_config_specs',
8
+ };
4
9
  exports.NetworkDefault = {
5
- eventsApi: 'https://prodregistryv2.org/v1',
6
- initializeApi: 'https://featureassets.org/v1',
7
- specsApi: 'https://assetsconfigcdn.org/v1',
10
+ [exports.Endpoint._rgstr]: 'https://prodregistryv2.org/v1',
11
+ [exports.Endpoint._initialize]: 'https://featureassets.org/v1',
12
+ [exports.Endpoint._download_config_specs]: 'https://assetsconfigcdn.org/v1',
8
13
  };
9
14
  exports.NetworkParam = {
10
15
  EventCount: 'ec',
@@ -1,15 +1,16 @@
1
1
  import './$_StatsigGlobal';
2
+ import { ErrorBoundary } from './ErrorBoundary';
2
3
  import { NetworkPriority } from './NetworkConfig';
3
4
  import { StatsigClientEmitEventFunc } from './StatsigClientBase';
4
5
  import { AnyStatsigOptions } from './StatsigOptionsCommon';
5
6
  import { Flatten } from './TypingUtils';
7
+ import { UrlConfiguration } from './UrlConfiguration';
6
8
  type RequestArgs = {
7
9
  sdkKey: string;
8
- url: string;
10
+ urlConfig: UrlConfiguration;
9
11
  priority?: NetworkPriority;
10
12
  retries?: number;
11
13
  attempt?: number;
12
- isInitialize?: boolean;
13
14
  params?: Record<string, string>;
14
15
  headers?: Record<string, string>;
15
16
  };
@@ -18,7 +19,7 @@ export type RequestArgsWithData = Flatten<RequestArgs & {
18
19
  isStatsigEncodable?: boolean;
19
20
  isCompressable?: boolean;
20
21
  }>;
21
- type BeaconRequestArgs = Pick<RequestArgsWithData, 'data' | 'sdkKey' | 'url' | 'params' | 'isCompressable' | 'attempt'>;
22
+ type BeaconRequestArgs = Pick<RequestArgsWithData, 'data' | 'sdkKey' | 'urlConfig' | 'params' | 'isCompressable' | 'attempt'>;
22
23
  type NetworkResponse = {
23
24
  body: string | null;
24
25
  code: number;
@@ -28,14 +29,18 @@ export declare class NetworkCore {
28
29
  private readonly _timeout;
29
30
  private readonly _netConfig;
30
31
  private readonly _options;
32
+ private readonly _fallbackResolver;
33
+ private _errorBoundary;
31
34
  constructor(options: AnyStatsigOptions | null, _emitter?: StatsigClientEmitEventFunc | undefined);
32
- post(args: RequestArgsWithData): Promise<NetworkResponse | null>;
33
- get(args: RequestArgs): Promise<NetworkResponse | null>;
35
+ setErrorBoundary(errorBoundary: ErrorBoundary): void;
34
36
  isBeaconSupported(): boolean;
35
37
  beacon(args: BeaconRequestArgs): Promise<boolean>;
38
+ post(args: RequestArgsWithData): Promise<NetworkResponse | null>;
39
+ get(args: RequestArgs): Promise<NetworkResponse | null>;
36
40
  private _sendRequest;
37
41
  private _getPopulatedURL;
38
42
  private _getPopulatedBody;
39
43
  private _attemptToEncodeString;
44
+ private _getInternalRequestArgs;
40
45
  }
41
46
  export {};
@@ -15,6 +15,7 @@ const __StatsigGlobal_1 = require("./$_StatsigGlobal");
15
15
  const Diagnostics_1 = require("./Diagnostics");
16
16
  const Log_1 = require("./Log");
17
17
  const NetworkConfig_1 = require("./NetworkConfig");
18
+ const NetworkFallbackResolver_1 = require("./NetworkFallbackResolver");
18
19
  const SDKType_1 = require("./SDKType");
19
20
  const SafeJs_1 = require("./SafeJs");
20
21
  const SessionID_1 = require("./SessionID");
@@ -30,6 +31,7 @@ class NetworkCore {
30
31
  this._timeout = DEFAULT_TIMEOUT_MS;
31
32
  this._netConfig = {};
32
33
  this._options = {};
34
+ this._errorBoundary = null;
33
35
  if (options) {
34
36
  this._options = options;
35
37
  }
@@ -39,18 +41,13 @@ class NetworkCore {
39
41
  if (this._netConfig.networkTimeoutMs) {
40
42
  this._timeout = this._netConfig.networkTimeoutMs;
41
43
  }
44
+ this._fallbackResolver = new NetworkFallbackResolver_1.NetworkFallbackResolver(this._options);
42
45
  }
43
- post(args) {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- let body = yield this._getPopulatedBody(args);
46
- if (args.isStatsigEncodable) {
47
- body = this._attemptToEncodeString(args, body);
48
- }
49
- return this._sendRequest(Object.assign({ method: 'POST', body }, args));
50
- });
51
- }
52
- get(args) {
53
- return this._sendRequest(Object.assign({ method: 'GET' }, args));
46
+ setErrorBoundary(errorBoundary) {
47
+ this._errorBoundary = errorBoundary;
48
+ this._errorBoundary.wrap(this);
49
+ this._errorBoundary.wrap(this._fallbackResolver);
50
+ this._fallbackResolver.setErrorBoundary(errorBoundary);
54
51
  }
55
52
  isBeaconSupported() {
56
53
  return (typeof navigator !== 'undefined' &&
@@ -61,12 +58,27 @@ class NetworkCore {
61
58
  if (!_ensureValidSdkKey(args)) {
62
59
  return false;
63
60
  }
64
- const body = yield this._getPopulatedBody(args);
65
- const url = yield this._getPopulatedURL(args);
61
+ const argsInternal = this._getInternalRequestArgs('POST', args);
62
+ const body = yield this._getPopulatedBody(argsInternal, args.data);
63
+ const url = yield this._getPopulatedURL(argsInternal);
66
64
  const nav = navigator;
67
65
  return nav.sendBeacon.bind(nav)(url, body);
68
66
  });
69
67
  }
68
+ post(args) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const argsInternal = this._getInternalRequestArgs('POST', args);
71
+ argsInternal.body = yield this._getPopulatedBody(argsInternal, args.data);
72
+ if (args.isStatsigEncodable) {
73
+ argsInternal.body = this._attemptToEncodeString(argsInternal, argsInternal.body);
74
+ }
75
+ return this._sendRequest(argsInternal);
76
+ });
77
+ }
78
+ get(args) {
79
+ const argsInternal = this._getInternalRequestArgs('GET', args);
80
+ return this._sendRequest(argsInternal);
81
+ }
70
82
  _sendRequest(args) {
71
83
  var _a, _b, _c;
72
84
  return __awaiter(this, void 0, void 0, function* () {
@@ -78,9 +90,11 @@ class NetworkCore {
78
90
  }
79
91
  const { method, body, retries, attempt } = args;
80
92
  const currentAttempt = attempt !== null && attempt !== void 0 ? attempt : 1;
81
- const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
82
- const handle = setTimeout(() => controller === null || controller === void 0 ? void 0 : controller.abort(`Timeout of ${this._timeout}ms expired.`), this._timeout);
83
- const url = yield this._getPopulatedURL(args);
93
+ const abortController = typeof AbortController !== 'undefined' ? new AbortController() : null;
94
+ const timeoutHandle = setTimeout(() => {
95
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort(`Timeout of ${this._timeout}ms expired.`);
96
+ }, this._timeout);
97
+ const populatedUrl = yield this._getPopulatedURL(args);
84
98
  let response = null;
85
99
  const keepalive = (0, VisibilityObserving_1._isUnloading)();
86
100
  try {
@@ -88,37 +102,35 @@ class NetworkCore {
88
102
  method,
89
103
  body,
90
104
  headers: Object.assign({}, args.headers),
91
- signal: controller === null || controller === void 0 ? void 0 : controller.signal,
105
+ signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal,
92
106
  priority: args.priority,
93
107
  keepalive,
94
108
  };
95
- if (args.isInitialize) {
96
- Diagnostics_1.Diagnostics._markInitNetworkReqStart(args.sdkKey, {
97
- attempt: currentAttempt,
98
- });
99
- }
109
+ _tryMarkInitStart(args, currentAttempt);
100
110
  const func = (_a = this._netConfig.networkOverrideFunc) !== null && _a !== void 0 ? _a : fetch;
101
- response = yield func(url, config);
102
- clearTimeout(handle);
111
+ response = yield func(populatedUrl, config);
112
+ clearTimeout(timeoutHandle);
103
113
  if (!response.ok) {
104
114
  const text = yield response.text().catch(() => 'No Text');
105
- const err = new Error(`NetworkError: ${url} ${text}`);
115
+ const err = new Error(`NetworkError: ${populatedUrl} ${text}`);
106
116
  err.name = 'NetworkError';
107
117
  throw err;
108
118
  }
109
119
  const text = yield response.text();
110
- if (args.isInitialize) {
111
- Diagnostics_1.Diagnostics._markInitNetworkReqEnd(args.sdkKey, Diagnostics_1.Diagnostics._getDiagnosticsData(response, currentAttempt, text));
112
- }
120
+ _tryMarkInitEnd(args, response, currentAttempt, text);
121
+ this._fallbackResolver.tryBumpExpiryTime(args.sdkKey, args.urlConfig);
113
122
  return {
114
123
  body: text,
115
124
  code: response.status,
116
125
  };
117
126
  }
118
127
  catch (error) {
119
- const errorMessage = _getErrorMessage(controller, error);
120
- if (args.isInitialize) {
121
- Diagnostics_1.Diagnostics._markInitNetworkReqEnd(args.sdkKey, Diagnostics_1.Diagnostics._getDiagnosticsData(response, currentAttempt, '', error));
128
+ const errorMessage = _getErrorMessage(abortController, error);
129
+ const timedOut = _didTimeout(abortController);
130
+ _tryMarkInitEnd(args, response, currentAttempt, '', error);
131
+ const fallbackUpdated = yield this._fallbackResolver.tryFetchUpdatedFallbackInfo(args.sdkKey, args.urlConfig, errorMessage, timedOut);
132
+ if (fallbackUpdated) {
133
+ args.fallbackUrl = this._fallbackResolver.getActiveFallbackUrl(args.sdkKey, args.urlConfig);
122
134
  }
123
135
  if (!retries ||
124
136
  currentAttempt > retries ||
@@ -129,40 +141,42 @@ class NetworkCore {
129
141
  tag: StatsigClientEventEmitter_1.ErrorTag.NetworkError,
130
142
  requestArgs: args,
131
143
  });
132
- Log_1.Log.error(`A networking error occured during ${method} request to ${url}.`, errorMessage, error);
144
+ Log_1.Log.error(`A networking error occured during ${method} request to ${populatedUrl}.`, errorMessage, error);
133
145
  return null;
134
146
  }
135
- return this._sendRequest(Object.assign(Object.assign({}, args), { retries: retries, attempt: currentAttempt + 1 }));
147
+ return this._sendRequest(Object.assign(Object.assign({}, args), { retries, attempt: currentAttempt + 1 }));
136
148
  }
137
149
  });
138
150
  }
139
151
  _getPopulatedURL(args) {
152
+ var _a;
140
153
  return __awaiter(this, void 0, void 0, function* () {
154
+ const url = (_a = args.fallbackUrl) !== null && _a !== void 0 ? _a : args.urlConfig.getUrl();
141
155
  const params = Object.assign({ [NetworkConfig_1.NetworkParam.SdkKey]: args.sdkKey, [NetworkConfig_1.NetworkParam.SdkType]: SDKType_1.SDKType._get(args.sdkKey), [NetworkConfig_1.NetworkParam.SdkVersion]: StatsigMetadata_1.SDK_VERSION, [NetworkConfig_1.NetworkParam.Time]: String(Date.now()), [NetworkConfig_1.NetworkParam.SessionID]: SessionID_1.SessionID.get(args.sdkKey) }, args.params);
142
156
  const query = Object.keys(params)
143
157
  .map((key) => {
144
158
  return `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
145
159
  })
146
160
  .join('&');
147
- return `${args.url}${query ? `?${query}` : ''}`;
161
+ return `${url}${query ? `?${query}` : ''}`;
148
162
  });
149
163
  }
150
- _getPopulatedBody(args) {
164
+ _getPopulatedBody(args, data) {
151
165
  return __awaiter(this, void 0, void 0, function* () {
152
- const { data, sdkKey } = args;
166
+ const { sdkKey, fallbackUrl } = args;
153
167
  const stableID = StableID_1.StableID.get(sdkKey);
154
168
  const sessionID = SessionID_1.SessionID.get(sdkKey);
155
169
  const sdkType = SDKType_1.SDKType._get(sdkKey);
156
170
  return JSON.stringify(Object.assign(Object.assign({}, data), { statsigMetadata: Object.assign(Object.assign({}, StatsigMetadata_1.StatsigMetadataProvider.get()), { stableID,
157
171
  sessionID,
158
- sdkType }) }));
172
+ sdkType,
173
+ fallbackUrl }) }));
159
174
  });
160
175
  }
161
176
  _attemptToEncodeString(args, input) {
162
177
  var _a, _b;
163
178
  const win = (0, SafeJs_1._getWindowSafe)();
164
- if (!args.isStatsigEncodable ||
165
- this._options.disableStatsigEncoding ||
179
+ if (this._options.disableStatsigEncoding ||
166
180
  (0, __StatsigGlobal_1._getStatsigGlobalFlag)('no-encode') != null ||
167
181
  !(win === null || win === void 0 ? void 0 : win.btoa)) {
168
182
  return input;
@@ -173,10 +187,15 @@ class NetworkCore {
173
187
  return result;
174
188
  }
175
189
  catch (_c) {
176
- Log_1.Log.warn('/initialize request encoding failed');
190
+ Log_1.Log.warn(`Request encoding failed for ${args.urlConfig.getUrl()}`);
177
191
  return input;
178
192
  }
179
193
  }
194
+ _getInternalRequestArgs(method, args) {
195
+ const fallbackUrl = this._fallbackResolver.getActiveFallbackUrl(args.sdkKey, args.urlConfig);
196
+ return Object.assign(Object.assign({}, args), { method,
197
+ fallbackUrl });
198
+ }
180
199
  }
181
200
  exports.NetworkCore = NetworkCore;
182
201
  const _ensureValidSdkKey = (args) => {
@@ -199,3 +218,23 @@ function _getErrorMessage(controller, error) {
199
218
  }
200
219
  return 'Unknown Error';
201
220
  }
221
+ function _didTimeout(controller) {
222
+ const timeout = (controller === null || controller === void 0 ? void 0 : controller.signal.aborted) &&
223
+ typeof controller.signal.reason === 'string' &&
224
+ controller.signal.reason.includes('Timeout');
225
+ return timeout || false;
226
+ }
227
+ function _tryMarkInitStart(args, attempt) {
228
+ if (args.urlConfig.endpoint !== NetworkConfig_1.Endpoint._initialize) {
229
+ return;
230
+ }
231
+ Diagnostics_1.Diagnostics._markInitNetworkReqStart(args.sdkKey, {
232
+ attempt,
233
+ });
234
+ }
235
+ function _tryMarkInitEnd(args, response, attempt, body, err) {
236
+ if (args.urlConfig.endpoint !== NetworkConfig_1.Endpoint._initialize) {
237
+ return;
238
+ }
239
+ Diagnostics_1.Diagnostics._markInitNetworkReqEnd(args.sdkKey, Diagnostics_1.Diagnostics._getDiagnosticsData(response, attempt, body, err));
240
+ }
@@ -0,0 +1,22 @@
1
+ import { ErrorBoundary } from './ErrorBoundary';
2
+ import { AnyStatsigOptions } from './StatsigOptionsCommon';
3
+ import { UrlConfiguration } from './UrlConfiguration';
4
+ export type FallbackResolverArgs = {
5
+ fallbackUrl: string | null;
6
+ };
7
+ export declare class NetworkFallbackResolver {
8
+ private _fallbackInfo;
9
+ private _errorBoundary;
10
+ private _networkOverrideFunc?;
11
+ private _dnsQueryCooldowns;
12
+ constructor(options: AnyStatsigOptions);
13
+ setErrorBoundary(errorBoundary: ErrorBoundary): void;
14
+ tryBumpExpiryTime(sdkKey: string, urlConfig: UrlConfiguration): void;
15
+ getActiveFallbackUrl(sdkKey: string, urlConfig: UrlConfiguration): string | null;
16
+ getFallbackFromProvided(url: string): string | null;
17
+ tryFetchUpdatedFallbackInfo(sdkKey: string, urlConfig: UrlConfiguration, errorMessage: string | null, timedOut: boolean): Promise<boolean>;
18
+ private _updateFallbackInfoWithNewUrl;
19
+ private _tryFetchFallbackUrlsFromNetwork;
20
+ private _pickNewFallbackUrl;
21
+ }
22
+ export declare function _isDomainFailure(errorMsg: string | null, timedOut: boolean): boolean;
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports._isDomainFailure = exports.NetworkFallbackResolver = void 0;
13
+ const DnsTxtQuery_1 = require("./DnsTxtQuery");
14
+ const Hashing_1 = require("./Hashing");
15
+ const Log_1 = require("./Log");
16
+ const StorageProvider_1 = require("./StorageProvider");
17
+ const DEFAULT_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
18
+ const COOLDOWN_TIME_MS = 4 * 60 * 60 * 1000; // 4 hours
19
+ class NetworkFallbackResolver {
20
+ constructor(options) {
21
+ var _a;
22
+ this._fallbackInfo = null;
23
+ this._errorBoundary = null;
24
+ this._dnsQueryCooldowns = {};
25
+ this._networkOverrideFunc = (_a = options.networkConfig) === null || _a === void 0 ? void 0 : _a.networkOverrideFunc;
26
+ }
27
+ setErrorBoundary(errorBoundary) {
28
+ this._errorBoundary = errorBoundary;
29
+ }
30
+ tryBumpExpiryTime(sdkKey, urlConfig) {
31
+ var _a;
32
+ const info = (_a = this._fallbackInfo) === null || _a === void 0 ? void 0 : _a[urlConfig.endpoint];
33
+ if (!info) {
34
+ return;
35
+ }
36
+ info.expiryTime = Date.now() + DEFAULT_TTL_MS;
37
+ _tryWriteFallbackInfoToCache(sdkKey, Object.assign(Object.assign({}, this._fallbackInfo), { [urlConfig.endpoint]: info }));
38
+ }
39
+ getActiveFallbackUrl(sdkKey, urlConfig) {
40
+ var _a, _b;
41
+ let info = this._fallbackInfo;
42
+ if (info == null) {
43
+ info = (_a = _readFallbackInfoFromCache(sdkKey)) !== null && _a !== void 0 ? _a : {};
44
+ this._fallbackInfo = info;
45
+ }
46
+ const entry = info[urlConfig.endpoint];
47
+ if (!entry || Date.now() > ((_b = entry.expiryTime) !== null && _b !== void 0 ? _b : 0)) {
48
+ delete info[urlConfig.endpoint];
49
+ this._fallbackInfo = info;
50
+ _tryWriteFallbackInfoToCache(sdkKey, this._fallbackInfo);
51
+ return null;
52
+ }
53
+ if (entry.url) {
54
+ return entry.url;
55
+ }
56
+ return null;
57
+ }
58
+ getFallbackFromProvided(url) {
59
+ const path = _extractPathFromUrl(url);
60
+ if (path) {
61
+ return url.replace(path, '');
62
+ }
63
+ return null;
64
+ }
65
+ tryFetchUpdatedFallbackInfo(sdkKey, urlConfig, errorMessage, timedOut) {
66
+ var _a, _b;
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ try {
69
+ if (!_isDomainFailure(errorMessage, timedOut)) {
70
+ return false;
71
+ }
72
+ const canUseNetworkFallbacks = urlConfig.customUrl == null && urlConfig.fallbackUrls == null;
73
+ const urls = canUseNetworkFallbacks
74
+ ? yield this._tryFetchFallbackUrlsFromNetwork(urlConfig)
75
+ : urlConfig.fallbackUrls;
76
+ const newUrl = this._pickNewFallbackUrl((_a = this._fallbackInfo) === null || _a === void 0 ? void 0 : _a[urlConfig.endpoint], urls);
77
+ if (!newUrl) {
78
+ return false;
79
+ }
80
+ this._updateFallbackInfoWithNewUrl(sdkKey, urlConfig.endpoint, newUrl);
81
+ return true;
82
+ }
83
+ catch (error) {
84
+ (_b = this._errorBoundary) === null || _b === void 0 ? void 0 : _b.logError('tryFetchUpdatedFallbackInfo', error);
85
+ return false;
86
+ }
87
+ });
88
+ }
89
+ _updateFallbackInfoWithNewUrl(sdkKey, endpoint, newUrl) {
90
+ var _a, _b, _c;
91
+ const newFallbackInfo = {
92
+ url: newUrl,
93
+ expiryTime: Date.now() + DEFAULT_TTL_MS,
94
+ previous: [],
95
+ };
96
+ const previousInfo = (_a = this._fallbackInfo) === null || _a === void 0 ? void 0 : _a[endpoint];
97
+ if (previousInfo) {
98
+ newFallbackInfo.previous.push(...previousInfo.previous);
99
+ }
100
+ if (newFallbackInfo.previous.length > 10) {
101
+ newFallbackInfo.previous = [];
102
+ }
103
+ const previousUrl = (_c = (_b = this._fallbackInfo) === null || _b === void 0 ? void 0 : _b[endpoint]) === null || _c === void 0 ? void 0 : _c.url;
104
+ if (previousUrl != null) {
105
+ newFallbackInfo.previous.push(previousUrl);
106
+ }
107
+ this._fallbackInfo = Object.assign(Object.assign({}, this._fallbackInfo), { [endpoint]: newFallbackInfo });
108
+ _tryWriteFallbackInfoToCache(sdkKey, this._fallbackInfo);
109
+ }
110
+ _tryFetchFallbackUrlsFromNetwork(urlConfig) {
111
+ var _a;
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const cooldown = this._dnsQueryCooldowns[urlConfig.endpoint];
114
+ if (cooldown && Date.now() < cooldown) {
115
+ return null;
116
+ }
117
+ this._dnsQueryCooldowns[urlConfig.endpoint] = Date.now() + COOLDOWN_TIME_MS;
118
+ const result = [];
119
+ const records = yield (0, DnsTxtQuery_1._fetchTxtRecords)((_a = this._networkOverrideFunc) !== null && _a !== void 0 ? _a : fetch);
120
+ const path = _extractPathFromUrl(urlConfig.defaultUrl);
121
+ for (const record of records) {
122
+ if (!record.startsWith(urlConfig.endpointDnsKey + '=')) {
123
+ continue;
124
+ }
125
+ const parts = record.split('=');
126
+ if (parts.length > 1) {
127
+ let baseUrl = parts[1];
128
+ if (baseUrl.endsWith('/')) {
129
+ baseUrl = baseUrl.slice(0, -1);
130
+ }
131
+ result.push(`https://${baseUrl}${path}`);
132
+ }
133
+ }
134
+ return result;
135
+ });
136
+ }
137
+ _pickNewFallbackUrl(currentFallbackInfo, urls) {
138
+ var _a;
139
+ if (urls == null) {
140
+ return null;
141
+ }
142
+ const previouslyUsed = new Set((_a = currentFallbackInfo === null || currentFallbackInfo === void 0 ? void 0 : currentFallbackInfo.previous) !== null && _a !== void 0 ? _a : []);
143
+ const currentFallbackUrl = currentFallbackInfo === null || currentFallbackInfo === void 0 ? void 0 : currentFallbackInfo.url;
144
+ let found = null;
145
+ for (const loopUrl of urls) {
146
+ const url = loopUrl.endsWith('/') ? loopUrl.slice(0, -1) : loopUrl;
147
+ if (!previouslyUsed.has(loopUrl) && url !== currentFallbackUrl) {
148
+ found = url;
149
+ break;
150
+ }
151
+ }
152
+ return found;
153
+ }
154
+ }
155
+ exports.NetworkFallbackResolver = NetworkFallbackResolver;
156
+ function _isDomainFailure(errorMsg, timedOut) {
157
+ var _a;
158
+ const lowerErrorMsg = (_a = errorMsg === null || errorMsg === void 0 ? void 0 : errorMsg.toLowerCase()) !== null && _a !== void 0 ? _a : '';
159
+ return (timedOut ||
160
+ lowerErrorMsg.includes('uncaught exception') ||
161
+ lowerErrorMsg.includes('failed to fetch') ||
162
+ lowerErrorMsg.includes('networkerror when attempting to fetch resource'));
163
+ }
164
+ exports._isDomainFailure = _isDomainFailure;
165
+ function _getFallbackInfoStorageKey(sdkKey) {
166
+ return `statsig.network_fallback.${(0, Hashing_1._DJB2)(sdkKey)}`;
167
+ }
168
+ function _tryWriteFallbackInfoToCache(sdkKey, info) {
169
+ const hashKey = _getFallbackInfoStorageKey(sdkKey);
170
+ if (!info || Object.keys(info).length === 0) {
171
+ StorageProvider_1.Storage.removeItem(hashKey);
172
+ return;
173
+ }
174
+ StorageProvider_1.Storage.setItem(hashKey, JSON.stringify(info));
175
+ }
176
+ function _readFallbackInfoFromCache(sdkKey) {
177
+ const hashKey = _getFallbackInfoStorageKey(sdkKey);
178
+ const data = StorageProvider_1.Storage.getItem(hashKey);
179
+ if (!data) {
180
+ return null;
181
+ }
182
+ try {
183
+ return JSON.parse(data);
184
+ }
185
+ catch (_a) {
186
+ Log_1.Log.error('Failed to parse FallbackInfo');
187
+ return null;
188
+ }
189
+ }
190
+ function _extractPathFromUrl(urlString) {
191
+ try {
192
+ const url = new URL(urlString);
193
+ return url.pathname;
194
+ }
195
+ catch (error) {
196
+ return null;
197
+ }
198
+ }
@@ -36,9 +36,9 @@ class StatsigClientBase {
36
36
  this._logger = new EventLogger_1.EventLogger(sdkKey, emitter, network, options);
37
37
  this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(sdkKey, options, emitter);
38
38
  this._errorBoundary.wrap(this);
39
- this._errorBoundary.wrap(network);
40
39
  this._errorBoundary.wrap(adapter);
41
40
  this._errorBoundary.wrap(this._logger);
41
+ network.setErrorBoundary(this._errorBoundary);
42
42
  this.dataAdapter = adapter;
43
43
  this.dataAdapter.attach(sdkKey, options);
44
44
  this.storageProvider = StorageProvider_1.Storage;
@@ -22,24 +22,35 @@ const _isExposureEvent = ({ eventName, }) => {
22
22
  };
23
23
  exports._isExposureEvent = _isExposureEvent;
24
24
  const _createGateExposure = (user, gate) => {
25
- var _a, _b;
26
- return _createExposure(GATE_EXPOSURE_NAME, user, gate.details, {
25
+ var _a, _b, _c;
26
+ const metadata = {
27
27
  gate: gate.name,
28
28
  gateValue: String(gate.value),
29
29
  ruleID: gate.ruleID,
30
- }, (_b = (_a = gate.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
30
+ };
31
+ if (((_a = gate.__evaluation) === null || _a === void 0 ? void 0 : _a.version) != null) {
32
+ metadata['configVersion'] = gate.__evaluation.version;
33
+ }
34
+ return _createExposure(GATE_EXPOSURE_NAME, user, gate.details, metadata, (_c = (_b = gate.__evaluation) === null || _b === void 0 ? void 0 : _b.secondary_exposures) !== null && _c !== void 0 ? _c : []);
31
35
  };
32
36
  exports._createGateExposure = _createGateExposure;
33
37
  const _createConfigExposure = (user, config) => {
34
- var _a, _b;
35
- return _createExposure(CONFIG_EXPOSURE_NAME, user, config.details, {
38
+ var _a, _b, _c, _d;
39
+ const metadata = {
36
40
  config: config.name,
37
41
  ruleID: config.ruleID,
38
- }, (_b = (_a = config.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
42
+ };
43
+ if (((_a = config.__evaluation) === null || _a === void 0 ? void 0 : _a.version) != null) {
44
+ metadata['configVersion'] = config.__evaluation.version;
45
+ }
46
+ if (((_b = config.__evaluation) === null || _b === void 0 ? void 0 : _b.passed) != null) {
47
+ metadata['rulePassed'] = String(config.__evaluation.passed);
48
+ }
49
+ return _createExposure(CONFIG_EXPOSURE_NAME, user, config.details, metadata, (_d = (_c = config.__evaluation) === null || _c === void 0 ? void 0 : _c.secondary_exposures) !== null && _d !== void 0 ? _d : []);
39
50
  };
40
51
  exports._createConfigExposure = _createConfigExposure;
41
52
  const _createLayerParameterExposure = (user, layer, parameterName) => {
42
- var _a, _b, _c;
53
+ var _a, _b, _c, _d;
43
54
  const evaluation = layer.__evaluation;
44
55
  const isExplicit = ((_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.explicit_parameters) === null || _a === void 0 ? void 0 : _a.includes(parameterName)) === true;
45
56
  let allocatedExperiment = '';
@@ -48,13 +59,17 @@ const _createLayerParameterExposure = (user, layer, parameterName) => {
48
59
  allocatedExperiment = (_c = evaluation.allocated_experiment_name) !== null && _c !== void 0 ? _c : '';
49
60
  secondaryExposures = evaluation.secondary_exposures;
50
61
  }
51
- return _createExposure(LAYER_EXPOSURE_NAME, user, layer.details, {
62
+ const metadata = {
52
63
  config: layer.name,
53
64
  parameterName,
54
65
  ruleID: layer.ruleID,
55
66
  allocatedExperiment,
56
67
  isExplicitParameter: String(isExplicit),
57
- }, secondaryExposures);
68
+ };
69
+ if (((_d = layer.__evaluation) === null || _d === void 0 ? void 0 : _d.version) != null) {
70
+ metadata['configVersion'] = layer.__evaluation.version;
71
+ }
72
+ return _createExposure(LAYER_EXPOSURE_NAME, user, layer.details, metadata, secondaryExposures);
58
73
  };
59
74
  exports._createLayerParameterExposure = _createLayerParameterExposure;
60
75
  const _addEvaluationDetailsToMetadata = (details, metadata) => {
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "3.4.0";
1
+ export declare const SDK_VERSION = "3.5.0";
2
2
  export type StatsigMetadata = {
3
3
  readonly [key: string]: string | undefined;
4
4
  readonly appVersion?: string;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StatsigMetadataProvider = exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = '3.4.0';
4
+ exports.SDK_VERSION = '3.5.0';
5
5
  let metadata = {
6
6
  sdkVersion: exports.SDK_VERSION,
7
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
@@ -27,6 +27,10 @@ export type NetworkConfigCommon = {
27
27
  * default: `https://featuregates.org/v1/initialize`
28
28
  */
29
29
  logEventUrl?: string;
30
+ /**
31
+ * A list of URLs to try if the primary logEventUrl fails.
32
+ */
33
+ logEventFallbackUrls?: string[];
30
34
  /**
31
35
  * Overrides the default networking layer used by the Statsig client.
32
36
  * By default, the client use `fetch`, but overriding this
@@ -0,0 +1,11 @@
1
+ import { Endpoint } from './NetworkConfig';
2
+ export type EndpointDnsKey = 'i' | 'e' | 'd';
3
+ export declare class UrlConfiguration {
4
+ readonly endpoint: Endpoint;
5
+ readonly endpointDnsKey: EndpointDnsKey;
6
+ readonly defaultUrl: string;
7
+ readonly customUrl: string | null;
8
+ readonly fallbackUrls: string[] | null;
9
+ constructor(endpoint: Endpoint, customUrl: string | undefined | null, customApi: string | undefined | null, fallbackUrls: string[] | undefined | null);
10
+ getUrl(): string;
11
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UrlConfiguration = void 0;
4
+ const NetworkConfig_1 = require("./NetworkConfig");
5
+ const ENDPOINT_DNS_KEY_MAP = {
6
+ [NetworkConfig_1.Endpoint._initialize]: 'i',
7
+ [NetworkConfig_1.Endpoint._rgstr]: 'e',
8
+ [NetworkConfig_1.Endpoint._download_config_specs]: 'd',
9
+ };
10
+ class UrlConfiguration {
11
+ constructor(endpoint, customUrl, customApi, fallbackUrls) {
12
+ this.customUrl = null;
13
+ this.fallbackUrls = null;
14
+ this.endpoint = endpoint;
15
+ this.endpointDnsKey = ENDPOINT_DNS_KEY_MAP[endpoint];
16
+ if (customUrl) {
17
+ this.customUrl = customUrl;
18
+ }
19
+ if (!customUrl && customApi) {
20
+ this.customUrl = `${customApi}/${endpoint}`;
21
+ }
22
+ if (fallbackUrls) {
23
+ this.fallbackUrls = fallbackUrls;
24
+ }
25
+ const defaultApi = NetworkConfig_1.NetworkDefault[endpoint];
26
+ this.defaultUrl = `${defaultApi}/${endpoint}`;
27
+ }
28
+ getUrl() {
29
+ var _a;
30
+ return (_a = this.customUrl) !== null && _a !== void 0 ? _a : this.defaultUrl;
31
+ }
32
+ }
33
+ exports.UrlConfiguration = UrlConfiguration;
package/src/index.d.ts CHANGED
@@ -37,7 +37,7 @@ export * from './StatsigUser';
37
37
  export * from './StorageProvider';
38
38
  export * from './TypedJsonParse';
39
39
  export * from './TypingUtils';
40
- export * from './UrlOverrides';
40
+ export * from './UrlConfiguration';
41
41
  export * from './UUID';
42
42
  export * from './VisibilityObserving';
43
43
  export { EventLogger, Storage, Log, Diagnostics };
package/src/index.js CHANGED
@@ -59,7 +59,7 @@ __exportStar(require("./StatsigUser"), exports);
59
59
  __exportStar(require("./StorageProvider"), exports);
60
60
  __exportStar(require("./TypedJsonParse"), exports);
61
61
  __exportStar(require("./TypingUtils"), exports);
62
- __exportStar(require("./UrlOverrides"), exports);
62
+ __exportStar(require("./UrlConfiguration"), exports);
63
63
  __exportStar(require("./UUID"), exports);
64
64
  __exportStar(require("./VisibilityObserving"), exports);
65
65
  __STATSIG__ = Object.assign(Object.assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { Log: Log_1.Log,
@@ -1 +0,0 @@
1
- export declare function _getOverridableUrl(overrideUrl: string | undefined, overrideApi: string | undefined, defaultEndpoint: string, defaultApi: string): string;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._getOverridableUrl = void 0;
4
- function _getOverridableUrl(overrideUrl, overrideApi, defaultEndpoint, defaultApi) {
5
- if (overrideUrl) {
6
- return overrideUrl;
7
- }
8
- else if (overrideApi) {
9
- return `${overrideApi}${defaultEndpoint}`;
10
- }
11
- else {
12
- return `${defaultApi}${defaultEndpoint}`;
13
- }
14
- }
15
- exports._getOverridableUrl = _getOverridableUrl;