akeyless-client-commons 1.1.98 → 1.1.100

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.
@@ -312,6 +312,9 @@ var __toCommonJS = function(mod) {
312
312
  // src/hooks/index.ts
313
313
  var index_exports = {};
314
314
  __export(index_exports, {
315
+ useDbDocumentSnapshots: function() {
316
+ return useDbDocumentSnapshots;
317
+ },
315
318
  useDbSnapshots: function() {
316
319
  return useDbSnapshots;
317
320
  },
@@ -572,6 +575,99 @@ var snapshot = function(config, snapshotsFirstTime, settings) {
572
575
  unsubscribe: unsubscribe
573
576
  };
574
577
  };
578
+ var snapshotDocument = function(config, snapshotsFirstTime) {
579
+ var resolvePromise;
580
+ var isResolved = false;
581
+ var promise = new Promise(function(resolve) {
582
+ console.log("==> Document in ".concat(config.collectionName, " subscribed."));
583
+ resolvePromise = function() {
584
+ if (!isResolved) {
585
+ isResolved = true;
586
+ resolve();
587
+ }
588
+ };
589
+ });
590
+ var documentRef = (0, import_firestore2.doc)(db, config.collectionName, config.documentId);
591
+ var unsubscribe = (0, import_firestore2.onSnapshot)(documentRef, function(docSnapshot) {
592
+ if (!snapshotsFirstTime.includes(config.collectionName)) {
593
+ snapshotsFirstTime.push(config.collectionName);
594
+ if (docSnapshot.exists()) {
595
+ var document2 = simpleExtractData(docSnapshot);
596
+ if (checkConditions(document2, config.conditions)) {
597
+ var _config_onFirstTime, _config_extraParsers;
598
+ (_config_onFirstTime = config.onFirstTime) === null || _config_onFirstTime === void 0 ? void 0 : _config_onFirstTime.call(config, [
599
+ document2
600
+ ], config);
601
+ (_config_extraParsers = config.extraParsers) === null || _config_extraParsers === void 0 ? void 0 : _config_extraParsers.forEach(function(extraParser) {
602
+ var _extraParser_onFirstTime;
603
+ (_extraParser_onFirstTime = extraParser.onFirstTime) === null || _extraParser_onFirstTime === void 0 ? void 0 : _extraParser_onFirstTime.call(extraParser, [
604
+ document2
605
+ ], config);
606
+ });
607
+ } else {
608
+ console.warn("Document in ".concat(config.collectionName, " does not meet conditions."));
609
+ }
610
+ } else {
611
+ console.warn("Document not found in ".concat(config.collectionName, "."));
612
+ }
613
+ resolvePromise();
614
+ } else {
615
+ if (docSnapshot.exists()) {
616
+ var document21 = simpleExtractData(docSnapshot);
617
+ if (checkConditions(document21, config.conditions)) {
618
+ var _config_onModify, _config_extraParsers1;
619
+ (_config_onModify = config.onModify) === null || _config_onModify === void 0 ? void 0 : _config_onModify.call(config, [
620
+ document21
621
+ ], config);
622
+ (_config_extraParsers1 = config.extraParsers) === null || _config_extraParsers1 === void 0 ? void 0 : _config_extraParsers1.forEach(function(extraParser) {
623
+ var _extraParser_onModify;
624
+ (_extraParser_onModify = extraParser.onModify) === null || _extraParser_onModify === void 0 ? void 0 : _extraParser_onModify.call(extraParser, [
625
+ document21
626
+ ], config);
627
+ });
628
+ }
629
+ } else {
630
+ var _config_onRemove, _config_extraParsers2;
631
+ (_config_onRemove = config.onRemove) === null || _config_onRemove === void 0 ? void 0 : _config_onRemove.call(config, [], config);
632
+ (_config_extraParsers2 = config.extraParsers) === null || _config_extraParsers2 === void 0 ? void 0 : _config_extraParsers2.forEach(function(extraParser) {
633
+ var _extraParser_onRemove;
634
+ (_extraParser_onRemove = extraParser.onRemove) === null || _extraParser_onRemove === void 0 ? void 0 : _extraParser_onRemove.call(extraParser, [], config);
635
+ });
636
+ }
637
+ }
638
+ }, function(error) {
639
+ console.error("Error listening to document in ".concat(config.collectionName, ":"), error);
640
+ resolvePromise();
641
+ });
642
+ return {
643
+ promise: promise,
644
+ unsubscribe: unsubscribe
645
+ };
646
+ };
647
+ var checkConditions = function(document2, conditions) {
648
+ if (!conditions || conditions.length === 0) return true;
649
+ return conditions.every(function(condition) {
650
+ var fieldValue = document2[condition.field_name];
651
+ switch(condition.operator){
652
+ case "==":
653
+ return fieldValue === condition.value;
654
+ case "!=":
655
+ return fieldValue !== condition.value;
656
+ case "<":
657
+ return fieldValue < condition.value;
658
+ case "<=":
659
+ return fieldValue <= condition.value;
660
+ case ">":
661
+ return fieldValue > condition.value;
662
+ case ">=":
663
+ return fieldValue >= condition.value;
664
+ case "array-contains":
665
+ return Array.isArray(fieldValue) && fieldValue.includes(condition.value);
666
+ default:
667
+ return false;
668
+ }
669
+ });
670
+ };
575
671
  // src/helpers/global.ts
576
672
  var import_akeyless_types_commons = require("akeyless-types-commons");
577
673
  var import_axios = __toESM(require("axios"));
@@ -1304,7 +1400,7 @@ var useWebWorker = function(fn, options) {
1304
1400
  // src/hooks/snapshots/index.ts
1305
1401
  var import_react5 = require("react");
1306
1402
  // src/hooks/snapshots/snapshotWorker.ts
1307
- var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
1403
+ var createRecentEventsDedupe = function(options) {
1308
1404
  var recentEvents = /* @__PURE__ */ new Map();
1309
1405
  var _options_eventTtlMs;
1310
1406
  var EVENT_TTL_MS = (_options_eventTtlMs = options === null || options === void 0 ? void 0 : options.eventTtlMs) !== null && _options_eventTtlMs !== void 0 ? _options_eventTtlMs : 1500;
@@ -1313,16 +1409,6 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
1313
1409
  var now = function() {
1314
1410
  return Date.now();
1315
1411
  };
1316
- var makeKey = function(op, docs, collectionName) {
1317
- try {
1318
- var ids = (docs || []).map(function(d) {
1319
- return d === null || d === void 0 ? void 0 : d.id;
1320
- }).filter(Boolean).sort().join(",");
1321
- return "".concat(collectionName || "unknown", ":").concat(op, ":").concat(ids);
1322
- } catch (e) {
1323
- return "".concat(collectionName || "unknown", ":").concat(op, ":*");
1324
- }
1325
- };
1326
1412
  var shouldProcess = function(key) {
1327
1413
  var ts = recentEvents.get(key);
1328
1414
  if (ts && now() - ts < EVENT_TTL_MS) {
@@ -1354,6 +1440,22 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
1354
1440
  recentEvents.set(key, now());
1355
1441
  return true;
1356
1442
  };
1443
+ return {
1444
+ shouldProcess: shouldProcess
1445
+ };
1446
+ };
1447
+ var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
1448
+ var shouldProcess = createRecentEventsDedupe(options).shouldProcess;
1449
+ var makeKey = function(op, docs, collectionName) {
1450
+ try {
1451
+ var ids = (docs || []).map(function(d) {
1452
+ return d === null || d === void 0 ? void 0 : d.id;
1453
+ }).filter(Boolean).sort().join(",");
1454
+ return "".concat(collectionName || "unknown", ":").concat(op, ":").concat(ids);
1455
+ } catch (e) {
1456
+ return "".concat(collectionName || "unknown", ":").concat(op, ":*");
1457
+ }
1458
+ };
1357
1459
  var wrapCb = function(cb, op, run) {
1358
1460
  if (!cb) return void 0;
1359
1461
  var handlerId = Math.random().toString(36).slice(2);
@@ -1429,6 +1531,91 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
1429
1531
  return wrapped;
1430
1532
  });
1431
1533
  };
1534
+ var wrapDocumentConfigsWithWorker = function(cfgs, runProcessor, options) {
1535
+ var shouldProcess = createRecentEventsDedupe(options).shouldProcess;
1536
+ var makeKey = function(op, docs, collectionName, documentId) {
1537
+ try {
1538
+ var ids = (docs || []).map(function(d) {
1539
+ return d === null || d === void 0 ? void 0 : d.id;
1540
+ }).filter(Boolean).sort().join(",");
1541
+ return "".concat(collectionName || "unknown", "/").concat(documentId || "unknown", ":").concat(op, ":").concat(ids);
1542
+ } catch (e) {
1543
+ return "".concat(collectionName || "unknown", "/").concat(documentId || "unknown", ":").concat(op, ":*");
1544
+ }
1545
+ };
1546
+ var wrapCb = function(cb, op) {
1547
+ if (!cb) return void 0;
1548
+ var handlerId = Math.random().toString(36).slice(2);
1549
+ return /*#__PURE__*/ function() {
1550
+ var _ref = _async_to_generator(function(docs, config) {
1551
+ var key, perHandlerKey, safeDocs, _ref, processed, e;
1552
+ return _ts_generator(this, function(_state) {
1553
+ switch(_state.label){
1554
+ case 0:
1555
+ _state.trys.push([
1556
+ 0,
1557
+ 2,
1558
+ ,
1559
+ 3
1560
+ ]);
1561
+ key = makeKey(op, docs, config === null || config === void 0 ? void 0 : config.collectionName, config === null || config === void 0 ? void 0 : config.documentId);
1562
+ perHandlerKey = "".concat(handlerId, ":").concat(key);
1563
+ if (!shouldProcess(perHandlerKey)) {
1564
+ return [
1565
+ 2
1566
+ ];
1567
+ }
1568
+ safeDocs = (options === null || options === void 0 ? void 0 : options.jsonClone) === false ? docs : JSON.parse(JSON.stringify(docs));
1569
+ return [
1570
+ 4,
1571
+ runProcessor({
1572
+ op: op,
1573
+ docs: safeDocs
1574
+ })
1575
+ ];
1576
+ case 1:
1577
+ _ref = _state.sent(), processed = _ref.docs;
1578
+ cb(processed, config);
1579
+ return [
1580
+ 3,
1581
+ 3
1582
+ ];
1583
+ case 2:
1584
+ e = _state.sent();
1585
+ cb(docs, config);
1586
+ return [
1587
+ 3,
1588
+ 3
1589
+ ];
1590
+ case 3:
1591
+ return [
1592
+ 2
1593
+ ];
1594
+ }
1595
+ });
1596
+ });
1597
+ return function(docs, config) {
1598
+ return _ref.apply(this, arguments);
1599
+ };
1600
+ }();
1601
+ };
1602
+ return cfgs.map(function(cfg) {
1603
+ var wrapped = _object_spread({}, cfg);
1604
+ wrapped.onFirstTime = wrapCb(cfg.onFirstTime, "first");
1605
+ wrapped.onModify = wrapCb(cfg.onModify, "modify");
1606
+ wrapped.onRemove = wrapCb(cfg.onRemove, "remove");
1607
+ if (cfg.extraParsers && cfg.extraParsers.length) {
1608
+ wrapped.extraParsers = cfg.extraParsers.map(function(p) {
1609
+ return {
1610
+ onFirstTime: wrapCb(p.onFirstTime, "first"),
1611
+ onModify: wrapCb(p.onModify, "modify"),
1612
+ onRemove: wrapCb(p.onRemove, "remove")
1613
+ };
1614
+ });
1615
+ }
1616
+ return wrapped;
1617
+ });
1618
+ };
1432
1619
  // src/hooks/snapshots/index.ts
1433
1620
  var useDbSnapshots = function(configs, label, settings) {
1434
1621
  var snapshotsFirstTime = (0, import_react5.useRef)([]);
@@ -1505,6 +1692,93 @@ var useDbSnapshots = function(configs, label, settings) {
1505
1692
  };
1506
1693
  }, []);
1507
1694
  };
