expf-sigma-node.js 1.1.8 → 1.2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/sigma.js +64 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "1.1.8",
3
+ "version": "1.2.1",
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
@@ -110,7 +110,9 @@ var SigmaUserData = class {
110
110
  var import_node_cache = __toESM(require("node-cache"));
111
111
  var sigmaCache = new import_node_cache.default();
112
112
  var SigmaCache = class {
113
- constructor(postfix) {
113
+ constructor(postfix, geoCacheTTL) {
114
+ this.geoCacheTTL = geoCacheTTL || null;
115
+ this.sigmaGeoCache = new import_node_cache.default({ stdTTL: this.geoCacheTTL });
114
116
  this.postfix = "";
115
117
  if (postfix)
116
118
  this.postfix = "__" + postfix;
@@ -138,6 +140,47 @@ var SigmaCache = class {
138
140
  }
139
141
  };
140
142
 
143
+ // src/modules/sigmaGeoCache.js
144
+ var import_node_cache2 = __toESM(require("node-cache"));
145
+ var defaultGeoCacheTTL = 5 * 60;
146
+ var maxGeoCacheTTL = 120 * 60;
147
+ var minGeoCacheTTL = 1 * 60;
148
+ var SigmaGeoCache = class {
149
+ constructor(postfix, geoCacheTTL) {
150
+ this.geoCacheTTL = geoCacheTTL || defaultGeoCacheTTL;
151
+ if (geoCacheTTL > maxGeoCacheTTL || geoCacheTTL < minGeoCacheTTL) {
152
+ this.geoCacheTTL = defaultGeoCacheTTL;
153
+ }
154
+ this.sigmaGeoCache = new import_node_cache2.default({ stdTTL: this.geoCacheTTL });
155
+ this.postfix = "";
156
+ if (postfix)
157
+ this.postfix = "__" + postfix;
158
+ }
159
+ get(key) {
160
+ try {
161
+ return this.sigmaGeoCache.get(key + this.postfix);
162
+ } catch (e) {
163
+ console.log(e);
164
+ }
165
+ }
166
+ set(key, value) {
167
+ try {
168
+ return this.sigmaGeoCache.set(key + this.postfix, value);
169
+ } catch (e) {
170
+ console.log(e);
171
+ }
172
+ }
173
+ keys() {
174
+ return this.sigmaGeoCache.keys();
175
+ }
176
+ parse(data) {
177
+ if (!this.get(data)) {
178
+ throw new Error(`${data} not found in cache`);
179
+ }
180
+ return JSON.parse(this.get(data));
181
+ }
182
+ };
183
+
141
184
  // src/modules/sigmaHashes.js
142
185
  var Hashes;
143
186
  function utf8Encode(str) {
@@ -890,7 +933,8 @@ var Sigma = class {
890
933
  constructor(token, userData, cacheTTL = 10, options = {
891
934
  api: "",
892
935
  retries: "",
893
- postfix: ""
936
+ postfix: "",
937
+ geoCacheTTL: null
894
938
  }) {
895
939
  if (!token) {
896
940
  throw new Error("Please specify a token");
@@ -915,6 +959,7 @@ var Sigma = class {
915
959
  os: this.userData.os || null,
916
960
  geo: this.userData.geo || null
917
961
  });
962
+ this.geoCache = new SigmaGeoCache(this.token, options.geoCacheTTL);
918
963
  this.cache.set(sigmaUserId, this.userData.userId);
919
964
  this.setUserCookie();
920
965
  }
@@ -985,20 +1030,19 @@ var Sigma = class {
985
1030
  }
986
1031
  }
987
1032
  async setUserGeoData() {
988
- let jsonUserGeoData = JSON.stringify({ location: "none" });
989
- try {
990
- if (this.userData.ip && !this.userData.geo) {
991
- const userGeoData = await this.getUserGeoData();
992
- if (!this.cache.get(sigmaGeoData) || userGeoData) {
993
- jsonUserGeoData = JSON.stringify(userGeoData);
994
- }
995
- } else if (this.userData.geo) {
996
- jsonUserGeoData = JSON.stringify(this.userData.geo);
997
- }
998
- } catch (error) {
999
- console.log(error);
1033
+ if (!this.userData.ip)
1034
+ return;
1035
+ const geoKey = `${this.userData.ip}__${this.token}`;
1036
+ const foundingGeoKey = this.geoCache.keys().find((k) => k === geoKey);
1037
+ if (foundingGeoKey) {
1038
+ const userGeoData = this.geoCache.get(this.userData.ip);
1039
+ this.sigmaUserData.setGeo(userGeoData);
1040
+ return;
1041
+ } else {
1042
+ const userGeoData = await this.getUserGeoData();
1043
+ this.geoCache.set(this.userData.ip, userGeoData);
1044
+ this.sigmaUserData.setGeo(userGeoData);
1000
1045
  }
1001
- this.cache.set(sigmaGeoData, jsonUserGeoData);
1002
1046
  }
1003
1047
  async saveToCache() {
1004
1048
  const { api } = this;
@@ -1011,7 +1055,6 @@ var Sigma = class {
1011
1055
  token: ${this.token}
1012
1056
  `);
1013
1057
  }
1014
- await this.setUserGeoData();
1015
1058
  let data;
1016
1059
  try {
1017
1060
  data = await this.getDataFile(`${api}/config.json`);
@@ -1051,7 +1094,7 @@ var Sigma = class {
1051
1094
  this.cache.get("sigmaDataFileLastUpdate")
1052
1095
  );
1053
1096
  const currentTime = Math.floor(Date.now() / 1e3);
1054
- if (currentTime > lastUpdate + this.cacheTTL || this.userData.geo || this.userData.ip) {
1097
+ if (currentTime > lastUpdate + this.cacheTTL) {
1055
1098
  return true;
1056
1099
  }
1057
1100
  return false;
@@ -1074,11 +1117,10 @@ var Sigma = class {
1074
1117
  return null;
1075
1118
  }
1076
1119
  await this.updateCache();
1077
- const cacheKey = this.cache.parse("sigmaDataFile");
1120
+ await this.setUserGeoData();
1121
+ const cacheKey = this.cache.parse(sigmaDataFile);
1078
1122
  if (!cacheKey.feature_flags)
1079
1123
  return;
1080
- const geoData = await this.cache.parse("sigmaGeoData");
1081
- this.sigmaUserData.setGeo(geoData);
1082
1124
  let flag = null;
1083
1125
  const expName = this.getExperimentByFeatureFlag(cacheKey, flagName);
1084
1126
  if (expName) {
@@ -1232,6 +1274,7 @@ var Sigma = class {
1232
1274
  return null;
1233
1275
  }
1234
1276
  await this.updateCache();
1277
+ await this.setUserGeoData();
1235
1278
  const sigmaDataLs = await this.cache.parse(sigmaDataFile);
1236
1279
  if (!sigmaDataLs.experiments) {
1237
1280
  throw new Error("Experiments not found in cache");
@@ -1446,7 +1489,7 @@ var Sigma = class {
1446
1489
  return data;
1447
1490
  }
1448
1491
  async getCacheGeoData() {
1449
- const data = await this.cache.get("sigmaGeoData");
1492
+ const data = await this.cache.get(sigmaGeoData);
1450
1493
  return data;
1451
1494
  }
1452
1495
  async getCacheUserId() {