@xrystal/core 3.28.3 → 3.37.3
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/README.md +2331 -21
- package/package.json +66 -24
- package/tsconfig.base.json +15 -0
- package/x/bin/commands/create.mjs +136 -0
- package/x/bin/commands/doctor.mjs +149 -0
- package/x/bin/commands/hooks.mjs +55 -0
- package/x/bin/commands/project.mjs +126 -0
- package/x/bin/commands/release.mjs +66 -0
- package/x/bin/commands/sync-toolchain.mjs +43 -0
- package/x/bin/commands/tmp.mjs +57 -0
- package/x/bin/constants/index.mjs +6 -0
- package/x/bin/helpers/.generated/.generated-from +1 -0
- package/x/bin/helpers/.generated/tmp-schema.mjs +192 -0
- package/x/bin/helpers/banner.mjs +80 -0
- package/x/bin/helpers/build-config.mjs +342 -0
- package/x/bin/helpers/cli-help.mjs +164 -0
- package/x/bin/helpers/cli-identity.mjs +17 -0
- package/x/bin/helpers/create-target.mjs +35 -0
- package/{bin → x/bin}/helpers/index.mjs +44 -1
- package/x/bin/helpers/project-artifacts.mjs +140 -0
- package/x/bin/helpers/project-root.mjs +70 -0
- package/x/bin/helpers/runtime-info.mjs +172 -0
- package/x/bin/helpers/tmp-config.mjs +67 -0
- package/x/bin/helpers/tmp-layout.mjs +281 -0
- package/x/bin/helpers/tmp-schema.mjs +9 -0
- package/x/bin/helpers/toolchain-config.mjs +233 -0
- package/x/bin/main-cli.js +80 -0
- package/x/bin/paths.mjs +14 -0
- package/x/bin/scripts/clean-source-types.mjs +19 -0
- package/x/bin/scripts/fix-dist-imports.mjs +80 -0
- package/x/bin/scripts/install-git-hooks.mjs +86 -0
- package/x/bin/scripts/minify-dist.mjs +50 -0
- package/x/bin/scripts/prepare-npm-publish.mjs +49 -0
- package/x/bin/scripts/release-prepare.mjs +56 -0
- package/x/bin/scripts/tag.mjs +17 -0
- package/x/bin/scripts/unpublish-leaked-versions.mjs +62 -0
- package/x/bin/scripts/verify-npm-pack.mjs +45 -0
- package/x/bin/scripts/verify-version-bump.mjs +27 -0
- package/x/bin/scripts/version-check.mjs +370 -0
- package/x/dist/index.d.ts +3 -0
- package/x/dist/index.js +1 -0
- package/{source → x/dist}/loader/clients/index.d.ts +2 -1
- package/x/dist/loader/clients/index.js +1 -0
- package/{source → x/dist}/loader/configs/index.d.ts +3 -2
- package/x/dist/loader/configs/index.js +1 -0
- package/x/dist/loader/controller/index.d.ts +69 -0
- package/x/dist/loader/controller/index.js +1 -0
- package/x/dist/loader/dto/index.d.ts +39 -0
- package/x/dist/loader/dto/index.js +1 -0
- package/{source → x/dist}/loader/events/index.d.ts +1 -1
- package/x/dist/loader/events/index.js +1 -0
- package/{source → x/dist}/loader/index.d.ts +2 -1
- package/x/dist/loader/index.js +1 -0
- package/{source → x/dist}/loader/kafka/index.d.ts +6 -3
- package/x/dist/loader/kafka/index.js +1 -0
- package/{source → x/dist}/loader/localizations/index.d.ts +4 -3
- package/x/dist/loader/localizations/index.js +1 -0
- package/{source → x/dist}/loader/logger/index.d.ts +10 -7
- package/x/dist/loader/logger/index.js +2 -0
- package/x/dist/loader/service/index.js +1 -0
- package/{source → x/dist}/loader/system/index.d.ts +4 -3
- package/x/dist/loader/system/index.js +1 -0
- package/{source → x/dist}/project/index.d.ts +2 -2
- package/x/dist/project/index.js +1 -0
- package/x/dist/utils/constants/defaults.json +44 -0
- package/x/dist/utils/constants/index.d.ts +27 -0
- package/x/dist/utils/constants/index.js +1 -0
- package/x/dist/utils/helpers/configs/index.js +1 -0
- package/x/dist/utils/helpers/context/controller-store.d.ts +51 -0
- package/x/dist/utils/helpers/context/controller-store.js +1 -0
- package/x/dist/utils/helpers/context/index.d.ts +18 -0
- package/x/dist/utils/helpers/context/index.js +1 -0
- package/{source → x/dist}/utils/helpers/core/index.d.ts +8 -6
- package/x/dist/utils/helpers/core/index.js +1 -0
- package/x/dist/utils/helpers/core/paths.d.ts +2 -0
- package/x/dist/utils/helpers/core/paths.js +1 -0
- package/x/dist/utils/helpers/core/tmp-layout.d.ts +45 -0
- package/x/dist/utils/helpers/core/tmp-layout.js +1 -0
- package/{source → x/dist}/utils/helpers/date/index.d.ts +1 -1
- package/x/dist/utils/helpers/date/index.js +1 -0
- package/x/dist/utils/helpers/dispatch/index.d.ts +9 -0
- package/x/dist/utils/helpers/dispatch/index.js +1 -0
- package/x/dist/utils/helpers/dto/index.d.ts +10 -0
- package/x/dist/utils/helpers/dto/index.js +1 -0
- package/x/dist/utils/helpers/filters/index.js +1 -0
- package/x/dist/utils/helpers/framework/index.d.ts +3 -0
- package/x/dist/utils/helpers/framework/index.js +1 -0
- package/x/dist/utils/helpers/handlers/adapters.d.ts +10 -0
- package/x/dist/utils/helpers/handlers/adapters.js +1 -0
- package/x/dist/utils/helpers/handlers/execute.d.ts +5 -0
- package/x/dist/utils/helpers/handlers/execute.js +1 -0
- package/x/dist/utils/helpers/handlers/index.d.ts +18 -0
- package/x/dist/utils/helpers/handlers/index.js +1 -0
- package/x/dist/utils/helpers/hash/crypto.js +1 -0
- package/x/dist/utils/helpers/id/index.js +1 -0
- package/x/dist/utils/helpers/index.d.ts +26 -0
- package/x/dist/utils/helpers/index.js +1 -0
- package/x/dist/utils/helpers/ip/index.js +1 -0
- package/x/dist/utils/helpers/is/index.js +1 -0
- package/{source → x/dist}/utils/helpers/locales/index.d.ts +1 -0
- package/x/dist/utils/helpers/locales/index.js +1 -0
- package/x/dist/utils/helpers/math/index.js +1 -0
- package/x/dist/utils/helpers/middleware/index.d.ts +19 -0
- package/x/dist/utils/helpers/middleware/index.js +1 -0
- package/x/dist/utils/helpers/objects/index.js +1 -0
- package/x/dist/utils/helpers/path/index.js +1 -0
- package/x/dist/utils/helpers/protocol/index.d.ts +20 -0
- package/x/dist/utils/helpers/protocol/index.js +1 -0
- package/x/dist/utils/helpers/protocol/options.d.ts +6 -0
- package/x/dist/utils/helpers/protocol/options.js +1 -0
- package/x/dist/utils/helpers/regex/checkSpecialRegexControl.js +1 -0
- package/x/dist/utils/helpers/runtime/dto-access.d.ts +3 -0
- package/x/dist/utils/helpers/runtime/dto-access.js +1 -0
- package/x/dist/utils/helpers/runtime/dto-types.d.ts +19 -0
- package/x/dist/utils/helpers/runtime/dto-types.js +1 -0
- package/x/dist/utils/helpers/runtime/index.d.ts +3 -0
- package/x/dist/utils/helpers/runtime/index.js +1 -0
- package/x/dist/utils/helpers/runtime/shutdown.d.ts +3 -0
- package/x/dist/utils/helpers/runtime/shutdown.js +1 -0
- package/x/dist/utils/helpers/state/index.d.ts +59 -0
- package/x/dist/utils/helpers/state/index.js +1 -0
- package/x/dist/utils/helpers/state/props-from-tmp.d.ts +9 -0
- package/x/dist/utils/helpers/state/props-from-tmp.js +1 -0
- package/x/dist/utils/helpers/state/request-key-guard.d.ts +3 -0
- package/x/dist/utils/helpers/state/request-key-guard.js +1 -0
- package/x/dist/utils/helpers/state/scopes.d.ts +21 -0
- package/x/dist/utils/helpers/state/scopes.js +1 -0
- package/x/dist/utils/helpers/string/index.js +1 -0
- package/x/dist/utils/helpers/tests/tmp-schema.test.js +1 -0
- package/{source/utils/models/types/index.js → x/dist/utils/helpers/tests/tmp-watch.test.d.ts} +0 -1
- package/x/dist/utils/helpers/tests/tmp-watch.test.js +1 -0
- package/x/dist/utils/helpers/timer/index.js +1 -0
- package/x/dist/utils/helpers/tmp/index.d.ts +5 -0
- package/x/dist/utils/helpers/tmp/index.js +1 -0
- package/x/dist/utils/helpers/tmp/loader-tmp-sync.d.ts +4 -0
- package/x/dist/utils/helpers/tmp/loader-tmp-sync.js +1 -0
- package/x/dist/utils/helpers/tmp/runtime-tmp.d.ts +9 -0
- package/x/dist/utils/helpers/tmp/runtime-tmp.js +1 -0
- package/x/dist/utils/helpers/tmp/tmp-loader.d.ts +7 -0
- package/x/dist/utils/helpers/tmp/tmp-loader.js +1 -0
- package/x/dist/utils/helpers/tmp/tmp-schema.d.ts +47 -0
- package/x/dist/utils/helpers/tmp/tmp-schema.js +1 -0
- package/x/dist/utils/helpers/tmp/tmp-watch.d.ts +1 -0
- package/x/dist/utils/helpers/tmp/tmp-watch.js +1 -0
- package/x/dist/utils/helpers/validates/index.js +1 -0
- package/x/dist/utils/index.d.ts +3 -0
- package/x/dist/utils/index.js +1 -0
- package/x/dist/utils/models/classes/class.clients.js +1 -0
- package/x/dist/utils/models/classes/class.interfaces.js +1 -0
- package/x/dist/utils/models/classes/class.service-locator.js +1 -0
- package/{source → x/dist}/utils/models/classes/class.tmp-file-loader.d.ts +3 -2
- package/x/dist/utils/models/classes/class.tmp-file-loader.js +1 -0
- package/x/dist/utils/models/classes/class.x.js +1 -0
- package/{source → x/dist}/utils/models/enums/index.d.ts +14 -1
- package/x/dist/utils/models/enums/index.js +1 -0
- package/x/dist/utils/models/index.d.ts +9 -0
- package/x/dist/utils/models/index.js +1 -0
- package/x/dist/utils/models/types/index.d.ts +5 -0
- package/x/dist/utils/models/types/index.js +1 -0
- package/x/dist/utils/package-root.d.ts +2 -0
- package/x/dist/utils/package-root.js +1 -0
- package/x/tmp.yml +48 -17
- package/bin/constants/index.mjs +0 -17
- package/bin/main-cli.js +0 -131
- package/source/index.js +0 -6
- package/source/loader/clients/index.js +0 -18
- package/source/loader/configs/index.js +0 -81
- package/source/loader/controller/index.d.ts +0 -129
- package/source/loader/controller/index.js +0 -327
- package/source/loader/events/index.js +0 -44
- package/source/loader/index.js +0 -10
- package/source/loader/kafka/index.js +0 -122
- package/source/loader/localizations/index.js +0 -40
- package/source/loader/logger/index.js +0 -183
- package/source/loader/service/index.js +0 -7
- package/source/loader/system/index.js +0 -19
- package/source/project/index.js +0 -66
- package/source/utils/constants/index.d.ts +0 -10
- package/source/utils/constants/index.js +0 -11
- package/source/utils/helpers/configs/index.js +0 -1
- package/source/utils/helpers/core/index.js +0 -105
- package/source/utils/helpers/date/index.js +0 -48
- package/source/utils/helpers/filters/index.js +0 -44
- package/source/utils/helpers/hash/crypto.js +0 -22
- package/source/utils/helpers/id/index.js +0 -24
- package/source/utils/helpers/index.d.ts +0 -17
- package/source/utils/helpers/index.js +0 -17
- package/source/utils/helpers/ip/index.js +0 -21
- package/source/utils/helpers/is/index.js +0 -35
- package/source/utils/helpers/locales/index.js +0 -63
- package/source/utils/helpers/math/index.js +0 -14
- package/source/utils/helpers/middlewares/index.d.ts +0 -1
- package/source/utils/helpers/middlewares/index.js +0 -26
- package/source/utils/helpers/objects/index.js +0 -55
- package/source/utils/helpers/path/index.js +0 -4
- package/source/utils/helpers/regex/checkSpecialRegexControl.js +0 -3
- package/source/utils/helpers/string/index.js +0 -9
- package/source/utils/helpers/timer/index.js +0 -5
- package/source/utils/helpers/validates/index.js +0 -20
- package/source/utils/index.d.ts +0 -3
- package/source/utils/index.js +0 -3
- package/source/utils/models/classes/class.clients.js +0 -142
- package/source/utils/models/classes/class.dtos.d.ts +0 -47
- package/source/utils/models/classes/class.dtos.js +0 -36
- package/source/utils/models/classes/class.interfaces.js +0 -10
- package/source/utils/models/classes/class.response.d.ts +0 -17
- package/source/utils/models/classes/class.response.js +0 -35
- package/source/utils/models/classes/class.service-locator.js +0 -16
- package/source/utils/models/classes/class.tmp-file-loader.js +0 -38
- package/source/utils/models/classes/class.x.js +0 -179
- package/source/utils/models/enums/index.js +0 -133
- package/source/utils/models/index.d.ts +0 -11
- package/source/utils/models/index.js +0 -11
- package/source/utils/models/types/index.d.ts +0 -3
- /package/{source → x/dist}/loader/service/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/configs/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/filters/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/hash/crypto.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/id/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/ip/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/is/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/math/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/objects/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/path/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/regex/checkSpecialRegexControl.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/string/index.d.ts +0 -0
- /package/{source/index.d.ts → x/dist/utils/helpers/tests/tmp-schema.test.d.ts} +0 -0
- /package/{source → x/dist}/utils/helpers/timer/index.d.ts +0 -0
- /package/{source → x/dist}/utils/helpers/validates/index.d.ts +0 -0
- /package/{source → x/dist}/utils/models/classes/class.clients.d.ts +0 -0
- /package/{source → x/dist}/utils/models/classes/class.interfaces.d.ts +0 -0
- /package/{source → x/dist}/utils/models/classes/class.service-locator.d.ts +0 -0
- /package/{source → x/dist}/utils/models/classes/class.x.d.ts +0 -0
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
import qs from 'qs';
|
|
2
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import Logger from '../logger';
|
|
4
|
-
import System from '../system';
|
|
5
|
-
import { getClientIp, LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../utils/index';
|
|
6
|
-
export const controllerContextStorage = new AsyncLocalStorage();
|
|
7
|
-
export const getControllerCtx = () => controllerContextStorage.getStore();
|
|
8
|
-
export class BaseController {
|
|
9
|
-
system = x.get(System);
|
|
10
|
-
logger = x.get(Logger);
|
|
11
|
-
supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
|
|
12
|
-
get protocol() {
|
|
13
|
-
return this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
14
|
-
}
|
|
15
|
-
get currentStore() {
|
|
16
|
-
return getControllerCtx();
|
|
17
|
-
}
|
|
18
|
-
get req() {
|
|
19
|
-
const store = this.currentStore;
|
|
20
|
-
const ctx = store?.ctx || {};
|
|
21
|
-
const req = store?.req || {};
|
|
22
|
-
const identityT = (k) => k;
|
|
23
|
-
const body = store?.metadata?._parsedBody || ctx.body || req.body || ctx.request?.body || {};
|
|
24
|
-
let query = store?.metadata?._parsedQuery || ctx.query || req.query || ctx.request?.query || {};
|
|
25
|
-
const urlStr = ctx.request?.url || req.url || ctx.url || "";
|
|
26
|
-
if ((!query || Object.keys(query).length === 0) && urlStr && typeof urlStr === "string") {
|
|
27
|
-
if (urlStr.includes("?")) {
|
|
28
|
-
const queryString = urlStr.split("?")[1];
|
|
29
|
-
if (queryString)
|
|
30
|
-
query = qs.parse(queryString);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const params = {
|
|
34
|
-
...(req.params || {}),
|
|
35
|
-
...(ctx.params || {}),
|
|
36
|
-
...(ctx.request?.params || {}),
|
|
37
|
-
...(ctx.request?.routeParams || {})
|
|
38
|
-
};
|
|
39
|
-
const headers = ctx.headers || req.headers || ctx.request?.headers || {};
|
|
40
|
-
let cookies = ctx.cookies || ctx.cookie || req.cookies || ctx.request?.cookie || ctx.request?.cookies || {};
|
|
41
|
-
if (Object.keys(cookies).length === 0 && headers.cookie) {
|
|
42
|
-
cookies = headers.cookie.split(';').reduce((acc, v) => {
|
|
43
|
-
const parts = v.split('=');
|
|
44
|
-
if (parts.length === 2)
|
|
45
|
-
acc[decodeURIComponent(parts[0].trim())] = decodeURIComponent(parts[1].trim());
|
|
46
|
-
return acc;
|
|
47
|
-
}, {});
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
url: urlStr,
|
|
51
|
-
method: (ctx.method || req.method || ctx.request?.method || "").toUpperCase(),
|
|
52
|
-
headers,
|
|
53
|
-
body,
|
|
54
|
-
query: query || {},
|
|
55
|
-
params: params || {},
|
|
56
|
-
cookies: cookies || {},
|
|
57
|
-
ip: getClientIp(req, ctx),
|
|
58
|
-
lang: ctx.lang || req.lang || "en",
|
|
59
|
-
t: ctx.t || req.t || identityT,
|
|
60
|
-
accounts: ctx.accounts || req.accounts || ctx.user
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
get res() {
|
|
64
|
-
const store = this.currentStore;
|
|
65
|
-
const self = this;
|
|
66
|
-
const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, cookie: function () { return this; }, locals: {} };
|
|
67
|
-
if (!store)
|
|
68
|
-
return fallbackRes;
|
|
69
|
-
if (!store.metadata)
|
|
70
|
-
store.metadata = { locals: {}, _cookiesToSet: {} };
|
|
71
|
-
return {
|
|
72
|
-
get locals() { return store.metadata.locals; },
|
|
73
|
-
status(code) {
|
|
74
|
-
store.metadata._code = code;
|
|
75
|
-
return this;
|
|
76
|
-
},
|
|
77
|
-
cookie(name, value, options = {}) {
|
|
78
|
-
if (!store.metadata._cookiesToSet)
|
|
79
|
-
store.metadata._cookiesToSet = {};
|
|
80
|
-
store.metadata._cookiesToSet[name] = { value, options };
|
|
81
|
-
const ctx = store.ctx;
|
|
82
|
-
if (ctx?.res?.cookie)
|
|
83
|
-
ctx.res.cookie(name, value, options);
|
|
84
|
-
return this;
|
|
85
|
-
},
|
|
86
|
-
send(data) {
|
|
87
|
-
if (self.protocol === ProtocolEnum.WEBSOCKET)
|
|
88
|
-
return data;
|
|
89
|
-
if (data instanceof Response)
|
|
90
|
-
return data;
|
|
91
|
-
const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer || (typeof data?.pipe === "function");
|
|
92
|
-
const isRaw = store.metadata?._isRaw || isBinary;
|
|
93
|
-
let contentType = store.metadata?._contentType || (isRaw ? "application/octet-stream" : "application/json");
|
|
94
|
-
let bizCode = store.metadata._code || 200;
|
|
95
|
-
let httpStatus = store.metadata._statusCode;
|
|
96
|
-
if (!httpStatus) {
|
|
97
|
-
httpStatus = (bizCode >= 100 && bizCode <= 101) || (bizCode >= 200 && bizCode <= 599) ? bizCode : (bizCode === 0 || (bizCode >= 200 && bizCode <= 299) ? 200 : 400);
|
|
98
|
-
}
|
|
99
|
-
const responseHeaders = new Headers();
|
|
100
|
-
responseHeaders.set("content-type", contentType);
|
|
101
|
-
if (store.metadata._cookiesToSet) {
|
|
102
|
-
Object.entries(store.metadata._cookiesToSet).forEach(([name, { value, options }]) => {
|
|
103
|
-
let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
104
|
-
if (options.expires)
|
|
105
|
-
cookieStr += `; Expires=${options.expires.toUTCString()}`;
|
|
106
|
-
if (options.maxAge)
|
|
107
|
-
cookieStr += `; Max-Age=${options.maxAge}`;
|
|
108
|
-
if (options.domain)
|
|
109
|
-
cookieStr += `; Domain=${options.domain}`;
|
|
110
|
-
if (options.path)
|
|
111
|
-
cookieStr += `; Path=${options.path}`;
|
|
112
|
-
if (options.secure)
|
|
113
|
-
cookieStr += `; Secure`;
|
|
114
|
-
if (options.httpOnly)
|
|
115
|
-
cookieStr += `; HttpOnly`;
|
|
116
|
-
if (options.sameSite)
|
|
117
|
-
cookieStr += `; SameSite=${options.sameSite}`;
|
|
118
|
-
responseHeaders.append("set-cookie", cookieStr);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
const body = isRaw || typeof data === "string" ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
|
|
122
|
-
return new Response(body, { status: httpStatus, headers: responseHeaders });
|
|
123
|
-
},
|
|
124
|
-
json(data) { return this.send(data); }
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
export default class Controller extends BaseController {
|
|
129
|
-
constructor() { super(); }
|
|
130
|
-
async onInit() {
|
|
131
|
-
const protocols = this.system?.tmp?.configs?.loaders?.controller?.protocols;
|
|
132
|
-
this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
|
|
133
|
-
}
|
|
134
|
-
parseMessage(msg, t, isError = false) {
|
|
135
|
-
const content = String(msg || "");
|
|
136
|
-
if (!content)
|
|
137
|
-
return isError ? responseMessageHelper.unsuccessful("", "", t) : responseMessageHelper.successfully("", "", t);
|
|
138
|
-
let method = isError ? "unsuccessful" : "successfully";
|
|
139
|
-
let payloadString = "";
|
|
140
|
-
if (content.startsWith("@")) {
|
|
141
|
-
const parts = content.split(" ");
|
|
142
|
-
method = parts[0].substring(1);
|
|
143
|
-
payloadString = parts.slice(1).join(" ");
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
payloadString = content;
|
|
147
|
-
}
|
|
148
|
-
const params = payloadString ? payloadString.split("-").map(p => p.trim()).filter(p => p !== "") : [];
|
|
149
|
-
const p1 = params.length > 0 ? params[0] : (content.startsWith("@") ? undefined : "");
|
|
150
|
-
const p2 = params.length > 1 ? params.slice(1).join(" ") : (content.startsWith("@") ? undefined : "");
|
|
151
|
-
if (responseMessageHelper[method]) {
|
|
152
|
-
return responseMessageHelper[method](p1, p2, t);
|
|
153
|
-
}
|
|
154
|
-
return isError ? responseMessageHelper.unsuccessful(p1 || "", p2 || "", t) : responseMessageHelper.successfully(p1 || "", p2 || "", t);
|
|
155
|
-
}
|
|
156
|
-
async schema({ checks, logic, response }) {
|
|
157
|
-
const currentRes = this.res;
|
|
158
|
-
const store = this.currentStore;
|
|
159
|
-
try {
|
|
160
|
-
const rawReq = store?.req || store?.ctx?.request || store?.ctx?.req;
|
|
161
|
-
const currentReqBase = this.req;
|
|
162
|
-
const method = currentReqBase.method;
|
|
163
|
-
const headers = currentReqBase.headers;
|
|
164
|
-
const contentType = (headers["content-type"] || "").toLowerCase();
|
|
165
|
-
const contentLength = parseInt(headers["content-length"] || "-1");
|
|
166
|
-
let parsedBody = currentReqBase.body;
|
|
167
|
-
const parseText = (text) => {
|
|
168
|
-
if (!text)
|
|
169
|
-
return {};
|
|
170
|
-
if (contentType.includes("application/json")) {
|
|
171
|
-
try {
|
|
172
|
-
return JSON.parse(text);
|
|
173
|
-
}
|
|
174
|
-
catch {
|
|
175
|
-
return text;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
179
|
-
return qs.parse(text);
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
return JSON.parse(text);
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
try {
|
|
186
|
-
return qs.parse(text);
|
|
187
|
-
}
|
|
188
|
-
catch {
|
|
189
|
-
return text;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
const isStream = (obj) => obj instanceof ReadableStream || (obj && typeof obj.getReader === "function");
|
|
194
|
-
const isProcessed = parsedBody && typeof parsedBody === "object" && !isStream(parsedBody) && Object.keys(parsedBody).length > 0;
|
|
195
|
-
if (this.protocol !== ProtocolEnum.WEBSOCKET && !["GET", "HEAD", "OPTIONS"].includes(method) && !isProcessed) {
|
|
196
|
-
const isModern = rawReq && typeof rawReq.json === "function" && typeof rawReq.text === "function";
|
|
197
|
-
const isNode = rawReq && typeof rawReq.on === "function";
|
|
198
|
-
if (isModern && (isStream(parsedBody) || contentLength > 0)) {
|
|
199
|
-
try {
|
|
200
|
-
const cloned = rawReq.clone();
|
|
201
|
-
parsedBody = parseText(await cloned.text());
|
|
202
|
-
}
|
|
203
|
-
catch {
|
|
204
|
-
if (typeof parsedBody === "string")
|
|
205
|
-
parsedBody = parseText(parsedBody);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
else if (isNode && contentLength !== 0) {
|
|
209
|
-
parsedBody = await new Promise((resolve) => {
|
|
210
|
-
let bodyStr = "";
|
|
211
|
-
const timer = setTimeout(() => resolve({}), 200);
|
|
212
|
-
rawReq.on("data", (chunk) => { bodyStr += chunk; });
|
|
213
|
-
rawReq.on("end", () => { clearTimeout(timer); resolve(parseText(bodyStr)); });
|
|
214
|
-
rawReq.on("error", () => { clearTimeout(timer); resolve({}); });
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
else if (typeof parsedBody === "string" && parsedBody.length > 0) {
|
|
218
|
-
parsedBody = parseText(parsedBody);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (parsedBody && typeof parsedBody === "object" && parsedBody.constructor?.name === "FormData") {
|
|
222
|
-
parsedBody = Object.fromEntries(parsedBody.entries());
|
|
223
|
-
}
|
|
224
|
-
if (store) {
|
|
225
|
-
if (!store.metadata)
|
|
226
|
-
store.metadata = { locals: {} };
|
|
227
|
-
store.metadata._parsedBody = parsedBody || {};
|
|
228
|
-
store.metadata._parsedQuery = currentReqBase.query;
|
|
229
|
-
}
|
|
230
|
-
const currentReq = this.req;
|
|
231
|
-
const p = {
|
|
232
|
-
req: currentReq,
|
|
233
|
-
res: currentRes,
|
|
234
|
-
body: currentReq.body,
|
|
235
|
-
query: currentReq.query,
|
|
236
|
-
queries: currentReq.query,
|
|
237
|
-
params: currentReq.params,
|
|
238
|
-
cookies: currentReq.cookies,
|
|
239
|
-
locals: currentRes.locals,
|
|
240
|
-
t: currentReq.t,
|
|
241
|
-
};
|
|
242
|
-
const extractMeta = (obj) => {
|
|
243
|
-
if (!obj || typeof obj !== "object")
|
|
244
|
-
return;
|
|
245
|
-
if (obj.code !== undefined && store?.metadata)
|
|
246
|
-
store.metadata._code = obj.code;
|
|
247
|
-
if (obj.statusCode !== undefined && store?.metadata)
|
|
248
|
-
store.metadata._statusCode = obj.statusCode;
|
|
249
|
-
};
|
|
250
|
-
if (checks) {
|
|
251
|
-
const checkResult = await checks(p);
|
|
252
|
-
if (checkResult !== true) {
|
|
253
|
-
extractMeta(checkResult);
|
|
254
|
-
const bizCode = store?.metadata?._code ?? 400;
|
|
255
|
-
let errorPayload = undefined;
|
|
256
|
-
if (checkResult?.payload) {
|
|
257
|
-
const d = checkResult.payload.data ?? checkResult.payload;
|
|
258
|
-
errorPayload = { data: d };
|
|
259
|
-
if (checkResult.payload.extraData !== undefined)
|
|
260
|
-
errorPayload.extraData = checkResult.payload.extraData;
|
|
261
|
-
}
|
|
262
|
-
return currentRes.status(bizCode).send(new ResponseSchema({ status: false, message: this.parseMessage(checkResult?.message || checkResult, currentReq.t, true), code: bizCode, payload: errorPayload }).getResponse);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
let logicResult = logic ? await logic(p) : {};
|
|
266
|
-
extractMeta(logicResult);
|
|
267
|
-
if (logicResult instanceof Response || logicResult?.constructor?.name === "Response")
|
|
268
|
-
return logicResult;
|
|
269
|
-
if (logicResult && typeof logicResult === "object" && typeof logicResult.response === "function") {
|
|
270
|
-
logicResult = await logicResult.response();
|
|
271
|
-
extractMeta(logicResult);
|
|
272
|
-
}
|
|
273
|
-
if (logicResult?.status === false) {
|
|
274
|
-
const bizCode = store?.metadata?._code ?? 400;
|
|
275
|
-
return currentRes.status(bizCode).send(new ResponseSchema({ status: false, message: this.parseMessage(logicResult.message, currentReq.t, true), code: bizCode }).getResponse);
|
|
276
|
-
}
|
|
277
|
-
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
278
|
-
extractMeta(resResult);
|
|
279
|
-
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
280
|
-
return currentRes.status(200).send(resResult);
|
|
281
|
-
let finalData = undefined;
|
|
282
|
-
let finalExtraData = undefined;
|
|
283
|
-
let messageSource = "success_process";
|
|
284
|
-
const extractData = (obj) => {
|
|
285
|
-
if (!obj || typeof obj !== "object")
|
|
286
|
-
return;
|
|
287
|
-
if (obj.payload && typeof obj.payload === "object") {
|
|
288
|
-
finalData = obj.payload.data !== undefined ? obj.payload.data : obj.payload;
|
|
289
|
-
finalExtraData = obj.payload.extraData;
|
|
290
|
-
}
|
|
291
|
-
else if (obj.data !== undefined || obj.extraData !== undefined) {
|
|
292
|
-
finalData = obj.data;
|
|
293
|
-
finalExtraData = obj.extraAddr;
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined || obj.statusCode !== undefined;
|
|
297
|
-
if (!hasMeta && Object.keys(obj).length > 0)
|
|
298
|
-
finalData = obj;
|
|
299
|
-
}
|
|
300
|
-
if (obj.message)
|
|
301
|
-
messageSource = obj.message;
|
|
302
|
-
};
|
|
303
|
-
if (typeof resResult === "string") {
|
|
304
|
-
messageSource = resResult;
|
|
305
|
-
extractData(logicResult);
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
extractData(resResult !== undefined ? resResult : logicResult);
|
|
309
|
-
}
|
|
310
|
-
const bizSuccessCode = store?.metadata?._code || 200;
|
|
311
|
-
let payload = undefined;
|
|
312
|
-
if (finalData !== undefined || finalExtraData !== undefined) {
|
|
313
|
-
payload = {};
|
|
314
|
-
if (finalData !== undefined)
|
|
315
|
-
payload.data = finalData;
|
|
316
|
-
if (finalExtraData !== undefined)
|
|
317
|
-
payload.extraData = finalExtraData;
|
|
318
|
-
}
|
|
319
|
-
return currentRes.status(bizSuccessCode).send(new ResponseSchema({ status: true, message: this.parseMessage(messageSource, currentReq.t, false), payload, code: bizSuccessCode }).getResponse);
|
|
320
|
-
}
|
|
321
|
-
catch (error) {
|
|
322
|
-
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
323
|
-
const t = this.req?.t || ((k) => k);
|
|
324
|
-
return this.res.status(500).send(new ResponseSchema({ status: false, message: this.parseMessage(error.message, t, true), code: 500 }).getResponse);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { LoggerLayerEnum, x } from '../../utils';
|
|
2
|
-
export default class Events {
|
|
3
|
-
#logger;
|
|
4
|
-
constructor({ logger }) {
|
|
5
|
-
this.#logger = logger;
|
|
6
|
-
}
|
|
7
|
-
onInit = ({}) => {
|
|
8
|
-
this._globalLoader();
|
|
9
|
-
this._DILoader();
|
|
10
|
-
};
|
|
11
|
-
_globalLoader = () => {
|
|
12
|
-
process.on("uncaughtException", (error, origin) => {
|
|
13
|
-
this.#logger.log({
|
|
14
|
-
level: LoggerLayerEnum.CRITICAL,
|
|
15
|
-
message: `Uncaught Exception: ${error.message}`,
|
|
16
|
-
payload: {
|
|
17
|
-
stack: error.stack,
|
|
18
|
-
origin: origin
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
process.on("unhandledRejection", (reason, promise) => {
|
|
23
|
-
const error = reason instanceof Error ? reason : new Error(String(reason));
|
|
24
|
-
this.#logger.log({
|
|
25
|
-
level: LoggerLayerEnum.CRITICAL,
|
|
26
|
-
message: `Unhandled Rejection: ${error.message}`,
|
|
27
|
-
payload: {
|
|
28
|
-
stack: error.stack,
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
_DILoader = () => {
|
|
34
|
-
process.on('SIGTERM', () => {
|
|
35
|
-
x.shutdown();
|
|
36
|
-
});
|
|
37
|
-
process.on('SIGINT', () => {
|
|
38
|
-
x.shutdown();
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
get global() {
|
|
42
|
-
return this._globalLoader();
|
|
43
|
-
}
|
|
44
|
-
}
|
package/source/loader/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import System from "./system";
|
|
2
|
-
import Configs from "./configs";
|
|
3
|
-
import KafkaForCore from "./kafka";
|
|
4
|
-
import Logger from "./logger";
|
|
5
|
-
import Events from "./events";
|
|
6
|
-
import Localizations from "./localizations";
|
|
7
|
-
import Controller from "./controller";
|
|
8
|
-
import Service from "./service";
|
|
9
|
-
import Clients from "./clients";
|
|
10
|
-
export { System, Configs, Logger, Events, Localizations, Controller, Service, Clients, KafkaForCore };
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { Kafka as KafkaClient, logLevel } from "kafkajs";
|
|
2
|
-
export default class KafkaForCore {
|
|
3
|
-
_instance;
|
|
4
|
-
_producer;
|
|
5
|
-
clientId = "";
|
|
6
|
-
brokers = "";
|
|
7
|
-
username = null;
|
|
8
|
-
password = null;
|
|
9
|
-
#system;
|
|
10
|
-
#configs;
|
|
11
|
-
constructor({ system, configs, }) {
|
|
12
|
-
this.#system = system;
|
|
13
|
-
this.#configs = configs;
|
|
14
|
-
}
|
|
15
|
-
onInit = async ({}) => {
|
|
16
|
-
this.clientId = this.#configs.all?.kafkaClientId;
|
|
17
|
-
this.brokers = this.#configs.all?.kafkaBrokers;
|
|
18
|
-
this.username = this.#configs.all?.kafkaUsername;
|
|
19
|
-
this.password = this.#configs.all?.kafkaPassword;
|
|
20
|
-
if (this.#configs.all.isKafkaPassive) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const brokersArray = (Array.isArray(this.brokers) ? this.brokers : (this.brokers?.split(',') ?? []))
|
|
24
|
-
.map(b => b?.trim())
|
|
25
|
-
.filter(Boolean);
|
|
26
|
-
if (brokersArray?.length === 0) {
|
|
27
|
-
throw new Error('Broker list error');
|
|
28
|
-
}
|
|
29
|
-
await this.kafkaLoader({
|
|
30
|
-
clientId: this.clientId,
|
|
31
|
-
brokers: brokersArray,
|
|
32
|
-
username: this.username,
|
|
33
|
-
password: this.password
|
|
34
|
-
});
|
|
35
|
-
await this.initializeKafkaInfrastructure();
|
|
36
|
-
};
|
|
37
|
-
kafkaLoader = async ({ clientId, brokers, username, password }) => {
|
|
38
|
-
const kafkaConfig = {
|
|
39
|
-
clientId,
|
|
40
|
-
brokers,
|
|
41
|
-
sasl: (username && password) ? { mechanism: 'plain', username, password } : undefined,
|
|
42
|
-
ssl: false,
|
|
43
|
-
logLevel: logLevel.ERROR,
|
|
44
|
-
logCreator: () => {
|
|
45
|
-
return ({ level, log }) => {
|
|
46
|
-
console.error(`Kafka ${log.message}`);
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
retry: {
|
|
50
|
-
initialRetryTime: 100,
|
|
51
|
-
retries: 8
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
try {
|
|
55
|
-
const kafka = new KafkaClient(kafkaConfig);
|
|
56
|
-
this._instance = kafka;
|
|
57
|
-
this._producer = kafka.producer();
|
|
58
|
-
await this.producer.connect();
|
|
59
|
-
return kafka;
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
console.error(`Kafka ${error}`);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
initializeKafkaInfrastructure = async () => {
|
|
66
|
-
const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
|
|
67
|
-
isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
|
|
68
|
-
kafkaBrokers: process.env?.KAFKA_BROKERS,
|
|
69
|
-
kafkaTopics: [
|
|
70
|
-
...new Set([
|
|
71
|
-
...this.#configs.all.kafkaTopics
|
|
72
|
-
])
|
|
73
|
-
],
|
|
74
|
-
};
|
|
75
|
-
if (isKafkaPassive === true || !kafkaBrokers) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
|
|
79
|
-
if (topicsToCreate.length === 0)
|
|
80
|
-
return;
|
|
81
|
-
const admin = this._instance.admin();
|
|
82
|
-
try {
|
|
83
|
-
await admin.connect();
|
|
84
|
-
const existingTopics = await admin.listTopics();
|
|
85
|
-
const newTopics = topicsToCreate
|
|
86
|
-
.filter(topic => !existingTopics.includes(topic))
|
|
87
|
-
.map(topic => ({
|
|
88
|
-
topic,
|
|
89
|
-
numPartitions: 1,
|
|
90
|
-
replicationFactor: 1
|
|
91
|
-
}));
|
|
92
|
-
if (newTopics.length > 0) {
|
|
93
|
-
await admin.createTopics({
|
|
94
|
-
waitForLeaders: true,
|
|
95
|
-
topics: newTopics
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
//console.log('Core kafka error', error)
|
|
101
|
-
}
|
|
102
|
-
finally {
|
|
103
|
-
await admin.disconnect();
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
// => Helpers
|
|
107
|
-
sendMessage = async (topic, message) => {
|
|
108
|
-
if (!this.producer)
|
|
109
|
-
throw new Error("Producer not ready");
|
|
110
|
-
await this.producer.send({
|
|
111
|
-
topic,
|
|
112
|
-
messages: [{ value: JSON.stringify(message) }],
|
|
113
|
-
acks: 0
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
get producer() {
|
|
117
|
-
return this._producer;
|
|
118
|
-
}
|
|
119
|
-
get instance() {
|
|
120
|
-
return this._instance;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import i18next from "i18next";
|
|
3
|
-
import Backend from 'i18next-fs-backend';
|
|
4
|
-
import * as middleware from 'i18next-http-middleware';
|
|
5
|
-
export default class Localizations {
|
|
6
|
-
_instance = null;
|
|
7
|
-
#system;
|
|
8
|
-
#configs;
|
|
9
|
-
constructor({ system, configs, }) {
|
|
10
|
-
this.#system = system;
|
|
11
|
-
this.#configs = configs;
|
|
12
|
-
}
|
|
13
|
-
onInit = ({}) => {
|
|
14
|
-
const localization = this.#system.tmp.configs.loaders.localization;
|
|
15
|
-
this.i18next({
|
|
16
|
-
loadPath: path.join(this.#system.tmp.configs.rootFolderPath, localization.loadPath),
|
|
17
|
-
fallbackLang: localization.fallbackLang,
|
|
18
|
-
preloadLang: localization.preloadLangs,
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
i18next = ({ loadPath, fallbackLang, preloadLang, }) => {
|
|
22
|
-
if (this._instance) {
|
|
23
|
-
return this._instance;
|
|
24
|
-
}
|
|
25
|
-
i18next
|
|
26
|
-
.use(Backend)
|
|
27
|
-
.use(middleware.LanguageDetector)
|
|
28
|
-
.init({
|
|
29
|
-
fallbackLng: fallbackLang,
|
|
30
|
-
preload: preloadLang,
|
|
31
|
-
backend: { loadPath: `${loadPath}/{{lng}}.json` }
|
|
32
|
-
});
|
|
33
|
-
if (i18next)
|
|
34
|
-
this._instance = i18next;
|
|
35
|
-
return i18next;
|
|
36
|
-
};
|
|
37
|
-
get instance() {
|
|
38
|
-
return this._instance;
|
|
39
|
-
}
|
|
40
|
-
}
|