cgserver 6.4.2 → 6.4.4

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.
@@ -5,6 +5,7 @@ let os = require('os');
5
5
  let request = require('request');
6
6
  const _ = require("underscore");
7
7
  const crypto = require("crypto");
8
+ const Log_1 = require("../Logic/Log");
8
9
  /**
9
10
  * 常用的工具函数类
10
11
  */
@@ -564,5 +565,35 @@ class core {
564
565
  }, milliseconds);
565
566
  });
566
567
  }
568
+ static async safeCall(func, thisArg = null, ...params) {
569
+ if (!func) {
570
+ return;
571
+ }
572
+ try {
573
+ if (core.isAsyncFunc(func)) {
574
+ if (thisArg) {
575
+ await func.call(thisArg, params).catch((reason) => {
576
+ Log_1.GLog.error(reason);
577
+ });
578
+ }
579
+ else {
580
+ await func(params).catch((reason) => {
581
+ Log_1.GLog.error(reason);
582
+ });
583
+ }
584
+ }
585
+ else {
586
+ if (thisArg) {
587
+ func.call(thisArg, params);
588
+ }
589
+ else {
590
+ func(params);
591
+ }
592
+ }
593
+ }
594
+ catch (e) {
595
+ Log_1.GLog.error(e.stack);
596
+ }
597
+ }
567
598
  }
568
599
  exports.core = core;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GAsyncQueueTool = void 0;
4
4
  const cgserver_1 = require("../cgserver");
