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