expf-sigma-node.js 0.0.7 → 0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/sigma.js +169 -129
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "0.0.7",
3
+ "version": "0.1.0",
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
@@ -5780,6 +5780,109 @@ var js_cookie_default = api;
5780
5780
 
5781
5781
  // src/sigma.js
5782
5782
  var import_jshashes = __toESM(require_hashes());
5783
+
5784
+ // src/helpers/checkBoolValue.js
5785
+ function checkBoolValue(value) {
5786
+ if (value)
5787
+ return true;
5788
+ return false;
5789
+ }
5790
+
5791
+ // src/helpers/checkForcedList.js
5792
+ function checkForcedList(usersList, data) {
5793
+ if (usersList.length) {
5794
+ for (let id2 in usersList) {
5795
+ if (usersList[id2] == data) {
5796
+ return true;
5797
+ }
5798
+ }
5799
+ }
5800
+ return false;
5801
+ }
5802
+
5803
+ // src/helpers/compareVersions.js
5804
+ function compareVersions(versionA, versionB, strict = true) {
5805
+ let resultCompare;
5806
+ const versionsLength = Math.max(versionA.length, versionB.length);
5807
+ versionA = (versionA + "").split(".");
5808
+ versionB = (versionB + "").split(".");
5809
+ for (let i = 0; i < versionsLength; i++) {
5810
+ if (versionA[i] === void 0) {
5811
+ versionA[i] = "0";
5812
+ }
5813
+ if (versionB[i] === void 0) {
5814
+ versionB[i] = "0";
5815
+ }
5816
+ resultCompare = parseInt(versionA[i], 10) - parseInt(versionB[i], 10);
5817
+ if (resultCompare !== 0) {
5818
+ return resultCompare < 0 ? false : true;
5819
+ }
5820
+ }
5821
+ if (strict) {
5822
+ return false;
5823
+ }
5824
+ if (!strict) {
5825
+ return true;
5826
+ }
5827
+ }
5828
+
5829
+ // src/helpers/delay.js
5830
+ function delay(ms) {
5831
+ new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
5832
+ }
5833
+
5834
+ // src/helpers/digestHash.js
5835
+ function digestHash(string) {
5836
+ let hash = 0, i, chr;
5837
+ if (string.length === 0)
5838
+ return hash;
5839
+ for (i = 0; i < string.length; i++) {
5840
+ chr = string.charCodeAt(i);
5841
+ hash = (hash << 5) - hash + chr;
5842
+ hash |= 0;
5843
+ }
5844
+ return hash;
5845
+ }
5846
+
5847
+ // src/helpers/doTypeConversion.js
5848
+ function doTypeConversion(type, value) {
5849
+ if (type === "string")
5850
+ return value;
5851
+ if (type === "bool") {
5852
+ return value === "true" ? true : false;
5853
+ }
5854
+ if (type === "integer" || type === "number")
5855
+ return parseFloat(value);
5856
+ return value;
5857
+ }
5858
+
5859
+ // src/helpers/makeIdCookie.js
5860
+ function makeIdCookie() {
5861
+ let text = "";
5862
+ let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
5863
+ for (let i = 0; i < 80; i++)
5864
+ text += possible.charAt(Math.floor(Math.random() * possible.length));
5865
+ return text;
5866
+ }
5867
+
5868
+ // src/helpers/getUserGroup.js
5869
+ function getUserGroup(salt, exposureRate, controlBucketPerc, forcedExp) {
5870
+ let userInGroup = "a";
5871
+ let currentDiapason = 0;
5872
+ if (Math.abs(salt) / 100 <= parseFloat(exposureRate) * 100 || forcedExp) {
5873
+ let bucket = Math.abs(salt) % 100;
5874
+ for (let i = 0; i < controlBucketPerc.length; i++) {
5875
+ currentDiapason += controlBucketPerc[i].weight * 100;
5876
+ if (bucket <= currentDiapason) {
5877
+ return userInGroup = controlBucketPerc[i].name;
5878
+ }
5879
+ }
5880
+ return userInGroup;
5881
+ }
5882
+ return false;
5883
+ }
5884
+
5885
+ // src/sigma.js
5783
5886
  if (typeof import_node_fetch.default.default !== "undefined")
5784
5887
  import_node_fetch.default.default;
5785
5888
  var defaultApi = "https://api.expf.ru/api/v1";