5
- const Log_1 = require("./Log");
6
5
  class AsyncQueueItem {
7
6
  key = "";
8
7
  running = false;
@@ -10,9 +9,11 @@ class AsyncQueueItem {
10
9
  constructor(key) {
11
10
  this.key = key;
12
11
  }
13
- add(func, cb) {
14
- this.funcs.push({ func: func, cb: cb });
15
- this._run();
12
+ async add(func, thisArg = null, ...params) {
13
+ return new Promise((resolve, reject) => {
14
+ this.funcs.push({ func: func, thisArg: thisArg, params: params, resolve: resolve });
15
+ this._run();
16
+ });
16
17
  }
17
18
  async _run() {
18
19
  if (this.running) {
@@ -20,29 +21,12 @@ class AsyncQueueItem {
20
21
  }
21
22
  this.running = true;
22
23
  while (this.funcs.length > 0) {
23
- try {
24
- let funcitem = this.funcs.shift();
25
- if (!funcitem.func) {
26
- continue;
27
- }
28
- if (cgserver_1.core.isAsyncFunc(funcitem.func)) {
29
- await funcitem.func().catch((error) => {
30
- Log_1.GLog.error(error);
31
- });
32
- if (funcitem.cb) {
33
- funcitem.cb();
34
- }
35
- }
36
- else {
37
- funcitem.func();
38
- if (funcitem.cb) {
39
- funcitem.cb();
40
- }
41
- }
42
- }
43
- catch (error) {
44
- Log_1.GLog.error(error);
24
+ let funcitem = this.funcs.shift();
25
+ if (!funcitem.func) {
26
+ continue;
45
27
  }
28
+ await cgserver_1.core.safeCall(funcitem.func, funcitem.thisArg, funcitem.params);
29
+ await cgserver_1.core.safeCall(funcitem.resolve);
46
30
  }
47
31
  this.running = false;
48
32
  }
@@ -0,0 +1,2 @@
1
+ class HelpTool {
2
+ }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GLog = void 0;
4
- const Core_1 = require("../Core/Core");
5
4
  const colors = require("colors");
6
5
  const log4js = require("log4js");
7
6
  /**
@@ -40,29 +39,29 @@ class Log {
40
39
  this._errorLogger = log4js.getLogger(cfg.categories.default.appenders[2] || "error_log_file");
41
40
  }
42
41
  error(message, ...optionalParams) {
43
- if (Core_1.core.isObject(message)) {
42
+ if (this.isObject(message)) {
44
43
  message = JSON.stringify(message);
45
44
  }
46
45
  this._errorLogger.error(message);
47
46
  var time = new Date();
48
- var time_str = Core_1.core.format(time, "[YYYY-MM-DD HH:mm:SS.");
47
+ var time_str = this.format(time, "[YYYY-MM-DD HH:mm:SS.");
49
48
  time_str += time.getMilliseconds() + "]";
50
49
  console.error(time_str + " " + message);
51
50
  }
52
51
  info(message, to_console) {
53
- if (Core_1.core.isObject(message)) {
52
+ if (this.isObject(message)) {
54
53
  message = JSON.stringify(message);
55
54
  }
56
55
  this._logger.info(message);
57
56
  if (to_console) {
58
57
  var time = new Date();
59
- var time_str = Core_1.core.format(time, "[YYYY-MM-DD HH:mm:SS.");
58
+ var time_str = this.format(time, "[YYYY-MM-DD HH:mm:SS.");
60
59
  time_str += time.getMilliseconds() + "]";
61
60
  console.log(time_str + " " + message);
62
61
  }
63
62
  }
64
63
  warn(message, ...optionalParams) {
65
- if (Core_1.core.isObject(message)) {
64
+ if (this.isObject(message)) {
66
65
  message = JSON.stringify(message);
67
66
  }
68
67
  this._errorLogger.warn(message);
@@ -73,5 +72,47 @@ class Log {
73
72
  clientLog(message, ...optionalParams) {
74
73
  this._client_logger.error(message);
75
74
  }
75
+ isObject(param) {
76
+ return typeof (param) === "object";
77
+ }
78
+ isString(param) {
79
+ return typeof (param) === "string";
80
+ }
81
+ format = function (src, formatStr) {
82
+ if (this.isString(src)) {
83
+ let args = Array.prototype.slice.call(arguments, 1);
84
+ return src.replace(/\{(\d+)\}/g, function (m, i) {
85
+ return args[i];
86
+ });
87
+ }
88
+ else {
89
+ if (this.isNumber(src)) {
90
+ src = new Date(src);
91
+ }
92
+ let str = formatStr;
93
+ let Week = ['日', '一', '二', '三', '四', '五', '六'];
94
+ let month = src.getMonth() + 1;
95
+ let year = src.getFullYear();
96
+ let date = src.getDate();
97
+ let hour = src.getHours();
98
+ let min = src.getMinutes();
99
+ let sec = src.getSeconds();
100
+ let day = src.getDay();
101
+ str = str.replace(/yyyy|YYYY/, year);
102
+ str = str.replace(/yy|YY/, (year % 100) > 9 ? (year % 100).toString() : '0' + (year % 100));
103
+ str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
104
+ str = str.replace(/M/g, month);
105
+ str = str.replace(/w|W/g, Week[day]);
106
+ str = str.replace(/dd|DD/, date > 9 ? date.toString() : '0' + date);
107
+ str = str.replace(/d|D/g, date);
108
+ str = str.replace(/hh|HH/, hour > 9 ? hour.toString() : '0' + hour);
109
+ str = str.replace(/h|H/g, hour);
110
+ str = str.replace(/mm/, min > 9 ? min.toString() : '0' + min);
111
+ str = str.replace(/m/g, min);
112
+ str = str.replace(/ss|SS/, sec > 9 ? sec.toString() : '0' + sec);
113
+ str = str.replace(/s|S/g, sec);
114
+ return str;
115
+ }
116
+ };
76
117
  }
77
118
  exports.GLog = new Log();
@@ -100,4 +100,5 @@ export declare class core {
100
100
  */
101
101
  static convertToGlobalStr(num: number): string;
102
102
  static sleep(milliseconds: any): Promise<unknown>;
103
+ static safeCall(func: Function, thisArg?: any, ...params: any[]): Promise<void>;
103
104
  }
@@ -3,10 +3,12 @@ declare class AsyncQueueItem {
3
3
  running: boolean;
4
4
  funcs: {
5
5
  func: Function;
6
- cb?: Function;
6
+ thisArg: any;
7
+ params: any[];
8
+ resolve?: Function;
7
9
  }[];
8
10
  constructor(key: string);
9
- add(func: Function, cb?: Function): void;
11
+ add(func: Function, thisArg?: any, ...params: any[]): Promise<unknown>;
10
12
  protected _run(): Promise<void>;
11
13
  }
12
14
  declare class AsyncQueueTool {
@@ -0,0 +1,2 @@
1
+ declare class HelpTool {
2
+ }
@@ -11,5 +11,8 @@ declare class Log {
11
11
  warn(message?: any, ...optionalParams: any[]): void;
12
12
  record(message?: any, ...optionalParams: any[]): void;
13
13
  clientLog(message?: any, ...optionalParams: any[]): void;
14
+ isObject(param: any): boolean;
15
+ isString(param: any): boolean;
16
+ format: (src: any, formatStr: any) => any;
14
17
  }
15
18
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "6.4.2",
3
+ "version": "6.4.4",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",