@statsig/client-core 1.0.0 → 1.0.1

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.0.1",
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.0.1";
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.0.1';
5
5
  let metadata = {
6
6
  sdkVersion: exports.SDK_VERSION,
7
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
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