@resolveio/server-lib 20.14.39 → 20.14.41

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.
@@ -2150,12 +2150,16 @@ var SubscriptionManager = /** @class */ (function () {
2150
2150
  if (a.byteLength !== b.byteLength) {
2151
2151
  return false;
2152
2152
  }
2153
- for (var index = 0; index < a.byteLength; index++) {
2154
- if (a[index] !== b[index]) {
2155
- return false;
2153
+ var toBuffer = function (value) {
2154
+ if (Buffer.isBuffer(value)) {
2155
+ return value;
2156
2156
  }
2157
- }
2158
- return true;
2157
+ var buffer = value.buffer;
2158
+ return Buffer.from(buffer, value.byteOffset, value.byteLength);
2159
+ };
2160
+ var aBuf = toBuffer(a);
2161
+ var bBuf = toBuffer(b);
2162
+ return aBuf.equals(bBuf);
2159
2163
  };
2160
2164
  SubscriptionManager.prototype.processSubscription = function (sub, ws, messageId) {
2161
2165
  return __awaiter(this, void 0, void 0, function () {
@@ -2236,6 +2240,9 @@ var SubscriptionManager = /** @class */ (function () {
2236
2240
  return __generator(this, function (_c) {
2237
2241
  switch (_c.label) {
2238
2242
  case 0:
2243
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
2244
+ return [2 /*return*/];
2245
+ }
2239
2246
  monitor = this._monitorManagerFunction.startMonitorFunction('User Specific Publication', sub.publication, '', '', sub.subscriptionData);
2240
2247
  _c.label = 1;
2241
2248
  case 1:
@@ -2313,13 +2320,32 @@ var SubscriptionManager = /** @class */ (function () {
2313
2320
  // Fetch pub once, send to all clients linked to this pub
2314
2321
  SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
2315
2322
  return __awaiter(this, void 0, void 0, function () {
2316
- var subIndex, monitor, res, dependencySnapshot, execution, packedRes, payloadBytes, shouldCache, cachedBuffer, isSame, sendStartMs, sentClients, _a, _b, client, ws, serverRes, sendStartMs, sentClients, _c, _d, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _e, normalizedError, correlationId, _f, _g, client, ws, serverRes, errorPayload;
2317
- var e_6, _h, e_7, _j, e_8, _k;
2318
- var _l, _m;
2319
- return __generator(this, function (_o) {
2320
- switch (_o.label) {
2323
+ var activeClients, _a, _b, client, ws, subIndex, monitor, res, dependencySnapshot, execution, packedRes, payloadBytes, shouldCache, cachedBuffer, isSame, sendStartMs, sentClients, activeClients_1, activeClients_1_1, entry, client, ws, serverRes, sendStartMs, sentClients, activeClients_2, activeClients_2_1, entry, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _c, normalizedError, correlationId, _d, _e, client, ws, serverRes, errorPayload;
2324
+ var e_6, _f, e_7, _g, e_8, _h, e_9, _j;
2325
+ var _k, _l;
2326
+ return __generator(this, function (_m) {
2327
+ switch (_m.label) {
2321
2328
  case 0:
2322
- if (!!sub.clients.length) return [3 /*break*/, 1];
2329
+ activeClients = [];
2330
+ if (sub.clients.length) {
2331
+ try {
2332
+ for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2333
+ client = _b.value;
2334
+ ws = this._websocketManager.getWebSocket(client.id_socket);
2335
+ if (ws && ws.readyState === WebSocket.OPEN) {
2336
+ activeClients.push({ client: client, ws: ws });
2337
+ }
2338
+ }
2339
+ }
2340
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2341
+ finally {
2342
+ try {
2343
+ if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
2344
+ }
2345
+ finally { if (e_6) throw e_6.error; }
2346
+ }
2347
+ }
2348
+ if (!!activeClients.length) return [3 /*break*/, 1];
2323
2349
  if (sub.cacheId) {
2324
2350
  this._nodeCache.del(sub.cacheId);
2325
2351
  sub.cacheId = 0;
@@ -2330,22 +2356,25 @@ var SubscriptionManager = /** @class */ (function () {
2330
2356
  }
2331
2357
  return [2 /*return*/];
2332
2358
  case 1:
2359
+ if (activeClients.length !== sub.clients.length) {
2360
+ sub.clients = activeClients.map(function (entry) { return entry.client; });
2361
+ }
2333
2362
  monitor = this._monitorManagerFunction.startMonitorFunction('Publication', sub.publication, '', '', sub.subscriptionData);
2334
2363
  res = void 0;
2335
2364
  dependencySnapshot = void 0;
2336
- _o.label = 2;
2365
+ _m.label = 2;
2337
2366
  case 2:
2338
- _o.trys.push([2, 4, 6, 8]);
2367
+ _m.trys.push([2, 4, 6, 8]);
2339
2368
  if (sub.publication !== 'superadminAPM' && sub.publication !== 'loggedInUsers') {
2340
2369
  resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertSubscriptionLog', type, sub.publication, collection, JSON.stringify(sub.subscriptionData));
2341
2370
  }
2342
2371
  return [4 /*yield*/, this.runPublicationExecution(sub)];
2343
2372
  case 3:
2344
- execution = _o.sent();
2373
+ execution = _m.sent();
2345
2374
  res = execution.result;
2346
2375
  dependencySnapshot = execution.snapshot;
2347
2376
  this.updateSubscriptionDependencies(sub, dependencySnapshot);
2348
- packedRes = (_l = execution.packedResult) !== null && _l !== void 0 ? _l : this.packCachePayload(res);
2377
+ packedRes = (_k = execution.packedResult) !== null && _k !== void 0 ? _k : this.packCachePayload(res);
2349
2378
  payloadBytes = packedRes ? packedRes.byteLength : null;
2350
2379
  shouldCache = this.shouldCachePayload(sub, packedRes);
2351
2380
  if (sub.cacheId) {
@@ -2355,31 +2384,29 @@ var SubscriptionManager = /** @class */ (function () {
2355
2384
  sendStartMs = Date.now();
2356
2385
  sentClients = 0;
2357
2386
  try {
2358
- for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2359
- client = _b.value;
2360
- ws = this._websocketManager.getWebSocket(client.id_socket);
2361
- if (ws && ws.readyState === WebSocket.OPEN) {
2362
- if (packedRes) {
2363
- this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2364
- }
2365
- else {
2366
- serverRes = {
2367
- messageId: client.messageId,
2368
- hasError: false,
2369
- data: res
2370
- };
2371
- this.sendWS(ws, serverRes);
2372
- }
2373
- sentClients += 1;
2387
+ for (activeClients_1 = __values(activeClients), activeClients_1_1 = activeClients_1.next(); !activeClients_1_1.done; activeClients_1_1 = activeClients_1.next()) {
2388
+ entry = activeClients_1_1.value;
2389
+ client = entry.client, ws = entry.ws;
2390
+ if (packedRes) {
2391
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2374
2392
  }
2393
+ else {
2394
+ serverRes = {
2395
+ messageId: client.messageId,
2396
+ hasError: false,
2397
+ data: res
2398
+ };
2399
+ this.sendWS(ws, serverRes);
2400
+ }
2401
+ sentClients += 1;
2375
2402
  }
2376
2403
  }
2377
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
2404
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2378
2405
  finally {
2379
2406
  try {
2380
- if (_b && !_b.done && (_h = _a.return)) _h.call(_a);
2407
+ if (activeClients_1_1 && !activeClients_1_1.done && (_g = activeClients_1.return)) _g.call(activeClients_1);
2381
2408
  }
2382
- finally { if (e_6) throw e_6.error; }
2409
+ finally { if (e_7) throw e_7.error; }
2383
2410
  }
2384
2411
  this.logSubSend(sub, {
2385
2412
  collection: collection,
@@ -2408,31 +2435,29 @@ var SubscriptionManager = /** @class */ (function () {
2408
2435
  sendStartMs = Date.now();
2409
2436
  sentClients = 0;
2410
2437
  try {
2411
- for (_c = __values(sub.clients), _d = _c.next(); !_d.done; _d = _c.next()) {
2412
- client = _d.value;
2413
- ws = this._websocketManager.getWebSocket(client.id_socket);
2414
- if (ws && ws.readyState === WebSocket.OPEN) {
2415
- if (packedRes) {
2416
- this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2417
- }
2418
- else {
2419
- serverRes = {
2420
- messageId: client.messageId,
2421
- hasError: false,
2422
- data: res
2423
- };
2424
- this.sendWS(ws, serverRes);
2425
- }
2426
- sentClients += 1;
2438
+ for (activeClients_2 = __values(activeClients), activeClients_2_1 = activeClients_2.next(); !activeClients_2_1.done; activeClients_2_1 = activeClients_2.next()) {
2439
+ entry = activeClients_2_1.value;
2440
+ client = entry.client, ws = entry.ws;
2441
+ if (packedRes) {
2442
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack');
2443
+ }
2444
+ else {
2445
+ serverRes = {
2446
+ messageId: client.messageId,
2447
+ hasError: false,
2448
+ data: res
2449
+ };
2450
+ this.sendWS(ws, serverRes);
2427
2451
  }
2452
+ sentClients += 1;
2428
2453
  }
2429
2454
  }
2430
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
2455
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
2431
2456
  finally {
2432
2457
  try {
2433
- if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
2458
+ if (activeClients_2_1 && !activeClients_2_1.done && (_h = activeClients_2.return)) _h.call(activeClients_2);
2434
2459
  }
2435
- finally { if (e_7) throw e_7.error; }
2460
+ finally { if (e_8) throw e_8.error; }
2436
2461
  }
2437
2462
  this.logSubSend(sub, {
2438
2463
  collection: collection,
@@ -2474,11 +2499,11 @@ var SubscriptionManager = /** @class */ (function () {
2474
2499
  }
2475
2500
  return [3 /*break*/, 8];
2476
2501
  case 4:
2477
- err_2 = _o.sent();
2478
- _e = (0, error_tracking_1.ensureErrorWithCorrelation)(err_2), normalizedError = _e.error, correlationId = _e.correlationId;
2502
+ err_2 = _m.sent();
2503
+ _c = (0, error_tracking_1.ensureErrorWithCorrelation)(err_2), normalizedError = _c.error, correlationId = _c.correlationId;
2479
2504
  try {
2480
- for (_f = __values(sub.clients), _g = _f.next(); !_g.done; _g = _f.next()) {
2481
- client = _g.value;
2505
+ for (_d = __values(sub.clients), _e = _d.next(); !_e.done; _e = _d.next()) {
2506
+ client = _e.value;
2482
2507
  ws = this._websocketManager.getWebSocket(client.id_socket);
2483
2508
  if (ws && ws.readyState === WebSocket.OPEN) {
2484
2509
  serverRes = {
@@ -2490,12 +2515,12 @@ var SubscriptionManager = /** @class */ (function () {
2490
2515
  }
2491
2516
  }
2492
2517
  }
2493
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
2518
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
2494
2519
  finally {
2495
2520
  try {
2496
- if (_g && !_g.done && (_k = _f.return)) _k.call(_f);
2521
+ if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
2497
2522
  }
2498
- finally { if (e_8) throw e_8.error; }
2523
+ finally { if (e_9) throw e_9.error; }
2499
2524
  }
2500
2525
  errorPayload = {
2501
2526
  publication: sub.publication,
@@ -2514,7 +2539,7 @@ var SubscriptionManager = /** @class */ (function () {
2514
2539
  return [4 /*yield*/, error_reporter_1.ErrorReporter.report({
2515
2540
  sourceApp: 'subscription-manager',
2516
2541
  message: 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'],
2517
- environment: (_m = this.serverConfig) === null || _m === void 0 ? void 0 : _m.ROOT_URL,
2542
+ environment: (_l = this.serverConfig) === null || _l === void 0 ? void 0 : _l.ROOT_URL,
2518
2543
  clientSlug: resolveio_server_app_1.ResolveIOServer.getClientName(),
2519
2544
  clientName: this.serverConfig['CLIENT_NAME'],
2520
2545
  stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
@@ -2527,11 +2552,11 @@ var SubscriptionManager = /** @class */ (function () {
2527
2552
  correlationId: correlationId
2528
2553
  })];
2529
2554
  case 5:
2530
- _o.sent();
2555
+ _m.sent();
2531
2556
  return [3 /*break*/, 8];
2532
2557
  case 6: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2533
2558
  case 7:
2534
- _o.sent();
2559
+ _m.sent();
2535
2560
  return [7 /*endfinally*/];
2536
2561
  case 8: return [2 /*return*/];
2537
2562
  }
@@ -2656,7 +2681,7 @@ var SubscriptionManager = /** @class */ (function () {
2656
2681
  '$bitsAnyClear'
2657
2682
  ]);
2658
2683
  var walk = function (value) {
2659
- var e_9, _a;
2684
+ var e_10, _a;
2660
2685
  if (Array.isArray(value)) {
2661
2686
  return value.every(function (item) { return walk(item); });
2662
2687
  }
@@ -2679,19 +2704,19 @@ var SubscriptionManager = /** @class */ (function () {
2679
2704
  }
2680
2705
  }
2681
2706
  }
2682
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
2707
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
2683
2708
  finally {
2684
2709
  try {
2685
2710
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2686
2711
  }
2687
- finally { if (e_9) throw e_9.error; }
2712
+ finally { if (e_10) throw e_10.error; }
2688
2713
  }
2689
2714
  return true;
2690
2715
  };
2691
2716
  return walk(filter);
2692
2717
  };
2693
2718
  SubscriptionManager.prototype.documentIdMatchesEvent = function (documentId, document) {
2694
- var e_10, _a;
2719
+ var e_11, _a;
2695
2720
  var _this = this;
2696
2721
  if (!document || documentId === undefined || documentId === null) {
2697
2722
  return true;
@@ -2714,12 +2739,12 @@ var SubscriptionManager = /** @class */ (function () {
2714
2739
  }
2715
2740
  }
2716
2741
  }
2717
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
2742
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
2718
2743
  finally {
2719
2744
  try {
2720
2745
  if (eventCandidates_1_1 && !eventCandidates_1_1.done && (_a = eventCandidates_1.return)) _a.call(eventCandidates_1);
2721
2746
  }
2722
- finally { if (e_10) throw e_10.error; }
2747
+ finally { if (e_11) throw e_11.error; }
2723
2748
  }
2724
2749
  return false;
2725
2750
  };
@@ -2743,8 +2768,8 @@ var SubscriptionManager = /** @class */ (function () {
2743
2768
  };
2744
2769
  SubscriptionManager.prototype.documentMatchesFilter = function (collection, documentId, filter, document) {
2745
2770
  return __awaiter(this, void 0, void 0, function () {
2746
- var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_11_1, _a;
2747
- var e_11, _b;
2771
+ var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_12_1, _a;
2772
+ var e_12, _b;
2748
2773
  return __generator(this, function (_c) {
2749
2774
  switch (_c.label) {
2750
2775
  case 0:
@@ -2782,14 +2807,14 @@ var SubscriptionManager = /** @class */ (function () {
2782
2807
  return [3 /*break*/, 2];
2783
2808
  case 5: return [3 /*break*/, 8];
2784
2809
  case 6:
2785
- e_11_1 = _c.sent();
2786
- e_11 = { error: e_11_1 };
2810
+ e_12_1 = _c.sent();
2811
+ e_12 = { error: e_12_1 };
2787
2812
  return [3 /*break*/, 8];
2788
2813
  case 7:
2789
2814
  try {
2790
2815
  if (idCandidates_1_1 && !idCandidates_1_1.done && (_b = idCandidates_1.return)) _b.call(idCandidates_1);
2791
2816
  }
2792
- finally { if (e_11) throw e_11.error; }
2817
+ finally { if (e_12) throw e_12.error; }
2793
2818
  return [7 /*endfinally*/];
2794
2819
  case 8: return [2 /*return*/, false];
2795
2820
  case 9:
@@ -2802,8 +2827,8 @@ var SubscriptionManager = /** @class */ (function () {
2802
2827
  };
2803
2828
  SubscriptionManager.prototype.shouldInvalidateSubscription = function (sub, collection, type, documentId, document) {
2804
2829
  return __awaiter(this, void 0, void 0, function () {
2805
- var normalizedDocumentId, hasDependencyData, trackedIds, filters, filters_1, filters_1_1, filter, e_12_1;
2806
- var e_12, _a;
2830
+ var normalizedDocumentId, hasDependencyData, trackedIds, filters, filters_1, filters_1_1, filter, e_13_1;
2831
+ var e_13, _a;
2807
2832
  var _b, _c;
2808
2833
  return __generator(this, function (_d) {
2809
2834
  switch (_d.label) {
@@ -2860,14 +2885,14 @@ var SubscriptionManager = /** @class */ (function () {
2860
2885
  return [3 /*break*/, 2];
2861
2886
  case 5: return [3 /*break*/, 8];
2862
2887
  case 6:
2863
- e_12_1 = _d.sent();
2864
- e_12 = { error: e_12_1 };
2888
+ e_13_1 = _d.sent();
2889
+ e_13 = { error: e_13_1 };
2865
2890
  return [3 /*break*/, 8];
2866
2891
  case 7:
2867
2892
  try {
2868
2893
  if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
2869
2894
  }
2870
- finally { if (e_12) throw e_12.error; }
2895
+ finally { if (e_13) throw e_13.error; }
2871
2896
  return [7 /*endfinally*/];
2872
2897
  case 8:
2873
2898
  this.dependencyDebug('Skip invalidation after dependency checks', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
@@ -2878,8 +2903,8 @@ var SubscriptionManager = /** @class */ (function () {
2878
2903
  };
2879
2904
  SubscriptionManager.prototype.shouldInvalidateSubscriptionForEvents = function (sub, collection, events) {
2880
2905
  return __awaiter(this, void 0, void 0, function () {
2881
- var sawInsert, sawDelete, events_1, events_1_1, event_1, e_13_1, paginationMeta, paginationReasons;
2882
- var e_13, _a;
2906
+ var sawInsert, sawDelete, events_1, events_1_1, event_1, e_14_1, paginationMeta, paginationReasons;
2907
+ var e_14, _a;
2883
2908
  return __generator(this, function (_b) {
2884
2909
  switch (_b.label) {
2885
2910
  case 0:
@@ -2910,14 +2935,14 @@ var SubscriptionManager = /** @class */ (function () {
2910
2935
  return [3 /*break*/, 2];
2911
2936
  case 5: return [3 /*break*/, 8];
2912
2937
  case 6:
2913
- e_13_1 = _b.sent();
2914
- e_13 = { error: e_13_1 };
2938
+ e_14_1 = _b.sent();
2939
+ e_14 = { error: e_14_1 };
2915
2940
  return [3 /*break*/, 8];
2916
2941
  case 7:
2917
2942
  try {
2918
2943
  if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
2919
2944
  }
2920
- finally { if (e_13) throw e_13.error; }
2945
+ finally { if (e_14) throw e_14.error; }
2921
2946
  return [7 /*endfinally*/];
2922
2947
  case 8:
2923
2948
  paginationMeta = this.getPaginationMeta(sub, collection);