@bmfk/api 0.0.0-stage → 1.0.0
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 +83 -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,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataProvider = void 0;
|
|
4
|
+
const Signal_1 = require("../../signal/Signal");
|
|
5
|
+
class DataProvider {
|
|
6
|
+
static nextID = 1;
|
|
7
|
+
name;
|
|
8
|
+
busy = false;
|
|
9
|
+
lastRequestTime = 0;
|
|
10
|
+
cacheTime = 1000 * 60 * 5;
|
|
11
|
+
data;
|
|
12
|
+
callbacks = new Signal_1.Signal();
|
|
13
|
+
wasError = false;
|
|
14
|
+
executor;
|
|
15
|
+
constructor(executor, cacheTime, name) {
|
|
16
|
+
this.executor = executor;
|
|
17
|
+
if (cacheTime !== undefined)
|
|
18
|
+
this.cacheTime = +cacheTime;
|
|
19
|
+
if (isNaN(this.cacheTime))
|
|
20
|
+
this.cacheTime = 0;
|
|
21
|
+
this.name = name ?? "DataProvider " + (DataProvider.nextID++);
|
|
22
|
+
}
|
|
23
|
+
async forceUpdate() {
|
|
24
|
+
if (this.busy)
|
|
25
|
+
return;
|
|
26
|
+
this.wasError = false;
|
|
27
|
+
this.lastRequestTime = 0;
|
|
28
|
+
await this.getData();
|
|
29
|
+
}
|
|
30
|
+
localUpdate(data) {
|
|
31
|
+
if (this.busy)
|
|
32
|
+
return;
|
|
33
|
+
this.wasError = false;
|
|
34
|
+
this.lastRequestTime = +new Date();
|
|
35
|
+
this.data = data;
|
|
36
|
+
}
|
|
37
|
+
async getData() {
|
|
38
|
+
if (this.busy)
|
|
39
|
+
return new Promise((resolve, reject) => this.callbacks.subscribe((d) => resolve(d)));
|
|
40
|
+
if (this.lastRequestTime > 0 && !this.wasError) {
|
|
41
|
+
const diff = (+new Date()) - this.lastRequestTime;
|
|
42
|
+
if (diff < this.cacheTime)
|
|
43
|
+
return this.data;
|
|
44
|
+
}
|
|
45
|
+
this.busy = true;
|
|
46
|
+
this.wasError = false;
|
|
47
|
+
const res = await this.executor();
|
|
48
|
+
this.busy = false;
|
|
49
|
+
this.wasError = res.error ?? false;
|
|
50
|
+
this.data = res.data;
|
|
51
|
+
this.lastRequestTime = +new Date();
|
|
52
|
+
this.callbacks.invoke(this.data);
|
|
53
|
+
this.callbacks.removeAll();
|
|
54
|
+
return this.data;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.DataProvider = DataProvider;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import net from "net";
|
|
2
|
+
export declare class IpAllowList {
|
|
3
|
+
static build(entries: readonly string[] | undefined | null): {
|
|
4
|
+
list: net.BlockList | null;
|
|
5
|
+
error?: string;
|
|
6
|
+
};
|
|
7
|
+
static allows(list: net.BlockList | null, ip: string | undefined | null): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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.IpAllowList = void 0;
|
|
7
|
+
const net_1 = __importDefault(require("net"));
|
|
8
|
+
class IpAllowList {
|
|
9
|
+
static build(entries) {
|
|
10
|
+
const items = (entries ?? []).map(e => String(e).trim()).filter(e => e.length > 0);
|
|
11
|
+
if (items.length === 0)
|
|
12
|
+
return { list: null };
|
|
13
|
+
const list = new net_1.default.BlockList();
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
const [addr, prefix, extra] = item.split("/");
|
|
16
|
+
const family = net_1.default.isIP(addr);
|
|
17
|
+
if (!family || extra !== undefined)
|
|
18
|
+
return { list: null, error: `"${item}" is not an IP address or subnet` };
|
|
19
|
+
const type = family === 6 ? "ipv6" : "ipv4";
|
|
20
|
+
if (prefix === undefined) {
|
|
21
|
+
list.addAddress(addr, type);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const bits = Number(prefix), max = family === 6 ? 128 : 32;
|
|
25
|
+
if (!/^\d+$/.test(prefix) || bits > max)
|
|
26
|
+
return { list: null, error: `"${item}" has an invalid prefix (0–${max})` };
|
|
27
|
+
list.addSubnet(addr, bits, type);
|
|
28
|
+
}
|
|
29
|
+
return { list };
|
|
30
|
+
}
|
|
31
|
+
static allows(list, ip) {
|
|
32
|
+
if (!list)
|
|
33
|
+
return true;
|
|
34
|
+
const family = net_1.default.isIP(String(ip ?? ""));
|
|
35
|
+
if (!family)
|
|
36
|
+
return false;
|
|
37
|
+
return list.check(String(ip), family === 6 ? "ipv6" : "ipv4");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.IpAllowList = IpAllowList;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function JSONStableStringify(obj: any): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSONStableStringify = JSONStableStringify;
|
|
4
|
+
function JSONStableStringify(obj) {
|
|
5
|
+
if (obj === null || typeof obj !== "object") {
|
|
6
|
+
return JSON.stringify(obj);
|
|
7
|
+
}
|
|
8
|
+
if (Array.isArray(obj)) {
|
|
9
|
+
return "[" + obj.map(JSONStableStringify).join(",") + "]";
|
|
10
|
+
}
|
|
11
|
+
const keys = Object.keys(obj).sort();
|
|
12
|
+
return ("{" +
|
|
13
|
+
keys.map(k => JSON.stringify(k) + ":" + JSONStableStringify(obj[k])).join(",") +
|
|
14
|
+
"}");
|
|
15
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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.PasswordHash = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const DEFAULTS = { N: 16384, r: 8, p: 1, keylen: 32 };
|
|
9
|
+
function scryptAsync(secret, salt, keylen, opts) {
|
|
10
|
+
return new Promise((resolve, reject) => crypto_1.default.scrypt(secret, salt, keylen, opts, (err, key) => err ? reject(err) : resolve(key)));
|
|
11
|
+
}
|
|
12
|
+
class PasswordHash {
|
|
13
|
+
static isHash(value) {
|
|
14
|
+
return typeof value === "string" && value.startsWith("scrypt$");
|
|
15
|
+
}
|
|
16
|
+
static async hash(secret) {
|
|
17
|
+
const { N, r, p, keylen } = DEFAULTS;
|
|
18
|
+
const salt = crypto_1.default.randomBytes(16);
|
|
19
|
+
const key = await scryptAsync(secret, salt, keylen, { N, r, p, maxmem: 256 * N * r });
|
|
20
|
+
return `scrypt$${N}$${r}$${p}$${salt.toString("base64")}$${key.toString("base64")}`;
|
|
21
|
+
}
|
|
22
|
+
static async verify(secret, stored) {
|
|
23
|
+
if (typeof secret !== "string" || !PasswordHash.isHash(stored))
|
|
24
|
+
return false;
|
|
25
|
+
const parts = stored.split("$");
|
|
26
|
+
if (parts.length !== 6)
|
|
27
|
+
return false;
|
|
28
|
+
const N = Number(parts[1]), r = Number(parts[2]), p = Number(parts[3]);
|
|
29
|
+
if (!Number.isInteger(N) || N < 1024 || N > 1 << 20 || (N & (N - 1)) !== 0)
|
|
30
|
+
return false;
|
|
31
|
+
if (!Number.isInteger(r) || r < 1 || r > 32 || !Number.isInteger(p) || p < 1 || p > 16)
|
|
32
|
+
return false;
|
|
33
|
+
let salt, expected;
|
|
34
|
+
try {
|
|
35
|
+
salt = Buffer.from(parts[4], "base64");
|
|
36
|
+
expected = Buffer.from(parts[5], "base64");
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (salt.length < 8 || expected.length < 16)
|
|
42
|
+
return false;
|
|
43
|
+
try {
|
|
44
|
+
const key = await scryptAsync(secret, salt, expected.length, { N, r, p, maxmem: 256 * N * r });
|
|
45
|
+
return crypto_1.default.timingSafeEqual(key, expected);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.PasswordHash = PasswordHash;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Sleep: (ms: number) => Promise<unknown>;
|
|
@@ -0,0 +1,30 @@
|
|
|
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.Trace = exports.TRACE_HEADER = void 0;
|
|
7
|
+
const async_hooks_1 = require("async_hooks");
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
exports.TRACE_HEADER = "x-request-id";
|
|
10
|
+
const VALID = /^[A-Za-z0-9._:-]{8,128}$/;
|
|
11
|
+
const storage = new async_hooks_1.AsyncLocalStorage();
|
|
12
|
+
exports.Trace = {
|
|
13
|
+
newId() {
|
|
14
|
+
return crypto_1.default.randomUUID();
|
|
15
|
+
},
|
|
16
|
+
isValid(id) {
|
|
17
|
+
return typeof id === "string" && VALID.test(id);
|
|
18
|
+
},
|
|
19
|
+
current() {
|
|
20
|
+
return storage.getStore()?.traceId ?? null;
|
|
21
|
+
},
|
|
22
|
+
run(traceId, fn) {
|
|
23
|
+
return storage.run({ traceId }, fn);
|
|
24
|
+
},
|
|
25
|
+
adopt(traceId) {
|
|
26
|
+
const ctx = storage.getStore();
|
|
27
|
+
if (ctx && exports.Trace.isValid(traceId))
|
|
28
|
+
ctx.traceId = traceId;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class UID {
|
|
2
|
+
private static readonly epoch;
|
|
3
|
+
private static nextUniqueID;
|
|
4
|
+
private static nextUniqueTRXID;
|
|
5
|
+
static generate(): string;
|
|
6
|
+
static generateRandomNumberBase(count: number): string;
|
|
7
|
+
static generateNumeric(year?: string): bigint;
|
|
8
|
+
static getDateFromNumeric(uid: number | bigint): Date;
|
|
9
|
+
static generateNumberic(year?: string): number;
|
|
10
|
+
static getNextDescendingIndex(): number;
|
|
11
|
+
static getNext(): string;
|
|
12
|
+
static getNextTRX(): string;
|
|
13
|
+
static generateTrxUID(date?: Date): number;
|
|
14
|
+
static sha256(str: string, nochanges?: boolean): string;
|
|
15
|
+
static leadZero(i: number): string;
|
|
16
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
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.UID = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
class UID {
|
|
9
|
+
static epoch = 1697037922668;
|
|
10
|
+
static nextUniqueID = 1;
|
|
11
|
+
static nextUniqueTRXID = 1;
|
|
12
|
+
static generate() {
|
|
13
|
+
let uid = Buffer.from(parseInt(((+new Date()) - this.epoch)
|
|
14
|
+
+ ""
|
|
15
|
+
+ Math.round(Math.random() * 1000)).toString(16), 'hex').toString('base64');
|
|
16
|
+
uid = uid.replaceAll("=", "");
|
|
17
|
+
uid = uid.replaceAll("+", "pL");
|
|
18
|
+
uid = uid.replaceAll("/", "sl");
|
|
19
|
+
return uid;
|
|
20
|
+
}
|
|
21
|
+
static generateRandomNumberBase(count) {
|
|
22
|
+
let base = "";
|
|
23
|
+
for (let i = 0; i < count; i++)
|
|
24
|
+
base += Math.round(Math.random() * 9);
|
|
25
|
+
return base;
|
|
26
|
+
}
|
|
27
|
+
static generateNumeric(year) {
|
|
28
|
+
if (!year)
|
|
29
|
+
year = new Date().getFullYear().toString();
|
|
30
|
+
const yearSuffix = year.substring(2);
|
|
31
|
+
const now = BigInt(Date.now());
|
|
32
|
+
const delta = now - BigInt(UID.epoch);
|
|
33
|
+
const counter = UID.nextUniqueID++;
|
|
34
|
+
if (UID.nextUniqueID > 99) {
|
|
35
|
+
UID.nextUniqueID = 1;
|
|
36
|
+
}
|
|
37
|
+
const counterStr = counter.toString().padStart(2, '0');
|
|
38
|
+
const idStr = `${yearSuffix}${delta.toString()}${counterStr}`;
|
|
39
|
+
return BigInt(idStr);
|
|
40
|
+
}
|
|
41
|
+
static getDateFromNumeric(uid) {
|
|
42
|
+
const uidStr = uid.toString();
|
|
43
|
+
const yearSuffix = uidStr.substring(0, 2);
|
|
44
|
+
const counterDigits = 2;
|
|
45
|
+
const deltaStr = uidStr.substring(2, uidStr.length - counterDigits);
|
|
46
|
+
const delta = BigInt(deltaStr);
|
|
47
|
+
const timestamp = BigInt(UID.epoch) + delta;
|
|
48
|
+
return new Date(Number(timestamp));
|
|
49
|
+
}
|
|
50
|
+
static generateNumberic(year) {
|
|
51
|
+
if (!year)
|
|
52
|
+
year = new Date().getFullYear() + "";
|
|
53
|
+
const nextID = UID.nextUniqueID++;
|
|
54
|
+
if (UID.nextUniqueID > 99)
|
|
55
|
+
UID.nextUniqueID = 1;
|
|
56
|
+
return parseInt(year.substring(2) + "" + (+new Date() - this.epoch) + "" + (nextID + "").padStart(2, "0"));
|
|
57
|
+
}
|
|
58
|
+
static getNextDescendingIndex() {
|
|
59
|
+
return Number.MAX_SAFE_INTEGER - (+new Date());
|
|
60
|
+
}
|
|
61
|
+
static getNext() {
|
|
62
|
+
const nextID = UID.nextUniqueID++;
|
|
63
|
+
if (UID.nextUniqueID > 99)
|
|
64
|
+
UID.nextUniqueID = 1;
|
|
65
|
+
return this.leadZero(nextID);
|
|
66
|
+
}
|
|
67
|
+
static getNextTRX() {
|
|
68
|
+
const nextID = UID.nextUniqueTRXID++;
|
|
69
|
+
if (UID.nextUniqueTRXID > 99)
|
|
70
|
+
UID.nextUniqueTRXID = 1;
|
|
71
|
+
return this.leadZero(nextID);
|
|
72
|
+
}
|
|
73
|
+
static generateTrxUID(date) {
|
|
74
|
+
if (!date)
|
|
75
|
+
date = new Date();
|
|
76
|
+
const year = (new Date().getFullYear() + "").substring(2);
|
|
77
|
+
const seconds = (date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()) + "";
|
|
78
|
+
const str = year
|
|
79
|
+
+ this.leadZero(date.getMonth() + 1)
|
|
80
|
+
+ this.leadZero(date.getDate())
|
|
81
|
+
+ seconds.padStart(5, "0")
|
|
82
|
+
+ this.getNextTRX();
|
|
83
|
+
return parseInt(str);
|
|
84
|
+
}
|
|
85
|
+
static sha256(str, nochanges) {
|
|
86
|
+
if (nochanges)
|
|
87
|
+
return crypto_1.default.createHash("sha256").update(str).digest("hex");
|
|
88
|
+
let uid = crypto_1.default.createHash("sha256").update(str).digest("base64");
|
|
89
|
+
uid = uid.replaceAll("=", "");
|
|
90
|
+
uid = uid.replaceAll("+", ".");
|
|
91
|
+
uid = uid.replaceAll("/", "-");
|
|
92
|
+
return uid;
|
|
93
|
+
}
|
|
94
|
+
static leadZero(i) {
|
|
95
|
+
return (i + "").padStart(2, "0");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.UID = UID;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export type TValidatorType = "string" | "number" | "boolean" | "date" | "array" | "object" | "email" | "phone";
|
|
2
|
+
export interface IStructureOptions {
|
|
3
|
+
optional?: boolean;
|
|
4
|
+
skip_validation?: boolean;
|
|
5
|
+
default?: any;
|
|
6
|
+
values?: any;
|
|
7
|
+
regex?: string;
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
details?: string;
|
|
11
|
+
type?: string;
|
|
12
|
+
ignore_in_documentation?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface IValidatorOptions {
|
|
15
|
+
min?: number;
|
|
16
|
+
max?: number;
|
|
17
|
+
type?: TValidatorType;
|
|
18
|
+
regex?: RegExp;
|
|
19
|
+
}
|
|
20
|
+
export interface IValidaotrType {
|
|
21
|
+
name: string;
|
|
22
|
+
type: TValidatorType;
|
|
23
|
+
optional?: boolean;
|
|
24
|
+
arrayType?: IValidaotrType;
|
|
25
|
+
}
|
|
26
|
+
export declare enum ValidationReport {
|
|
27
|
+
OK = 1,
|
|
28
|
+
VALUE_IS_NULL = 2,
|
|
29
|
+
TYPE_MISSMATCH = 3,
|
|
30
|
+
VALUE_TOO_BIG = 4,
|
|
31
|
+
VALUE_TOO_SHORT = 5,
|
|
32
|
+
VALUE_INCORRECT = 6
|
|
33
|
+
}
|
|
34
|
+
type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
35
|
+
type WidenLiterals<T> = {
|
|
36
|
+
[K in keyof T]: T[K] extends readonly (infer R)[] ? readonly WidenLiteral<R>[] : T[K] extends (infer R)[] ? WidenLiteral<R>[] : T[K] extends object ? WidenLiterals<T[K]> : WidenLiteral<T[K]>;
|
|
37
|
+
};
|
|
38
|
+
type DeepMutable<T> = T extends ReadonlyArray<infer U> ? DeepMutable<U>[] : T extends Array<infer U> ? DeepMutable<U>[] : T extends (...args: any[]) => any ? T : T extends object ? {
|
|
39
|
+
-readonly [K in keyof T]: DeepMutable<T[K]>;
|
|
40
|
+
} : T;
|
|
41
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
42
|
+
type Or<A, B> = A extends true ? true : (B extends true ? true : false);
|
|
43
|
+
type CleanFields<T> = WidenLiterals<{
|
|
44
|
+
[K in keyof T as K extends `$__${string}` ? never : K]: T[K];
|
|
45
|
+
}>;
|
|
46
|
+
type HasOptional<T, K extends string> = Or<`$__${K}_optional` extends keyof T ? T[`$__${K}_optional`] extends true ? true : false : false, `$__${K}` extends keyof T ? T[`$__${K}`] extends {
|
|
47
|
+
optional: true;
|
|
48
|
+
} ? true : false : false>;
|
|
49
|
+
type ExtractOptionalFieldNames<T> = {
|
|
50
|
+
[K in (keyof T & string) as HasOptional<T, K> extends true ? K : never]: true;
|
|
51
|
+
};
|
|
52
|
+
type OptionalFields<T> = keyof ExtractOptionalFieldNames<T> & string;
|
|
53
|
+
type ValuesArr<T, K extends string> = `$__${K}_values` extends keyof T ? T[`$__${K}_values`] extends readonly (infer V)[] ? readonly V[] : T[`$__${K}_values`] extends (infer V)[] ? readonly V[] : T[`$__${K}_values`] extends Record<string, infer V> ? readonly V[] : never : `$__${K}` extends keyof T ? T[`$__${K}`] extends {
|
|
54
|
+
values: readonly (infer V)[];
|
|
55
|
+
} ? readonly V[] : T[`$__${K}`] extends {
|
|
56
|
+
values: (infer V)[];
|
|
57
|
+
} ? readonly V[] : T[`$__${K}`] extends {
|
|
58
|
+
values: Record<string, infer V>;
|
|
59
|
+
} ? readonly V[] : never : never;
|
|
60
|
+
type ExtractEnumValues<T> = {
|
|
61
|
+
[K in (keyof T & string) as IsNever<ValuesArr<T, K>> extends true ? never : K]: ValuesArr<T, K> extends ReadonlyArray<infer V> ? V : never;
|
|
62
|
+
};
|
|
63
|
+
type EnumFieldNames<T> = keyof ExtractEnumValues<T> & string;
|
|
64
|
+
type EnumFieldValues<T> = ExtractEnumValues<T>;
|
|
65
|
+
type FieldOriginal<T, K extends PropertyKey> = K extends keyof T ? T[K] : never;
|
|
66
|
+
type ProcessValue<T, K extends keyof CleanFields<T>> = FieldOriginal<T, K> extends ReadonlyArray<infer Item> ? Item extends object ? ValidationModel<Item>[] : WidenLiteral<Item>[] : FieldOriginal<T, K> extends Array<infer Item> ? Item extends object ? ValidationModel<Item>[] : WidenLiteral<Item>[] : FieldOriginal<T, K> extends (...a: any[]) => any ? FieldOriginal<T, K> : FieldOriginal<T, K> extends object ? ValidationModel<FieldOriginal<T, K>> : WidenLiteral<FieldOriginal<T, K>>;
|
|
67
|
+
export type ValidationModel<T> = DeepMutable<{
|
|
68
|
+
[K in keyof CleanFields<T> as K extends OptionalFields<T> ? K : never]?: K extends EnumFieldNames<T> ? EnumFieldValues<T>[K] : ProcessValue<T, K>;
|
|
69
|
+
} & {
|
|
70
|
+
[K in keyof CleanFields<T> as K extends OptionalFields<T> ? never : K]: K extends EnumFieldNames<T> ? EnumFieldValues<T>[K] : ProcessValue<T, K>;
|
|
71
|
+
}>;
|
|
72
|
+
export declare class Validator {
|
|
73
|
+
static validateObject(fields: string[], object: any): boolean;
|
|
74
|
+
static documentStructure(structure: any): Record<string, any> | undefined;
|
|
75
|
+
static boundFromKey(structure: Record<string, any>, name: string): number | undefined;
|
|
76
|
+
static numericSamples(structure: any, parentPath?: string): string[];
|
|
77
|
+
static parseStructureOptions(key: string, structure: Record<string, any>): IStructureOptions;
|
|
78
|
+
static validateStructure(structure: any, object: any, level?: number, parentPath?: string): Promise<string[] | undefined>;
|
|
79
|
+
static filterStructure(structure: any, object: any): void;
|
|
80
|
+
static isPlainObject(v: any): boolean;
|
|
81
|
+
static clone<T>(v: T): T;
|
|
82
|
+
static syncStructure(structure: any, node: any): any;
|
|
83
|
+
static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
|
|
84
|
+
static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
|
|
85
|
+
}
|
|
86
|
+
export {};
|