@statsig/client-core 0.0.1-beta.39 → 0.0.1-beta.40

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": "0.0.1-beta.39",
3
+ "version": "0.0.1-beta.40",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -59,7 +59,7 @@ class ErrorBoundary {
59
59
  try {
60
60
  Log_1.Log.warn(`Caught error in ${tag}`, { error });
61
61
  const impl = () => __awaiter(this, void 0, void 0, function* () {
62
- var _a, _b, _c, _d;
62
+ var _a, _b, _c, _d, _e, _f, _g;
63
63
  const unwrapped = (error !== null && error !== void 0 ? error : Error('[Statsig] Error was empty'));
64
64
  const isError = unwrapped instanceof Error;
65
65
  const name = isError ? unwrapped.name : 'No Name';
@@ -75,7 +75,8 @@ class ErrorBoundary {
75
75
  const statsigMetadata = StatsigMetadata_1.StatsigMetadataProvider.get();
76
76
  const info = isError ? unwrapped.stack : _getDescription(unwrapped);
77
77
  const body = JSON.stringify(Object.assign({ tag, exception: name, info }, Object.assign(Object.assign({}, statsigMetadata), { sdkType })));
78
- yield fetch(exports.EXCEPTION_ENDPOINT, {
78
+ const func = (_f = (_e = (_d = this._options) === null || _d === void 0 ? void 0 : _d.networkConfig) === null || _e === void 0 ? void 0 : _e.networkOverrideFunc) !== null && _f !== void 0 ? _f : fetch;
79
+ yield func(exports.EXCEPTION_ENDPOINT, {
79
80
  method: 'POST',
80
81
  headers: {
81
82
  'STATSIG-API-KEY': this._sdkKey,
@@ -85,7 +86,7 @@ class ErrorBoundary {
85
86
  },
86
87
  body,
87
88
  });
88
- (_d = this._emitter) === null || _d === void 0 ? void 0 : _d.call(this, { name: 'error', error });
89
+ (_g = this._emitter) === null || _g === void 0 ? void 0 : _g.call(this, { name: 'error', error });
89
90
  });
90
91
  impl()
91
92
  .then(() => {
@@ -146,14 +146,18 @@ class EventLogger {
146
146
  return true;
147
147
  }
148
148
  _sendEvents(events) {
149
+ var _a, _b;
149
150
  return __awaiter(this, void 0, void 0, function* () {
150
151
  if (this._isLoggingDisabled) {
151
152
  this._saveFailedLogsToStorage(events);
152
153
  return;
153
154
  }
154
155
  try {
155
- const isInForeground = (0, VisibilityObserving_1._isCurrentlyVisible)();
156
- const response = !isInForeground && this._network.isBeaconSupported()
156
+ const isInBackground = !(0, VisibilityObserving_1._isCurrentlyVisible)();
157
+ const shouldUseBeacon = isInBackground &&
158
+ this._network.isBeaconSupported() &&
159
+ ((_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.networkConfig) === null || _b === void 0 ? void 0 : _b.networkOverrideFunc) == null;
160
+ const response = shouldUseBeacon
157
161
  ? yield this._sendEventsViaBeacon(events)
158
162
  : yield this._sendEventsViaPost(events);
159
163
  if (response.success) {
@@ -166,7 +170,7 @@ class EventLogger {
166
170
  this._saveFailedLogsToStorage(events);
167
171
  }
168
172
  }
169
- catch (_a) {
173
+ catch (_c) {
170
174
  Log_1.Log.warn('Failed to flush events.');
171
175
  }
172
176
  });
@@ -4,6 +4,9 @@ export declare const NetworkDefault: {
4
4
  specsApi: "https://assetsconfigcdn.org/v1";
5
5
  };
6
6
  export type NetworkPriority = 'high' | 'low' | 'auto';
7
+ export type NetworkArgs = RequestInit & {
8
+ priority?: NetworkPriority;
9
+ };
7
10
  export declare enum NetworkParam {
8
11
  EventCount = "ec",
9
12
  SdkKey = "k",
@@ -52,7 +52,7 @@ class NetworkCore {
52
52
  });
53
53
  }
54
54
  _sendRequest(args) {
55
- var _a, _b, _c;
55
+ var _a, _b, _c, _d, _e, _f;
56
56
  return __awaiter(this, void 0, void 0, function* () {
57
57
  if (!_ensureValidSdkKey(args)) {
58
58
  return null;
@@ -72,8 +72,10 @@ class NetworkCore {
72
72
  headers: Object.assign({}, args.headers),
73
73
  signal: controller.signal,
74
74
  priority: args.priority,
75
+ keepalive: true,
75
76
  };
76
- response = yield fetch(url, config);
77
+ const func = (_e = (_d = (_c = this._options) === null || _c === void 0 ? void 0 : _c.networkConfig) === null || _d === void 0 ? void 0 : _d.networkOverrideFunc) !== null && _e !== void 0 ? _e : fetch;
78
+ response = yield func(url, config);
77
79
  clearTimeout(handle);
78
80
  if (!response.ok) {
79
81
  const text = yield response.text().catch(() => 'No Text');
@@ -92,7 +94,7 @@ class NetworkCore {
92
94
  const errorMessage = _getErrorMessage(controller, error);
93
95
  Diagnostics_1.Diagnostics.mark();
94
96
  if (!retries || retries <= 0) {
95
- (_c = this._emitter) === null || _c === void 0 ? void 0 : _c.call(this, { name: 'error', error });
97
+ (_f = this._emitter) === null || _f === void 0 ? void 0 : _f.call(this, { name: 'error', error });
96
98
  Log_1.Log.error(`A networking error occured during ${method} request to ${url}.`, errorMessage, error);
97
99
  return null;
98
100
  }
@@ -161,5 +163,5 @@ function _getErrorMessage(controller, error) {
161
163
  if (error instanceof Error) {
162
164
  return `${error.name}: ${error.message}`;
163
165
  }
164
- return null;
166
+ return 'Unknown Error';
165
167
  }
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.0.1-beta.39";
1
+ export declare const SDK_VERSION = "0.0.1-beta.40";
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 = '0.0.1-beta.39';
4
+ exports.SDK_VERSION = '0.0.1-beta.40';
5
5
  let metadata = {
6
6
  sdkVersion: exports.SDK_VERSION,
7
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
@@ -1,4 +1,5 @@
1
1
  import { LogLevel } from './Log';
2
+ import { NetworkArgs } from './NetworkConfig';
2
3
  import { OverrideAdapter } from './OverrideAdapter';
3
4
  /** Options that can be set at init and updated during runtime. */
4
5
  export type StatsigRuntimeMutableOptions = {
@@ -42,6 +43,18 @@ export type NetworkConfigCommon = {
42
43
  * Intended for testing purposes. Prevents any network requests being made.
43
44
  */
44
45
  preventAllNetworkTraffic?: boolean;
46
+ /**
47
+ * Overrides the default networking layer used by the Statsig client.
48
+ * By default, the client use `fetch`, but overriding this
49
+ * you could use `axios` or raw `XMLHttpRequest`
50
+ *
51
+ * default: `Fetch API`
52
+ *
53
+ * @param {string} url Where the request is going.
54
+ * @param {NetworkArgs} args Configuration for the network request.
55
+ * @returns {Response}
56
+ */
57
+ networkOverrideFunc?: (url: string, args: NetworkArgs) => Promise<Response>;
45
58
  };
46
59
  /** Options for configuring a Statsig client. */
47
60
  export type StatsigOptionsCommon<NetworkConfig extends NetworkConfigCommon> = StatsigRuntimeMutableOptions & {
@@ -1,4 +1,4 @@
1
- type StorageProvider = {
1
+ export type StorageProvider = {
2
2
  _getProviderName: () => string;
3
3
  _getItem: (key: string) => Promise<string | null>;
4
4
  _setItem: (key: string, value: string) => Promise<void>;
@@ -66,13 +66,25 @@ catch (error) {
66
66
  }
67
67
  let _main = _localStorageProvider !== null && _localStorageProvider !== void 0 ? _localStorageProvider : _inMemoryProvider;
68
68
  let _current = _main;
69
+ function _inMemoryBreaker(get) {
70
+ try {
71
+ return get();
72
+ }
73
+ catch (error) {
74
+ if (error instanceof Error && error.name === 'SecurityError') {
75
+ exports.Storage._setProvider(_inMemoryProvider);
76
+ return null;
77
+ }
78
+ throw error;
79
+ }
80
+ }
69
81
  exports.Storage = {
70
82
  _getProviderName: () => _current._getProviderName(),
71
- _getItem: (key) => _current._getItem(key),
83
+ _getItem: (key) => __awaiter(void 0, void 0, void 0, function* () { return _inMemoryBreaker(() => _current._getItem(key)); }),
84
+ _getItemSync: (key) => _inMemoryBreaker(() => { var _a, _b; return (_b = (_a = _current._getItemSync) === null || _a === void 0 ? void 0 : _a.call(_current, key)) !== null && _b !== void 0 ? _b : null; }),
72
85
  _setItem: (key, value) => _current._setItem(key, value),
73
86
  _removeItem: (key) => _current._removeItem(key),
74
87
  _getAllKeys: () => _current._getAllKeys(),
75
- _getItemSync: (key) => { var _a, _b; return (_b = (_a = _current._getItemSync) === null || _a === void 0 ? void 0 : _a.call(_current, key)) !== null && _b !== void 0 ? _b : null; },
76
88
  // StorageProviderManagment
77
89
  _setProvider: (newProvider) => {
78
90
  _main = newProvider;
@@ -89,14 +101,14 @@ exports.Storage = {
89
101
  };
90
102
  function _getObjectFromStorage(key) {
91
103
  return __awaiter(this, void 0, void 0, function* () {
92
- const value = yield _current._getItem(key);
104
+ const value = yield exports.Storage._getItem(key);
93
105
  return JSON.parse(value !== null && value !== void 0 ? value : 'null');
94
106
  });
95
107
  }
96
108
  exports._getObjectFromStorage = _getObjectFromStorage;
97
109
  function _setObjectInStorage(key, obj) {
98
110
  return __awaiter(this, void 0, void 0, function* () {
99
- yield _current._setItem(key, JSON.stringify(obj));
111
+ yield exports.Storage._setItem(key, JSON.stringify(obj));
100
112
  });
101
113
  }
102
114
  exports._setObjectInStorage = _setObjectInStorage;