@statsig/client-core 1.0.0 → 1.1.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": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
package/src/CacheKey.js CHANGED
@@ -3,15 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._getStorageKey = exports._getUserStorageKey = void 0;
4
4
  const Hashing_1 = require("./Hashing");
5
5
  function _getUserStorageKey(sdkKey, user, customKeyGenerator) {
6
- var _a, _b;
6
+ var _a;
7
7
  if (customKeyGenerator) {
8
8
  return customKeyGenerator(sdkKey, user);
9
9
  }
10
+ const cids = user && user.customIDs ? user.customIDs : {};
10
11
  const parts = [
11
12
  `uid:${(_a = user === null || user === void 0 ? void 0 : user.userID) !== null && _a !== void 0 ? _a : ''}`,
12
- `cids:${Object.entries((_b = user === null || user === void 0 ? void 0 : user.customIDs) !== null && _b !== void 0 ? _b : {})
13
- .sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey))
14
- .map(([key, value]) => `${key}-${value}`)
13
+ `cids:${Object.keys(cids)
14
+ .sort((leftKey, rightKey) => leftKey.localeCompare(rightKey))
15
+ .map((key) => `${key}-${cids[key]}`)
15
16
  .join(',')}`,
16
17
  `k:${sdkKey}`,
17
18
  ];
@@ -135,16 +135,11 @@ class DataAdapterCore {
135
135
  return __awaiter(this, void 0, void 0, function* () {
136
136
  const lastModifiedTimeMap = (_a = (yield (0, StorageProvider_1._getObjectFromStorage)(this._lastModifiedStoreKey))) !== null && _a !== void 0 ? _a : {};
137
137
  lastModifiedTimeMap[cacheKey] = Date.now();
138
- const entries = Object.entries(lastModifiedTimeMap);
139
- if (entries.length <= CACHE_LIMIT) {
140
- yield (0, StorageProvider_1._setObjectInStorage)(this._lastModifiedStoreKey, lastModifiedTimeMap);
141
- return;
138
+ const evictable = _getEvictableKey(lastModifiedTimeMap, CACHE_LIMIT);
139
+ if (evictable) {
140
+ delete lastModifiedTimeMap[evictable];
141
+ yield StorageProvider_1.Storage._removeItem(evictable);
142
142
  }
143
- const oldest = entries.reduce((acc, current) => {
144
- return current[1] < acc[1] ? current : acc;
145
- });
146
- delete lastModifiedTimeMap[oldest[0]];
147
- yield StorageProvider_1.Storage._removeItem(oldest[0]);
148
143
  yield (0, StorageProvider_1._setObjectInStorage)(this._lastModifiedStoreKey, lastModifiedTimeMap);
149
144
  });
150
145
  }
@@ -175,18 +170,27 @@ class InMemoryCache {
175
170
  return result;
176
171
  }
177
172
  add(cacheKey, value) {
178
- const entries = Object.entries(this._data);
179
- if (entries.length < CACHE_LIMIT) {
180
- this._data[cacheKey] = value;
181
- return;
173
+ const oldest = _getEvictableKey(this._data, CACHE_LIMIT - 1);
174
+ if (oldest) {
175
+ delete this._data[oldest];
182
176
  }
183
- const [oldest] = entries.reduce((acc, curr) => {
184
- return curr[1] < acc[1] ? curr : acc;
185
- });
186
- delete this._data[oldest];
187
177
  this._data[cacheKey] = value;
188
178
  }
189
179
  merge(values) {
190
180
  this._data = Object.assign(Object.assign({}, this._data), values);
191
181
  }
192
182
  }
183
+ function _getEvictableKey(data, limit) {
184
+ const keys = Object.keys(data);
185
+ if (keys.length <= limit) {
186
+ return null;
187
+ }
188
+ return keys.reduce((prevKey, currKey) => {
189
+ const prev = data[prevKey];
190
+ const current = data[currKey];
191
+ if (typeof prev === 'object' && typeof current === 'object') {
192
+ return current.receivedAt < prev.receivedAt ? currKey : prevKey;
193
+ }
194
+ return current < prev ? currKey : prevKey;
195
+ });
196
+ }
@@ -124,9 +124,9 @@ class NetworkCore {
124
124
  _getPopulatedURL(args) {
125
125
  return __awaiter(this, void 0, void 0, function* () {
126
126
  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]: yield SessionID_1.SessionID.get(args.sdkKey) }, args.params);
127
- const query = Object.entries(params)
128
- .map(([key, value]) => {
129
- return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
127
+ const query = Object.keys(params)
128
+ .map((key) => {
129
+ return `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
130
130
  })
131
131
  .join('&');
