@statsig/client-core 3.2.0 → 3.3.0-beta.2

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.2.0",
3
+ "version": "3.3.0-beta.2",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -0,0 +1,2 @@
1
+ import { NetworkArgs } from './NetworkConfig';
2
+ export declare function _fetchTxtRecords(networkFunc: (url: string, args: NetworkArgs) => Promise<Response>): Promise<string[]>;
@@ -0,0 +1,63 @@
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._fetchTxtRecords = void 0;
13
+ // See example: https://github.com/statsig-io/private-js-client-monorepo/pull/340
14
+ const FEATURE_ASSETS_DNS_QUERY = new Uint8Array([
15
+ 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d,
16
+ 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x61, 0x73, 0x73, 0x65, 0x74, 0x73,
17
+ 0x03, 0x6f, 0x72, 0x67, 0x00, 0x00, 0x10, 0x00, 0x01,
18
+ ]);
19
+ const DNS_QUERY_ENDPOINT = 'https://cloudflare-dns.com/dns-query';
20
+ const DOMAIN_CHARS = [
21
+ 'i', // initialize
22
+ 'e', // events
23
+ 'd', // dcs
24
+ ];
25
+ const MAX_START_LOOKUP = 200;
26
+ function _fetchTxtRecords(networkFunc) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const response = yield networkFunc(DNS_QUERY_ENDPOINT, {
29
+ method: 'POST',
30
+ headers: {
31
+ 'Content-Type': 'application/dns-message',
32
+ Accept: 'application/dns-message',
33
+ },
34
+ body: FEATURE_ASSETS_DNS_QUERY,
35
+ });
36
+ if (!response.ok) {
37
+ const err = new Error('Failed to fetch TXT records from DNS');
38
+ err.name = 'DnsTxtFetchError';
39
+ throw err;
40
+ }
41
+ const data = yield response.arrayBuffer();
42
+ const bytes = new Uint8Array(data);
43
+ return _parseDnsResponse(bytes);
44
+ });
45
+ }
46
+ exports._fetchTxtRecords = _fetchTxtRecords;
47
+ function _parseDnsResponse(input) {
48
+ // loop until we find the first valid domain char. One of [i=, e=, d=]
49
+ const start = input.findIndex((byte, index) => index < MAX_START_LOOKUP &&
50
+ String.fromCharCode(byte) === '=' &&
51
+ DOMAIN_CHARS.includes(String.fromCharCode(input[index - 1])));
52
+ if (start === -1) {
53
+ const err = new Error('Failed to parse TXT records from DNS');
54
+ err.name = 'DnsTxtParseError';
55
+ throw err;
56
+ }
57
+ // decode the remaining bytes as a string
58
+ let result = '';
59
+ for (let i = start - 1; i < input.length; i++) {
60
+ result += String.fromCharCode(input[i]);
61
+ }
62
+ return result.split(',');
63
+ }
@@ -69,7 +69,11 @@ class ErrorBoundary {
69
69
  }
70
70
  this._seen.add(name);
71
71
  if ((_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.networkConfig) === null || _b === void 0 ? void 0 : _b.preventAllNetworkTraffic) {
72
- (_c = this._emitter) === null || _c === void 0 ? void 0 : _c.call(this, { name: 'error', error });
72
+ (_c = this._emitter) === null || _c === void 0 ? void 0 : _c.call(this, {
73
+ name: 'error',
74
+ error,
75
+ tag,
76
+ });
73
77
  return;
74
78
  }
75
79
  const sdkType = SDKType_1.SDKType._get(this._sdkKey);
@@ -87,7 +91,11 @@ class ErrorBoundary {
87
91
  },
88
92
  body,
89
93
  });
90
- (_g = this._emitter) === null || _g === void 0 ? void 0 : _g.call(this, { name: 'error', error });
94
+ (_g = this._emitter) === null || _g === void 0 ? void 0 : _g.call(this, {
95
+ name: 'error',
96
+ error,
97
+ tag,
98
+ });
91
99
  });
92
100
  impl()
