@resolveio/server-lib 2.2.4 → 2.2.5

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.
@@ -31,6 +31,9 @@ var schema = {
31
31
  },
32
32
  data: {
33
33
  type: String
34
+ },
35
+ date: {
36
+ type: Date
34
37
  }
35
38
  };
36
39
  var DefaultSchema = new mongoose_1.Schema(schema, {
@@ -4,4 +4,5 @@ export interface MethodCallModel extends CollectionDocument {
4
4
  message_id: string;
5
5
  method: string;
6
6
  data: string;
7
+ date: Date;
7
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {
package/server-app.d.ts CHANGED
@@ -11,6 +11,8 @@ export default class ResolveIOMainServer {
11
11
  private _wss;
12
12
  private _serverConfig;
13
13
  private _clientDir;
14
+ private _msgQueue;
15
+ private _msgQueueRunning;
14
16
  private _resolveioServer;
15
17
  private _subscriptionManager;
16
18
  private _methodManager;
package/server-app.js CHANGED
@@ -60,6 +60,8 @@ var support_manager_1 = require("./managers/support.manager");
60
60
  var queue_method_manager_1 = require("./managers/queue-method.manager");
61
61
  var ResolveIOMainServer = /** @class */ (function () {
62
62
  function ResolveIOMainServer(mainServer, serverConfig, clientDir) {
63
+ this._msgQueue = [];
64
+ this._msgQueueRunning = false;
63
65
  this._resolveioServer = mainServer;
64
66
  this._serverConfig = serverConfig;
65
67
  this._clientDir = clientDir;
@@ -248,381 +250,14 @@ var ResolveIOMainServer = /** @class */ (function () {
248
250
  }, 10000);
249
251
  // On data received (message)
250
252
  ws.on('message', function (message) { return __awaiter(_this, void 0, void 0, function () {
251
- var _a, _b, _c, _d, _e, data, messageId, type, method, serverRes, query, methodCall, subType, pub, supportMethod, serverRes;
252
- var _this = this;
253
- return __generator(this, function (_f) {
254
- switch (_f.label) {
255
- case 0:
256
- data = JSON.parse(message, common_1.dateReviver);
257
- // console.log(data);
258
- if (data[0] === 'ping') {
259
- if (ws && ws.readyState === ws.OPEN) {
260
- ws.send('pong', function (error) {
261
- if (error) {
262
- console.log('Error on WS: ', error);
263
- _this.unsubscribeWS(ws);
264
- }
265
- });
266
- }
267
- return [2 /*return*/];
268
- }
269
- messageId = data.shift();
270
- type = data.shift();
271
- method = '';
272
- if (!(type === 'method')) return [3 /*break*/, 8];
273
- method = data.shift();
274
- if (method === 'setWSAppVersion') {
275
- clearTimeout(ws['versionTimer']);
276
- }
277
- if (method === 'disconnectWSCommand') {
278
- setTimeout(function () {
279
- _this.unsubscribeWS(ws);
280
- ws.terminate();
281
- }, 5000);
282
- }
283
- if (method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
284
- if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
285
- log_collection_1.Logs.create({
286
- _id: mongoose_1.Types.ObjectId().toHexString(),
287
- date: new Date(),
288
- type: 'client-request',
289
- title: 'Client Request - Method',
290
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
291
- payload: data,
292
- method: method,
293
- user: ws['user'],
294
- messageId: messageId
295
- });
296
- }
297
- else {
298
- log_collection_1.Logs.create({
299
- _id: mongoose_1.Types.ObjectId().toHexString(),
300
- date: new Date(),
301
- type: 'client-request',
302
- title: 'Client Request - Method',
303
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
304
- payload: 'Too Big',
305
- method: method,
306
- user: ws['user'],
307
- messageId: messageId
308
- });
309
- }
310
- }
311
- else {
312
- log_collection_1.Logs.create({
313
- _id: mongoose_1.Types.ObjectId().toHexString(),
314
- date: new Date(),
315
- type: 'client-request',
316
- title: 'Client Request - Method',
317
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
318
- payload: 'Too Big',
319
- method: method,
320
- user: ws['user'],
321
- messageId: messageId
322
- });
323
- }
324
- serverRes = {
325
- messageId: messageId,
326
- hasError: false,
327
- data: 'ACK'
328
- };
329
- if (ws && ws.readyState === ws.OPEN) {
330
- ws.send(JSON.stringify(serverRes), function (error) {
331
- if (error) {
332
- console.log('Error on WS: ', error);
333
- _this.unsubscribeWS(ws);
334
- }
335
- });
336
- }
337
- if (!this._methodManager._methods[method]) return [3 /*break*/, 6];
338
- if (!this._methodManager._methods[method].skipQueue) return [3 /*break*/, 1];
339
- (_a = this._methodManager).callMethod.apply(_a, [ws, messageId, method].concat(data));
340
- return [3 /*break*/, 5];
341
- case 1:
342
- query = { $and: [
343
- { id_user: ws['id_user'] },
344
- { message_id: messageId },
345
- { data: JSON.stringify(data) }
346
- ] };
347
- if (method !== 'updateDocumentOffline' && method !== 'updateDocumentPropsOffline') {
348
- query.$and.push({ method: method });
349
- }
350
- else {
351
- if (method === 'updateDocumentOffline') {
352
- query.$and.push({
353
- $or: [
354
- { method: method },
355
- { method: 'updateDocument' }
356
- ]
357
- });
358
- }
359
- else if (method === 'updateDocumentPropsOffline') {
360
- query.$and.push({
361
- $or: [
362
- { method: method },
363
- { method: 'updateDocumentProps' }
364
- ]
365
- });
366
- }
367
- }
368
- return [4 /*yield*/, method_call_collection_1.MethodCalls.findOne(query).exec()];
369
- case 2:
370
- methodCall = _f.sent();
371
- if (!!methodCall) return [3 /*break*/, 4];
372
- return [4 /*yield*/, method_call_collection_1.MethodCalls.create({
373
- _id: mongoose_1.Types.ObjectId().toHexString(),
374
- __v: 0,
375
- id_user: ws['id_user'],
376
- message_id: messageId,
377
- method: method,
378
- data: JSON.stringify(data)
379
- })];
380
- case 3:
381
- _f.sent();
382
- if (this._methodManager._methods[method]) {
383
- if (this._methodManager._methods[method].skipQueue) {
384
- (_b = this._methodManager).callMethod.apply(_b, [ws, messageId, method].concat(data));
385
- }
386
- else {
387
- this._queueManager.addMethodToQueue(ws['id_socket'], ws['id_user'], ws['user'], messageId, method, data);
388
- }
389
- }
390
- else {
391
- console.log('Could not find method: ' + method);
392
- }
393
- return [3 /*break*/, 5];
394
- case 4:
395
- method_response_collection_1.MethodResponses.findOne({ $and: [
396
- { id_user: ws['id_user'] },
397
- { message_id: messageId },
398
- { method: method },
399
- { data: JSON.stringify(data) }
400
- ] }).exec(function (err, res) {
401
- if (res) {
402
- if (ws && ws.readyState === ws.OPEN) {
403
- ws.send(JSON.stringify(res.response), function (error) {
404
- if (error) {
405
- console.log('Error on WS: ', error);
406
- _this.unsubscribeWS(ws);
407
- }
408
- });
409
- }
410
- }
411
- });
412
- _f.label = 5;
413
- case 5: return [3 /*break*/, 7];
414
- case 6:
415
- console.log('Could not find method: ' + method);
416
- _f.label = 7;
417
- case 7: return [3 /*break*/, 9];
418
- case 8:
419
- if (type === 'methodRetry') {
420
- method = data.shift();
421
- if (method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
422
- if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
423
- log_collection_1.Logs.create({
424
- _id: mongoose_1.Types.ObjectId().toHexString(),
425
- date: new Date(),
426
- type: 'client-request',
427
- title: 'Client Request - Method Retry',
428
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
429
- payload: data,
430
- method: method,
431
- user: ws['user'],
432
- messageId: messageId
433
- });
434
- }
435
- else {
436
- log_collection_1.Logs.create({
437
- _id: mongoose_1.Types.ObjectId().toHexString(),
438
- date: new Date(),
439
- type: 'client-request',
440
- title: 'Client Request - Method Retry',
441
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
442
- payload: 'Too Big',
443
- method: method,
444
- user: ws['user'],
445
- messageId: messageId
446
- });
447
- }
448
- }
449
- else {
450
- log_collection_1.Logs.create({
451
- _id: mongoose_1.Types.ObjectId().toHexString(),
452
- date: new Date(),
453
- type: 'client-request',
454
- title: 'Client Request - Method Retry',
455
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
456
- payload: 'Too Big',
457
- method: method,
458
- user: ws['user'],
459
- messageId: messageId
460
- });
461
- }
462
- method_call_collection_1.MethodCalls.findOne({ $and: [
463
- { id_user: ws['id_user'] },
464
- { message_id: messageId },
465
- { method: method },
466
- { data: JSON.stringify(data) }
467
- ] }).exec(function (err, res) {
468
- if (res) {
469
- var serverRes = {
470
- messageId: messageId,
471
- hasError: false,
472
- data: 'ACK'
473
- };
474
- if (ws && ws.readyState === ws.OPEN) {
475
- ws.send(JSON.stringify(serverRes), function (error) {
476
- if (error) {
477
- console.log('Error on WS: ', error);
478
- _this.unsubscribeWS(ws);
479
- }
480
- });
481
- }
482
- }
483
- else {
484
- var serverRes = {
485
- messageId: messageId,
486
- hasError: false,
487
- data: 'NACK'
488
- };
489
- if (ws && ws.readyState === ws.OPEN) {
490
- ws.send(JSON.stringify(serverRes), function (error) {
491
- if (error) {
492
- console.log('Error on WS: ', error);
493
- _this.unsubscribeWS(ws);
494
- }
495
- });
496
- }
497
- }
498
- });
499
- }
500
- else if (type === 'methodResponse') {
501
- method = data.shift();
502
- // if (method !== 'generatePDF' && method !== 'getWOOfflineData' &&method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
503
- // if (getBinarySize(JSON.stringify(data)) < 200000) {
504
- // Logs.create({
505
- // _id: Types.ObjectId().toHexString(),
506
- // date: new Date(),
507
- // type: 'client-request',
508
- // title: 'Client Request - Method Response',
509
- // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
510
- // payload: data,
511
- // method: method,
512
- // user: ws['user'],
513
- // messageId: messageId
514
- // });
515
- // }
516
- // else {
517
- // Logs.create({
518
- // _id: Types.ObjectId().toHexString(),
519
- // date: new Date(),
520
- // type: 'client-request',
521
- // title: 'Client Request - Method Response',
522
- // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
523
- // payload: 'Too Big',
524
- // method: method,
525
- // user: ws['user'],
526
- // messageId: messageId
527
- // });
528
- // }
529
- // }
530
- // else {
531
- // Logs.create({
532
- // _id: Types.ObjectId().toHexString(),
533
- // date: new Date(),
534
- // type: 'client-request',
535
- // title: 'Client Request - Method Response',
536
- // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
537
- // payload: 'Too Big',
538
- // method: method,
539
- // user: ws['user'],
540
- // messageId: messageId
541
- // });
542
- // }
543
- method_response_collection_1.MethodResponses.findOne({ $and: [
544
- { id_user: ws['id_user'] },
545
- { message_id: messageId },
546
- { method: method },
547
- { data: JSON.stringify(data) }
548
- ] }).exec(function (err, res) {
549
- if (res) {
550
- if (ws && ws.readyState === ws.OPEN) {
551
- ws.send(JSON.stringify(res.response), function (error) {
552
- if (error) {
553
- console.log('Error on WS: ', error);
554
- _this.unsubscribeWS(ws);
555
- }
556
- });
557
- }
558
- }
559
- });
560
- }
561
- // Subscription call
562
- else if (type === 'subscription') {
563
- subType = data.shift();
564
- pub = data.shift();
565
- // Subscribe
566
- if (subType === 'sub') {
567
- (_c = this._subscriptionManager).subscribe.apply(_c, [ws, messageId, pub].concat(data));
568
- }
569
- // Unsubscribe
570
- else {
571
- (_d = this._subscriptionManager).unsubscribe.apply(_d, [ws, messageId, pub].concat(data));
572
- }
573
- }
574
- else if (type === 'support') {
575
- supportMethod = data.shift();
576
- // if (supportMethod !== 'countQuery' && supportMethod !== 'countCollectionWithQuery' && supportMethod !== 'find' && supportMethod !== 'findOne' && supportMethod !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'uploadFileAndSave') {
577
- if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
578
- log_collection_1.Logs.create({
579
- _id: mongoose_1.Types.ObjectId().toHexString(),
580
- date: new Date(),
581
- type: 'client-request',
582
- title: 'Client Request - Support',
583
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
584
- payload: data,
585
- method: supportMethod,
586
- user: ws['user'],
587
- messageId: messageId
588
- });
589
- }
590
- else {
591
- log_collection_1.Logs.create({
592
- _id: mongoose_1.Types.ObjectId().toHexString(),
593
- date: new Date(),
594
- type: 'client-request',
595
- title: 'Client Request - Support',
596
- message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
597
- payload: 'Too Big',
598
- method: supportMethod,
599
- user: ws['user'],
600
- messageId: messageId
601
- });
602
- }
603
- serverRes = {
604
- messageId: messageId,
605
- hasError: false,
606
- data: 'ACK'
607
- };
608
- if (ws && ws.readyState === ws.OPEN) {
609
- ws.send(JSON.stringify(serverRes), function (error) {
610
- if (error) {
611
- console.log('Error on WS: ', error);
612
- _this.unsubscribeWS(ws);
613
- }
614
- });
615
- }
616
- if (this._supportManager._supportMethods[supportMethod]) {
617
- (_e = this._supportManager).callSupportMethod.apply(_e, [ws, messageId, supportMethod].concat(data));
618
- }
619
- else {
620
- console.log('Could not find support method: ' + supportMethod);
621
- }
622
- }
623
- _f.label = 9;
624
- case 9: return [2 /*return*/];
625
- }
253
+ var data;
254
+ return __generator(this, function (_a) {
255
+ data = JSON.parse(message, common_1.dateReviver);
256
+ this._msgQueue.splice(0, 0, {
257
+ ws: ws,
258
+ data: data
259
+ });
260
+ return [2 /*return*/];
626
261
  });
627
262
  }); });
628
263
  ws.on('close', function () {
@@ -651,6 +286,412 @@ var ResolveIOMainServer = /** @class */ (function () {
651
286
  }
652
287
  });
653
288
  }, 5000);
289
+ setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
290
+ var _loop_1, this_1, i, state_1;
291
+ var _this = this;
292
+ return __generator(this, function (_a) {
293
+ switch (_a.label) {
294
+ case 0:
295
+ if (!!this._msgQueueRunning) return [3 /*break*/, 5];
296
+ _loop_1 = function (i) {
297
+ var _a, _b, _c, _d, msg, data, ws, messageDate, messageId, type, method, serverRes, query, methodCall, subType, pub, supportMethod, serverRes;
298
+ return __generator(this, function (_e) {
299
+ switch (_e.label) {
300
+ case 0:
301
+ this_1._msgQueueRunning = true;
302
+ msg = this_1._msgQueue.pop();
303
+ data = msg.data;
304
+ ws = msg.ws;
305
+ if (data[1] === 'ping') {
306
+ if (ws && ws.readyState === ws.OPEN) {
307
+ ws.send('pong', function (error) {
308
+ if (error) {
309
+ console.log('Error on WS: ', error);
310
+ _this.unsubscribeWS(ws);
311
+ }
312
+ });
313
+ }
314
+ return [2 /*return*/, { value: void 0 }];
315
+ }
316
+ messageDate = data.shift();
317
+ messageId = data.shift();
318
+ type = data.shift();
319
+ method = '';
320
+ if (!(type === 'method')) return [3 /*break*/, 7];
321
+ method = data.shift();
322
+ if (method === 'setWSAppVersion') {
323
+ clearTimeout(ws['versionTimer']);
324
+ }
325
+ if (method === 'disconnectWSCommand') {
326
+ setTimeout(function () {
327
+ _this.unsubscribeWS(ws);
328
+ ws.terminate();
329
+ }, 5000);
330
+ }
331
+ if (method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
332
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
333
+ log_collection_1.Logs.create({
334
+ _id: mongoose_1.Types.ObjectId().toHexString(),
335
+ date: new Date(),
336
+ type: 'client-request',
337
+ title: 'Client Request - Method',
338
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
339
+ payload: data,
340
+ method: method,
341
+ user: ws['user'],
342
+ messageId: messageId
343
+ });
344
+ }
345
+ else {
346
+ log_collection_1.Logs.create({
347
+ _id: mongoose_1.Types.ObjectId().toHexString(),
348
+ date: new Date(),
349
+ type: 'client-request',
350
+ title: 'Client Request - Method',
351
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
352
+ payload: 'Too Big',
353
+ method: method,
354
+ user: ws['user'],
355
+ messageId: messageId
356
+ });
357
+ }
358
+ }
359
+ else {
360
+ log_collection_1.Logs.create({
361
+ _id: mongoose_1.Types.ObjectId().toHexString(),
362
+ date: new Date(),
363
+ type: 'client-request',
364
+ title: 'Client Request - Method',
365
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
366
+ payload: 'Too Big',
367
+ method: method,
368
+ user: ws['user'],
369
+ messageId: messageId
370
+ });
371
+ }
372
+ serverRes = {
373
+ messageId: messageId,
374
+ hasError: false,
375
+ data: 'ACK'
376
+ };
377
+ if (ws && ws.readyState === ws.OPEN) {
378
+ ws.send(JSON.stringify(serverRes), function (error) {
379
+ if (error) {
380
+ console.log('Error on WS: ', error);
381
+ _this.unsubscribeWS(ws);
382
+ }
383
+ });
384
+ }
385
+ if (!this_1._methodManager._methods[method]) return [3 /*break*/, 5];
386
+ query = { $and: [
387
+ { id_user: ws['id_user'] },
388
+ { message_id: messageId },
389
+ { data: JSON.stringify(data) },
390
+ { date: messageDate }
391
+ ] };
392
+ if (method !== 'updateDocumentOffline' && method !== 'updateDocumentPropsOffline') {
393
+ query.$and.push({ method: method });
394
+ }
395
+ else {
396
+ if (method === 'updateDocumentOffline') {
397
+ query.$and.push({
398
+ $or: [
399
+ { method: method },
400
+ { method: 'updateDocument' }
401
+ ]
402
+ });
403
+ }
404
+ else if (method === 'updateDocumentPropsOffline') {
405
+ query.$and.push({
406
+ $or: [
407
+ { method: method },
408
+ { method: 'updateDocumentProps' }
409
+ ]
410
+ });
411
+ }
412
+ }
413
+ return [4 /*yield*/, method_call_collection_1.MethodCalls.findOne(query).exec()];
414
+ case 1:
415
+ methodCall = _e.sent();
416
+ if (!!methodCall) return [3 /*break*/, 3];
417
+ return [4 /*yield*/, method_call_collection_1.MethodCalls.create({
418
+ _id: mongoose_1.Types.ObjectId().toHexString(),
419
+ __v: 0,
420
+ id_user: ws['id_user'],
421
+ message_id: messageId,
422
+ method: method,
423
+ data: JSON.stringify(data),
424
+ date: messageDate
425
+ })];
426
+ case 2:
427
+ _e.sent();
428
+ if (this_1._methodManager._methods[method]) {
429
+ if (this_1._methodManager._methods[method].skipQueue) {
430
+ (_a = this_1._methodManager).callMethod.apply(_a, [ws, messageId, method].concat(data));
431
+ }
432
+ else {
433
+ this_1._queueManager.addMethodToQueue(ws['id_socket'], ws['id_user'], ws['user'], messageId, method, data);
434
+ }
435
+ }
436
+ else {
437
+ console.log('Could not find method: ' + method);
438
+ }
439
+ return [3 /*break*/, 4];
440
+ case 3:
441
+ method_response_collection_1.MethodResponses.findOne({ $and: [
442
+ { id_user: ws['id_user'] },
443
+ { message_id: messageId },
444
+ { method: method },
445
+ { data: JSON.stringify(data) }
446
+ ] }).exec(function (err, res) {
447
+ if (res) {
448
+ if (ws && ws.readyState === ws.OPEN) {
449
+ ws.send(JSON.stringify(res.response), function (error) {
450
+ if (error) {
451
+ console.log('Error on WS: ', error);
452
+ _this.unsubscribeWS(ws);
453
+ }
454
+ });
455
+ }
456
+ }
457
+ });
458
+ _e.label = 4;
459
+ case 4: return [3 /*break*/, 6];
460
+ case 5:
461
+ console.log('Could not find method: ' + method);
462
+ _e.label = 6;
463
+ case 6: return [3 /*break*/, 8];
464
+ case 7:
465
+ if (type === 'methodRetry') {
466
+ method = data.shift();
467
+ if (method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
468
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
469
+ log_collection_1.Logs.create({
470
+ _id: mongoose_1.Types.ObjectId().toHexString(),
471
+ date: new Date(),
472
+ type: 'client-request',
473
+ title: 'Client Request - Method Retry',
474
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
475
+ payload: data,
476
+ method: method,
477
+ user: ws['user'],
478
+ messageId: messageId
479
+ });
480
+ }
481
+ else {
482
+ log_collection_1.Logs.create({
483
+ _id: mongoose_1.Types.ObjectId().toHexString(),
484
+ date: new Date(),
485
+ type: 'client-request',
486
+ title: 'Client Request - Method Retry',
487
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
488
+ payload: 'Too Big',
489
+ method: method,
490
+ user: ws['user'],
491
+ messageId: messageId
492
+ });
493
+ }
494
+ }
495
+ else {
496
+ log_collection_1.Logs.create({
497
+ _id: mongoose_1.Types.ObjectId().toHexString(),
498
+ date: new Date(),
499
+ type: 'client-request',
500
+ title: 'Client Request - Method Retry',
501
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
502
+ payload: 'Too Big',
503
+ method: method,
504
+ user: ws['user'],
505
+ messageId: messageId
506
+ });
507
+ }
508
+ method_call_collection_1.MethodCalls.findOne({ $and: [
509
+ { id_user: ws['id_user'] },
510
+ { message_id: messageId },
511
+ { method: method },
512
+ { data: JSON.stringify(data) }
513
+ ] }).exec(function (err, res) {
514
+ if (res) {
515
+ var serverRes = {
516
+ messageId: messageId,
517
+ hasError: false,
518
+ data: 'ACK'
519
+ };
520
+ if (ws && ws.readyState === ws.OPEN) {
521
+ ws.send(JSON.stringify(serverRes), function (error) {
522
+ if (error) {
523
+ console.log('Error on WS: ', error);
524
+ _this.unsubscribeWS(ws);
525
+ }
526
+ });
527
+ }
528
+ }
529
+ else {
530
+ var serverRes = {
531
+ messageId: messageId,
532
+ hasError: false,
533
+ data: 'NACK'
534
+ };
535
+ if (ws && ws.readyState === ws.OPEN) {
536
+ ws.send(JSON.stringify(serverRes), function (error) {
537
+ if (error) {
538
+ console.log('Error on WS: ', error);
539
+ _this.unsubscribeWS(ws);
540
+ }
541
+ });
542
+ }
543
+ }
544
+ });
545
+ }
546
+ else if (type === 'methodResponse') {
547
+ method = data.shift();
548
+ // if (method !== 'generatePDF' && method !== 'getWOOfflineData' &&method !== 'countQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion') {
549
+ // if (getBinarySize(JSON.stringify(data)) < 200000) {
550
+ // Logs.create({
551
+ // _id: Types.ObjectId().toHexString(),
552
+ // date: new Date(),
553
+ // type: 'client-request',
554
+ // title: 'Client Request - Method Response',
555
+ // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
556
+ // payload: data,
557
+ // method: method,
558
+ // user: ws['user'],
559
+ // messageId: messageId
560
+ // });
561
+ // }
562
+ // else {
563
+ // Logs.create({
564
+ // _id: Types.ObjectId().toHexString(),
565
+ // date: new Date(),
566
+ // type: 'client-request',
567
+ // title: 'Client Request - Method Response',
568
+ // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
569
+ // payload: 'Too Big',
570
+ // method: method,
571
+ // user: ws['user'],
572
+ // messageId: messageId
573
+ // });
574
+ // }
575
+ // }
576
+ // else {
577
+ // Logs.create({
578
+ // _id: Types.ObjectId().toHexString(),
579
+ // date: new Date(),
580
+ // type: 'client-request',
581
+ // title: 'Client Request - Method Response',
582
+ // message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
583
+ // payload: 'Too Big',
584
+ // method: method,
585
+ // user: ws['user'],
586
+ // messageId: messageId
587
+ // });
588
+ // }
589
+ method_response_collection_1.MethodResponses.findOne({ $and: [
590
+ { id_user: ws['id_user'] },
591
+ { message_id: messageId },
592
+ { method: method },
593
+ { data: JSON.stringify(data) }
594
+ ] }).exec(function (err, res) {
595
+ if (res) {
596
+ if (ws && ws.readyState === ws.OPEN) {
597
+ ws.send(JSON.stringify(res.response), function (error) {
598
+ if (error) {
599
+ console.log('Error on WS: ', error);
600
+ _this.unsubscribeWS(ws);
601
+ }
602
+ });
603
+ }
604
+ }
605
+ });
606
+ }
607
+ // Subscription call
608
+ else if (type === 'subscription') {
609
+ subType = data.shift();
610
+ pub = data.shift();
611
+ // Subscribe
612
+ if (subType === 'sub') {
613
+ (_b = this_1._subscriptionManager).subscribe.apply(_b, [ws, messageId, pub].concat(data));
614
+ }
615
+ // Unsubscribe
616
+ else {
617
+ (_c = this_1._subscriptionManager).unsubscribe.apply(_c, [ws, messageId, pub].concat(data));
618
+ }
619
+ }
620
+ else if (type === 'support') {
621
+ supportMethod = data.shift();
622
+ // if (supportMethod !== 'countQuery' && supportMethod !== 'countCollectionWithQuery' && supportMethod !== 'find' && supportMethod !== 'findOne' && supportMethod !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'uploadFileAndSave') {
623
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
624
+ log_collection_1.Logs.create({
625
+ _id: mongoose_1.Types.ObjectId().toHexString(),
626
+ date: new Date(),
627
+ type: 'client-request',
628
+ title: 'Client Request - Support',
629
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
630
+ payload: data,
631
+ method: supportMethod,
632
+ user: ws['user'],
633
+ messageId: messageId
634
+ });
635
+ }
636
+ else {
637
+ log_collection_1.Logs.create({
638
+ _id: mongoose_1.Types.ObjectId().toHexString(),
639
+ date: new Date(),
640
+ type: 'client-request',
641
+ title: 'Client Request - Support',
642
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
643
+ payload: 'Too Big',
644
+ method: supportMethod,
645
+ user: ws['user'],
646
+ messageId: messageId
647
+ });
648
+ }
649
+ serverRes = {
650
+ messageId: messageId,
651
+ hasError: false,
652
+ data: 'ACK'
653
+ };
654
+ if (ws && ws.readyState === ws.OPEN) {
655
+ ws.send(JSON.stringify(serverRes), function (error) {
656
+ if (error) {
657
+ console.log('Error on WS: ', error);
658
+ _this.unsubscribeWS(ws);
659
+ }
660
+ });
661
+ }
662
+ if (this_1._supportManager._supportMethods[supportMethod]) {
663
+ (_d = this_1._supportManager).callSupportMethod.apply(_d, [ws, messageId, supportMethod].concat(data));
664
+ }
665
+ else {
666
+ console.log('Could not find support method: ' + supportMethod);
667
+ }
668
+ }
669
+ _e.label = 8;
670
+ case 8: return [2 /*return*/];
671
+ }
672
+ });
673
+ };
674
+ this_1 = this;
675
+ i = this._msgQueue.length;
676
+ _a.label = 1;
677
+ case 1:
678
+ if (!(i >= 0)) return [3 /*break*/, 4];
679
+ return [5 /*yield**/, _loop_1(i)];
680
+ case 2:
681
+ state_1 = _a.sent();
682
+ if (typeof state_1 === "object")
683
+ return [2 /*return*/, state_1.value];
684
+ _a.label = 3;
685
+ case 3:
686
+ i--;
687
+ return [3 /*break*/, 1];
688
+ case 4:
689
+ this._msgQueueRunning = false;
690
+ _a.label = 5;
691
+ case 5: return [2 /*return*/];
692
+ }
693
+ });
694
+ }); }, 100);
654
695
  };
655
696
  ResolveIOMainServer.prototype.unsubscribeWS = function (ws) {
656
697
  this._subscriptionManager.unsubscribeAll(ws);