koatty 3.13.0 → 4.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/.package.json.backup +1 -0
- package/.rollup.config.js +89 -82
- package/.turbo/turbo-build.log +86 -0
- package/CHANGELOG.md +1881 -2064
- package/LICENSE +29 -29
- package/README.md +346 -162
- package/dist/LICENSE +29 -29
- package/dist/README.md +346 -162
- package/dist/index.d.ts +58 -5
- package/dist/index.js +252 -155
- package/dist/index.mjs +369 -272
- package/dist/package.json +107 -106
- package/examples/README.md +172 -0
- package/examples/basic-app/README.md +72 -0
- package/examples/basic-app/config/config.ts +142 -0
- package/examples/basic-app/package.json +20 -0
- package/examples/basic-app/pnpm-workspace.yaml +4 -0
- package/examples/basic-app/static/.gitkeep +0 -0
- package/examples/basic-app/static/favicon.ico +0 -0
- package/examples/basic-app/test-simple.ts +13 -0
- package/examples/basic-app/view/index.html +25 -0
- package/package.json +107 -106
- package/rollup.config-1761147987514.cjs +88 -0
- package/tsconfig.test.json +14 -14
- package/.vscode/launch.json +0 -42
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2025-
|
|
3
|
+
* @Date: 2025-11-02 10:59:15
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -9,36 +9,38 @@ import { LoadConfigs as t } from "koatty_config";
|
|
|
9
9
|
|
|
10
10
|
export { Config } from "koatty_config";
|
|
11
11
|
|
|
12
|
-
import { IOC as
|
|
12
|
+
import { IOC as o, TAGGED_CLS as e } from "koatty_container";
|
|
13
13
|
|
|
14
14
|
export * from "koatty_container";
|
|
15
15
|
|
|
16
|
-
import {
|
|
16
|
+
import { AppEvent as a, AppEventArr as r, implementsMiddlewareInterface as s, MIDDLEWARE_OPTIONS as i, protocolMiddleware as n, implementsControllerInterface as l, implementsServiceInterface as c, implementsPluginInterface as d, implementsAspectInterface as p, Koatty as f, asyncEvent as u } from "koatty_core";
|
|
17
17
|
|
|
18
18
|
export * from "koatty_core";
|
|
19
19
|
|
|
20
20
|
export * from "koatty_exception";
|
|
21
21
|
|
|
22
|
-
import { Helper as
|
|
22
|
+
import { Helper as g } from "koatty_lib";
|
|
23
23
|
|
|
24
24
|
export { Helper } from "koatty_lib";
|
|
25
25
|
|
|
26
|
-
import { NewRouter as
|
|
26
|
+
import { NewRouter as h } from "koatty_router";
|
|
27
27
|
|
|
28
28
|
export * from "koatty_router";
|
|
29
29
|
|
|
30
|
-
import { DefaultLogger as
|
|
30
|
+
import { DefaultLogger as y } from "koatty_logger";
|
|
31
31
|
|
|
32
|
-
import { Load as
|
|
32
|
+
import { Load as m } from "koatty_loader";
|
|
33
33
|
|
|
34
|
-
import { NewServe as
|
|
34
|
+
import { NewServe as L } from "koatty_serve";
|
|
35
35
|
|
|
36
|
-
import
|
|
36
|
+
import { Trace as v } from "koatty_trace";
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
import * as C from "path";
|
|
39
39
|
|
|
40
|
-
var
|
|
41
|
-
|
|
40
|
+
var w = "3.14.0-beta.1";
|
|
41
|
+
|
|
42
|
+
var E = {
|
|
43
|
+
node: ">=18.0.0"
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
/*
|
|
@@ -49,16 +51,25 @@ var v = {
|
|
|
49
51
|
* @LastEditTime: 2024-10-31 17:52:43
|
|
50
52
|
* @License: BSD (3-Clause)
|
|
51
53
|
* @Copyright (c): <richenlin(at)gmail.com>
|
|
52
|
-
*/ const
|
|
53
|
-
|
|
54
|
-
function
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
*/ const b = y;
|
|
55
|
+
|
|
56
|
+
function R(t, o) {
|
|
57
|
+
if (!t.appDebug) y.enableBuffering({
|
|
58
|
+
maxBufferSize: 200,
|
|
59
|
+
flushInterval: 500,
|
|
60
|
+
flushOnLevel: "error"
|
|
61
|
+
});
|
|
62
|
+
if (o.logLevel) y.setLevel(o.logLevel);
|
|
63
|
+
if (o.logFilePath && !t.silent) {
|
|
64
|
+
g.define(t, "logsPath", o.logFilePath);
|
|
65
|
+
process.env.LOGS_PATH = o.logFilePath;
|
|
66
|
+
y.setLogFilePath(o.logFilePath);
|
|
60
67
|
}
|
|
61
|
-
if (
|
|
68
|
+
if (o.sensFields) y.setSensFields(o.sensFields);
|
|
69
|
+
t.once(a.appStop, async () => {
|
|
70
|
+
await y.flush();
|
|
71
|
+
await y.destroy();
|
|
72
|
+
});
|
|
62
73
|
}
|
|
63
74
|
|
|
64
75
|
/*
|
|
@@ -69,38 +80,38 @@ function C(t, e) {
|
|
|
69
80
|
* @LastEditTime: 2025-01-14 16:14:10
|
|
70
81
|
* @License: BSD (3-Clause)
|
|
71
82
|
* @Copyright (c): <richenlin(at)gmail.com>
|
|
72
|
-
*/ const
|
|
83
|
+
*/ const P = w;
|
|
73
84
|
|
|
74
|
-
const
|
|
85
|
+
const A = E.node.slice(1) || "12.0.0";
|
|
75
86
|
|
|
76
|
-
function
|
|
77
|
-
if (
|
|
78
|
-
if (
|
|
79
|
-
const a = Object.keys(
|
|
80
|
-
if (a[0] != t &&
|
|
81
|
-
} else if (
|
|
87
|
+
function M(t, o, e, a) {
|
|
88
|
+
if (g.isClass(e) && e.name != t) throw Error(`The file(${o}) name should be always the same as class name.`);
|
|
89
|
+
if (e["__esModule"]) if (e.name === void 0) {
|
|
90
|
+
const a = Object.keys(e);
|
|
91
|
+
if (a[0] != t && g.isClass(e[a[0]])) throw Error(`The file(${o}) name should be always the same as class name.`);
|
|
92
|
+
} else if (e.name != t) throw Error(`The file(${o}) name should be always the same as class name.`);
|
|
82
93
|
if (!a) return;
|
|
83
|
-
if (a.has(t)) throw new Error(`A same class already exists. at \`${
|
|
94
|
+
if (a.has(t)) throw new Error(`A same class already exists. at \`${o}\`.`);
|
|
84
95
|
a.add(t);
|
|
85
96
|
return;
|
|
86
97
|
}
|
|
87
98
|
|
|
88
|
-
function
|
|
89
|
-
let t =
|
|
99
|
+
function K() {
|
|
100
|
+
let t = A;
|
|
90
101
|
t = t.slice(0, t.lastIndexOf("."));
|
|
91
|
-
let
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
|
|
102
|
+
let o = process.version;
|
|
103
|
+
if (o[0] === "v") o = o.slice(1);
|
|
104
|
+
o = o.slice(0, o.lastIndexOf("."));
|
|
105
|
+
if (g.toNumber(t) > g.toNumber(o)) {
|
|
106
|
+
b.Fatal(`Koatty need node version > ${t}, current version is ${o}, please upgrade it.`);
|
|
96
107
|
process.exit(-1);
|
|
97
108
|
}
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
const $ = () => {
|
|
101
112
|
let t = !1;
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
113
|
+
const o = JSON.stringify(process.argv[1]);
|
|
114
|
+
if (o.indexOf("jest") > -1) t = !0;
|
|
104
115
|
return t;
|
|
105
116
|
};
|
|
106
117
|
|
|
@@ -112,11 +123,11 @@ const $ = () => {
|
|
|
112
123
|
* @LastEditTime: 2023-12-09 23:00:13
|
|
113
124
|
* @License: BSD (3-Clause)
|
|
114
125
|
* @Copyright (c): <richenlin(at)gmail.com>
|
|
115
|
-
*/ const
|
|
126
|
+
*/ const k = "COMPONENT_SCAN";
|
|
116
127
|
|
|
117
|
-
const
|
|
128
|
+
const O = "CONFIGURATION_SCAN";
|
|
118
129
|
|
|
119
|
-
const
|
|
130
|
+
const S = `\n\n┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬\n├┴┐│ │├─┤ │ │ └┬┘\n┴ ┴└─┘┴ ┴ ┴ ┴ ┴ \n-------------------------------------------\nhttps://github.com/koatty\n`
|
|
120
131
|
/*
|
|
121
132
|
* @Description: framework loader
|
|
122
133
|
* @Usage:
|
|
@@ -132,261 +143,352 @@ class Loader {
|
|
|
132
143
|
this.app = t;
|
|
133
144
|
}
|
|
134
145
|
static initialize(t) {
|
|
135
|
-
if (t.env == "development")
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
const a =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
const
|
|
144
|
-
if (
|
|
145
|
-
t.name =
|
|
146
|
-
t.version = t.version ||
|
|
146
|
+
if (t.env == "development") b.setLevel("debug"); else b.setLevel("info");
|
|
147
|
+
const o = t.rootPath || process.cwd();
|
|
148
|
+
const e = t.appPath || C.resolve(o, t.appDebug ? "src" : "dist");
|
|
149
|
+
const a = C.resolve(__dirname, "..");
|
|
150
|
+
g.define(t, "rootPath", o);
|
|
151
|
+
g.define(t, "appPath", e);
|
|
152
|
+
g.define(t, "koattyPath", a);
|
|
153
|
+
if (g.isEmpty(t.name)) {
|
|
154
|
+
const o = g.safeRequire(`${C.dirname(e)}/package.json`);
|
|
155
|
+
if (o.name) {
|
|
156
|
+
t.name = o.name;
|
|
157
|
+
t.version = t.version || o.version;
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
|
-
process.env.ROOT_PATH =
|
|
150
|
-
process.env.APP_PATH =
|
|
160
|
+
process.env.ROOT_PATH = o;
|
|
161
|
+
process.env.APP_PATH = e;
|
|
151
162
|
process.env.KOATTY_PATH = a;
|
|
152
|
-
|
|
163
|
+
g.define(t, "thinkPath", a);
|
|
153
164
|
process.env.THINK_PATH = a;
|
|
154
165
|
}
|
|
155
166
|
static GetComponentMeta(t, a) {
|
|
156
|
-
let s = [];
|
|
157
|
-
const r = e.getClassMetadata(o, R, a);
|
|
158
|
-
if (r) if (f.isArray(r)) s = r; else s.push(r);
|
|
159
|
-
if (s.length < 1) s = [ t.appPath ];
|
|
160
|
-
return s;
|
|
161
|
-
}
|
|
162
|
-
static GetConfigurationMeta(t, a) {
|
|
163
|
-
const s = e.getClassMetadata(o, k, a);
|
|
164
167
|
let r = [];
|
|
165
|
-
|
|
168
|
+
const s = o.getClassMetadata(e, k, a);
|
|
169
|
+
if (s) if (g.isArray(s)) r = s; else r.push(s);
|
|
170
|
+
if (r.length < 1) r = [ t.appPath ];
|
|
166
171
|
return r;
|
|
167
172
|
}
|
|
168
|
-
static
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
static GetConfigurationMeta(t, a) {
|
|
174
|
+
const r = o.getClassMetadata(e, O, a);
|
|
175
|
+
let s = [];
|
|
176
|
+
if (r) if (g.isArray(r)) s = r; else s.push(r);
|
|
177
|
+
return s;
|
|
178
|
+
}
|
|
179
|
+
static CheckAllComponents(t, o) {
|
|
180
|
+
const e = Loader.GetComponentMeta(t, o);
|
|
181
|
+
const a = Loader.GetConfigurationMeta(t, o);
|
|
182
|
+
const r = new Set;
|
|
183
|
+
m(e, "", (t, o, e) => {
|
|
184
|
+
M(t, o, e, r);
|
|
185
|
+
}, [ "**/**.js", "**/**.ts", "!**/**.d.ts" ], [ ...a, `${o.name || ".no"}.ts` ]);
|
|
186
|
+
r.clear();
|
|
176
187
|
}
|
|
177
188
|
static SetLogger(t) {
|
|
178
|
-
const
|
|
179
|
-
const
|
|
180
|
-
if (
|
|
181
|
-
const
|
|
182
|
-
let a = "debug",
|
|
189
|
+
const o = t.getMetaData("_configs") || [];
|
|
190
|
+
const e = o[0] || {};
|
|
191
|
+
if (e.config) {
|
|
192
|
+
const o = e.config;
|
|
193
|
+
let a = "debug", r = "", s = [];
|
|
183
194
|
if (t.env === "production") a = "info";
|
|
184
|
-
if (
|
|
185
|
-
if (
|
|
186
|
-
if (
|
|
187
|
-
|
|
195
|
+
if (o.logsLevel) a = o.logsLevel.toLowerCase();
|
|
196
|
+
if (o.logsPath) r = o.logsPath;
|
|
197
|
+
if (o.sensFields) s = o.sensFields;
|
|
198
|
+
R(t, {
|
|
188
199
|
logLevel: a,
|
|
189
|
-
logFilePath:
|
|
190
|
-
sensFields:
|
|
200
|
+
logFilePath: r,
|
|
201
|
+
sensFields: s
|
|
191
202
|
});
|
|
192
203
|
}
|
|
193
204
|
}
|
|
194
205
|
static LoadAppEventHooks(t, s) {
|
|
195
|
-
const
|
|
196
|
-
for (const t of
|
|
197
|
-
let
|
|
206
|
+
const i = new Map;
|
|
207
|
+
for (const t of r) {
|
|
208
|
+
let r;
|
|
198
209
|
switch (t) {
|
|
199
|
-
case
|
|
200
|
-
|
|
201
|
-
if (
|
|
210
|
+
case a.appBoot:
|
|
211
|
+
r = o.getClassMetadata(e, a.appBoot, s);
|
|
212
|
+
if (g.isArray(r)) i.set(a.appBoot, r);
|
|
202
213
|
break;
|
|
203
214
|
|
|
204
|
-
case
|
|
205
|
-
|
|
206
|
-
if (
|
|
215
|
+
case a.appReady:
|
|
216
|
+
r = o.getClassMetadata(e, a.appReady, s);
|
|
217
|
+
if (g.isArray(r)) i.set(a.appReady, r);
|
|
207
218
|
break;
|
|
208
219
|
|
|
209
|
-
case
|
|
210
|
-
|
|
211
|
-
if (
|
|
220
|
+
case a.appStart:
|
|
221
|
+
r = o.getClassMetadata(e, a.appStart, s);
|
|
222
|
+
if (g.isArray(r)) i.set(a.appStart, r);
|
|
212
223
|
break;
|
|
213
224
|
|
|
214
|
-
case
|
|
215
|
-
|
|
216
|
-
if (
|
|
225
|
+
case a.appStop:
|
|
226
|
+
r = o.getClassMetadata(e, a.appStop, s);
|
|
227
|
+
if (g.isArray(r)) i.set(a.appStop, r);
|
|
217
228
|
break;
|
|
218
229
|
}
|
|
219
230
|
}
|
|
220
|
-
for (const [
|
|
231
|
+
for (const [o, e] of i) for (const a of e) t.once(o, () => a(t));
|
|
221
232
|
}
|
|
222
233
|
static async LoadAllComponents(t, e) {
|
|
223
|
-
var
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
234
|
+
var a, r, s;
|
|
235
|
+
try {
|
|
236
|
+
if (g.isFunction(o.preloadMetadata)) o.preloadMetadata();
|
|
237
|
+
} catch {
|
|
238
|
+
b.Warn("[Loader] preloadMetadata is optional, ignore if not available");
|
|
239
|
+
}
|
|
240
|
+
b.Log("Koatty", "", "Load Configurations ...");
|
|
241
|
+
const i = Loader.GetConfigurationMeta(t, e);
|
|
242
|
+
const n = new Loader(t);
|
|
243
|
+
n.LoadConfigs(i);
|
|
244
|
+
Loader.SetLogger(t);
|
|
245
|
+
const l = (a = t.config("server")) !== null && a !== void 0 ? a : {
|
|
229
246
|
protocol: "http"
|
|
230
247
|
};
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
f.
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
await
|
|
240
|
-
|
|
241
|
-
await
|
|
242
|
-
|
|
243
|
-
await
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
+
const c = (r = l.protocol) !== null && r !== void 0 ? r : "http";
|
|
249
|
+
const d = (s = t.config(void 0, "router")) !== null && s !== void 0 ? s : {};
|
|
250
|
+
const p = g.isArray(c) ? c : [ c ];
|
|
251
|
+
const f = Loader.CreateServers(t, l, p);
|
|
252
|
+
g.define(t, "server", f);
|
|
253
|
+
const u = Loader.CreateRouters(t, d, p);
|
|
254
|
+
g.define(t, "router", u);
|
|
255
|
+
b.Log("Koatty", "", "Load Components ...");
|
|
256
|
+
await n.LoadComponents();
|
|
257
|
+
b.Log("Koatty", "", "Load Middlewares ...");
|
|
258
|
+
await n.LoadMiddlewares();
|
|
259
|
+
b.Log("Koatty", "", "Load Services ...");
|
|
260
|
+
await n.LoadServices();
|
|
261
|
+
b.Log("Koatty", "", "Load Controllers ...");
|
|
262
|
+
const h = await n.LoadControllers();
|
|
263
|
+
b.Log("Koatty", "", "Load Routers ...");
|
|
264
|
+
await n.LoadRouter(h);
|
|
265
|
+
}
|
|
266
|
+
static CreateServers(t, o, e) {
|
|
267
|
+
if (e.length > 1) {
|
|
268
|
+
const a = [];
|
|
269
|
+
const r = g.isArray(o.port) ? o.port : [ o.port ];
|
|
270
|
+
const s = [];
|
|
271
|
+
for (let t = 0; t < e.length; t++) if (t < r.length) s.push(g.toNumber(r[t])); else s.push(g.toNumber(r[0]) + t);
|
|
272
|
+
for (let r = 0; r < e.length; r++) {
|
|
273
|
+
const i = e[r];
|
|
274
|
+
const n = {
|
|
275
|
+
...o,
|
|
276
|
+
protocol: i,
|
|
277
|
+
port: s[r]
|
|
278
|
+
};
|
|
279
|
+
a.push(L(t, n));
|
|
280
|
+
}
|
|
281
|
+
return a;
|
|
282
|
+
} else {
|
|
283
|
+
const a = e[0];
|
|
284
|
+
const r = {
|
|
285
|
+
protocol: a,
|
|
286
|
+
...o
|
|
287
|
+
};
|
|
288
|
+
return L(t, r);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
static CreateRouters(t, o, e) {
|
|
292
|
+
if (e.length > 1) {
|
|
293
|
+
const a = {};
|
|
294
|
+
for (const r of e) {
|
|
295
|
+
const e = {
|
|
296
|
+
protocol: r,
|
|
297
|
+
...o
|
|
298
|
+
};
|
|
299
|
+
if (o.ext && o.ext[r]) e.ext = o.ext[r];
|
|
300
|
+
a[r] = h(t, e);
|
|
301
|
+
}
|
|
302
|
+
return a;
|
|
303
|
+
} else {
|
|
304
|
+
const a = e[0];
|
|
305
|
+
return h(t, {
|
|
306
|
+
protocol: a,
|
|
307
|
+
...o
|
|
308
|
+
});
|
|
309
|
+
}
|
|
248
310
|
}
|
|
249
|
-
LoadConfigs(
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
})
|
|
254
|
-
if (
|
|
255
|
-
let a = t(
|
|
256
|
-
a =
|
|
311
|
+
LoadConfigs(o) {
|
|
312
|
+
const e = {};
|
|
313
|
+
m([ "./config" ], this.app.koattyPath, function(t, o, a) {
|
|
314
|
+
e[t] = a;
|
|
315
|
+
});
|
|
316
|
+
if (g.isArray(o)) o = o.length > 0 ? o : [ "./config" ];
|
|
317
|
+
let a = t(o, this.app.appPath);
|
|
318
|
+
a = g.extend(e, a, !0);
|
|
257
319
|
this.app.setMetaData("_configs", a);
|
|
258
320
|
}
|
|
259
321
|
async LoadMiddlewares() {
|
|
260
|
-
var t;
|
|
261
|
-
|
|
262
|
-
|
|
322
|
+
var t, e, a;
|
|
323
|
+
try {
|
|
324
|
+
const o = (t = this.app.config("trace")) !== null && t !== void 0 ? t : {};
|
|
325
|
+
const e = v(o, this.app);
|
|
326
|
+
g.define(this.app, "tracer", e);
|
|
327
|
+
this.app.use(e);
|
|
328
|
+
b.Debug(`Load trace middleware`);
|
|
329
|
+
} catch (t) {
|
|
330
|
+
b.Warn(`Trace middleware failed to load: ${t.message}`);
|
|
331
|
+
}
|
|
332
|
+
let r = this.app.config(void 0, "middleware");
|
|
333
|
+
if (g.isEmpty(r)) r = {
|
|
263
334
|
config: {},
|
|
264
|
-
list: []
|
|
265
|
-
routeList: []
|
|
335
|
+
list: []
|
|
266
336
|
};
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
var
|
|
270
|
-
t.id = ((
|
|
271
|
-
if (t.id &&
|
|
272
|
-
|
|
337
|
+
const l = (e = o.listClass("MIDDLEWARE")) !== null && e !== void 0 ? e : [];
|
|
338
|
+
l.forEach(t => {
|
|
339
|
+
var e;
|
|
340
|
+
t.id = ((e = t.id) !== null && e !== void 0 ? e : "").replace("MIDDLEWARE:", "");
|
|
341
|
+
if (t.id && g.isClass(t.target)) {
|
|
342
|
+
o.reg(t.id, t.target, {
|
|
273
343
|
scope: "Prototype",
|
|
274
344
|
type: "MIDDLEWARE",
|
|
275
345
|
args: []
|
|
276
346
|
});
|
|
277
|
-
const
|
|
278
|
-
if (!s(
|
|
347
|
+
const e = o.getInsByClass(t.target);
|
|
348
|
+
if (!s(e)) throw Error(`The middleware ${t.id} must implements interface 'IMiddleware'.`);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
const c = r.list || [];
|
|
352
|
+
const d = new Set([]);
|
|
353
|
+
c.forEach(t => {
|
|
354
|
+
d.add(t);
|
|
355
|
+
});
|
|
356
|
+
const p = r.config || {};
|
|
357
|
+
for (const t of d) {
|
|
358
|
+
const e = o.get(t, "MIDDLEWARE");
|
|
359
|
+
if (!e) throw Error(`Middleware ${t} load error.`);
|
|
360
|
+
if (!g.isFunction(e.run)) throw Error(`The middleware ${t} must implements interface 'IMiddleware'.`);
|
|
361
|
+
b.Debug(`Load middleware: ${t}`);
|
|
362
|
+
const r = p[t] || {};
|
|
363
|
+
const s = (a = l.find(o => o.id === t)) === null || a === void 0 ? void 0 : a.target;
|
|
364
|
+
let c = {};
|
|
365
|
+
if (s) try {
|
|
366
|
+
c = o.getPropertyData(i, s, t) || {};
|
|
367
|
+
} catch {
|
|
368
|
+
c = {};
|
|
369
|
+
}
|
|
370
|
+
const d = {
|
|
371
|
+
...c,
|
|
372
|
+
...r
|
|
373
|
+
};
|
|
374
|
+
if (d.enabled === !1) {
|
|
375
|
+
b.Warn(`The middleware ${t} has been loaded but is disabled.`);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
const f = await e.run(d, this.app);
|
|
379
|
+
if (g.isFunction(f)) {
|
|
380
|
+
let o = f;
|
|
381
|
+
if (d.protocol) {
|
|
382
|
+
const e = g.isArray(d.protocol) ? d.protocol : [ d.protocol ];
|
|
383
|
+
b.Log("Koatty", "", `Middleware ${t} limited to protocols: ${e.join(", ")}`);
|
|
384
|
+
o = n(e, f);
|
|
385
|
+
}
|
|
386
|
+
if (o.length < 3) this.app.use(o); else this.app.useExp(o);
|
|
279
387
|
}
|
|
280
|
-
}));
|
|
281
|
-
const i = o.list || [];
|
|
282
|
-
const n = new Set([]);
|
|
283
|
-
i.forEach((t => {
|
|
284
|
-
n.add(t);
|
|
285
|
-
}));
|
|
286
|
-
const l = o.routeList || [];
|
|
287
|
-
const c = new Set([]);
|
|
288
|
-
l.forEach((t => {
|
|
289
|
-
c.add(t);
|
|
290
|
-
}));
|
|
291
|
-
const p = o.config || {};
|
|
292
|
-
for (const t of n) {
|
|
293
|
-
const o = e.get(t, "MIDDLEWARE");
|
|
294
|
-
if (!o) throw Error(`Middleware ${t} load error.`);
|
|
295
|
-
if (!f.isFunction(o.run)) throw Error(`The middleware ${t} must implements interface 'IMiddleware'.`);
|
|
296
|
-
w.Debug(`Load middleware: ${t}`);
|
|
297
|
-
const a = p[t] || {};
|
|
298
|
-
const s = await o.run(a, this.app);
|
|
299
|
-
if (f.isFunction(s)) if (s.length < 3) this.app.use(s); else this.app.useExp(s);
|
|
300
|
-
}
|
|
301
|
-
for (const t of c) {
|
|
302
|
-
const o = e.get(t, "MIDDLEWARE");
|
|
303
|
-
if (!o) throw Error(`Router middleware ${t} load error.`);
|
|
304
|
-
if (!f.isFunction(o.run)) throw Error(`The router middleware ${t} must implements interface 'IMiddleware'.`);
|
|
305
|
-
w.Debug(`Load router middleware: ${t}`);
|
|
306
|
-
const a = await o.run(p[t] || {}, this.app);
|
|
307
|
-
if (f.isFunction(a)) this.app.setMetaData(`routerMiddleware_${t}`, a.length > 2 ? r(a) : a);
|
|
308
388
|
}
|
|
309
389
|
}
|
|
310
390
|
async LoadControllers() {
|
|
311
|
-
const t =
|
|
312
|
-
const
|
|
313
|
-
t.forEach(
|
|
391
|
+
const t = o.listClass("CONTROLLER");
|
|
392
|
+
const e = [];
|
|
393
|
+
t.forEach(t => {
|
|
314
394
|
var a;
|
|
315
395
|
t.id = ((a = t.id) !== null && a !== void 0 ? a : "").replace("CONTROLLER:", "");
|
|
316
|
-
if (t.id &&
|
|
317
|
-
|
|
318
|
-
|
|
396
|
+
if (t.id && g.isClass(t.target)) {
|
|
397
|
+
b.Debug(`Load controller: ${t.id}`);
|
|
398
|
+
o.reg(t.id, t.target, {
|
|
319
399
|
scope: "Prototype",
|
|
320
400
|
type: "CONTROLLER",
|
|
321
401
|
args: []
|
|
322
402
|
});
|
|
323
|
-
const a =
|
|
324
|
-
if (!
|
|
325
|
-
|
|
403
|
+
const a = o.getInsByClass(t.target);
|
|
404
|
+
if (!l(a)) throw Error(`The controller ${t.id} must implements interface 'IController'.`);
|
|
405
|
+
e.push(t.id);
|
|
326
406
|
}
|
|
327
|
-
})
|
|
328
|
-
return
|
|
407
|
+
});
|
|
408
|
+
return e;
|
|
329
409
|
}
|
|
330
410
|
async LoadServices() {
|
|
331
|
-
const t =
|
|
332
|
-
t.forEach(
|
|
333
|
-
var
|
|
334
|
-
t.id = ((
|
|
335
|
-
if (t.id &&
|
|
336
|
-
|
|
337
|
-
|
|
411
|
+
const t = o.listClass("SERVICE");
|
|
412
|
+
t.forEach(t => {
|
|
413
|
+
var e;
|
|
414
|
+
t.id = ((e = t.id) !== null && e !== void 0 ? e : "").replace("SERVICE:", "");
|
|
415
|
+
if (t.id && g.isClass(t.target)) {
|
|
416
|
+
b.Debug(`Load service: ${t.id}`);
|
|
417
|
+
o.reg(t.id, t.target, {
|
|
338
418
|
scope: "Singleton",
|
|
339
419
|
type: "SERVICE",
|
|
340
420
|
args: []
|
|
341
421
|
});
|
|
342
|
-
const
|
|
343
|
-
if (!
|
|
422
|
+
const e = o.getInsByClass(t.target);
|
|
423
|
+
if (!c(e)) throw Error(`The service ${t.id} must implements interface 'IService'.`);
|
|
344
424
|
}
|
|
345
|
-
})
|
|
425
|
+
});
|
|
346
426
|
}
|
|
347
427
|
async LoadComponents() {
|
|
348
|
-
var t,
|
|
349
|
-
const a =
|
|
350
|
-
const
|
|
351
|
-
a.forEach(
|
|
352
|
-
var
|
|
353
|
-
t.id = ((
|
|
354
|
-
if (
|
|
355
|
-
|
|
428
|
+
var t, e;
|
|
429
|
+
const a = o.listClass("COMPONENT");
|
|
430
|
+
const r = [];
|
|
431
|
+
a.forEach(async t => {
|
|
432
|
+
var e;
|
|
433
|
+
t.id = ((e = t.id) !== null && e !== void 0 ? e : "").replace("COMPONENT:", "");
|
|
434
|
+
if (g.isClass(t.target)) {
|
|
435
|
+
o.reg(t.id, t.target, {
|
|
356
436
|
scope: "Singleton",
|
|
357
437
|
type: "COMPONENT",
|
|
358
438
|
args: []
|
|
359
439
|
});
|
|
360
440
|
if (t.id && t.id.endsWith("Plugin")) {
|
|
361
|
-
const
|
|
362
|
-
if (!
|
|
363
|
-
|
|
441
|
+
const e = o.getInsByClass(t.target);
|
|
442
|
+
if (!d(e)) throw Error(`The plugin ${t.id} must implements interface 'IPlugin'.`);
|
|
443
|
+
r.push(t.id);
|
|
364
444
|
}
|
|
365
445
|
if (t.id && t.id.endsWith("Aspect")) {
|
|
366
|
-
const
|
|
367
|
-
if (!
|
|
446
|
+
const e = o.getInsByClass(t.target);
|
|
447
|
+
if (!p(e)) throw Error(`The aspect ${t.id} must implements interface 'IAspect'.`);
|
|
368
448
|
}
|
|
369
449
|
}
|
|
370
|
-
})
|
|
371
|
-
let
|
|
372
|
-
if (
|
|
450
|
+
});
|
|
451
|
+
let s = this.app.config(void 0, "plugin");
|
|
452
|
+
if (g.isEmpty(s)) s = {
|
|
373
453
|
config: {},
|
|
374
454
|
list: []
|
|
375
455
|
};
|
|
376
|
-
const i = (t =
|
|
456
|
+
const i = (t = s.list) !== null && t !== void 0 ? t : [];
|
|
377
457
|
for (const t of i) {
|
|
378
|
-
const a =
|
|
458
|
+
const a = o.get(t, "COMPONENT");
|
|
379
459
|
if (!a) throw Error(`Plugin ${t} load error.`);
|
|
380
|
-
if (!
|
|
381
|
-
if (
|
|
382
|
-
|
|
460
|
+
if (!g.isFunction(a.run)) throw Error(`Plugin ${t} must implements interface 'IPlugin'.`);
|
|
461
|
+
if (s.config[t] === !1) {
|
|
462
|
+
b.Warn(`Plugin ${t} already loaded but not effective.`);
|
|
383
463
|
continue;
|
|
384
464
|
}
|
|
385
|
-
await a.run((
|
|
465
|
+
await a.run((e = s.config[t]) !== null && e !== void 0 ? e : {}, this.app);
|
|
386
466
|
}
|
|
387
467
|
}
|
|
388
468
|
async LoadRouter(t) {
|
|
389
|
-
|
|
469
|
+
var o;
|
|
470
|
+
const e = this.app.router;
|
|
471
|
+
b.Log("Koatty", "", "============ LoadRouter START ============");
|
|
472
|
+
b.Log("Koatty", "", "router type:", typeof e);
|
|
473
|
+
b.Log("Koatty", "", "router is object:", g.isObject(e));
|
|
474
|
+
b.Log("Koatty", "", "router has LoadRouter:", g.isFunction(e === null || e === void 0 ? void 0 : e.LoadRouter));
|
|
475
|
+
b.Log("Koatty", "", "Controllers to load:", t);
|
|
476
|
+
if (g.isObject(e) && !g.isFunction(e.LoadRouter)) {
|
|
477
|
+
const a = e;
|
|
478
|
+
b.Log("Koatty", "", `Multi-protocol routing: found ${Object.keys(a).length} routers (${Object.keys(a).join(", ")})`);
|
|
479
|
+
for (const e in a) {
|
|
480
|
+
b.Log("Koatty", "", `Checking protocol: ${e}, has LoadRouter:`, g.isFunction((o = a[e]) === null || o === void 0 ? void 0 : o.LoadRouter));
|
|
481
|
+
if (a[e] && g.isFunction(a[e].LoadRouter)) {
|
|
482
|
+
b.Log("Koatty", "", `Loading routes for protocol: ${e}`);
|
|
483
|
+
await a[e].LoadRouter(this.app, t);
|
|
484
|
+
b.Log("Koatty", "", `Completed loading routes for protocol: ${e}`);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
} else if (g.isFunction(e.LoadRouter)) {
|
|
488
|
+
b.Log("Koatty", "", "Single protocol routing");
|
|
489
|
+
await e.LoadRouter(this.app, t);
|
|
490
|
+
} else b.Warn("No valid router found! router:", e);
|
|
491
|
+
b.Log("Koatty", "", "============ LoadRouter END ============");
|
|
390
492
|
}
|
|
391
493
|
}
|
|
392
494
|
|
|
@@ -398,87 +500,82 @@ class Loader {
|
|
|
398
500
|
* @LastEditTime: 2025-01-14 16:11:21
|
|
399
501
|
* @License: BSD (3-Clause)
|
|
400
502
|
* @Copyright (c): <richenlin(at)gmail.com>
|
|
401
|
-
*/ function
|
|
402
|
-
return function(
|
|
403
|
-
if (!(
|
|
404
|
-
|
|
503
|
+
*/ function T(t) {
|
|
504
|
+
return function(o) {
|
|
505
|
+
if (!(o.prototype instanceof f)) throw new Error(`class does not inherit from Koatty`);
|
|
506
|
+
x(o, t);
|
|
507
|
+
return o;
|
|
405
508
|
};
|
|
406
509
|
}
|
|
407
510
|
|
|
408
|
-
function
|
|
409
|
-
return async
|
|
410
|
-
if (!(
|
|
411
|
-
return await
|
|
511
|
+
function _(t) {
|
|
512
|
+
return async o => {
|
|
513
|
+
if (!(o.prototype instanceof f)) throw new Error(`class ${o.name} does not inherit from Koatty`);
|
|
514
|
+
return await x(o, t, !0);
|
|
412
515
|
};
|
|
413
516
|
}
|
|
414
517
|
|
|
415
|
-
function
|
|
518
|
+
function F(t) {
|
|
416
519
|
return a => {
|
|
417
|
-
if (!(a.prototype instanceof
|
|
520
|
+
if (!(a.prototype instanceof f)) throw new Error(`class does not inherit from Koatty`);
|
|
418
521
|
t = t !== null && t !== void 0 ? t : "";
|
|
419
|
-
|
|
522
|
+
o.saveClassMetadata(e, k, t, a);
|
|
420
523
|
};
|
|
421
524
|
}
|
|
422
525
|
|
|
423
|
-
function
|
|
526
|
+
function N(t) {
|
|
424
527
|
return a => {
|
|
425
|
-
if (!(a.prototype instanceof
|
|
528
|
+
if (!(a.prototype instanceof f)) throw new Error(`class does not inherit from Koatty`);
|
|
426
529
|
t = t !== null && t !== void 0 ? t : "";
|
|
427
|
-
|
|
530
|
+
o.saveClassMetadata(e, O, t, a);
|
|
428
531
|
};
|
|
429
532
|
}
|
|
430
533
|
|
|
431
|
-
function
|
|
432
|
-
|
|
534
|
+
function I(t, a, r) {
|
|
535
|
+
o.attachClassMetadata(e, t, a, r);
|
|
433
536
|
}
|
|
434
537
|
|
|
435
|
-
const
|
|
436
|
-
|
|
538
|
+
const x = async function(t, e, r = !1) {
|
|
539
|
+
K();
|
|
437
540
|
const s = $();
|
|
438
|
-
if (!
|
|
439
|
-
const
|
|
541
|
+
if (!r && s) return;
|
|
542
|
+
const i = Reflect.construct(t, []);
|
|
440
543
|
if (s) {
|
|
441
|
-
|
|
442
|
-
|
|
544
|
+
i.silent = !0;
|
|
545
|
+
b.enable(!1);
|
|
443
546
|
}
|
|
444
547
|
try {
|
|
445
|
-
if (!
|
|
446
|
-
if (!(
|
|
447
|
-
Loader.initialize(
|
|
448
|
-
if (
|
|
449
|
-
|
|
450
|
-
await
|
|
548
|
+
if (!i.silent) console.log(S);
|
|
549
|
+
if (!(i instanceof f)) throw new Error(`class ${t.name} does not inherit from Koatty`);
|
|
550
|
+
Loader.initialize(i);
|
|
551
|
+
if (g.isFunction(e)) {
|
|
552
|
+
b.Log("Koatty", "", "Execute bootFunc ...");
|
|
553
|
+
await e(i);
|
|
451
554
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
Loader.CheckAllComponents(
|
|
455
|
-
Loader.LoadAppEventHooks(
|
|
456
|
-
|
|
457
|
-
await
|
|
458
|
-
await Loader.LoadAllComponents(
|
|
459
|
-
|
|
460
|
-
await
|
|
461
|
-
if (!s)
|
|
462
|
-
return
|
|
555
|
+
o.setApp(i);
|
|
556
|
+
b.Log("Koatty", "", "ComponentScan ...");
|
|
557
|
+
Loader.CheckAllComponents(i, t);
|
|
558
|
+
Loader.LoadAppEventHooks(i, t);
|
|
559
|
+
b.Log("Koatty", "", "Emit App Boot ...");
|
|
560
|
+
await u(i, a.appBoot);
|
|
561
|
+
await Loader.LoadAllComponents(i, t);
|
|
562
|
+
b.Log("Koatty", "", "Emit App Ready ...");
|
|
563
|
+
await u(i, a.appReady);
|
|
564
|
+
if (!s) i.listen(D);
|
|
565
|
+
return i;
|
|
463
566
|
} catch (t) {
|
|
464
|
-
|
|
465
|
-
process.exit();
|
|
567
|
+
b.Fatal(t);
|
|
568
|
+
process.exit(1);
|
|
466
569
|
}
|
|
467
570
|
};
|
|
468
571
|
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
e.forEach((t => {
|
|
477
|
-
w.Log("Koatty", "", `Server: ${t.options.protocol.toUpperCase()} running at ${t.options.protocol === "http2" ? "https" : t.options.protocol}://${t.options.hostname || "127.0.0.1"}:${t.options.port}/`);
|
|
478
|
-
}));
|
|
479
|
-
w.Log("Koatty", "", "====================================");
|
|
480
|
-
if (t.appDebug) w.Warn(`Running in debug mode.`);
|
|
481
|
-
Loader.SetLogger(t);
|
|
572
|
+
const D = t => {
|
|
573
|
+
b.Log("Koatty", "", "====================================");
|
|
574
|
+
b.Log("Koatty", "", `Nodejs Version: ${process.version}`);
|
|
575
|
+
b.Log("Koatty", "", `Koatty Version: v${P}`);
|
|
576
|
+
b.Log("Koatty", "", `App Environment: ${t.env}`);
|
|
577
|
+
if (t.appDebug) b.Warn(`Running in debug mode.`);
|
|
578
|
+
b.Log("Koatty", "", "====================================");
|
|
482
579
|
};
|
|
483
580
|
|
|
484
|
-
export {
|
|
581
|
+
export { I as BindEventHook, T as Bootstrap, F as ComponentScan, N as ConfigurationScan, _ as ExecBootStrap, b as Logger };
|