@resolveio/server-lib 9.7.13 → 9.7.15

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.
@@ -23,7 +23,7 @@ export declare class SubscriptionManager {
23
23
  publications(method: SubscriptionModel): void;
24
24
  loggedInLatency(ws: WebSocket): Promise<void>;
25
25
  subscribe(messageDate: Date, ws: WebSocket, messageId: number, publication: string, ...subscriptionData: any[]): Promise<void>;
26
- ensureLoggedInUser(id_ws: string): Promise<LoggedInUserModel>;
26
+ createLoggedInUser(id_ws: string): Promise<LoggedInUserModel>;
27
27
  unsubscribe(messageDate: Date, ws: WebSocket, messageId: number, publication: string, ...subscriptionData: any[]): Promise<void>;
28
28
  unsubscribeAll(ws: WebSocket): Promise<void>;
29
29
  private getPublicationCollections;
@@ -225,7 +225,7 @@ var SubscriptionManager = /** @class */ (function () {
225
225
  userCopy = common_1.deepCopy(this._loggedInUsers);
226
226
  _loop_2 = function (i) {
227
227
  var loggedInUser = userCopy[i];
228
- if (!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 90) {
228
+ if (!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 30) {
229
229
  if (this_2._mainServer.getWS(loggedInUser.id_ws)) {
230
230
  this_2.unsubscribeAll(this_2._mainServer.getWS(loggedInUser.id_ws));
231
231
  }
@@ -362,7 +362,7 @@ var SubscriptionManager = /** @class */ (function () {
362
362
  loggedInUser = this._loggedInUsers.filter(function (a) { return a.id_ws === ws['id_socket']; })[0];
363
363
  if (loggedInUser) {
364
364
  loggedInUser.date = new Date();
365
- logged_in_users_collection_1.LoggedInUsers.findOneAndUpdate({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'], date: loggedInUser.date } }).then(function (res) {
365
+ logged_in_users_collection_1.LoggedInUsers.updateOne({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'], date: loggedInUser.date } }).then(function (res) {
366
366
  if (!res) {
367
367
  _this.unsubscribeAll(ws);
368
368
  }
@@ -383,132 +383,126 @@ var SubscriptionManager = /** @class */ (function () {
383
383
  return __awaiter(this, void 0, void 0, function () {
384
384
  var pub, valObj, valKeys, rootKeys, i, sub_1, newLoggedInUser;
385
385
  return __generator(this, function (_a) {
386
- switch (_a.label) {
387
- case 0:
388
- pub = this._publications[publication];
389
- if (!pub) {
390
- console.error(new Date(), 'No Publication: ' + publication);
386
+ pub = this._publications[publication];
387
+ if (!pub) {
388
+ console.error(new Date(), 'No Publication: ' + publication);
389
+ return [2 /*return*/];
390
+ }
391
+ else {
392
+ if (subscriptionData.length > 1 || subscriptionData[0]) {
393
+ if (!pub.check) {
394
+ console.error(new Date(), 'No Check Function For Pub ' + publication);
395
+ return [2 /*return*/];
396
+ }
397
+ else if (!pub.check._schema) {
398
+ console.error(new Date(), 'No Check Schema For Pub ' + publication);
391
399
  return [2 /*return*/];
392
400
  }
393
401
  else {
394
- if (subscriptionData.length > 1 || subscriptionData[0]) {
395
- if (!pub.check) {
396
- console.error(new Date(), 'No Check Function For Pub ' + publication);
397
- return [2 /*return*/];
398
- }
399
- else if (!pub.check._schema) {
400
- console.error(new Date(), 'No Check Schema For Pub ' + publication);
401
- return [2 /*return*/];
402
- }
403
- else {
404
- valObj = {};
405
- valKeys = Object.keys(pub.check._schema);
406
- rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
407
- for (i = 0; i < subscriptionData.length; i++) {
408
- valObj[rootKeys[i]] = subscriptionData[i];
409
- }
410
- try {
411
- pub.check.validate(valObj);
412
- }
413
- catch (errors) {
414
- if (errors) {
415
- console.error(new Date(), 'Error in Pub Check (' + publication + ')', errors);
416
- return [2 /*return*/];
417
- }
418
- }
419
- }
420
- }
421
- sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
422
- // If sub found (another user watching same data), add client to same sub
423
- if (sub_1) {
424
- if (!sub_1.clients.some(function (a) { return a.id_socket === ws['id_socket'] && a.messageId === messageId; })) {
425
- sub_1.clients.push({
426
- id_user: ws['id_user'],
427
- messageId: messageId,
428
- id_socket: ws['id_socket']
429
- });
430
- }
431
- }
432
- // If sub not found, create new sub
433
- else {
434
- this._subscriptions.push({
435
- publication: publication,
436
- subscriptionData: subscriptionData,
437
- collections: this.getPublicationCollections(publication),
438
- clients: [{
439
- id_user: ws['id_user'],
440
- messageId: messageId,
441
- id_socket: ws['id_socket']
442
- }],
443
- cacheId: 0
444
- });
445
- }
446
- if (!sub_1) {
447
- sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
402
+ valObj = {};
403
+ valKeys = Object.keys(pub.check._schema);
404
+ rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
405
+ for (i = 0; i < subscriptionData.length; i++) {
406
+ valObj[rootKeys[i]] = subscriptionData[i];
448
407
  }
449
- if (!sub_1.cacheId && !this._mongoQueue.some(function (a) { return a.subscription.publication === sub_1.publication && JSON.stringify(a.subscription.subscriptionData) === JSON.stringify(sub_1.subscriptionData); })) {
450
- this.addToQueue({
451
- _id: 0,
452
- type: 'newSub',
453
- collection: '',
454
- subscription: sub_1,
455
- running: false,
456
- run_again: false
457
- });
458
- // let endDate = new Date();
459
- // this.currentPerfomanceMonitor.push({
460
- // _id: this.idPerformance++,
461
- // function: 'subscribe',
462
- // publication: publication,
463
- // // subscriptionData: subscriptionData,
464
- // subscriptionData: [],
465
- // date_start: startDate,
466
- // date_end: endDate,
467
- // duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
468
- // result: 'Add to Queue'
469
- // });
408
+ try {
409
+ pub.check.validate(valObj);
470
410
  }
471
- else if (sub_1.cacheId) {
472
- this.sendPubDataOnce(ws, messageId, sub_1, 'newSub', publication);
473
- // let endDate = new Date();
474
- // this.currentPerfomanceMonitor.push({
475
- // _id: this.idPerformance++,
476
- // function: 'subscribe',
477
- // publication: publication,
478
- // // subscriptionData: subscriptionData,
479
- // subscriptionData: [],
480
- // date_start: startDate,
481
- // date_end: endDate,
482
- // duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
483
- // result: 'Send Pub Data Once'
484
- // });
411
+ catch (errors) {
412
+ if (errors) {
413
+ console.error(new Date(), 'Error in Pub Check (' + publication + ')', errors);
414
+ return [2 /*return*/];
415
+ }
485
416
  }
486
417
  }
487
- if (!(publication === 'appversion')) return [3 /*break*/, 2];
488
- return [4 /*yield*/, this.ensureLoggedInUser(ws['id_socket'])];
489
- case 1:
490
- newLoggedInUser = _a.sent();
491
- if (newLoggedInUser) {
492
- this._loggedInUsers.push(newLoggedInUser);
493
- if (ws['user'] !== 'Admin' && this.serverConfig['CLIENT_NAME'] !== 'ResolveIO') {
494
- this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.getMethodManager(), 'supportCreateBillingUser', {
495
- id_user: ws['id_user'],
496
- user: ws['user'],
497
- date: new Date(),
498
- client: index_1.ResolveIOServer.getClientName()
499
- });
500
- }
418
+ }
419
+ sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
420
+ // If sub found (another user watching same data), add client to same sub
421
+ if (sub_1) {
422
+ if (!sub_1.clients.some(function (a) { return a.id_socket === ws['id_socket'] && a.messageId === messageId; })) {
423
+ sub_1.clients.push({
424
+ id_user: ws['id_user'],
425
+ messageId: messageId,
426
+ id_socket: ws['id_socket']
427
+ });
501
428
  }
502
- else {
503
- this.unsubscribeAll(ws);
429
+ }
430
+ // If sub not found, create new sub
431
+ else {
432
+ this._subscriptions.push({
433
+ publication: publication,
434
+ subscriptionData: subscriptionData,
435
+ collections: this.getPublicationCollections(publication),
436
+ clients: [{
437
+ id_user: ws['id_user'],
438
+ messageId: messageId,
439
+ id_socket: ws['id_socket']
440
+ }],
441
+ cacheId: 0
442
+ });
443
+ }
444
+ if (!sub_1) {
445
+ sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
446
+ }
447
+ if (!sub_1.cacheId && !this._mongoQueue.some(function (a) { return a.subscription.publication === sub_1.publication && JSON.stringify(a.subscription.subscriptionData) === JSON.stringify(sub_1.subscriptionData); })) {
448
+ this.addToQueue({
449
+ _id: 0,
450
+ type: 'newSub',
451
+ collection: '',
452
+ subscription: sub_1,
453
+ running: false,
454
+ run_again: false
455
+ });
456
+ // let endDate = new Date();
457
+ // this.currentPerfomanceMonitor.push({
458
+ // _id: this.idPerformance++,
459
+ // function: 'subscribe',
460
+ // publication: publication,
461
+ // // subscriptionData: subscriptionData,
462
+ // subscriptionData: [],
463
+ // date_start: startDate,
464
+ // date_end: endDate,
465
+ // duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
466
+ // result: 'Add to Queue'
467
+ // });
468
+ }
469
+ else if (sub_1.cacheId) {
470
+ this.sendPubDataOnce(ws, messageId, sub_1, 'newSub', publication);
471
+ // let endDate = new Date();
472
+ // this.currentPerfomanceMonitor.push({
473
+ // _id: this.idPerformance++,
474
+ // function: 'subscribe',
475
+ // publication: publication,
476
+ // // subscriptionData: subscriptionData,
477
+ // subscriptionData: [],
478
+ // date_start: startDate,
479
+ // date_end: endDate,
480
+ // duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
481
+ // result: 'Send Pub Data Once'
482
+ // });
483
+ }
484
+ }
485
+ if (publication === 'appversion') {
486
+ newLoggedInUser = this._loggedInUsers.filter(function (a) { return a.id_ws === ws['id_socket']; })[0];
487
+ if (newLoggedInUser) {
488
+ if (ws['user'] !== 'Admin' && this.serverConfig['CLIENT_NAME'] !== 'ResolveIO') {
489
+ this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.getMethodManager(), 'supportCreateBillingUser', {
490
+ id_user: ws['id_user'],
491
+ user: ws['user'],
492
+ date: new Date(),
493
+ client: index_1.ResolveIOServer.getClientName()
494
+ });
504
495
  }
505
- _a.label = 2;
506
- case 2: return [2 /*return*/];
496
+ }
497
+ else {
498
+ this.unsubscribeAll(ws);
499
+ }
507
500
  }
501
+ return [2 /*return*/];
508
502
  });
509
503
  });
510
504
  };
511
- SubscriptionManager.prototype.ensureLoggedInUser = function (id_ws) {
505
+ SubscriptionManager.prototype.createLoggedInUser = function (id_ws) {
512
506
  return __awaiter(this, void 0, void 0, function () {
513
507
  var _this = this;
514
508
  return __generator(this, function (_a) {
@@ -519,18 +513,18 @@ var SubscriptionManager = /** @class */ (function () {
519
513
  case 0:
520
514
  ws = this._mainServer.getWS(id_ws);
521
515
  if (!ws) return [3 /*break*/, 2];
522
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOneAndUpdate({ id_ws: ws['id_socket'] }, {
523
- $setOnInsert: {
524
- _id: mongo_manager_1.objectIdHexString(),
525
- __v: 0,
526
- date: new Date(),
527
- id_user: ws['id_user'],
528
- user: ws['user'],
529
- id_ws: ws['id_socket']
530
- }
531
- }, { upsert: true, returnOriginal: false })];
516
+ user = {
517
+ _id: mongo_manager_1.objectIdHexString(),
518
+ __v: 0,
519
+ date: new Date(),
520
+ id_user: ws['id_user'],
521
+ user: ws['user'],
522
+ id_ws: ws['id_socket']
523
+ };
524
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.insertOne(user)];
532
525
  case 1:
533
- user = _a.sent();
526
+ _a.sent();
527
+ this._loggedInUsers.push(user);
534
528
  resolve(user);
535
529
  return [3 /*break*/, 3];
536
530
  case 2: