badmfck-api-server 1.4.3 → 1.4.5

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.5";
47
47
  options;
48
48
  netLog = [];
49
49
  constructor(options) {
@@ -177,7 +177,7 @@ class APIService extends BaseService_1.BaseService {
177
177
  }, tme, req.path);
178
178
  });
179
179
  app.listen(this.options.port, () => {
180
- (0, LogService_1.logCrit)('API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
180
+ (0, LogService_1.logCrit)('${APIService.js}', 'API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
181
181
  });
182
182
  }
183
183
  sendResponse(res, data, requestTime, endpoint, log) {
@@ -42,11 +42,11 @@ class BaseEndpoint {
42
42
  }
43
43
  async init() {
44
44
  if (!this.endpoints) {
45
- (0, LogService_1.logInfo)("No endpoints registered for " + this.endpoint);
45
+ (0, LogService_1.logCrit)("${BaseEndpoint.js}", "No endpoints registered for " + this.endpoint);
46
46
  return;
47
47
  }
48
48
  for (let i of this.endpoints)
49
- (0, LogService_1.logInfo)("endpoint: " + i.endpoint + " initalized, ignoreInterceptor: " + (i.ignoreInterceptor ?? "false"));
49
+ (0, LogService_1.logInfo)("${BaseEndpoint.js}", "endpoint: " + i.endpoint + " initalized, ignoreInterceptor: " + (i.ignoreInterceptor ?? "false"));
50
50
  }
51
51
  ;
52
52
  async execute(req) {
@@ -56,6 +56,7 @@ class BaseEndpoint {
56
56
  return i.handler(req);
57
57
  }
58
58
  }
59
+ (0, LogService_1.logWarn)("${BaseEndpoint.js}", "Unhandled entrypoint: " + this.endpoint);
59
60
  return {
60
61
  error: DefaultErrors_1.default.NOT_IMPLEMENTED,
61
62
  data: null
@@ -19,6 +19,8 @@ export interface ILogServiceOptions {
19
19
  output?: (data?: any) => void;
20
20
  stackSize: number;
21
21
  level: LOG_LEVEL;
22
+ textLimit: number;
23
+ watchFor?: string[];
22
24
  }
23
25
  export declare const getDefaultLogOptions: () => ILogServiceOptions;
24
26
  export declare const logInfo: (message: any, ...optional: any[]) => void;
@@ -32,16 +34,25 @@ export declare const S_LOG: Signal<{
32
34
  export declare const S_LOG_CREATED: Signal<ILogItem>;
33
35
  export declare const S_LOG_CHANGE_LEVEL: Signal<LOG_LEVEL>;
34
36
  export declare const S_LOG_FLUSH: Signal<void>;
37
+ export declare const S_LOG_ADD_SERVICE_TO_WATCH: Signal<string>;
38
+ export declare const S_LOG_REMOVE_SERVICE_FROM_WATCH: Signal<string>;
39
+ export declare const S_LOG_SET_TEXT_LIMIT: Signal<number>;
35
40
  export declare const REQ_LOG: Req<{
36
41
  lastID: number;
37
42
  } | null | undefined, ILogItem[]>;
43
+ export declare const REQ_LOG_UNIQUE_SERVICES: Req<void, {
44
+ name: string;
45
+ count: number;
46
+ }[]>;
38
47
  export declare class LogService extends BaseService {
39
48
  epoch: number;
40
49
  log: ILogItem[];
41
50
  options: ILogServiceOptions;
42
51
  level: LOG_LEVEL;
52
+ uniqueServices: Map<string, number>;
43
53
  constructor(opt?: ILogServiceOptions);
44
54
  init(): Promise<void>;
55
+ checkTextLimit(): void;
45
56
  createDate(d?: Date): string;
46
57
  leadZero(num: number): string;
47
58
  createText(data: any): 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_SET_TEXT_LIMIT = 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;
@@ -37,6 +37,7 @@ var LOG_LEVEL;
37
37
  const getDefaultLogOptions = () => {
38
38
  return {
39
39
  stackSize: 1000,
40
+ textLimit: 1024,
40
41
  level: LOG_LEVEL.ALL
41
42
  };
42
43
  };
@@ -53,20 +54,32 @@ exports.S_LOG = new badmfck_signal_1.default();
53
54
  exports.S_LOG_CREATED = new badmfck_signal_1.default();
54
55
  exports.S_LOG_CHANGE_LEVEL = new badmfck_signal_1.default();
55
56
  exports.S_LOG_FLUSH = new badmfck_signal_1.default();
57
+ exports.S_LOG_ADD_SERVICE_TO_WATCH = new badmfck_signal_1.default();
58
+ exports.S_LOG_REMOVE_SERVICE_FROM_WATCH = new badmfck_signal_1.default();
59
+ exports.S_LOG_SET_TEXT_LIMIT = new badmfck_signal_1.default();
56
60
  exports.REQ_LOG = new badmfck_signal_1.Req();
61
+ exports.REQ_LOG_UNIQUE_SERVICES = new badmfck_signal_1.Req();
57
62
  class LogService extends BaseService_1.BaseService {
58
63
  epoch = 1699476234961;
59
64
  log = [];
60
65
  options;
61
66
  level = LOG_LEVEL.ALL;
67
+ uniqueServices = new Map();
62
68
  constructor(opt) {
63
69
  super("LogService");
64
70
  if (!opt)
65
71
  opt = (0, exports.getDefaultLogOptions)();
66
72
  this.options = opt;
67
73
  this.level = opt.level;
74
+ this.checkTextLimit();
68
75
  }
69
76
  async init() {
77
+ exports.REQ_LOG_UNIQUE_SERVICES.listener = async () => {
78
+ const arr = [];
79
+ for (let i of this.uniqueServices)
80
+ arr.push({ name: i[0], count: i[1] });
81
+ return arr;
82
+ };
70
83
  exports.REQ_LOG.listener = async (req) => {
71
84
  if (req && req.lastID) {
72
85
  let result = this.log.filter(val => val.id > req.lastID);
@@ -75,6 +88,31 @@ class LogService extends BaseService_1.BaseService {
75
88
  else
76
89
  return [...this.log];
77
90
  };
91
+ exports.S_LOG_SET_TEXT_LIMIT.subscribe(l => {
92
+ this.options.textLimit = l;
93
+ this.checkTextLimit();
94
+ });
95
+ exports.S_LOG_ADD_SERVICE_TO_WATCH.subscribe(service => {
96
+ if (!this.options.watchFor)
97
+ this.options.watchFor = [];
98
+ if (this.options.watchFor.find(val => val === service)) {
99
+ (0, exports.logCrit)("${LogService.js}", "Service \"" + service + "\" aleready added");
100
+ return;
101
+ }
102
+ this.options.watchFor.push(service);
103
+ });
104
+ exports.S_LOG_REMOVE_SERVICE_FROM_WATCH.subscribe(service => {
105
+ if (!this.options.watchFor)
106
+ return;
107
+ try {
108
+ const i = this.options.watchFor.findIndex(val => val === service);
109
+ if (i > -1)
110
+ this.options.watchFor.splice(i, 1);
111
+ }
112
+ catch (e) {
113
+ (0, exports.logCrit)("${LogService.js}", "Service \"" + service + "\" not deleted");
114
+ }
115
+ });
78
116
  exports.S_LOG_CHANGE_LEVEL.subscribe(l => { this.level = l; });
79
117
  exports.S_LOG_FLUSH.subscribe(() => this.log = []);
80
118
  exports.S_LOG.subscribe((log) => {
@@ -88,6 +126,20 @@ class LogService extends BaseService_1.BaseService {
88
126
  source = text.substring(2, text.length - 1);
89
127
  text = "";
90
128
  }
129
+ const usc = this.uniqueServices.get(source) ?? 0;
130
+ this.uniqueServices.set(source, usc + 1);
131
+ let showlog = true;
132
+ if (this.options && this.options.watchFor && Array.isArray(this.options.watchFor) && this.options.watchFor.length > 0) {
133
+ showlog = false;
134
+ for (let i of this.options.watchFor) {
135
+ if (source.toLowerCase().indexOf(i.toLowerCase()) !== -1) {
136
+ showlog = true;
137
+ break;
138
+ }
139
+ }
140
+ }
141
+ if (!showlog && this.level === LOG_LEVEL.CRIT)
142
+ showlog = true;
91
143
  if (optional) {
92
144
  for (let i of optional)
93
145
  text += ", " + this.createText(i);
@@ -113,6 +165,10 @@ class LogService extends BaseService_1.BaseService {
113
165
  });
114
166
  super.init();
115
167
  }
168
+ checkTextLimit() {
169
+ if (this.options.textLimit > 1024 * 4)
170
+ this.options.textLimit = 1024 * 4;
171
+ }
116
172
  createDate(d) {
117
173
  if (!d)
118
174
  d = new Date();
@@ -142,14 +198,23 @@ class LogService extends BaseService_1.BaseService {
142
198
  res = `${data}`;
143
199
  }
144
200
  }
201
+ if (res === "[object Map]" && "size" in data) {
202
+ try {
203
+ const arr = [];
204
+ for (let i of data)
205
+ arr.push({ key: i[0], value: i[1] });
206
+ res = "MAP (size:" + data.size + ") \n" + JSON.stringify(arr, null, "\t");
207
+ }
208
+ catch (e) { }
209
+ }
145
210
  if (Array.isArray(data))
146
211
  res + ", total items: " + data.length;
147
212
  }
148
213
  else {
149
214
  res = data + "";
150
215
  }
151
- if (res.length > 1024)
152
- res = res.substring(0, 1024) + "... (total: " + res.length + ")";
216
+ if (res.length > this.options.textLimit)
217
+ res = res.substring(0, this.options.textLimit) + "... (total: " + res.length + ")";
153
218
  return res;
154
219
  }
155
220
  }
@@ -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.5",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",