93
101
  .then(() => {
@@ -1,4 +1,5 @@
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';
@@ -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,10 +15,12 @@ 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");
21
22
  const StableID_1 = require("./StableID");
23
+ const StatsigClientEventEmitter_1 = require("./StatsigClientEventEmitter");
22
24
  const StatsigMetadata_1 = require("./StatsigMetadata");
23
25
  const VisibilityObserving_1 = require("./VisibilityObserving");
24
26
  const DEFAULT_TIMEOUT_MS = 10000;
@@ -29,6 +31,7 @@ class NetworkCore {
29
31
  this._timeout = DEFAULT_TIMEOUT_MS;
30
32
  this._netConfig = {};
31
33
  this._options = {};
34
+ this._errorBoundary = null;
32
35
  if (options) {
33
36
  this._options = options;
34
37
  }
@@ -38,18 +41,13 @@ class NetworkCore {
38
41
  if (this._netConfig.networkTimeoutMs) {
39
42
  this._timeout = this._netConfig.networkTimeoutMs;
40
43
  }
44
+ this._fallbackResolver = new NetworkFallbackResolver_1.NetworkFallbackResolver(this._options);
41
45
  }
42
- post(args) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- let body = yield this._getPopulatedBody(args);
45
- if (args.isStatsigEncodable) {
46
- body = this._attemptToEncodeString(args, body);
47
- }
48
- return this._sendRequest(Object.assign({ method: 'POST', body }, args));
49
- });
50
- }
51
- get(args) {
52
- 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);
53
51
  }
54
52
  isBeaconSupported() {
55
53
  return (typeof navigator !== 'undefined' &&
@@ -60,12 +58,27 @@ class NetworkCore {
60
58
  if (!_ensureValidSdkKey(args)) {
61
59
  return false;
62
60
  }
63
- const body = yield this._getPopulatedBody(args);
64
- 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);
65
64
  const nav = navigator;
66
65
  return nav.sendBeacon.bind(nav)(url, body);
67
66
  });
68
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
+ }
69
82
  _sendRequest(args) {
70
83
  var _a, _b, _c;
71
84
  return __awaiter(this, void 0, void 0, function* () {
@@ -77,9 +90,11 @@ class NetworkCore {
77
90
  }
78
91
  const { method, body, retries, attempt } = args;
79
92
  const currentAttempt = attempt !== null && attempt !== void 0 ? attempt : 1;
80
- const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
81
- const handle = setTimeout(() => controller === null || controller === void 0 ? void 0 : controller.abort(`Timeout of ${this._timeout}ms expired.`), this._timeout);
82
- 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);
83
98
  let response = null;
84
99
  const keepalive = (0, VisibilityObserving_1._isUnloading)();
85
100
  try {
@@ -87,76 +102,76 @@ class NetworkCore {
87
102
  method,
88
103
  body,
89
104
  headers: Object.assign({}, args.headers),
90
- signal: controller === null || controller === void 0 ? void 0 : controller.signal,
105
+ signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal,
91
106
  priority: args.priority,
92
107
  keepalive,
93
108
  };
94
- if (args.isInitialize) {
95
- Diagnostics_1.Diagnostics._markInitNetworkReqStart(args.sdkKey, {
96
- attempt: currentAttempt,
97
- });
98
- }
109
+ _tryMarkInitStart(args, currentAttempt);
99
110
  const func = (_a = this._netConfig.networkOverrideFunc) !== null && _a !== void 0 ? _a : fetch;
100
- response = yield func(url, config);
101
- clearTimeout(handle);
111
+ response = yield func(populatedUrl, config);
112
+ clearTimeout(timeoutHandle);
102
113
  if (!response.ok) {
103
114
  const text = yield response.text().catch(() => 'No Text');
104
- const err = new Error(`NetworkError: ${url} ${text}`);
115
+ const err = new Error(`NetworkError: ${populatedUrl} ${text}`);
105
116
  err.name = 'NetworkError';
106
117
  throw err;
107
118
  }
108
119
  const text = yield response.text();
109
- if (args.isInitialize) {
110
- Diagnostics_1.Diagnostics._markInitNetworkReqEnd(args.sdkKey, Diagnostics_1.Diagnostics._getDiagnosticsData(response, currentAttempt, text));
111
- }
120
+ _tryMarkInitEnd(args, response, currentAttempt, text);
121
+ this._fallbackResolver.tryBumpExpiryTime(args.sdkKey, populatedUrl);
112
122
  return {
113
123
  body: text,
114
124
  code: response.status,
115
125
  };
116
126
  }
117
127
  catch (error) {
118
- const errorMessage = _getErrorMessage(controller, error);
119
- if (args.isInitialize) {
120
- 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, populatedUrl, errorMessage, timedOut);
132
+ if (fallbackUpdated) {
133
+ args.fallbackUrl = this._fallbackResolver.getFallbackUrl(args.sdkKey, args.url);
121
134
  }
122
135
  if (!retries ||
123
136
  currentAttempt > retries ||
124
137
  !RETRYABLE_CODES.has((_b = response === null || response === void 0 ? void 0 : response.status) !== null && _b !== void 0 ? _b : 500)) {
125
- (_c = this._emitter) === null || _c === void 0 ? void 0 : _c.call(this, { name: 'error', error });
126
- Log_1.Log.error(`A networking error occured during ${method} request to ${url}.`, errorMessage, error);
138
+ (_c = this._emitter) === null || _c === void 0 ? void 0 : _c.call(this, { name: 'error', error, tag: StatsigClientEventEmitter_1.ErrorTag.NetworkError });
139
+ Log_1.Log.error(`A networking error occured during ${method} request to ${populatedUrl}.`, errorMessage, error);
127
140
  return null;
128
141
  }
129
- return this._sendRequest(Object.assign(Object.assign({}, args), { retries: retries, attempt: currentAttempt + 1 }));
142
+ return this._sendRequest(Object.assign(Object.assign({}, args), { retries, attempt: currentAttempt + 1 }));
130
143
  }
131
144
  });
