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