132
132
  return `${args.url}${query ? `?${query}` : ''}`;
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "1.0.0";
1
+ export declare const SDK_VERSION = "1.1.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 = '1.0.0';
4
+ exports.SDK_VERSION = '1.1.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
@@ -26,17 +26,6 @@ export type NetworkConfigCommon = {
26
26
  * default: `https://featuregates.org/v1/initialize`
27
27
  */
28
28
  logEventUrl?: string;
29
- /**
30
- * The maximum amount of time (in milliseconds) that any network request can take
31
- * before timing out.
32
- *
33
- * default: `10,000 ms` (10 seconds)
34
- */
35
- networkTimeoutMs?: number;
36
- /**
37
- * Intended for testing purposes. Prevents any network requests being made.
38
- */
39
- preventAllNetworkTraffic?: boolean;
40
29
  /**
41
30
  * Overrides the default networking layer used by the Statsig client.
42
31
  * By default, the client use `fetch`, but overriding this
@@ -49,20 +38,43 @@ export type NetworkConfigCommon = {
49
38
  * @returns {Response}
50
39
  */
51
40
  networkOverrideFunc?: (url: string, args: NetworkArgs) => Promise<Response>;
41
+ /**
42
+ * The maximum amount of time (in milliseconds) that any network request can take
43
+ * before timing out.
44
+ *
45
+ * default: `10,000 ms` (10 seconds)
46
+ */
47
+ networkTimeoutMs?: number;
48
+ /**
49
+ * Intended for testing purposes. Prevents any network requests being made.
50
+ */
51
+ preventAllNetworkTraffic?: boolean;
52
52
  };
53
53
  /** Options for configuring a Statsig client. */
54
54
  export type StatsigOptionsCommon<NetworkConfig extends NetworkConfigCommon> = StatsigRuntimeMutableOptions & {
55
55
  /**
56
- * Allows for fine grained control over which api or urls are hit for specific Statsig network requests.
56
+ * Whether or not Statsig should compress JSON bodies for network requests where possible.
57
57
  *
58
- * For defaults see {@link StatsigClientUrlOverrideOptions}
58
+ * default: `false`
59
59
  */
60
- networkConfig?: NetworkConfig;
60
+ disableCompression?: boolean;
61
+ /**
62
+ * Whether or not Statsig should use raw JSON for network requests where possible.
63
+ *
64
+ * default: `false`
65
+ */
66
+ disableStatsigEncoding?: boolean;
61
67
  /**
62
68
  * An object you can use to set environment variables that apply to all of your users
63
69
  * in the same session.
64
70
  */
65
71
  environment?: StatsigEnvironment;
72
+ /**
73
+ * (Web only) Should the 'current page' url be included with logged events.
74
+ *
75
+ * default: true
76
+ */
77
+ includeCurrentPageUrlWithEvents?: boolean;
66
78
  /**
67
79
  * How much information is allowed to be printed to the console.
68
80
  *
@@ -78,32 +90,20 @@ export type StatsigOptionsCommon<NetworkConfig extends NetworkConfigCommon> = St
78
90
  /**
79
91
  * How often (in milliseconds) to flush logs to Statsig.
80
92
  *
81
- * default: `10,000 ms` (10 seconds)
93
+ * default: `10,000 ms` (10 seconds)
82
94
  */
83
95
  loggingIntervalMs?: number;
84
96
  /**
85
- * An implementor of {@link OverrideAdapter}, used to alter evaluations before its
86
- * returned to the caller of a check api (checkGate/getExperiment etc).
87
- */
88
- overrideAdapter?: OverrideAdapter;
89
- /**
90
- * (Web only) Should the 'current page' url be included with logged events.
91
- *
92
- * default: true
93
- */
94
- includeCurrentPageUrlWithEvents?: boolean;
95
- /**
96
- * Whether or not Statsig should use raw JSON for network requests where possible.
97
+ * Allows for fine grained control over which api or urls are hit for specific Statsig network requests.
97
98
  *
98
- * default: `false`
99
+ * For defaults see {@link StatsigClientUrlOverrideOptions}
99
100
  */
100
- disableStatsigEncoding?: boolean;
101
+ networkConfig?: NetworkConfig;
101
102
  /**
102
- * Whether or not Statsig should compress JSON bodies for network requests where possible.
103
- *
104
- * default: `false`
103
+ * An implementor of {@link OverrideAdapter}, used to alter evaluations before its
104
+ * returned to the caller of a check api (checkGate/getExperiment etc).
105
105
  */
106
- disableCompression?: boolean;
106
+ overrideAdapter?: OverrideAdapter;
107
107
  };
108
108
  export type AnyStatsigOptions = StatsigOptionsCommon<NetworkConfigCommon>;
109
109
  export type StatsigEnvironment = {
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # Statsig - Core
2
-
3
- The package that contains all the common logic shared by the various Statsig Javascript packages.
4
-
5
- Learn more by visiting: https://docs.statsig.com/client/javascript-sdk