expf-sigma-node.js 3.2.12 → 3.3.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 +375 -60
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "3.2.12",
3
+ "version": "3.3.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
@@ -135,9 +135,6 @@ var SigmaUserData = class {
135
135
  version: os.version || void 0
136
136
  };
137
137
  }
138
- setUserId(userId) {
139
- this.user.userId = userId;
140
- }
141
138
  setIp(ip) {
142
139
  this.user.ip = ip;
143
140
  }
@@ -151,11 +148,133 @@ var SigmaUserData = class {
151
148
  });
152
149
  }
153
150
  }
151
+ setGeo(data) {
152
+ if (!data || typeof data !== "object" || Array.isArray(data))
153
+ return;
154
+ this.user.geo = data;
155
+ }
154
156
  clearFlags() {
155
157
  this.user.featureFlags = [];
156
158
  }
157
- setGeo(data) {
158
- this.user.geo = data;
159
+ removeInjectedProperties() {
160
+ this.user.appVersion = null;
161
+ this.user.userId = null;
162
+ this.user.profileId = null;
163
+ this.user.deviceId = null;
164
+ this.user.email = null;
165
+ this.user.custom = { name: null, value: null };
166
+ }
167
+ setUserId(userId) {
168
+ if (!userId)
169
+ this.user.userId = null;
170
+ this.user.userId = userId;
171
+ }
172
+ setDeviceId(deviceId) {
173
+ if (!deviceId)
174
+ this.user.deviceId = null;
175
+ this.user.deviceId = deviceId;
176
+ }
177
+ setProfileId(profileId) {
178
+ if (!profileId)
179
+ this.user.profileId = null;
180
+ this.user.profileId = profileId;
181
+ }
182
+ setEmail(email) {
183
+ if (!email)
184
+ this.user.email = null;
185
+ this.user.email = email;
186
+ }
187
+ setAppVersion(appVersion) {
188
+ if (!appVersion)
189
+ this.user.appVersion = null;
190
+ this.user.appVersion = appVersion;
191
+ }
192
+ setBrowserName(browserName) {
193
+ if (!browserName)
194
+ return;
195
+ this.user.browser.name = browserName;
196
+ }
197
+ setBrowserVersion(browserVersion) {
198
+ if (!browserVersion)
199
+ return;
200
+ this.user.browser.version = browserVersion;
201
+ }
202
+ setOsName(osName) {
203
+ if (!osName)
204
+ return;
205
+ this.user.os.name = osName;
206
+ }
207
+ setOsVersion(osVersion) {
208
+ if (!osVersion)
209
+ return;
210
+ this.user.os.version = osVersion;
211
+ }
212
+ setGeoCountry(country) {
213
+ this.user.geo.country = country;
214
+ }
215
+ setGeoCode(code) {
216
+ this.user.geo.code = code;
217
+ }
218
+ setGeoState(state) {
219
+ this.user.geo.state = state;
220
+ }
221
+ setGeoCity(city) {
222
+ this.user.geo.city = city;
223
+ }
224
+ setGeoIp(ip) {
225
+ this.user.geo.ip = ip;
226
+ }
227
+ setCustomProperty(key, value) {
228
+ if (typeof key !== "string")
229
+ return;
230
+ this.user.custom = { ...this.user.custom, [key]: value };
231
+ }
232
+ removeUserId() {
233
+ this.user.userId = null;
234
+ }
235
+ removeDeviceId() {
236
+ this.user.deviceId = null;
237
+ }
238
+ removeProfileId() {
239
+ this.user.profileId = null;
240
+ }
241
+ removeEmail() {
242
+ this.user.email = null;
243
+ }
244
+ removeAppVersion() {
245
+ this.user.appVersion = null;
246
+ }
247
+ removeBrowserName() {
248
+ this.user.browser.name = null;
249
+ }
250
+ removeBrowserVersion() {
251
+ this.user.browser.version = null;
252
+ }
253
+ removeOsName() {
254
+ this.user.os.name = null;
255
+ }
256
+ removeOsVersion() {
257
+ this.user.os.version = null;
258
+ }
259
+ removeGeoCountry() {
260
+ this.user.geo.country = null;
261
+ }
262
+ removeGeoCode() {
263
+ this.user.geo.code = null;
264
+ }
265
+ removeGeoState() {
266
+ this.user.geo.state = null;
267
+ }
268
+ removeGeoCity() {
269
+ this.user.geo.city = null;
270
+ }
271
+ removeGeoIp() {
272
+ this.user.geo.ip = null;
273
+ }
274
+ removeCustomProperty(key) {
275
+ if (!key || typeof key !== "string")
276
+ return;
277
+ delete this.user.custom[key];
159
278
  }
160
279
  };
