@midwayjs/core 3.0.0-alpha.9 → 3.0.0-beta.12

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +149 -0
  2. package/README.md +1 -1
  3. package/dist/baseFramework.d.ts +56 -22
  4. package/dist/baseFramework.js +157 -232
  5. package/dist/common/applicationManager.d.ts +12 -0
  6. package/dist/common/applicationManager.js +66 -0
  7. package/dist/{util → common}/fileDetector.d.ts +0 -0
  8. package/dist/{util → common}/fileDetector.js +16 -10
  9. package/dist/common/filterManager.d.ts +19 -0
  10. package/dist/common/filterManager.js +85 -0
  11. package/dist/common/middlewareManager.d.ts +68 -0
  12. package/dist/common/middlewareManager.js +183 -0
  13. package/dist/common/serviceFactory.d.ts +15 -0
  14. package/dist/{util → common}/serviceFactory.js +16 -6
  15. package/dist/{util → common}/triggerCollector.d.ts +0 -0
  16. package/dist/{util → common}/triggerCollector.js +0 -0
  17. package/dist/common/webGenerator.d.ts +16 -0
  18. package/dist/common/webGenerator.js +117 -0
  19. package/dist/{util → common}/webRouterCollector.d.ts +21 -2
  20. package/dist/common/webRouterCollector.js +331 -0
  21. package/dist/config/config.default.d.ts +7 -0
  22. package/dist/config/config.default.js +28 -0
  23. package/dist/context/container.d.ts +73 -43
  24. package/dist/context/container.js +289 -271
  25. package/dist/context/definitionRegistry.d.ts +26 -0
  26. package/dist/context/definitionRegistry.js +124 -0
  27. package/dist/context/managedResolverFactory.d.ts +15 -32
  28. package/dist/context/managedResolverFactory.js +91 -320
  29. package/dist/context/providerWrapper.d.ts +2 -3
  30. package/dist/context/requestContainer.d.ts +0 -2
  31. package/dist/context/requestContainer.js +9 -26
  32. package/dist/definitions/functionDefinition.d.ts +3 -4
  33. package/dist/definitions/functionDefinition.js +0 -6
  34. package/dist/definitions/objectCreator.js +9 -8
  35. package/dist/definitions/objectDefinition.d.ts +4 -6
  36. package/dist/definitions/objectDefinition.js +0 -8
  37. package/dist/definitions/properties.d.ts +2 -15
  38. package/dist/definitions/properties.js +3 -72
  39. package/dist/error/base.d.ts +32 -0
  40. package/dist/error/base.js +48 -0
  41. package/dist/error/framework.d.ts +45 -0
  42. package/dist/error/framework.js +87 -0
  43. package/dist/error/http.d.ts +119 -0
  44. package/dist/error/http.js +169 -0
  45. package/dist/error/index.d.ts +4 -0
  46. package/dist/error/index.js +16 -0
  47. package/dist/functional/configuration.d.ts +4 -2
  48. package/dist/functional/configuration.js +12 -2
  49. package/dist/index.d.ts +24 -11
  50. package/dist/index.js +36 -36
  51. package/dist/interface.d.ts +270 -184
  52. package/dist/interface.js +9 -1
  53. package/dist/service/aspectService.d.ts +11 -14
  54. package/dist/service/aspectService.js +116 -172
  55. package/dist/service/configService.d.ts +10 -8
  56. package/dist/service/configService.js +61 -38
  57. package/dist/service/decoratorService.d.ts +23 -0
  58. package/dist/service/decoratorService.js +149 -0
  59. package/dist/service/environmentService.js +15 -4
  60. package/dist/service/frameworkService.d.ts +26 -0
  61. package/dist/service/frameworkService.js +176 -0
  62. package/dist/service/informationService.d.ts +4 -7
  63. package/dist/service/informationService.js +41 -10
  64. package/dist/service/lifeCycleService.d.ts +14 -0
  65. package/dist/service/lifeCycleService.js +130 -0
  66. package/dist/service/loggerService.d.ts +27 -0
  67. package/dist/service/loggerService.js +132 -0
  68. package/dist/service/middlewareService.d.ts +10 -0
  69. package/dist/service/middlewareService.js +123 -0
  70. package/dist/{features/pipeline.d.ts → service/pipelineService.d.ts} +56 -46
  71. package/dist/{features/pipeline.js → service/pipelineService.js} +54 -19
  72. package/dist/setup.d.ts +4 -0
  73. package/dist/setup.js +119 -0
  74. package/dist/util/contextUtil.d.ts +1 -1
  75. package/dist/util/contextUtil.js +2 -2
  76. package/dist/util/index.d.ts +64 -0
  77. package/dist/util/index.js +195 -7
  78. package/dist/util/pathFileUtil.js +2 -2
  79. package/dist/util/pathToRegexp.d.ts +17 -0
  80. package/dist/util/pathToRegexp.js +280 -0
  81. package/dist/util/webRouterParam.d.ts +2 -2
  82. package/dist/util/webRouterParam.js +43 -24
  83. package/package.json +10 -15
  84. package/dist/common/lodashWrap.d.ts +0 -9
  85. package/dist/common/lodashWrap.js +0 -18
  86. package/dist/common/notFoundError.d.ts +0 -8
  87. package/dist/common/notFoundError.js +0 -20
  88. package/dist/common/reflectTool.d.ts +0 -15
  89. package/dist/common/reflectTool.js +0 -83
  90. package/dist/context/applicationContext.d.ts +0 -81
  91. package/dist/context/applicationContext.js +0 -263
  92. package/dist/context/managed.d.ts +0 -45
  93. package/dist/context/managed.js +0 -69
  94. package/dist/context/resolverHandler.d.ts +0 -34
  95. package/dist/context/resolverHandler.js +0 -88
  96. package/dist/definitions/messageSource.d.ts +0 -13
  97. package/dist/definitions/messageSource.js +0 -74
  98. package/dist/definitions/resource.d.ts +0 -27
  99. package/dist/definitions/resource.js +0 -116
  100. package/dist/features/index.d.ts +0 -2
  101. package/dist/features/index.js +0 -3
  102. package/dist/logger.d.ts +0 -4
  103. package/dist/logger.js +0 -20
  104. package/dist/util/containerUtil.d.ts +0 -11
  105. package/dist/util/containerUtil.js +0 -26
  106. package/dist/util/emptyFramework.d.ts +0 -62
  107. package/dist/util/emptyFramework.js +0 -72
  108. package/dist/util/serviceFactory.d.ts +0 -13
  109. package/dist/util/staticConfig.d.ts +0 -10
  110. package/dist/util/staticConfig.js +0 -67
  111. package/dist/util/webRouterCollector.js +0 -348
@@ -1,348 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebRouterCollector = void 0;
4
- const decorator_1 = require("@midwayjs/decorator");
5
- const index_1 = require("./index");
6
- const container_1 = require("../context/container");
7
- const fileDetector_1 = require("./fileDetector");
8
- class WebRouterCollector {
9
- constructor(baseDir = '', options = {}) {
10
- this.isReady = false;
11
- this.routes = new Map();
12
- this.routesPriority = [];
13
- this.baseDir = baseDir;
14
- this.options = options;
15
- }
16
- async analyze() {
17
- if (this.baseDir) {
18
- const container = new container_1.MidwayContainer();
19
- container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
20
- loadDir: this.baseDir,
21
- }));
22
- await container.ready();
23
- }
24
- const controllerModules = decorator_1.listModule(decorator_1.CONTROLLER_KEY);
25
- for (const module of controllerModules) {
26
- this.collectRoute(module);
27
- }
28
- if (this.options.includeFunctionRouter) {
29
- const fnModules = decorator_1.listModule(decorator_1.FUNC_KEY);
30
- for (const module of fnModules) {
31
- this.collectFunctionRoute(module);
32
- }
33
- }
34
- // sort router
35
- for (const prefix of this.routes.keys()) {
36
- const routerInfo = this.routes.get(prefix);
37
- this.routes.set(prefix, this.sortRouter(routerInfo));
38
- }
39
- // sort prefix
40
- this.routesPriority = this.routesPriority.sort((routeA, routeB) => {
41
- return routeB.priority - routeA.priority;
42
- });
43
- }
44
- collectRoute(module, functionMeta = false) {
45
- const controllerId = decorator_1.getProviderId(module);
46
- const controllerOption = decorator_1.getClassMetadata(decorator_1.CONTROLLER_KEY, module);
47
- // sort for priority
48
- let priority = decorator_1.getClassMetadata(decorator_1.PRIORITY_KEY, module);
49
- // implement middleware in controller
50
- const middleware = controllerOption.routerOptions.middleware;
51
- const prefix = controllerOption.prefix || '/';
52
- if (prefix === '/' && priority === undefined) {
53
- priority = -999;
54
- }
55
- if (!this.routes.has(prefix)) {
56
- this.routes.set(prefix, []);
57
- this.routesPriority.push({
58
- prefix,
59
- priority: priority || 0,
60
- middleware,
61
- routerOptions: controllerOption.routerOptions,
62
- controllerId,
63
- });
64
- }
65
- const webRouterInfo = decorator_1.getClassMetadata(decorator_1.WEB_ROUTER_KEY, module);
66
- if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
67
- for (const webRouter of webRouterInfo) {
68
- const routeArgsInfo = decorator_1.getPropertyDataFromClass(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter.method) || [];
69
- const routerResponseData = decorator_1.getPropertyMetadata(decorator_1.WEB_RESPONSE_KEY, module, webRouter.method) || [];
70
- const data = {
71
- prefix,
72
- routerName: webRouter.routerName || '',
73
- url: webRouter.path,
74
- requestMethod: webRouter.requestMethod,
75
- method: webRouter.method,
76
- description: webRouter.description || '',
77
- summary: webRouter.summary || '',
78
- handlerName: `${controllerId}.${webRouter.method}`,
79
- funcHandlerName: `${controllerId}.${webRouter.method}`,
80
- controllerId,
81
- middleware: webRouter.middleware || [],
82
- controllerMiddleware: middleware || [],
83
- requestMetadata: routeArgsInfo,
84
- responseMetadata: routerResponseData,
85
- };
86
- if (functionMeta) {
87
- // get function information
88
- data.functionName = controllerId + '-' + webRouter.method;
89
- data.functionTriggerName = decorator_1.ServerlessTriggerType.HTTP;
90
- data.functionTriggerMetadata = {
91
- path: index_1.joinURLPath(prefix, webRouter.path.toString()),
92
- method: webRouter.requestMethod,
93
- };
94
- data.functionMetadata = {
95
- functionName: data.functionName,
96
- };
97
- }
98
- this.checkDuplicateAndPush(prefix, data);
99
- }
100
- }
101
- }
102
- collectFunctionRoute(module, functionMeta = false) {
103
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
104
- // 老的函数路由
105
- const webRouterInfo = decorator_1.getClassMetadata(decorator_1.FUNC_KEY, module);
106
- const controllerId = decorator_1.getProviderId(module);
107
- const prefix = '/';
108
- if (!this.routes.has(prefix)) {
109
- this.routes.set(prefix, []);
110
- this.routesPriority.push({
111
- prefix,
112
- priority: -999,
113
- middleware: [],
114
- routerOptions: {},
115
- controllerId,
116
- });
117
- }
118
- for (const webRouter of webRouterInfo) {
119
- if (webRouter['type']) {
120
- // 新的 @ServerlessTrigger 写法
121
- if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
122
- const routeArgsInfo = decorator_1.getPropertyDataFromClass(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
123
- const routerResponseData = decorator_1.getPropertyMetadata(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
124
- // 新 http/api gateway 函数
125
- const data = {
126
- prefix,
127
- routerName: '',
128
- url: webRouter['metadata']['path'],
129
- requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
130
- method: webRouter['methodName'],
131
- description: '',
132
- summary: '',
133
- handlerName: `${controllerId}.${webRouter['methodName']}`,
134
- funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
135
- controllerId,
136
- middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
137
- controllerMiddleware: [],
138
- requestMetadata: routeArgsInfo,
139
- responseMetadata: routerResponseData,
140
- };
141
- if (functionMeta) {
142
- data.functionName = webRouter['functionName'];
143
- data.functionTriggerName = webRouter['type'];
144
- data.functionTriggerMetadata = webRouter['metadata'];
145
- const functionMeta = decorator_1.getPropertyMetadata(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
146
- data.functionMetadata = {
147
- functionName: webRouter['functionName'],
148
- ...functionMeta,
149
- };
150
- }
151
- this.checkDuplicateAndPush(prefix, data);
152
- }
153
- else {
154
- if (functionMeta) {
155
- const functionMeta = decorator_1.getPropertyMetadata(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
156
- // 其他类型的函数
157
- this.checkDuplicateAndPush(prefix, {
158
- prefix,
159
- routerName: '',
160
- url: '',
161
- requestMethod: '',
162
- method: webRouter['methodName'],
163
- description: '',
164
- summary: '',
165
- handlerName: `${controllerId}.${webRouter['methodName']}`,
166
- funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
167
- controllerId,
168
- middleware: [],
169
- controllerMiddleware: [],
170
- requestMetadata: [],
171
- responseMetadata: [],
172
- functionName: webRouter['functionName'],
173
- functionTriggerName: webRouter['type'],
174
- functionTriggerMetadata: webRouter['metadata'],
175
- functionMetadata: {
176
- functionName: webRouter['functionName'],
177
- ...functionMeta,
178
- },
179
- });
180
- }
181
- }
182
- }
183
- else {
184
- // 老的 @Func 写法
185
- if (webRouter['path'] || webRouter['middleware']) {
186
- const data = {
187
- prefix,
188
- routerName: '',
189
- url: (_e = webRouter['path']) !== null && _e !== void 0 ? _e : '',
190
- requestMethod: (_f = webRouter['method']) !== null && _f !== void 0 ? _f : 'get',
191
- method: (_g = webRouter['key']) !== null && _g !== void 0 ? _g : '',
192
- description: '',
193
- summary: '',
194
- handlerName: `${controllerId}.${webRouter['key']}`,
195
- funcHandlerName: webRouter['funHandler'] || `${controllerId}.${webRouter['key']}`,
196
- controllerId,
197
- middleware: webRouter['middleware'] || [],
198
- controllerMiddleware: [],
199
- requestMetadata: [],
200
- responseMetadata: [],
201
- };
202
- if (functionMeta) {
203
- // get function information
204
- data.functionName = controllerId + '-' + ((_h = webRouter['key']) !== null && _h !== void 0 ? _h : '');
205
- data.functionTriggerName = decorator_1.ServerlessTriggerType.HTTP;
206
- data.functionTriggerMetadata = {
207
- path: (_j = webRouter['path']) !== null && _j !== void 0 ? _j : '/',
208
- method: (_k = webRouter['method']) !== null && _k !== void 0 ? _k : 'get',
209
- };
210
- data.functionMetadata = {
211
- functionName: data.functionName,
212
- };
213
- }
214
- // 老函数的 http
215
- this.checkDuplicateAndPush(prefix, data);
216
- }
217
- else {
218
- if (functionMeta) {
219
- // 非 http
220
- this.checkDuplicateAndPush(prefix, {
221
- prefix,
222
- routerName: '',
223
- url: '',
224
- requestMethod: '',
225
- method: webRouter['key'],
226
- description: '',
227
- summary: '',
228
- handlerName: `${controllerId}.${webRouter['key']}`,
229
- funcHandlerName: webRouter['funHandler'] ||
230
- `${controllerId}.${webRouter['key']}`,
231
- controllerId,
232
- middleware: webRouter['middleware'] || [],
233
- controllerMiddleware: [],
234
- requestMetadata: [],
235
- responseMetadata: [],
236
- functionName: webRouter['functionName'],
237
- functionTriggerName: webRouter['type'],
238
- functionTriggerMetadata: webRouter['metadata'],
239
- functionMetadata: {
240
- functionName: webRouter['functionName'],
241
- },
242
- });
243
- }
244
- }
245
- }
246
- }
247
- }
248
- sortRouter(urlMatchList) {
249
- // 1. 绝对路径规则优先级最高如 /ab/cb/e
250
- // 2. 星号只能出现最后且必须在/后面,如 /ab/cb/**
251
- // 3. 如果绝对路径和通配都能匹配一个路径时,绝对规则优先级高
252
- // 4. 有多个通配能匹配一个路径时,最长的规则匹配,如 /ab/** 和 /ab/cd/** 在匹配 /ab/cd/f 时命中 /ab/cd/**
253
- // 5. 如果 / 与 /* 都能匹配 / ,但 / 的优先级高于 /*
254
- return urlMatchList
255
- .map(item => {
256
- const urlString = item.url.toString();
257
- const weightArr = decorator_1.isRegExp(item.url)
258
- ? urlString.split('/')
259
- : urlString.split('/');
260
- let weight = 0;
261
- // 权重,比如通配的不加权,非通配加权,防止通配出现在最前面
262
- for (const fragment of weightArr) {
263
- if (fragment.includes(':') || fragment.includes('*')) {
264
- weight += 0;
265
- }
266
- else {
267
- weight += 1;
268
- }
269
- }
270
- let category = 2;
271
- const paramString = urlString.includes(':')
272
- ? urlString.replace(/:.+$/, '')
273
- : '';
274
- if (paramString) {
275
- category = 1;
276
- }
277
- if (urlString.includes('*')) {
278
- category = 0;
279
- }
280
- return {
281
- ...item,
282
- _pureRouter: urlString.replace(/\**$/, '').replace(/:\w+/, '123'),
283
- _level: urlString.split('/').length - 1,
284
- _paramString: paramString,
285
- _category: category,
286
- _weight: weight,
287
- };
288
- })
289
- .sort((handlerA, handlerB) => {
290
- // 不同一层级的对比
291
- if (handlerA._category !== handlerB._category) {
292
- return handlerB._category - handlerA._category;
293
- }
294
- // 不同长度
295
- if (handlerA._level === handlerB._level) {
296
- // 不同权重
297
- if (handlerA._weight !== handlerB._weight) {
298
- return handlerB._weight - handlerA._weight;
299
- }
300
- if (handlerB._pureRouter === handlerA._pureRouter) {
301
- return (handlerA.url.toString().length - handlerB.url.toString().length);
302
- }
303
- return handlerB._pureRouter.length - handlerA._pureRouter.length;
304
- }
305
- return handlerB._level - handlerA._level;
306
- });
307
- }
308
- async getRoutePriorityList() {
309
- if (!this.isReady) {
310
- await this.analyze();
311
- this.isReady = true;
312
- }
313
- return this.routesPriority;
314
- }
315
- async getRouterTable() {
316
- if (!this.isReady) {
317
- await this.analyze();
318
- this.isReady = true;
319
- }
320
- return this.routes;
321
- }
322
- async getFlattenRouterTable() {
323
- if (!this.isReady) {
324
- await this.analyze();
325
- this.isReady = true;
326
- }
327
- let routeArr = [];
328
- for (const routerInfo of this.routes.values()) {
329
- routeArr = routeArr.concat(routerInfo);
330
- }
331
- return routeArr;
332
- }
333
- checkDuplicateAndPush(prefix, routerInfo) {
334
- const prefixList = this.routes.get(prefix);
335
- const matched = prefixList.filter(item => {
336
- return (routerInfo.url &&
337
- routerInfo.requestMethod &&
338
- item.url === routerInfo.url &&
339
- item.requestMethod === routerInfo.requestMethod);
340
- });
341
- if (matched && matched.length) {
342
- throw new Error(`Duplicate router "${routerInfo.requestMethod} ${routerInfo.url}" at "${matched[0].handlerName}" and "${routerInfo.handlerName}"`);
343
- }
344
- prefixList.push(routerInfo);
345
- }
346
- }
347
- exports.WebRouterCollector = WebRouterCollector;
348
- //# sourceMappingURL=webRouterCollector.js.map