koatty 3.11.4-2 → 3.11.7
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/.rollup.config.js +59 -62
- package/.vscode/launch.json +41 -41
- package/CHANGELOG.md +365 -349
- package/LICENSE +29 -29
- package/README.md +179 -168
- package/dist/LICENSE +29 -29
- package/dist/README.md +179 -168
- package/dist/index.d.ts +5 -128
- package/dist/index.js +328 -459
- package/dist/index.mjs +322 -435
- package/dist/package.json +106 -99
- package/package.json +106 -99
- package/tsconfig.test.json +14 -14
- package/.vscode/settings.json +0 -5
package/dist/index.js
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
/*!
|
2
2
|
* @Author: richen
|
3
|
-
* @Date: 2024-
|
3
|
+
* @Date: 2024-12-06 09:38:17
|
4
4
|
* @License: BSD (3-Clause)
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
6
6
|
* @HomePage: https://koatty.org/
|
7
7
|
*/
|
8
8
|
'use strict';
|
9
9
|
|
10
|
-
var
|
10
|
+
var koatty_config = require('koatty_config');
|
11
11
|
var koatty_container = require('koatty_container');
|
12
12
|
var koatty_core = require('koatty_core');
|
13
|
-
var koatty_serve = require('koatty_serve');
|
14
|
-
var path = require('path');
|
15
|
-
var koatty_loader = require('koatty_loader');
|
16
13
|
var koatty_exception = require('koatty_exception');
|
17
|
-
var
|
18
|
-
var koatty_trace = require('koatty_trace');
|
14
|
+
var koatty_router = require('koatty_router');
|
19
15
|
var koatty_lib = require('koatty_lib');
|
20
16
|
var koatty_logger = require('koatty_logger');
|
21
|
-
require('
|
17
|
+
var koatty_loader = require('koatty_loader');
|
18
|
+
var koatty_serve = require('koatty_serve');
|
19
|
+
var path = require('path');
|
22
20
|
|
23
21
|
function _interopNamespaceDefault(e) {
|
24
22
|
var n = Object.create(null);
|
@@ -39,75 +37,12 @@ function _interopNamespaceDefault(e) {
|
|
39
37
|
|
40
38
|
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
41
39
|
|
42
|
-
function TraceHandler(app) {
|
43
|
-
const timeout = (app.config('http_timeout') || 10) * 1000;
|
44
|
-
const encoding = app.config('encoding') || 'utf-8';
|
45
|
-
const openTrace = app.config("open_trace") || false;
|
46
|
-
const asyncHooks = app.config("async_hooks") || false;
|
47
|
-
const options = {
|
48
|
-
RequestIdHeaderName: app.config('trace_header') || 'X-Request-Id',
|
49
|
-
RequestIdName: app.config('trace_id') || "requestId",
|
50
|
-
IdFactory: undefined,
|
51
|
-
Timeout: timeout,
|
52
|
-
Encoding: encoding,
|
53
|
-
OpenTrace: openTrace,
|
54
|
-
AsyncHooks: asyncHooks,
|
55
|
-
};
|
56
|
-
app.use(koatty_trace.Trace(options, app));
|
57
|
-
}
|
58
|
-
|
59
|
-
/*
|
60
|
-
* @Description: framework helper
|
61
|
-
* @Usage:
|
62
|
-
* @Author: richen
|
63
|
-
* @Date: 2023-12-09 21:56:32
|
64
|
-
* @LastEditTime: 2024-01-16 01:20:17
|
65
|
-
* @License: BSD (3-Clause)
|
66
|
-
* @Copyright (c): <richenlin(at)gmail.com>
|
67
|
-
*/
|
68
|
-
/**
|
69
|
-
* Check class file
|
70
|
-
* name should be always the same as class name
|
71
|
-
* class must be unique
|
72
|
-
*
|
73
|
-
* @export
|
74
|
-
* @param {string} fileName
|
75
|
-
* @param {string} xpath
|
76
|
-
* @param {*} target
|
77
|
-
* @param {Set<unknown>} [exSet]
|
78
|
-
* @returns {*}
|
79
|
-
*/
|
80
|
-
function checkClass(fileName, xpath, target, exSet) {
|
81
|
-
if (koatty_lib.Helper.isClass(target) && target.name != fileName) { // export default class name{}
|
82
|
-
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
83
|
-
}
|
84
|
-
if (target["__esModule"]) {
|
85
|
-
if (target.name === undefined) { // export class name{}
|
86
|
-
const keys = Object.keys(target);
|
87
|
-
if (keys[0] != fileName && koatty_lib.Helper.isClass(target[keys[0]])) {
|
88
|
-
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
89
|
-
}
|
90
|
-
}
|
91
|
-
else if (target.name != fileName) { // export default class {}
|
92
|
-
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
93
|
-
}
|
94
|
-
}
|
95
|
-
if (!exSet) {
|
96
|
-
return;
|
97
|
-
}
|
98
|
-
if (exSet.has(fileName)) {
|
99
|
-
throw new Error(`A same class already exists. at \`${xpath}\`.`);
|
100
|
-
}
|
101
|
-
exSet.add(fileName);
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
|
105
40
|
/*
|
106
41
|
* @Description: framework logger
|
107
42
|
* @Usage:
|
108
43
|
* @Author: richen
|
109
44
|
* @Date: 2023-12-09 21:56:32
|
110
|
-
* @LastEditTime:
|
45
|
+
* @LastEditTime: 2024-10-31 17:52:43
|
111
46
|
* @License: BSD (3-Clause)
|
112
47
|
* @Copyright (c): <richenlin(at)gmail.com>
|
113
48
|
*/
|
@@ -137,125 +72,98 @@ function SetLogger(app, config) {
|
|
137
72
|
}
|
138
73
|
}
|
139
74
|
|
75
|
+
var version = "3.11.7";
|
76
|
+
var engines = {
|
77
|
+
node: ">12.0.0"
|
78
|
+
};
|
79
|
+
|
140
80
|
/*
|
141
|
-
* @Description:
|
81
|
+
* @Description: framework runtime checker
|
142
82
|
* @Usage:
|
143
83
|
* @Author: richen
|
144
84
|
* @Date: 2023-12-09 21:56:32
|
145
|
-
* @LastEditTime:
|
85
|
+
* @LastEditTime: 2023-12-09 23:01:22
|
146
86
|
* @License: BSD (3-Clause)
|
147
87
|
* @Copyright (c): <richenlin(at)gmail.com>
|
148
88
|
*/
|
149
|
-
|
150
|
-
|
151
|
-
* Indicates that an decorated class is a "component".
|
152
|
-
*
|
153
|
-
* @export
|
154
|
-
* @param {string} [identifier] component name
|
155
|
-
* @returns {ClassDecorator}
|
156
|
-
*/
|
157
|
-
function Component(identifier) {
|
158
|
-
return (target) => {
|
159
|
-
identifier = identifier || koatty_container.IOCContainer.getIdentifier(target);
|
160
|
-
koatty_container.IOCContainer.saveClass("COMPONENT", target, identifier);
|
161
|
-
};
|
162
|
-
}
|
89
|
+
const KOATTY_VERSION = version;
|
90
|
+
const ENGINES_VERSION = engines.node.slice(1) || '12.0.0';
|
163
91
|
/**
|
164
|
-
*
|
165
|
-
*
|
166
|
-
* @export
|
167
|
-
* @param {string} [path] controller router path
|
168
|
-
* @returns {ClassDecorator}
|
92
|
+
* check node version
|
93
|
+
* @return {void} []
|
169
94
|
*/
|
170
|
-
function
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
95
|
+
function checkRuntime() {
|
96
|
+
let nodeEngines = ENGINES_VERSION;
|
97
|
+
nodeEngines = nodeEngines.slice(0, nodeEngines.lastIndexOf('.'));
|
98
|
+
let nodeVersion = process.version;
|
99
|
+
if (nodeVersion[0] === 'v') {
|
100
|
+
nodeVersion = nodeVersion.slice(1);
|
101
|
+
}
|
102
|
+
nodeVersion = nodeVersion.slice(0, nodeVersion.lastIndexOf('.'));
|
103
|
+
if (koatty_lib.Helper.toNumber(nodeEngines) > koatty_lib.Helper.toNumber(nodeVersion)) {
|
104
|
+
Logger.Error(`Koatty need node version > ${nodeEngines}, current version is ${nodeVersion}, please upgrade it.`);
|
105
|
+
process.exit(-1);
|
106
|
+
}
|
176
107
|
}
|
177
108
|
/**
|
178
|
-
*
|
179
|
-
*
|
180
|
-
* @
|
181
|
-
* @param {string} [identifier] class name
|
182
|
-
* @returns {ClassDecorator}
|
109
|
+
* unittest running environment detection
|
110
|
+
* only support jest
|
111
|
+
* @returns {boolean}
|
183
112
|
*/
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
* @
|
113
|
+
const checkUTRuntime = () => {
|
114
|
+
let isUTRuntime = false;
|
115
|
+
// UT运行环境判断,暂时先只判断jest
|
116
|
+
const argv = JSON.stringify(process.argv[1]);
|
117
|
+
if (argv.indexOf('jest') > -1) {
|
118
|
+
isUTRuntime = true;
|
119
|
+
}
|
120
|
+
return isUTRuntime;
|
121
|
+
};
|
122
|
+
|
123
|
+
/*
|
124
|
+
* @Description: framework helper
|
125
|
+
* @Usage:
|
126
|
+
* @Author: richen
|
127
|
+
* @Date: 2023-12-09 21:56:32
|
128
|
+
* @LastEditTime: 2024-01-16 01:20:17
|
129
|
+
* @License: BSD (3-Clause)
|
130
|
+
* @Copyright (c): <richenlin(at)gmail.com>
|
196
131
|
*/
|
197
|
-
function Service(identifier) {
|
198
|
-
return (target) => {
|
199
|
-
identifier = identifier || koatty_container.IOCContainer.getIdentifier(target);
|
200
|
-
koatty_container.IOCContainer.saveClass("SERVICE", target, identifier);
|
201
|
-
};
|
202
|
-
}
|
203
132
|
/**
|
204
|
-
*
|
133
|
+
* Check class file
|
134
|
+
* name should be always the same as class name
|
135
|
+
* class must be unique
|
205
136
|
*
|
206
137
|
* @export
|
207
|
-
* @param {string}
|
208
|
-
* @
|
138
|
+
* @param {string} fileName
|
139
|
+
* @param {string} xpath
|
140
|
+
* @param {*} target
|
141
|
+
* @param {Set<unknown>} [exSet]
|
142
|
+
* @returns {*}
|
209
143
|
*/
|
210
|
-
function
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
144
|
+
function checkClass(fileName, xpath, target, exSet) {
|
145
|
+
if (koatty_lib.Helper.isClass(target) && target.name != fileName) { // export default class name{}
|
146
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
147
|
+
}
|
148
|
+
if (target["__esModule"]) {
|
149
|
+
if (target.name === undefined) { // export class name{}
|
150
|
+
const keys = Object.keys(target);
|
151
|
+
if (keys[0] != fileName && koatty_lib.Helper.isClass(target[keys[0]])) {
|
152
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
153
|
+
}
|
216
154
|
}
|
217
|
-
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
* check is implements Controller Interface
|
230
|
-
* @param cls
|
231
|
-
* @returns
|
232
|
-
*/
|
233
|
-
function implementsControllerInterface(cls) {
|
234
|
-
return 'app' in cls && 'ctx' in cls;
|
235
|
-
}
|
236
|
-
/**
|
237
|
-
* check is implements Service Interface
|
238
|
-
* @param cls
|
239
|
-
* @returns
|
240
|
-
*/
|
241
|
-
function implementsServiceInterface(cls) {
|
242
|
-
return 'app' in cls;
|
243
|
-
}
|
244
|
-
/**
|
245
|
-
* check is implements Plugin Interface
|
246
|
-
* @param cls
|
247
|
-
* @returns
|
248
|
-
*/
|
249
|
-
function implementsPluginInterface(cls) {
|
250
|
-
return 'run' in cls && koatty_lib.Helper.isFunction(cls.run);
|
251
|
-
}
|
252
|
-
/**
|
253
|
-
* check is implements Aspect Interface
|
254
|
-
* @param cls
|
255
|
-
* @returns
|
256
|
-
*/
|
257
|
-
function implementsAspectInterface(cls) {
|
258
|
-
return 'app' in cls && 'run' in cls && koatty_lib.Helper.isFunction(cls.run);
|
155
|
+
else if (target.name != fileName) { // export default class {}
|
156
|
+
throw Error(`The file(${xpath}) name should be always the same as class name.`);
|
157
|
+
}
|
158
|
+
}
|
159
|
+
if (!exSet) {
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
if (exSet.has(fileName)) {
|
163
|
+
throw new Error(`A same class already exists. at \`${xpath}\`.`);
|
164
|
+
}
|
165
|
+
exSet.add(fileName);
|
166
|
+
return;
|
259
167
|
}
|
260
168
|
|
261
169
|
/*
|
@@ -270,13 +178,13 @@ function implementsAspectInterface(cls) {
|
|
270
178
|
const COMPONENT_SCAN = 'COMPONENT_SCAN';
|
271
179
|
const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
|
272
180
|
// tslint:disable: no-irregular-whitespace
|
273
|
-
const LOGO = `
|
274
|
-
|
275
|
-
┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
|
276
|
-
├┴┐│ │├─┤ │ │ └┬┘
|
277
|
-
┴ ┴└─┘┴ ┴ ┴ ┴ ┴
|
278
|
-
-------------------------------------------
|
279
|
-
https://github.com/koatty
|
181
|
+
const LOGO = `
|
182
|
+
|
183
|
+
┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
|
184
|
+
├┴┐│ │├─┤ │ │ └┬┘
|
185
|
+
┴ ┴└─┘┴ ┴ ┴ ┴ ┴
|
186
|
+
-------------------------------------------
|
187
|
+
https://github.com/koatty
|
280
188
|
`;
|
281
189
|
|
282
190
|
/*
|
@@ -284,7 +192,7 @@ https://github.com/koatty
|
|
284
192
|
* @Usage:
|
285
193
|
* @Author: richen
|
286
194
|
* @Date: 2023-12-09 22:55:49
|
287
|
-
* @LastEditTime: 2024-
|
195
|
+
* @LastEditTime: 2024-11-29 18:20:23
|
288
196
|
* @License: BSD (3-Clause)
|
289
197
|
* @Copyright (c): <richenlin(at)gmail.com>
|
290
198
|
*/
|
@@ -292,21 +200,30 @@ https://github.com/koatty
|
|
292
200
|
*
|
293
201
|
*/
|
294
202
|
class Loader {
|
203
|
+
/**
|
204
|
+
* Creates an instance of Loader.
|
205
|
+
* @param {KoattyApplication} app
|
206
|
+
* @memberof Loader
|
207
|
+
*/
|
208
|
+
constructor(app) {
|
209
|
+
this.app = app;
|
210
|
+
}
|
295
211
|
/**
|
296
212
|
* initialize env
|
297
213
|
*
|
298
214
|
* @static
|
299
|
-
* @param {
|
215
|
+
* @param {KoattyApplication} app
|
300
216
|
* @memberof Loader
|
301
217
|
*/
|
302
218
|
static initialize(app) {
|
303
|
-
|
219
|
+
var _a, _b;
|
220
|
+
const env = ((_a = process.execArgv) !== null && _a !== void 0 ? _a : []).join(",");
|
304
221
|
if (env.indexOf('ts-node') > -1 || env.indexOf('--debug') > -1) {
|
305
222
|
app.appDebug = true;
|
306
223
|
}
|
307
224
|
// app.env
|
308
225
|
app.env = process.env.KOATTY_ENV || process.env.NODE_ENV;
|
309
|
-
if ((env.indexOf('--production') > -1) || ((app.env
|
226
|
+
if ((env.indexOf('--production') > -1) || (((_b = app.env) !== null && _b !== void 0 ? _b : '').indexOf('pro') > -1)) {
|
310
227
|
app.appDebug = false;
|
311
228
|
}
|
312
229
|
if (app.appDebug) {
|
@@ -322,7 +239,7 @@ class Loader {
|
|
322
239
|
}
|
323
240
|
// define path
|
324
241
|
const rootPath = app.rootPath || process.cwd();
|
325
|
-
const appPath = app.appPath || path__namespace.resolve(rootPath,
|
242
|
+
const appPath = app.appPath || path__namespace.resolve(rootPath, app.appDebug ? 'src' : 'dist');
|
326
243
|
const koattyPath = path__namespace.resolve(__dirname, '..');
|
327
244
|
koatty_lib.Helper.define(app, 'rootPath', rootPath);
|
328
245
|
koatty_lib.Helper.define(app, 'appPath', appPath);
|
@@ -339,7 +256,6 @@ class Loader {
|
|
339
256
|
process.env.APP_PATH = appPath;
|
340
257
|
process.env.KOATTY_PATH = koattyPath;
|
341
258
|
// Compatible with old version, will be deprecated
|
342
|
-
koatty_lib.Helper.define(app, 'prevent', koatty_exception.prevent);
|
343
259
|
koatty_lib.Helper.define(app, 'thinkPath', koattyPath);
|
344
260
|
process.env.THINK_PATH = koattyPath;
|
345
261
|
}
|
@@ -347,14 +263,14 @@ class Loader {
|
|
347
263
|
* Get component metadata
|
348
264
|
*
|
349
265
|
* @static
|
350
|
-
* @param {
|
266
|
+
* @param {KoattyApplication} app
|
351
267
|
* @param {*} target
|
352
268
|
* @returns {*} {any[]}
|
353
269
|
* @memberof Loader
|
354
270
|
*/
|
355
|
-
static
|
271
|
+
static GetComponentMeta(app, target) {
|
356
272
|
let componentMetas = [];
|
357
|
-
const componentMeta = koatty_container.
|
273
|
+
const componentMeta = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, COMPONENT_SCAN, target);
|
358
274
|
if (componentMeta) {
|
359
275
|
if (koatty_lib.Helper.isArray(componentMeta)) {
|
360
276
|
componentMetas = componentMeta;
|
@@ -368,36 +284,17 @@ class Loader {
|
|
368
284
|
}
|
369
285
|
return componentMetas;
|
370
286
|
}
|
371
|
-
/**
|
372
|
-
* Load all bean, excepted config/*、App.ts
|
373
|
-
*
|
374
|
-
* @static
|
375
|
-
* @param {Koatty} app
|
376
|
-
* @param {*} target
|
377
|
-
* @memberof Loader
|
378
|
-
*/
|
379
|
-
static CheckAllComponents(app, target) {
|
380
|
-
// component metadata
|
381
|
-
const componentMetas = Loader.GetComponentMetas(app, target);
|
382
|
-
// configuration metadata
|
383
|
-
const configurationMetas = Loader.GetConfigurationMetas(app, target);
|
384
|
-
const exSet = new Set();
|
385
|
-
koatty_loader.Load(componentMetas, '', (fileName, xpath, xTarget) => {
|
386
|
-
checkClass(fileName, xpath, xTarget, exSet);
|
387
|
-
}, ['**/**.js', '**/**.ts', '!**/**.d.ts'], [...configurationMetas, `${target.name || '.no'}.ts`]);
|
388
|
-
exSet.clear();
|
389
|
-
}
|
390
287
|
/**
|
391
288
|
* Get configuration metadata
|
392
289
|
*
|
393
290
|
* @static
|
394
|
-
* @param {
|
291
|
+
* @param {KoattyApplication} app
|
395
292
|
* @param {*} target
|
396
293
|
* @returns {*} {any[]}
|
397
294
|
* @memberof Loader
|
398
295
|
*/
|
399
|
-
static
|
400
|
-
const confMeta = koatty_container.
|
296
|
+
static GetConfigurationMeta(app, target) {
|
297
|
+
const confMeta = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, CONFIGURATION_SCAN, target);
|
401
298
|
let configurationMetas = [];
|
402
299
|
if (confMeta) {
|
403
300
|
if (koatty_lib.Helper.isArray(confMeta)) {
|
@@ -409,11 +306,30 @@ class Loader {
|
|
409
306
|
}
|
410
307
|
return configurationMetas;
|
411
308
|
}
|
309
|
+
/**
|
310
|
+
* Load all bean, excepted config/*、App.ts
|
311
|
+
*
|
312
|
+
* @static
|
313
|
+
* @param {KoattyApplication} app
|
314
|
+
* @param {*} target
|
315
|
+
* @memberof Loader
|
316
|
+
*/
|
317
|
+
static CheckAllComponents(app, target) {
|
318
|
+
// component metadata
|
319
|
+
const componentMetas = Loader.GetComponentMeta(app, target);
|
320
|
+
// configuration metadata
|
321
|
+
const configurationMetas = Loader.GetConfigurationMeta(app, target);
|
322
|
+
const exSet = new Set();
|
323
|
+
koatty_loader.Load(componentMetas, '', (fileName, xpath, xTarget) => {
|
324
|
+
checkClass(fileName, xpath, xTarget, exSet);
|
325
|
+
}, ['**/**.js', '**/**.ts', '!**/**.d.ts'], [...configurationMetas, `${target.name || '.no'}.ts`]);
|
326
|
+
exSet.clear();
|
327
|
+
}
|
412
328
|
/**
|
413
329
|
* Set Logger level
|
414
330
|
*
|
415
331
|
* @static
|
416
|
-
* @param {
|
332
|
+
* @param {KoattyApplication} app
|
417
333
|
* @memberof Loader
|
418
334
|
*/
|
419
335
|
static SetLogger(app) {
|
@@ -442,7 +358,7 @@ class Loader {
|
|
442
358
|
* Load app event hook funcs
|
443
359
|
*
|
444
360
|
* @static
|
445
|
-
* @param {
|
361
|
+
* @param {KoattyApplication} app
|
446
362
|
* @param {*} target
|
447
363
|
* @memberof Loader
|
448
364
|
*/
|
@@ -452,25 +368,25 @@ class Loader {
|
|
452
368
|
let funcs;
|
453
369
|
switch (event) {
|
454
370
|
case "appBoot" /* AppEvent.appBoot */:
|
455
|
-
funcs = koatty_container.
|
371
|
+
funcs = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, "appBoot" /* AppEvent.appBoot */, target);
|
456
372
|
if (koatty_lib.Helper.isArray(funcs)) {
|
457
373
|
eventFuncs.set("appBoot" /* AppEvent.appBoot */, funcs);
|
458
374
|
}
|
459
375
|
break;
|
460
376
|
case "appReady" /* AppEvent.appReady */:
|
461
|
-
funcs = koatty_container.
|
377
|
+
funcs = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, "appReady" /* AppEvent.appReady */, target);
|
462
378
|
if (koatty_lib.Helper.isArray(funcs)) {
|
463
379
|
eventFuncs.set("appReady" /* AppEvent.appReady */, funcs);
|
464
380
|
}
|
465
381
|
break;
|
466
382
|
case "appStart" /* AppEvent.appStart */:
|
467
|
-
funcs = koatty_container.
|
383
|
+
funcs = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, "appStart" /* AppEvent.appStart */, target);
|
468
384
|
if (koatty_lib.Helper.isArray(funcs)) {
|
469
385
|
eventFuncs.set("appStart" /* AppEvent.appStart */, funcs);
|
470
386
|
}
|
471
387
|
break;
|
472
388
|
case "appStop" /* AppEvent.appStop */:
|
473
|
-
funcs = koatty_container.
|
389
|
+
funcs = koatty_container.IOC.getClassMetadata(koatty_container.TAGGED_CLS, "appStop" /* AppEvent.appStop */, target);
|
474
390
|
if (koatty_lib.Helper.isArray(funcs)) {
|
475
391
|
eventFuncs.set("appStop" /* AppEvent.appStop */, funcs);
|
476
392
|
}
|
@@ -484,39 +400,70 @@ class Loader {
|
|
484
400
|
}
|
485
401
|
}
|
486
402
|
}
|
403
|
+
/**
|
404
|
+
* LoadAllComponents
|
405
|
+
*/
|
406
|
+
static async LoadAllComponents(app, target) {
|
407
|
+
var _a;
|
408
|
+
// Load configuration
|
409
|
+
Logger.Log('Koatty', '', 'Load Configurations ...');
|
410
|
+
// configuration metadata
|
411
|
+
const configurationMeta = Loader.GetConfigurationMeta(app, target);
|
412
|
+
const loader = new Loader(app);
|
413
|
+
loader.LoadConfigs(configurationMeta);
|
414
|
+
// Create Server
|
415
|
+
const serveOpts = {
|
416
|
+
hostname: app.config('app_host'),
|
417
|
+
port: app.config('app_port'),
|
418
|
+
protocol: app.config('protocol'),
|
419
|
+
};
|
420
|
+
koatty_lib.Helper.define(app, "server", koatty_serve.NewServe(app, serveOpts));
|
421
|
+
// Create router
|
422
|
+
const routerOpts = (_a = app.config(undefined, 'router')) !== null && _a !== void 0 ? _a : {};
|
423
|
+
koatty_lib.Helper.define(app, "router", koatty_router.NewRouter(app, routerOpts));
|
424
|
+
// Load Components
|
425
|
+
Logger.Log('Koatty', '', 'Load Components ...');
|
426
|
+
await loader.LoadComponents();
|
427
|
+
// Load Middleware
|
428
|
+
Logger.Log('Koatty', '', 'Load Middlewares ...');
|
429
|
+
await loader.LoadMiddlewares();
|
430
|
+
// Load Services
|
431
|
+
Logger.Log('Koatty', '', 'Load Services ...');
|
432
|
+
await loader.LoadServices();
|
433
|
+
// Load Controllers
|
434
|
+
Logger.Log('Koatty', '', 'Load Controllers ...');
|
435
|
+
const controllers = await loader.LoadControllers();
|
436
|
+
// Load Routers
|
437
|
+
Logger.Log('Koatty', '', 'Load Routers ...');
|
438
|
+
loader.LoadRouter(controllers);
|
439
|
+
}
|
487
440
|
/**
|
488
441
|
* Load configuration
|
489
442
|
*
|
490
|
-
* @static
|
491
|
-
* @param {Koatty} app
|
492
443
|
* @param {string[]} [loadPath]
|
493
444
|
* @memberof Loader
|
494
445
|
*/
|
495
|
-
|
446
|
+
LoadConfigs(loadPath) {
|
496
447
|
const frameConfig = {};
|
497
448
|
// Logger.Debug(`Load configuration path: ${app.thinkPath}/config`);
|
498
|
-
koatty_loader.Load(["./config"], app.koattyPath, function (name, path, exp) {
|
449
|
+
koatty_loader.Load(["./config"], this.app.koattyPath, function (name, path, exp) {
|
499
450
|
frameConfig[name] = exp;
|
500
451
|
});
|
501
452
|
if (koatty_lib.Helper.isArray(loadPath)) {
|
502
453
|
loadPath = loadPath.length > 0 ? loadPath : ["./config"];
|
503
454
|
}
|
504
|
-
let appConfig = koatty_config.LoadConfigs(loadPath, app.appPath);
|
455
|
+
let appConfig = koatty_config.LoadConfigs(loadPath, this.app.appPath);
|
505
456
|
appConfig = koatty_lib.Helper.extend(frameConfig, appConfig, true);
|
506
|
-
app.setMetaData("_configs", appConfig);
|
457
|
+
this.app.setMetaData("_configs", appConfig);
|
507
458
|
}
|
508
459
|
/**
|
509
460
|
* Load middlewares
|
510
461
|
* [async]
|
511
|
-
* @static
|
512
|
-
* @param {*} app
|
513
|
-
* @param {(string | string[])} [loadPath]
|
514
462
|
* @memberof Loader
|
515
463
|
*/
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
let middlewareConf = app.config(undefined, "middleware");
|
464
|
+
async LoadMiddlewares() {
|
465
|
+
var _a;
|
466
|
+
let middlewareConf = this.app.config(undefined, "middleware");
|
520
467
|
if (koatty_lib.Helper.isEmpty(middlewareConf)) {
|
521
468
|
middlewareConf = { config: {}, list: [] };
|
522
469
|
}
|
@@ -524,13 +471,14 @@ class Loader {
|
|
524
471
|
// Load(loadPath || ["./middleware"], app.koattyPath);
|
525
472
|
//Mount application middleware
|
526
473
|
// const middleware: any = {};
|
527
|
-
const appMiddleware = koatty_container.
|
474
|
+
const appMiddleware = (_a = koatty_container.IOC.listClass("MIDDLEWARE")) !== null && _a !== void 0 ? _a : [];
|
528
475
|
appMiddleware.forEach((item) => {
|
529
|
-
|
476
|
+
var _a;
|
477
|
+
item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("MIDDLEWARE:", "");
|
530
478
|
if (item.id && koatty_lib.Helper.isClass(item.target)) {
|
531
|
-
koatty_container.
|
532
|
-
const ctl = koatty_container.
|
533
|
-
if (!implementsMiddlewareInterface(ctl)) {
|
479
|
+
koatty_container.IOC.reg(item.id, item.target, { scope: "Prototype", type: "MIDDLEWARE", args: [] });
|
480
|
+
const ctl = koatty_container.IOC.getInsByClass(item.target);
|
481
|
+
if (!koatty_core.implementsMiddlewareInterface(ctl)) {
|
534
482
|
throw Error(`The middleware ${item.id} must implements interface 'IMiddleware'.`);
|
535
483
|
}
|
536
484
|
}
|
@@ -544,7 +492,7 @@ class Loader {
|
|
544
492
|
//Automatically call middleware
|
545
493
|
const middlewareConfig = middlewareConf.config || {};
|
546
494
|
for (const key of appMList) {
|
547
|
-
const handle = koatty_container.
|
495
|
+
const handle = koatty_container.IOC.get(key, "MIDDLEWARE");
|
548
496
|
if (!handle) {
|
549
497
|
throw Error(`Middleware ${key} load error.`);
|
550
498
|
}
|
@@ -556,13 +504,13 @@ class Loader {
|
|
556
504
|
continue;
|
557
505
|
}
|
558
506
|
Logger.Debug(`Load middleware: ${key}`);
|
559
|
-
const result = await handle.run(middlewareConfig[key] || {}, app);
|
507
|
+
const result = await handle.run(middlewareConfig[key] || {}, this.app);
|
560
508
|
if (koatty_lib.Helper.isFunction(result)) {
|
561
509
|
if (result.length < 3) {
|
562
|
-
app.use(result);
|
510
|
+
this.app.use(result);
|
563
511
|
}
|
564
512
|
else {
|
565
|
-
app.useExp(result);
|
513
|
+
this.app.useExp(result);
|
566
514
|
}
|
567
515
|
}
|
568
516
|
}
|
@@ -570,21 +518,20 @@ class Loader {
|
|
570
518
|
/**
|
571
519
|
* Load controllers
|
572
520
|
*
|
573
|
-
* @static
|
574
|
-
* @param {*} app
|
575
521
|
* @memberof Loader
|
576
522
|
*/
|
577
|
-
|
578
|
-
const controllerList = koatty_container.
|
523
|
+
async LoadControllers() {
|
524
|
+
const controllerList = koatty_container.IOC.listClass("CONTROLLER");
|
579
525
|
const controllers = [];
|
580
526
|
controllerList.forEach((item) => {
|
581
|
-
|
527
|
+
var _a;
|
528
|
+
item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("CONTROLLER:", "");
|
582
529
|
if (item.id && koatty_lib.Helper.isClass(item.target)) {
|
583
530
|
Logger.Debug(`Load controller: ${item.id}`);
|
584
531
|
// registering to IOC
|
585
|
-
koatty_container.
|
586
|
-
const ctl = koatty_container.
|
587
|
-
if (!implementsControllerInterface(ctl)) {
|
532
|
+
koatty_container.IOC.reg(item.id, item.target, { scope: "Prototype", type: "CONTROLLER", args: [] });
|
533
|
+
const ctl = koatty_container.IOC.getInsByClass(item.target);
|
534
|
+
if (!koatty_core.implementsControllerInterface(ctl)) {
|
588
535
|
throw Error(`The controller ${item.id} must implements interface 'IController'.`);
|
589
536
|
}
|
590
537
|
controllers.push(item.id);
|
@@ -595,20 +542,19 @@ class Loader {
|
|
595
542
|
/**
|
596
543
|
* Load services
|
597
544
|
*
|
598
|
-
* @static
|
599
|
-
* @param {*} app
|
600
545
|
* @memberof Loader
|
601
546
|
*/
|
602
|
-
|
603
|
-
const serviceList = koatty_container.
|
547
|
+
async LoadServices() {
|
548
|
+
const serviceList = koatty_container.IOC.listClass("SERVICE");
|
604
549
|
serviceList.forEach((item) => {
|
605
|
-
|
550
|
+
var _a;
|
551
|
+
item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("SERVICE:", "");
|
606
552
|
if (item.id && koatty_lib.Helper.isClass(item.target)) {
|
607
553
|
Logger.Debug(`Load service: ${item.id}`);
|
608
554
|
// registering to IOC
|
609
|
-
koatty_container.
|
610
|
-
const ctl = koatty_container.
|
611
|
-
if (!implementsServiceInterface(ctl)) {
|
555
|
+
koatty_container.IOC.reg(item.id, item.target, { scope: "Singleton", type: "SERVICE", args: [] });
|
556
|
+
const ctl = koatty_container.IOC.getInsByClass(item.target);
|
557
|
+
if (!koatty_core.implementsServiceInterface(ctl)) {
|
612
558
|
throw Error(`The service ${item.id} must implements interface 'IService'.`);
|
613
559
|
}
|
614
560
|
}
|
@@ -617,42 +563,42 @@ class Loader {
|
|
617
563
|
/**
|
618
564
|
* Load components
|
619
565
|
*
|
620
|
-
* @static
|
621
|
-
* @param {*} app
|
622
566
|
* @memberof Loader
|
623
567
|
*/
|
624
|
-
|
625
|
-
|
568
|
+
async LoadComponents() {
|
569
|
+
var _a, _b;
|
570
|
+
const componentList = koatty_container.IOC.listClass("COMPONENT");
|
626
571
|
const pluginList = [];
|
627
572
|
componentList.forEach(async (item) => {
|
628
|
-
|
573
|
+
var _a;
|
574
|
+
item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("COMPONENT:", "");
|
629
575
|
if (koatty_lib.Helper.isClass(item.target)) {
|
630
576
|
// registering to IOC
|
631
|
-
koatty_container.
|
577
|
+
koatty_container.IOC.reg(item.id, item.target, { scope: "Singleton", type: "COMPONENT", args: [] });
|
632
578
|
if (item.id && (item.id).endsWith("Plugin")) {
|
633
|
-
const ctl = koatty_container.
|
634
|
-
if (!implementsPluginInterface(ctl)) {
|
579
|
+
const ctl = koatty_container.IOC.getInsByClass(item.target);
|
580
|
+
if (!koatty_core.implementsPluginInterface(ctl)) {
|
635
581
|
throw Error(`The plugin ${item.id} must implements interface 'IPlugin'.`);
|
636
582
|
}
|
637
583
|
pluginList.push(item.id);
|
638
584
|
}
|
639
585
|
if (item.id && (item.id).endsWith("Aspect")) {
|
640
|
-
const ctl = koatty_container.
|
641
|
-
if (!implementsAspectInterface(ctl)) {
|
586
|
+
const ctl = koatty_container.IOC.getInsByClass(item.target);
|
587
|
+
if (!koatty_core.implementsAspectInterface(ctl)) {
|
642
588
|
throw Error(`The aspect ${item.id} must implements interface 'IAspect'.`);
|
643
589
|
}
|
644
590
|
}
|
645
591
|
}
|
646
592
|
});
|
647
593
|
// load plugin config
|
648
|
-
let pluginsConf = app.config(undefined, "plugin");
|
594
|
+
let pluginsConf = this.app.config(undefined, "plugin");
|
649
595
|
if (koatty_lib.Helper.isEmpty(pluginsConf)) {
|
650
596
|
pluginsConf = { config: {}, list: [] };
|
651
597
|
}
|
652
|
-
const pluginConfList = pluginsConf.list
|
598
|
+
const pluginConfList = (_a = pluginsConf.list) !== null && _a !== void 0 ? _a : [];
|
653
599
|
// load plugin list
|
654
600
|
for (const key of pluginConfList) {
|
655
|
-
const handle = koatty_container.
|
601
|
+
const handle = koatty_container.IOC.get(key, "COMPONENT");
|
656
602
|
if (!handle) {
|
657
603
|
throw Error(`Plugin ${key} load error.`);
|
658
604
|
}
|
@@ -664,68 +610,110 @@ class Loader {
|
|
664
610
|
continue;
|
665
611
|
}
|
666
612
|
// sync exec
|
667
|
-
await handle.run(pluginsConf.config[key]
|
613
|
+
await handle.run((_b = pluginsConf.config[key]) !== null && _b !== void 0 ? _b : {}, this.app);
|
668
614
|
}
|
669
615
|
}
|
616
|
+
/**
|
617
|
+
* @description: load router
|
618
|
+
* @param {string} ctls
|
619
|
+
* @return {*}
|
620
|
+
*/
|
621
|
+
async LoadRouter(ctls) {
|
622
|
+
// load router
|
623
|
+
await this.app.router.LoadRouter(this.app, ctls);
|
624
|
+
}
|
670
625
|
}
|
671
626
|
|
672
|
-
var version = "3.11.4-2";
|
673
|
-
var engines = {
|
674
|
-
node: ">12.0.0"
|
675
|
-
};
|
676
|
-
|
677
627
|
/*
|
678
|
-
* @Description: framework
|
628
|
+
* @Description: framework bootstrap
|
679
629
|
* @Usage:
|
680
630
|
* @Author: richen
|
681
|
-
* @Date:
|
682
|
-
* @LastEditTime:
|
631
|
+
* @Date: 2021-12-09 21:56:32
|
632
|
+
* @LastEditTime: 2024-11-29 18:13:06
|
683
633
|
* @License: BSD (3-Clause)
|
684
634
|
* @Copyright (c): <richenlin(at)gmail.com>
|
685
635
|
*/
|
686
|
-
const KOATTY_VERSION = version;
|
687
|
-
const ENGINES_VERSION = engines.node.slice(1) || '12.0.0';
|
688
636
|
/**
|
689
|
-
*
|
690
|
-
*
|
637
|
+
* Bootstrap application decorator
|
638
|
+
*
|
639
|
+
* @export
|
640
|
+
* @param {Function} [bootFunc]
|
641
|
+
* @returns {ClassDecorator}
|
691
642
|
*/
|
692
|
-
function
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
}
|
699
|
-
nodeVersion = nodeVersion.slice(0, nodeVersion.lastIndexOf('.'));
|
700
|
-
if (koatty_lib.Helper.toNumber(nodeEngines) > koatty_lib.Helper.toNumber(nodeVersion)) {
|
701
|
-
Logger.Error(`Koatty need node version > ${nodeEngines}, current version is ${nodeVersion}, please upgrade it.`);
|
702
|
-
process.exit(-1);
|
703
|
-
}
|
643
|
+
function Bootstrap(bootFunc) {
|
644
|
+
return function (target) {
|
645
|
+
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
646
|
+
throw new Error(`class does not inherit from Koatty`);
|
647
|
+
}
|
648
|
+
executeBootstrap(target, bootFunc);
|
649
|
+
};
|
704
650
|
}
|
705
651
|
/**
|
706
|
-
*
|
707
|
-
*
|
708
|
-
* @
|
652
|
+
* Actively perform dependency injection
|
653
|
+
* Parse the decorator, return the instantiated app.
|
654
|
+
* @export ExecBootStrap
|
655
|
+
* @param {Function} [bootFunc] callback function
|
656
|
+
* @returns
|
709
657
|
*/
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
}
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
* @
|
722
|
-
* @
|
723
|
-
* @
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
658
|
+
function ExecBootStrap(bootFunc) {
|
659
|
+
return async (target) => {
|
660
|
+
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
661
|
+
throw new Error(`class ${target.name} does not inherit from Koatty`);
|
662
|
+
}
|
663
|
+
return await executeBootstrap(target, bootFunc, true);
|
664
|
+
};
|
665
|
+
}
|
666
|
+
/**
|
667
|
+
* Define project scan path
|
668
|
+
*
|
669
|
+
* @export
|
670
|
+
* @param {(string | string[])} [scanPath]
|
671
|
+
* @returns {ClassDecorator}
|
672
|
+
*/
|
673
|
+
function ComponentScan(scanPath) {
|
674
|
+
return (target) => {
|
675
|
+
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
676
|
+
throw new Error(`class does not inherit from Koatty`);
|
677
|
+
}
|
678
|
+
scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
|
679
|
+
koatty_container.IOC.saveClassMetadata(koatty_container.TAGGED_CLS, COMPONENT_SCAN, scanPath, target);
|
680
|
+
};
|
681
|
+
}
|
682
|
+
/**
|
683
|
+
* Define project configuration scan path
|
684
|
+
*
|
685
|
+
* @export
|
686
|
+
* @param {(string | string[])} [scanPath]
|
687
|
+
* @returns {ClassDecorator}
|
728
688
|
*/
|
689
|
+
function ConfigurationScan(scanPath) {
|
690
|
+
return (target) => {
|
691
|
+
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
692
|
+
throw new Error(`class does not inherit from Koatty`);
|
693
|
+
}
|
694
|
+
scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
|
695
|
+
koatty_container.IOC.saveClassMetadata(koatty_container.TAGGED_CLS, CONFIGURATION_SCAN, scanPath, target);
|
696
|
+
};
|
697
|
+
}
|
698
|
+
/**
|
699
|
+
* @description: bind App event hook func
|
700
|
+
* example:
|
701
|
+
* export function TestDecorator(): ClassDecorator {
|
702
|
+
* return (target: Function) => {
|
703
|
+
* BindEventHook(AppEvent.appBoot, (app: KoattyApplication) => {
|
704
|
+
* // todo
|
705
|
+
* return Promise.resolve();
|
706
|
+
* }, target)
|
707
|
+
* }
|
708
|
+
* }
|
709
|
+
* @param {AppEvent} eventName
|
710
|
+
* @param {EventHookFunc} eventFunc
|
711
|
+
* @param {any} target
|
712
|
+
* @return {*}
|
713
|
+
*/
|
714
|
+
function BindEventHook(eventName, eventFunc, target) {
|
715
|
+
koatty_container.IOC.attachClassMetadata(koatty_container.TAGGED_CLS, eventName, eventFunc, target);
|
716
|
+
}
|
729
717
|
/**
|
730
718
|
* execute bootstrap
|
731
719
|
*
|
@@ -750,7 +738,8 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
750
738
|
Logger.enable(false);
|
751
739
|
}
|
752
740
|
try {
|
753
|
-
!app.silent
|
741
|
+
if (!app.silent)
|
742
|
+
Logger.Log("Koatty", LOGO);
|
754
743
|
if (!(app instanceof koatty_core.Koatty)) {
|
755
744
|
throw new Error(`class ${target.name} does not inherit from Koatty`);
|
756
745
|
}
|
@@ -761,44 +750,20 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
761
750
|
Logger.Log('Koatty', '', 'Execute bootFunc ...');
|
762
751
|
await bootFunc(app);
|
763
752
|
}
|
764
|
-
// Set
|
765
|
-
koatty_container.
|
766
|
-
Logger.Log('Koatty', '', 'ComponentScan ...');
|
753
|
+
// Set IOC.app
|
754
|
+
koatty_container.IOC.setApp(app);
|
767
755
|
// Check all bean
|
756
|
+
Logger.Log('Koatty', '', 'ComponentScan ...');
|
768
757
|
Loader.CheckAllComponents(app, target);
|
769
|
-
// Load configuration
|
770
|
-
Logger.Log('Koatty', '', 'Load Configurations ...');
|
771
|
-
// configuration metadata
|
772
|
-
const configurationMetas = Loader.GetConfigurationMetas(app, target);
|
773
|
-
Loader.LoadConfigs(app, configurationMetas);
|
774
758
|
// Load App event hooks
|
775
759
|
Loader.LoadAppEventHooks(app, target);
|
776
760
|
Logger.Log('Koatty', '', 'Emit App Boot ...');
|
777
761
|
await asyncEvent(app, "appBoot" /* AppEvent.appBoot */);
|
778
|
-
//
|
779
|
-
|
780
|
-
koatty_lib.Helper.define(app, "server", koatty_serve.NewServe(app));
|
781
|
-
// Create router
|
782
|
-
// app.router = newRouter(app);
|
783
|
-
koatty_lib.Helper.define(app, "router", koatty_router.NewRouter(app));
|
784
|
-
// Load Components
|
785
|
-
Logger.Log('Koatty', '', 'Load Components ...');
|
786
|
-
await Loader.LoadComponents(app);
|
787
|
-
// Load Middleware
|
788
|
-
Logger.Log('Koatty', '', 'Load Middlewares ...');
|
789
|
-
await Loader.LoadMiddlewares(app);
|
790
|
-
// Load Services
|
791
|
-
Logger.Log('Koatty', '', 'Load Services ...');
|
792
|
-
Loader.LoadServices(app);
|
793
|
-
// Load Controllers
|
794
|
-
Logger.Log('Koatty', '', 'Load Controllers ...');
|
795
|
-
const controllers = Loader.LoadControllers(app);
|
762
|
+
// Load All components
|
763
|
+
await Loader.LoadAllComponents(app, target);
|
796
764
|
// Emit app ready event
|
797
765
|
Logger.Log('Koatty', '', 'Emit App Ready ...');
|
798
766
|
await asyncEvent(app, "appReady" /* AppEvent.appReady */);
|
799
|
-
// Load Routers
|
800
|
-
Logger.Log('Koatty', '', 'Load Routers ...');
|
801
|
-
app.router.LoadRouter(app, controllers);
|
802
767
|
if (!isUTRuntime) {
|
803
768
|
// Start Server
|
804
769
|
app.listen(listenCallback);
|
@@ -817,19 +782,24 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
817
782
|
* @returns {*}
|
818
783
|
*/
|
819
784
|
const listenCallback = (app) => {
|
820
|
-
|
785
|
+
let servers = [];
|
786
|
+
if (!Array.isArray(app.server)) {
|
787
|
+
servers = [app.server];
|
788
|
+
}
|
789
|
+
else {
|
790
|
+
servers = app.server;
|
791
|
+
}
|
821
792
|
Logger.Log('Koatty', '', '====================================');
|
822
793
|
Logger.Log("Koatty", "", `Nodejs Version: ${process.version}`);
|
823
794
|
Logger.Log("Koatty", "", `Koatty Version: v${KOATTY_VERSION}`);
|
824
795
|
Logger.Log("Koatty", "", `App Environment: ${app.env}`);
|
825
|
-
|
826
|
-
|
796
|
+
servers.forEach(s => {
|
797
|
+
Logger.Log('Koatty', '', `Server: ${(s.options.protocol).toUpperCase()} running at ${s.options.protocol === "http2" ?
|
798
|
+
"https" : s.options.protocol}://${s.options.hostname || '127.0.0.1'}:${s.options.port}/`);
|
799
|
+
});
|
827
800
|
Logger.Log("Koatty", "", "====================================");
|
828
|
-
|
829
|
-
|
830
|
-
koatty_serve.BindProcessEvent(app, 'appStop');
|
831
|
-
// tslint:disable-next-line: no-unused-expression
|
832
|
-
app.appDebug && Logger.Warn(`Running in debug mode.`);
|
801
|
+
if (app.appDebug)
|
802
|
+
Logger.Warn(`Running in debug mode.`);
|
833
803
|
// Set Logger
|
834
804
|
Loader.SetLogger(app);
|
835
805
|
};
|
@@ -849,122 +819,21 @@ const asyncEvent = async function (event, eventName) {
|
|
849
819
|
}
|
850
820
|
return event.removeAllListeners(eventName);
|
851
821
|
};
|
852
|
-
/**
|
853
|
-
* Bootstrap application
|
854
|
-
*
|
855
|
-
* @export
|
856
|
-
* @param {Function} [bootFunc]
|
857
|
-
* @returns {ClassDecorator}
|
858
|
-
*/
|
859
|
-
function Bootstrap(bootFunc) {
|
860
|
-
return function (target) {
|
861
|
-
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
862
|
-
throw new Error(`class does not inherit from Koatty`);
|
863
|
-
}
|
864
|
-
executeBootstrap(target, bootFunc);
|
865
|
-
};
|
866
|
-
}
|
867
|
-
/**
|
868
|
-
* Actively perform dependency injection
|
869
|
-
* Parse the decorator, return the instantiated app.
|
870
|
-
* @export ExecBootStrap
|
871
|
-
* @param {Function} [bootFunc] callback function
|
872
|
-
* @returns
|
873
|
-
*/
|
874
|
-
function ExecBootStrap(bootFunc) {
|
875
|
-
return async (target) => {
|
876
|
-
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
877
|
-
throw new Error(`class ${target.name} does not inherit from Koatty`);
|
878
|
-
}
|
879
|
-
return await executeBootstrap(target, bootFunc, true);
|
880
|
-
};
|
881
|
-
}
|
882
|
-
/**
|
883
|
-
* Define project scan path
|
884
|
-
*
|
885
|
-
* @export
|
886
|
-
* @param {(string | string[])} [scanPath]
|
887
|
-
* @returns {ClassDecorator}
|
888
|
-
*/
|
889
|
-
function ComponentScan(scanPath) {
|
890
|
-
return (target) => {
|
891
|
-
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
892
|
-
throw new Error(`class does not inherit from Koatty`);
|
893
|
-
}
|
894
|
-
scanPath = scanPath ?? '';
|
895
|
-
koatty_container.IOCContainer.saveClassMetadata(koatty_container.TAGGED_CLS, COMPONENT_SCAN, scanPath, target);
|
896
|
-
};
|
897
|
-
}
|
898
|
-
/**
|
899
|
-
* Define project configuration scan path
|
900
|
-
*
|
901
|
-
* @export
|
902
|
-
* @param {(string | string[])} [scanPath]
|
903
|
-
* @returns {ClassDecorator}
|
904
|
-
*/
|
905
|
-
function ConfigurationScan(scanPath) {
|
906
|
-
return (target) => {
|
907
|
-
if (!(target.prototype instanceof koatty_core.Koatty)) {
|
908
|
-
throw new Error(`class does not inherit from Koatty`);
|
909
|
-
}
|
910
|
-
scanPath = scanPath ?? '';
|
911
|
-
koatty_container.IOCContainer.saveClassMetadata(koatty_container.TAGGED_CLS, CONFIGURATION_SCAN, scanPath, target);
|
912
|
-
};
|
913
|
-
}
|
914
|
-
/**
|
915
|
-
* @description: bind App event hook func
|
916
|
-
* example:
|
917
|
-
* export function TestDecorator(): ClassDecorator {
|
918
|
-
* return (target: Function) => {
|
919
|
-
* BindEventHook(AppEvent.appBoot, (app: Koatty) => {
|
920
|
-
* // todo
|
921
|
-
* return Promise.resolve();
|
922
|
-
* }, target)
|
923
|
-
* }
|
924
|
-
* }
|
925
|
-
* @param {AppEvent} eventName
|
926
|
-
* @param {EventHookFunc} eventFunc
|
927
|
-
* @param {any} target
|
928
|
-
* @return {*}
|
929
|
-
*/
|
930
|
-
function BindEventHook(eventName, eventFunc, target) {
|
931
|
-
koatty_container.IOCContainer.attachClassMetadata(koatty_container.TAGGED_CLS, eventName, eventFunc, target);
|
932
|
-
}
|
933
822
|
|
934
|
-
Object.defineProperty(exports,
|
823
|
+
Object.defineProperty(exports, "Config", {
|
935
824
|
enumerable: true,
|
936
825
|
get: function () { return koatty_config.Config; }
|
937
826
|
});
|
938
|
-
Object.defineProperty(exports,
|
939
|
-
enumerable: true,
|
940
|
-
get: function () { return koatty_config.Value; }
|
941
|
-
});
|
942
|
-
Object.defineProperty(exports, 'Helper', {
|
827
|
+
Object.defineProperty(exports, "Helper", {
|
943
828
|
enumerable: true,
|
944
829
|
get: function () { return koatty_lib.Helper; }
|
945
830
|
});
|
946
831
|
exports.BindEventHook = BindEventHook;
|
947
832
|
exports.Bootstrap = Bootstrap;
|
948
|
-
exports.Component = Component;
|
949
833
|
exports.ComponentScan = ComponentScan;
|
950
834
|
exports.ConfigurationScan = ConfigurationScan;
|
951
|
-
exports.Controller = Controller;
|
952
835
|
exports.ExecBootStrap = ExecBootStrap;
|
953
836
|
exports.Logger = Logger;
|
954
|
-
exports.Middleware = Middleware;
|
955
|
-
exports.Plugin = Plugin;
|
956
|
-
exports.Service = Service;
|
957
|
-
exports.implementsAspectInterface = implementsAspectInterface;
|
958
|
-
exports.implementsControllerInterface = implementsControllerInterface;
|
959
|
-
exports.implementsMiddlewareInterface = implementsMiddlewareInterface;
|
960
|
-
exports.implementsPluginInterface = implementsPluginInterface;
|
961
|
-
exports.implementsServiceInterface = implementsServiceInterface;
|
962
|
-
Object.keys(koatty_router).forEach(function (k) {
|
963
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
964
|
-
enumerable: true,
|
965
|
-
get: function () { return koatty_router[k]; }
|
966
|
-
});
|
967
|
-
});
|
968
837
|
Object.keys(koatty_container).forEach(function (k) {
|
969
838
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
970
839
|
enumerable: true,
|
@@ -977,15 +846,15 @@ Object.keys(koatty_core).forEach(function (k) {
|
|
977
846
|
get: function () { return koatty_core[k]; }
|
978
847
|
});
|
979
848
|
});
|
980
|
-
Object.keys(
|
849
|
+
Object.keys(koatty_exception).forEach(function (k) {
|
981
850
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
982
851
|
enumerable: true,
|
983
|
-
get: function () { return
|
852
|
+
get: function () { return koatty_exception[k]; }
|
984
853
|
});
|
985
854
|
});
|
986
|
-
Object.keys(
|
855
|
+
Object.keys(koatty_router).forEach(function (k) {
|
987
856
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
988
857
|
enumerable: true,
|
989
|
-
get: function () { return
|
858
|
+
get: function () { return koatty_router[k]; }
|
990
859
|
});
|
991
860
|
});
|