badmfck-api-server 1.6.0 → 1.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,7 +47,7 @@ async function Initializer(services) {
47
47
  exports.Initializer = Initializer;
48
48
  class APIService extends BaseService_1.BaseService {
49
49
  static nextLogID = 0;
50
- version = "1.6.0";
50
+ version = "1.6.2";
51
51
  options;
52
52
  monitor;
53
53
  monitorIndexFile;
@@ -252,9 +252,9 @@ class APIService extends BaseService_1.BaseService {
252
252
  log.time = data.responseTime;
253
253
  if (res.destroyed || res.closed) {
254
254
  if (log)
255
- log.error = "Connection already closed, can't send response";
255
+ log.error = "Connection already closed, can't send response for: " + data.endpoint;
256
256
  if (this.options.onError)
257
- this.options.onError("Connection already closed, can't send response", data);
257
+ this.options.onError("Connection already closed, can't send response: " + data.endpoint, data);
258
258
  if (this.monitor)
259
259
  this.monitor.registrateError(data.endpoint);
260
260
  }
@@ -1,6 +1,7 @@
1
1
  import { FieldInfo, MysqlError, Pool, PoolConnection } from "mysql";
2
2
  import { BaseService } from "./BaseService";
3
- import { Req } from "badmfck-signal";
3
+ import Signal, { Req } from "badmfck-signal";
4
+ export declare const S_MYSQL_STARTED: Signal<void>;
4
5
  export declare const REQ_MYSQL_QUERY: Req<MySqlQuery | MySqlQuery[], MysqlResult[]>;
5
6
  export declare const executeQuery: (query: MySqlQuery | MySqlQuery[]) => Promise<MysqlResult[]>;
6
7
  export interface MysqlServiceOptions {
@@ -1,14 +1,38 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = void 0;
29
+ exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
7
30
  const mysql_1 = __importDefault(require("mysql"));
8
31
  const BaseService_1 = require("./BaseService");
9
- const badmfck_signal_1 = require("badmfck-signal");
32
+ const badmfck_signal_1 = __importStar(require("badmfck-signal"));
10
33
  const crypto_1 = require("crypto");
11
34
  const LogService_1 = require("./LogService");
35
+ exports.S_MYSQL_STARTED = new badmfck_signal_1.default();
12
36
  exports.REQ_MYSQL_QUERY = new badmfck_signal_1.Req("REQ_MYSQL_QUERY");
13
37
  const executeQuery = async (query) => { return await exports.REQ_MYSQL_QUERY.request(query); };
14
38
  exports.executeQuery = executeQuery;
@@ -60,6 +84,7 @@ class MysqlService extends BaseService_1.BaseService {
60
84
  else {
61
85
  this.serviceStarted = true;
62
86
  (0, LogService_1.logInfo)("${MysqlService.js}", "Mysql Service started!");
87
+ exports.S_MYSQL_STARTED.invoke();
63
88
  }
64
89
  }
65
90
  async onApplicationReady() { }
@@ -0,0 +1,16 @@
1
+ export declare class DataProvider<T> {
2
+ private static nextID;
3
+ private name?;
4
+ private busy;
5
+ private lastRequestTime;
6
+ private cacheTime;
7
+ private data;
8
+ private callbacks;
9
+ private wasError;
10
+ private executor;
11
+ constructor(executor: () => Promise<{
12
+ data: T | null | undefined;
13
+ error?: boolean;
14
+ }>, cacheTime?: number, name?: string);
15
+ getData(): Promise<T | null | undefined>;
16
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DataProvider = void 0;
7
+ const badmfck_signal_1 = __importDefault(require("badmfck-signal"));
8
+ class DataProvider {
9
+ static nextID = 1;
10
+ name;
11
+ busy = false;
12
+ lastRequestTime = 0;
13
+ cacheTime = 1000 * 60 * 5;
14
+ data;
15
+ callbacks = new badmfck_signal_1.default();
16
+ wasError = false;
17
+ executor;
18
+ constructor(executor, cacheTime, name) {
19
+ this.executor = executor;
20
+ if (cacheTime !== undefined)
21
+ this.cacheTime = +cacheTime;
22
+ if (isNaN(this.cacheTime))
23
+ this.cacheTime = 0;
24
+ if (!this.name)
25
+ this.name = "DataProvider " + (DataProvider.nextID++);
26
+ }
27
+ async getData() {
28
+ if (this.busy)
29
+ return new Promise((resolve, reject) => this.callbacks.subscribe((d) => resolve(d)));
30
+ if (this.lastRequestTime > 0 && !this.wasError) {
31
+ const diff = (+new Date()) - this.lastRequestTime;
32
+ if (diff < this.cacheTime)
33
+ return this.data;
34
+ }
35
+ this.busy = true;
36
+ this.wasError = false;
37
+ const res = await this.executor();
38
+ this.busy = false;
39
+ this.wasError = res.error ?? false;
40
+ this.data = res.data;
41
+ this.lastRequestTime = +new Date();
42
+ this.callbacks.invoke(this.data);
43
+ this.callbacks.removeAll();
44
+ return this.data;
45
+ }
46
+ }
47
+ exports.DataProvider = DataProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.6.0",
3
+ "version": "1.6.3",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",