expf-sigma-node.js 0.0.6 → 0.0.7

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 +62 -49
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
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
@@ -5651,16 +5651,21 @@ var SigmaUserData = class {
5651
5651
  var import_node_cache = __toESM(require_node_cache2());
5652
5652
  var sigmaCache = new import_node_cache.default();
5653
5653
  var SigmaCache = class {
5654
+ constructor(postfix) {
5655
+ this.postfix = "";
5656
+ if (postfix)
5657
+ this.postfix = "__" + postfix;
5658
+ }
5654
5659
  get(key) {
5655
5660
  try {
5656
- return sigmaCache.get(key);
5661
+ return sigmaCache.get(key + this.postfix);
5657
5662
  } catch (e) {
5658
5663
  console.log(e);
5659
5664
  }
5660
5665
  }
5661
5666
  set(key, value) {
5662
5667
  try {
5663
- sigmaCache.set(key, value);
5668
+ sigmaCache.set(key + this.postfix, value);
5664
5669
  return;
5665
5670
  } catch (e) {
5666
5671
  console.log(e);
@@ -5777,24 +5782,31 @@ var js_cookie_default = api;
5777
5782
  var import_jshashes = __toESM(require_hashes());
5778
5783
  if (typeof import_node_fetch.default.default !== "undefined")
5779
5784
  import_node_fetch.default.default;
5780
- var cache = new SigmaCache();
5781
5785
  var defaultApi = "https://api.expf.ru/api/v1";
5782
- var sigmaUserData = new SigmaUserData();
5783
5786
  var sigmaUserId = "sigmaUserId";
5784
5787
  var sigmaDataFile = "sigmaDataFile";
5788
+ var sigmaExperiments = "sigmaExperiments";
5785
5789
  var SHA256 = new import_jshashes.default.SHA256();
5786
5790
  var MD5 = new import_jshashes.default.MD5();
5791
+ var max_decimal_64 = 18446744073709552e3;
5792
+ var zeta = 8540717056e4;
5787
5793
  var Sigma = class {
5788
- constructor(token, userData, cacheTTL = 10, api2, retries = 3) {
5794
+ constructor(token, userData, cacheTTL = 10, options = {
5795
+ api: "",
5796
+ retries: "",
5797
+ postfix: ""
5798
+ }) {
5789
5799
  if (!token) {
5790
5800
  throw new Error("Please specify a token");
5791
5801
  }
5792
5802
  this.token = token;
5793
5803
  this.userData = userData || {};
5794
5804
  cacheTTL < 10 ? this.cacheTTL = 10 : this.cacheTTL = cacheTTL;
5795
- this.api = api2 || `${defaultApi}/config.json`;
5796
- this.retries = retries;
5797
- this.sigmaUserData = sigmaUserData;
5805
+ this.api = options.api || `${defaultApi}/config.json`;
5806
+ this.retries = options.retries || 3;
5807
+ this.sigmaUserData = new SigmaUserData();
5808
+ this.postfix = options.postfix || "";
5809
+ this.cache = new SigmaCache(this.postfix);
5798
5810
  this.userData.userId = this.validateUserDataUserId(this.userData.userId);
5799
5811
  this.sigmaUserData.init({
5800
5812
  userId: this.userData.userId || null,
@@ -5803,7 +5815,7 @@ var Sigma = class {
5803
5815
  appVersion: this.userData.appVersion || null,
5804
5816
  custom: this.userData.custom || null
5805
5817
  });
5806
- cache.set(sigmaUserId, this.userData.userId);
5818
+ this.cache.set(sigmaUserId, this.userData.userId);
5807
5819
  this.setUserCookie();
5808
5820
  }
5809
5821
  validateUserDataUserId(userId) {
@@ -5815,8 +5827,8 @@ var Sigma = class {
5815
5827
  if (userId.length === 0) {
5816
5828
  hasMakeId = true;
5817
5829
  }
5818
- if (hasMakeId && cache.get(sigmaUserId)) {
5819
- userId = cache.get(sigmaUserId);
5830
+ if (hasMakeId && this.cache.get(sigmaUserId)) {
5831
+ userId = this.cache.get(sigmaUserId);
5820
5832
  } else if (hasMakeId) {
5821
5833
  userId = this.makeIdCookie();
5822
5834
  }
@@ -5875,10 +5887,10 @@ var Sigma = class {
5875
5887
  }
5876
5888
  async saveToCache() {
5877
5889
  const { api: api2 } = this;
5878
- const localStorageHash = cache.get("sigmaHash");
5890
+ const localStorageHash = this.cache.get("sigmaHash");
5879
5891
  try {
5880
5892
  const timeRecord = Math.floor(Date.now() / 1e3);
5881
- cache.set("sigmaDataFileLastUpdate", timeRecord);
5893
+ this.cache.set("sigmaDataFileLastUpdate", timeRecord);
5882
5894
  } catch (error) {
5883
5895
  throw new Error(`save last update to cache: ${error},
5884
5896
  token: ${this.token}
@@ -5886,19 +5898,19 @@ var Sigma = class {
5886
5898
  }
5887
5899
  try {
5888
5900
  const userGeoData = await this.getUserGeoData();
5889
- if (!cache.get("sigmaGeoData") || cache.get("sigmaGeoData", { location: "none" }) && userGeoData) {
5890
- cache.set("sigmaGeoData", JSON.stringify(userGeoData));
5901
+ if (!this.cache.get("sigmaGeoData") || this.cache.get("sigmaGeoData", { location: "none" }) && userGeoData) {
5902
+ this.cache.set("sigmaGeoData", JSON.stringify(userGeoData));
5891
5903
  this.sigmaUserData.clearFlags();
5892
5904
  }
5893
5905
  } catch (error) {
5894
- cache.set("sigmaGeoData", JSON.stringify({ location: "none" }));
5906
+ this.cache.set("sigmaGeoData", JSON.stringify({ location: "none" }));
5895
5907
  console.error(error);
5896
5908
  }
5897
5909
  let data;
5898
5910
  try {
5899
5911
  data = await this.getDataFile(`${api2}`);
5900
5912
  } catch (error) {
5901
- if (!cache.get(sigmaDataFile)) {
5913
+ if (!this.cache.get(sigmaDataFile)) {
5902
5914
  throw new Error(`
5903
5915
  Get data: ${error},
5904
5916
  token: ${this.token},
@@ -5916,8 +5928,8 @@ var Sigma = class {
5916
5928
  }
5917
5929
  hash = await SHA256.hex(JSON.stringify(data));
5918
5930
  if (!localStorageHash || localStorageHash !== hash) {
5919
- cache.set("sigmaHash", hash);
5920
- cache.set(sigmaDataFile, JSON.stringify(data));
5931
+ this.cache.set("sigmaHash", hash);
5932
+ this.cache.set(sigmaDataFile, JSON.stringify(data));
5921
5933
  this.sigmaUserData.clearFlags();
5922
5934
  }
5923
5935
  } catch (error) {
@@ -5925,17 +5937,17 @@ var Sigma = class {
5925
5937
  }
5926
5938
  }
5927
5939
  setUserCookie() {
5928
- let uin = cache.get(sigmaUserId);
5940
+ let uin = this.cache.get(sigmaUserId);
5929
5941
  if (!uin) {
5930
5942
  uin = js_cookie_default.get(sigmaUserId);
5931
- cache.set(sigmaUserId, uin);
5943
+ this.cache.set(sigmaUserId, uin);
5932
5944
  } else {
5933
5945
  js_cookie_default.set(sigmaUserId, uin);
5934
5946
  }
5935
5947
  if (!uin) {
5936
5948
  uin = this.makeIdCookie();
5937
5949
  js_cookie_default.set(sigmaUserId, uin);
5938
- cache.set(sigmaUserId, uin);
5950
+ this.cache.set(sigmaUserId, uin);
5939
5951
  }
5940
5952
  return;
5941
5953
  }
@@ -5948,7 +5960,7 @@ var Sigma = class {
5948
5960
  }
5949
5961
  hasExpireCache() {
5950
5962
  const lastUpdate = parseInt(
5951
- cache.get("sigmaDataFileLastUpdate")
5963
+ this.cache.get("sigmaDataFileLastUpdate")
5952
5964
  );
5953
5965
  const currentTime = Math.floor(Date.now() / 1e3);
5954
5966
  if (currentTime > lastUpdate + this.cacheTTL) {
@@ -5957,14 +5969,14 @@ var Sigma = class {
5957
5969
  return false;
5958
5970
  }
5959
5971
  updateCache() {
5960
- if (!cache.get("sigmaDataFile") || this.hasExpireCache()) {
5972
+ if (!this.cache.get("sigmaDataFile") || this.hasExpireCache()) {
5961
5973
  return this.saveToCache();
5962
5974
  }
5963
5975
  return true;
5964
5976
  }
5965
5977
  async getFlags() {
5966
5978
  await this.updateCache();
5967
- const flags = cache.parse("sigmaDataFile");
5979
+ const flags = this.cache.parse("sigmaDataFile");
5968
5980
  if (!flags.feature_flags)
5969
5981
  return;
5970
5982
  return flags.feature_flags;
@@ -5974,10 +5986,10 @@ var Sigma = class {
5974
5986
  return false;
5975
5987
  }
5976
5988
  await this.updateCache();
5977
- const cacheKey = cache.parse("sigmaDataFile");
5989
+ const cacheKey = this.cache.parse("sigmaDataFile");
5978
5990
  if (!cacheKey.feature_flags)
5979
5991
  return;
5980
- const geoData = await cache.parse("sigmaGeoData");
5992
+ const geoData = await this.cache.parse("sigmaGeoData");
5981
5993
  this.sigmaUserData.setGeo(geoData);
5982
5994
  let flag = null;
5983
5995
  for (let i = 0; i < cacheKey.feature_flags.length; i++) {
@@ -6079,29 +6091,32 @@ var Sigma = class {
6079
6091
  case "on date":
6080
6092
  return Math.floor(
6081
6093
  new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60 * 60 * 24)
6082
- ) == sigmaUserData.user.date;
6094
+ ) == this.sigmaUserData.user.date;
6083
6095
  case "after time":
6084
6096
  return Math.floor(
6085
6097
  new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60)
6086
- ) < sigmaUserData.user.time;
6098
+ ) < this.sigmaUserData.user.time;
6087
6099
  case "before time":
6088
6100
  return Math.floor(
6089
6101
  new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60)
6090
- ) > sigmaUserData.user.time;
6102
+ ) > this.sigmaUserData.user.time;
6091
6103
  default:
6092
6104
  return false;
6093
6105
  }
6094
6106
  }
6095
6107
  getUserParamValue(paramKey) {
6108
+ if (!paramKey)
6109
+ return false;
6096
6110
  const user = this.sigmaUserData.user;
6097
6111
  if (paramKey.includes(".")) {
6098
6112
  const paramsArray = paramKey.split(".");
6099
6113
  const parent = paramsArray[0];
6100
6114
  const child = paramsArray[1];
6115
+ if (!child)
6116
+ return false;
6101
6117
  const isSetParentInUserData = Object.keys(user).find((i) => i === parent);
6102
- if (!user[isSetParentInUserData]) {
6118
+ if (!user[isSetParentInUserData])
6103
6119
  return false;
6104
- }
6105
6120
  const userObject = Object.entries(user[isSetParentInUserData]);
6106
6121
  for (let [userParamKey, userParamValue] of userObject) {
6107
6122
  if (userParamKey == child) {
@@ -6129,17 +6144,15 @@ var Sigma = class {
6129
6144
  return hash;
6130
6145
  }
6131
6146
  calcUserInGroup(salt, exposureRate, controlBucketPerc, forcedExp = false, layer = null, layerBounds = []) {
6132
- const max_decimal_64 = 2 ** 64;
6133
- const zeta = 8540717056;
6134
- const userId = sigmaUserData.user.userId ? sigmaUserData.user.userId : cache.get(sigmaUserId);
6147
+ const userId = this.sigmaUserData.user.userId ? this.sigmaUserData.user.userId : this.cache.get(sigmaUserId);
6135
6148
  const md5R = MD5.hex(String(userId) + salt);
6136
- const R = this.digestHash(md5R) * zeta * 1e4 / max_decimal_64;
6149
+ const R = this.digestHash(md5R) * zeta / max_decimal_64;
6137
6150
  let md5L = null;
6138
6151
  if (!layer || !layerBounds.length) {
6139
6152
  return this.getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
6140
6153
  } else {
6141
6154
  md5L = MD5.hex(String(userId) + layer);
6142
- const L = this.digestHash(md5L) * zeta * 1e4 / max_decimal_64;
6155
+ const L = this.digestHash(md5L) * zeta / max_decimal_64;
6143
6156
  for (let i in layerBounds) {
6144
6157
  const result = Math.abs(L / 10);
6145
6158
  if (result <= layerBounds[i].lower_bound || layerBounds[i].upper_bound <= result) {
@@ -6171,7 +6184,7 @@ var Sigma = class {
6171
6184
  return false;
6172
6185
  }
6173
6186
  await this.updateCache();
6174
- const sigmaDataLs = cache.parse(sigmaDataFile);
6187
+ const sigmaDataLs = this.cache.parse(sigmaDataFile);
6175
6188
  let experiment = null;
6176
6189
  let layer = null;
6177
6190
  let bounds = [];
@@ -6188,7 +6201,7 @@ var Sigma = class {
6188
6201
  bounds = sigmaDataLs.experiments[i]["layer_bounds"];
6189
6202
  }
6190
6203
  if (sigmaDataLs.experiments[i]["name"] === experimentName && !experiment) {
6191
- forcedUser = this.checkForcedList(sigmaDataLs.experiments[i].forced_user_list, sigmaUserData.user.userId);
6204
+ forcedUser = this.checkForcedList(sigmaDataLs.experiments[i].forced_user_list, this.sigmaUserData.user.userId);
6192
6205
  experiment = sigmaDataLs.experiments[i];
6193
6206
  break;
6194
6207
  }
@@ -6203,13 +6216,13 @@ var Sigma = class {
6203
6216
  layer,
6204
6217
  bounds
6205
6218
  );
6206
- cache.set(localStorageGroupName, userInGroupExperiment);
6219
+ this.cache.set(localStorageGroupName, userInGroupExperiment);
6207
6220
  }
6208
6221
  const getParamValue = (paramName) => {
6209
6222
  if (!paramName || !experiment) {
6210
6223
  return false;
6211
6224
  }
6212
- const groupName = cache.get(localStorageGroupName);
6225
+ const groupName = this.cache.get(localStorageGroupName);
6213
6226
  let params = null;
6214
6227
  for (let i = 0; i < experiment.params.length; i++) {
6215
6228
  if (experiment.params[i]["name"] === paramName) {
@@ -6235,7 +6248,7 @@ var Sigma = class {
6235
6248
  if (!flagName || !experiment) {
6236
6249
  return false;
6237
6250
  }
6238
- const groupName = cache.get(localStorageGroupName);
6251
+ const groupName = this.cache.get(localStorageGroupName);
6239
6252
  let flagExperiment = null;
6240
6253
  for (let i = 0; i < experiment.feature_flags.length; i++) {
6241
6254
  if (experiment.feature_flags[i]["name"] === flagName) {
@@ -6263,7 +6276,7 @@ var Sigma = class {
6263
6276
  }
6264
6277
  async getAllUserExperiments() {
6265
6278
  await this.updateCache();
6266
- const sigmaDataLs = cache.parse(sigmaDataFile);
6279
+ const sigmaDataLs = this.cache.parse(sigmaDataFile);
6267
6280
  if (!sigmaDataLs.experiments.length)
6268
6281
  return false;
6269
6282
  let result = "";
@@ -6279,7 +6292,7 @@ var Sigma = class {
6279
6292
  experiment.salt,
6280
6293
  experiment.allocation,
6281
6294
  experiment.groups,
6282
- this.checkForcedList(experiment.forced_user_list, sigmaUserData.user.userId),
6295
+ this.checkForcedList(experiment.forced_user_list, this.sigmaUserData.user.userId),
6283
6296
  layer,
6284
6297
  bounds
6285
6298
  );
@@ -6289,7 +6302,7 @@ var Sigma = class {
6289
6302
  }
6290
6303
  }
6291
6304
  if (result.length > 0) {
6292
- cache.set("sigmaExperiments", result.slice(0, -1));
6305
+ this.cache.set(sigmaExperiments, result.slice(0, -1));
6293
6306
  return result.slice(0, -1);
6294
6307
  }
6295
6308
  return false;
@@ -6383,19 +6396,19 @@ var Sigma = class {
6383
6396
  return value;
6384
6397
  }
6385
6398
  async getCacheDataFile() {
6386
- const data = await cache.get(sigmaDataFile);
6399
+ const data = await this.cache.get(sigmaDataFile);
6387
6400
  return data;
6388
6401
  }
6389
6402
  async getCacheHash() {
6390
- const data = await cache.get("sigmaHash");
6403
+ const data = await this.cache.get("sigmaHash");
6391
6404
  return data;
6392
6405
  }
6393
6406
  async getCacheGeoData() {
6394
- const data = await cache.get("sigmaGeoData");
6407
+ const data = await this.cache.get("sigmaGeoData");
6395
6408
  return data;
6396
6409
  }
6397
6410
  async getCacheUserId() {
6398
- const data = await cache.get(sigmaUserId);
6411
+ const data = await this.cache.get(sigmaUserId);
6399
6412
  return data;
6400
6413
  }
6401
6414
  };