132
145
  }
133
146
  _getPopulatedURL(args) {
147
+ var _a;
134
148
  return __awaiter(this, void 0, void 0, function* () {
149
+ const url = (_a = args.fallbackUrl) !== null && _a !== void 0 ? _a : args.url;
135
150
  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);
136
151
  const query = Object.keys(params)
137
152
  .map((key) => {
138
153
  return `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
139
154
  })
140
155
  .join('&');
141
- return `${args.url}${query ? `?${query}` : ''}`;
156
+ return `${url}${query ? `?${query}` : ''}`;
142
157
  });
143
158
  }
144
- _getPopulatedBody(args) {
159
+ _getPopulatedBody(args, data) {
145
160
  return __awaiter(this, void 0, void 0, function* () {
146
- const { data, sdkKey } = args;
161
+ const { sdkKey, fallbackUrl } = args;
147
162
  const stableID = StableID_1.StableID.get(sdkKey);
148
163
  const sessionID = SessionID_1.SessionID.get(sdkKey);
149
164
  const sdkType = SDKType_1.SDKType._get(sdkKey);
150
165
  return JSON.stringify(Object.assign(Object.assign({}, data), { statsigMetadata: Object.assign(Object.assign({}, StatsigMetadata_1.StatsigMetadataProvider.get()), { stableID,
151
166
  sessionID,
152
- sdkType }) }));
167
+ sdkType,
168
+ fallbackUrl }) }));
153
169
  });
154
170
  }
155
171
  _attemptToEncodeString(args, input) {
156
172
  var _a, _b;
157
173
  const win = (0, SafeJs_1._getWindowSafe)();
158
- if (!args.isStatsigEncodable ||
159
- this._options.disableStatsigEncoding ||
174
+ if (this._options.disableStatsigEncoding ||
160
175
  (0, __StatsigGlobal_1._getStatsigGlobalFlag)('no-encode') != null ||
161
176
  !(win === null || win === void 0 ? void 0 : win.btoa)) {
162
177
  return input;
@@ -171,6 +186,11 @@ class NetworkCore {
171
186
  return input;
172
187
  }
173
188
  }
189
+ _getInternalRequestArgs(method, args) {
190
+ const fallbackUrl = this._fallbackResolver.getFallbackUrl(args.sdkKey, args.url);
191
+ return Object.assign(Object.assign({}, args), { method,
192
+ fallbackUrl });
193
+ }
174
194
  }
175
195
  exports.NetworkCore = NetworkCore;
176
196
  const _ensureValidSdkKey = (args) => {
@@ -193,3 +213,23 @@ function _getErrorMessage(controller, error) {
193
213
  }
194
214
  return 'Unknown Error';
195
215
  }
216
+ function _didTimeout(controller) {
217
+ const timeout = (controller === null || controller === void 0 ? void 0 : controller.signal.aborted) &&
218
+ typeof controller.signal.reason === 'string' &&
219
+ controller.signal.reason.includes('Timeout');
220
+ return timeout || false;
221
+ }
222
+ function _tryMarkInitStart(args, attempt) {
223
+ if (!args.isInitialize) {
224
+ return;
225
+ }
226
+ Diagnostics_1.Diagnostics._markInitNetworkReqStart(args.sdkKey, {
227
+ attempt,
228
+ });
229
+ }
230
+ function _tryMarkInitEnd(args, response, attempt, body, err) {
231
+ if (!args.isInitialize) {
232
+ return;
233
+ }
234
+ Diagnostics_1.Diagnostics._markInitNetworkReqEnd(args.sdkKey, Diagnostics_1.Diagnostics._getDiagnosticsData(response, attempt, body, err));
235
+ }
@@ -0,0 +1,20 @@
1
+ import { ErrorBoundary } from './ErrorBoundary';
2
+ import { AnyStatsigOptions } from './StatsigOptionsCommon';
3
+ export type FallbackResolverArgs = {
4
+ fallbackUrl: string | null;
5
+ };
6
+ export declare class NetworkFallbackResolver {
7
+ private _fallbackInfo;
8
+ private _errorBoundary;
9
+ private _networkOverrideFunc?;
10
+ private _cooldowns;
11
+ constructor(options: AnyStatsigOptions);
12
+ setErrorBoundary(errorBoundary: ErrorBoundary): void;
13
+ tryBumpExpiryTime(sdkKey: string, url: string): void;
14
+ getFallbackUrl(sdkKey: string, url: string): string | null;
15
+ tryFetchUpdatedFallbackInfo(sdkKey: string, url: string, errorMessage: string | null, timedOut: boolean): Promise<boolean>;
16
+ private _updateFallbackInfoWithNewUrl;
17
+ private _fetchFallbackUrl;
18
+ }
19
+ export declare function _isDefaultUrl(url: string): boolean;
20
+ export declare function _isDomainFailure(errorMsg: string | null, timedOut: boolean): boolean;
@@ -0,0 +1,208 @@
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._isDefaultUrl = exports.NetworkFallbackResolver = void 0;
13
+ const DnsTxtQuery_1 = require("./DnsTxtQuery");
14
+ const Hashing_1 = require("./Hashing");
15
+ const Log_1 = require("./Log");
16
+ const NetworkConfig_1 = require("./NetworkConfig");
17
+ const StorageProvider_1 = require("./StorageProvider");
18
+ const DEFAULT_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
19
+ const COOLDOWN_TIME_MS = 4 * 60 * 60 * 1000; // 4 hours
20
+ class NetworkFallbackResolver {
21
+ constructor(options) {
22
+ var _a;
23
+ this._fallbackInfo = null;
24
+ this._errorBoundary = null;
25
+ this._cooldowns = {};
26
+ this._networkOverrideFunc = (_a = options.networkConfig) === null || _a === void 0 ? void 0 : _a.networkOverrideFunc;
27
+ }
28
+ setErrorBoundary(errorBoundary) {
29
+ this._errorBoundary = errorBoundary;
30
+ }
31
+ tryBumpExpiryTime(sdkKey, url) {
32
+ var _a;
33
+ const domainKey = _getDomainKeyFromEndpoint(url);
34
+ if (!domainKey) {
35
+ return;
36
+ }
37
+ const info = (_a = this._fallbackInfo) === null || _a === void 0 ? void 0 : _a[domainKey];
38
+ if (!info) {
39
+ return;
40
+ }
41
+ info.expiryTime = Date.now() + DEFAULT_TTL_MS;
42
+ _tryWriteFallbackInfoToCache(sdkKey, Object.assign(Object.assign({}, this._fallbackInfo), { [domainKey]: info }));
43
+ }
44
+ getFallbackUrl(sdkKey, url) {
45
+ var _a, _b;
46
+ const domainKey = _getDomainKeyFromEndpoint(url);
47
+ if (!_isDefaultUrl(url) || !domainKey) {
48
+ return null;
49
+ }
50
+ let info = this._fallbackInfo;
51
+ if (info == null) {
52
+ info = (_a = _readFallbackInfoFromCache(sdkKey)) !== null && _a !== void 0 ? _a : {};
53
+ this._fallbackInfo = info;
54
+ }
55
+ const entry = info[domainKey];
56
+ if (!entry || Date.now() > ((_b = entry.expiryTime) !== null && _b !== void 0 ? _b : 0)) {
57
+ delete info[domainKey];
58
+ this._fallbackInfo = info;
59
+ _tryWriteFallbackInfoToCache(sdkKey, this._fallbackInfo);
60
+ return null;
61
+ }
62
+ const endpoint = _extractEndpointForUrl(url);
63
+ if (entry.url) {
64
+ return `https://${entry.url}/${endpoint}`;
65
+ }
66
+ return null;
67
+ }
68
+ tryFetchUpdatedFallbackInfo(sdkKey, url, errorMessage, timedOut) {
69
+ var _a;
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ try {
72
+ const domainKey = _getDomainKeyFromEndpoint(url);
73
+ if (!_isDomainFailure(errorMessage, timedOut) || !domainKey) {
74
+ return false;
75
+ }
76
+ if (this._cooldowns[domainKey] &&
77
+ Date.now() < this._cooldowns[domainKey]) {
78
+ return false;
79
+ }
80
+ this._cooldowns[domainKey] = Date.now() + COOLDOWN_TIME_MS;
81
+ const newUrl = yield this._fetchFallbackUrl(domainKey);
82
+ if (!newUrl) {
83
+ return false;
84
+ }
85
+ this._updateFallbackInfoWithNewUrl(sdkKey, domainKey, newUrl);
86
+ return true;
87
+ }
88
+ catch (error) {
89
+ (_a = this._errorBoundary) === null || _a === void 0 ? void 0 : _a.logError('tryFetchUpdatedFallbackInfo', error);
90
+ return false;
91
+ }
92
+ });
93
+ }
94
+ _updateFallbackInfoWithNewUrl(sdkKey, domainKey, newUrl) {
95
+ var _a, _b, _c;
96
+ const newFallbackInfo = {
97
+ url: newUrl,
98
+ expiryTime: Date.now() + DEFAULT_TTL_MS,
99
+ previous: [],
100
+ };
101
+ const previousInfo = (_a = this._fallbackInfo) === null || _a === void 0 ? void 0 : _a[domainKey];
102
+ if (previousInfo) {
103
+ newFallbackInfo.previous.push(...previousInfo.previous);
104
+ }
105
+ if (newFallbackInfo.previous.length > 10) {
106
+ newFallbackInfo.previous = [];
107
+ }
108
+ const previousUrl = (_c = (_b = this._fallbackInfo) === null || _b === void 0 ? void 0 : _b[domainKey]) === null || _c === void 0 ? void 0 : _c.url;
109
+ if (previousUrl != null) {
110
+ newFallbackInfo.previous.push(previousUrl);
111
+ }
112
+ this._fallbackInfo = Object.assign(Object.assign({}, this._fallbackInfo), { [domainKey]: newFallbackInfo });
113
+ _tryWriteFallbackInfoToCache(sdkKey, this._fallbackInfo);
114
+ }
115
+ _fetchFallbackUrl(domainKey) {
116
+ var _a, _b, _c, _d, _e, _f;
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ const records = yield (0, DnsTxtQuery_1._fetchTxtRecords)((_a = this._networkOverrideFunc) !== null && _a !== void 0 ? _a : fetch);
119
+ if (records.length === 0) {
120
+ return null;
121
+ }
122
+ const seen = new Set((_d = (_c = (_b = this._fallbackInfo) === null || _b === void 0 ? void 0 : _b[domainKey]) === null || _c === void 0 ? void 0 : _c.previous) !== null && _d !== void 0 ? _d : []);
123
+ const currentUrl = (_f = (_e = this._fallbackInfo) === null || _e === void 0 ? void 0 : _e[domainKey]) === null || _f === void 0 ? void 0 : _f.url;
124
+ let found = null;
125
+ for (const record of records) {
126
+ const [recordKey, recordUrl] = record.split('=');
127
+ if (!recordUrl || recordKey !== domainKey) {
128
+ continue;
129
+ }
130
+ let url = recordUrl;
131
+ if (recordUrl.endsWith('/')) {
132
+ url = recordUrl.slice(0, -1);
133
+ }
134
+ if (!seen.has(recordUrl) && url !== currentUrl) {
135
+ found = url;
136
+ break;
137
+ }
138
+ }
139
+ return found;
140
+ });
141
+ }
142
+ }
143
+ exports.NetworkFallbackResolver = NetworkFallbackResolver;
144
+ function _isDefaultUrl(url) {
145
+ for (const key in NetworkConfig_1.NetworkDefault) {
146
+ if (url.startsWith(NetworkConfig_1.NetworkDefault[key])) {
147
+ return true;
148
+ }
149
+ }
150
+ return false;
151
+ }
152
+ exports._isDefaultUrl = _isDefaultUrl;
153
+ function _isDomainFailure(errorMsg, timedOut) {
154
+ var _a;
155
+ const lowerErrorMsg = (_a = errorMsg === null || errorMsg === void 0 ? void 0 : errorMsg.toLowerCase()) !== null && _a !== void 0 ? _a : '';
156
+ return (timedOut ||
157
+ lowerErrorMsg.includes('uncaught exception') ||
158
+ lowerErrorMsg.includes('failed to fetch') ||
159
+ lowerErrorMsg.includes('networkerror when attempting to fetch resource'));
160
+ }
161
+ exports._isDomainFailure = _isDomainFailure;
162
+ function _getFallbackInfoStorageKey(sdkKey) {
163
+ return `statsig.network_fallback.${(0, Hashing_1._DJB2)(sdkKey)}`;
164
+ }
165
+ function _tryWriteFallbackInfoToCache(sdkKey, info) {
166
+ const hashKey = _getFallbackInfoStorageKey(sdkKey);
167
+ if (!info || Object.keys(info).length === 0) {
168
+ StorageProvider_1.Storage.removeItem(hashKey);
169
+ return;
170
+ }
171
+ StorageProvider_1.Storage.setItem(hashKey, JSON.stringify(info));
172
+ }
173
+ function _readFallbackInfoFromCache(sdkKey) {
174
+ const hashKey = _getFallbackInfoStorageKey(sdkKey);
175
+ const data = StorageProvider_1.Storage.getItem(hashKey);
176
+ if (!data) {
177
+ return null;
178
+ }
179
+ try {
180
+ return JSON.parse(data);
181
+ }
182
+ catch (_a) {
183
+ Log_1.Log.error('Failed to parse FallbackInfo');
184
+ return null;
185
+ }
186
+ }
187
+ function _extractEndpointForUrl(urlString) {
188
+ try {
189
+ const url = new URL(urlString);
190
+ const endpoint = url.pathname.substring(1);
191
+ return endpoint;
192
+ }
193
+ catch (error) {
194
+ return '';
195
+ }
196
+ }
197
+ function _getDomainKeyFromEndpoint(endpoint) {
198
+ if (endpoint.includes('initialize')) {
199
+ return 'i';
200
+ }
201
+ if (endpoint.includes('rgstr')) {
202
+ return 'e';
203
+ }
204
+ if (endpoint.includes('download_config_specs')) {
205
+ return 'd';
206
+ }
207
+ return null;
208
+ }
@@ -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;
@@ -2,6 +2,10 @@ import { DataAdapterResult } from './StatsigDataAdapter';
2
2
  import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
3
3
  import { Flatten } from './TypingUtils';
4
4
  export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready';
5
+ export declare const ErrorTag: {
6
+ readonly NetworkError: "NetworkError";
7
+ };
8
+ export type ErrorTag = (typeof ErrorTag)[keyof typeof ErrorTag];
5
9
  type EventNameToEventDataMap = {
6
10
  values_updated: {
7
11
  status: StatsigLoadingStatus;
@@ -10,6 +14,7 @@ type EventNameToEventDataMap = {
10
14
  session_expired: object;
11
15
  error: {
12
16
  error: unknown;
17
+ tag: ErrorTag | string;
13
18
  };
14
19
  logs_flushed: {
15
20
  events: Record<string, unknown>[];
@@ -1,2 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorTag = void 0;
4
+ exports.ErrorTag = {
5
+ NetworkError: 'NetworkError',
6
+ };
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "3.2.0";
1
+ export declare const SDK_VERSION = "3.3.0-beta.2";
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.2.0';
4
+ exports.SDK_VERSION = '3.3.0-beta.2';
5
5
  let metadata = {
6
6
  sdkVersion: exports.SDK_VERSION,
7
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients