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