1695
+ var useDbDocumentSnapshots = function(configs, label, settings) {
1696
+ var unsubscribeFunctions = (0, import_react5.useRef)([]);
1697
+ var lastDbDocsRef = (0, import_react5.useRef)([]);
1698
+ var perDocFirstTimeRef = (0, import_react5.useRef)({});
1699
+ var workerProcessorDb = (0, import_react5.useCallback)(function(payload) {
1700
+ return {
1701
+ docs: payload.docs
1702
+ };
1703
+ }, []);
1704
+ var runProcessor = useWebWorker(workerProcessorDb, settings === null || settings === void 0 ? void 0 : settings.worker);
1705
+ var wrapConfigsForWorker = function(cfgs) {
1706
+ return wrapDocumentConfigsWithWorker(cfgs, function(payload) {
1707
+ return runProcessor(payload);
1708
+ });
1709
+ };
1710
+ var getDocKey = function(config) {
1711
+ var _config_conditions;
1712
+ var conditionsKey = ((_config_conditions = config.conditions) === null || _config_conditions === void 0 ? void 0 : _config_conditions.length) ? JSON.stringify(config.conditions) : "";
1713
+ return "".concat(config.collectionName, "/").concat(config.documentId).concat(conditionsKey ? ":".concat(conditionsKey) : "");
1714
+ };
1715
+ var getFirstTimeBucket = function(config) {
1716
+ var key = getDocKey(config);
1717
+ if (!perDocFirstTimeRef.current[key]) {
1718
+ perDocFirstTimeRef.current[key] = [];
1719
+ }
1720
+ return perDocFirstTimeRef.current[key];
1721
+ };
1722
+ useDeepCompareEffect(function() {
1723
+ var start = performance.now();
1724
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
1725
+ console.log("==> ".concat(label || "DB document snapshots", " started from db... "));
1726
+ }
1727
+ var wrappedConfigs = wrapConfigsForWorker(configs);
1728
+ lastDbDocsRef.current = configs.map(function(c) {
1729
+ return "".concat(c.collectionName, "/").concat(c.documentId);
1730
+ });
1731
+ var snapshotResults = wrappedConfigs.map(function(config) {
1732
+ return snapshotDocument(config, getFirstTimeBucket(config));
1733
+ });
1734
+ unsubscribeFunctions.current = snapshotResults.map(function(result) {
1735
+ return result.unsubscribe;
1736
+ });
1737
+ Promise.all(snapshotResults.map(function(result) {
1738
+ return result.promise;
1739
+ })).then(function() {
1740
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
1741
+ console.log("==> ".concat(label || "DB document snapshots", " ended from db. It took ").concat((performance.now() - start).toFixed(2), " ms"));
1742
+ }
1743
+ });
1744
+ if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
1745
+ return function() {
1746
+ var _settings_worker;
1747
+ unsubscribeFunctions.current.forEach(function(unsubscribe) {
1748
+ if (unsubscribe) {
1749
+ unsubscribe();
1750
+ }
1751
+ });
1752
+ if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbDocsRef.current.length) {
1753
+ console.log("==> ".concat(label || "DB document snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbDocsRef.current.join(", "), "]"));
1754
+ }
1755
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
1756
+ console.log("==> ".concat(label || "DB document snapshots", " unsubscribed from db"));
1757
+ }
1758
+ };
1759
+ }
1760
+ }, [
1761
+ configs,
1762
+ label,
1763
+ settings
1764
+ ]);
1765
+ (0, import_react5.useEffect)(function() {
1766
+ return function() {
1767
+ var _settings_worker;
1768
+ unsubscribeFunctions.current.forEach(function(unsubscribe) {
1769
+ if (unsubscribe) {
1770
+ unsubscribe();
1771
+ }
1772
+ });
1773
+ if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbDocsRef.current.length) {
1774
+ console.log("==> ".concat(label || "DB document snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbDocsRef.current.join(", "), "]"));
1775
+ }
1776
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
1777
+ console.log("==> ".concat(label || "DB document snapshots", " unsubscribed"));
1778
+ }
1779
+ };
1780
+ }, []);
1781
+ };
1508
1782
  var useSmartSnapshots = function(configs, label, settings) {
1509
1783
  var _ref = _sliced_to_array((0, import_react5.useState)(null), 2), cacheCollectionsConfig = _ref[0], setCacheCollectionsConfig = _ref[1];
1510
1784
  (0, import_react5.useEffect)(function() {
@@ -1701,6 +1975,7 @@ var useSocketSnapshots = function(configs, label, settings) {
1701
1975
  };
1702
1976
  // Annotate the CommonJS export names for ESM import in node:
1703
1977
  0 && (module.exports = {
1978
+ useDbDocumentSnapshots: useDbDocumentSnapshots,
1704
1979
  useDbSnapshots: useDbSnapshots,
1705
1980
  useDeepCompareEffect: useDeepCompareEffect,
1706
1981
  useDeepCompareMemo: useDeepCompareMemo,