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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-04-19 23:06:49
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 e, TAGGED_CLS as o } from "koatty_container";
12
+ import { IOC as o, TAGGED_CLS as e } from "koatty_container";
13
13
 
14
14
  export * from "koatty_container";
15
15
 
16
- import { AppEventArr as a, implementsMiddlewareInterface as s, parseExp as r, implementsControllerInterface as i, implementsServiceInterface as n, implementsPluginInterface as l, implementsAspectInterface as c, Koatty as p, asyncEvent as d } from "koatty_core";
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 f } from "koatty_lib";
22
+ import { Helper as g } from "koatty_lib";
23
23
 
24
24
  export { Helper } from "koatty_lib";
25
25
 
26
- import { NewRouter as g } from "koatty_router";
26
+ import { NewRouter as h } from "koatty_router";
27
27
 
28
28
  export * from "koatty_router";
29
29
 
30
- import { DefaultLogger as u } from "koatty_logger";
30
+ import { DefaultLogger as y } from "koatty_logger";
31
31
 
32
- import { Load as h } from "koatty_loader";
32
+ import { Load as m } from "koatty_loader";
33
33
 
34
- import { NewServe as m } from "koatty_serve";
34
+ import { NewServe as L } from "koatty_serve";
35
35
 
36
- import * as y from "path";
36
+ import { Trace as v } from "koatty_trace";
37
37
 
38
- var L = "3.13.0";
38
+ import * as C from "path";
39
39
 
