badmfck-api-server 1.4.3 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,10 +21,10 @@ function getDefaultOptions() {
21
21
  endpoints: [],
22
22
  jsonLimit: "10mb",
23
23
  onNetworkLog: function (log) {
24
- (0, LogService_1.logInfo)(log);
24
+ (0, LogService_1.logInfo)("${ApiService.js}", log);
25
25
  },
26
26
  onError: function (err) {
27
- (0, LogService_1.logError)(err);
27
+ (0, LogService_1.logError)("${ApiService.js}", err);
28
28
  },
29
29
  isProductionEnvironment: false
30
30
  };
@@ -43,7 +43,7 @@ async function Initializer(services) {
43
43
  exports.Initializer = Initializer;
44
44
  class APIService extends BaseService_1.BaseService {
45
45
  static nextLogID = 0;
46
- version = "1.4.3";
46
+ version = "1.4.4";
47
47
  options;
48
48
  netLog = [];
49
49
  constructor(options) {
@@ -19,6 +19,7 @@ export interface ILogServiceOptions {
19
19
  output?: (data?: any) => void;
20
20
  stackSize: number;
21
21
  level: LOG_LEVEL;
22
+ watchFor?: string[];
22
23
  }
23
24
  export declare const getDefaultLogOptions: () => ILogServiceOptions;
24
25
  export declare const logInfo: (message: any, ...optional: any[]) => void;
@@ -32,14 +33,21 @@ export declare const S_LOG: Signal<{
32
33
  export declare const S_LOG_CREATED: Signal<ILogItem>;
33
34
  export declare const S_LOG_CHANGE_LEVEL: Signal<LOG_LEVEL>;
34
35
  export declare const S_LOG_FLUSH: Signal<void>;
36
+ export declare const S_LOG_ADD_SERVICE_TO_WATCH: Signal<string>;
37
+ export declare const S_LOG_REMOVE_SERVICE_FROM_WATCH: Signal<string>;
35
38
  export declare const REQ_LOG: Req<{
36
39
  lastID: number;
37
40
  } | null | undefined, ILogItem[]>;
41
+ export declare const REQ_LOG_UNIQUE_SERVICES: Req<void, {
42
+ name: string;
43
+ count: number;
44
+ }[]>;
38
45
  export declare class LogService extends BaseService {
39
46
  epoch: number;
40
47
  log: ILogItem[];
41
48
  options: ILogServiceOptions;
42
49
  level: LOG_LEVEL;
50
+ uniqueServices: Map<string, number>;
43
51
  constructor(opt?: ILogServiceOptions);
44
52
  init(): Promise<void>;
45
53
  createDate(d?: Date): string;
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.LogService = exports.REQ_LOG = exports.S_LOG_FLUSH = exports.S_LOG_CHANGE_LEVEL = exports.S_LOG_CREATED = exports.S_LOG = exports.logError = exports.logCrit = exports.logWarn = exports.logInfo = exports.getDefaultLogOptions = exports.LOG_LEVEL = void 0;
26
+ exports.LogService = exports.REQ_LOG_UNIQUE_SERVICES = exports.REQ_LOG = exports.S_LOG_REMOVE_SERVICE_FROM_WATCH = exports.S_LOG_ADD_SERVICE_TO_WATCH = exports.S_LOG_FLUSH = exports.S_LOG_CHANGE_LEVEL = exports.S_LOG_CREATED = exports.S_LOG = exports.logError = exports.logCrit = exports.logWarn = exports.logInfo = exports.getDefaultLogOptions = exports.LOG_LEVEL = void 0;
27
27
  const badmfck_signal_1 = __importStar(require("badmfck-signal"));
28
28
  const BaseService_1 = require("./BaseService");
29
29
  var LOG_LEVEL;
@@ -53,12 +53,16 @@ exports.S_LOG = new badmfck_signal_1.default();
53
53
  exports.S_LOG_CREATED = new badmfck_signal_1.default();
54
54
  exports.S_LOG_CHANGE_LEVEL = new badmfck_signal_1.default();
55
55
  exports.S_LOG_FLUSH = new badmfck_signal_1.default();
56
+ exports.S_LOG_ADD_SERVICE_TO_WATCH = new badmfck_signal_1.default();
57
+ exports.S_LOG_REMOVE_SERVICE_FROM_WATCH = new badmfck_signal_1.default();
56
58
  exports.REQ_LOG = new badmfck_signal_1.Req();
59
+ exports.REQ_LOG_UNIQUE_SERVICES = new badmfck_signal_1.Req();
57
60
  class LogService extends BaseService_1.BaseService {
58
61
  epoch = 1699476234961;
59
62
  log = [];
60
63
  options;
61
64
  level = LOG_LEVEL.ALL;
65
+ uniqueServices = new Map();
62
66
  constructor(opt) {
63
67
  super("LogService");
64
68
  if (!opt)
@@ -67,6 +71,12 @@ class LogService extends BaseService_1.BaseService {
67
71
  this.level = opt.level;
68
72
  }
69
73
  async init() {
74
+ exports.REQ_LOG_UNIQUE_SERVICES.listener = async () => {
75
+ const arr = [];
76
+ for (let i of this.uniqueServices)
77
+ arr.push({ name: i[0], count: i[1] });
78
+ return arr;
79
+ };
70
80
  exports.REQ_LOG.listener = async (req) => {
71
81
  if (req && req.lastID) {
72
82
  let result = this.log.filter(val => val.id > req.lastID);
@@ -75,6 +85,27 @@ class LogService extends BaseService_1.BaseService {
75
85
  else
76
86
  return [...this.log];
77
87
  };
88
+ exports.S_LOG_ADD_SERVICE_TO_WATCH.subscribe(service => {
89
+ if (!this.options.watchFor)
90
+ this.options.watchFor = [];
91
+ if (this.options.watchFor.find(val => val === service)) {
92
+ (0, exports.logCrit)("${LogService.js}", "Service \"" + service + "\" aleready added");
93
+ return;
94
+ }
95
+ this.options.watchFor.push(service);
96
+ });
97
+ exports.S_LOG_REMOVE_SERVICE_FROM_WATCH.subscribe(service => {
98
+ if (!this.options.watchFor)
99
+ return;
100
+ try {
101
+ const i = this.options.watchFor.findIndex(val => val === service);
102
+ if (i > -1)
103
+ this.options.watchFor.splice(i, 1);
104
+ }
105
+ catch (e) {
106
+ (0, exports.logCrit)("${LogService.js}", "Service \"" + service + "\" not deleted");
107
+ }
108
+ });
78
109
  exports.S_LOG_CHANGE_LEVEL.subscribe(l => { this.level = l; });
79
110
  exports.S_LOG_FLUSH.subscribe(() => this.log = []);
80
111
  exports.S_LOG.subscribe((log) => {
@@ -88,6 +119,20 @@ class LogService extends BaseService_1.BaseService {
88
119
  source = text.substring(2, text.length - 1);
89
120
  text = "";
90
121
  }
122
+ const usc = this.uniqueServices.get(source) ?? 0;
123
+ this.uniqueServices.set(source, usc + 1);
124
+ let showlog = true;
125
+ if (this.options && this.options.watchFor && Array.isArray(this.options.watchFor) && this.options.watchFor.length > 0) {
126
+ showlog = false;
127
+ for (let i of this.options.watchFor) {
128
+ if (source.toLowerCase().indexOf(i.toLowerCase()) !== -1) {
129
+ showlog = true;
130
+ break;
131
+ }
132
+ }
133
+ }
134
+ if (!showlog && this.level === LOG_LEVEL.CRIT)
135
+ showlog = true;
91
136
  if (optional) {
92
137
  for (let i of optional)
93
138
  text += ", " + this.createText(i);
@@ -142,6 +187,15 @@ class LogService extends BaseService_1.BaseService {
142
187
  res = `${data}`;
143
188
  }
144
189
  }
190
+ if (res === "[object Map]" && "size" in data) {
191
+ try {
192
+ const arr = [];
193
+ for (let i of data)
194
+ arr.push({ key: i[0], value: i[1] });
195
+ res = "MAP (size:" + data.size + ") \n" + JSON.stringify(arr, null, "\t");
196
+ }
197
+ catch (e) { }
198
+ }
145
199
  if (Array.isArray(data))
146
200
  res + ", total items: " + data.length;
147
201
  }
@@ -48,18 +48,18 @@ class MysqlService extends BaseService_1.BaseService {
48
48
  };
49
49
  }
50
50
  async recreatePool() {
51
- (0, LogService_1.logInfo)("Mysql server trying to create pool");
51
+ (0, LogService_1.logInfo)("${MysqlService.js}", "Mysql server trying to create pool");
52
52
  this.serviceStarted = false;
53
53
  const ok = await this.createPool();
54
54
  if (!ok) {
55
- (0, LogService_1.logWarn)("Mysql server not connected, retrying in 3 sec");
55
+ (0, LogService_1.logWarn)("${MysqlService.js}", "Mysql server not connected, retrying in 3 sec");
56
56
  if (this.timeoutID)
57
57
  clearTimeout(this.timeoutID);
58
58
  this.timeoutID = setTimeout(() => { this.recreatePool(); }, 3000);
59
59
  }
60
60
  else {
61
61
  this.serviceStarted = true;
62
- (0, LogService_1.logInfo)("Mysql Service started!");
62
+ (0, LogService_1.logInfo)("${MysqlService.js}", "Mysql Service started!");
63
63
  }
64
64
  }
65
65
  async onApplicationReady() { }
@@ -132,7 +132,7 @@ class MysqlService extends BaseService_1.BaseService {
132
132
  async execute(query) {
133
133
  return new Promise((resolve, reject) => {
134
134
  if (!this.pool) {
135
- (0, LogService_1.logError)("No pool");
135
+ (0, LogService_1.logError)("${MysqlService.js}", "No pool");
136
136
  resolve({
137
137
  error: {
138
138
  code: "NO_POOL",
@@ -149,7 +149,7 @@ class MysqlService extends BaseService_1.BaseService {
149
149
  }
150
150
  this.pool.getConnection((err, conn) => {
151
151
  if (err) {
152
- (0, LogService_1.logError)(err);
152
+ (0, LogService_1.logError)("${MysqlService.js}", err);
153
153
  if (`${err}`.indexOf('ECONNREFUSED') !== -1) {
154
154
  this.recreatePool();
155
155
  }
@@ -161,7 +161,7 @@ class MysqlService extends BaseService_1.BaseService {
161
161
  return;
162
162
  }
163
163
  if (!conn) {
164
- (0, LogService_1.logCrit)(`No connection created!`);
164
+ (0, LogService_1.logCrit)("${MysqlService.js}", `No connection created!`);
165
165
  resolve({
166
166
  error: {
167
167
  code: "NO_CONN",
@@ -184,7 +184,7 @@ class MysqlService extends BaseService_1.BaseService {
184
184
  conn.release();
185
185
  }
186
186
  catch (e) { }
187
- (0, LogService_1.logError)("QUERY_ERR: " + e);
187
+ (0, LogService_1.logError)("${MysqlService.js}", "QUERY_ERR: " + e);
188
188
  resolve({
189
189
  error: {
190
190
  code: "QUERY_ERR",
@@ -207,7 +207,7 @@ class MysqlService extends BaseService_1.BaseService {
207
207
  errCatched = true;
208
208
  conn.release();
209
209
  conn.removeAllListeners();
210
- (0, LogService_1.logError)(err);
210
+ (0, LogService_1.logError)("${MysqlService.js}", err);
211
211
  conn.removeAllListeners();
212
212
  resolve({
213
213
  error: {
@@ -222,7 +222,7 @@ class MysqlService extends BaseService_1.BaseService {
222
222
  fields: null
223
223
  });
224
224
  });
225
- (0, LogService_1.logInfo)(query);
225
+ (0, LogService_1.logInfo)("${MysqlService.js}", query);
226
226
  conn.query(query, (err, results, fields) => {
227
227
  conn.release();
228
228
  conn.removeAllListeners();
@@ -230,7 +230,7 @@ class MysqlService extends BaseService_1.BaseService {
230
230
  return;
231
231
  if (err) {
232
232
  const dup = `${err}`.toLowerCase().indexOf("er_dup_entry") !== -1;
233
- (0, LogService_1.logError)(err);
233
+ (0, LogService_1.logError)("${MysqlService.js}", err);
234
234
  resolve({
235
235
  error: err,
236
236
  data: null,
@@ -248,18 +248,18 @@ class MysqlService extends BaseService_1.BaseService {
248
248
  });
249
249
  }
250
250
  async createPool() {
251
- (0, LogService_1.logInfo)("Connecting to mysql: \n HOST: " + this.options.host + '\n PORT:' + this.options.port);
251
+ (0, LogService_1.logInfo)("${MysqlService.js}", "Connecting to mysql: \n HOST: " + this.options.host + '\n PORT:' + this.options.port);
252
252
  let err = false;
253
253
  if (this.pool) {
254
254
  try {
255
255
  this.pool.removeAllListeners();
256
256
  this.pool.end(err => {
257
257
  if (err)
258
- (0, LogService_1.logError)(err);
258
+ (0, LogService_1.logError)("${MysqlService.js}", err);
259
259
  });
260
260
  }
261
261
  catch (e) {
262
- (0, LogService_1.logCrit)(e);
262
+ (0, LogService_1.logCrit)("${MysqlService.js}", e);
263
263
  }
264
264
  }
265
265
  try {
@@ -274,12 +274,12 @@ class MysqlService extends BaseService_1.BaseService {
274
274
  });
275
275
  }
276
276
  catch (e) {
277
- (0, LogService_1.logCrit)("Can't connect to MYSQL!");
277
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't connect to MYSQL!");
278
278
  err = true;
279
279
  }
280
280
  if (!err && this.pool) {
281
281
  this.pool.on("error", (evt) => {
282
- (0, LogService_1.logError)(evt);
282
+ (0, LogService_1.logError)("${MysqlService.js}", evt);
283
283
  });
284
284
  }
285
285
  return new Promise((res, rej) => {
@@ -293,7 +293,7 @@ class MysqlService extends BaseService_1.BaseService {
293
293
  }
294
294
  this.pool.getConnection((e, cnn) => {
295
295
  if (e) {
296
- (0, LogService_1.logError)(e.message);
296
+ (0, LogService_1.logError)("${MysqlService.js}", e.message);
297
297
  res(false);
298
298
  return;
299
299
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",