@resolveio/server-lib 20.14.32 → 20.14.34

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.
@@ -25,6 +25,7 @@ export declare class SubscriptionManager {
25
25
  private _monitorManagerFunction;
26
26
  private _enableDebug;
27
27
  private _enableDependencyDebug;
28
+ private _connectDebug;
28
29
  private _debugOplogCollections;
29
30
  private _debugOplogHits;
30
31
  private _debugSubCollections;
@@ -93,7 +94,11 @@ export declare class SubscriptionManager {
93
94
  private getWatchedDatabases;
94
95
  private resolveOplogMode;
95
96
  private resolveLocalOplogResyncIntervalMs;
97
+ private resolveResumeTokenMaxAgeMs;
98
+ private resolveConnectDebug;
99
+ private parseDebugFlag;
96
100
  private parsePositiveNumber;
101
+ private connectDebug;
97
102
  private parsePositiveFloat;
98
103
  private isChangeStreamUnsupported;
99
104
  private isResumeTokenInvalid;
@@ -134,6 +134,7 @@ var SubscriptionManager = /** @class */ (function () {
134
134
  this._heapSize = resolveHeapLimitBytes();
135
135
  this._enableDebug = false;
136
136
  this._enableDependencyDebug = false;
137
+ this._connectDebug = false;
137
138
  this._debugOplogCollections = [];
138
139
  this._debugOplogHits = 0;
139
140
  this._debugSubCollections = [];
@@ -224,6 +225,7 @@ var SubscriptionManager = /** @class */ (function () {
224
225
  // }, 10000);
225
226
  this.serverConfig = serverConfig;
226
227
  this._wss = wss;
228
+ this._connectDebug = this.resolveConnectDebug();
227
229
  this._oplogMode = this.resolveOplogMode();
228
230
  this._localOplogResyncIntervalMs = this.resolveLocalOplogResyncIntervalMs();
229
231
  this.registerCorePublications();
@@ -947,6 +949,14 @@ var SubscriptionManager = /** @class */ (function () {
947
949
  switch (_a.label) {
948
950
  case 0:
949
951
  this._debugSubHits += 1;
952
+ this.connectDebug('Subscribe request', {
953
+ publication: publication,
954
+ messageRoute: messageRoute,
955
+ messageId: messageId,
956
+ id_socket: ws ? ws['id_socket'] : null,
957
+ user: ws ? ws['user'] : null,
958
+ args: Array.isArray(subscriptionData) ? subscriptionData.length : 0
959
+ });
950
960
  if (!this._debugSubCollections.some(function (a) { return a.publication === publication; })) {
951
961
  this._debugSubCollections.push({
952
962
  publication: publication,
@@ -959,15 +969,18 @@ var SubscriptionManager = /** @class */ (function () {
959
969
  pub = this._publications[publication];
960
970
  if (!!pub) return [3 /*break*/, 1];
961
971
  console.error(new Date(), 'No Publication: ' + publication);
972
+ this.connectDebug('Missing publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
962
973
  return [2 /*return*/];
963
974
  case 1:
964
975
  if (subscriptionData.length > 1 || subscriptionData[0]) {
965
976
  if (!pub.check) {
966
977
  console.error(new Date(), 'No Check Function For Pub ' + publication);
978
+ this.connectDebug('Missing check for publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
967
979
  return [2 /*return*/];
968
980
  }
969
981
  else if (!pub.check._schema) {
970
982
  console.error(new Date(), 'No Check Schema For Pub ' + publication);
983
+ this.connectDebug('Missing check schema for publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
971
984
  return [2 /*return*/];
972
985
  }
973
986
  else {
@@ -983,6 +996,14 @@ var SubscriptionManager = /** @class */ (function () {
983
996
  catch (errors) {
984
997
  if (errors) {
985
998
  console.error(new Date(), 'Error in Pub Check (' + publication + ')', errors);
999
+ this.connectDebug('Publication check failed', {
1000
+ publication: publication,
1001
+ messageRoute: messageRoute,
1002
+ messageId: messageId,
1003
+ args: subscriptionData.length,
1004
+ valObj: valObj,
1005
+ error: (errors === null || errors === void 0 ? void 0 : errors.message) || errors
1006
+ });
986
1007
  return [2 /*return*/];
987
1008
  }
988
1009
  }
@@ -1194,6 +1215,38 @@ var SubscriptionManager = /** @class */ (function () {
1194
1215
  || config['SUBSCRIPTION_OPLOG_RESYNC_MS'];
1195
1216
  return this.parsePositiveNumber(raw);
1196
1217
  };
1218
+ SubscriptionManager.prototype.resolveResumeTokenMaxAgeMs = function () {
1219
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1220
+ var raw = process.env.OPLOG_RESUME_TOKEN_MAX_AGE_MS
1221
+ || process.env.SUBSCRIPTION_OPLOG_RESUME_TOKEN_MAX_AGE_MS
1222
+ || config['OPLOG_RESUME_TOKEN_MAX_AGE_MS']
1223
+ || config['SUBSCRIPTION_OPLOG_RESUME_TOKEN_MAX_AGE_MS'];
1224
+ return this.parsePositiveNumber(raw);
1225
+ };
1226
+ SubscriptionManager.prototype.resolveConnectDebug = function () {
1227
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1228
+ var raw = process.env.WS_CONNECT_DEBUG
1229
+ || process.env.CONNECT_DEBUG
1230
+ || config['WS_CONNECT_DEBUG']
1231
+ || config['CONNECT_DEBUG'];
1232
+ return this.parseDebugFlag(raw);
1233
+ };
1234
+ SubscriptionManager.prototype.parseDebugFlag = function (value) {
1235
+ if (value === true) {
1236
+ return true;
1237
+ }
1238
+ if (value === false || value === null || value === undefined) {
1239
+ return false;
1240
+ }
1241
+ if (typeof value === 'number') {
1242
+ return value === 1;
1243
+ }
1244
+ if (typeof value === 'string') {
1245
+ var normalized = value.trim().toLowerCase();
1246
+ return ['1', 'true', 'yes', 'y', 'on'].includes(normalized);
1247
+ }
1248
+ return false;
1249
+ };
1197
1250
  SubscriptionManager.prototype.parsePositiveNumber = function (value) {
1198
1251
  if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
1199
1252
  return value;
@@ -1206,6 +1259,17 @@ var SubscriptionManager = /** @class */ (function () {
1206
1259
  }
1207
1260
  return 0;
1208
1261
  };
1262
+ SubscriptionManager.prototype.connectDebug = function (message, details) {
1263
+ if (!this._connectDebug) {
1264
+ return;
1265
+ }
1266
+ if (details) {
1267
+ console.log(new Date(), '[Connect Debug]', message, JSON.stringify(details));
1268
+ }
1269
+ else {
1270
+ console.log(new Date(), '[Connect Debug]', message);
1271
+ }
1272
+ };
1209
1273
  SubscriptionManager.prototype.parsePositiveFloat = function (value) {
1210
1274
  if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
1211
1275
  return value;
@@ -1337,31 +1401,66 @@ var SubscriptionManager = /** @class */ (function () {
1337
1401
  };
1338
1402
  SubscriptionManager.prototype.loadResumeToken = function () {
1339
1403
  return __awaiter(this, void 0, void 0, function () {
1340
- var db, doc, error_3;
1341
- return __generator(this, function (_a) {
1342
- switch (_a.label) {
1404
+ var db_1, loadToken, mongoManager, _a, error_3;
1405
+ var _this = this;
1406
+ return __generator(this, function (_b) {
1407
+ switch (_b.label) {
1343
1408
  case 0:
1344
- _a.trys.push([0, 2, , 3]);
1345
- db = resolveio_server_app_1.ResolveIOServer.getMainDB();
1346
- if (!db) {
1409
+ _b.trys.push([0, 5, , 6]);
1410
+ db_1 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1411
+ if (!db_1) {
1347
1412
  return [2 /*return*/, null];
1348
1413
  }
1349
- return [4 /*yield*/, db.collection(this.RESUME_TOKEN_COLLECTION).findOne({ _id: this.getResumeTokenDocId() })];
1414
+ loadToken = function () { return __awaiter(_this, void 0, void 0, function () {
1415
+ var collection, doc, token, maxAgeMs, ageMs;
1416
+ return __generator(this, function (_a) {
1417
+ switch (_a.label) {
1418
+ case 0:
1419
+ collection = db_1.collection(this.RESUME_TOKEN_COLLECTION);
1420
+ return [4 /*yield*/, collection.findOne({ _id: this.getResumeTokenDocId() })];
1421
+ case 1:
1422
+ doc = _a.sent();
1423
+ token = (doc === null || doc === void 0 ? void 0 : doc.token) || null;
1424
+ if (!token) {
1425
+ return [2 /*return*/, null];
1426
+ }
1427
+ maxAgeMs = this.resolveResumeTokenMaxAgeMs();
1428
+ if (!(maxAgeMs > 0 && (doc === null || doc === void 0 ? void 0 : doc.updatedAt) instanceof Date)) return [3 /*break*/, 3];
1429
+ ageMs = Date.now() - doc.updatedAt.getTime();
1430
+ if (!(ageMs > maxAgeMs)) return [3 /*break*/, 3];
1431
+ return [4 /*yield*/, collection.deleteOne({ _id: this.getResumeTokenDocId() })];
1432
+ case 2:
1433
+ _a.sent();
1434
+ console.log(new Date(), 'Sub Manager', 'Resume token expired, clearing', (0, common_1.round)(ageMs / 1000), 's');
1435
+ return [2 /*return*/, null];
1436
+ case 3: return [2 /*return*/, token];
1437
+ }
1438
+ });
1439
+ }); };
1440
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1441
+ if (!mongoManager) return [3 /*break*/, 2];
1442
+ return [4 /*yield*/, mongoManager.runWithoutSession(loadToken)];
1350
1443
  case 1:
1351
- doc = _a.sent();
1352
- return [2 /*return*/, (doc === null || doc === void 0 ? void 0 : doc.token) || null];
1353
- case 2:
1354
- error_3 = _a.sent();
1444
+ _a = _b.sent();
1445
+ return [3 /*break*/, 4];
1446
+ case 2: return [4 /*yield*/, loadToken()];
1447
+ case 3:
1448
+ _a = _b.sent();
1449
+ _b.label = 4;
1450
+ case 4: return [2 /*return*/, _a];
1451
+ case 5:
1452
+ error_3 = _b.sent();
1355
1453
  console.log(new Date(), 'Sub Manager', 'Failed to load oplog resume token', error_3);
1356
1454
  return [2 /*return*/, null];
1357
- case 3: return [2 /*return*/];
1455
+ case 6: return [2 /*return*/];
1358
1456
  }
1359
1457
  });
1360
1458
  });
1361
1459
  };
1362
1460
  SubscriptionManager.prototype.saveResumeToken = function (token) {
1363
1461
  return __awaiter(this, void 0, void 0, function () {
1364
- var db, error_4;
1462
+ var db_2, saveToken, mongoManager, error_4;
1463
+ var _this = this;
1365
1464
  return __generator(this, function (_a) {
1366
1465
  switch (_a.label) {
1367
1466
  case 0:
@@ -1370,44 +1469,80 @@ var SubscriptionManager = /** @class */ (function () {
1370
1469
  }
1371
1470
  _a.label = 1;
1372
1471
  case 1:
1373
- _a.trys.push([1, 3, , 4]);
1374
- db = resolveio_server_app_1.ResolveIOServer.getMainDB();
1375
- if (!db) {
1472
+ _a.trys.push([1, 6, , 7]);
1473
+ db_2 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1474
+ if (!db_2) {
1376
1475
  return [2 /*return*/];
1377
1476
  }
1378
- return [4 /*yield*/, db.collection(this.RESUME_TOKEN_COLLECTION).updateOne({ _id: this.getResumeTokenDocId() }, { $set: { token: token, updatedAt: new Date() } }, { upsert: true })];
1477
+ saveToken = function () { return __awaiter(_this, void 0, void 0, function () {
1478
+ return __generator(this, function (_a) {
1479
+ switch (_a.label) {
1480
+ case 0: return [4 /*yield*/, db_2.collection(this.RESUME_TOKEN_COLLECTION).updateOne({ _id: this.getResumeTokenDocId() }, { $set: { token: token, updatedAt: new Date() } }, { upsert: true })];
1481
+ case 1:
1482
+ _a.sent();
1483
+ return [2 /*return*/];
1484
+ }
1485
+ });
1486
+ }); };
1487
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1488
+ if (!mongoManager) return [3 /*break*/, 3];
1489
+ return [4 /*yield*/, mongoManager.runWithoutSession(saveToken)];
1379
1490
  case 2:
1380
1491
  _a.sent();
1381
- return [3 /*break*/, 4];
1382
- case 3:
1492
+ return [3 /*break*/, 5];
1493
+ case 3: return [4 /*yield*/, saveToken()];
1494
+ case 4:
1495
+ _a.sent();
1496
+ _a.label = 5;
1497
+ case 5: return [3 /*break*/, 7];
1498
+ case 6:
1383
1499
  error_4 = _a.sent();
1384
1500
  console.log(new Date(), 'Sub Manager', 'Failed to persist oplog resume token', error_4);
1385
- return [3 /*break*/, 4];
1386
- case 4: return [2 /*return*/];
1501
+ return [3 /*break*/, 7];
1502
+ case 7: return [2 /*return*/];
1387
1503
  }
1388
1504
  });
1389
1505
  });
1390
1506
  };
1391
1507
  SubscriptionManager.prototype.clearResumeToken = function () {
1392
1508
  return __awaiter(this, void 0, void 0, function () {
1393
- var db, error_5;
1509
+ var db_3, clearToken, mongoManager, error_5;
1510
+ var _this = this;
1394
1511
  return __generator(this, function (_a) {
1395
1512
  switch (_a.label) {
1396
1513
  case 0:
1397
- _a.trys.push([0, 2, , 3]);
1398
- db = resolveio_server_app_1.ResolveIOServer.getMainDB();
1399
- if (!db) {
1514
+ _a.trys.push([0, 5, , 6]);
1515
+ db_3 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1516
+ if (!db_3) {
1400
1517
  return [2 /*return*/];
1401
1518
  }
1402
- return [4 /*yield*/, db.collection(this.RESUME_TOKEN_COLLECTION).deleteOne({ _id: this.getResumeTokenDocId() })];
1519
+ clearToken = function () { return __awaiter(_this, void 0, void 0, function () {
1520
+ return __generator(this, function (_a) {
1521
+ switch (_a.label) {
1522
+ case 0: return [4 /*yield*/, db_3.collection(this.RESUME_TOKEN_COLLECTION)
1523
+ .deleteOne({ _id: this.getResumeTokenDocId() })];
1524
+ case 1:
1525
+ _a.sent();
1526
+ return [2 /*return*/];
1527
+ }
1528
+ });
1529
+ }); };
1530
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1531
+ if (!mongoManager) return [3 /*break*/, 2];
1532
+ return [4 /*yield*/, mongoManager.runWithoutSession(clearToken)];
1403
1533
  case 1:
1404
1534
  _a.sent();
1405
- return [3 /*break*/, 3];
1406
- case 2:
1535
+ return [3 /*break*/, 4];
1536
+ case 2: return [4 /*yield*/, clearToken()];
1537
+ case 3:
1538
+ _a.sent();
1539
+ _a.label = 4;
1540
+ case 4: return [3 /*break*/, 6];
1541
+ case 5:
1407
1542
  error_5 = _a.sent();
1408
1543
  console.log(new Date(), 'Sub Manager', 'Failed to clear oplog resume token', error_5);
1409
- return [3 /*break*/, 3];
1410
- case 3: return [2 /*return*/];
1544
+ return [3 /*break*/, 6];
1545
+ case 6: return [2 /*return*/];
1411
1546
  }
1412
1547
  });
1413
1548
  });