expf-sigma-node.js 3.3.0 → 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 +91 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "3.3.0",
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
@@ -963,6 +963,10 @@ var errorTypesInConfig = {
963
963
  invalidType: 'Sigma warning. Invalid type "%type%" in config.'
964
964
  };
965
965
  var sdkName = "node.js";
966
+ var statusSuccess = "success";
967
+ var statusFailed = "failed";
968
+ var status200 = 200;
969
+ var status400 = 400;
966
970
 
967
971
  // src/modules/sigmaCache.js
968
972
  var sigmaCache = new import_node_cache.default({
@@ -1155,11 +1159,6 @@ function conditionOperation(userValue, conditionValues, operation, date, time) {
1155
1159
  }
1156
1160
  }
1157
1161
 
1158
- // src/helpers/delay.js
1159
- function delay(ms) {
1160
- return new Promise((resolve) => setTimeout(() => resolve(void 0), ms));
1161
- }
1162
-
1163
1162
  // src/helpers/digestHash.js
1164
1163
  function digestHash(string) {
1165
1164
  let hash = 0, i, chr;
@@ -1260,11 +1259,42 @@ function generateFilteredList(array, name) {
1260
1259
  }
1261
1260
 
1262
1261
  // package.json
1263
- var version = "3.3.0";
1262
+ var version = "3.3.1";
1264
1263
 
1265
1264
  // src/helpers/sdkVersion.js
1266
1265
  var sdkVersion = version;
1267
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
+
1268
1298
  // src/sigma.js
1269
1299
  if (typeof import_node_fetch.default.default !== "undefined")
1270
1300
  import_node_fetch.default.default;
@@ -1332,67 +1362,76 @@ var Sigma = class {
1332
1362
  this.cache.set(sigmaPrivateId, generateRandomId());
1333
1363
  }
1334
1364
  }
1335
- retryFetch(url, fetchOptions, retries = 3, timeout) {
1336
- return new Promise((resolve, reject) => {
1337
- if (timeout)
1338
- setTimeout(() => reject("error: timeout"), timeout);
1339
- const wrapper = (n) => {
1340
- (0, import_node_fetch.default)(url, fetchOptions).then((res) => resolve(res)).catch((err) => {
1341
- if (n > 0) {
1342
- delay(0).then(() => {
1343
- wrapper(--n);
1344
- });
1345
- } else {
1346
- reject(err);
1347
- }
1348
- });
1349
- };
1350
- wrapper(retries);
1351
- });
1352
- }
1353
- async getDataFile(url, method, body) {
1354
- const options = {
1355
- method: method || "GET",
1356
- body,
1357
- credentials: "same-origin",
1358
- headers: {
1359
- "ip": this.userData.ip,
1360
- "X-Real-IP": this.userData.ip,
1361
- token: this.token,
1362
- "user-id": this.cache.get(sigmaPrivateId),
1363
- "sdk-name": sdkName,
1364
- "sdk-version": sdkVersion
1365
- }
1365
+ async getDataFile(url) {
1366
+ let data;
1367
+ let sigmaData = {
1368
+ status: "",
1369
+ statusCode: "",
1370
+ data: null
1366
1371
  };
1367
- const data = await this.retryFetch(
1368
- url,
1369
- options,
1370
- this.retries
1371
- );
1372
- 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;
1373
1395
  }
1374
1396
  async getConfig() {
1375
1397
  let data;
1376
1398
  const localStorageHash = this.cache.get(sigmaHash);
1377
1399
  let hash;
1378
1400
  data = await this.getDataFile(`${this.api}/config.json`);
1379
- if (this.userData.platform) {
1380
- 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;
1381
1411
  }
1382
- hash = await SHA256.hex(JSON.stringify(data));
1383
- if (!localStorageHash || localStorageHash !== hash) {
1384
- this.cache.set(sigmaHash, hash);
1385
- 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
+ }
1386
1416
  }
1417
+ return;
1387
1418
  }
1388
1419
  async getUserGeoData() {
1420
+ if (this.userData.ip && isLocalOrIPv6(this.userData.ip)) {
1421
+ this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1422
+ return;
1423
+ }
1389
1424
  if (this.userData.ip) {
1390
1425
  const userGeoData = await this.getDataFile(`${this.api}/geo`);
1391
- 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
+ }
1392
1431
  } else if (this.userData.geo) {
1393
1432
  this.geoCache.set(sigmaGeoData, JSON.stringify(this.userData.geo));
1394
1433
  } else {
1395
- this.geoCache.set(sigmaGeoData, JSON.stringify({}));
1434
+ this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1396
1435
  }
1397
1436
  }
1398
1437
  recordLastUpdateInCache() {
@@ -1418,11 +1457,8 @@ var Sigma = class {
1418
1457
  await this.getUserGeoData();
1419
1458
  } catch {
1420
1459
  if (!this.geoCache.get(sigmaGeoData)) {
1421
- this.geoCache.set(sigmaGeoData, JSON.stringify({}));
1460
+ this.geoCache.set(sigmaGeoData, JSON.stringify({ location: "none" }));
1422
1461
  }
1423
- errorMessages.APIRequestErrorGeoData.token = this.token;
1424
- errorMessages.APIRequestErrorGeoData.userData = JSON.stringify(this.userData);
1425
- console.error(errorMessages.APIRequestErrorGeoData);
1426
1462
  }
1427
1463
  this.recordLastUpdateInCache();
1428
1464
  }