@@ -5830,13 +5933,10 @@ var Sigma = class {
5830
5933
  if (hasMakeId && this.cache.get(sigmaUserId)) {
5831
5934
  userId = this.cache.get(sigmaUserId);
5832
5935
  } else if (hasMakeId) {
5833
- userId = this.makeIdCookie();
5936
+ userId = makeIdCookie();
5834
5937
  }
5835
5938
  return userId;
5836
5939
  }
5837
- delay(ms) {
5838
- new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
5839
- }
5840
5940
  retryFetch(url, fetchOptions, retries = 3, timeout) {
5841
5941
  return new Promise((resolve, reject) => {
5842
5942
  if (timeout)
@@ -5844,7 +5944,7 @@ var Sigma = class {
5844
5944
  const wrapper = (n) => {
5845
5945
  (0, import_node_fetch.default)(url, fetchOptions).then((res) => resolve(res)).catch((err) => {
5846
5946
  if (n > 0) {
5847
- this.delay(1e3);
5947
+ delay(1e3);
5848
5948
  wrapper(--n);
5849
5949
  } else {
5850
5950
  reject(err);
@@ -5945,19 +6045,12 @@ var Sigma = class {
5945
6045
  js_cookie_default.set(sigmaUserId, uin);
5946
6046
  }
5947
6047
  if (!uin) {
5948
- uin = this.makeIdCookie();
6048
+ uin = makeIdCookie();
5949
6049
  js_cookie_default.set(sigmaUserId, uin);
5950
6050
  this.cache.set(sigmaUserId, uin);
5951
6051
  }
5952
6052
  return;
5953
6053
  }
5954
- makeIdCookie() {
5955
- let text = "";
5956
- let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
5957
- for (let i = 0; i < 80; i++)
5958
- text += possible.charAt(Math.floor(Math.random() * possible.length));
5959
- return text;
5960
- }
5961
6054
  hasExpireCache() {
5962
6055
  const lastUpdate = parseInt(
5963
6056
  this.cache.get("sigmaDataFileLastUpdate")
@@ -5993,8 +6086,6 @@ var Sigma = class {
5993
6086
  this.sigmaUserData.setGeo(geoData);
5994
6087
  let flag = null;
5995
6088
  for (let i = 0; i < cacheKey.feature_flags.length; i++) {
5996
- if (cacheKey.feature_flags[i]["is_active"] == "false")
5997
- continue;
5998
6089
  if (cacheKey.feature_flags[i]["name"] === flagName) {
5999
6090
  flag = cacheKey.feature_flags[i];
6000
6091
  break;
@@ -6021,30 +6112,6 @@ var Sigma = class {
6021
6112
  }
6022
6113
  return flagValue;
6023
6114
  }
6024
- compareVersions(versionA, versionB, strict = true) {
6025
- let resultCompare;
6026
- const versionsLength = Math.max(versionA.length, versionB.length);
6027
- versionA = (versionA + "").split(".");
6028
- versionB = (versionB + "").split(".");
6029
- for (let i = 0; i < versionsLength; i++) {
6030
- if (versionA[i] === void 0) {
6031
- versionA[i] = "0";
6032
- }
6033
- if (versionB[i] === void 0) {
6034
- versionB[i] = "0";
6035
- }
6036
- resultCompare = parseInt(versionA[i], 10) - parseInt(versionB[i], 10);
6037
- if (resultCompare !== 0) {
6038
- return resultCompare < 0 ? false : true;
6039
- }
6040
- }
6041
- if (strict) {
6042
- return false;
6043
- }
6044
- if (!strict) {
6045
- return true;
6046
- }
6047
- }
6048
6115
  conditionOperation(userValue, conditionValues, operation) {
6049
6116
  !operation ? operation = "=" : operation;
6050
6117
  if (!userValue || typeof userValue === "undefined")
@@ -6055,17 +6122,17 @@ var Sigma = class {
6055
6122
  case "not equal":
6056
6123
  return userValue.toLowerCase().trim() != conditionValues.toLowerCase().trim();
6057
6124
  case "greater":
6058
- return this.compareVersions(conditionValues.trim(), userValue.trim());
6125
+ return compareVersions(conditionValues.trim(), userValue.trim());
6059
6126
  case "less":
6060
- return this.compareVersions(userValue.trim(), conditionValues.trim());
6127
+ return compareVersions(userValue.trim(), conditionValues.trim());
6061
6128
  case "greater equal":
6062
- return this.compareVersions(
6129
+ return compareVersions(
6063
6130
  conditionValues.trim(),
6064
6131
  userValue.trim(),
6065
6132
  false
6066
6133
  );
6067
6134
  case "less equal":
6068
- return this.compareVersions(
6135
+ return compareVersions(
6069
6136
  userValue.trim(),
6070
6137
  conditionValues.trim(),
6071
6138
  false
@@ -6104,10 +6171,11 @@ var Sigma = class {
6104
6171
  return false;
6105
6172
  }
6106
6173
  }
6107
- getUserParamValue(paramKey) {
6174
+ getUserParamValue(paramKey, privateFlag = null) {
6108
6175
  if (!paramKey)
6109
6176
  return false;
6110
6177
  const user = this.sigmaUserData.user;
6178
+ let defaultValue = null;
6111
6179
  if (paramKey.includes(".")) {
6112
6180
  const paramsArray = paramKey.split(".");
6113
6181
  const parent = paramsArray[0];
@@ -6115,107 +6183,87 @@ var Sigma = class {
6115
6183
  if (!child)
6116
6184
  return false;
6117
6185
  const isSetParentInUserData = Object.keys(user).find((i) => i === parent);
6118
- if (!user[isSetParentInUserData])
6186
+ if (!user[isSetParentInUserData]) {
6119
6187
  return false;
6188
+ }
6120
6189
  const userObject = Object.entries(user[isSetParentInUserData]);
6121
6190
  for (let [userParamKey, userParamValue] of userObject) {
6122
6191
  if (userParamKey == child) {
6123
- return userParamValue;
6192
+ defaultValue = userParamValue;
6193
+ break;
6124
6194
  }
6125
6195
  }
6126
6196
  } else {
6127
6197
  for (let [key, value] of Object.entries(user)) {
6128
6198
  if (key == paramKey) {
6129
- return value;
6199
+ defaultValue = value;
6200
+ break;
6130
6201
  }
6131
6202
  }
6132
6203
  }
6133
- return false;
6134
- }
6135
- digestHash(string) {
6136
- let hash = 0, i, chr;
6137
- if (string.length === 0)
6138
- return hash;
6139
- for (i = 0; i < string.length; i++) {
6140
- chr = string.charCodeAt(i);
6141
- hash = (hash << 5) - hash + chr;
6142
- hash |= 0;
6143
- }
6144
- return hash;
6204
+ if (privateFlag && defaultValue)
6205
+ return MD5.hex(defaultValue);
6206
+ return defaultValue;
6145
6207
  }
6146
6208
  calcUserInGroup(salt, exposureRate, controlBucketPerc, forcedExp = false, layer = null, layerBounds = []) {
6147
6209
  const userId = this.sigmaUserData.user.userId ? this.sigmaUserData.user.userId : this.cache.get(sigmaUserId);
6148
6210
  const md5R = MD5.hex(String(userId) + salt);
6149
- const R = this.digestHash(md5R) * zeta / max_decimal_64;
6211
+ const R = digestHash(md5R) * zeta / max_decimal_64;
6150
6212
  let md5L = null;
6151
- if (!layer || !layerBounds.length) {
6152
- return this.getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
6213
+ if (!layer && !layerBounds.length || forcedExp) {
6214
+ return getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
6153
6215
  } else {
6154
6216
  md5L = MD5.hex(String(userId) + layer);
6155
- const L = this.digestHash(md5L) * zeta / max_decimal_64;
6217
+ const L = digestHash(md5L) * zeta / max_decimal_64;
6156
6218
  for (let i in layerBounds) {
6157
6219
  const result = Math.abs(L / 10);
6158
6220
  if (result <= layerBounds[i].lower_bound || layerBounds[i].upper_bound <= result) {
6159
6221
  continue;
6160
6222
  } else {
6161
- return this.getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
6223
+ return getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
6162
6224
  }
6163
6225
  }
6164
6226
  }
6165
6227
  return false;
6166
6228
  }
6167
- getUserGroup(salt, exposureRate, controlBucketPerc, forcedExp) {
6168
- let userInGroup = "a";
6169
- let currentDiaposon = 0;
6170
- if (Math.abs(salt) / 100 <= parseFloat(exposureRate) * 100 || forcedExp) {
6171
- let bucket = Math.abs(salt) % 100;
6172
- for (let i = 0; i < controlBucketPerc.length; i++) {
6173
- currentDiaposon += controlBucketPerc[i].weight * 100;
6174
- if (bucket <= currentDiaposon) {
6175
- return userInGroup = controlBucketPerc[i].name;
6176
- }
6177
- }
6178
- return userInGroup;
6179
- }
6180
- return false;
6181
- }
6182
6229
  async getExperiment(experimentName) {
6183
6230
  if (!experimentName) {
6184
6231
  return false;
6185
6232
  }
6186
6233
  await this.updateCache();
6187
- const sigmaDataLs = this.cache.parse(sigmaDataFile);
6234
+ const sigmaDataLs = await this.cache.parse(sigmaDataFile);
6235
+ if (!sigmaDataLs.experiments) {
6236
+ throw new Error("Experiments not found in cache");
6237
+ }
6188
6238
  let experiment = null;
6189
6239
  let layer = null;
6190
6240
  let bounds = [];
6191
6241
  let userInGroupExperiment = null;
6192
6242
  let forcedUser = false;
6193
- if (!sigmaDataLs.experiments) {
6194
- throw new Error("experiments in cache not found");
6195
- }
6196
6243
  for (let i = 0; i < sigmaDataLs.experiments.length; i++) {
6197
- if (sigmaDataLs.experiments[i]["is_active"] == "false")
6198
- continue;
6199
- if (sigmaDataLs.experiments[i]["name"] === experimentName && this.checkBoolValue(sigmaDataLs.experiments[i]["layer_id"])) {
6244
+ if (sigmaDataLs.experiments[i]["name"] === experimentName && checkBoolValue(sigmaDataLs.experiments[i]["layer_id"])) {
6200
6245
  layer = sigmaDataLs.experiments[i]["layer_id"];
6201
6246
  bounds = sigmaDataLs.experiments[i]["layer_bounds"];
6202
6247
  }
6203
6248
  if (sigmaDataLs.experiments[i]["name"] === experimentName && !experiment) {
6204
- forcedUser = this.checkForcedList(sigmaDataLs.experiments[i].forced_user_list, this.sigmaUserData.user.userId);
6249
+ forcedUser = checkForcedList(sigmaDataLs.experiments[i].forced_user_list, this.sigmaUserData.user.userId);
6205
6250
  experiment = sigmaDataLs.experiments[i];
6206
6251
  break;
6207
6252
  }
6208
6253
  }
6209
6254
  const localStorageGroupName = `sigma_group_${experimentName}`;
6210
6255
  if (experiment) {
6211
- userInGroupExperiment = this.calcUserInGroup(
6212
- experiment.salt,
6213
- experiment.allocation,
6214
- experiment.groups,
6215
- forcedUser,
6216
- layer,
6217
- bounds
6218
- );
6256
+ userInGroupExperiment = this.findUserInForcedGroup(experiment);
6257
+ if (!userInGroupExperiment) {
6258
+ userInGroupExperiment = this.calcUserInGroup(
6259
+ experiment.salt,
6260
+ experiment.allocation,
6261
+ experiment.groups,
6262
+ forcedUser,
6263
+ layer,
6264
+ bounds
6265
+ );
6266
+ }
6219
6267
  this.cache.set(localStorageGroupName, userInGroupExperiment);
6220
6268
  }
6221
6269
  const getParamValue = (paramName) => {
@@ -6236,7 +6284,7 @@ var Sigma = class {
6236
6284
  let paramValue = null;
6237
6285
  for (let item in params.values) {
6238
6286
  if (params.values[item].group === groupName) {
6239
- paramValue = params.values[item].value;
6287
+ paramValue = doTypeConversion(params.type, params.values[item].value);
6240
6288
  }
6241
6289
  }
6242
6290
  if (!paramValue) {
@@ -6284,18 +6332,21 @@ var Sigma = class {
6284
6332
  const experiment = sigmaDataLs.experiments[i];
6285
6333
  let layer = null;
6286
6334
  let bounds = [];
6287
- if (this.checkBoolValue(experiment["layer_id"])) {
6335
+ let group = this.findUserInForcedGroup(experiment);
6336
+ if (checkBoolValue(experiment["layer_id"]) && !group) {
6288
6337
  layer = experiment["layer_id"];
6289
6338
  bounds = experiment["layer_bounds"];
6290
6339
  }
6291
- const group = this.calcUserInGroup(
6292
- experiment.salt,
6293
- experiment.allocation,
6294
- experiment.groups,
6295
- this.checkForcedList(experiment.forced_user_list, this.sigmaUserData.user.userId),
6296
- layer,
6297
- bounds
6298
- );
6340
+ if (!group) {
6341
+ group = this.calcUserInGroup(
6342
+ experiment.salt,
6343
+ experiment.allocation,
6344
+ experiment.groups,
6345
+ checkForcedList(experiment.forced_user_list, this.sigmaUserData.user.userId),
6346
+ layer,
6347
+ bounds
6348
+ );
6349
+ }
6299
6350
  if (group) {
6300
6351
  const groupIndex = experiment.groups.findIndex((i2) => i2.name == group);
6301
6352
  result = result.concat(experiment.name).concat(".").concat(groupIndex).concat("|");
@@ -6307,20 +6358,19 @@ var Sigma = class {
6307
6358
  }
6308
6359
  return false;
6309
6360
  }
6310
- checkForcedList(usersList, data) {
6311
- if (usersList.length) {
6312
- for (let id2 in usersList) {
6313
- if (usersList[id2] == data) {
6314
- return true;
6361
+ findUserInForcedGroup(experiment) {
6362
+ let groupName = false;
6363
+ for (const groupItem in experiment.groups) {
6364
+ const forcedList = experiment.groups[groupItem].forced_user_list;
6365
+ if (forcedList && forcedList.length) {
6366
+ for (const item in forcedList) {
6367
+ if (forcedList[item] === this.sigmaUserData.user.userId) {
6368
+ return groupName = experiment.groups[groupItem].name;
6369
+ }
6315
6370
  }
6316
6371
  }
6317
6372
  }
6318
- return false;
6319
- }
6320
- checkBoolValue(value) {
6321
- if (value)
6322
- return true;
6323
- return false;
6373
+ return groupName;
6324
6374
  }
6325
6375
  findingSpotCondition(flag, saveToUser) {
6326
6376
  let flagRules = flag.rules;
@@ -6347,7 +6397,7 @@ var Sigma = class {
6347
6397
  const conditions = flagRules[rule].conditions;
6348
6398
  let results = [];
6349
6399
  for (let i in conditions) {
6350
- let userValue = this.getUserParamValue(conditions[i].name);
6400
+ let userValue = this.getUserParamValue(conditions[i].name, flag.is_private);
6351
6401
  results.push(
6352
6402
  this.conditionOperation(
6353
6403
  userValue,
@@ -6365,7 +6415,7 @@ var Sigma = class {
6365
6415
  const findingItem = results.find((item) => item === false);
6366
6416
  findingItem === false ? isSuccessRule : isSuccessRule = true;
6367
6417
  }
6368
- let ruleValue = this.doTypeConversion(flag.type, flagRules[rule].value);
6418
+ let ruleValue = doTypeConversion(flag.type, flagRules[rule].value);
6369
6419
  if (isSuccessRule) {
6370
6420
  if (saveToUser) {
6371
6421
  saveToUser.addFlag(flag.name, {
@@ -6377,7 +6427,7 @@ var Sigma = class {
6377
6427
  return ruleValue;
6378
6428
  }
6379
6429
  }
6380
- let defaultValue = this.doTypeConversion(flagDefaultResult.type, flagDefaultResult.value);
6430
+ let defaultValue = doTypeConversion(flagDefaultResult.type, flagDefaultResult.value);
6381
6431
  if (saveToUser) {
6382
6432
  flagDefaultResult.value = defaultValue;
6383
6433
  saveToUser.addFlag(flag.name, flagDefaultResult);
@@ -6385,16 +6435,6 @@ var Sigma = class {
6385
6435
  return defaultValue;
6386
6436
  }
6387
6437
  }
6388
- doTypeConversion(type, value) {
6389
- if (type === "string")
6390
- return value;
6391
- if (type === "bool") {
6392
- return value === "true" ? true : false;
6393
- }
6394
- if (type === "integer")
6395
- return parseFloat(value);
6396
- return value;
6397
- }
6398
6438
  async getCacheDataFile() {
6399
6439
  const data = await this.cache.get(sigmaDataFile);
6400
6440
  return data;