akeyless-client-commons 1.1.129 → 1.1.130

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.
@@ -7,6 +7,9 @@ function _array_like_to_array(arr, len) {
7
7
  function _array_with_holes(arr) {
8
8
  if (Array.isArray(arr)) return arr;
9
9
  }
10
+ function _array_without_holes(arr) {
11
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
12
+ }
10
13
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
11
14
  try {
12
15
  var info = gen[key](arg);
@@ -75,6 +78,9 @@ function _instanceof(left, right) {
75
78
  return left instanceof right;
76
79
  }
77
80
  }
81
+ function _iterable_to_array(iter) {
82
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
83
+ }
78
84
  function _iterable_to_array_limit(arr, i) {
79
85
  var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
80
86
  if (_i == null) return;
@@ -102,6 +108,9 @@ function _iterable_to_array_limit(arr, i) {
102
108
  function _non_iterable_rest() {
103
109
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
104
110
  }
111
+ function _non_iterable_spread() {
112
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
113
+ }
105
114
  function _object_spread(target) {
106
115
  for(var i = 1; i < arguments.length; i++){
107
116
  var source = arguments[i] != null ? arguments[i] : {};
@@ -154,6 +163,9 @@ function _tagged_template_literal(strings, raw) {
154
163
  }
155
164
  }));
156
165
  }
166
+ function _to_consumable_array(arr) {
167
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
168
+ }
157
169
  function _type_of(obj) {
158
170
  "@swc/helpers - typeof";
159
171
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
@@ -424,17 +436,105 @@ var import_storage2 = require("firebase/storage");
424
436
  // src/helpers/time_helpers.ts
425
437
  var import_firestore = require("firebase/firestore");
426
438
  var import_moment_timezone = __toESM(require("moment-timezone"));
427
- // src/helpers/firebase.ts
439
+ // src/helpers/global.ts
440
+ var import_akeyless_types_commons = require("akeyless-types-commons");
441
+ var import_axios = __toESM(require("axios"));
442
+ var import_lodash = require("lodash");
428
443
  var import_meta = {};
444
+ var getUserCountryByIp = /*#__PURE__*/ function() {
445
+ var _ref = _async_to_generator(function() {
446
+ var response, data, error;
447
+ return _ts_generator(this, function(_state) {
448
+ switch(_state.label){
449
+ case 0:
450
+ _state.trys.push([
451
+ 0,
452
+ 3,
453
+ ,
454
+ 4
455
+ ]);
456
+ return [
457
+ 4,
458
+ fetch("https://ipapi.co/json/")
459
+ ];
460
+ case 1:
461
+ response = _state.sent();
462
+ return [
463
+ 4,
464
+ response.json()
465
+ ];
466
+ case 2:
467
+ data = _state.sent();
468
+ return [
469
+ 2,
470
+ (data.country_code || import_akeyless_types_commons.CountryOptions.IL).toLowerCase()
471
+ ];
472
+ case 3:
473
+ error = _state.sent();
474
+ console.error("Error fetching Country:", error);
475
+ return [
476
+ 2,
477
+ import_akeyless_types_commons.CountryOptions.IL
478
+ ];
479
+ case 4:
480
+ return [
481
+ 2
482
+ ];
483
+ }
484
+ });
485
+ });
486
+ return function getUserCountryByIp() {
487
+ return _ref.apply(this, arguments);
488
+ };
489
+ }();
490
+ var isNodeEnv = typeof process !== "undefined" && process.env;
491
+ var _ref = {
492
+ mode: isNodeEnv ? process.env.NEXT_PUBLIC_MODE : import_meta.env.VITE_MODE,
493
+ isLocal: (isNodeEnv ? process.env.NEXT_PUBLIC_IS_LOCAL : import_meta.env.VITE_is_local) === "true"
494
+ }, mode = _ref.mode, isLocal = _ref.isLocal;
495
+ var checkConditions = function(document2, conditions) {
496
+ if (!conditions || conditions.length === 0) return true;
497
+ return conditions.every(function(condition) {
498
+ var fieldValue = document2[condition.field_name];
499
+ switch(condition.operator){
500
+ case "==":
501
+ return fieldValue === condition.value;
502
+ case "!=":
503
+ return fieldValue !== condition.value;
504
+ case "<":
505
+ return fieldValue < condition.value;
506
+ case "<=":
507
+ return fieldValue <= condition.value;
508
+ case ">":
509
+ return fieldValue > condition.value;
510
+ case ">=":
511
+ return fieldValue >= condition.value;
512
+ case "in":
513
+ return Array.isArray(condition.value) && condition.value.includes(fieldValue);
514
+ case "not-in":
515
+ return Array.isArray(condition.value) && !condition.value.includes(fieldValue);
516
+ case "array-contains":
517
+ return Array.isArray(fieldValue) && fieldValue.includes(condition.value);
518
+ case "array-contains-any":
519
+ return Array.isArray(fieldValue) && Array.isArray(condition.value) && condition.value.some(function(v) {
520
+ return fieldValue.includes(v);
521
+ });
522
+ default:
523
+ return false;
524
+ }
525
+ });
526
+ };
527
+ // src/helpers/firebase.ts
528
+ var import_meta2 = {};
429
529
  var initApp = function() {
430
530
  var isNodeEnv2 = typeof process !== "undefined" && process.env;
431
531
  var firebaseConfig = {
432
- apiKey: isNodeEnv2 ? process.env.NEXT_PUBLIC_API_KEY : import_meta.env.VITE_API_KEY,
433
- authDomain: isNodeEnv2 ? process.env.NEXT_PUBLIC_AUTH_DOMAIN : import_meta.env.VITE_AUTH_DOMAIN,
434
- projectId: isNodeEnv2 ? process.env.NEXT_PUBLIC_PROJECT_ID : import_meta.env.VITE_PROJECT_ID,
435
- storageBucket: isNodeEnv2 ? process.env.NEXT_PUBLIC_STORAGE_BUCKET : import_meta.env.VITE_STORAGE_BUCKET,
436
- messagingSenderId: isNodeEnv2 ? process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID : import_meta.env.VITE_MESSAGING_SENDER_ID,
437
- appId: isNodeEnv2 ? process.env.NEXT_PUBLIC_APP_ID : import_meta.env.VITE_APP_ID
532
+ apiKey: isNodeEnv2 ? process.env.NEXT_PUBLIC_API_KEY : import_meta2.env.VITE_API_KEY,
533
+ authDomain: isNodeEnv2 ? process.env.NEXT_PUBLIC_AUTH_DOMAIN : import_meta2.env.VITE_AUTH_DOMAIN,
534
+ projectId: isNodeEnv2 ? process.env.NEXT_PUBLIC_PROJECT_ID : import_meta2.env.VITE_PROJECT_ID,
535
+ storageBucket: isNodeEnv2 ? process.env.NEXT_PUBLIC_STORAGE_BUCKET : import_meta2.env.VITE_STORAGE_BUCKET,
536
+ messagingSenderId: isNodeEnv2 ? process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID : import_meta2.env.VITE_MESSAGING_SENDER_ID,
537
+ appId: isNodeEnv2 ? process.env.NEXT_PUBLIC_APP_ID : import_meta2.env.VITE_APP_ID
438
538
  };
439
539
  try {
440
540
  var app2 = (0, import_app.initializeApp)(firebaseConfig);
@@ -443,7 +543,7 @@ var initApp = function() {
443
543
  var db2 = (0, import_firestore2.getFirestore)(app2);
444
544
  var storage2 = (0, import_storage.getStorage)(app2);
445
545
  var googleLoginProvider2 = new import_auth.GoogleAuthProvider();
446
- var recaptchaSiteKey = isNodeEnv2 ? process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY : import_meta.env.VITE_RECAPTCHA_SITE_KEY;
546
+ var recaptchaSiteKey = isNodeEnv2 ? process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY : import_meta2.env.VITE_RECAPTCHA_SITE_KEY;
447
547
  var result = {
448
548
  db: db2,
449
549
  auth: auth2,
@@ -687,86 +787,6 @@ var snapshotDocument = function(config, snapshotsFirstTime) {
687
787
  unsubscribe: unsubscribe
688
788
  };
689
789
  };
690
- var checkConditions = function(document2, conditions) {
691
- if (!conditions || conditions.length === 0) return true;
692
- return conditions.every(function(condition) {
693
- var fieldValue = document2[condition.field_name];
694
- switch(condition.operator){
695
- case "==":
696
- return fieldValue === condition.value;
697
- case "!=":
698
- return fieldValue !== condition.value;
699
- case "<":
700
- return fieldValue < condition.value;
701
- case "<=":
702
- return fieldValue <= condition.value;
703
- case ">":
704
- return fieldValue > condition.value;
705
- case ">=":
706
- return fieldValue >= condition.value;
707
- case "array-contains":
708
- return Array.isArray(fieldValue) && fieldValue.includes(condition.value);
709
- default:
710
- return false;
711
- }
712
- });
713
- };
714
- // src/helpers/global.ts
715
- var import_akeyless_types_commons = require("akeyless-types-commons");
716
- var import_axios = __toESM(require("axios"));
717
- var import_lodash = require("lodash");
718
- var import_meta2 = {};
719
- var getUserCountryByIp = /*#__PURE__*/ function() {
720
- var _ref = _async_to_generator(function() {
721
- var response, data, error;
722
- return _ts_generator(this, function(_state) {
723
- switch(_state.label){
724
- case 0:
725
- _state.trys.push([
726
- 0,
727
- 3,
728
- ,
729
- 4
730
- ]);
731
- return [
732
- 4,
733
- fetch("https://ipapi.co/json/")
734
- ];
735
- case 1:
736
- response = _state.sent();
737
- return [
738
- 4,
739
- response.json()
740
- ];
741
- case 2:
742
- data = _state.sent();
743
- return [
744
- 2,
745
- (data.country_code || import_akeyless_types_commons.CountryOptions.IL).toLowerCase()
746
- ];
747
- case 3:
748
- error = _state.sent();
749
- console.error("Error fetching Country:", error);
750
- return [
751
- 2,
752
- import_akeyless_types_commons.CountryOptions.IL
753
- ];
754
- case 4:
755
- return [
756
- 2
757
- ];
758
- }
759
- });
760
- });
761
- return function getUserCountryByIp() {
762
- return _ref.apply(this, arguments);
763
- };
764
- }();
765
- var isNodeEnv = typeof process !== "undefined" && process.env;
766
- var _ref = {
767
- mode: isNodeEnv ? process.env.NEXT_PUBLIC_MODE : import_meta2.env.VITE_MODE,
768
- isLocal: (isNodeEnv ? process.env.NEXT_PUBLIC_IS_LOCAL : import_meta2.env.VITE_is_local) === "true"
769
- }, mode = _ref.mode, isLocal = _ref.isLocal;
770
790
  // src/helpers/forms.ts
771
791
  var import_xregexp = __toESM(require("xregexp"));
772
792
  var textRegex = (0, import_xregexp.default)(String.raw(_templateObject()), "gu");
@@ -809,6 +829,9 @@ var SocketService = /*#__PURE__*/ function() {
809
829
  this.disconnectCallbacks = [];
810
830
  this.authToken = null;
811
831
  this.isDisconnected = true;
832
+ /// the data-socket keeps one subscription per collection per socket, so a second condition set
833
+ /// for a collection never reaches it and would silently receive the first one's documents
834
+ this.activeConditions = /* @__PURE__ */ new Map();
812
835
  this.handleDisconnect = function(source, reason) {
813
836
  var _this_socket;
814
837
  if (_this.isDisconnected) {
@@ -976,6 +999,39 @@ var SocketService = /*#__PURE__*/ function() {
976
999
  }
977
1000
  }
978
1001
  },
1002
+ {
1003
+ key: "registerConditions",
1004
+ value: function registerConditions(collectionName) {
1005
+ var conditions = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
1006
+ var signature = JSON.stringify(conditions);
1007
+ var active = this.activeConditions.get(collectionName);
1008
+ if (!active) {
1009
+ this.activeConditions.set(collectionName, {
1010
+ signature: signature,
1011
+ conditions: conditions,
1012
+ count: 1
1013
+ });
1014
+ return;
1015
+ }
1016
+ active.count++;
1017
+ if (active.signature !== signature) {
1018
+ console.error('[socket] "'.concat(collectionName, '" is already subscribed with different conditions, and a collection supports one condition set at a time. The active set stays in effect, so this subscription receives its documents instead of its own. active: ').concat(active.signature, " requested: ").concat(signature));
1019
+ }
1020
+ }
1021
+ },
1022
+ {
1023
+ key: "releaseConditions",
1024
+ value: function releaseConditions(collectionName) {
1025
+ var active = this.activeConditions.get(collectionName);
1026
+ if (!active) {
1027
+ return;
1028
+ }
1029
+ active.count--;
1030
+ if (active.count <= 0) {
1031
+ this.activeConditions.delete(collectionName);
1032
+ }
1033
+ }
1034
+ },
979
1035
  {
980
1036
  key: "disconnectSocket",
981
1037
  value: function disconnectSocket() {
@@ -987,6 +1043,8 @@ var SocketService = /*#__PURE__*/ function() {
987
1043
  },
988
1044
  {
989
1045
  /// subscribe to collections
1046
+ /// conditions are derived from OnSnapshotConfig.conditions, never passed in, so a caller cannot
1047
+ /// set them in a second place and have them silently overwritten
990
1048
  key: "subscribeToCollections",
991
1049
  value: function subscribeToCollections(config, options) {
992
1050
  var _this = this;
@@ -999,26 +1057,77 @@ var SocketService = /*#__PURE__*/ function() {
999
1057
  });
1000
1058
  var eventHandlers = [];
1001
1059
  config.forEach(function(configuration) {
1002
- var collectionName = configuration.collectionName, onAdd = configuration.onAdd, onFirstTime = configuration.onFirstTime, onModify = configuration.onModify, onRemove = configuration.onRemove, extraParsers = configuration.extraParsers;
1060
+ var collectionName = configuration.collectionName, conditions = configuration.conditions;
1061
+ var parsers = [
1062
+ configuration
1063
+ ].concat(_to_consumable_array(configuration.extraParsers || []));
1064
+ var matchedIds = /* @__PURE__ */ new Set();
1003
1065
  var attach = function(eventName, handler) {
1004
- if (!handler) return;
1005
- _this.socket.off(eventName, handler);
1006
1066
  _this.socket.on(eventName, handler);
1007
1067
  eventHandlers.push({
1008
1068
  eventName: eventName,
1009
1069
  handler: handler
1010
1070
  });
1011
1071
  };
1012
- attach("initial:".concat(collectionName), onFirstTime);
1013
- attach("add:".concat(collectionName), onAdd);
1014
- attach("update:".concat(collectionName), onModify);
1015
- attach("delete:".concat(collectionName), onRemove);
1016
- extraParsers === null || extraParsers === void 0 ? void 0 : extraParsers.forEach(function(parsers) {
1017
- var extraOnAdd = parsers.onAdd, extraOnFirstTime = parsers.onFirstTime, extraOnModify = parsers.onModify, extraOnRemove = parsers.onRemove;
1018
- attach("initial:".concat(collectionName), extraOnFirstTime);
1019
- attach("add:".concat(collectionName), extraOnAdd);
1020
- attach("update:".concat(collectionName), extraOnModify);
1021
- attach("delete:".concat(collectionName), extraOnRemove);
1072
+ var toDocs = function(payload) {
1073
+ return Array.isArray(payload) ? payload : payload ? [
1074
+ payload
1075
+ ] : [];
1076
+ };
1077
+ var run = function(op, docs) {
1078
+ return parsers.forEach(function(parser) {
1079
+ var _parser_op;
1080
+ return (_parser_op = parser[op]) === null || _parser_op === void 0 ? void 0 : _parser_op.call(parser, docs, configuration);
1081
+ });
1082
+ };
1083
+ var runIfAny = function(op, docs) {
1084
+ if (docs.length) {
1085
+ run(op, docs);
1086
+ }
1087
+ };
1088
+ attach("initial:".concat(collectionName), function(payload) {
1089
+ var docs = toDocs(payload).filter(function(doc2) {
1090
+ return checkConditions(doc2, conditions);
1091
+ });
1092
+ matchedIds.clear();
1093
+ docs.forEach(function(doc2) {
1094
+ return matchedIds.add(doc2.id);
1095
+ });
1096
+ run("onFirstTime", docs);
1097
+ });
1098
+ var handleUpsert = function(payload, defaultOp) {
1099
+ var added = [];
1100
+ var modified = [];
1101
+ var removed = [];
1102
+ toDocs(payload).forEach(function(doc2) {
1103
+ if (!(conditions === null || conditions === void 0 ? void 0 : conditions.length)) {
1104
+ (defaultOp === "onAdd" ? added : modified).push(doc2);
1105
+ return;
1106
+ }
1107
+ var wasMatching = matchedIds.has(doc2.id);
1108
+ if (checkConditions(doc2, conditions)) {
1109
+ matchedIds.add(doc2.id);
1110
+ (wasMatching ? modified : added).push(doc2);
1111
+ } else if (wasMatching) {
1112
+ matchedIds.delete(doc2.id);
1113
+ removed.push(doc2);
1114
+ }
1115
+ });
1116
+ runIfAny("onAdd", added);
1117
+ runIfAny("onModify", modified);
1118
+ runIfAny("onRemove", removed);
1119
+ };
1120
+ attach("add:".concat(collectionName), function(payload) {
1121
+ return handleUpsert(payload, "onAdd");
1122
+ });
1123
+ attach("update:".concat(collectionName), function(payload) {
1124
+ return handleUpsert(payload, "onModify");
1125
+ });
1126
+ attach("delete:".concat(collectionName), function(payload) {
1127
+ var docs = toDocs(payload).filter(function(doc2) {
1128
+ return !(conditions === null || conditions === void 0 ? void 0 : conditions.length) || matchedIds.delete(doc2.id);
1129
+ });
1130
+ runIfAny("onRemove", docs);
1022
1131
  });
1023
1132
  });
1024
1133
  var acknowledge = function(callback) {
@@ -1028,12 +1137,28 @@ var SocketService = /*#__PURE__*/ function() {
1028
1137
  console.error("Failed to subscribe to ".concat(config.join(", "), ": ").concat(callback.message));
1029
1138
  }
1030
1139
  };
1031
- if (options) {
1032
- s.emit("subscribe_options", options);
1140
+ config.forEach(function(c) {
1141
+ return _this.registerConditions(c.collectionName, c.conditions);
1142
+ });
1143
+ var conditionsByCollection = config.reduce(function(acc, c) {
1144
+ var active = _this.activeConditions.get(c.collectionName);
1145
+ if (active === null || active === void 0 ? void 0 : active.conditions.length) {
1146
+ acc[c.collectionName] = active.conditions;
1147
+ }
1148
+ return acc;
1149
+ }, {});
1150
+ var subscribeOptions = Object.keys(conditionsByCollection).length ? _object_spread_props(_object_spread({}, options), {
1151
+ conditions: conditionsByCollection
1152
+ }) : options;
1153
+ if (subscribeOptions) {
1154
+ s.emit("subscribe_options", subscribeOptions);
1033
1155
  }
1034
1156
  s.emit("subscribe_collections", collectionsNames, acknowledge);
1035
1157
  return function() {
1036
1158
  console.log("Cleaning up subscriptions for: ".concat(collectionsNames.join(", ")));
1159
+ collectionsNames.forEach(function(name) {
1160
+ return _this.releaseConditions(name);
1161
+ });
1037
1162
  s.emit("unsubscribe_collections", collectionsNames);
1038
1163
  eventHandlers.forEach(function(eh) {
1039
1164
  s.off(eh.eventName, eh.handler);