koatty 3.13.2 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-04-25 21:47:59
3
+ * @Date: 2026-01-28 11:05:04
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -19,17 +19,19 @@ var a = require("koatty_lib");
19
19
 
20
20
  var n = require("koatty_router");
21
21
 
22
- var s = require("koatty_logger");
22
+ var i = require("koatty_logger");
23
23
 
24
- var i = require("koatty_loader");
24
+ var s = 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 p(e) {
32
+ function d(e) {
31
33
  var t = Object.create(null);
32
- if (e) Object.keys(e).forEach((function(o) {
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 = p(c);
49
+ var f = d(c);
48
50
 
49
- var d = "3.13.2";
51
+ var u = "4.0.3";
50
52
 
51
- var u = {
52
- node: ">12.0.0"
53
+ var g = {
54
+ node: ">=18.0.0"
53
55
  };
54
56
 
55
57
  /*
@@ -60,16 +62,25 @@ 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 g = s.DefaultLogger;
65
+ */ const y = i.DefaultLogger;
64
66
 
65
- function h(e, t) {
66
- if (t.logLevel) s.DefaultLogger.setLevel(t.logLevel);
67
+ function L(e, t) {
68
+ if (!e.appDebug) i.DefaultLogger.enableBuffering({
69
+ maxBufferSize: 200,
70
+ flushInterval: 500,
71
+ flushOnLevel: "error"
72
+ });
73
+ if (t.logLevel) i.DefaultLogger.setLevel(t.logLevel);
67
74
  if (t.logFilePath && !e.silent) {
68
75
  a.Helper.define(e, "logsPath", t.logFilePath);
69
76
  process.env.LOGS_PATH = t.logFilePath;
70
- s.DefaultLogger.setLogFilePath(t.logFilePath);
77
+ i.DefaultLogger.setLogFilePath(t.logFilePath);
71
78
  }
72
- if (t.sensFields) s.DefaultLogger.setSensFields(t.sensFields);
79
+ if (t.sensFields) i.DefaultLogger.setSensFields(t.sensFields);
80
+ e.once(o.AppEvent.appStop, async () => {
81
+ await i.DefaultLogger.flush();
82
+ await i.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 y = d;
94
+ */ const v = u;
84
95
 
85
- const C = u.node.slice(1) || "12.0.0";
96
+ const E = g.node.slice(1) || "12.0.0";
86
97
 
87
- function m(e, t, o, r) {
98
+ function h(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 L() {
100
- let e = C;
110
+ function C() {
111
+ let e = E;
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
- g.Error(`Koatty need node version > ${e}, current version is ${t}, please upgrade it.`);
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 v = () => {
122
+ const m = () => {
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 E = "COMPONENT_SCAN";
137
+ */ const w = "COMPONENT_SCAN";
127
138
 
128
139
  const O = "CONFIGURATION_SCAN";
129
140
 
130
- const w = `\n\n┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬\n├┴┐│ │├─┤ │ │ └┬┘\n┴ ┴└─┘┴ ┴ ┴ ┴ ┴ \n-------------------------------------------\nhttps://github.com/koatty\n`
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") g.setLevel("debug"); else g.setLevel("info");
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, "..");
@@ -157,6 +168,7 @@ class Loader {
157
168
  e.version = e.version || t.version;
158
169
  }
159
170
  }
171
+ y.Warn("Using process.env for paths is deprecated. Use app.rootPath, app.appPath, app.koattyPath instead.");
160
172
  process.env.ROOT_PATH = t;
161
173
  process.env.APP_PATH = o;
162
174
  process.env.KOATTY_PATH = r;
@@ -165,7 +177,7 @@ class Loader {
165
177
  }
166
178
  static GetComponentMeta(e, o) {
167
179
  let r = [];
168
- const n = t.IOC.getClassMetadata(t.TAGGED_CLS, E, o);
180
+ const n = t.IOC.getClassMetadata(t.TAGGED_CLS, w, o);
169
181
  if (n) if (a.Helper.isArray(n)) r = n; else r.push(n);
170
182
  if (r.length < 1) r = [ e.appPath ];
171
183
  return r;
@@ -180,9 +192,9 @@ class Loader {
180
192
  const o = Loader.GetComponentMeta(e, t);
181
193
  const r = Loader.GetConfigurationMeta(e, t);
182
194
  const a = new Set;
183
- i.Load(o, "", ((e, t, o) => {
184
- m(e, t, o, a);
185
- }), [ "**/**.js", "**/**.ts", "!**/**.d.ts" ], [ ...r, `${t.name || ".no"}.ts` ]);
195
+ s.Load(o, "", (e, t, o) => {
196
+ h(e, t, o, a);
197
+ }, [ "**/**.js", "**/**.ts", "!**/**.d.ts" ], [ ...r, `${t.name || ".no"}.ts` ]);
186
198
  a.clear();
187
199
  }
188
200
  static SetLogger(e) {
@@ -195,7 +207,7 @@ class Loader {
195
207
  if (t.logsLevel) r = t.logsLevel.toLowerCase();
196
208
  if (t.logsPath) a = t.logsPath;
197
209
  if (t.sensFields) n = t.sensFields;
198
- h(e, {
210
+ L(e, {
199
211
  logLevel: r,
200
212
  logFilePath: a,
201
213
  sensFields: n
@@ -205,78 +217,155 @@ class Loader {
205
217
  static LoadAppEventHooks(e, r) {
206
218
  const n = new Map;
207
219
  for (const e of o.AppEventArr) {
208
- let o;
220
+ let i;
209
221
  switch (e) {
210
- case "appBoot":
211
- o = t.IOC.getClassMetadata(t.TAGGED_CLS, "appBoot", r);
212
- if (a.Helper.isArray(o)) n.set("appBoot", o);
222
+ case o.AppEvent.appBoot:
223
+ i = t.IOC.getClassMetadata(t.TAGGED_CLS, o.AppEvent.appBoot, r);
224
+ if (a.Helper.isArray(i)) n.set(o.AppEvent.appBoot, i);
213
225
  break;
214
226
 
215
- case "appReady":
216
- o = t.IOC.getClassMetadata(t.TAGGED_CLS, "appReady", r);
217
- if (a.Helper.isArray(o)) n.set("appReady", o);
227
+ case o.AppEvent.appReady:
228
+ i = t.IOC.getClassMetadata(t.TAGGED_CLS, o.AppEvent.appReady, r);
229
+ if (a.Helper.isArray(i)) n.set(o.AppEvent.appReady, i);
218
230
  break;
219
231
 
220
- case "appStart":
221
- o = t.IOC.getClassMetadata(t.TAGGED_CLS, "appStart", r);
222
- if (a.Helper.isArray(o)) n.set("appStart", o);
232
+ case o.AppEvent.appStart:
233
+ i = t.IOC.getClassMetadata(t.TAGGED_CLS, o.AppEvent.appStart, r);
234
+ if (a.Helper.isArray(i)) n.set(o.AppEvent.appStart, i);
223
235
  break;
224
236
 
225
- case "appStop":
226
- o = t.IOC.getClassMetadata(t.TAGGED_CLS, "appStop", r);
227
- if (a.Helper.isArray(o)) n.set("appStop", o);
237
+ case o.AppEvent.appStop:
238
+ i = t.IOC.getClassMetadata(t.TAGGED_CLS, o.AppEvent.appStop, r);
239
+ if (a.Helper.isArray(i)) n.set(o.AppEvent.appStop, i);
228
240
  break;
229
241
  }
230
242
  }
231
- for (const [t, o] of n) for (const r of o) e.once(t, (() => r(e)));
243
+ for (const [t, o] of n) for (const r of o) e.once(t, () => r(e));
232
244
  }
233
- static async LoadAllComponents(e, t) {
234
- var o, r, s;
235
- g.Log("Koatty", "", "Load Configurations ...");
236
- const i = Loader.GetConfigurationMeta(e, t);
237
- const c = new Loader(e);
238
- c.LoadConfigs(i);
239
- const p = (o = e.config("server")) !== null && o !== void 0 ? o : {
240
- protocol: "http"
241
- };
242
- const f = (r = p.protocol) !== null && r !== void 0 ? r : "http";
243
- a.Helper.define(e, "server", l.NewServe(e, p));
244
- const d = (s = e.config(void 0, "router")) !== null && s !== void 0 ? s : {};
245
- a.Helper.define(e, "router", n.NewRouter(e, {
246
- protocol: f,
247
- ...d
248
- }));
249
- g.Log("Koatty", "", "Load Components ...");
250
- await c.LoadComponents();
251
- g.Log("Koatty", "", "Load Middlewares ...");
252
- await c.LoadMiddlewares();
253
- g.Log("Koatty", "", "Load Services ...");
254
- await c.LoadServices();
255
- g.Log("Koatty", "", "Load Controllers ...");
256
- const u = await c.LoadControllers();
257
- g.Log("Koatty", "", "Load Routers ...");
258
- c.LoadRouter(u);
245
+ static async LoadAllComponents(e, r) {
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 n = Loader.GetConfigurationMeta(e, r);
253
+ const i = new Loader(e);
254
+ i.LoadConfigs(n);
255
+ Loader.SetLogger(e);
256
+ y.Log("Koatty", "", "Emit Config Loaded ...");
257
+ await o.asyncEvent(e, o.AppEvent.configLoaded);
258
+ y.Log("Koatty", "", "Initializing Component Manager ...");
259
+ const s = new o.ComponentManager(e);
260
+ a.Helper.define(e, "componentManager", s);
261
+ s.discoverPlugins();
262
+ s.registerCorePluginHooks();
263
+ const l = s.getStats();
264
+ y.Log("Koatty", "", `Discovered ${l.corePlugins} core plugins, ${l.userPlugins} user plugins`);
265
+ y.Log("Koatty", "", "Emit Before Component Load ...");
266
+ await o.asyncEvent(e, o.AppEvent.beforeComponentLoad);
267
+ y.Log("Koatty", "", "Load Components ...");
268
+ await i.LoadComponents(s);
269
+ y.Log("Koatty", "", "Emit After Component Load ...");
270
+ await o.asyncEvent(e, o.AppEvent.afterComponentLoad);
271
+ y.Log("Koatty", "", "Emit Before Middleware Load ...");
272
+ await o.asyncEvent(e, o.AppEvent.beforeMiddlewareLoad);
273
+ y.Log("Koatty", "", "Load Middlewares ...");
274
+ await i.LoadMiddlewares();
275
+ y.Log("Koatty", "", "Emit After Middleware Load ...");
276
+ await o.asyncEvent(e, o.AppEvent.afterMiddlewareLoad);
277
+ y.Log("Koatty", "", "Emit Before Service Load ...");
278
+ await o.asyncEvent(e, o.AppEvent.beforeServiceLoad);
279
+ y.Log("Koatty", "", "Load Services ...");
280
+ await i.LoadServices();
281
+ y.Log("Koatty", "", "Emit After Service Load ...");
282
+ await o.asyncEvent(e, o.AppEvent.afterServiceLoad);
283
+ y.Log("Koatty", "", "Emit Before Controller Load ...");
284
+ await o.asyncEvent(e, o.AppEvent.beforeControllerLoad);
285
+ y.Log("Koatty", "", "Load Controllers ...");
286
+ const p = await i.LoadControllers();
287
+ y.Log("Koatty", "", "Emit After Controller Load ...");
288
+ await o.asyncEvent(e, o.AppEvent.afterControllerLoad);
289
+ y.Log("Koatty", "", "Emit Before Router Load ...");
290
+ await o.asyncEvent(e, o.AppEvent.beforeRouterLoad);
291
+ y.Log("Koatty", "", "Load Routers ...");
292
+ await i.LoadRouter(p);
293
+ y.Log("Koatty", "", "Emit After Router Load ...");
294
+ await o.asyncEvent(e, o.AppEvent.afterRouterLoad);
295
+ }
296
+ static CreateServers(e, t, o) {
297
+ if (o.length > 1) {
298
+ const r = [];
299
+ const n = a.Helper.isArray(t.port) ? t.port : [ t.port ];
300
+ const i = [];
301
+ for (let e = 0; e < o.length; e++) if (e < n.length) i.push(a.Helper.toNumber(n[e])); else i.push(a.Helper.toNumber(n[0]) + e);
302
+ for (let a = 0; a < o.length; a++) {
303
+ const n = o[a];
304
+ const s = {
305
+ ...t,
306
+ protocol: n,
307
+ port: i[a]
308
+ };
309
+ r.push(l.NewServe(e, s));
310
+ }
311
+ return r;
312
+ } else {
313
+ const r = o[0];
314
+ const a = {
315
+ protocol: r,
316
+ ...t
317
+ };
318
+ return l.NewServe(e, a);
319
+ }
320
+ }
321
+ static CreateRouters(e, t, o) {
322
+ if (o.length > 1) {
323
+ const r = {};
324
+ for (const a of o) {
325
+ const o = {
326
+ protocol: a,
327
+ ...t
328
+ };
329
+ if (t.ext && t.ext[a]) o.ext = t.ext[a];
330
+ r[a] = n.NewRouter(e, o);
331
+ }
332
+ return r;
333
+ } else {
334
+ const r = o[0];
335
+ return n.NewRouter(e, {
336
+ protocol: r,
337
+ ...t
338
+ });
339
+ }
259
340
  }
260
341
  LoadConfigs(t) {
261
342
  const o = {};
262
- i.Load([ "./config" ], this.app.koattyPath, (function(e, t, r) {
343
+ s.Load([ "./config" ], this.app.koattyPath, function(e, t, r) {
263
344
  o[e] = r;
264
- }));
345
+ });
265
346
  if (a.Helper.isArray(t)) t = t.length > 0 ? t : [ "./config" ];
266
347
  let r = e.LoadConfigs(t, this.app.appPath);
267
348
  r = a.Helper.extend(o, r, !0);
268
349
  this.app.setMetaData("_configs", r);
269
350
  }
270
351
  async LoadMiddlewares() {
271
- var e;
272
- let r = this.app.config(void 0, "middleware");
273
- if (a.Helper.isEmpty(r)) r = {
352
+ var e, r, n;
353
+ try {
354
+ const t = (e = this.app.config("trace")) !== null && e !== void 0 ? e : {};
355
+ const o = p.Trace(t, this.app);
356
+ a.Helper.define(this.app, "tracer", o);
357
+ this.app.use(o);
358
+ y.Debug(`Load trace middleware`);
359
+ } catch (e) {
360
+ y.Warn(`Trace middleware failed to load: ${e.message}`);
361
+ }
362
+ let i = this.app.config(void 0, "middleware");
363
+ if (a.Helper.isEmpty(i)) i = {
274
364
  config: {},
275
- list: [],
276
- routeList: []
365
+ list: []
277
366
  };
278
- const n = (e = t.IOC.listClass("MIDDLEWARE")) !== null && e !== void 0 ? e : [];
279
- n.forEach((e => {
367
+ const s = (r = t.IOC.listClass("MIDDLEWARE")) !== null && r !== void 0 ? r : [];
368
+ s.forEach(e => {
280
369
  var r;
281
370
  e.id = ((r = e.id) !== null && r !== void 0 ? r : "").replace("MIDDLEWARE:", "");
282
371
  if (e.id && a.Helper.isClass(e.target)) {
@@ -288,44 +377,54 @@ class Loader {
288
377
  const r = t.IOC.getInsByClass(e.target);
289
378
  if (!o.implementsMiddlewareInterface(r)) throw Error(`The middleware ${e.id} must implements interface 'IMiddleware'.`);
290
379
  }
291
- }));
292
- const s = r.list || [];
293
- const i = new Set([]);
294
- s.forEach((e => {
295
- i.add(e);
296
- }));
297
- const l = r.routeList || [];
380
+ });
381
+ const l = i.list || [];
298
382
  const c = new Set([]);
299
- l.forEach((e => {
383
+ l.forEach(e => {
300
384
  c.add(e);
301
- }));
302
- const p = r.config || {};
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
- }
385
+ });
386
+ const d = i.config || {};
312
387
  for (const e of c) {
313
388
  const r = t.IOC.get(e, "MIDDLEWARE");
314
- if (!r) throw Error(`Router middleware ${e} load error.`);
315
- if (!a.Helper.isFunction(r.run)) throw Error(`The router middleware ${e} must implements interface 'IMiddleware'.`);
316
- g.Debug(`Load router middleware: ${e}`);
317
- const n = await r.run(p[e] || {}, this.app);
318
- if (a.Helper.isFunction(n)) this.app.setMetaData(`routerMiddleware_${e}`, n.length > 2 ? o.parseExp(n) : n);
389
+ if (!r) throw Error(`Middleware ${e} load error.`);
390
+ if (!a.Helper.isFunction(r.run)) throw Error(`The middleware ${e} must implements interface 'IMiddleware'.`);
391
+ y.Debug(`Load middleware: ${e}`);
392
+ const i = d[e] || {};
393
+ const l = (n = s.find(t => t.id === e)) === null || n === void 0 ? void 0 : n.target;
394
+ let p = {};
395
+ if (l) try {
396
+ p = t.IOC.getPropertyData(o.MIDDLEWARE_OPTIONS, l, e) || {};
397
+ } catch {
398
+ p = {};
399
+ }
400
+ const c = {
401
+ ...p,
402
+ ...i
403
+ };
404
+ if (c.enabled === !1) {
405
+ y.Warn(`The middleware ${e} has been loaded but is disabled.`);
406
+ continue;
407
+ }
408
+ const f = await r.run(c, this.app);
409
+ if (a.Helper.isFunction(f)) {
410
+ let t = f;
411
+ if (c.protocol) {
412
+ const r = a.Helper.isArray(c.protocol) ? c.protocol : [ c.protocol ];
413
+ y.Log("Koatty", "", `Middleware ${e} limited to protocols: ${r.join(", ")}`);
414
+ t = o.protocolMiddleware(r, f);
415
+ }
416
+ if (t.length < 3) this.app.use(t); else this.app.useExp(t);
417
+ }
319
418
  }
320
419
  }
321
420
  async LoadControllers() {
322
421
  const e = t.IOC.listClass("CONTROLLER");
323
422
  const r = [];
324
- e.forEach((e => {
423
+ e.forEach(e => {
325
424
  var n;
326
425
  e.id = ((n = e.id) !== null && n !== void 0 ? n : "").replace("CONTROLLER:", "");
327
426
  if (e.id && a.Helper.isClass(e.target)) {
328
- g.Debug(`Load controller: ${e.id}`);
427
+ y.Debug(`Load controller: ${e.id}`);
329
428
  t.IOC.reg(e.id, e.target, {
330
429
  scope: "Prototype",
331
430
  type: "CONTROLLER",
@@ -335,31 +434,30 @@ class Loader {
335
434
  if (!o.implementsControllerInterface(a)) throw Error(`The controller ${e.id} must implements interface 'IController'.`);
336
435
  r.push(e.id);
337
436
  }
338
- }));
437
+ });
339
438
  return r;
340
439
  }
341
440
  async LoadServices() {
342
- const e = t.IOC.listClass("SERVICE");
343
- e.forEach((e => {
344
- var r;
345
- e.id = ((r = e.id) !== null && r !== void 0 ? r : "").replace("SERVICE:", "");
346
- if (e.id && a.Helper.isClass(e.target)) {
347
- g.Debug(`Load service: ${e.id}`);
348
- t.IOC.reg(e.id, e.target, {
441
+ var e;
442
+ const r = t.IOC.listClass("SERVICE");
443
+ for (const n of r) {
444
+ n.id = ((e = n.id) !== null && e !== void 0 ? e : "").replace("SERVICE:", "");
445
+ if (n.id && a.Helper.isClass(n.target)) {
446
+ y.Debug(`Load service: ${n.id}`);
447
+ t.IOC.reg(n.id, n.target, {
349
448
  scope: "Singleton",
350
449
  type: "SERVICE",
351
450
  args: []
352
451
  });
353
- const r = t.IOC.getInsByClass(e.target);
354
- if (!o.implementsServiceInterface(r)) throw Error(`The service ${e.id} must implements interface 'IService'.`);
452
+ const e = t.IOC.getInsByClass(n.target);
453
+ if (!o.implementsServiceInterface(e)) throw Error(`The service ${n.id} must implements interface 'IService'.`);
355
454
  }
356
- }));
455
+ }
357
456
  }
358
- async LoadComponents() {
359
- var e, r;
360
- const n = t.IOC.listClass("COMPONENT");
361
- const s = [];
362
- n.forEach((async e => {
457
+ async LoadComponents(e) {
458
+ var r, n;
459
+ const i = t.IOC.listClass("COMPONENT");
460
+ i.forEach(e => {
363
461
  var r;
364
462
  e.id = ((r = e.id) !== null && r !== void 0 ? r : "").replace("COMPONENT:", "");
365
463
  if (a.Helper.isClass(e.target)) {
@@ -368,36 +466,56 @@ class Loader {
368
466
  type: "COMPONENT",
369
467
  args: []
370
468
  });
371
- if (e.id && e.id.endsWith("Plugin")) {
372
- const r = t.IOC.getInsByClass(e.target);
373
- if (!o.implementsPluginInterface(r)) throw Error(`The plugin ${e.id} must implements interface 'IPlugin'.`);
374
- s.push(e.id);
375
- }
376
469
  if (e.id && e.id.endsWith("Aspect")) {
377
470
  const r = t.IOC.getInsByClass(e.target);
378
471
  if (!o.implementsAspectInterface(r)) throw Error(`The aspect ${e.id} must implements interface 'IAspect'.`);
379
472
  }
380
473
  }
381
- }));
382
- let i = this.app.config(void 0, "plugin");
383
- if (a.Helper.isEmpty(i)) i = {
384
- config: {},
385
- list: []
386
- };
387
- const l = (e = i.list) !== null && e !== void 0 ? e : [];
388
- for (const e of l) {
389
- const o = t.IOC.get(e, "COMPONENT");
390
- if (!o) throw Error(`Plugin ${e} load error.`);
391
- if (!a.Helper.isFunction(o.run)) throw Error(`Plugin ${e} must implements interface 'IPlugin'.`);
392
- if (i.config[e] === !1) {
393
- g.Warn(`Plugin ${e} already loaded but not effective.`);
394
- continue;
474
+ });
475
+ if (e) await e.loadUserPlugins(); else {
476
+ y.Warn("Loading plugins in legacy mode");
477
+ let e = this.app.config(void 0, "plugin");
478
+ if (a.Helper.isEmpty(e)) e = {
479
+ config: {},
480
+ list: []
481
+ };
482
+ const o = (r = e.list) !== null && r !== void 0 ? r : [];
483
+ for (const r of o) {
484
+ const o = t.IOC.get(r, "COMPONENT");
485
+ if (!o) throw Error(`Plugin ${r} load error.`);
486
+ if (!a.Helper.isFunction(o.run)) throw Error(`Plugin ${r} must implements interface 'IPlugin'.`);
487
+ if (e.config[r] === !1) {
488
+ y.Warn(`Plugin ${r} already loaded but not effective.`);
489
+ continue;
490
+ }
491
+ await o.run((n = e.config[r]) !== null && n !== void 0 ? n : {}, this.app);
395
492
  }
396
- await o.run((r = i.config[e]) !== null && r !== void 0 ? r : {}, this.app);
397
493
  }
398
494
  }
399
495
  async LoadRouter(e) {
400
- await this.app.router.LoadRouter(this.app, e);
496
+ var t;
497
+ const o = this.app.router;
498
+ y.Log("Koatty", "", "============ LoadRouter START ============");
499
+ y.Log("Koatty", "", "router type:", typeof o);
500
+ y.Log("Koatty", "", "router is object:", a.Helper.isObject(o));
501
+ y.Log("Koatty", "", "router has LoadRouter:", a.Helper.isFunction(o === null || o === void 0 ? void 0 : o.LoadRouter));
502
+ y.Log("Koatty", "", "Controllers to load:", e);
503
+ if (a.Helper.isObject(o) && !a.Helper.isFunction(o.LoadRouter)) {
504
+ const r = o;
505
+ y.Log("Koatty", "", `Multi-protocol routing: found ${Object.keys(r).length} routers (${Object.keys(r).join(", ")})`);
506
+ for (const o in r) {
507
+ y.Log("Koatty", "", `Checking protocol: ${o}, has LoadRouter:`, a.Helper.isFunction((t = r[o]) === null || t === void 0 ? void 0 : t.LoadRouter));
508
+ if (r[o] && a.Helper.isFunction(r[o].LoadRouter)) {
509
+ y.Log("Koatty", "", `Loading routes for protocol: ${o}`);
510
+ await r[o].LoadRouter(this.app, e);
511
+ y.Log("Koatty", "", `Completed loading routes for protocol: ${o}`);
512
+ }
513
+ }
514
+ } else if (a.Helper.isFunction(o.LoadRouter)) {
515
+ y.Log("Koatty", "", "Single protocol routing");
516
+ await o.LoadRouter(this.app, e);
517
+ } else y.Warn("No valid router found! router:", o);
518
+ y.Log("Koatty", "", "============ LoadRouter END ============");
401
519
  }
402
520
  }
403
521
 
@@ -409,29 +527,30 @@ class Loader {
409
527
  * @LastEditTime: 2025-01-14 16:11:21
410
528
  * @License: BSD (3-Clause)
411
529
  * @Copyright (c): <richenlin(at)gmail.com>
412
- */ function I(e) {
530
+ */ function b(e) {
413
531
  return function(t) {
414
532
  if (!(t.prototype instanceof o.Koatty)) throw new Error(`class does not inherit from Koatty`);
415
- S(t, e);
533
+ H(t, e);
534
+ return t;
416
535
  };
417
536
  }
418
537
 
419
538
  function P(e) {
420
539
  return async t => {
421
540
  if (!(t.prototype instanceof o.Koatty)) throw new Error(`class ${t.name} does not inherit from Koatty`);
422
- return await S(t, e, !0);
541
+ return await H(t, e, !0);
423
542
  };
424
543
  }
425
544
 
426
- function b(e) {
545
+ function S(e) {
427
546
  return r => {
428
547
  if (!(r.prototype instanceof o.Koatty)) throw new Error(`class does not inherit from Koatty`);
429
548
  e = e !== null && e !== void 0 ? e : "";
430
- t.IOC.saveClassMetadata(t.TAGGED_CLS, E, e, r);
549
+ t.IOC.saveClassMetadata(t.TAGGED_CLS, w, e, r);
431
550
  };
432
551
  }
433
552
 
434
- function A(e) {
553
+ function I(e) {
435
554
  return r => {
436
555
  if (!(r.prototype instanceof o.Koatty)) throw new Error(`class does not inherit from Koatty`);
437
556
  e = e !== null && e !== void 0 ? e : "";
@@ -439,59 +558,81 @@ function A(e) {
439
558
  };
440
559
  }
441
560
 
442
- function H(e, o, r) {
561
+ function K(e, o, r) {
443
562
  t.IOC.attachClassMetadata(t.TAGGED_CLS, e, o, r);
444
563
  }
445
564
 
446
- const S = async function(e, r, n = !1) {
447
- L();
448
- const s = v();
449
- if (!n && s) return;
450
- const i = Reflect.construct(e, []);
451
- if (s) {
452
- i.silent = !0;
453
- g.enable(!1);
565
+ const H = async function(e, r, n = !1) {
566
+ C();
567
+ const i = m();
568
+ if (!n && i) return;
569
+ const s = Reflect.construct(e, []);
570
+ if (i) {
571
+ s.silent = !0;
572
+ y.enable(!1);
454
573
  }
455
574
  try {
456
- if (!i.silent) g.Log("Koatty", w);
457
- if (!(i instanceof o.Koatty)) throw new Error(`class ${e.name} does not inherit from Koatty`);
458
- Loader.initialize(i);
575
+ if (!s.silent) console.log(A);
576
+ if (!(s instanceof o.Koatty)) throw new Error(`class ${e.name} does not inherit from Koatty`);
577
+ Loader.initialize(s);
459
578
  if (a.Helper.isFunction(r)) {
460
- g.Log("Koatty", "", "Execute bootFunc ...");
461
- await r(i);
579
+ y.Log("Koatty", "", "Execute bootFunc ...");
580
+ await r(s);
462
581
  }
463
- t.IOC.setApp(i);
464
- g.Log("Koatty", "", "ComponentScan ...");
465
- Loader.CheckAllComponents(i, e);
466
- Loader.LoadAppEventHooks(i, e);
467
- g.Log("Koatty", "", "Emit App Boot ...");
468
- await o.asyncEvent(i, "appBoot");
469
- await Loader.LoadAllComponents(i, e);
470
- g.Log("Koatty", "", "Emit App Ready ...");
471
- await o.asyncEvent(i, "appReady");
472
- if (!s) i.listen(M);
473
- return i;
582
+ t.IOC.setApp(s);
583
+ y.Log("Koatty", "", "ComponentScan ...");
584
+ Loader.CheckAllComponents(s, e);
585
+ Loader.LoadAppEventHooks(s, e);
586
+ y.Log("Koatty", "", "Emit App Boot ...");
587
+ await o.asyncEvent(s, o.AppEvent.appBoot);
588
+ await Loader.LoadAllComponents(s, e);
589
+ y.Log("Koatty", "", "Emit App Ready ...");
590
+ await o.asyncEvent(s, o.AppEvent.appReady);
591
+ if (!i) {
592
+ y.Log("Koatty", "", "Emit Before Server Start ...");
593
+ await o.asyncEvent(s, o.AppEvent.beforeServerStart);
594
+ s.listen(M);
595
+ y.Log("Koatty", "", "Emit After Server Start ...");
596
+ await o.asyncEvent(s, o.AppEvent.afterServerStart);
597
+ }
598
+ return s;
474
599
  } catch (e) {
475
- g.Error(e);
476
- process.exit();
600
+ y.Fatal(e);
601
+ process.exit(1);
477
602
  }
478
603
  };
479
604
 
480
605
  const M = e => {
481
- let t = [];
482
- if (!Array.isArray(e.server)) t = [ e.server ]; else t = e.server;
483
- g.Log("Koatty", "", "====================================");
484
- g.Log("Koatty", "", `Nodejs Version: ${process.version}`);
485
- g.Log("Koatty", "", `Koatty Version: v${y}`);
486
- g.Log("Koatty", "", `App Environment: ${e.env}`);
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);
606
+ y.Log("Koatty", "", "====================================");
607
+ y.Log("Koatty", "", `Nodejs Version: ${process.version}`);
608
+ y.Log("Koatty", "", `Koatty Version: v${v}`);
609
+ y.Log("Koatty", "", `App Environment: ${e.env}`);
610
+ if (e.appDebug) y.Warn(`Running in debug mode.`);
611
+ y.Log("Koatty", "", "====================================");
493
612
  };
494
613
 
614
+ /*
615
+ * @Description: framework main file
616
+ * @Usage:
617
+ * @Author: richen
618
+ * @Date: 2023-12-09 21:56:32
619
+ * @LastEditTime: 2025-01-14 16:12:41
620
+ * @License: BSD (3-Clause)
621
+ * @Copyright (c): <richenlin(at)gmail.com>
622
+ */ function R() {
623
+ try {
624
+ require("koatty_router/dist/RouterPlugin");
625
+ } catch (e) {}
626
+ try {
627
+ require("koatty_serve/dist/ServePlugin");
628
+ } catch (e) {}
629
+ try {
630
+ require("koatty_trace/dist/TracePlugin");
631
+ } catch (e) {}
632
+ }
633
+
634
+ R();
635
+
495
636
  Object.defineProperty(exports, "Config", {
496
637
  enumerable: !0,
497
638
  get: function() {
@@ -506,50 +647,50 @@ Object.defineProperty(exports, "Helper", {
506
647
  }
507
648
  });
508
649
 
509
- exports.BindEventHook = H;
650
+ exports.BindEventHook = K;
510
651
 
511
- exports.Bootstrap = I;
652
+ exports.Bootstrap = b;
512
653
 
513
- exports.ComponentScan = b;
654
+ exports.ComponentScan = S;
514
655
 
515
- exports.ConfigurationScan = A;
656
+ exports.ConfigurationScan = I;
516
657
 
517
658
  exports.ExecBootStrap = P;
518
659
 
519
- exports.Logger = g;
660
+ exports.Logger = y;
520
661
 
521
- Object.keys(t).forEach((function(e) {
662
+ Object.keys(t).forEach(function(e) {
522
663
  if (e !== "default" && !Object.prototype.hasOwnProperty.call(exports, e)) Object.defineProperty(exports, e, {
523
664
  enumerable: !0,
524
665
  get: function() {
525
666
  return t[e];
526
667
  }
527
668
  });
528
- }));
669
+ });
529
670
 
530
- Object.keys(o).forEach((function(e) {
671
+ Object.keys(o).forEach(function(e) {
531
672
  if (e !== "default" && !Object.prototype.hasOwnProperty.call(exports, e)) Object.defineProperty(exports, e, {
532
673
  enumerable: !0,
533
674
  get: function() {
534
675
  return o[e];
535
676
  }
536
677
  });
537
- }));
678
+ });
538
679
 
539
- Object.keys(r).forEach((function(e) {
680
+ Object.keys(r).forEach(function(e) {
540
681
  if (e !== "default" && !Object.prototype.hasOwnProperty.call(exports, e)) Object.defineProperty(exports, e, {
541
682
  enumerable: !0,
542
683
  get: function() {
543
684
  return r[e];
544
685
  }
545
686
  });
546
- }));
687
+ });
547
688
 
548
- Object.keys(n).forEach((function(e) {
689
+ Object.keys(n).forEach(function(e) {
549
690
  if (e !== "default" && !Object.prototype.hasOwnProperty.call(exports, e)) Object.defineProperty(exports, e, {
550
691
  enumerable: !0,
551
692
  get: function() {
552
693
  return n[e];
553
694
  }
554
695
  });
555
- }));
696
+ });