@bmfk/api 0.0.0-stage → 1.0.1
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.
- package/CHANGELOG.md +90 -0
- package/LICENSE +21 -0
- package/README.md +1293 -2
- package/dist/apiServer/APIService.d.ts +125 -0
- package/dist/apiServer/APIService.js +769 -0
- package/dist/apiServer/BaseEndpoint.d.ts +90 -0
- package/dist/apiServer/BaseEndpoint.js +314 -0
- package/dist/apiServer/BaseService.d.ts +17 -0
- package/dist/apiServer/BaseService.js +39 -0
- package/dist/apiServer/DBService.d.ts +131 -0
- package/dist/apiServer/DBService.js +290 -0
- package/dist/apiServer/DocumentService.d.ts +9 -0
- package/dist/apiServer/DocumentService.js +65 -0
- package/dist/apiServer/LocalRequest.d.ts +3 -0
- package/dist/apiServer/LocalRequest.js +6 -0
- package/dist/apiServer/LogService.d.ts +78 -0
- package/dist/apiServer/LogService.js +349 -0
- package/dist/apiServer/MonitorService.d.ts +102 -0
- package/dist/apiServer/MonitorService.js +615 -0
- package/dist/apiServer/StatService.d.ts +4 -0
- package/dist/apiServer/StatService.js +10 -0
- package/dist/apiServer/TimeframeService.d.ts +28 -0
- package/dist/apiServer/TimeframeService.js +97 -0
- package/dist/apiServer/WebSocketServer.d.ts +39 -0
- package/dist/apiServer/WebSocketServer.js +173 -0
- package/dist/apiServer/db/IDBAdapter.d.ts +14 -0
- package/dist/apiServer/db/IDBAdapter.js +2 -0
- package/dist/apiServer/db/MysqlAdapter.d.ts +72 -0
- package/dist/apiServer/db/MysqlAdapter.js +869 -0
- package/dist/apiServer/db/PostgresAdapter.d.ts +41 -0
- package/dist/apiServer/db/PostgresAdapter.js +73 -0
- package/dist/apiServer/deployment/AccessConfigWizard.d.ts +5 -0
- package/dist/apiServer/deployment/AccessConfigWizard.js +254 -0
- package/dist/apiServer/deployment/Activate.d.ts +4 -0
- package/dist/apiServer/deployment/Activate.js +124 -0
- package/dist/apiServer/deployment/BackupManager.d.ts +33 -0
- package/dist/apiServer/deployment/BackupManager.js +174 -0
- package/dist/apiServer/deployment/Backups.d.ts +2 -0
- package/dist/apiServer/deployment/Backups.js +44 -0
- package/dist/apiServer/deployment/BlueGreenState.d.ts +31 -0
- package/dist/apiServer/deployment/BlueGreenState.js +150 -0
- package/dist/apiServer/deployment/Bundler.d.ts +12 -0
- package/dist/apiServer/deployment/Bundler.js +178 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +203 -0
- package/dist/apiServer/deployment/ConfigService.js +195 -0
- package/dist/apiServer/deployment/Deploy.d.ts +4 -0
- package/dist/apiServer/deployment/Deploy.js +404 -0
- package/dist/apiServer/deployment/DeployKeyWizard.d.ts +2 -0
- package/dist/apiServer/deployment/DeployKeyWizard.js +84 -0
- package/dist/apiServer/deployment/Deployer.d.ts +13 -0
- package/dist/apiServer/deployment/Deployer.js +96 -0
- package/dist/apiServer/deployment/DeployerInit.d.ts +25 -0
- package/dist/apiServer/deployment/DeployerInit.js +214 -0
- package/dist/apiServer/deployment/DeployerService.d.ts +378 -0
- package/dist/apiServer/deployment/DeployerService.js +1553 -0
- package/dist/apiServer/deployment/HashPassword.d.ts +3 -0
- package/dist/apiServer/deployment/HashPassword.js +126 -0
- package/dist/apiServer/deployment/MigrateConfigsWizard.d.ts +1 -0
- package/dist/apiServer/deployment/MigrateConfigsWizard.js +202 -0
- package/dist/apiServer/deployment/MigrateProject.d.ts +31 -0
- package/dist/apiServer/deployment/MigrateProject.js +335 -0
- package/dist/apiServer/deployment/NginxHelper.d.ts +10 -0
- package/dist/apiServer/deployment/NginxHelper.js +111 -0
- package/dist/apiServer/deployment/Notifier.d.ts +30 -0
- package/dist/apiServer/deployment/Notifier.js +82 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.js +148 -0
- package/dist/apiServer/deployment/Restore.d.ts +2 -0
- package/dist/apiServer/deployment/Restore.js +89 -0
- package/dist/apiServer/deployment/ReviewStore.d.ts +62 -0
- package/dist/apiServer/deployment/ReviewStore.js +186 -0
- package/dist/apiServer/deployment/SecretsInjector.d.ts +21 -0
- package/dist/apiServer/deployment/SecretsInjector.js +189 -0
- package/dist/apiServer/deployment/SetupConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/SetupConfigWizard.js +238 -0
- package/dist/apiServer/deployment/Status.d.ts +2 -0
- package/dist/apiServer/deployment/Status.js +83 -0
- package/dist/apiServer/deployment/TelegramNotifier.d.ts +8 -0
- package/dist/apiServer/deployment/TelegramNotifier.js +23 -0
- package/dist/apiServer/deployment/Watchdog.d.ts +15 -0
- package/dist/apiServer/deployment/Watchdog.js +65 -0
- package/dist/apiServer/deployment/Wizards.d.ts +1 -0
- package/dist/apiServer/deployment/Wizards.js +83 -0
- package/dist/apiServer/deployment/cli.d.ts +2 -0
- package/dist/apiServer/deployment/cli.js +224 -0
- package/dist/apiServer/deployment/cliCommon.d.ts +27 -0
- package/dist/apiServer/deployment/cliCommon.js +110 -0
- package/dist/apiServer/deployment/cliPrompt.d.ts +5 -0
- package/dist/apiServer/deployment/cliPrompt.js +38 -0
- package/dist/apiServer/deployment/resolveConfig.d.ts +19 -0
- package/dist/apiServer/deployment/resolveConfig.js +139 -0
- package/dist/apiServer/documentation/Documentation.d.ts +18 -0
- package/dist/apiServer/documentation/Documentation.js +143 -0
- package/dist/apiServer/documentation/index.html +460 -0
- package/dist/apiServer/external/MicroserviceClient.d.ts +28 -0
- package/dist/apiServer/external/MicroserviceClient.js +144 -0
- package/dist/apiServer/external/MicroserviceHost.d.ts +52 -0
- package/dist/apiServer/external/MicroserviceHost.js +183 -0
- package/dist/apiServer/external/MicroserviceHostController.d.ts +6 -0
- package/dist/apiServer/external/MicroserviceHostController.js +65 -0
- package/dist/apiServer/helper/AppDataPath.d.ts +9 -0
- package/dist/apiServer/helper/AppDataPath.js +81 -0
- package/dist/apiServer/helper/ClientIp.d.ts +3 -0
- package/dist/apiServer/helper/ClientIp.js +33 -0
- package/dist/apiServer/helper/DataProvider.d.ts +18 -0
- package/dist/apiServer/helper/DataProvider.js +57 -0
- package/dist/apiServer/helper/IpAllowList.d.ts +8 -0
- package/dist/apiServer/helper/IpAllowList.js +40 -0
- package/dist/apiServer/helper/JSONStableStringify.d.ts +1 -0
- package/dist/apiServer/helper/JSONStableStringify.js +15 -0
- package/dist/apiServer/helper/PasswordHash.d.ts +5 -0
- package/dist/apiServer/helper/PasswordHash.js +52 -0
- package/dist/apiServer/helper/Sleep.d.ts +1 -0
- package/dist/apiServer/helper/Sleep.js +5 -0
- package/dist/apiServer/helper/Trace.d.ts +8 -0
- package/dist/apiServer/helper/Trace.js +30 -0
- package/dist/apiServer/helper/UID.d.ts +16 -0
- package/dist/apiServer/helper/UID.js +98 -0
- package/dist/apiServer/helper/Validator.d.ts +86 -0
- package/dist/apiServer/helper/Validator.js +560 -0
- package/dist/apiServer/helper/YYYYMMDDHH.d.ts +2 -0
- package/dist/apiServer/helper/YYYYMMDDHH.js +31 -0
- package/dist/apiServer/helper/ZipUtils.d.ts +6 -0
- package/dist/apiServer/helper/ZipUtils.js +28 -0
- package/dist/apiServer/http/Http.d.ts +183 -0
- package/dist/apiServer/http/Http.js +627 -0
- package/dist/apiServer/interceptors/InterceptorPayAuth.d.ts +19 -0
- package/dist/apiServer/interceptors/InterceptorPayAuth.js +124 -0
- package/dist/apiServer/interceptors/InterceptorSignedAuth.d.ts +40 -0
- package/dist/apiServer/interceptors/InterceptorSignedAuth.js +150 -0
- package/dist/apiServer/monitor/AccessStore.d.ts +27 -0
- package/dist/apiServer/monitor/AccessStore.js +163 -0
- package/dist/apiServer/monitor/LogFiles.d.ts +34 -0
- package/dist/apiServer/monitor/LogFiles.js +109 -0
- package/dist/apiServer/monitor/Monitor.d.ts +54 -0
- package/dist/apiServer/monitor/Monitor.js +447 -0
- package/dist/apiServer/monitor/MonitorAuth.d.ts +87 -0
- package/dist/apiServer/monitor/MonitorAuth.js +474 -0
- package/dist/apiServer/monitor/ProcessMonitor.d.ts +177 -0
- package/dist/apiServer/monitor/ProcessMonitor.js +468 -0
- package/dist/apiServer/monitor/SystemInfo.d.ts +70 -0
- package/dist/apiServer/monitor/SystemInfo.js +217 -0
- package/dist/apiServer/monitor/ThreatMonitor.d.ts +73 -0
- package/dist/apiServer/monitor/ThreatMonitor.js +239 -0
- package/dist/apiServer/monitor/index.html +591 -0
- package/dist/apiServer/review/Review.d.ts +23 -0
- package/dist/apiServer/review/Review.js +149 -0
- package/dist/apiServer/review/ReviewAuth.d.ts +56 -0
- package/dist/apiServer/review/ReviewAuth.js +147 -0
- package/dist/apiServer/review/ReviewDiff.d.ts +43 -0
- package/dist/apiServer/review/ReviewDiff.js +175 -0
- package/dist/apiServer/review/index.html +501 -0
- package/dist/apiServer/routes/Liveness.d.ts +7 -0
- package/dist/apiServer/routes/Liveness.js +27 -0
- package/dist/apiServer/routes/Readiness.d.ts +13 -0
- package/dist/apiServer/routes/Readiness.js +20 -0
- package/dist/apiServer/security/BlockedIps.d.ts +46 -0
- package/dist/apiServer/security/BlockedIps.js +185 -0
- package/dist/apiServer/structures/DefaultErrors.d.ts +36 -0
- package/dist/apiServer/structures/DefaultErrors.js +52 -0
- package/dist/apiServer/structures/HTTPStatus.d.ts +7 -0
- package/dist/apiServer/structures/HTTPStatus.js +11 -0
- package/dist/apiServer/structures/Interfaces.d.ts +55 -0
- package/dist/apiServer/structures/Interfaces.js +2 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +110 -0
- package/dist/signal/Signal.d.ts +23 -0
- package/dist/signal/Signal.js +85 -0
- package/package.json +88 -4
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TimeframeService = exports.S_TIMEFRAME_TASK_EXECUTED = exports.REQ_TIMEFRAME_TASK_ADD = void 0;
|
|
4
|
+
const Signal_1 = require("../signal/Signal");
|
|
5
|
+
const BaseService_1 = require("./BaseService");
|
|
6
|
+
const LogService_1 = require("./LogService");
|
|
7
|
+
exports.REQ_TIMEFRAME_TASK_ADD = new Signal_1.Req(undefined, "REQ_TIMEFRAME_TASK_ADD");
|
|
8
|
+
exports.S_TIMEFRAME_TASK_EXECUTED = new Signal_1.Signal("S_TIMEFRAME_TASK_EXECUTED");
|
|
9
|
+
class TimeframeService extends BaseService_1.BaseService {
|
|
10
|
+
TICK_INTERVAL_MS = 1000;
|
|
11
|
+
RETRY_DELAY_MS = 10_000;
|
|
12
|
+
GRACE_WINDOW_MS = 30_000;
|
|
13
|
+
tasks = [];
|
|
14
|
+
intervalId = 0;
|
|
15
|
+
static async wait(ms) {
|
|
16
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
17
|
+
}
|
|
18
|
+
constructor() {
|
|
19
|
+
super("TimeframeService");
|
|
20
|
+
}
|
|
21
|
+
async init() {
|
|
22
|
+
exports.REQ_TIMEFRAME_TASK_ADD.listener = async (req) => {
|
|
23
|
+
(0, LogService_1.logInfo)("Add task " + req.name);
|
|
24
|
+
this.tasks.push({
|
|
25
|
+
...req,
|
|
26
|
+
executed: 0,
|
|
27
|
+
retries: 0,
|
|
28
|
+
processing: false
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
this.intervalId = setInterval(() => {
|
|
32
|
+
this.loop().catch(e => (0, LogService_1.logError)("Timeframe loop failed:", e));
|
|
33
|
+
}, this.TICK_INTERVAL_MS);
|
|
34
|
+
}
|
|
35
|
+
async loop() {
|
|
36
|
+
const now = Date.now();
|
|
37
|
+
for (let i = 0; i < this.tasks.length; i++) {
|
|
38
|
+
const task = this.tasks[i];
|
|
39
|
+
if (!task)
|
|
40
|
+
continue;
|
|
41
|
+
if (task.repeat && !task.processing && now > (task.start + this.GRACE_WINDOW_MS)) {
|
|
42
|
+
const diff = now - task.start;
|
|
43
|
+
const missedIntervals = Math.ceil(diff / task.repeat);
|
|
44
|
+
task.start += missedIntervals * task.repeat;
|
|
45
|
+
(0, LogService_1.logInfo)(`Task ${task.name} skipped and rescheduled to ${new Date(task.start).toLocaleTimeString()}`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const isReady = now >= task.start;
|
|
49
|
+
const isNotExecutedYet = !task.executed || task.executed < task.start;
|
|
50
|
+
if (isReady && isNotExecutedYet && !task.processing) {
|
|
51
|
+
if (now - task.start <= this.GRACE_WINDOW_MS) {
|
|
52
|
+
this.runTask(task);
|
|
53
|
+
}
|
|
54
|
+
else if (!task.repeat) {
|
|
55
|
+
(0, LogService_1.logInfo)(`One-off task ${task.name} missed grace window. Dropping.`);
|
|
56
|
+
task.executed = now;
|
|
57
|
+
this.tasks = this.tasks.filter(t => t !== task);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async runTask(task) {
|
|
63
|
+
task.processing = true;
|
|
64
|
+
try {
|
|
65
|
+
(0, LogService_1.logInfo)("Execute task ", task.name);
|
|
66
|
+
await task.callback(task.params);
|
|
67
|
+
task.executed = Date.now();
|
|
68
|
+
task.retries = 0;
|
|
69
|
+
(0, LogService_1.logInfo)("Executed task ", task.name);
|
|
70
|
+
if (task.repeat) {
|
|
71
|
+
task.start += task.repeat;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.tasks = this.tasks.filter(t => t !== task);
|
|
75
|
+
}
|
|
76
|
+
exports.S_TIMEFRAME_TASK_EXECUTED.invoke(task);
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
(0, LogService_1.logError)(`Task ${task.name} error:`, e);
|
|
80
|
+
task.retries = (task.retries || 0) + 1;
|
|
81
|
+
if (task.retries > (task.maxRetries ?? 0)) {
|
|
82
|
+
(0, LogService_1.logWarn)(`Task ${task.name} failed too many times. Dropping.`);
|
|
83
|
+
this.tasks = this.tasks.filter(t => t !== task);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
task.start = Date.now() + this.RETRY_DELAY_MS;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
task.processing = false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async finishService() {
|
|
94
|
+
clearInterval(this.intervalId);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.TimeframeService = TimeframeService;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Signal } from "../signal/Signal";
|
|
2
|
+
import WebSocket from "ws";
|
|
3
|
+
import { IncomingMessage } from "http";
|
|
4
|
+
export declare const S_WS_ON_CONNECTED: Signal<WebSocketConnection>;
|
|
5
|
+
export declare const S_WS_ON_MESSAGE: Signal<{
|
|
6
|
+
connection: WebSocketConnection;
|
|
7
|
+
message: any;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const S_WS_ON_CLOSE: Signal<WebSocketConnection>;
|
|
10
|
+
export declare const WS_CLOSE_UNAUTHORIZED = 4401;
|
|
11
|
+
export declare const WS_CLOSE_AUTH_TIMEOUT = 4408;
|
|
12
|
+
export declare const WS_CLOSE_RATE_LIMIT = 1008;
|
|
13
|
+
export declare const WS_CLOSE_INTERNAL = 1011;
|
|
14
|
+
export type WebSocketAuthorizer = (connection: WebSocketConnection, message: any) => Promise<any> | any;
|
|
15
|
+
export interface IWebSocketConnectionOptions {
|
|
16
|
+
authTimeoutMs: number;
|
|
17
|
+
maxMessagesPerSecond: number;
|
|
18
|
+
}
|
|
19
|
+
export declare class WebSocketConnection {
|
|
20
|
+
authorized: boolean;
|
|
21
|
+
auth: any;
|
|
22
|
+
ws: WebSocket;
|
|
23
|
+
id: number;
|
|
24
|
+
ip: string;
|
|
25
|
+
req: IncomingMessage;
|
|
26
|
+
connectedAt: number;
|
|
27
|
+
constructor(ws: WebSocket, req: IncomingMessage, ip: string);
|
|
28
|
+
get isOpen(): boolean;
|
|
29
|
+
send(data: any): boolean;
|
|
30
|
+
close(code?: number, reason?: string): void;
|
|
31
|
+
}
|
|
32
|
+
export declare class WebSocketServer {
|
|
33
|
+
#private;
|
|
34
|
+
connections: WebSocketConnection[];
|
|
35
|
+
constructor(onAuthorize: WebSocketAuthorizer | null, options: IWebSocketConnectionOptions);
|
|
36
|
+
accept(ws: WebSocket, req: IncomingMessage, ip: string): void;
|
|
37
|
+
broadcast(data: any, filter?: (c: WebSocketConnection) => boolean): number;
|
|
38
|
+
static parse(data: WebSocket.RawData, isBinary: boolean): any;
|
|
39
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
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.WebSocketServer = exports.WebSocketConnection = exports.WS_CLOSE_INTERNAL = exports.WS_CLOSE_RATE_LIMIT = exports.WS_CLOSE_AUTH_TIMEOUT = exports.WS_CLOSE_UNAUTHORIZED = exports.S_WS_ON_CLOSE = exports.S_WS_ON_MESSAGE = exports.S_WS_ON_CONNECTED = void 0;
|
|
7
|
+
const Signal_1 = require("../signal/Signal");
|
|
8
|
+
const ws_1 = __importDefault(require("ws"));
|
|
9
|
+
const LogService_1 = require("./LogService");
|
|
10
|
+
exports.S_WS_ON_CONNECTED = new Signal_1.Signal("S_WS_ON_CONNECTED");
|
|
11
|
+
exports.S_WS_ON_MESSAGE = new Signal_1.Signal("S_WS_ON_MESSAGE");
|
|
12
|
+
exports.S_WS_ON_CLOSE = new Signal_1.Signal("S_WS_ON_CLOSE");
|
|
13
|
+
exports.WS_CLOSE_UNAUTHORIZED = 4401;
|
|
14
|
+
exports.WS_CLOSE_AUTH_TIMEOUT = 4408;
|
|
15
|
+
exports.WS_CLOSE_RATE_LIMIT = 1008;
|
|
16
|
+
exports.WS_CLOSE_INTERNAL = 1011;
|
|
17
|
+
var WS_NEXT_ID = 1;
|
|
18
|
+
class WebSocketConnection {
|
|
19
|
+
authorized = false;
|
|
20
|
+
auth = undefined;
|
|
21
|
+
ws;
|
|
22
|
+
id;
|
|
23
|
+
ip;
|
|
24
|
+
req;
|
|
25
|
+
connectedAt = Date.now();
|
|
26
|
+
constructor(ws, req, ip) {
|
|
27
|
+
this.ws = ws;
|
|
28
|
+
this.req = req;
|
|
29
|
+
this.ip = ip;
|
|
30
|
+
this.id = WS_NEXT_ID++;
|
|
31
|
+
}
|
|
32
|
+
get isOpen() {
|
|
33
|
+
return this.ws.readyState === ws_1.default.OPEN;
|
|
34
|
+
}
|
|
35
|
+
send(data) {
|
|
36
|
+
if (!this.isOpen)
|
|
37
|
+
return false;
|
|
38
|
+
const payload = typeof data === "string" || Buffer.isBuffer(data) ? data : JSON.stringify(data);
|
|
39
|
+
this.ws.send(payload, err => { if (err)
|
|
40
|
+
(0, LogService_1.logWarn)(`WebSocket ${this.id}: send failed — ${err.message}`); });
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
close(code = 1000, reason = "") {
|
|
44
|
+
if (this.ws.readyState === ws_1.default.OPEN || this.ws.readyState === ws_1.default.CONNECTING)
|
|
45
|
+
this.ws.close(code, reason);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.WebSocketConnection = WebSocketConnection;
|
|
49
|
+
class WebSocketServer {
|
|
50
|
+
#onAuthorize;
|
|
51
|
+
#options;
|
|
52
|
+
connections = [];
|
|
53
|
+
constructor(onAuthorize, options) {
|
|
54
|
+
this.#onAuthorize = onAuthorize;
|
|
55
|
+
this.#options = options;
|
|
56
|
+
}
|
|
57
|
+
accept(ws, req, ip) {
|
|
58
|
+
const connection = new WebSocketConnection(ws, req, ip);
|
|
59
|
+
ws.on("error", err => (0, LogService_1.logWarn)(`WebSocket ${connection.id} (${ip}): ${err.message}`));
|
|
60
|
+
this.#limitMessageRate(connection);
|
|
61
|
+
let stage = "waiting";
|
|
62
|
+
const timer = setTimeout(() => {
|
|
63
|
+
if (stage === "done")
|
|
64
|
+
return;
|
|
65
|
+
stage = "done";
|
|
66
|
+
(0, LogService_1.logWarn)(`WebSocket ${connection.id} (${ip}) closed: not authorized within ${this.#options.authTimeoutMs} ms`);
|
|
67
|
+
connection.close(exports.WS_CLOSE_AUTH_TIMEOUT, "Authorization timeout");
|
|
68
|
+
}, this.#options.authTimeoutMs);
|
|
69
|
+
ws.on("close", () => {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
stage = "done";
|
|
72
|
+
if (!connection.authorized)
|
|
73
|
+
return;
|
|
74
|
+
connection.authorized = false;
|
|
75
|
+
this.connections = this.connections.filter(c => c !== connection);
|
|
76
|
+
this.#invoke(exports.S_WS_ON_CLOSE, connection);
|
|
77
|
+
});
|
|
78
|
+
ws.on("message", async (data, isBinary) => {
|
|
79
|
+
const message = WebSocketServer.parse(data, isBinary);
|
|
80
|
+
if (connection.authorized) {
|
|
81
|
+
this.#invoke(exports.S_WS_ON_MESSAGE, { connection, message });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (stage !== "waiting")
|
|
85
|
+
return;
|
|
86
|
+
stage = "checking";
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
89
|
+
result = this.#onAuthorize ? await this.#onAuthorize(connection, message) : true;
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
if (stage !== "checking")
|
|
93
|
+
return;
|
|
94
|
+
stage = "done";
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
(0, LogService_1.logError)(`WebSocket ${connection.id} (${ip}): onAuthorize threw — ${e instanceof Error ? e.message : String(e)}`);
|
|
97
|
+
connection.close(exports.WS_CLOSE_INTERNAL, "Authorization failed");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (stage !== "checking" || !connection.isOpen)
|
|
101
|
+
return;
|
|
102
|
+
stage = "done";
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
if (result === false || result === null || result === undefined) {
|
|
105
|
+
(0, LogService_1.logWarn)(`WebSocket ${connection.id} (${ip}) refused: unauthorized`);
|
|
106
|
+
connection.close(exports.WS_CLOSE_UNAUTHORIZED, "Unauthorized");
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
connection.authorized = true;
|
|
110
|
+
if (result !== true)
|
|
111
|
+
connection.auth = result;
|
|
112
|
+
this.connections.push(connection);
|
|
113
|
+
this.#invoke(exports.S_WS_ON_CONNECTED, connection);
|
|
114
|
+
});
|
|
115
|
+
if (!this.#onAuthorize) {
|
|
116
|
+
stage = "done";
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
connection.authorized = true;
|
|
119
|
+
this.connections.push(connection);
|
|
120
|
+
this.#invoke(exports.S_WS_ON_CONNECTED, connection);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
broadcast(data, filter) {
|
|
124
|
+
let n = 0;
|
|
125
|
+
for (const c of this.connections)
|
|
126
|
+
if ((!filter || filter(c)) && c.send(data))
|
|
127
|
+
n++;
|
|
128
|
+
return n;
|
|
129
|
+
}
|
|
130
|
+
static parse(data, isBinary) {
|
|
131
|
+
if (isBinary)
|
|
132
|
+
return Array.isArray(data) ? Buffer.concat(data) : Buffer.from(data);
|
|
133
|
+
const text = data.toString();
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(text);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return text;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
#invoke(signal, data) {
|
|
142
|
+
try {
|
|
143
|
+
signal.invoke(data);
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
(0, LogService_1.logError)(`WebSocket signal handler threw: ${e instanceof Error ? e.message : String(e)}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
#limitMessageRate(connection) {
|
|
150
|
+
const ws = connection.ws, max = this.#options.maxMessagesPerSecond;
|
|
151
|
+
let windowStart = Date.now(), count = 0, over = false;
|
|
152
|
+
const emit = ws.emit.bind(ws);
|
|
153
|
+
ws.emit = ((event, ...args) => {
|
|
154
|
+
if (event !== "message")
|
|
155
|
+
return emit(event, ...args);
|
|
156
|
+
if (over)
|
|
157
|
+
return false;
|
|
158
|
+
const now = Date.now();
|
|
159
|
+
if (now - windowStart >= 1000) {
|
|
160
|
+
windowStart = now;
|
|
161
|
+
count = 0;
|
|
162
|
+
}
|
|
163
|
+
if (++count > max) {
|
|
164
|
+
over = true;
|
|
165
|
+
(0, LogService_1.logWarn)(`WebSocket ${connection.id} (${connection.ip}) closed: over ${max} messages/s`);
|
|
166
|
+
connection.close(exports.WS_CLOSE_RATE_LIMIT, "Rate limit exceeded");
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return emit(event, ...args);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.WebSocketServer = WebSocketServer;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IDBBulkQuery, IDBQuery, IDBResult, IDBStatFrame, IPoolStatus } from "../DBService";
|
|
2
|
+
export interface IDBAdapter {
|
|
3
|
+
init(): Promise<void>;
|
|
4
|
+
finish(): Promise<void>;
|
|
5
|
+
tBegin?(): Promise<IDBResult>;
|
|
6
|
+
tCommit?(trxid: number): Promise<IDBResult>;
|
|
7
|
+
tRollback?(trxid: number): Promise<IDBResult>;
|
|
8
|
+
prepareQuery(request: IDBQuery): string;
|
|
9
|
+
query(request: IDBQuery): Promise<IDBResult>;
|
|
10
|
+
bulk(request: IDBBulkQuery): Promise<IDBResult[]>;
|
|
11
|
+
poolStatus(): IPoolStatus;
|
|
12
|
+
escape(value: string): string;
|
|
13
|
+
stat(): Map<number, IDBStatFrame>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { DBAdapterOptions, IDBBulkQuery, IDBError, IDBQuery, IDBQueryField, IDBResult, IDBStatFrame } from "../DBService";
|
|
2
|
+
import { IDBAdapter } from "./IDBAdapter";
|
|
3
|
+
import mysql from 'mysql2/promise';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
interface ITransaction {
|
|
6
|
+
id: number;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
conn: mysql.PoolConnection;
|
|
9
|
+
queries: {
|
|
10
|
+
sql: string;
|
|
11
|
+
status: string;
|
|
12
|
+
}[];
|
|
13
|
+
}
|
|
14
|
+
export declare class MysqlAdapter implements IDBAdapter {
|
|
15
|
+
options: DBAdapterOptions;
|
|
16
|
+
serviceStarted: boolean;
|
|
17
|
+
reconnectionTimeout: number;
|
|
18
|
+
reconnecting: boolean;
|
|
19
|
+
pool: mysql.Pool | null;
|
|
20
|
+
timeoutID: any;
|
|
21
|
+
queries: never[];
|
|
22
|
+
static nextTransactionID: number;
|
|
23
|
+
transactions: ITransaction[];
|
|
24
|
+
maxTransactionWaitTime: number;
|
|
25
|
+
lastSuccessQueryTime: number;
|
|
26
|
+
pingInterval: number;
|
|
27
|
+
failReportFileStream: fs.WriteStream | null;
|
|
28
|
+
failReportFileStreamName: string | null;
|
|
29
|
+
failReportLastAccessTime: number;
|
|
30
|
+
poolConnections: number;
|
|
31
|
+
acquiredPoolConnections: number;
|
|
32
|
+
statFrames: Map<number, IDBStatFrame>;
|
|
33
|
+
constructor(options: DBAdapterOptions);
|
|
34
|
+
escape(value: string): string;
|
|
35
|
+
stat(): Map<number, IDBStatFrame>;
|
|
36
|
+
private statRetentionMs;
|
|
37
|
+
private getOrCreateStatSlot;
|
|
38
|
+
collectStat(query: string, execTime: number, failed: boolean): void;
|
|
39
|
+
collectTransactionStat(): void;
|
|
40
|
+
init(): Promise<void>;
|
|
41
|
+
poolStatus(): {
|
|
42
|
+
lastSuccessQueryTime: string;
|
|
43
|
+
poolConnections: number;
|
|
44
|
+
acquiredPoolConnections: number;
|
|
45
|
+
};
|
|
46
|
+
isCleanupRunning: boolean;
|
|
47
|
+
setupTimers(): void;
|
|
48
|
+
recreatePool(): Promise<boolean>;
|
|
49
|
+
tBegin(): Promise<IDBResult>;
|
|
50
|
+
tCommit(trxid: number): Promise<IDBResult>;
|
|
51
|
+
tRollback(trxid: number): Promise<IDBResult>;
|
|
52
|
+
bulk(request: IDBBulkQuery): Promise<IDBResult[]>;
|
|
53
|
+
getConnection(transactionId?: number): Promise<mysql.PoolConnection | IDBError>;
|
|
54
|
+
withTimeout<T>(promise: Promise<T>, ms: number, query: string): Promise<T>;
|
|
55
|
+
query(request: IDBQuery, conn?: mysql.PoolConnection): Promise<IDBResult>;
|
|
56
|
+
killQuery(conn: mysql.PoolConnection): void;
|
|
57
|
+
destroyConnection(conn: mysql.PoolConnection): void;
|
|
58
|
+
finalizeConnection(conn: mysql.PoolConnection): void;
|
|
59
|
+
prepareQuery(request: IDBQuery): string;
|
|
60
|
+
static substitute(text: string, values: Map<string, string>): string;
|
|
61
|
+
static quoteIdentifier(name: string): string;
|
|
62
|
+
static safeReplace(query: string, key: string, value: string): string;
|
|
63
|
+
static prepareQueryFieldValue(field: IDBQueryField): string | number | boolean | null | undefined;
|
|
64
|
+
prepareCountQuery(query: string): string;
|
|
65
|
+
commit(trx: ITransaction): Promise<IDBError | null>;
|
|
66
|
+
rollbackTransaction(trx: ITransaction, donNotRemove?: boolean): Promise<IDBError | null>;
|
|
67
|
+
finish(): Promise<void>;
|
|
68
|
+
createMysqlQueryError(err: any, throwable?: boolean): IDBError;
|
|
69
|
+
storeTransactionAsProblem(trx: ITransaction, message: string): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
export declare function isError(obj: any): obj is IDBError;
|
|
72
|
+
export {};
|