@resolveio/server-lib 20.14.16 → 20.14.18

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.
@@ -93,6 +93,11 @@ export declare class SubscriptionManager {
93
93
  private queueFullResync;
94
94
  private fullResyncSubscriptions;
95
95
  private tailOpLog;
96
+ private packCachePayload;
97
+ private decodeCachePayload;
98
+ private getCacheBuffer;
99
+ private shouldCachePayload;
100
+ private buffersEqual;
96
101
  private processSubscription;
97
102
  private sendDataToOne;
98
103
  private sendDataToAll;
@@ -74,6 +74,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
74
74
  Object.defineProperty(exports, "__esModule", { value: true });
75
75
  exports.SubscriptionManager = void 0;
76
76
  var NodeCache = require("node-cache");
77
+ var msgpackr_1 = require("msgpackr");
77
78
  var flag_collection_1 = require("../collections/flag.collection");
78
79
  var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
79
80
  var app_status_1 = require("../publications/app-status");
@@ -1679,6 +1680,108 @@ var SubscriptionManager = /** @class */ (function () {
1679
1680
  });
1680
1681
  });
1681
1682
  };
1683
+ SubscriptionManager.prototype.packCachePayload = function (data) {
1684
+ if (data === undefined) {
1685
+ return null;
1686
+ }
1687
+ try {
1688
+ var packed = (0, msgpackr_1.pack)(data);
1689
+ return Buffer.isBuffer(packed) ? packed : Buffer.from(packed);
1690
+ }
1691
+ catch (err) {
1692
+ if (this._enableDebug) {
1693
+ console.log(new Date(), 'Sub Cache', 'Pack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
1694
+ }
1695
+ return null;
1696
+ }
1697
+ };
1698
+ SubscriptionManager.prototype.decodeCachePayload = function (raw) {
1699
+ if (!raw) {
1700
+ return null;
1701
+ }
1702
+ if (Buffer.isBuffer(raw)) {
1703
+ try {
1704
+ return (0, msgpackr_1.unpack)(raw);
1705
+ }
1706
+ catch (err) {
1707
+ if (this._enableDebug) {
1708
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
1709
+ }
1710
+ return null;
1711
+ }
1712
+ }
1713
+ if (raw instanceof Uint8Array) {
1714
+ try {
1715
+ return (0, msgpackr_1.unpack)(raw);
1716
+ }
1717
+ catch (err) {
1718
+ if (this._enableDebug) {
1719
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
1720
+ }
1721
+ return null;
1722
+ }
1723
+ }
1724
+ if (raw instanceof ArrayBuffer) {
1725
+ try {
1726
+ return (0, msgpackr_1.unpack)(new Uint8Array(raw));
1727
+ }
1728
+ catch (err) {
1729
+ if (this._enableDebug) {
1730
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
1731
+ }
1732
+ return null;
1733
+ }
1734
+ }
1735
+ // Treat legacy string caches as invalid to avoid JSON parsing in the hot path.
1736
+ if (typeof raw === 'string') {
1737
+ return null;
1738
+ }
1739
+ return raw;
1740
+ };
1741
+ SubscriptionManager.prototype.getCacheBuffer = function (raw) {
1742
+ if (!raw) {
1743
+ return null;
1744
+ }
1745
+ if (Buffer.isBuffer(raw)) {
1746
+ return raw;
1747
+ }
1748
+ if (raw instanceof Uint8Array) {
1749
+ return Buffer.from(raw);
1750
+ }
1751
+ if (raw instanceof ArrayBuffer) {
1752
+ return Buffer.from(new Uint8Array(raw));
1753
+ }
1754
+ return null;
1755
+ };
1756
+ SubscriptionManager.prototype.shouldCachePayload = function (sub, payload) {
1757
+ if (!payload || payload.byteLength >= 1000000) {
1758
+ return false;
1759
+ }
1760
+ if (sub.collections.includes('logs')) {
1761
+ return false;
1762
+ }
1763
+ if (sub.collections.find(function (a) { return a.endsWith('.versions'); })) {
1764
+ return false;
1765
+ }
1766
+ if (sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
1767
+ return false;
1768
+ }
1769
+ return true;
1770
+ };
1771
+ SubscriptionManager.prototype.buffersEqual = function (a, b) {
1772
+ if (!a || !b) {
1773
+ return false;
1774
+ }
1775
+ if (a.byteLength !== b.byteLength) {
1776
+ return false;
1777
+ }
1778
+ for (var index = 0; index < a.byteLength; index++) {
1779
+ if (a[index] !== b[index]) {
1780
+ return false;
1781
+ }
1782
+ }
1783
+ return true;
1784
+ };
1682
1785
  SubscriptionManager.prototype.processSubscription = function (sub, ws, messageId) {
1683
1786
  return __awaiter(this, void 0, void 0, function () {
1684
1787
  var cacheData, serverRes, _a;
@@ -1690,7 +1793,10 @@ var SubscriptionManager = /** @class */ (function () {
1690
1793
  _b.label = 1;
1691
1794
  case 1:
1692
1795
  _b.trys.push([1, 2, , 4]);
1693
- cacheData = JSON.parse(this._nodeCache.get(sub.cacheId), common_1.dateReviver);
1796
+ cacheData = this.decodeCachePayload(this._nodeCache.get(sub.cacheId));
1797
+ if (cacheData === null || cacheData === undefined) {
1798
+ throw new Error('cache-miss');
1799
+ }
1694
1800
  serverRes = {
1695
1801
  messageId: messageId,
1696
1802
  hasError: false,
@@ -1840,7 +1946,7 @@ var SubscriptionManager = /** @class */ (function () {
1840
1946
  // Fetch pub once, send to all clients linked to this pub
1841
1947
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
1842
1948
  return __awaiter(this, void 0, void 0, function () {
1843
- var subIndex, startMs, monitor, res_1, dependencySnapshot, execution, cacheData, _a, _b, client, ws, serverRes, _c, _d, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _e, normalizedError, correlationId, _f, _g, client, ws, serverRes, errorPayload, durationMs;
1949
+ var subIndex, startMs, monitor, res, dependencySnapshot, execution, packedRes, shouldCache, cachedBuffer, isSame, _a, _b, client, ws, serverRes, _c, _d, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _e, normalizedError, correlationId, _f, _g, client, ws, serverRes, errorPayload, durationMs;
1844
1950
  var e_6, _h, e_7, _j, e_8, _k;
1845
1951
  var _this = this;
1846
1952
  var _l;
@@ -1860,6 +1966,7 @@ var SubscriptionManager = /** @class */ (function () {
1860
1966
  case 1:
1861
1967
  startMs = Date.now();
1862
1968
  monitor = this._monitorManagerFunction.startMonitorFunction('Publication', sub.publication, '', '', sub.subscriptionData);
1969
+ res = void 0;
1863
1970
  dependencySnapshot = void 0;
1864
1971
  _m.label = 2;
1865
1972
  case 2:
@@ -1876,69 +1983,51 @@ var SubscriptionManager = /** @class */ (function () {
1876
1983
  })];
1877
1984
  case 3:
1878
1985
  execution = _m.sent();
1879
- res_1 = execution.result;
1986
+ res = execution.result;
1880
1987
  dependencySnapshot = execution.snapshot;
1881
1988
  this.updateSubscriptionDependencies(sub, dependencySnapshot);
1989
+ packedRes = this.packCachePayload(res);
1990
+ shouldCache = this.shouldCachePayload(sub, packedRes);
1882
1991
  if (sub.cacheId) {
1883
- try {
1884
- cacheData = JSON.parse(this._nodeCache.get(sub.cacheId), common_1.dateReviver);
1885
- if (JSON.stringify(cacheData) !== JSON.stringify(res_1)) {
1886
- try {
1887
- for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
1888
- client = _b.value;
1889
- ws = this._websocketManager.getWebSocket(client.id_socket);
1890
- if (ws && ws.readyState === ws.OPEN) {
1891
- serverRes = {
1892
- messageId: client.messageId,
1893
- hasError: false,
1894
- data: res_1
1895
- };
1896
- this.sendWS(ws, serverRes);
1897
- }
1898
- }
1899
- }
1900
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1901
- finally {
1902
- try {
1903
- if (_b && !_b.done && (_h = _a.return)) _h.call(_a);
1992
+ cachedBuffer = this.getCacheBuffer(this._nodeCache.get(sub.cacheId));
1993
+ isSame = this.buffersEqual(cachedBuffer, packedRes);
1994
+ if (!isSame) {
1995
+ try {
1996
+ for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
1997
+ client = _b.value;
1998
+ ws = this._websocketManager.getWebSocket(client.id_socket);
1999
+ if (ws && ws.readyState === ws.OPEN) {
2000
+ serverRes = {
2001
+ messageId: client.messageId,
2002
+ hasError: false,
2003
+ data: res
2004
+ };
2005
+ this.sendWS(ws, serverRes);
1904
2006
  }
1905
- finally { if (e_6) throw e_6.error; }
1906
- }
1907
- this._nodeCache.del(sub.cacheId);
1908
- if ((0, common_1.getBinarySize)(JSON.stringify(res_1)) < 1000000 &&
1909
- !sub.collections.includes('logs') &&
1910
- !sub.collections.find(function (a) { return a.endsWith('.versions'); }) &&
1911
- !sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
1912
- this._nodeCache.set(sub.cacheId, JSON.stringify(res_1));
1913
- }
1914
- else {
1915
- sub.cacheId = 0;
1916
2007
  }
1917
2008
  }
1918
- }
1919
- catch (_o) {
1920
- sub.clients.map(function (client) {
1921
- var ws = _this._websocketManager.getWebSocket(client.id_socket);
1922
- if (ws && ws.readyState === ws.OPEN) {
1923
- var serverRes = {
1924
- messageId: client.messageId,
1925
- hasError: false,
1926
- data: res_1
1927
- };
1928
- _this.sendWS(ws, serverRes);
2009
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2010
+ finally {
2011
+ try {
2012
+ if (_b && !_b.done && (_h = _a.return)) _h.call(_a);
1929
2013
  }
1930
- });
2014
+ finally { if (e_6) throw e_6.error; }
2015
+ }
1931
2016
  this._nodeCache.del(sub.cacheId);
1932
- if ((0, common_1.getBinarySize)(JSON.stringify(res_1)) < 1000000 &&
1933
- !sub.collections.includes('logs') &&
1934
- !sub.collections.find(function (a) { return a.endsWith('.versions'); }) &&
1935
- !sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
1936
- this._nodeCache.set(sub.cacheId, JSON.stringify(res_1));
2017
+ if (shouldCache) {
2018
+ this._nodeCache.set(sub.cacheId, packedRes);
1937
2019
  }
1938
2020
  else {
1939
2021
  sub.cacheId = 0;
1940
2022
  }
1941
2023
  }
2024
+ else if (!cachedBuffer && shouldCache) {
2025
+ this._nodeCache.set(sub.cacheId, packedRes);
2026
+ }
2027
+ else if (!shouldCache) {
2028
+ this._nodeCache.del(sub.cacheId);
2029
+ sub.cacheId = 0;
2030
+ }
1942
2031
  }
1943
2032
  else {
1944
2033
  try {
@@ -1949,7 +2038,7 @@ var SubscriptionManager = /** @class */ (function () {
1949
2038
  serverRes = {
1950
2039
  messageId: client.messageId,
1951
2040
  hasError: false,
1952
- data: res_1
2041
+ data: res
1953
2042
  };
1954
2043
  this.sendWS(ws, serverRes);
1955
2044
  }
@@ -1962,12 +2051,9 @@ var SubscriptionManager = /** @class */ (function () {
1962
2051
  }
1963
2052
  finally { if (e_7) throw e_7.error; }
1964
2053
  }
1965
- if ((0, common_1.getBinarySize)(JSON.stringify(res_1)) < 1000000 &&
1966
- !sub.collections.includes('logs') &&
1967
- !sub.collections.find(function (a) { return a.endsWith('.versions'); }) &&
1968
- !sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
2054
+ if (shouldCache) {
1969
2055
  sub.cacheId = this._cacheId++;
1970
- this._nodeCache.set(sub.cacheId, JSON.stringify(res_1));
2056
+ this._nodeCache.set(sub.cacheId, packedRes);
1971
2057
  nodeCacheSize = this._nodeCache.getStats().vsize;
1972
2058
  if (nodeCacheSize > this._heapLimit) {
1973
2059
  deleteCount = 0;