koatty 4.1.6 → 4.1.8
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/.turbo/turbo-build.log +39 -21
- package/CHANGELOG.md +25 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +753 -478
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +704 -409
- package/dist/index.mjs.map +1 -0
- package/dist/package.json +32 -41
- package/package.json +29 -38
- package/tsup.config.ts +6 -0
package/dist/index.mjs
CHANGED
|
@@ -1,501 +1,796 @@
|
|
|
1
|
+
import { LoadConfigs } from 'koatty_config';
|
|
2
|
+
export { Config } from 'koatty_config';
|
|
3
|
+
import { IOC, TAGGED_CLS } from 'koatty_container';
|
|
4
|
+
export * from 'koatty_container';
|
|
5
|
+
import { AppEvent, AppEventArr, asyncEvent, ComponentManager, implementsMiddlewareInterface, MIDDLEWARE_OPTIONS, protocolMiddleware, implementsControllerInterface, implementsServiceInterface, implementsAspectInterface, Koatty } from 'koatty_core';
|
|
6
|
+
export * from 'koatty_core';
|
|
7
|
+
export * from 'koatty_exception';
|
|
8
|
+
import { Helper } from 'koatty_lib';
|
|
9
|
+
export { Helper } from 'koatty_lib';
|
|
10
|
+
export * from 'koatty_router';
|
|
11
|
+
export * from 'koatty_serve';
|
|
12
|
+
import { DefaultLogger } from 'koatty_logger';
|
|
13
|
+
export { Log, LogLevelType, DefaultLogger as Logger } from 'koatty_logger';
|
|
14
|
+
import { Load } from 'koatty_loader';
|
|
15
|
+
import { Trace } from 'koatty_trace';
|
|
16
|
+
import * as path from 'path';
|
|
17
|
+
|
|
1
18
|
/*!
|
|
2
19
|
* @Author: richen
|
|
3
|
-
* @Date: 2026-02-
|
|
20
|
+
* @Date: 2026-02-10 09:13:30
|
|
4
21
|
* @License: BSD (3-Clause)
|
|
5
22
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
23
|
* @HomePage: https://koatty.org/
|
|
7
24
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export * from "koatty_container";
|
|
15
|
-
|
|
16
|
-
import { AppEvent as a, AppEventArr as s, asyncEvent as n, ComponentManager as i, implementsMiddlewareInterface as r, MIDDLEWARE_OPTIONS as l, protocolMiddleware as c, implementsControllerInterface as d, implementsServiceInterface as f, implementsAspectInterface as p, Koatty as g } from "koatty_core";
|
|
17
|
-
|
|
18
|
-
export * from "koatty_core";
|
|
19
|
-
|
|
20
|
-
export * from "koatty_exception";
|
|
21
|
-
|
|
22
|
-
import { Helper as m } from "koatty_lib";
|
|
23
|
-
|
|
24
|
-
export { Helper } from "koatty_lib";
|
|
25
|
-
|
|
26
|
-
export * from "koatty_router";
|
|
27
|
-
|
|
28
|
-
export * from "koatty_serve";
|
|
29
|
-
|
|
30
|
-
import { DefaultLogger as u } from "koatty_logger";
|
|
31
|
-
|
|
32
|
-
export { Log, DefaultLogger as Logger } from "koatty_logger";
|
|
33
|
-
|
|
34
|
-
import { Load as h } from "koatty_loader";
|
|
35
|
-
|
|
36
|
-
import { Trace as y } from "koatty_trace";
|
|
37
|
-
|
|
38
|
-
import * as L from "path";
|
|
39
|
-
|
|
40
|
-
/*
|
|
41
|
-
* @Description: framework helper
|
|
42
|
-
* @Usage:
|
|
43
|
-
* @Author: richen
|
|
44
|
-
* @Date: 2023-12-09 21:56:32
|
|
45
|
-
* @LastEditTime: 2025-01-14 16:14:10
|
|
46
|
-
* @License: BSD (3-Clause)
|
|
47
|
-
* @Copyright (c): <richenlin(at)gmail.com>
|
|
48
|
-
*/ const v = require("../../package.json");
|
|
49
|
-
|
|
50
|
-
const {engines: w, version: C} = v;
|
|
51
|
-
|
|
52
|
-
const E = C;
|
|
53
|
-
|
|
54
|
-
const b = w.node.slice(1) || "12.0.0";
|
|
25
|
+
var __defProp = Object.defineProperty;
|
|
26
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
27
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
28
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
29
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
30
|
+
};
|
|
55
31
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
32
|
+
// package.json
|
|
33
|
+
var require_package = __commonJS({
|
|
34
|
+
"package.json"(exports$1, module) {
|
|
35
|
+
module.exports = {
|
|
36
|
+
name: "koatty",
|
|
37
|
+
version: "4.1.8",
|
|
38
|
+
description: "Koa + Typescript = koatty. Use Typescript's decorator implement auto injection.",
|
|
39
|
+
scripts: {
|
|
40
|
+
build: "pnpm run build:js && pnpm run build:dts && pnpm run build:doc && pnpm run build:cp",
|
|
41
|
+
"build:js": "tsup",
|
|
42
|
+
"build:dts": "bash scripts/build-dts.sh",
|
|
43
|
+
"build:doc": "api-documenter markdown --input temp --output docs/api",
|
|
44
|
+
"build:cp": "node scripts/copyFiles && node ../../scripts/postBuild.js",
|
|
45
|
+
clean: "rimraf dist temp docs/api",
|
|
46
|
+
lint: "eslint --ext .ts,.js ./src",
|
|
47
|
+
test: "pnpm run lint && jest --passWithNoTests",
|
|
48
|
+
"test:cov": "jest --collectCoverage --detectOpenHandles"
|
|
49
|
+
},
|
|
50
|
+
main: "./dist/index.js",
|
|
51
|
+
exports: {
|
|
52
|
+
".": {
|
|
53
|
+
types: "./dist/index.d.ts",
|
|
54
|
+
import: "./dist/index.mjs",
|
|
55
|
+
require: "./dist/index.js"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
keywords: [
|
|
59
|
+
"koatty",
|
|
60
|
+
"koa",
|
|
61
|
+
"typescript",
|
|
62
|
+
"framework",
|
|
63
|
+
"mvc",
|
|
64
|
+
"http",
|
|
65
|
+
"https",
|
|
66
|
+
"websocket",
|
|
67
|
+
"grpc",
|
|
68
|
+
"quic",
|
|
69
|
+
"http2",
|
|
70
|
+
"http3",
|
|
71
|
+
"di",
|
|
72
|
+
"aop",
|
|
73
|
+
"ioc",
|
|
74
|
+
"inversion of control",
|
|
75
|
+
"dependency injection",
|
|
76
|
+
"aspect-oriented programming",
|
|
77
|
+
"decorator",
|
|
78
|
+
"decorator pattern",
|
|
79
|
+
"decorator factory"
|
|
80
|
+
],
|
|
81
|
+
directories: {
|
|
82
|
+
doc: "docs"
|
|
83
|
+
},
|
|
84
|
+
repository: {
|
|
85
|
+
type: "git",
|
|
86
|
+
url: "git+https://github.com/koatty/koatty.git"
|
|
87
|
+
},
|
|
88
|
+
engines: {
|
|
89
|
+
node: ">=18.0.0"
|
|
90
|
+
},
|
|
91
|
+
author: {
|
|
92
|
+
name: "richenlin",
|
|
93
|
+
email: "richenlin@gmail.com"
|
|
94
|
+
},
|
|
95
|
+
license: "BSD-3-Clause",
|
|
96
|
+
bugs: {
|
|
97
|
+
url: "https://github.com/koatty/koatty/issues"
|
|
98
|
+
},
|
|
99
|
+
homepage: "https://github.com/koatty/koatty",
|
|
100
|
+
maintainers: [
|
|
101
|
+
{
|
|
102
|
+
name: "richenlin",
|
|
103
|
+
email: "richenlin@gmail.com"
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
devDependencies: {
|
|
107
|
+
"@grpc/grpc-js": "^1.12.2",
|
|
108
|
+
"@microsoft/api-documenter": "^7.26.x",
|
|
109
|
+
"@microsoft/api-extractor": "^7.52.x",
|
|
110
|
+
"@types/formidable": "^3.x.x",
|
|
111
|
+
"@types/jest": "^29.5.x",
|
|
112
|
+
"@types/koa": "^2.x.x",
|
|
113
|
+
"@types/koa-compose": "^3.x.x",
|
|
114
|
+
"@types/lodash": "^4.x.x",
|
|
115
|
+
"@types/node": "^22.x.x",
|
|
116
|
+
"@types/on-finished": "^2.x.x",
|
|
117
|
+
"@types/supertest": "^6.x.x",
|
|
118
|
+
"@types/ws": "^8.x.x",
|
|
119
|
+
"@typescript-eslint/eslint-plugin": "^8.x.x",
|
|
120
|
+
"@typescript-eslint/parser": "^8.x.x",
|
|
121
|
+
eslint: "^8.x.x",
|
|
122
|
+
"eslint-plugin-jest": "^28.x.x",
|
|
123
|
+
jest: "^29.x.x",
|
|
124
|
+
"jest-html-reporters": "^3.x.x",
|
|
125
|
+
koatty_validation: "workspace:*",
|
|
126
|
+
"reflect-metadata": "^0.2.2",
|
|
127
|
+
supertest: "^7.x.x",
|
|
128
|
+
"ts-jest": "^29.x.x",
|
|
129
|
+
tslib: "^2.x.x"
|
|
130
|
+
},
|
|
131
|
+
dependencies: {
|
|
132
|
+
koa: "^3.1.1",
|
|
133
|
+
koatty_config: "workspace:*",
|
|
134
|
+
koatty_container: "workspace:*",
|
|
135
|
+
koatty_core: "workspace:*",
|
|
136
|
+
koatty_exception: "workspace:*",
|
|
137
|
+
koatty_lib: "workspace:*",
|
|
138
|
+
koatty_loader: "workspace:*",
|
|
139
|
+
koatty_logger: "workspace:*",
|
|
140
|
+
koatty_router: "workspace:*",
|
|
141
|
+
koatty_serve: "workspace:*",
|
|
142
|
+
koatty_trace: "workspace:*",
|
|
143
|
+
"ts-morph": "^27.0.2"
|
|
144
|
+
},
|
|
145
|
+
types: "./dist/index.d.ts"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
var pkg = require_package();
|
|
150
|
+
var { engines, version } = pkg;
|
|
151
|
+
var KOATTY_VERSION = version;
|
|
152
|
+
var ENGINES_VERSION = engines.node.slice(1) || "12.0.0";
|
|
153
|
+
function checkClass(fileName, xpath, target, exSet) {
|
|
154
|
+
if (Helper.isClass(target) && target.name != fileName) {
|
|
155
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
|
156
|
+
}
|
|
157
|
+
if (target["__esModule"]) {
|
|
158
|
+
if (target.name === void 0) {
|
|
159
|
+
const keys = Object.keys(target);
|
|
160
|
+
if (keys[0] != fileName && Helper.isClass(target[keys[0]])) {
|
|
161
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
|
162
|
+
}
|
|
163
|
+
} else if (target.name != fileName) {
|
|
164
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!exSet) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (exSet.has(fileName)) {
|
|
171
|
+
throw new Error(`A same class already exists. at \`${xpath}\`.`);
|
|
172
|
+
}
|
|
173
|
+
exSet.add(fileName);
|
|
65
174
|
return;
|
|
66
175
|
}
|
|
67
|
-
|
|
68
|
-
function
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
let
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
176
|
+
__name(checkClass, "checkClass");
|
|
177
|
+
function checkRuntime() {
|
|
178
|
+
let nodeEngines = ENGINES_VERSION;
|
|
179
|
+
nodeEngines = nodeEngines.slice(0, nodeEngines.lastIndexOf("."));
|
|
180
|
+
let nodeVersion = process.version;
|
|
181
|
+
if (nodeVersion[0] === "v") {
|
|
182
|
+
nodeVersion = nodeVersion.slice(1);
|
|
183
|
+
}
|
|
184
|
+
nodeVersion = nodeVersion.slice(0, nodeVersion.lastIndexOf("."));
|
|
185
|
+
if (Helper.toNumber(nodeEngines) > Helper.toNumber(nodeVersion)) {
|
|
186
|
+
DefaultLogger.Fatal(`Koatty need node version > ${nodeEngines}, current version is ${nodeVersion}, please upgrade it.`);
|
|
187
|
+
}
|
|
75
188
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
let
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
* @Author: richen
|
|
101
|
-
* @Date: 2023-12-09 22:55:49
|
|
102
|
-
* @LastEditTime: 2025-03-13 16:44:39
|
|
103
|
-
* @License: BSD (3-Clause)
|
|
104
|
-
* @Copyright (c): <richenlin(at)gmail.com>
|
|
105
|
-
*/;
|
|
106
|
-
|
|
107
|
-
class Loader {
|
|
108
|
-
constructor(t) {
|
|
109
|
-
this.app = t;
|
|
189
|
+
__name(checkRuntime, "checkRuntime");
|
|
190
|
+
var checkUTRuntime = /* @__PURE__ */ __name(() => {
|
|
191
|
+
let isUTRuntime = false;
|
|
192
|
+
const argv = JSON.stringify(process.argv[1]);
|
|
193
|
+
if (argv.indexOf("jest") > -1) {
|
|
194
|
+
isUTRuntime = true;
|
|
195
|
+
}
|
|
196
|
+
return isUTRuntime;
|
|
197
|
+
}, "checkUTRuntime");
|
|
198
|
+
|
|
199
|
+
// src/core/Constants.ts
|
|
200
|
+
var COMPONENT_SCAN = "COMPONENT_SCAN";
|
|
201
|
+
var CONFIGURATION_SCAN = "CONFIGURATION_SCAN";
|
|
202
|
+
var LOGO = `
|
|
203
|
+
|
|
204
|
+
\u252C\u250C\u2500\u250C\u2500\u2510\u250C\u2500\u2510\u250C\u252C\u2510\u250C\u252C\u2510\u252C \u252C
|
|
205
|
+
\u251C\u2534\u2510\u2502 \u2502\u251C\u2500\u2524 \u2502 \u2502 \u2514\u252C\u2518
|
|
206
|
+
\u2534 \u2534\u2514\u2500\u2518\u2534 \u2534 \u2534 \u2534 \u2534
|
|
207
|
+
-------------------------------------------
|
|
208
|
+
https://github.com/koatty
|
|
209
|
+
`;
|
|
210
|
+
var Loader = class _Loader {
|
|
211
|
+
static {
|
|
212
|
+
__name(this, "Loader");
|
|
110
213
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
214
|
+
app;
|
|
215
|
+
/**
|
|
216
|
+
* Creates an instance of Loader.
|
|
217
|
+
* @param {KoattyApplication} app
|
|
218
|
+
* @memberof Loader
|
|
219
|
+
*/
|
|
220
|
+
constructor(app) {
|
|
221
|
+
this.app = app;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Initialize application configuration and environment settings.
|
|
225
|
+
* Sets up logging levels, defines essential paths, and loads application metadata.
|
|
226
|
+
*
|
|
227
|
+
* @param {KoattyApplication} app - The Koatty application instance
|
|
228
|
+
* - Sets logging level based on environment
|
|
229
|
+
* - Defines root, app and framework paths on app object
|
|
230
|
+
* - Loads application name and version from package.json
|
|
231
|
+
* - Sets environment variables for paths
|
|
232
|
+
* - Maintains backward compatibility with legacy path variables
|
|
233
|
+
*/
|
|
234
|
+
static initialize(app) {
|
|
235
|
+
if (app.env == "development") {
|
|
236
|
+
DefaultLogger.setLevel("debug");
|
|
237
|
+
} else {
|
|
238
|
+
DefaultLogger.setLevel("info");
|
|
239
|
+
}
|
|
240
|
+
const rootPath = app.rootPath || process.cwd();
|
|
241
|
+
const appPath = app.appPath || path.resolve(rootPath, app.appDebug ? "src" : "dist");
|
|
242
|
+
const koattyPath = path.resolve(__dirname, "..");
|
|
243
|
+
Helper.define(app, "rootPath", rootPath);
|
|
244
|
+
Helper.define(app, "appPath", appPath);
|
|
245
|
+
Helper.define(app, "koattyPath", koattyPath);
|
|
246
|
+
if (Helper.isEmpty(app.name)) {
|
|
247
|
+
const pkg2 = Helper.safeRequire(`${path.dirname(appPath)}/package.json`);
|
|
248
|
+
if (pkg2.name) {
|
|
249
|
+
app.name = pkg2.name;
|
|
250
|
+
app.version = app.version || pkg2.version;
|
|
124
251
|
}
|
|
125
252
|
}
|
|
126
|
-
process.env.ROOT_PATH =
|
|
127
|
-
process.env.APP_PATH =
|
|
128
|
-
process.env.KOATTY_PATH =
|
|
129
|
-
process.env.THINK_PATH =
|
|
130
|
-
|
|
253
|
+
process.env.ROOT_PATH = rootPath;
|
|
254
|
+
process.env.APP_PATH = appPath;
|
|
255
|
+
process.env.KOATTY_PATH = koattyPath;
|
|
256
|
+
process.env.THINK_PATH = koattyPath;
|
|
257
|
+
Helper.define(app, "thinkPath", koattyPath);
|
|
131
258
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Get component metadata from target class.
|
|
261
|
+
*
|
|
262
|
+
* @param {KoattyApplication} app - The Koatty application instance
|
|
263
|
+
* @param {any} target - The target class to get metadata from
|
|
264
|
+
* @returns {any[]} Array of component metadata paths
|
|
265
|
+
*
|
|
266
|
+
* @static
|
|
267
|
+
* @public
|
|
268
|
+
*/
|
|
269
|
+
static GetComponentMeta(app, target) {
|
|
270
|
+
let componentMetas = [];
|
|
271
|
+
const componentMeta = IOC.getClassMetadata(TAGGED_CLS, COMPONENT_SCAN, target);
|
|
272
|
+
if (componentMeta) {
|
|
273
|
+
if (Helper.isArray(componentMeta)) {
|
|
274
|
+
componentMetas = componentMeta;
|
|
275
|
+
} else {
|
|
276
|
+
componentMetas.push(componentMeta);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (componentMetas.length < 1) {
|
|
280
|
+
componentMetas = [
|
|
281
|
+
app.appPath
|
|
282
|
+
];
|
|
283
|
+
}
|
|
284
|
+
return componentMetas;
|
|
138
285
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Get configuration metadata from target class.
|
|
288
|
+
*
|
|
289
|
+
* @param {KoattyApplication} app Application instance
|
|
290
|
+
* @param {any} target Target class
|
|
291
|
+
* @returns {any[]} Array of configuration metadata
|
|
292
|
+
*/
|
|
293
|
+
static GetConfigurationMeta(app, target) {
|
|
294
|
+
const confMeta = IOC.getClassMetadata(TAGGED_CLS, CONFIGURATION_SCAN, target);
|
|
295
|
+
let configurationMetas = [];
|
|
296
|
+
if (confMeta) {
|
|
297
|
+
if (Helper.isArray(confMeta)) {
|
|
298
|
+
configurationMetas = confMeta;
|
|
299
|
+
} else {
|
|
300
|
+
configurationMetas.push(confMeta);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return configurationMetas;
|
|
144
304
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
305
|
+
/**
|
|
306
|
+
* Check and load all components(excepted config/*、App.ts) in the application.
|
|
307
|
+
*
|
|
308
|
+
* @param {KoattyApplication} app - The Koatty application instance
|
|
309
|
+
* @param {any} target - The target class or object to check components from
|
|
310
|
+
* @static
|
|
311
|
+
* @public
|
|
312
|
+
*/
|
|
313
|
+
static CheckAllComponents(app, target) {
|
|
314
|
+
const componentMetas = _Loader.GetComponentMeta(app, target);
|
|
315
|
+
const configurationMetas = _Loader.GetConfigurationMeta(app, target);
|
|
316
|
+
const exSet = /* @__PURE__ */ new Set();
|
|
317
|
+
Load(componentMetas, "", (fileName, xpath, xTarget) => {
|
|
318
|
+
checkClass(fileName, xpath, xTarget, exSet);
|
|
319
|
+
}, [
|
|
320
|
+
"**/**.js",
|
|
321
|
+
"**/**.ts",
|
|
322
|
+
"!**/**.d.ts"
|
|
323
|
+
], [
|
|
324
|
+
...configurationMetas,
|
|
325
|
+
`${target.name || ".no"}.ts`
|
|
326
|
+
]);
|
|
327
|
+
exSet.clear();
|
|
153
328
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Set logger configuration for the Koatty application.
|
|
331
|
+
*
|
|
332
|
+
* @param {KoattyApplication} app - The Koatty application instance
|
|
333
|
+
* @description Configures logging settings based on application environment and config options.
|
|
334
|
+
* Handles log level, log file path, and sensitive fields configuration.
|
|
335
|
+
* In production environment, default log level is 'info', otherwise 'debug'.
|
|
336
|
+
*/
|
|
337
|
+
static SetLogger(app) {
|
|
338
|
+
const data = app.getMetaData("_configs") || [];
|
|
339
|
+
const configs = data[0] || {};
|
|
340
|
+
if (configs.config) {
|
|
341
|
+
const opt = configs.config;
|
|
342
|
+
let logLevel = "debug", logFilePath = "", sensFields = [];
|
|
343
|
+
if (app.env === "production") {
|
|
344
|
+
logLevel = "info";
|
|
345
|
+
}
|
|
346
|
+
if (opt.logsLevel) {
|
|
347
|
+
logLevel = opt.logsLevel.toLowerCase();
|
|
348
|
+
}
|
|
349
|
+
if (opt.logsPath) {
|
|
350
|
+
logFilePath = opt.logsPath;
|
|
351
|
+
}
|
|
352
|
+
if (opt.sensFields) {
|
|
353
|
+
sensFields = opt.sensFields;
|
|
354
|
+
}
|
|
355
|
+
if (!app.appDebug) {
|
|
356
|
+
DefaultLogger.enableBatch(true);
|
|
357
|
+
DefaultLogger.setBatchConfig({
|
|
167
358
|
maxSize: 200,
|
|
168
359
|
flushInterval: 500
|
|
169
360
|
});
|
|
170
361
|
}
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
m.define(t, "logsPath", n);
|
|
174
|
-
process.env.LOGS_PATH = n;
|
|
175
|
-
u.setLogFilePath(n);
|
|
362
|
+
if (logLevel) {
|
|
363
|
+
DefaultLogger.setLevel(logLevel);
|
|
176
364
|
}
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
365
|
+
if (logFilePath && !app.silent) {
|
|
366
|
+
Helper.define(app, "logsPath", logFilePath);
|
|
367
|
+
process.env.LOGS_PATH = logFilePath;
|
|
368
|
+
DefaultLogger.setLogFilePath(logFilePath);
|
|
369
|
+
}
|
|
370
|
+
if (sensFields) {
|
|
371
|
+
DefaultLogger.setSensFields(sensFields);
|
|
372
|
+
}
|
|
373
|
+
app.once(AppEvent.appStop, async () => {
|
|
374
|
+
await DefaultLogger.flushBatch();
|
|
375
|
+
await DefaultLogger.destroy();
|
|
181
376
|
});
|
|
182
377
|
}
|
|
183
378
|
}
|
|
184
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Load all components and initialize the application.
|
|
381
|
+
*
|
|
382
|
+
* @param app - The KoattyApplication instance
|
|
383
|
+
* @param target - The target class or object containing configuration metadata
|
|
384
|
+
*
|
|
385
|
+
* This method performs the following initialization steps:
|
|
386
|
+
* 1. Loads configurations
|
|
387
|
+
* 2. Creates server and router instances
|
|
388
|
+
* 3. Loads components, middlewares, services and controllers
|
|
389
|
+
* 4. Sets up routing
|
|
390
|
+
*
|
|
391
|
+
* @static
|
|
392
|
+
* @async
|
|
393
|
+
*/
|
|
394
|
+
static async LoadAllComponents(app, target) {
|
|
185
395
|
try {
|
|
186
|
-
if (
|
|
396
|
+
if (Helper.isFunction(IOC.preloadMetadata)) {
|
|
397
|
+
IOC.preloadMetadata();
|
|
398
|
+
}
|
|
187
399
|
} catch {
|
|
188
|
-
|
|
400
|
+
DefaultLogger.Warn("[Loader] preloadMetadata is optional, ignore if not available");
|
|
189
401
|
}
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
for (const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
case a.appReady:
|
|
253
|
-
u.Log("Koatty", "", "Emit appReady ...");
|
|
254
|
-
await n(t, e);
|
|
255
|
-
break;
|
|
256
|
-
|
|
257
|
-
default:
|
|
258
|
-
await n(t, e);
|
|
259
|
-
break;
|
|
402
|
+
const configurationMeta = _Loader.GetConfigurationMeta(app, target);
|
|
403
|
+
const loader = new _Loader(app);
|
|
404
|
+
for (const event of AppEventArr) {
|
|
405
|
+
switch (event) {
|
|
406
|
+
case AppEvent.appBoot:
|
|
407
|
+
DefaultLogger.Log("Koatty", "", "Load Configurations ...");
|
|
408
|
+
loader.LoadConfigs(configurationMeta);
|
|
409
|
+
_Loader.SetLogger(app);
|
|
410
|
+
await asyncEvent(app, event);
|
|
411
|
+
break;
|
|
412
|
+
case AppEvent.loadConfigure:
|
|
413
|
+
DefaultLogger.Log("Koatty", "", "Emit loadConfigure ...");
|
|
414
|
+
await asyncEvent(app, event);
|
|
415
|
+
break;
|
|
416
|
+
case AppEvent.loadComponent:
|
|
417
|
+
DefaultLogger.Log("Koatty", "", "Initializing Component Manager ...");
|
|
418
|
+
const componentManager = new ComponentManager(app);
|
|
419
|
+
Helper.define(app, "componentManager", componentManager);
|
|
420
|
+
componentManager.discoverComponents();
|
|
421
|
+
const stats = componentManager.getStats();
|
|
422
|
+
DefaultLogger.Log("Koatty", "", `Discovered ${stats.coreComponents} core components, ${stats.userComponents} user components`);
|
|
423
|
+
DefaultLogger.Log("Koatty", "", "Load Components ...");
|
|
424
|
+
await loader.LoadComponents(componentManager);
|
|
425
|
+
componentManager.registerAppEvents(target);
|
|
426
|
+
componentManager.registerCoreComponentHooks();
|
|
427
|
+
await asyncEvent(app, event);
|
|
428
|
+
break;
|
|
429
|
+
case AppEvent.loadPlugin:
|
|
430
|
+
DefaultLogger.Log("Koatty", "", "Emit loadPlugin ...");
|
|
431
|
+
await asyncEvent(app, event);
|
|
432
|
+
break;
|
|
433
|
+
case AppEvent.loadMiddleware:
|
|
434
|
+
DefaultLogger.Log("Koatty", "", "Load Middlewares ...");
|
|
435
|
+
await loader.LoadMiddlewares();
|
|
436
|
+
await asyncEvent(app, event);
|
|
437
|
+
break;
|
|
438
|
+
case AppEvent.loadService:
|
|
439
|
+
DefaultLogger.Log("Koatty", "", "Load Services ...");
|
|
440
|
+
await loader.LoadServices();
|
|
441
|
+
await asyncEvent(app, event);
|
|
442
|
+
break;
|
|
443
|
+
case AppEvent.loadController:
|
|
444
|
+
DefaultLogger.Log("Koatty", "", "Load Controllers ...");
|
|
445
|
+
await loader.LoadControllers();
|
|
446
|
+
await asyncEvent(app, event);
|
|
447
|
+
break;
|
|
448
|
+
case AppEvent.loadRouter:
|
|
449
|
+
DefaultLogger.Log("Koatty", "", "Initialize Router and Load Routes ...");
|
|
450
|
+
await asyncEvent(app, event);
|
|
451
|
+
break;
|
|
452
|
+
case AppEvent.loadServe:
|
|
453
|
+
DefaultLogger.Log("Koatty", "", "Emit loadServe ...");
|
|
454
|
+
await asyncEvent(app, event);
|
|
455
|
+
break;
|
|
456
|
+
case AppEvent.appReady:
|
|
457
|
+
DefaultLogger.Log("Koatty", "", "Emit appReady ...");
|
|
458
|
+
await asyncEvent(app, event);
|
|
459
|
+
break;
|
|
460
|
+
default:
|
|
461
|
+
await asyncEvent(app, event);
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
260
464
|
}
|
|
261
465
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
466
|
+
/**
|
|
467
|
+
* Load configuration files from specified paths.
|
|
468
|
+
* First loads framework configurations from './config' directory,
|
|
469
|
+
* then loads application configurations from custom paths.
|
|
470
|
+
* Finally merges both configurations with framework configs as lower priority.
|
|
471
|
+
*
|
|
472
|
+
* @protected
|
|
473
|
+
* @param {string[]} [loadPath] - Optional array of paths to load application configs from
|
|
474
|
+
*/
|
|
475
|
+
LoadConfigs(loadPath) {
|
|
476
|
+
const frameConfig = {};
|
|
477
|
+
Load([
|
|
478
|
+
"./config"
|
|
479
|
+
], this.app.koattyPath, function(name, path2, exp) {
|
|
480
|
+
frameConfig[name] = exp;
|
|
266
481
|
});
|
|
267
|
-
if (
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
482
|
+
if (Helper.isArray(loadPath)) {
|
|
483
|
+
loadPath = loadPath.length > 0 ? loadPath : [
|
|
484
|
+
"./config"
|
|
485
|
+
];
|
|
486
|
+
}
|
|
487
|
+
let appConfig = LoadConfigs(loadPath, this.app.appPath);
|
|
488
|
+
appConfig = Helper.extend(frameConfig, appConfig, true);
|
|
489
|
+
this.app.setMetaData("_configs", appConfig);
|
|
271
490
|
}
|
|
491
|
+
/**
|
|
492
|
+
* Load and register middleware components.
|
|
493
|
+
* Processes middleware configuration, registers middleware classes with IOC container,
|
|
494
|
+
* and mounts middleware to the application.
|
|
495
|
+
* Supports protocol-specific middleware mounting via @Middleware({ protocol }) option.
|
|
496
|
+
*
|
|
497
|
+
* @protected
|
|
498
|
+
* @returns {Promise<void>}
|
|
499
|
+
* @throws {Error} When middleware doesn't implement IMiddleware interface
|
|
500
|
+
* @throws {Error} When middleware loading fails
|
|
501
|
+
*/
|
|
272
502
|
async LoadMiddlewares() {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
503
|
+
let middlewareConf = this.app.config(void 0, "middleware");
|
|
504
|
+
if (Helper.isEmpty(middlewareConf)) {
|
|
505
|
+
middlewareConf = {
|
|
506
|
+
config: {},
|
|
507
|
+
list: []
|
|
508
|
+
};
|
|
509
|
+
}
|
|
279
510
|
try {
|
|
280
|
-
const
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
this.app.use(
|
|
284
|
-
|
|
285
|
-
} catch (
|
|
286
|
-
|
|
511
|
+
const traceOptions = middlewareConf.config?.trace ?? {};
|
|
512
|
+
const tracer = Trace(traceOptions, this.app);
|
|
513
|
+
Helper.define(this.app, "tracer", tracer);
|
|
514
|
+
this.app.use(tracer);
|
|
515
|
+
DefaultLogger.Debug(`Trace middleware registered`);
|
|
516
|
+
} catch (error) {
|
|
517
|
+
DefaultLogger.Warn(`Trace middleware failed to load: ${error.message}`);
|
|
287
518
|
}
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
e.reg(t.id, t.target, {
|
|
519
|
+
const appMiddleware = IOC.listClass("MIDDLEWARE") ?? [];
|
|
520
|
+
appMiddleware.forEach((item) => {
|
|
521
|
+
item.id = (item.id ?? "").replace("MIDDLEWARE:", "");
|
|
522
|
+
if (item.id && Helper.isClass(item.target)) {
|
|
523
|
+
IOC.reg(item.id, item.target, {
|
|
294
524
|
scope: "Prototype",
|
|
295
525
|
type: "MIDDLEWARE",
|
|
296
526
|
args: []
|
|
297
527
|
});
|
|
298
|
-
const
|
|
299
|
-
if (!
|
|
528
|
+
const ctl = IOC.getInsByClass(item.target);
|
|
529
|
+
if (!implementsMiddlewareInterface(ctl)) {
|
|
530
|
+
throw Error(`The middleware ${item.id} must implements interface 'IMiddleware'.`);
|
|
531
|
+
}
|
|
300
532
|
}
|
|
301
533
|
});
|
|
302
|
-
const
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
534
|
+
const middlewareList = middlewareConf.list || [];
|
|
535
|
+
const appMList = /* @__PURE__ */ new Set([]);
|
|
536
|
+
middlewareList.forEach((item) => {
|
|
537
|
+
appMList.add(item);
|
|
306
538
|
});
|
|
307
|
-
const
|
|
308
|
-
for (const
|
|
309
|
-
const
|
|
310
|
-
if (!
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
539
|
+
const middlewareConfig = middlewareConf.config || {};
|
|
540
|
+
for (const key of Array.from(appMList)) {
|
|
541
|
+
const handle = IOC.get(key, "MIDDLEWARE");
|
|
542
|
+
if (!handle) {
|
|
543
|
+
throw Error(`Middleware ${key} load error.`);
|
|
544
|
+
}
|
|
545
|
+
if (!Helper.isFunction(handle.run)) {
|
|
546
|
+
throw Error(`The middleware ${key} must implements interface 'IMiddleware'.`);
|
|
547
|
+
}
|
|
548
|
+
DefaultLogger.Debug(`Load middleware: ${key}`);
|
|
549
|
+
const middlewareOpt = middlewareConfig[key] || {};
|
|
550
|
+
const middlewareClass = appMiddleware.find((m) => m.id === key)?.target;
|
|
551
|
+
let decoratorOptions = {};
|
|
552
|
+
if (middlewareClass) {
|
|
553
|
+
try {
|
|
554
|
+
decoratorOptions = IOC.getPropertyData(MIDDLEWARE_OPTIONS, middlewareClass, key) || {};
|
|
555
|
+
} catch {
|
|
556
|
+
decoratorOptions = {};
|
|
557
|
+
}
|
|
320
558
|
}
|
|
321
|
-
const
|
|
322
|
-
...
|
|
323
|
-
...
|
|
559
|
+
const mergedOptions = {
|
|
560
|
+
...decoratorOptions,
|
|
561
|
+
...middlewareOpt
|
|
324
562
|
};
|
|
325
|
-
if (
|
|
326
|
-
|
|
563
|
+
if (mergedOptions.enabled === false) {
|
|
564
|
+
DefaultLogger.Warn(`The middleware ${key} has been loaded but is disabled.`);
|
|
327
565
|
continue;
|
|
328
566
|
}
|
|
329
|
-
const
|
|
330
|
-
if (
|
|
331
|
-
let
|
|
332
|
-
if (
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
567
|
+
const result = await handle.run(mergedOptions, this.app);
|
|
568
|
+
if (Helper.isFunction(result)) {
|
|
569
|
+
let middleware = result;
|
|
570
|
+
if (mergedOptions.protocol) {
|
|
571
|
+
const protocols = Helper.isArray(mergedOptions.protocol) ? mergedOptions.protocol : [
|
|
572
|
+
mergedOptions.protocol
|
|
573
|
+
];
|
|
574
|
+
DefaultLogger.Log("Koatty", "", `Middleware ${key} limited to protocols: ${protocols.join(", ")}`);
|
|
575
|
+
middleware = protocolMiddleware(protocols, result);
|
|
576
|
+
}
|
|
577
|
+
if (middleware.length < 3) {
|
|
578
|
+
this.app.use(middleware);
|
|
579
|
+
} else {
|
|
580
|
+
this.app.useExp(middleware);
|
|
336
581
|
}
|
|
337
|
-
if (e.length < 3) this.app.use(e); else this.app.useExp(e);
|
|
338
582
|
}
|
|
339
583
|
}
|
|
340
584
|
}
|
|
585
|
+
/**
|
|
586
|
+
* Load and register controller classes from IOC container.
|
|
587
|
+
* Each controller must implement the IController interface.
|
|
588
|
+
*
|
|
589
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of controller IDs.
|
|
590
|
+
* @protected
|
|
591
|
+
* @throws {Error} If a controller does not implement the IController interface.
|
|
592
|
+
*/
|
|
341
593
|
async LoadControllers() {
|
|
342
|
-
const
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
e.reg(t.id, t.target, {
|
|
594
|
+
const controllerList = IOC.listClass("CONTROLLER");
|
|
595
|
+
const controllers = [];
|
|
596
|
+
controllerList.forEach((item) => {
|
|
597
|
+
item.id = (item.id ?? "").replace("CONTROLLER:", "");
|
|
598
|
+
if (item.id && Helper.isClass(item.target)) {
|
|
599
|
+
DefaultLogger.Debug(`Load controller: ${item.id}`);
|
|
600
|
+
IOC.reg(item.id, item.target, {
|
|
350
601
|
scope: "Prototype",
|
|
351
602
|
type: "CONTROLLER",
|
|
352
603
|
args: []
|
|
353
604
|
});
|
|
354
|
-
const
|
|
355
|
-
if (!
|
|
356
|
-
|
|
605
|
+
const ctl = IOC.getInsByClass(item.target);
|
|
606
|
+
if (!implementsControllerInterface(ctl)) {
|
|
607
|
+
throw Error(`The controller ${item.id} must implements interface 'IController'.`);
|
|
608
|
+
}
|
|
609
|
+
controllers.push(item.id);
|
|
357
610
|
}
|
|
358
611
|
});
|
|
359
|
-
this.app.setMetaData("_controllers",
|
|
360
|
-
return
|
|
612
|
+
this.app.setMetaData("_controllers", controllers);
|
|
613
|
+
return controllers;
|
|
361
614
|
}
|
|
615
|
+
/**
|
|
616
|
+
* Load and register service components into IOC container.
|
|
617
|
+
* Each service must implement the IService interface.
|
|
618
|
+
* Services are registered with singleton scope.
|
|
619
|
+
*
|
|
620
|
+
* @protected
|
|
621
|
+
* @returns {Promise<void>}
|
|
622
|
+
* @throws {Error} When service does not implement IService interface
|
|
623
|
+
*/
|
|
362
624
|
async LoadServices() {
|
|
363
|
-
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
e.reg(a.id, a.target, {
|
|
625
|
+
const serviceList = IOC.listClass("SERVICE");
|
|
626
|
+
for (const item of serviceList) {
|
|
627
|
+
item.id = (item.id ?? "").replace("SERVICE:", "");
|
|
628
|
+
if (item.id && Helper.isClass(item.target)) {
|
|
629
|
+
DefaultLogger.Debug(`Load service: ${item.id}`);
|
|
630
|
+
IOC.reg(item.id, item.target, {
|
|
370
631
|
scope: "Singleton",
|
|
371
632
|
type: "SERVICE",
|
|
372
633
|
args: []
|
|
373
634
|
});
|
|
374
|
-
const
|
|
375
|
-
if (!
|
|
635
|
+
const ctl = IOC.getInsByClass(item.target);
|
|
636
|
+
if (!implementsServiceInterface(ctl)) {
|
|
637
|
+
throw Error(`The service ${item.id} must implements interface 'IService'.`);
|
|
638
|
+
}
|
|
376
639
|
}
|
|
377
640
|
}
|
|
378
641
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
642
|
+
/**
|
|
643
|
+
* Load and initialize components, plugins and aspects.
|
|
644
|
+
* Components with suffix 'Plugin' must implement IPlugin interface.
|
|
645
|
+
* Components with suffix 'Aspect' must implement IAspect interface.
|
|
646
|
+
* Plugins are loaded based on configuration and executed synchronously.
|
|
647
|
+
*
|
|
648
|
+
* @protected
|
|
649
|
+
* @returns {Promise<void>}
|
|
650
|
+
* @throws {Error} When plugin/aspect doesn't implement required interface
|
|
651
|
+
* @throws {Error} When plugin loading fails
|
|
652
|
+
*/
|
|
653
|
+
async LoadComponents(componentManager) {
|
|
654
|
+
const componentList = IOC.listClass("COMPONENT");
|
|
655
|
+
componentList.forEach((item) => {
|
|
656
|
+
item.id = (item.id ?? "").replace("COMPONENT:", "");
|
|
657
|
+
if (Helper.isClass(item.target)) {
|
|
658
|
+
IOC.reg(item.id, item.target, {
|
|
387
659
|
scope: "Singleton",
|
|
388
660
|
type: "COMPONENT",
|
|
389
661
|
args: []
|
|
390
662
|
});
|
|
391
|
-
if (
|
|
392
|
-
const
|
|
393
|
-
if (!
|
|
663
|
+
if (item.id && item.id.endsWith("Aspect")) {
|
|
664
|
+
const ctl = IOC.getInsByClass(item.target);
|
|
665
|
+
if (!implementsAspectInterface(ctl)) {
|
|
666
|
+
throw Error(`The aspect ${item.id} must implements interface 'IAspect'.`);
|
|
667
|
+
}
|
|
394
668
|
}
|
|
395
669
|
}
|
|
396
670
|
});
|
|
397
|
-
if (
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
671
|
+
if (componentManager) {
|
|
672
|
+
await componentManager.loadUserComponents();
|
|
673
|
+
} else {
|
|
674
|
+
DefaultLogger.Warn("Loading plugins in legacy mode");
|
|
675
|
+
let pluginsConf = this.app.config(void 0, "plugin");
|
|
676
|
+
if (Helper.isEmpty(pluginsConf)) {
|
|
677
|
+
pluginsConf = {
|
|
678
|
+
config: {},
|
|
679
|
+
list: []
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
const pluginConfList = pluginsConf.list ?? [];
|
|
683
|
+
for (const key of pluginConfList) {
|
|
684
|
+
const handle = IOC.get(key, "COMPONENT");
|
|
685
|
+
if (!handle) {
|
|
686
|
+
throw Error(`Plugin ${key} load error.`);
|
|
687
|
+
}
|
|
688
|
+
if (!Helper.isFunction(handle.run)) {
|
|
689
|
+
throw Error(`Plugin ${key} must implements interface 'IPlugin'.`);
|
|
690
|
+
}
|
|
691
|
+
if (pluginsConf.config[key] === false) {
|
|
692
|
+
DefaultLogger.Warn(`Plugin ${key} already loaded but not effective.`);
|
|
411
693
|
continue;
|
|
412
694
|
}
|
|
413
|
-
await
|
|
695
|
+
await handle.run(pluginsConf.config[key] ?? {}, this.app);
|
|
414
696
|
}
|
|
415
697
|
}
|
|
416
698
|
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
* @LastEditTime: 2025-01-14 16:11:21
|
|
425
|
-
* @License: BSD (3-Clause)
|
|
426
|
-
* @Copyright (c): <richenlin(at)gmail.com>
|
|
427
|
-
*/ function A(t) {
|
|
428
|
-
return async e => {
|
|
429
|
-
if (!(e.prototype instanceof g)) throw new Error(`class ${e.name} does not inherit from Koatty`);
|
|
430
|
-
return await $(e, t, !0);
|
|
699
|
+
};
|
|
700
|
+
function ExecBootStrap(bootFunc) {
|
|
701
|
+
return async (target) => {
|
|
702
|
+
if (!(target.prototype instanceof Koatty)) {
|
|
703
|
+
throw new Error(`class ${target.name} does not inherit from Koatty`);
|
|
704
|
+
}
|
|
705
|
+
return await executeBootstrap(target, bootFunc, true);
|
|
431
706
|
};
|
|
432
707
|
}
|
|
433
|
-
|
|
434
|
-
|
|
708
|
+
__name(ExecBootStrap, "ExecBootStrap");
|
|
709
|
+
var executeBootstrap = /* @__PURE__ */ __name(async function(target, bootFunc, isInitiative = false) {
|
|
435
710
|
if (process.env.NODE_DEBUG) {
|
|
436
|
-
const
|
|
437
|
-
process.env.NODE_DEBUG =
|
|
711
|
+
const debugModules = process.env.NODE_DEBUG.split(",").filter((m) => !m.includes("winston")).join(",");
|
|
712
|
+
process.env.NODE_DEBUG = debugModules;
|
|
713
|
+
}
|
|
714
|
+
checkRuntime();
|
|
715
|
+
const isUTRuntime = checkUTRuntime();
|
|
716
|
+
if (!isInitiative && isUTRuntime) {
|
|
717
|
+
return;
|
|
438
718
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
if (s) {
|
|
444
|
-
n.silent = !0;
|
|
445
|
-
u.enable(!1);
|
|
719
|
+
const app = Reflect.construct(target, []);
|
|
720
|
+
if (isUTRuntime) {
|
|
721
|
+
app.silent = true;
|
|
722
|
+
DefaultLogger.enable(false);
|
|
446
723
|
}
|
|
447
724
|
try {
|
|
448
|
-
if (!
|
|
449
|
-
if (!(
|
|
450
|
-
|
|
451
|
-
if (m.isFunction(o)) {
|
|
452
|
-
u.Log("Koatty", "", "Execute bootFunc ...");
|
|
453
|
-
await o(n);
|
|
725
|
+
if (!app.silent) console.log(LOGO);
|
|
726
|
+
if (!(app instanceof Koatty)) {
|
|
727
|
+
throw new Error(`class ${target.name} does not inherit from Koatty`);
|
|
454
728
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
729
|
+
Loader.initialize(app);
|
|
730
|
+
if (Helper.isFunction(bootFunc)) {
|
|
731
|
+
DefaultLogger.Log("Koatty", "", "Execute bootFunc ...");
|
|
732
|
+
await bootFunc(app);
|
|
733
|
+
}
|
|
734
|
+
IOC.setApp(app);
|
|
735
|
+
DefaultLogger.Log("Koatty", "", "ComponentScan ...");
|
|
736
|
+
Loader.CheckAllComponents(app, target);
|
|
737
|
+
await Loader.LoadAllComponents(app, target);
|
|
738
|
+
if (!isUTRuntime) {
|
|
739
|
+
app.listen(listenCallback);
|
|
740
|
+
}
|
|
741
|
+
return app;
|
|
742
|
+
} catch (err) {
|
|
743
|
+
DefaultLogger.Fatal(err);
|
|
463
744
|
}
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
return
|
|
745
|
+
}, "executeBootstrap");
|
|
746
|
+
var listenCallback = /* @__PURE__ */ __name((app) => {
|
|
747
|
+
DefaultLogger.Log("Koatty", "", "====================================");
|
|
748
|
+
DefaultLogger.Log("Koatty", "", `Nodejs Version: ${process.version}`);
|
|
749
|
+
DefaultLogger.Log("Koatty", "", `Koatty Version: v${KOATTY_VERSION}`);
|
|
750
|
+
DefaultLogger.Log("Koatty", "", `App Environment: ${app.env}`);
|
|
751
|
+
if (app.appDebug) DefaultLogger.Warn(`Running in debug mode.`);
|
|
752
|
+
DefaultLogger.Log("Koatty", "", "====================================");
|
|
753
|
+
asyncEvent(app, AppEvent.appStart);
|
|
754
|
+
}, "listenCallback");
|
|
755
|
+
function Bootstrap(bootFunc) {
|
|
756
|
+
return function(target) {
|
|
757
|
+
if (!(target.prototype instanceof Koatty)) {
|
|
758
|
+
throw new Error(`class does not inherit from Koatty`);
|
|
759
|
+
}
|
|
760
|
+
IOC.saveClass("COMPONENT", target, "KOATTY_APP");
|
|
761
|
+
ExecBootStrap(bootFunc)(target);
|
|
762
|
+
return target;
|
|
482
763
|
};
|
|
483
764
|
}
|
|
484
|
-
|
|
485
|
-
function
|
|
486
|
-
return
|
|
487
|
-
if (!(
|
|
488
|
-
|
|
489
|
-
|
|
765
|
+
__name(Bootstrap, "Bootstrap");
|
|
766
|
+
function ComponentScan(scanPath) {
|
|
767
|
+
return (target) => {
|
|
768
|
+
if (!(target.prototype instanceof Koatty)) {
|
|
769
|
+
throw new Error(`class does not inherit from Koatty`);
|
|
770
|
+
}
|
|
771
|
+
scanPath = scanPath ?? "";
|
|
772
|
+
IOC.saveClassMetadata(TAGGED_CLS, COMPONENT_SCAN, scanPath, target);
|
|
490
773
|
};
|
|
491
774
|
}
|
|
492
|
-
|
|
493
|
-
function
|
|
494
|
-
return
|
|
495
|
-
if (!(
|
|
496
|
-
|
|
497
|
-
|
|
775
|
+
__name(ComponentScan, "ComponentScan");
|
|
776
|
+
function ConfigurationScan(scanPath) {
|
|
777
|
+
return (target) => {
|
|
778
|
+
if (!(target.prototype instanceof Koatty)) {
|
|
779
|
+
throw new Error(`class does not inherit from Koatty`);
|
|
780
|
+
}
|
|
781
|
+
scanPath = scanPath ?? "";
|
|
782
|
+
IOC.saveClassMetadata(TAGGED_CLS, CONFIGURATION_SCAN, scanPath, target);
|
|
498
783
|
};
|
|
499
784
|
}
|
|
500
|
-
|
|
501
|
-
|
|
785
|
+
__name(ConfigurationScan, "ConfigurationScan");
|
|
786
|
+
/**
|
|
787
|
+
* Decorator for Koatty framework
|
|
788
|
+
* @author richen
|
|
789
|
+
* @copyright Copyright (c) - <richenlin(at)gmail.com>
|
|
790
|
+
* @license BSD (3-Clause)
|
|
791
|
+
* @version 2026-02-03 10:00:00
|
|
792
|
+
*/
|
|
793
|
+
|
|
794
|
+
export { Bootstrap, ComponentScan, ConfigurationScan, ExecBootStrap };
|
|
795
|
+
//# sourceMappingURL=index.mjs.map
|
|
796
|
+
//# sourceMappingURL=index.mjs.map
|