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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-11-02 10:59:15
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, 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";
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 g } from "koatty_lib";
22
+ import { Helper as u } from "koatty_lib";
23
23
 
24
24
  export { Helper } from "koatty_lib";
25
25
 
26
- import { NewRouter as h } from "koatty_router";
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 L } from "koatty_serve";
34
+ import { NewServe as h } from "koatty_serve";
35
35
 
36
36
  import { Trace as v } from "koatty_trace";
37
37
 
38
- import * as C from "path";
38
+ import * as w from "path";
39
39
 
40
- var w = "3.14.0-beta.1";
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 b = y;
54
+ */ const K = y;
55
55
 
56
- function R(t, o) {
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
- g.define(t, "logsPath", o.logFilePath);
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 P = w;
83
+ */ const b = C;
84
84
 
85
- const A = E.node.slice(1) || "12.0.0";
85
+ const M = E.node.slice(1) || "12.0.0";
86
86
 
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.`);
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 && g.isClass(e[a[0]])) throw Error(`The file(${o}) name should be always the same as class name.`);
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 K() {
100
- let t = A;
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 (g.toNumber(t) > g.toNumber(o)) {
106
- b.Fatal(`Koatty need node version > ${t}, current version is ${o}, please upgrade it.`);
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 O = "CONFIGURATION_SCAN";
128
+ const $ = "CONFIGURATION_SCAN";
129
129
 
130
- const S = `\n\n┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬\n├┴┐│ │├─┤ │ │ └┬┘\n┴ ┴└─┘┴ ┴ ┴ ┴ ┴ \n-------------------------------------------\nhttps://github.com/koatty\n`
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") b.setLevel("debug"); else b.setLevel("info");
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 || 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`);
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
- g.define(t, "thinkPath", a);
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 s = o.getClassMetadata(e, k, a);
169
- if (s) if (g.isArray(s)) r = s; else r.push(s);
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, O, a);
175
- let s = [];
176
- if (r) if (g.isArray(r)) s = r; else s.push(r);
177
- return s;
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
- M(t, o, e, r);
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 = "", s = [];
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) s = o.sensFields;
198
- R(t, {
198
+ if (o.sensFields) i = o.sensFields;
199
+ P(t, {
199
200
  logLevel: a,
200
201
  logFilePath: r,
201
- sensFields: s
202
+ sensFields: i
202
203
  });
203
204
  }
204
205
  }
205
- static LoadAppEventHooks(t, s) {
206
- const i = new Map;
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, s);
212
- if (g.isArray(r)) i.set(a.appBoot, r);
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, s);
217
- if (g.isArray(r)) i.set(a.appReady, r);
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, s);
222
- if (g.isArray(r)) i.set(a.appStart, r);
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, s);
227
- if (g.isArray(r)) i.set(a.appStop, r);
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 i) for (const a of e) t.once(o, () => a(t));
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 (g.isFunction(o.preloadMetadata)) o.preloadMetadata();
236
+ if (u.isFunction(o.preloadMetadata)) o.preloadMetadata();
237
237
  } catch {
238
- b.Warn("[Loader] preloadMetadata is optional, ignore if not available");
238
+ K.Warn("[Loader] preloadMetadata is optional, ignore if not available");
239
239
  }
240
- b.Log("Koatty", "", "Load Configurations ...");
241
- const i = Loader.GetConfigurationMeta(t, e);
240
+ K.Log("Koatty", "", "Load Configurations ...");
241
+ const r = Loader.GetConfigurationMeta(t, e);
242
242
  const n = new Loader(t);
243
- n.LoadConfigs(i);
243
+ n.LoadConfigs(r);
244
244
  Loader.SetLogger(t);
245
- const l = (a = t.config("server")) !== null && a !== void 0 ? a : {
246
- protocol: "http"
247
- };
248
- const c = (r = l.protocol) !== null && r !== void 0 ? r : "http";
249
- const d = (s = t.config(void 0, "router")) !== null && s !== void 0 ? s : {};
250
- const p = g.isArray(c) ? c : [ c ];
251
- const f = Loader.CreateServers(t, l, p);
252
- g.define(t, "server", f);
253
- const u = Loader.CreateRouters(t, d, p);
254
- g.define(t, "router", u);
255
- b.Log("Koatty", "", "Load Components ...");
256
- await n.LoadComponents();
257
- b.Log("Koatty", "", "Load Middlewares ...");
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
- b.Log("Koatty", "", "Load Services ...");
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
- b.Log("Koatty", "", "Load Controllers ...");
262
- const h = await n.LoadControllers();
263
- b.Log("Koatty", "", "Load Routers ...");
264
- await n.LoadRouter(h);
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 = 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);
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 i = e[r];
292
+ const s = e[r];
274
293
  const n = {
275
294
  ...o,
276
- protocol: i,
277
- port: s[r]
295
+ protocol: s,
296
+ port: i[r]
278
297
  };
279
- a.push(L(t, n));
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 L(t, r);
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] = h(t, e);
319
+ a[r] = L(t, e);
301
320
  }
302
321
  return a;
303
322
  } else {
304
323
  const a = e[0];
305
- return h(t, {
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 (g.isArray(o)) o = o.length > 0 ? o : [ "./config" ];
335
+ if (u.isArray(o)) o = o.length > 0 ? o : [ "./config" ];
317
336
  let a = t(o, this.app.appPath);
318
- a = g.extend(e, a, !0);
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
- g.define(this.app, "tracer", e);
345
+ u.define(this.app, "tracer", e);
327
346
  this.app.use(e);
328
- b.Debug(`Load trace middleware`);
347
+ K.Debug(`Load trace middleware`);
329
348
  } catch (t) {
330
- b.Warn(`Trace middleware failed to load: ${t.message}`);
349
+ K.Warn(`Trace middleware failed to load: ${t.message}`);
331
350
  }
332
351
  let r = this.app.config(void 0, "middleware");
333
- if (g.isEmpty(r)) r = {
352
+ if (u.isEmpty(r)) r = {
334
353
  config: {},
335
354
  list: []
336
355
  };
337
- const l = (e = o.listClass("MIDDLEWARE")) !== null && e !== void 0 ? e : [];
338
- l.forEach(t => {
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 && g.isClass(t.target)) {
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 (!s(e)) throw Error(`The middleware ${t.id} must implements interface 'IMiddleware'.`);
367
+ if (!n(e)) throw Error(`The middleware ${t.id} must implements interface 'IMiddleware'.`);
349
368
  }
350
369
  });
351
- const c = r.list || [];
370
+ const s = r.list || [];
352
371
  const d = new Set([]);
353
- c.forEach(t => {
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 (!g.isFunction(e.run)) throw Error(`The middleware ${t} must implements interface 'IMiddleware'.`);
361
- b.Debug(`Load middleware: ${t}`);
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 = l.find(o => o.id === t)) === null || a === void 0 ? void 0 : a.target;
364
- let c = {};
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
- c = o.getPropertyData(i, s, t) || {};
385
+ n = o.getPropertyData(l, s, t) || {};
367
386
  } catch {
368
- c = {};
387
+ n = {};
369
388
  }
370
389
  const d = {
371
- ...c,
390
+ ...n,
372
391
  ...r
373
392
  };
374
393
  if (d.enabled === !1) {
375
- b.Warn(`The middleware ${t} has been loaded but is disabled.`);
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 (g.isFunction(f)) {
398
+ if (u.isFunction(f)) {
380
399
  let o = f;
381
400
  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);
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 && g.isClass(t.target)) {
397
- b.Debug(`Load controller: ${t.id}`);
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 (!l(a)) throw Error(`The controller ${t.id} must implements interface 'IController'.`);
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
- 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, {
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 e = o.getInsByClass(t.target);
423
- if (!c(e)) throw Error(`The service ${t.id} must implements interface 'IService'.`);
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 t, e;
429
- const a = o.listClass("COMPONENT");
430
- const r = [];
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 (g.isClass(t.target)) {
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 (!p(e)) throw Error(`The aspect ${t.id} must implements interface 'IAspect'.`);
460
+ if (!f(e)) throw Error(`The aspect ${t.id} must implements interface 'IAspect'.`);
448
461
  }
449
462
  }
450
463
  });
451
- let s = this.app.config(void 0, "plugin");
452
- if (g.isEmpty(s)) s = {
453
- config: {},
454
- list: []
455
- };
456
- const i = (t = s.list) !== null && t !== void 0 ? t : [];
457
- for (const t of i) {
458
- const a = o.get(t, "COMPONENT");
459
- if (!a) throw Error(`Plugin ${t} load error.`);
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.`);
463
- continue;
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
- 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)) {
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
- b.Log("Koatty", "", `Multi-protocol routing: found ${Object.keys(a).length} routers (${Object.keys(a).join(", ")})`);
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
- 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}`);
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
- b.Log("Koatty", "", `Completed loading routes for protocol: ${e}`);
500
+ K.Log("Koatty", "", `Completed loading routes for protocol: ${e}`);
485
501
  }
486
502
  }
487
- } else if (g.isFunction(e.LoadRouter)) {
488
- b.Log("Koatty", "", "Single protocol routing");
503
+ } else if (u.isFunction(e.LoadRouter)) {
504
+ K.Log("Koatty", "", "Single protocol routing");
489
505
  await e.LoadRouter(this.app, t);
490
- } else b.Warn("No valid router found! router:", e);
491
- b.Log("Koatty", "", "============ LoadRouter END ============");
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 f)) throw new Error(`class does not inherit from Koatty`);
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 f)) throw new Error(`class ${o.name} does not inherit from Koatty`);
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 f)) throw new Error(`class does not inherit from Koatty`);
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 f)) throw new Error(`class does not inherit from Koatty`);
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, O, t, a);
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
- K();
540
- const s = $();
555
+ S();
556
+ const s = A();
541
557
  if (!r && s) return;
542
- const i = Reflect.construct(t, []);
558
+ const n = Reflect.construct(t, []);
543
559
  if (s) {
544
- i.silent = !0;
545
- b.enable(!1);
560
+ n.silent = !0;
561
+ K.enable(!1);
546
562
  }
547
563
  try {
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);
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(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;
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
- b.Fatal(t);
589
+ K.Fatal(t);
568
590
  process.exit(1);
569
591
  }
570
592
  };
571
593
 
572
594
  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", "", "====================================");
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
- export { I as BindEventHook, T as Bootstrap, F as ComponentScan, N as ConfigurationScan, _ as ExecBootStrap, b as Logger };
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 };