expf-sigma-node.js 0.0.5 → 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.
- package/package.json +1 -1
- package/public/sigma.js +77 -52
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expf-sigma-node.js",
|
|
3
|
-
"version": "0.0.
|
|
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,32 @@ 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,
|
|
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 =
|
|
5796
|
-
this.retries = retries;
|
|
5797
|
-
this.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);
|
|
5810
|
+
this.userData.userId = this.validateUserDataUserId(this.userData.userId);
|
|
5798
5811
|
this.sigmaUserData.init({
|
|
5799
5812
|
userId: this.userData.userId || null,
|
|
5800
5813
|
ip: this.userData.ip || null,
|
|
@@ -5802,13 +5815,24 @@ var Sigma = class {
|
|
|
5802
5815
|
appVersion: this.userData.appVersion || null,
|
|
5803
5816
|
custom: this.userData.custom || null
|
|
5804
5817
|
});
|
|
5805
|
-
|
|
5806
|
-
|
|
5807
|
-
|
|
5808
|
-
|
|
5809
|
-
|
|
5810
|
-
|
|
5818
|
+
this.cache.set(sigmaUserId, this.userData.userId);
|
|
5819
|
+
this.setUserCookie();
|
|
5820
|
+
}
|
|
5821
|
+
validateUserDataUserId(userId) {
|
|
5822
|
+
let hasMakeId = false;
|
|
5823
|
+
if (typeof userId === "undefined") {
|
|
5824
|
+
hasMakeId = true;
|
|
5825
|
+
}
|
|
5826
|
+
userId = String(userId).trim();
|
|
5827
|
+
if (userId.length === 0) {
|
|
5828
|
+
hasMakeId = true;
|
|
5811
5829
|
}
|
|
5830
|
+
if (hasMakeId && this.cache.get(sigmaUserId)) {
|
|
5831
|
+
userId = this.cache.get(sigmaUserId);
|
|
5832
|
+
} else if (hasMakeId) {
|
|
5833
|
+
userId = this.makeIdCookie();
|
|
5834
|
+
}
|
|
5835
|
+
return userId;
|
|
5812
5836
|
}
|
|
5813
5837
|
delay(ms) {
|
|
5814
5838
|
new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
|
|
@@ -5863,10 +5887,10 @@ var Sigma = class {
|
|
|
5863
5887
|
}
|
|
5864
5888
|
async saveToCache() {
|
|
5865
5889
|
const { api: api2 } = this;
|
|
5866
|
-
const localStorageHash = cache.get("sigmaHash");
|
|
5890
|
+
const localStorageHash = this.cache.get("sigmaHash");
|
|
5867
5891
|
try {
|
|
5868
5892
|
const timeRecord = Math.floor(Date.now() / 1e3);
|
|
5869
|
-
cache.set("sigmaDataFileLastUpdate", timeRecord);
|
|
5893
|
+
this.cache.set("sigmaDataFileLastUpdate", timeRecord);
|
|
5870
5894
|
} catch (error) {
|
|
5871
5895
|
throw new Error(`save last update to cache: ${error},
|
|
5872
5896
|
token: ${this.token}
|
|
@@ -5874,19 +5898,19 @@ var Sigma = class {
|
|
|
5874
5898
|
}
|
|
5875
5899
|
try {
|
|
5876
5900
|
const userGeoData = await this.getUserGeoData();
|
|
5877
|
-
if (!cache.get("sigmaGeoData") || cache.get("sigmaGeoData", { location: "none" }) && userGeoData) {
|
|
5878
|
-
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));
|
|
5879
5903
|
this.sigmaUserData.clearFlags();
|
|
5880
5904
|
}
|
|
5881
5905
|
} catch (error) {
|
|
5882
|
-
cache.set("sigmaGeoData", JSON.stringify({ location: "none" }));
|
|
5906
|
+
this.cache.set("sigmaGeoData", JSON.stringify({ location: "none" }));
|
|
5883
5907
|
console.error(error);
|
|
5884
5908
|
}
|
|
5885
5909
|
let data;
|
|
5886
5910
|
try {
|
|
5887
5911
|
data = await this.getDataFile(`${api2}`);
|
|
5888
5912
|
} catch (error) {
|
|
5889
|
-
if (!cache.get(sigmaDataFile)) {
|
|
5913
|
+
if (!this.cache.get(sigmaDataFile)) {
|
|
5890
5914
|
throw new Error(`
|
|
5891
5915
|
Get data: ${error},
|
|
5892
5916
|
token: ${this.token},
|
|
@@ -5904,8 +5928,8 @@ var Sigma = class {
|
|
|
5904
5928
|
}
|
|
5905
5929
|
hash = await SHA256.hex(JSON.stringify(data));
|
|
5906
5930
|
if (!localStorageHash || localStorageHash !== hash) {
|
|
5907
|
-
cache.set("sigmaHash", hash);
|
|
5908
|
-
cache.set(sigmaDataFile, JSON.stringify(data));
|
|
5931
|
+
this.cache.set("sigmaHash", hash);
|
|
5932
|
+
this.cache.set(sigmaDataFile, JSON.stringify(data));
|
|
5909
5933
|
this.sigmaUserData.clearFlags();
|
|
5910
5934
|
}
|
|
5911
5935
|
} catch (error) {
|
|
@@ -5913,17 +5937,17 @@ var Sigma = class {
|
|
|
5913
5937
|
}
|
|
5914
5938
|
}
|
|
5915
5939
|
setUserCookie() {
|
|
5916
|
-
let uin = cache.get(sigmaUserId);
|
|
5940
|
+
let uin = this.cache.get(sigmaUserId);
|
|
5917
5941
|
if (!uin) {
|
|
5918
5942
|
uin = js_cookie_default.get(sigmaUserId);
|
|
5919
|
-
cache.set(sigmaUserId, uin);
|
|
5943
|
+
this.cache.set(sigmaUserId, uin);
|
|
5920
5944
|
} else {
|
|
5921
5945
|
js_cookie_default.set(sigmaUserId, uin);
|
|
5922
5946
|
}
|
|
5923
5947
|
if (!uin) {
|
|
5924
5948
|
uin = this.makeIdCookie();
|
|
5925
5949
|
js_cookie_default.set(sigmaUserId, uin);
|
|
5926
|
-
cache.set(sigmaUserId, uin);
|
|
5950
|
+
this.cache.set(sigmaUserId, uin);
|
|
5927
5951
|
}
|
|
5928
5952
|
return;
|
|
5929
5953
|
}
|
|
@@ -5936,7 +5960,7 @@ var Sigma = class {
|
|
|
5936
5960
|
}
|
|
5937
5961
|
hasExpireCache() {
|
|
5938
5962
|
const lastUpdate = parseInt(
|
|
5939
|
-
cache.get("sigmaDataFileLastUpdate")
|
|
5963
|
+
this.cache.get("sigmaDataFileLastUpdate")
|
|
5940
5964
|
);
|
|
5941
5965
|
const currentTime = Math.floor(Date.now() / 1e3);
|
|
5942
5966
|
if (currentTime > lastUpdate + this.cacheTTL) {
|
|
@@ -5945,14 +5969,14 @@ var Sigma = class {
|
|
|
5945
5969
|
return false;
|
|
5946
5970
|
}
|
|
5947
5971
|
updateCache() {
|
|
5948
|
-
if (!cache.get("sigmaDataFile") || this.hasExpireCache()) {
|
|
5972
|
+
if (!this.cache.get("sigmaDataFile") || this.hasExpireCache()) {
|
|
5949
5973
|
return this.saveToCache();
|
|
5950
5974
|
}
|
|
5951
5975
|
return true;
|
|
5952
5976
|
}
|
|
5953
5977
|
async getFlags() {
|
|
5954
5978
|
await this.updateCache();
|
|
5955
|
-
const flags = cache.parse("sigmaDataFile");
|
|
5979
|
+
const flags = this.cache.parse("sigmaDataFile");
|
|
5956
5980
|
if (!flags.feature_flags)
|
|
5957
5981
|
return;
|
|
5958
5982
|
return flags.feature_flags;
|
|
@@ -5962,10 +5986,10 @@ var Sigma = class {
|
|
|
5962
5986
|
return false;
|
|
5963
5987
|
}
|
|
5964
5988
|
await this.updateCache();
|
|
5965
|
-
const cacheKey = cache.parse("sigmaDataFile");
|
|
5989
|
+
const cacheKey = this.cache.parse("sigmaDataFile");
|
|
5966
5990
|
if (!cacheKey.feature_flags)
|
|
5967
5991
|
return;
|
|
5968
|
-
const geoData = await cache.parse("sigmaGeoData");
|
|
5992
|
+
const geoData = await this.cache.parse("sigmaGeoData");
|
|
5969
5993
|
this.sigmaUserData.setGeo(geoData);
|
|
5970
5994
|
let flag = null;
|
|
5971
5995
|
for (let i = 0; i < cacheKey.feature_flags.length; i++) {
|
|
@@ -6067,29 +6091,32 @@ var Sigma = class {
|
|
|
6067
6091
|
case "on date":
|
|
6068
6092
|
return Math.floor(
|
|
6069
6093
|
new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60 * 60 * 24)
|
|
6070
|
-
) == sigmaUserData.user.date;
|
|
6094
|
+
) == this.sigmaUserData.user.date;
|
|
6071
6095
|
case "after time":
|
|
6072
6096
|
return Math.floor(
|
|
6073
6097
|
new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60)
|
|
6074
|
-
) < sigmaUserData.user.time;
|
|
6098
|
+
) < this.sigmaUserData.user.time;
|
|
6075
6099
|
case "before time":
|
|
6076
6100
|
return Math.floor(
|
|
6077
6101
|
new Date(conditionValues.trim()).getTime() / (1e3 * 60 * 60)
|
|
6078
|
-
) > sigmaUserData.user.time;
|
|
6102
|
+
) > this.sigmaUserData.user.time;
|
|
6079
6103
|
default:
|
|
6080
6104
|
return false;
|
|
6081
6105
|
}
|
|
6082
6106
|
}
|
|
6083
6107
|
getUserParamValue(paramKey) {
|
|
6108
|
+
if (!paramKey)
|
|
6109
|
+
return false;
|
|
6084
6110
|
const user = this.sigmaUserData.user;
|
|
6085
6111
|
if (paramKey.includes(".")) {
|
|
6086
6112
|
const paramsArray = paramKey.split(".");
|
|
6087
6113
|
const parent = paramsArray[0];
|
|
6088
6114
|
const child = paramsArray[1];
|
|
6115
|
+
if (!child)
|
|
6116
|
+
return false;
|
|
6089
6117
|
const isSetParentInUserData = Object.keys(user).find((i) => i === parent);
|
|
6090
|
-
if (!user[isSetParentInUserData])
|
|
6118
|
+
if (!user[isSetParentInUserData])
|
|
6091
6119
|
return false;
|
|
6092
|
-
}
|
|
6093
6120
|
const userObject = Object.entries(user[isSetParentInUserData]);
|
|
6094
6121
|
for (let [userParamKey, userParamValue] of userObject) {
|
|
6095
6122
|
if (userParamKey == child) {
|
|
@@ -6117,17 +6144,15 @@ var Sigma = class {
|
|
|
6117
6144
|
return hash;
|
|
6118
6145
|
}
|
|
6119
6146
|
calcUserInGroup(salt, exposureRate, controlBucketPerc, forcedExp = false, layer = null, layerBounds = []) {
|
|
6120
|
-
const
|
|
6121
|
-
const zeta = 8540717056;
|
|
6122
|
-
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);
|
|
6123
6148
|
const md5R = MD5.hex(String(userId) + salt);
|
|
6124
|
-
const R = this.digestHash(md5R) * zeta
|
|
6149
|
+
const R = this.digestHash(md5R) * zeta / max_decimal_64;
|
|
6125
6150
|
let md5L = null;
|
|
6126
6151
|
if (!layer || !layerBounds.length) {
|
|
6127
6152
|
return this.getUserGroup(R, exposureRate, controlBucketPerc, forcedExp);
|
|
6128
6153
|
} else {
|
|
6129
6154
|
md5L = MD5.hex(String(userId) + layer);
|
|
6130
|
-
const L = this.digestHash(md5L) * zeta
|
|
6155
|
+
const L = this.digestHash(md5L) * zeta / max_decimal_64;
|
|
6131
6156
|
for (let i in layerBounds) {
|
|
6132
6157
|
const result = Math.abs(L / 10);
|
|
6133
6158
|
if (result <= layerBounds[i].lower_bound || layerBounds[i].upper_bound <= result) {
|
|
@@ -6159,7 +6184,7 @@ var Sigma = class {
|
|
|
6159
6184
|
return false;
|
|
6160
6185
|
}
|
|
6161
6186
|
await this.updateCache();
|
|
6162
|
-
const sigmaDataLs = cache.parse(sigmaDataFile);
|
|
6187
|
+
const sigmaDataLs = this.cache.parse(sigmaDataFile);
|
|
6163
6188
|
let experiment = null;
|
|
6164
6189
|
let layer = null;
|
|
6165
6190
|
let bounds = [];
|
|
@@ -6176,7 +6201,7 @@ var Sigma = class {
|
|
|
6176
6201
|
bounds = sigmaDataLs.experiments[i]["layer_bounds"];
|
|
6177
6202
|
}
|
|
6178
6203
|
if (sigmaDataLs.experiments[i]["name"] === experimentName && !experiment) {
|
|
6179
|
-
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);
|
|
6180
6205
|
experiment = sigmaDataLs.experiments[i];
|
|
6181
6206
|
break;
|
|
6182
6207
|
}
|
|
@@ -6191,13 +6216,13 @@ var Sigma = class {
|
|
|
6191
6216
|
layer,
|
|
6192
6217
|
bounds
|
|
6193
6218
|
);
|
|
6194
|
-
cache.set(localStorageGroupName, userInGroupExperiment);
|
|
6219
|
+
this.cache.set(localStorageGroupName, userInGroupExperiment);
|
|
6195
6220
|
}
|
|
6196
6221
|
const getParamValue = (paramName) => {
|
|
6197
6222
|
if (!paramName || !experiment) {
|
|
6198
6223
|
return false;
|
|
6199
6224
|
}
|
|
6200
|
-
const groupName = cache.get(localStorageGroupName);
|
|
6225
|
+
const groupName = this.cache.get(localStorageGroupName);
|
|
6201
6226
|
let params = null;
|
|
6202
6227
|
for (let i = 0; i < experiment.params.length; i++) {
|
|
6203
6228
|
if (experiment.params[i]["name"] === paramName) {
|
|
@@ -6223,7 +6248,7 @@ var Sigma = class {
|
|
|
6223
6248
|
if (!flagName || !experiment) {
|
|
6224
6249
|
return false;
|
|
6225
6250
|
}
|
|
6226
|
-
const groupName = cache.get(localStorageGroupName);
|
|
6251
|
+
const groupName = this.cache.get(localStorageGroupName);
|
|
6227
6252
|
let flagExperiment = null;
|
|
6228
6253
|
for (let i = 0; i < experiment.feature_flags.length; i++) {
|
|
6229
6254
|
if (experiment.feature_flags[i]["name"] === flagName) {
|
|
@@ -6251,7 +6276,7 @@ var Sigma = class {
|
|
|
6251
6276
|
}
|
|
6252
6277
|
async getAllUserExperiments() {
|
|
6253
6278
|
await this.updateCache();
|
|
6254
|
-
const sigmaDataLs = cache.parse(sigmaDataFile);
|
|
6279
|
+
const sigmaDataLs = this.cache.parse(sigmaDataFile);
|
|
6255
6280
|
if (!sigmaDataLs.experiments.length)
|
|
6256
6281
|
return false;
|
|
6257
6282
|
let result = "";
|
|
@@ -6267,7 +6292,7 @@ var Sigma = class {
|
|
|
6267
6292
|
experiment.salt,
|
|
6268
6293
|
experiment.allocation,
|
|
6269
6294
|
experiment.groups,
|
|
6270
|
-
this.checkForcedList(experiment.forced_user_list, sigmaUserData.user.userId),
|
|
6295
|
+
this.checkForcedList(experiment.forced_user_list, this.sigmaUserData.user.userId),
|
|
6271
6296
|
layer,
|
|
6272
6297
|
bounds
|
|
6273
6298
|
);
|
|
@@ -6277,7 +6302,7 @@ var Sigma = class {
|
|
|
6277
6302
|
}
|
|
6278
6303
|
}
|
|
6279
6304
|
if (result.length > 0) {
|
|
6280
|
-
cache.set(
|
|
6305
|
+
this.cache.set(sigmaExperiments, result.slice(0, -1));
|
|
6281
6306
|
return result.slice(0, -1);
|
|
6282
6307
|
}
|
|
6283
6308
|
return false;
|
|
@@ -6371,19 +6396,19 @@ var Sigma = class {
|
|
|
6371
6396
|
return value;
|
|
6372
6397
|
}
|
|
6373
6398
|
async getCacheDataFile() {
|
|
6374
|
-
const data = await cache.get(sigmaDataFile);
|
|
6399
|
+
const data = await this.cache.get(sigmaDataFile);
|
|
6375
6400
|
return data;
|
|
6376
6401
|
}
|
|
6377
6402
|
async getCacheHash() {
|
|
6378
|
-
const data = await cache.get("sigmaHash");
|
|
6403
|
+
const data = await this.cache.get("sigmaHash");
|
|
6379
6404
|
return data;
|
|
6380
6405
|
}
|
|
6381
6406
|
async getCacheGeoData() {
|
|
6382
|
-
const data = await cache.get("sigmaGeoData");
|
|
6407
|
+
const data = await this.cache.get("sigmaGeoData");
|
|
6383
6408
|
return data;
|
|
6384
6409
|
}
|
|
6385
6410
|
async getCacheUserId() {
|
|
6386
|
-
const data = await cache.get(sigmaUserId);
|
|
6411
|
+
const data = await this.cache.get(sigmaUserId);
|
|
6387
6412
|
return data;
|
|
6388
6413
|
}
|
|
6389
6414
|
};
|