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