@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.
package/server-app.js CHANGED
@@ -359,230 +359,241 @@ var ResolveIOMainServer = /** @class */ (function () {
359
359
  console.log('Running server on port %s', _this._port);
360
360
  });
361
361
  // On websocket connection (already verified)
362
- this._wss.on('connection', function (ws, req) {
363
- // Get user from token
364
- ws['id_user'] = req['id_user'];
365
- ws['user'] = req['user'];
366
- ws['user_readonly'] = req['user_readonly'];
367
- ws['doc_user'] = req['doc_user'];
368
- var idSocket = '';
369
- while (!idSocket || Array.from(_this._wss.clients).some(function (a) { return a['id_socket'] === idSocket; })) {
370
- idSocket = uuid_1.v4();
371
- }
372
- ws['id_socket'] = idSocket;
373
- ws['retryCnt'] = 0;
374
- setTimeout(function () {
375
- ws['pingTime'] = new Date();
376
- ws.ping(function () { });
377
- }, 5000);
378
- if (_this.LOGGER === 'DEBUG') {
379
- console.log('Connection from: ' + req['user']);
380
- }
381
- // Use for keeping connection alive (ping/pong)
382
- ws['isAlive'] = true;
383
- ws.on('pong', function () {
384
- ws['isAlive'] = true;
385
- ws['pongTime'] = new Date();
386
- ws['latency'] = moment.duration(moment(ws['pongTime']).diff(ws['pingTime'])).asMilliseconds();
387
- _this._subscriptionManager.loggedInLatency(ws);
388
- });
389
- ws['versionTimer'] = setTimeout(function () {
390
- _this.unsubscribeWS(ws);
391
- }, 20000);
392
- // On data received (message)
393
- ws.on('message', function (message) { return __awaiter(_this, void 0, void 0, function () {
394
- var parseErrorFound, socketData, messageRoute_1, messageDate, messageId, type, subType, pub, serverRes, offlineUpdates, i, update, serverResMethod, i, update, data, updateRoute, updateDate, updateMessageId, updateType, method, methodLatencyId, method, serverRes, methodLatencyId;
395
- var _a, _b, _c, _d;
396
- var _this = this;
397
- return __generator(this, function (_e) {
398
- switch (_e.label) {
399
- case 0:
400
- if (this.LOGGER === 'DEBUG') {
401
- console.log('Message from: ' + ws['user'], message);
402
- }
403
- parseErrorFound = false;
404
- socketData = [];
405
- try {
406
- socketData = JSON.parse(message, common_1.dateReviver);
407
- }
408
- catch (e) {
409
- console.log('Error - JSON.parse', message);
410
- this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - JSON Parse Error - ' + this._serverConfig['CLIENT_NAME'], JSON.stringify([message, e]));
411
- parseErrorFound = true;
412
- }
413
- if (!!parseErrorFound) return [3 /*break*/, 8];
414
- if (socketData[0] === 'ping') {
415
- if (ws && ws.readyState === ws.OPEN) {
416
- ws.send('pong', function (error) {
417
- if (error) {
418
- console.log('Error on WS: ', error);
419
- _this.unsubscribeWS(ws);
362
+ this._wss.on('connection', function (ws, req) { return __awaiter(_this, void 0, void 0, function () {
363
+ var idSocket;
364
+ var _this = this;
365
+ return __generator(this, function (_a) {
366
+ switch (_a.label) {
367
+ case 0:
368
+ // Get user from token
369
+ ws['id_user'] = req['id_user'];
370
+ ws['user'] = req['user'];
371
+ ws['user_readonly'] = req['user_readonly'];
372
+ ws['doc_user'] = req['doc_user'];
373
+ idSocket = '';
374
+ while (!idSocket || Array.from(this._wss.clients).some(function (a) { return a['id_socket'] === idSocket; })) {
375
+ idSocket = uuid_1.v4();
376
+ }
377
+ ws['id_socket'] = idSocket;
378
+ ws['retryCnt'] = 0;
379
+ return [4 /*yield*/, this._subscriptionManager.createLoggedInUser(idSocket)];
380
+ case 1:
381
+ _a.sent();
382
+ setTimeout(function () {
383
+ ws['pingTime'] = new Date();
384
+ ws.ping(function () { });
385
+ }, 5000);
386
+ if (this.LOGGER === 'DEBUG') {
387
+ console.log('Connection from: ' + req['user']);
388
+ }
389
+ // Use for keeping connection alive (ping/pong)
390
+ ws['isAlive'] = true;
391
+ ws.on('pong', function () {
392
+ ws['isAlive'] = true;
393
+ ws['pongTime'] = new Date();
394
+ ws['latency'] = moment.duration(moment(ws['pongTime']).diff(ws['pingTime'])).asMilliseconds();
395
+ _this._subscriptionManager.loggedInLatency(ws);
396
+ });
397
+ ws['versionTimer'] = setTimeout(function () {
398
+ _this.unsubscribeWS(ws);
399
+ }, 20000);
400
+ // On data received (message)
401
+ ws.on('message', function (message) { return __awaiter(_this, void 0, void 0, function () {
402
+ var parseErrorFound, socketData, messageRoute_1, messageDate, messageId, type, subType, pub, serverRes, offlineUpdates, i, update, serverResMethod, i, update, data, updateRoute, updateDate, updateMessageId, updateType, method, methodLatencyId, method, serverRes, methodLatencyId;
403
+ var _a, _b, _c, _d;
404
+ var _this = this;
405
+ return __generator(this, function (_e) {
406
+ switch (_e.label) {
407
+ case 0:
408
+ if (this.LOGGER === 'DEBUG') {
409
+ console.log('Message from: ' + ws['user'], message);
420
410
  }
421
- });
422
- }
423
- return [2 /*return*/];
424
- }
425
- messageRoute_1 = socketData[0];
426
- if (this._clientRoutes.some(function (a) { return messageRoute_1.includes(a); }) && !ws['doc_user'].roles.groups.some(function (a) { return a.views.some(function (b) { return messageRoute_1.includes(b); }); }) && !ws['doc_user'].roles.super_admin) {
427
- return [2 /*return*/];
428
- }
429
- messageDate = socketData[1];
430
- messageId = socketData[2];
431
- type = socketData[3];
432
- if (!(type === 'subscription')) return [3 /*break*/, 1];
433
- subType = socketData[4];
434
- pub = socketData[5];
435
- // Subscribe
436
- if (subType === 'sub') {
437
- (_a = this._subscriptionManager).subscribe.apply(_a, __spreadArrays([messageDate, ws, messageId, pub], socketData.slice(6)));
438
- }
439
- // Unsubscribe
440
- else {
441
- (_b = this._subscriptionManager).unsubscribe.apply(_b, __spreadArrays([messageDate, ws, messageId, pub], socketData.slice(6)));
442
- }
443
- return [3 /*break*/, 8];
444
- case 1:
445
- if (!(type === 'offline')) return [3 /*break*/, 7];
446
- serverRes = {
447
- messageId: messageId,
448
- hasError: false,
449
- data: 'ACK'
450
- };
451
- if (ws && ws.readyState === ws.OPEN) {
452
- ws.send(JSON.stringify(serverRes), function (error) {
453
- if (error) {
454
- console.log('Error on WS: ', error);
455
- _this.unsubscribeWS(ws);
456
- }
457
- });
458
- }
459
- offlineUpdates = socketData[4];
460
- for (i = 0; i < offlineUpdates.length; i++) {
461
- update = offlineUpdates[i];
462
- serverResMethod = {
463
- messageId: update.data[2],
464
- hasError: false,
465
- data: 'ACK'
466
- };
467
- if (ws && ws.readyState === ws.OPEN) {
468
- ws.send(JSON.stringify(serverResMethod), function (error) {
469
- if (error) {
470
- console.log('Error on WS: ', error);
471
- _this.unsubscribeWS(ws);
411
+ parseErrorFound = false;
412
+ socketData = [];
413
+ try {
414
+ socketData = JSON.parse(message, common_1.dateReviver);
472
415
  }
473
- });
474
- }
475
- }
476
- i = 0;
477
- _e.label = 2;
478
- case 2:
479
- if (!(i < offlineUpdates.length)) return [3 /*break*/, 6];
480
- update = offlineUpdates[i];
481
- data = update.data;
482
- if (update.oldData) {
483
- data.push(update.oldData);
484
- }
485
- updateRoute = data.shift();
486
- updateDate = data.shift();
487
- updateMessageId = data.shift();
488
- updateType = data.shift();
489
- method = data.shift();
490
- methodLatencyId = mongo_manager_1.objectIdHexString();
491
- log_method_latency_collection_1.LogMethodLatencies.create({
492
- _id: methodLatencyId,
493
- __v: 0,
494
- date_start: new Date(),
495
- date_end: null,
496
- latency_ms: 0,
497
- method: method
498
- });
499
- if (method !== 'reportBuilderGetResults' && method !== 'reportBuilderGetDistinctValue' && method !== 'reportBuilderBuildTree' && method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countWithQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'getDrivers') {
500
- log_collection_1.Logs.insertOne({
501
- _id: mongo_manager_1.objectIdHexString(),
502
- type: 'client-request',
503
- collection: '',
504
- id_document: '',
505
- payload: common_1.getBinarySize(JSON.stringify(data)) < 200000 ? JSON.stringify(data, null, 2) : 'Too Big',
506
- method: method,
507
- id_user: ws['id_user'] || '',
508
- user: ws['user'] || '',
509
- messageId: messageId,
510
- route: messageRoute_1
511
- });
512
- }
513
- if (!this._methodManager._methods[method]) return [3 /*break*/, 4];
514
- return [4 /*yield*/, (_c = this._methodManager.callMethodInternal).call.apply(_c, __spreadArrays([Object.assign({}, this._methodManager, { id_user: ws['id_user'], user: ws['user'], id_ws: ws['id_socket'] }), method], data))];
515
- case 3:
516
- _e.sent();
517
- if (method === 'updateDocumentOffline' || method === 'updateDocumentPropsOffline') {
518
- index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(data[0]);
519
- }
520
- return [3 /*break*/, 5];
521
- case 4:
522
- console.log('Offline - Could not find method: ' + method);
523
- _e.label = 5;
524
- case 5:
525
- i++;
526
- return [3 /*break*/, 2];
527
- case 6: return [3 /*break*/, 8];
528
- case 7:
529
- if (type === 'method' && socketData[4] === 'setWSAppVersion') {
530
- clearTimeout(ws['versionTimer']);
531
- method = socketData[4];
532
- serverRes = {
533
- messageId: messageId,
534
- hasError: false,
535
- data: 'ACK'
536
- };
537
- if (ws && ws.readyState === ws.OPEN) {
538
- ws.send(JSON.stringify(serverRes), function (error) {
539
- if (error) {
540
- console.log('Error on WS: ', error);
541
- _this.unsubscribeWS(ws);
416
+ catch (e) {
417
+ console.log('Error - JSON.parse', message);
418
+ this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - JSON Parse Error - ' + this._serverConfig['CLIENT_NAME'], JSON.stringify([message, e]));
419
+ parseErrorFound = true;
542
420
  }
543
- });
544
- }
545
- methodLatencyId = mongo_manager_1.objectIdHexString();
546
- log_method_latency_collection_1.LogMethodLatencies.create({
547
- _id: methodLatencyId,
548
- __v: 0,
549
- date_start: new Date(),
550
- date_end: null,
551
- latency_ms: 0,
552
- method: method
553
- });
554
- if (this._methodManager._methods[method]) {
555
- (_d = this._methodManager).callMethod.apply(_d, __spreadArrays([methodLatencyId, ws, messageDate, messageId, method], socketData.slice(5)));
556
- }
557
- else {
558
- console.log('Could not find method: ' + method);
421
+ if (!!parseErrorFound) return [3 /*break*/, 8];
422
+ if (socketData[0] === 'ping') {
423
+ if (ws && ws.readyState === ws.OPEN) {
424
+ ws.send('pong', function (error) {
425
+ if (error) {
426
+ console.log('Error on WS: ', error);
427
+ _this.unsubscribeWS(ws);
428
+ }
429
+ });
430
+ }
431
+ return [2 /*return*/];
432
+ }
433
+ messageRoute_1 = socketData[0];
434
+ if (this._clientRoutes.some(function (a) { return messageRoute_1.includes(a); }) && !ws['doc_user'].roles.groups.some(function (a) { return a.views.some(function (b) { return messageRoute_1.includes(b); }); }) && !ws['doc_user'].roles.super_admin) {
435
+ return [2 /*return*/];
436
+ }
437
+ messageDate = socketData[1];
438
+ messageId = socketData[2];
439
+ type = socketData[3];
440
+ if (!(type === 'subscription')) return [3 /*break*/, 1];
441
+ subType = socketData[4];
442
+ pub = socketData[5];
443
+ // Subscribe
444
+ if (subType === 'sub') {
445
+ (_a = this._subscriptionManager).subscribe.apply(_a, __spreadArrays([messageDate, ws, messageId, pub], socketData.slice(6)));
446
+ }
447
+ // Unsubscribe
448
+ else {
449
+ (_b = this._subscriptionManager).unsubscribe.apply(_b, __spreadArrays([messageDate, ws, messageId, pub], socketData.slice(6)));
450
+ }
451
+ return [3 /*break*/, 8];
452
+ case 1:
453
+ if (!(type === 'offline')) return [3 /*break*/, 7];
454
+ serverRes = {
455
+ messageId: messageId,
456
+ hasError: false,
457
+ data: 'ACK'
458
+ };
459
+ if (ws && ws.readyState === ws.OPEN) {
460
+ ws.send(JSON.stringify(serverRes), function (error) {
461
+ if (error) {
462
+ console.log('Error on WS: ', error);
463
+ _this.unsubscribeWS(ws);
464
+ }
465
+ });
466
+ }
467
+ offlineUpdates = socketData[4];
468
+ for (i = 0; i < offlineUpdates.length; i++) {
469
+ update = offlineUpdates[i];
470
+ serverResMethod = {
471
+ messageId: update.data[2],
472
+ hasError: false,
473
+ data: 'ACK'
474
+ };
475
+ if (ws && ws.readyState === ws.OPEN) {
476
+ ws.send(JSON.stringify(serverResMethod), function (error) {
477
+ if (error) {
478
+ console.log('Error on WS: ', error);
479
+ _this.unsubscribeWS(ws);
480
+ }
481
+ });
482
+ }
483
+ }
484
+ i = 0;
485
+ _e.label = 2;
486
+ case 2:
487
+ if (!(i < offlineUpdates.length)) return [3 /*break*/, 6];
488
+ update = offlineUpdates[i];
489
+ data = update.data;
490
+ if (update.oldData) {
491
+ data.push(update.oldData);
492
+ }
493
+ updateRoute = data.shift();
494
+ updateDate = data.shift();
495
+ updateMessageId = data.shift();
496
+ updateType = data.shift();
497
+ method = data.shift();
498
+ methodLatencyId = mongo_manager_1.objectIdHexString();
499
+ log_method_latency_collection_1.LogMethodLatencies.create({
500
+ _id: methodLatencyId,
501
+ __v: 0,
502
+ date_start: new Date(),
503
+ date_end: null,
504
+ latency_ms: 0,
505
+ method: method
506
+ });
507
+ if (method !== 'reportBuilderGetResults' && method !== 'reportBuilderGetDistinctValue' && method !== 'reportBuilderBuildTree' && method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countWithQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'getDrivers') {
508
+ log_collection_1.Logs.insertOne({
509
+ _id: mongo_manager_1.objectIdHexString(),
510
+ type: 'client-request',
511
+ collection: '',
512
+ id_document: '',
513
+ payload: common_1.getBinarySize(JSON.stringify(data)) < 200000 ? JSON.stringify(data, null, 2) : 'Too Big',
514
+ method: method,
515
+ id_user: ws['id_user'] || '',
516
+ user: ws['user'] || '',
517
+ messageId: messageId,
518
+ route: messageRoute_1
519
+ });
520
+ }
521
+ if (!this._methodManager._methods[method]) return [3 /*break*/, 4];
522
+ return [4 /*yield*/, (_c = this._methodManager.callMethodInternal).call.apply(_c, __spreadArrays([Object.assign({}, this._methodManager, { id_user: ws['id_user'], user: ws['user'], id_ws: ws['id_socket'] }), method], data))];
523
+ case 3:
524
+ _e.sent();
525
+ if (method === 'updateDocumentOffline' || method === 'updateDocumentPropsOffline') {
526
+ index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(data[0]);
527
+ }
528
+ return [3 /*break*/, 5];
529
+ case 4:
530
+ console.log('Offline - Could not find method: ' + method);
531
+ _e.label = 5;
532
+ case 5:
533
+ i++;
534
+ return [3 /*break*/, 2];
535
+ case 6: return [3 /*break*/, 8];
536
+ case 7:
537
+ if (type === 'method' && socketData[4] === 'setWSAppVersion') {
538
+ clearTimeout(ws['versionTimer']);
539
+ method = socketData[4];
540
+ serverRes = {
541
+ messageId: messageId,
542
+ hasError: false,
543
+ data: 'ACK'
544
+ };
545
+ if (ws && ws.readyState === ws.OPEN) {
546
+ ws.send(JSON.stringify(serverRes), function (error) {
547
+ if (error) {
548
+ console.log('Error on WS: ', error);
549
+ _this.unsubscribeWS(ws);
550
+ }
551
+ });
552
+ }
553
+ methodLatencyId = mongo_manager_1.objectIdHexString();
554
+ log_method_latency_collection_1.LogMethodLatencies.create({
555
+ _id: methodLatencyId,
556
+ __v: 0,
557
+ date_start: new Date(),
558
+ date_end: null,
559
+ latency_ms: 0,
560
+ method: method
561
+ });
562
+ if (this._methodManager._methods[method]) {
563
+ (_d = this._methodManager).callMethod.apply(_d, __spreadArrays([methodLatencyId, ws, messageDate, messageId, method], socketData.slice(5)));
564
+ }
565
+ else {
566
+ console.log('Could not find method: ' + method);
567
+ }
568
+ }
569
+ else {
570
+ this._msgQueue.splice(0, 0, {
571
+ ws: ws,
572
+ data: socketData
573
+ });
574
+ }
575
+ _e.label = 8;
576
+ case 8: return [2 /*return*/];
559
577
  }
560
- }
561
- else {
562
- this._msgQueue.splice(0, 0, {
563
- ws: ws,
564
- data: socketData
565
- });
566
- }
567
- _e.label = 8;
568
- case 8: return [2 /*return*/];
569
- }
570
- });
571
- }); });
572
- ws.on('close', function () {
573
- _this.unsubscribeWS(ws);
574
- });
575
- ws.on('error', function (error) {
576
- console.log(new Date(), ws['user'], ws['id_socket'], error);
577
- _this.unsubscribeWS(ws);
578
+ });
579
+ }); });
580
+ ws.on('close', function () {
581
+ _this.unsubscribeWS(ws);
582
+ });
583
+ ws.on('error', function (error) {
584
+ console.log(new Date(), ws['user'], ws['id_socket'], error);
585
+ _this.unsubscribeWS(ws);
586
+ });
587
+ return [2 /*return*/];
588
+ }
578
589
  });
579
- });
590
+ }); });
580
591
  // Keep alive timer to ping/pong
581
592
  setInterval(function () {
582
593
  _this._wss.clients.forEach(function (ws) {
583
594
  if (ws['isAlive'] === false) {
584
595
  ws['retryCnt']++;
585
- if (ws['retryCnt'] >= 5) {
596
+ if (ws['retryCnt'] >= 60) {
586
597
  _this.unsubscribeWS(ws);
587
598
  }
588
599
  else {
@@ -597,7 +608,7 @@ var ResolveIOMainServer = /** @class */ (function () {
597
608
  ws.ping(function () { });
598
609
  }
599
610
  });
600
- }, 60000);
611
+ }, 15000);
601
612
  setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
602
613
  var _loop_1, this_1, i;
603
614
  var _this = this;