161
280
 
@@ -844,6 +963,10 @@ var errorTypesInConfig = {
844
963
  invalidType: 'Sigma warning. Invalid type "%type%" in config.'
845
964
  };
846
965
  var sdkName = "node.js";
966
+ var statusSuccess = "success";
967
+ var statusFailed = "failed";
968
+ var status200 = 200;
969
+ var status400 = 400;
847
970
 
848
971
  // src/modules/sigmaCache.js
849
972
  var sigmaCache = new import_node_cache.default({
@@ -1036,11 +1159,6 @@ function conditionOperation(userValue, conditionValues, operation, date, time) {
1036
1159
  }
1037
1160
  }
1038
1161
 
1039
- // src/helpers/delay.js
1040
- function delay(ms) {
1041
- return new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
1042
- }
1043
-
1044
1162
  // src/helpers/digestHash.js
1045
1163
  function digestHash(string) {
1046
1164
  let hash = 0, i, chr;
@@ -1141,11 +1259,42 @@ function generateFilteredList(array, name) {
1141
1259
  }
1142
1260
 
1143
1261
  // package.json
1144
- var version = "3.2.12";
1262
+ var version = "3.3.1";
1145
1263
 
1146
1264
  // src/helpers/sdkVersion.js
1147
1265
  var sdkVersion = version;
1148
1266
 
1267
+ // src/helpers/isLocalOrIPv6.js
1268
+ var blackListLocalIps = [
1269
+ ["192.168.0.0", "192.168.255.255"],
1270
+ ["172.0.0.0", "172.255.255.255"],
1271
+ ["10.0.0.0", "10.255.255.255"],
1272
+ ["127.0.0.0", "127.255.255.255"],
1273
+ ["100.64.0.0", "100.127.255.255"]
1274
+ ];
1275
+ function isLocalOrIPv6(ip) {
1276
+ let isLocalIpOrIPv6 = false;
1277
+ if (typeof ip !== "string")
1278
+ return isLocalIpOrIPv6 = true;
1279
+ const dotCount = ip.split(".").length - 1;
1280
+ if (dotCount !== 3)
1281
+ return isLocalIpOrIPv6 = true;
1282
+ function ipToNumber(ip2) {
1283
+ const parts = ip2.split(".");
1284
+ return (parseInt(parts[0]) << 24) + (parseInt(parts[1]) << 16) + (parseInt(parts[2]) << 8) + parseInt(parts[3]);
1285
+ }
1286
+ const ipNumber = ipToNumber(ip);
1287
+ for (const i in blackListLocalIps) {
1288
+ const startRange = ipToNumber(blackListLocalIps[i][0]);
1289
+ const endRange = ipToNumber(blackListLocalIps[i][1]);
1290
+ if (ipNumber >= startRange && ipNumber <= endRange) {
1291
+ isLocalIpOrIPv6 = true;
1292
+ break;
1293
+ }
1294
+ }
1295
+ return isLocalIpOrIPv6;
1296
+ }
1297
+
1149
1298
  // src/sigma.js
1150
1299
  if (typeof import_node_fetch.default.default !== "undefined")
1151
1300
  import_node_fetch.default.default;
@@ -1175,11 +1324,11 @@ var Sigma = class {
1175
1324
  ip: this.userData.ip || null,
1176
1325
  email: this.userData.email || null,
1177
1326
  appVersion: this.userData.appVersion || null,
1178
- custom: this.userData.custom || null,
1327
+ custom: this.userData.custom || {},
1179
1328
  deviceCategory: this.userData.deviceCategory || null,
1180
1329
  browser: this.userData.browser || this.sigmaUserData.getUserBrowser(this.userData.ua),
1181
1330
  os: this.userData.os || this.sigmaUserData.getUserOs(this.userData.ua),
1182
- geo: this.userData.geo || null,
1331
+ geo: this.userData.geo || {},
1183
1332
  domain: this.userData.domain || null,
1184
1333
  url: this.userData.url || null,
1185
1334
  query: this.userData.query || null,
@@ -1213,63 +1362,72 @@ var Sigma = class {
1213
1362
  this.cache.set(sigmaPrivateId, generateRandomId());
1214
1363
  }
1215
1364
  }
1216
- retryFetch(url, fetchOptions, retries = 3, timeout) {
1217
- return new Promise((resolve, reject) => {
1218
- if (timeout)
1219
- setTimeout(() => reject("error: timeout"), timeout);
1220
- const wrapper = (n) => {
1221
- (0, import_node_fetch.default)(url, fetchOptions).then((res) => resolve(res)).catch((err) => {
1222
- if (n > 0) {
1223
- delay(0).then(() => {
1224
- wrapper(--n);
1225
- });
1226
- } else {
1227
- reject(err);
1228
- }
1229
- });
1230
- };
1231
- wrapper(retries);
1232
- });
1233
- }
1234
- async getDataFile(url, method, body) {
1235
- const options = {
1236
- method: method || "GET",
1237
- body,
1238
- credentials: "same-origin",
1239
- headers: {
1240
- "ip": this.userData.ip,
1241
- "X-Real-IP": this.userData.ip,
1242
- token: this.token,
1243
- "user-id": this.cache.get(sigmaPrivateId),
1244
- "sdk-name": sdkName,
1245
- "sdk-version": sdkVersion
1246
- }
1365
+ async getDataFile(url) {
1366
+ let data;
1367
+ let sigmaData = {
1368
+ status: "",
1369
+ statusCode: "",
1370
+ data: null
1247
1371
  };
1248
- const data = await this.retryFetch(
1249
- url,
1250
- options,
1251
- this.retries
1252
- );
1253
- return data.json();
1372
+ const controller = new AbortController();
1373
+ try {
1374
+ data = await (0, import_node_fetch.default)(url, {
1375
+ headers: {
1376
+ ip: this.userData.ip || "",
1377
+ "X-Real-IP": this.userData.ip || "",
1378
+ token: this.token,
1379
+ "user-id": generateRandomId(),
1380
+ "sdk-name": sdkName,
1381
+ "sdk-version": sdkVersion
1382
+ },
1383
+ signal: controller.signal
1384
+ });
1385
+ sigmaData.status = data.status === status200 ? statusSuccess : statusFailed;
1386
+ sigmaData.statusCode = data.status;
1387
+ sigmaData.data = data.status === status200 ? await data.json() : null;
1388
+ } catch {
1389
+ controller.abort();
1390
+ sigmaData.status = statusFailed;
1391
+ sigmaData.statusCode = status400;
1392
+ sigmaData.data = null;
1393
+ }
1394
+ return sigmaData;
1254
1395
  }
1255
1396
  async getConfig() {
1256
1397
  let data;
1257
1398
  const localStorageHash = this.cache.get(sigmaHash);
1258
1399
  let hash;
1259
1400
  data = await this.getDataFile(`${this.api}/config.json`);
1260
- if (this.userData.platform) {
1261
- data = filterByPlatform(data, this.userData.platform);
1401
+ if (data.status === statusSuccess) {
1402
+ if (this.userData.platform) {
1403
+ data.data = filterByPlatform(data.data, this.userData.platform);
1404
+ }
1405
+ hash = await SHA256.hex(JSON.stringify(data.data));
1406
+ if (!localStorageHash || localStorageHash !== hash) {
1407
+ this.cache.set(sigmaHash, hash);
1408
+ this.cache.set(sigmaDataFile, JSON.stringify(data.data));
1409
+ }
1410
+ return;
1262
1411
  }
1263
- hash = await SHA256.hex(JSON.stringify(data));
1264
- if (!localStorageHash || localStorageHash !== hash) {
1265
- this.cache.set(sigmaHash, hash);
1266
- this.cache.set(sigmaDataFile, JSON.stringify(data));
1412
+ if (data.status === statusFailed) {
1413
+ if (!this.cache.get(sigmaDataFile)) {
1414
+ throw new Error(errorMessages.APIRequestErrorConfig.detail);
1415
+ }
1267
1416
  }
1417
+ return;
1268
1418
  }
1269
1419
  async getUserGeoData() {
1420
+ if (this.userData.ip && isLocalOrIPv6(this.userData.ip)) {
1421
+ this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1422
+ return;
1423
+ }
1270
1424
  if (this.userData.ip) {
1271
1425
  const userGeoData = await this.getDataFile(`${this.api}/geo`);
1272
- this.geoCache.set(sigmaGeoData, JSON.stringify(userGeoData));
1426
+ if (userGeoData.status === statusSuccess) {
1427
+ this.geoCache.set(sigmaGeoData, JSON.stringify(userGeoData.data));
1428
+ } else {
1429
+ this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1430
+ }
1273
1431
  } else if (this.userData.geo) {
1274
1432
  this.geoCache.set(sigmaGeoData, JSON.stringify(this.userData.geo));
1275
1433
  } else {
@@ -1301,9 +1459,6 @@ var Sigma = class {
1301
1459
  if (!this.geoCache.get(sigmaGeoData)) {
1302
1460
  this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1303
1461
  }
1304
- errorMessages.APIRequestErrorGeoData.token = this.token;
1305
- errorMessages.APIRequestErrorGeoData.userData = JSON.stringify(this.userData);
1306
- console.error(errorMessages.APIRequestErrorGeoData);
1307
1462
  }
1308
1463
  this.recordLastUpdateInCache();
1309
1464
  }
@@ -1744,6 +1899,166 @@ var Sigma = class {
1744
1899
  }
1745
1900
  return isSuccessRule;
1746
1901
  }
1902
+ clearCacheByKeys(keys) {
1903
+ for (const key in keys) {
1904
+ this.cache.remove(keys[key]);
1905
+ }
1906
+ }
1907
+ clearUserProperties() {
1908
+ this.clearCacheByKeys([
1909
+ sigmaUserId,
1910
+ sigmaDeviceId,
1911
+ sigmaProfileId,
1912
+ sigmaExpiration,
1913
+ sigmaExperiments
1914
+ ]);
1915
+ this.sigmaUserData.removeInjectedProperties();
1916
+ }
1917
+ overwriteGeoCache(key, value) {
1918
+ const geoCache = this.geoCache.parse(sigmaGeoData);
1919
+ geoCache[key] = value;
1920
+ this.geoCache.set(sigmaGeoData, JSON.stringify(geoCache));
1921
+ }
1922
+ editUserProperties() {
1923
+ return {
1924
+ setUserId: (userId) => {
1925
+ this.sigmaUserData.setUserId(userId);
1926
+ this.cache.set(sigmaUserId, userId);
1927
+ return this.editUserProperties();
1928
+ },
1929
+ setDeviceId: (deviceId) => {
1930
+ this.sigmaUserData.setDeviceId(deviceId);
1931
+ this.cache.set(sigmaDeviceId, deviceId);
1932
+ return this.editUserProperties();
1933
+ },
1934
+ setProfileId: (profileId) => {
1935
+ this.sigmaUserData.setProfileId(profileId);
1936
+ this.cache.set(sigmaProfileId, profileId);
1937
+ return this.editUserProperties();
1938
+ },
1939
+ setEmail: (email) => {
1940
+ this.sigmaUserData.setEmail(email);
1941
+ return this.editUserProperties();
1942
+ },
1943
+ setAppVersion: (appVersion) => {
1944
+ this.sigmaUserData.setAppVersion(appVersion);
1945
+ return this.editUserProperties();
1946
+ },
1947
+ setBrowserName: (browserName) => {
1948
+ this.sigmaUserData.setBrowserName(browserName);
1949
+ return this.editUserProperties();
1950
+ },
1951
+ setBrowserVersion: (browserVersion) => {
1952
+ this.sigmaUserData.setBrowserVersion(browserVersion);
1953
+ return this.editUserProperties();
1954
+ },
1955
+ setOsName: (osName) => {
1956
+ this.sigmaUserData.setOsName(osName);
1957
+ return this.editUserProperties();
1958
+ },
1959
+ setOsVersion: (osVersion) => {
1960
+ this.sigmaUserData.setOsVersion(osVersion);
1961
+ return this.editUserProperties();
1962
+ },
1963
+ setGeoCountry: (country) => {
1964
+ this.sigmaUserData.setGeoCountry(country);
1965
+ this.overwriteGeoCache("country", country);
1966
+ return this.editUserProperties();
1967
+ },
1968
+ setGeoCode: (code) => {
1969
+ this.sigmaUserData.setGeoCode(code);
1970
+ this.overwriteGeoCache("code", code);
1971
+ return this.editUserProperties();
1972
+ },
1973
+ setGeoState: (state) => {
1974
+ this.sigmaUserData.setGeoState(state);
1975
+ this.overwriteGeoCache("state", state);
1976
+ return this.editUserProperties();
1977
+ },
1978
+ setGeoCity: (city) => {
1979
+ this.sigmaUserData.setGeoCity(city);
1980
+ this.overwriteGeoCache("city", city);
1981
+ return this.editUserProperties();
1982
+ },
1983
+ setGeoIp: (ip) => {
1984
+ this.sigmaUserData.setGeoIp(ip);
1985
+ this.overwriteGeoCache("ip", ip);
1986
+ return this.editUserProperties();
1987
+ },
1988
+ setCustomProperty: (key, value) => {
1989
+ this.sigmaUserData.setCustomProperty(key, value);
1990
+ return this.editUserProperties();
1991
+ },
1992
+ removeUserId: () => {
1993
+ this.sigmaUserData.removeUserId();
1994
+ this.cache.remove(sigmaUserId);
1995
+ return this.editUserProperties();
1996
+ },
1997
+ removeDeviceId: () => {
1998
+ this.sigmaUserData.removeDeviceId();
1999
+ this.cache.remove(sigmaDeviceId);
2000
+ return this.editUserProperties();
2001
+ },
2002
+ removeProfileId: () => {
2003
+ this.sigmaUserData.removeProfileId();
2004
+ this.cache.remove(sigmaProfileId);
2005
+ return this.editUserProperties();
2006
+ },
2007
+ removeAppVersion: () => {
2008
+ this.sigmaUserData.removeAppVersion();
2009
+ return this.editUserProperties();
2010
+ },
2011
+ removeBrowserName: () => {
2012
+ this.sigmaUserData.removeBrowserName();
2013
+ return this.editUserProperties();
2014
+ },
2015
+ removeBrowserVersion: () => {
2016
+ this.sigmaUserData.removeBrowserVersion();
2017
+ return this.editUserProperties();
2018
+ },
2019
+ removeEmail: () => {
2020
+ this.sigmaUserData.removeEmail();
2021
+ return this.editUserProperties();
2022
+ },
2023
+ removeOsName: () => {
2024
+ this.sigmaUserData.removeOsName();
2025
+ return this.editUserProperties();
2026
+ },
2027
+ removeOsVersion: () => {
2028
+ this.sigmaUserData.removeOsVersion();
2029
+ return this.editUserProperties();
2030
+ },
2031
+ removeGeoCountry: () => {
2032
+ this.sigmaUserData.removeGeoCountry();
2033
+ this.overwriteGeoCache("country", null);
2034
+ return this.editUserProperties();
2035
+ },
2036
+ removeGeoCode: () => {
2037
+ this.sigmaUserData.removeGeoCode();
2038
+ this.overwriteGeoCache("code", null);
2039
+ return this.editUserProperties();
2040
+ },
2041
+ removeGeoState: () => {
2042
+ this.sigmaUserData.removeGeoState();
2043
+ this.overwriteGeoCache("state", null);
2044
+ return this.editUserProperties();
2045
+ },
2046
+ removeGeoCity: () => {
2047
+ this.sigmaUserData.removeGeoCity();
2048
+ this.overwriteGeoCache("city", null);
2049
+ return this.editUserProperties();
2050
+ },
2051
+ removeGeoIp: () => {
2052
+ this.sigmaUserData.removeGeoIp();
2053
+ this.overwriteGeoCache("ip", null);
2054
+ return this.editUserProperties();
2055
+ },
2056
+ removeCustomProperty: (key) => {
2057
+ this.sigmaUserData.removeCustomProperty(key);
2058
+ return this.editUserProperties();
2059
+ }
2060
+ };
2061
+ }
1747
2062
  };
1748
2063
  var sigma_default = Sigma;
1749
2064
  // Annotate the CommonJS export names for ESM import in node: