@midwayjs/core 3.0.0-alpha.36 → 3.0.0-alpha.37

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.
@@ -107,6 +107,11 @@ class BaseFramework {
107
107
  await this.initializeLogger(options);
108
108
  }
109
109
  async containerDirectoryLoad(options) {
110
+ if (options.preloadModules && options.preloadModules.length) {
111
+ for (const preloadModule of options.preloadModules) {
112
+ this.applicationContext.bindClass(preloadModule);
113
+ }
114
+ }
110
115
  // register app
111
116
  this.applicationContext.registerDataHandler(decorator_1.APPLICATION_KEY, (key, meta) => {
112
117
  var _a;
@@ -20,8 +20,10 @@ const globalDebugLogger = util.debuglog('midway:container');
20
20
  class ContainerConfiguration {
21
21
  constructor(container) {
22
22
  this.container = container;
23
+ this.loadedMap = new WeakMap();
23
24
  }
24
25
  load(module) {
26
+ let namespace = decorator_1.MAIN_MODULE_KEY;
25
27
  // 可能导出多个
26
28
  const configurationExports = this.getConfigurationExport(module);
27
29
  if (!configurationExports.length)
@@ -29,6 +31,10 @@ class ContainerConfiguration {
29
31
  // 多个的情况,数据交给第一个保存
30
32
  for (let i = 0; i < configurationExports.length; i++) {
31
33
  const configurationExport = configurationExports[i];
34
+ if (this.loadedMap.get(configurationExport)) {
35
+ // 已经加载过就跳过循环
36
+ continue;
37
+ }
32
38
  let configurationOptions;
33
39
  if (configurationExport instanceof configuration_1.FunctionalConfiguration) {
34
40
  // 函数式写法
@@ -38,20 +44,21 @@ class ContainerConfiguration {
38
44
  // 普通类写法
39
45
  configurationOptions = (0, decorator_1.getClassMetadata)(decorator_1.CONFIGURATION_KEY, configurationExport);
40
46
  }
47
+ // 已加载标记,防止死循环
48
+ this.loadedMap.set(configurationExport, true);
41
49
  debug(' configuration export %j.', configurationOptions);
42
50
  if (configurationOptions) {
43
- if (this.namespace !== decorator_1.MAIN_MODULE_KEY &&
44
- configurationOptions.namespace !== undefined) {
45
- this.namespace = configurationOptions.namespace;
51
+ if (configurationOptions.namespace !== undefined) {
52
+ namespace = configurationOptions.namespace;
46
53
  }
47
54
  this.addImports(configurationOptions.imports);
48
55
  this.addImportObjects(configurationOptions.importObjects);
49
56
  this.addImportConfigs(configurationOptions.importConfigs);
50
- this.bindConfigurationClass(configurationExport);
57
+ this.bindConfigurationClass(configurationExport, namespace);
51
58
  }
52
59
  }
53
60
  // bind module
54
- this.container.bindClass(module, this.namespace);
61
+ this.container.bindClass(module, namespace);
55
62
  }
56
63
  addImportConfigs(importConfigs) {
57
64
  if (importConfigs && importConfigs.length) {
@@ -68,21 +75,19 @@ class ContainerConfiguration {
68
75
  if (typeof importPackage === 'string') {
69
76
  importPackage = require(importPackage);
70
77
  }
71
- // for package
72
- const subContainerConfiguration = new ContainerConfiguration(this.container);
73
78
  if ('Configuration' in importPackage) {
74
79
  // component is object
75
80
  debug('\n---------- start load configuration from submodule" ----------');
76
- subContainerConfiguration.load(importPackage);
81
+ this.load(importPackage);
77
82
  debug(`---------- end load configuration from sub package "${importPackage}" ----------`);
78
83
  }
79
84
  else if ('component' in importPackage) {
80
85
  if ((_b = (_a = importPackage) === null || _a === void 0 ? void 0 : _a.enabledEnvironment) === null || _b === void 0 ? void 0 : _b.includes(this.container.getCurrentEnv())) {
81
- subContainerConfiguration.load(importPackage.component);
86
+ this.load(importPackage.component);
82
87
  }
83
88
  }
84
89
  else {
85
- subContainerConfiguration.load(importPackage);
90
+ this.load(importPackage);
86
91
  }
87
92
  }
88
93
  }
@@ -100,7 +105,7 @@ class ContainerConfiguration {
100
105
  }
101
106
  }
102
107
  }
103
- bindConfigurationClass(clzz, filePath) {
108
+ bindConfigurationClass(clzz, namespace) {
104
109
  if (clzz instanceof configuration_1.FunctionalConfiguration) {
105
110
  // 函数式写法不需要绑定到容器
106
111
  }
@@ -109,8 +114,7 @@ class ContainerConfiguration {
109
114
  (0, decorator_1.saveProviderId)(undefined, clzz);
110
115
  const id = (0, decorator_1.getProviderUUId)(clzz);
111
116
  this.container.bind(id, clzz, {
112
- namespace: this.namespace,
113
- srcPath: filePath,
117
+ namespace: namespace,
114
118
  scope: decorator_1.ScopeEnum.Singleton,
115
119
  });
116
120
  }
@@ -122,7 +126,7 @@ class ContainerConfiguration {
122
126
  if (!exists) {
123
127
  (0, decorator_1.saveModule)(decorator_1.CONFIGURATION_KEY, {
124
128
  target: clzz,
125
- namespace: this.namespace,
129
+ namespace: namespace,
126
130
  });
127
131
  }
128
132
  }
@@ -1,4 +1,8 @@
1
1
  export interface RouterInfo {
2
+ /**
3
+ * uuid
4
+ */
5
+ id: string;
2
6
  /**
3
7
  * router prefix
4
8
  */
@@ -92,6 +96,10 @@ export declare class WebRouterCollector {
92
96
  _paramString: string;
93
97
  _category: number;
94
98
  _weight: number;
99
+ /**
100
+ * uuid
101
+ */
102
+ id: string;
95
103
  /**
96
104
  * router prefix
97
105
  */
@@ -42,7 +42,8 @@ class WebRouterCollector {
42
42
  });
43
43
  }
44
44
  collectRoute(module, functionMeta = false) {
45
- const controllerId = (0, decorator_1.getProviderId)(module);
45
+ const controllerId = (0, decorator_1.getProviderName)(module);
46
+ const id = (0, decorator_1.getProviderUUId)(module);
46
47
  const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
47
48
  let priority;
48
49
  // implement middleware in controller
@@ -67,6 +68,7 @@ class WebRouterCollector {
67
68
  const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter.method) || [];
68
69
  const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter.method) || [];
69
70
  const data = {
71
+ id,
70
72
  prefix,
71
73
  routerName: webRouter.routerName || '',
72
74
  url: webRouter.path,
@@ -99,10 +101,11 @@ class WebRouterCollector {
99
101
  }
100
102
  }
101
103
  collectFunctionRoute(module, functionMeta = false) {
102
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
103
- // 老的函数路由
104
+ var _a, _b, _c, _d, _e, _f, _g, _h;
105
+ // serverlessTrigger metadata
104
106
  const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.FUNC_KEY, module);
105
- const controllerId = (0, decorator_1.getProviderId)(module);
107
+ const controllerId = (0, decorator_1.getProviderName)(module);
108
+ const id = (0, decorator_1.getProviderUUId)(module);
106
109
  const prefix = '/';
107
110
  if (!this.routes.has(prefix)) {
108
111
  this.routes.set(prefix, []);
@@ -115,131 +118,70 @@ class WebRouterCollector {
115
118
  });
116
119
  }
117
120
  for (const webRouter of webRouterInfo) {
118
- if (webRouter['type']) {
119
- // 新的 @ServerlessTrigger 写法
120
- if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
121
- const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
122
- const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
123
- // http/api gateway 函数
124
- const data = {
125
- prefix,
126
- routerName: '',
127
- url: webRouter['metadata']['path'],
128
- requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
129
- method: webRouter['methodName'],
130
- description: '',
131
- summary: '',
132
- handlerName: `${controllerId}.${webRouter['methodName']}`,
133
- funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
134
- controllerId,
135
- middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
136
- controllerMiddleware: [],
137
- requestMetadata: routeArgsInfo,
138
- responseMetadata: routerResponseData,
121
+ // 新的 @ServerlessTrigger 写法
122
+ if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
123
+ const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
124
+ const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
125
+ // http/api gateway 函数
126
+ const data = {
127
+ id,
128
+ prefix,
129
+ routerName: '',
130
+ url: webRouter['metadata']['path'],
131
+ requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
132
+ method: webRouter['methodName'],
133
+ description: '',
134
+ summary: '',
135
+ handlerName: `${controllerId}.${webRouter['methodName']}`,
136
+ funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
137
+ controllerId,
138
+ middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
139
+ controllerMiddleware: [],
140
+ requestMetadata: routeArgsInfo,
141
+ responseMetadata: routerResponseData,
142
+ };
143
+ if (functionMeta) {
144
+ const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
145
+ const functionName = (_f = (_e = functionMeta['functionName']) !== null && _e !== void 0 ? _e : webRouter['functionName']) !== null && _f !== void 0 ? _f : createFunctionName(module, webRouter['methodName']);
146
+ data.functionName = functionName;
147
+ data.functionTriggerName = webRouter['type'];
148
+ data.functionTriggerMetadata = webRouter['metadata'];
149
+ data.functionMetadata = {
150
+ functionName,
151
+ ...functionMeta,
139
152
  };
140
- if (functionMeta) {
141
- data.functionName = webRouter['functionName'];
142
- data.functionTriggerName = webRouter['type'];
143
- data.functionTriggerMetadata = webRouter['metadata'];
144
- const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
145
- data.functionMetadata = {
146
- functionName: webRouter['functionName'],
147
- ...functionMeta,
148
- };
149
- }
150
- this.checkDuplicateAndPush(prefix, data);
151
- }
152
- else {
153
- if (functionMeta) {
154
- const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
155
- // 其他类型的函数
156
- this.checkDuplicateAndPush(prefix, {
157
- prefix,
158
- routerName: '',
159
- url: '',
160
- requestMethod: '',
161
- method: webRouter['methodName'],
162
- description: '',
163
- summary: '',
164
- handlerName: `${controllerId}.${webRouter['methodName']}`,
165
- funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
166
- controllerId,
167
- middleware: [],
168
- controllerMiddleware: [],
169
- requestMetadata: [],
170
- responseMetadata: [],
171
- functionName: webRouter['functionName'],
172
- functionTriggerName: webRouter['type'],
173
- functionTriggerMetadata: webRouter['metadata'],
174
- functionMetadata: {
175
- functionName: webRouter['functionName'],
176
- ...functionMeta,
177
- },
178
- });
179
- }
180
153
  }
154
+ this.checkDuplicateAndPush(prefix, data);
181
155
  }
182
156
  else {
183
- // 老的 @Func 写法
184
- if (webRouter['path'] || webRouter['middleware']) {
185
- const data = {
157
+ if (functionMeta) {
158
+ const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
159
+ const functionName = (_h = (_g = functionMeta['functionName']) !== null && _g !== void 0 ? _g : webRouter['functionName']) !== null && _h !== void 0 ? _h : createFunctionName(module, webRouter['methodName']);
160
+ // 其他类型的函数
161
+ this.checkDuplicateAndPush(prefix, {
162
+ id,
186
163
  prefix,
187
164
  routerName: '',
188
- url: (_e = webRouter['path']) !== null && _e !== void 0 ? _e : '',
189
- requestMethod: (_f = webRouter['method']) !== null && _f !== void 0 ? _f : 'get',
190
- method: (_g = webRouter['key']) !== null && _g !== void 0 ? _g : '',
165
+ url: '',
166
+ requestMethod: '',
167
+ method: webRouter['methodName'],
191
168
  description: '',
192
169
  summary: '',
193
- handlerName: `${controllerId}.${webRouter['key']}`,
194
- funcHandlerName: webRouter['funHandler'] || `${controllerId}.${webRouter['key']}`,
170
+ handlerName: `${controllerId}.${webRouter['methodName']}`,
171
+ funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
195
172
  controllerId,
196
- middleware: webRouter['middleware'] || [],
173
+ middleware: [],
197
174
  controllerMiddleware: [],
198
175
  requestMetadata: [],
199
176
  responseMetadata: [],
200
- };
201
- if (functionMeta) {
202
- // get function information
203
- data.functionName = controllerId + '-' + ((_h = webRouter['key']) !== null && _h !== void 0 ? _h : '');
204
- data.functionTriggerName = decorator_1.ServerlessTriggerType.HTTP;
205
- data.functionTriggerMetadata = {
206
- path: (_j = webRouter['path']) !== null && _j !== void 0 ? _j : '/',
207
- method: (_k = webRouter['method']) !== null && _k !== void 0 ? _k : 'get',
208
- };
209
- data.functionMetadata = {
210
- functionName: data.functionName,
211
- };
212
- }
213
- // 老函数的 http
214
- this.checkDuplicateAndPush(prefix, data);
215
- }
216
- else {
217
- if (functionMeta) {
218
- // 非 http
219
- this.checkDuplicateAndPush(prefix, {
220
- prefix,
221
- routerName: '',
222
- url: '',
223
- requestMethod: '',
224
- method: webRouter['key'],
225
- description: '',
226
- summary: '',
227
- handlerName: `${controllerId}.${webRouter['key']}`,
228
- funcHandlerName: webRouter['funHandler'] ||
229
- `${controllerId}.${webRouter['key']}`,
230
- controllerId,
231
- middleware: webRouter['middleware'] || [],
232
- controllerMiddleware: [],
233
- requestMetadata: [],
234
- responseMetadata: [],
235
- functionName: webRouter['functionName'],
236
- functionTriggerName: webRouter['type'],
237
- functionTriggerMetadata: webRouter['metadata'],
238
- functionMetadata: {
239
- functionName: webRouter['functionName'],
240
- },
241
- });
242
- }
177
+ functionName,
178
+ functionTriggerName: webRouter['type'],
179
+ functionTriggerMetadata: webRouter['metadata'],
180
+ functionMetadata: {
181
+ functionName,
182
+ ...functionMeta,
183
+ },
184
+ });
243
185
  }
244
186
  }
245
187
  }
@@ -344,4 +286,7 @@ class WebRouterCollector {
344
286
  }
345
287
  }
346
288
  exports.WebRouterCollector = WebRouterCollector;
289
+ function createFunctionName(target, functionName) {
290
+ return (0, decorator_1.getProviderName)(target).replace(/[:#]/g, '-') + '-' + functionName;
291
+ }
347
292
  //# sourceMappingURL=webRouterCollector.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.0.0-alpha.36+b2d0a363",
3
+ "version": "3.0.0-alpha.37+574e016e",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.0-alpha.36+b2d0a363",
24
+ "@midwayjs/decorator": "^3.0.0-alpha.37+574e016e",
25
25
  "chai": "^4.2.0",
26
26
  "mm": "3",
27
27
  "sinon": "^7.2.2"
@@ -51,5 +51,5 @@
51
51
  "engines": {
52
52
  "node": ">= 10.0.0"
53
53
  },
54
- "gitHead": "b2d0a363d98302fda64ff47a6d25fca6e003a434"
54
+ "gitHead": "574e016e6bdc4be94f2ee8f82ada1670011911f1"
55
55
  }
@@ -1,10 +0,0 @@
1
- import { IConfigService } from '..';
2
- export declare class StaticConfigLoader {
3
- baseDir: string;
4
- configService: IConfigService;
5
- constructor(baseDir: string, currentEnvironment: string);
6
- getSerializeConfig(): Promise<string>;
7
- analyzeConfiguration(configurationModule: any): void;
8
- private getConfigurationExport;
9
- }
10
- //# sourceMappingURL=staticConfig.d.ts.map
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StaticConfigLoader = void 0;
4
- const path_1 = require("path");
5
- const decorator_1 = require("@midwayjs/decorator");
6
- const __1 = require("..");
7
- const configService_1 = require("../service/configService");
8
- class StaticConfigLoader {
9
- constructor(baseDir, currentEnvironment) {
10
- this.baseDir = baseDir;
11
- this.configService = new configService_1.MidwayConfigService({
12
- getCurrentEnv() {
13
- return currentEnvironment;
14
- },
15
- });
16
- }
17
- async getSerializeConfig() {
18
- const mainModule = (0, __1.safeRequire)(this.baseDir);
19
- let mainConfiguration;
20
- if (mainModule && mainModule['Configuration']) {
21
- mainConfiguration = mainModule['Configuration'];
22
- }
23
- else {
24
- mainConfiguration = (0, __1.safeRequire)((0, path_1.join)(this.baseDir, 'src', 'configuration.ts'));
25
- }
26
- const modules = this.getConfigurationExport(mainConfiguration);
27
- for (const module of modules) {
28
- this.analyzeConfiguration(module);
29
- }
30
- this.configService.load();
31
- return this.configService.getConfiguration();
32
- }
33
- analyzeConfiguration(configurationModule) {
34
- if (!configurationModule)
35
- return;
36
- const configurationOptions = (0, decorator_1.getClassMetadata)(decorator_1.CONFIGURATION_KEY, configurationModule);
37
- if (!configurationOptions)
38
- return;
39
- if (configurationOptions.imports) {
40
- for (const importModule of configurationOptions.imports) {
41
- if (typeof importModule !== 'string') {
42
- this.analyzeConfiguration(importModule['Configuration']);
43
- }
44
- }
45
- }
46
- if (configurationOptions === null || configurationOptions === void 0 ? void 0 : configurationOptions.importConfigs) {
47
- this.configService.add(configurationOptions.importConfigs);
48
- }
49
- }
50
- getConfigurationExport(exports) {
51
- const mods = [];
52
- if ((0, decorator_1.isClass)(exports) || (0, decorator_1.isFunction)(exports)) {
53
- mods.push(exports);
54
- }
55
- else {
56
- for (const m in exports) {
57
- const module = exports[m];
58
- if ((0, decorator_1.isClass)(module) || (0, decorator_1.isFunction)(module)) {
59
- mods.push(module);
60
- }
61
- }
62
- }
63
- return mods;
64
- }
65
- }
66
- exports.StaticConfigLoader = StaticConfigLoader;
67
- //# sourceMappingURL=staticConfig.js.map