40
- var v = {
41
- node: ">12.0.0"
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 w = u;
53
-
54
- function C(t, e) {
55
- if (e.logLevel) u.setLevel(e.logLevel);
56
- if (e.logFilePath && !t.silent) {
57
- f.define(t, "logsPath", e.logFilePath);
58
- process.env.LOGS_PATH = e.logFilePath;
59
- u.setLogFilePath(e.logFilePath);
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 (e.sensFields) u.setSensFields(e.sensFields);
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 E = L;
83
+ */ const P = w;
73
84
 
74
- const P = v.node.slice(1) || "12.0.0";
85
+ const A = E.node.slice(1) || "12.0.0";
75
86
 
76
- function A(t, e, o, a) {
77
- if (f.isClass(o) && o.name != t) throw Error(`The file(${e}) name should be always the same as class name.`);
78
- if (o["__esModule"]) if (o.name === void 0) {
79
- const a = Object.keys(o);
80
- if (a[0] != t && f.isClass(o[a[0]])) throw Error(`The file(${e}) name should be always the same as class name.`);
81
- } else if (o.name != t) throw Error(`The file(${e}) name should be always the same as class name.`);
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 \`${e}\`.`);
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 M() {
89
- let t = P;
99
+ function K() {
100
+ let t = A;
90
101
  t = t.slice(0, t.lastIndexOf("."));
91
- let e = process.version;
92
- if (e[0] === "v") e = e.slice(1);
93
- e = e.slice(0, e.lastIndexOf("."));
94
- if (f.toNumber(t) > f.toNumber(e)) {
95
- w.Error(`Koatty need node version > ${t}, current version is ${e}, please upgrade it.`);
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 e = JSON.stringify(process.argv[1]);
103
- if (e.indexOf("jest") > -1) t = !0;
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 R = "COMPONENT_SCAN";
126
+ */ const k = "COMPONENT_SCAN";
116
127
 
117
- const k = "CONFIGURATION_SCAN";
128
+ const O = "CONFIGURATION_SCAN";
118
129
 
119
- const T = `\n\n┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬\n├┴┐│ │├─┤ │ │ └┬┘\n┴ ┴└─┘┴ ┴ ┴ ┴ ┴ \n-------------------------------------------\nhttps://github.com/koatty\n`
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") w.setLevel("debug"); else w.setLevel("info");
136
- const e = t.rootPath || process.cwd();
137
- const o = t.appPath || y.resolve(e, t.appDebug ? "src" : "dist");
138
- const a = y.resolve(__dirname, "..");
139
- f.define(t, "rootPath", e);
140
- f.define(t, "appPath", o);
141
- f.define(t, "koattyPath", a);
142
- if (f.isEmpty(t.name)) {
143
- const e = f.safeRequire(`${y.dirname(o)}/package.json`);
144
- if (e.name) {
145
- t.name = e.name;
146
- t.version = t.version || e.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 = e;
150
- process.env.APP_PATH = o;
160
+ process.env.ROOT_PATH = o;
161
+ process.env.APP_PATH = e;
151
162
  process.env.KOATTY_PATH = a;
152
- f.define(t, "thinkPath", a);
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
- if (s) if (f.isArray(s)) r = s; else r.push(s);
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 CheckAllComponents(t, e) {
169
- const o = Loader.GetComponentMeta(t, e);
170
- const a = Loader.GetConfigurationMeta(t, e);
171
- const s = new Set;
172
- h(o, "", ((t, e, o) => {
173
- A(t, e, o, s);
174
- }), [ "**/**.js", "**/**.ts", "!**/**.d.ts" ], [ ...a, `${e.name || ".no"}.ts` ]);
175
- s.clear();
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 e = t.getMetaData("_configs") || [];
179
- const o = e[0] || {};
180
- if (o.config) {
181
- const e = o.config;
182
- let a = "debug", s = "", r = [];
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 (e.logsLevel) a = e.logsLevel.toLowerCase();
185
- if (e.logsPath) s = e.logsPath;
186
- if (e.sensFields) r = e.sensFields;
187
- C(t, {
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: s,
190
- sensFields: r
200
+ logFilePath: r,
201
+ sensFields: s
191
202
  });
192
203
  }
193
204
  }
194
205
  static LoadAppEventHooks(t, s) {
195
- const r = new Map;
196
- for (const t of a) {
197
- let a;
206
+ const i = new Map;
207
+ for (const t of r) {
208
+ let r;
198
209
  switch (t) {
199
- case "appBoot":
200
- a = e.getClassMetadata(o, "appBoot", s);
201
- if (f.isArray(a)) r.set("appBoot", a);
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 "appReady":
205
- a = e.getClassMetadata(o, "appReady", s);
206
- if (f.isArray(a)) r.set("appReady", a);
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 "appStart":
210
- a = e.getClassMetadata(o, "appStart", s);
211
- if (f.isArray(a)) r.set("appStart", a);
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 "appStop":
215
- a = e.getClassMetadata(o, "appStop", s);
216
- if (f.isArray(a)) r.set("appStop", a);
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 [e, o] of r) for (const a of o) t.once(e, (() => a(t)));
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 o, a, s;
224
- w.Log("Koatty", "", "Load Configurations ...");
225
- const r = Loader.GetConfigurationMeta(t, e);
226
- const i = new Loader(t);
227
- i.LoadConfigs(r);
228
- const n = (o = t.config("server")) !== null && o !== void 0 ? o : {
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 l = (a = n.protocol) !== null && a !== void 0 ? a : "http";
232
- f.define(t, "server", m(t, n));
233
- const c = (s = t.config(void 0, "router")) !== null && s !== void 0 ? s : {};
234
- f.define(t, "router", g(t, {
235
- protocol: l,
236
- ...c
237
- }));
238
- w.Log("Koatty", "", "Load Components ...");
239
- await i.LoadComponents();
240
- w.Log("Koatty", "", "Load Middlewares ...");
241
- await i.LoadMiddlewares();
242
- w.Log("Koatty", "", "Load Services ...");
243
- await i.LoadServices();
244
- w.Log("Koatty", "", "Load Controllers ...");
245
- const p = await i.LoadControllers();
246
- w.Log("Koatty", "", "Load Routers ...");
247
- i.LoadRouter(p);
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(e) {
250
- const o = {};
251
- h([ "./config" ], this.app.koattyPath, (function(t, e, a) {
252
- o[t] = a;
253
- }));
254
- if (f.isArray(e)) e = e.length > 0 ? e : [ "./config" ];
255
- let a = t(e, this.app.appPath);
256
- a = f.extend(o, a, !0);
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
- let o = this.app.config(void 0, "middleware");
262
- if (f.isEmpty(o)) o = {
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 a = (t = e.listClass("MIDDLEWARE")) !== null && t !== void 0 ? t : [];
268
- a.forEach((t => {
269
- var o;
270
- t.id = ((o = t.id) !== null && o !== void 0 ? o : "").replace("MIDDLEWARE:", "");
271
- if (t.id && f.isClass(t.target)) {
272
- e.reg(t.id, t.target, {
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 o = e.getInsByClass(t.target);
278
- if (!s(o)) throw Error(`The middleware ${t.id} must implements interface 'IMiddleware'.`);
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 = e.listClass("CONTROLLER");
312
- const o = [];
313
- t.forEach((t => {
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 && f.isClass(t.target)) {
317
- w.Debug(`Load controller: ${t.id}`);
318
- e.reg(t.id, t.target, {
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 = e.getInsByClass(t.target);
324
- if (!i(a)) throw Error(`The controller ${t.id} must implements interface 'IController'.`);
325
- o.push(t.id);
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 o;
407
+ });
408
+ return e;
329
409
  }
330
410
  async LoadServices() {
331
- const t = e.listClass("SERVICE");
332
- t.forEach((t => {
333
- var o;
334
- t.id = ((o = t.id) !== null && o !== void 0 ? o : "").replace("SERVICE:", "");
335
- if (t.id && f.isClass(t.target)) {
336
- w.Debug(`Load service: ${t.id}`);
337
- e.reg(t.id, t.target, {
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 o = e.getInsByClass(t.target);
343
- if (!n(o)) throw Error(`The service ${t.id} must implements interface 'IService'.`);
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, o;
349
- const a = e.listClass("COMPONENT");
350
- const s = [];
351
- a.forEach((async t => {
352
- var o;
353
- t.id = ((o = t.id) !== null && o !== void 0 ? o : "").replace("COMPONENT:", "");
354
- if (f.isClass(t.target)) {
355
- e.reg(t.id, t.target, {
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 o = e.getInsByClass(t.target);
362
- if (!l(o)) throw Error(`The plugin ${t.id} must implements interface 'IPlugin'.`);
363
- s.push(t.id);
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 o = e.getInsByClass(t.target);
367
- if (!c(o)) throw Error(`The aspect ${t.id} must implements interface 'IAspect'.`);
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 r = this.app.config(void 0, "plugin");
372
- if (f.isEmpty(r)) r = {
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 = r.list) !== null && t !== void 0 ? t : [];
456
+ const i = (t = s.list) !== null && t !== void 0 ? t : [];
377
457
  for (const t of i) {
378
- const a = e.get(t, "COMPONENT");
458
+ const a = o.get(t, "COMPONENT");
379
459
  if (!a) throw Error(`Plugin ${t} load error.`);
380
- if (!f.isFunction(a.run)) throw Error(`Plugin ${t} must implements interface 'IPlugin'.`);
381
- if (r.config[t] === !1) {
382
- w.Warn(`Plugin ${t} already loaded but not effective.`);
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((o = r.config[t]) !== null && o !== void 0 ? o : {}, this.app);
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
- await this.app.router.LoadRouter(this.app, t);
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 _(t) {
402
- return function(e) {
403
- if (!(e.prototype instanceof p)) throw new Error(`class does not inherit from Koatty`);
404
- b(e, t);
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 O(t) {
409
- return async e => {
410
- if (!(e.prototype instanceof p)) throw new Error(`class ${e.name} does not inherit from Koatty`);
411
- return await b(e, t, !0);
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 S(t) {
518
+ function F(t) {
416
519
  return a => {
417
- if (!(a.prototype instanceof p)) throw new Error(`class does not inherit from Koatty`);
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
- e.saveClassMetadata(o, R, t, a);
522
+ o.saveClassMetadata(e, k, t, a);
420
523
  };
421
524
  }
422
525
 
423
- function I(t) {
526
+ function N(t) {
424
527
  return a => {
425
- if (!(a.prototype instanceof p)) throw new Error(`class does not inherit from Koatty`);
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
- e.saveClassMetadata(o, k, t, a);
530
+ o.saveClassMetadata(e, O, t, a);
428
531
  };
429
532
  }
430
533
 
431
- function K(t, a, s) {
432
- e.attachClassMetadata(o, t, a, s);
534
+ function I(t, a, r) {
535
+ o.attachClassMetadata(e, t, a, r);
433
536
  }
434
537
 
435
- const b = async function(t, o, a = !1) {
436
- M();
538
+ const x = async function(t, e, r = !1) {
539
+ K();
437
540
  const s = $();
438
- if (!a && s) return;
439
- const r = Reflect.construct(t, []);
541
+ if (!r && s) return;
542
+ const i = Reflect.construct(t, []);
440
543
  if (s) {
441
- r.silent = !0;
442
- w.enable(!1);
544
+ i.silent = !0;
545
+ b.enable(!1);
443
546
  }
444
547
  try {
445
- if (!r.silent) w.Log("Koatty", T);
446
- if (!(r instanceof p)) throw new Error(`class ${t.name} does not inherit from Koatty`);
447
- Loader.initialize(r);
448
- if (f.isFunction(o)) {
449
- w.Log("Koatty", "", "Execute bootFunc ...");
450
- await o(r);
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
- e.setApp(r);
453
- w.Log("Koatty", "", "ComponentScan ...");
454
- Loader.CheckAllComponents(r, t);
455
- Loader.LoadAppEventHooks(r, t);
456
- w.Log("Koatty", "", "Emit App Boot ...");
457
- await d(r, "appBoot");
458
- await Loader.LoadAllComponents(r, t);
459
- w.Log("Koatty", "", "Emit App Ready ...");
460
- await d(r, "appReady");
461
- if (!s) r.listen(N);
462
- return r;
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
- w.Error(t);
465
- process.exit();
567
+ b.Fatal(t);
568
+ process.exit(1);
466
569
  }
467
570
  };
468
571
 
469
- const N = t => {
470
- let e = [];
471
- if (!Array.isArray(t.server)) e = [ t.server ]; else e = t.server;
472
- w.Log("Koatty", "", "====================================");
473
- w.Log("Koatty", "", `Nodejs Version: ${process.version}`);
474
- w.Log("Koatty", "", `Koatty Version: v${E}`);
475
- w.Log("Koatty", "", `App Environment: ${t.env}`);
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 { K as BindEventHook, _ as Bootstrap, S as ComponentScan, I as ConfigurationScan, O as ExecBootStrap, w as Logger };
581
+ export { I as BindEventHook, T as Bootstrap, F as ComponentScan, N as ConfigurationScan, _ as ExecBootStrap, b as Logger };