expf-sigma-node.js 3.0.1 → 3.0.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/sigma.js +43 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "expf-sigma-node.js lets you manage features flags and remote config across web, server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
5
5
  "main": "public/sigma.js",
6
6
  "keywords": [
package/public/sigma.js CHANGED
@@ -111,10 +111,13 @@ var SigmaUserData = class {
111
111
  }
112
112
  addFlag(flagName, flagRule) {
113
113
  const { user } = this;
114
- user.featureFlags.push({
115
- name: flagName,
116
- rule: flagRule
117
- });
114
+ const userHasFlag = user.featureFlags.find((item) => item.name === flagName);
115
+ if (!userHasFlag) {
116
+ user.featureFlags.push({
117
+ name: flagName,
118
+ rule: flagRule
119
+ });
120
+ }
118
121
  }
119
122
  clearFlags() {
120
123
  this.user.featureFlags = [];
@@ -148,6 +151,13 @@ var SigmaCache = class {
148
151
  console.log(e);
149
152
  }
150
153
  }
154
+ remove(key) {
155
+ try {
156
+ return sigmaCache.del(key + this.postfix);
157
+ } catch (e) {
158
+ console.log(e);
159
+ }
160
+ }
151
161
  parse(data) {
152
162
  if (!this.get(data)) {
153
163
  throw new Error(`${data} not found in cache`);
@@ -238,7 +248,7 @@ function compareVersions(versionA, versionB, strict = true) {
238
248
 
239
249
  // src/helpers/delay.js
240
250
  function delay(ms) {
241
- new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
251
+ return new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
242
252
  }
243
253
 
244
254
  // src/helpers/digestHash.js
@@ -266,8 +276,8 @@ function doTypeConversion(type, value) {
266
276
  return value;
267
277
  }
268
278
 
269
- // src/helpers/makeIdCookie.js
270
- function makeIdCookie() {
279
+ // src/helpers/generateRandomId.js
280
+ function generateRandomId() {
271
281
  let text = "";
272
282
  let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
273
283
  for (let i = 0; i < 80; i++)
@@ -932,14 +942,17 @@ var defaultApi = "https://api.expf.ru/api/v1";
932
942
  var sigmaUserId = "sigmaUserId";
933
943
  var sigmaProfileId = "sigmaProfileId";
934
944
  var sigmaDeviceId = "sigmaDeviceId";
945
+ var sigmaPrivateId = "sigmaPrivateId";
935
946
  var sigmaHash = "sigmaHash";
936
947
  var sigmaDataFileLastUpdate = "sigmaDataFileLastUpdate";
948
+ var sigmaExpiration = "sigmaExpiration";
937
949
  var sigmaDataFile = "sigmaDataFile";
938
950
  var sigmaExperiments = "sigmaExperiments";
939
951
  var max_decimal_64 = 18446744073709552e3;
940
952
  var zeta = 8540717056e4;
941
953
  var SHA256 = new sigmaHashes_default.SHA256();
942
954
  var MD5 = new sigmaHashes_default.MD5();
955
+ var expirationDate = 24 * 60 * 60 * 1e3;
943
956
 
944
957
  // src/sigma.js
945
958
  if (typeof import_node_fetch.default.default !== "undefined")
@@ -981,40 +994,33 @@ var Sigma = class {
981
994
  });
982
995
  this.geoCache = new SigmaGeoCache(this.token, options.geoCacheTTL);
983
996
  this.setSplitIdsToCache(this.userData);
997
+ this.makePrivateIdInCache();
984
998
  }
985
999
  setSplitIdsToCache(userData) {
986
1000
  for (let [key, value] of Object.entries(userData)) {
1001
+ if (!value || !String(value).trim().length)
1002
+ continue;
987
1003
  switch (key) {
988
1004
  case "userId":
989
- if (value)
990
- this.cache.set(sigmaUserId, String(value).trim());
1005
+ this.cache.set(sigmaUserId, String(value).trim());
1006
+ this.cache.set(sigmaExpiration, new Date().getTime() + expirationDate);
991
1007
  break;
992
1008
  case "profileId":
993
- if (value)
994
- this.cache.set(sigmaProfileId, String(value).trim());
1009
+ this.cache.set(sigmaProfileId, String(value).trim());
995
1010
  break;
996
1011
  case "deviceId":
997
- if (value)
998
- this.cache.set(sigmaDeviceId, String(value).trim());
1012
+ this.cache.set(sigmaDeviceId, String(value).trim());
999
1013
  break;
1000
1014
  }
1001
1015
  }
1002
1016
  }
1003
- validateUserDataUserId(userId) {
1004
- let hasMakeId = false;
1005
- if (typeof userId === "undefined") {
1006
- hasMakeId = true;
1017
+ makePrivateIdInCache() {
1018
+ let privateId = this.cache.get(sigmaPrivateId);
1019
+ if (!privateId) {
1020
+ privateId = generateRandomId();
1021
+ this.cache.set(sigmaPrivateId, privateId);
1007
1022
  }
1008
- userId = String(userId).trim();
1009
- if (userId.length === 0) {
1010
- hasMakeId = true;
1011
- }
1012
- if (hasMakeId && this.cache.get(sigmaUserId)) {
1013
- userId = this.cache.get(sigmaUserId);
1014
- } else if (hasMakeId) {
1015
- userId = makeIdCookie();
1016
- }
1017
- return userId;
1023
+ return privateId;
1018
1024
  }
1019
1025
  retryFetch(url, fetchOptions, retries = 3, timeout) {
1020
1026
  return new Promise((resolve, reject) => {
@@ -1023,8 +1029,9 @@ var Sigma = class {
1023
1029
  const wrapper = (n) => {
1024
1030
  (0, import_node_fetch.default)(url, fetchOptions).then((res) => resolve(res)).catch((err) => {
1025
1031
  if (n > 0) {
1026
- delay(1e3);
1027
- wrapper(--n);
1032
+ delay(1e3).then(() => {
1033
+ wrapper(--n);
1034
+ });
1028
1035
  } else {
1029
1036
  reject(err);
1030
1037
  }
@@ -1042,7 +1049,7 @@ var Sigma = class {
1042
1049
  "ip": this.userData.ip,
1043
1050
  "X-Real-IP": this.userData.ip,
1044
1051
  token: this.token,
1045
- "user-id": this.validateUserDataUserId(this.userData.userId)
1052
+ "user-id": this.cache.get(sigmaPrivateId)
1046
1053
  }
1047
1054
  };
1048
1055
  if (method !== "GET") {
@@ -1315,6 +1322,9 @@ var Sigma = class {
1315
1322
  switch (experiment.split_by) {
1316
1323
  case "userId":
1317
1324
  id = this.cache.get(sigmaUserId) || null;
1325
+ if (this.cache.get(sigmaExpiration) && parseInt(new Date().getTime()) >= parseInt(this.cache.get(sigmaExpiration))) {
1326
+ id = null;
1327
+ }
1318
1328
  break;
1319
1329
  case "profileId":
1320
1330
  id = this.cache.get(sigmaProfileId) || null;
@@ -1462,7 +1472,7 @@ var Sigma = class {
1462
1472
  await this.updateCache();
1463
1473
  const sigmaDataLs = this.cache.parse(sigmaDataFile);
1464
1474
  if (!sigmaDataLs.experiments.length)
1465
- return false;
1475
+ return null;
1466
1476
  let result = "";
1467
1477
  for (let i in sigmaDataLs.experiments) {
1468
1478
  let layer = null;
@@ -1495,6 +1505,8 @@ var Sigma = class {
1495
1505
  if (result.length > 0) {
1496
1506
  this.cache.set(sigmaExperiments, result.slice(0, -1));
1497
1507
  return result.slice(0, -1);
1508
+ } else {
1509
+ this.cache.remove(sigmaExperiments);
1498
1510
  }
1499
1511
  return null;
1500
1512
  }