@midwayjs/core 3.3.6 → 3.4.0-beta.11

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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/dist/baseFramework.d.ts +3 -0
  3. package/dist/baseFramework.js +32 -13
  4. package/dist/common/asyncContextManager.d.ts +61 -0
  5. package/dist/common/asyncContextManager.js +63 -0
  6. package/dist/common/dataSourceManager.d.ts +8 -1
  7. package/dist/common/dataSourceManager.js +61 -3
  8. package/dist/common/fileDetector.d.ts +1 -0
  9. package/dist/common/fileDetector.js +20 -0
  10. package/dist/common/filterManager.d.ts +1 -0
  11. package/dist/common/filterManager.js +37 -5
  12. package/dist/common/serviceFactory.js +2 -1
  13. package/dist/common/webGenerator.d.ts +4 -3
  14. package/dist/common/webGenerator.js +24 -15
  15. package/dist/config/config.default.d.ts +3 -0
  16. package/dist/config/config.default.js +3 -0
  17. package/dist/context/container.js +3 -0
  18. package/dist/error/base.d.ts +1 -1
  19. package/dist/error/base.js +6 -1
  20. package/dist/error/framework.d.ts +8 -0
  21. package/dist/error/framework.js +15 -1
  22. package/dist/error/http.js +16 -16
  23. package/dist/index.d.ts +5 -3
  24. package/dist/index.js +11 -3
  25. package/dist/interface.d.ts +14 -4
  26. package/dist/interface.js +3 -1
  27. package/dist/service/configService.js +7 -4
  28. package/dist/service/middlewareService.js +13 -5
  29. package/dist/service/slsFunctionService.d.ts +25 -0
  30. package/dist/service/slsFunctionService.js +242 -0
  31. package/dist/{common/webRouterCollector.d.ts → service/webRouterService.d.ts} +87 -36
  32. package/dist/{common/webRouterCollector.js → service/webRouterService.js} +121 -116
  33. package/dist/setup.js +3 -0
  34. package/dist/util/contextUtil.d.ts +2 -0
  35. package/dist/util/contextUtil.js +6 -1
  36. package/dist/util/index.d.ts +1 -0
  37. package/dist/util/index.js +23 -2
  38. package/dist/util/pathToRegexp.d.ts +113 -7
  39. package/dist/util/pathToRegexp.js +326 -205
  40. package/package.json +3 -3
  41. package/CHANGELOG.md +0 -2551
  42. package/dist/common/triggerCollector.d.ts +0 -8
  43. package/dist/common/triggerCollector.js +0 -37
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WebRouterCollector = exports.MidwayServerlessFunctionService = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const webRouterService_1 = require("./webRouterService");
15
+ const container_1 = require("../context/container");
16
+ const fileDetector_1 = require("../common/fileDetector");
17
+ const contextUtil_1 = require("../util/contextUtil");
18
+ let MidwayServerlessFunctionService = class MidwayServerlessFunctionService extends webRouterService_1.MidwayWebRouterService {
19
+ constructor(options = {}) {
20
+ super(Object.assign({}, options, {
21
+ includeFunctionRouter: true,
22
+ }));
23
+ this.options = options;
24
+ }
25
+ async analyze() {
26
+ this.analyzeController();
27
+ this.analyzeFunction();
28
+ this.sortPrefixAndRouter();
29
+ // requestMethod all transform to other method
30
+ for (const routerInfo of this.routes.values()) {
31
+ for (const info of routerInfo) {
32
+ if (info.requestMethod === 'all') {
33
+ info.functionTriggerMetadata.method = [
34
+ 'get',
35
+ 'post',
36
+ 'put',
37
+ 'delete',
38
+ 'head',
39
+ 'patch',
40
+ 'options',
41
+ ];
42
+ }
43
+ }
44
+ }
45
+ }
46
+ analyzeFunction() {
47
+ const fnModules = (0, decorator_1.listModule)(decorator_1.FUNC_KEY);
48
+ for (const module of fnModules) {
49
+ this.collectFunctionRoute(module);
50
+ }
51
+ }
52
+ collectFunctionRoute(module) {
53
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
54
+ // serverlessTrigger metadata
55
+ const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.FUNC_KEY, module);
56
+ const controllerId = (0, decorator_1.getProviderName)(module);
57
+ const id = (0, decorator_1.getProviderUUId)(module);
58
+ const prefix = '/';
59
+ if (!this.routes.has(prefix)) {
60
+ this.routes.set(prefix, []);
61
+ this.routesPriority.push({
62
+ prefix,
63
+ priority: -999,
64
+ middleware: [],
65
+ routerOptions: {},
66
+ controllerId,
67
+ routerModule: module,
68
+ });
69
+ }
70
+ for (const webRouter of webRouterInfo) {
71
+ // 新的 @ServerlessTrigger 写法
72
+ if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
73
+ const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
74
+ const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
75
+ // 新 http/api gateway 函数
76
+ const data = {
77
+ id,
78
+ prefix,
79
+ routerName: '',
80
+ url: webRouter['metadata']['path'],
81
+ requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
82
+ method: webRouter['methodName'],
83
+ description: '',
84
+ summary: '',
85
+ handlerName: `${controllerId}.${webRouter['methodName']}`,
86
+ funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
87
+ controllerId,
88
+ middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
89
+ controllerMiddleware: [],
90
+ requestMetadata: routeArgsInfo,
91
+ responseMetadata: routerResponseData,
92
+ };
93
+ const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
94
+ const functionName = (_f = (_e = functionMeta['functionName']) !== null && _e !== void 0 ? _e : webRouter['functionName']) !== null && _f !== void 0 ? _f : createFunctionName(module, webRouter['methodName']);
95
+ data.functionName = functionName;
96
+ data.functionTriggerName = webRouter['type'];
97
+ data.functionTriggerMetadata = webRouter['metadata'];
98
+ data.functionMetadata = {
99
+ functionName,
100
+ ...functionMeta,
101
+ };
102
+ this.checkDuplicateAndPush(prefix, data);
103
+ }
104
+ else {
105
+ const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
106
+ const functionName = (_h = (_g = functionMeta['functionName']) !== null && _g !== void 0 ? _g : webRouter['functionName']) !== null && _h !== void 0 ? _h : createFunctionName(module, webRouter['methodName']);
107
+ // 其他类型的函数
108
+ this.checkDuplicateAndPush(prefix, {
109
+ id,
110
+ prefix,
111
+ routerName: '',
112
+ url: '',
113
+ requestMethod: '',
114
+ method: webRouter['methodName'],
115
+ description: '',
116
+ summary: '',
117
+ handlerName: `${controllerId}.${webRouter['methodName']}`,
118
+ funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
119
+ controllerId,
120
+ middleware: ((_j = webRouter['metadata']) === null || _j === void 0 ? void 0 : _j['middleware']) || [],
121
+ controllerMiddleware: [],
122
+ requestMetadata: [],
123
+ responseMetadata: [],
124
+ functionName,
125
+ functionTriggerName: webRouter['type'],
126
+ functionTriggerMetadata: webRouter['metadata'],
127
+ functionMetadata: {
128
+ functionName,
129
+ ...functionMeta,
130
+ },
131
+ });
132
+ }
133
+ }
134
+ }
135
+ async getFunctionList() {
136
+ return this.getFlattenRouterTable({
137
+ compileUrlPattern: true,
138
+ });
139
+ }
140
+ addServerlessFunction(func, triggerOptions, functionOptions = {}) {
141
+ var _a, _b;
142
+ const prefix = '';
143
+ if (!this.routes.has(prefix)) {
144
+ this.routes.set(prefix, []);
145
+ this.routesPriority.push({
146
+ prefix,
147
+ priority: 0,
148
+ middleware: [],
149
+ routerOptions: {},
150
+ controllerId: undefined,
151
+ routerModule: undefined,
152
+ });
153
+ }
154
+ const functionName = (_a = triggerOptions.functionName) !== null && _a !== void 0 ? _a : functionOptions.functionName;
155
+ this.checkDuplicateAndPush(prefix, {
156
+ id: null,
157
+ method: func,
158
+ url: triggerOptions.metadata['path'] || '',
159
+ requestMethod: triggerOptions.metadata['method'] || '',
160
+ description: '',
161
+ summary: '',
162
+ handlerName: '',
163
+ funcHandlerName: triggerOptions.handlerName || functionOptions.handlerName,
164
+ controllerId: '',
165
+ middleware: ((_b = triggerOptions.metadata) === null || _b === void 0 ? void 0 : _b.middleware) || [],
166
+ controllerMiddleware: [],
167
+ requestMetadata: [],
168
+ responseMetadata: [],
169
+ functionName,
170
+ functionTriggerName: triggerOptions.metadata.name,
171
+ functionTriggerMetadata: triggerOptions.metadata,
172
+ functionMetadata: {
173
+ functionName,
174
+ ...functionOptions,
175
+ },
176
+ });
177
+ }
178
+ };
179
+ MidwayServerlessFunctionService = __decorate([
180
+ (0, decorator_1.Provide)(),
181
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
182
+ __metadata("design:paramtypes", [Object])
183
+ ], MidwayServerlessFunctionService);
184
+ exports.MidwayServerlessFunctionService = MidwayServerlessFunctionService;
185
+ function createFunctionName(target, functionName) {
186
+ return (0, decorator_1.getProviderName)(target).replace(/[:#]/g, '-') + '-' + functionName;
187
+ }
188
+ /**
189
+ * @deprecated use built-in MidwayWebRouterService first
190
+ */
191
+ class WebRouterCollector {
192
+ constructor(baseDir = '', options = {}) {
193
+ this.baseDir = baseDir;
194
+ this.options = options;
195
+ }
196
+ async init() {
197
+ if (!this.proxy) {
198
+ if (this.baseDir) {
199
+ const container = new container_1.MidwayContainer();
200
+ (0, decorator_1.bindContainer)(container);
201
+ container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
202
+ loadDir: this.baseDir,
203
+ }));
204
+ await container.ready();
205
+ }
206
+ if (this.options.includeFunctionRouter) {
207
+ if ((0, contextUtil_1.getCurrentMainFramework)()) {
208
+ this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
209
+ .getApplicationContext()
210
+ .getAsync(MidwayServerlessFunctionService, [this.options]);
211
+ }
212
+ else {
213
+ this.proxy = new MidwayServerlessFunctionService(this.options);
214
+ }
215
+ }
216
+ else {
217
+ if ((0, contextUtil_1.getCurrentMainFramework)()) {
218
+ this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
219
+ .getApplicationContext()
220
+ .getAsync(webRouterService_1.MidwayWebRouterService, [this.options]);
221
+ }
222
+ else {
223
+ this.proxy = new webRouterService_1.MidwayWebRouterService(this.options);
224
+ }
225
+ }
226
+ }
227
+ }
228
+ async getRoutePriorityList() {
229
+ await this.init();
230
+ return this.proxy.getRoutePriorityList();
231
+ }
232
+ async getRouterTable() {
233
+ await this.init();
234
+ return this.proxy.getRouterTable();
235
+ }
236
+ async getFlattenRouterTable() {
237
+ await this.init();
238
+ return this.proxy.getFlattenRouterTable();
239
+ }
240
+ }
241
+ exports.WebRouterCollector = WebRouterCollector;
242
+ //# sourceMappingURL=slsFunctionService.js.map
@@ -1,16 +1,17 @@
1
+ import { ControllerOption } from '@midwayjs/decorator';
1
2
  export interface RouterInfo {
2
3
  /**
3
4
  * uuid
4
5
  */
5
- id: string;
6
+ id?: string;
6
7
  /**
7
8
  * router prefix from controller
8
9
  */
9
- prefix: string;
10
+ prefix?: string;
10
11
  /**
11
12
  * router alias name
12
13
  */
13
- routerName: string;
14
+ routerName?: string;
14
15
  /**
15
16
  * router path, without prefix
16
17
  */
@@ -22,40 +23,43 @@ export interface RouterInfo {
22
23
  /**
23
24
  * invoke function method
24
25
  */
25
- method: string;
26
+ method: string | ((...args: any[]) => void);
26
27
  /**
27
28
  * router description
28
29
  */
29
- description: string;
30
- summary: string;
30
+ description?: string;
31
+ /**
32
+ * @deprecated
33
+ */
34
+ summary?: string;
31
35
  /**
32
36
  * router handler function key,for IoC container load
33
37
  */
34
- handlerName: string;
38
+ handlerName?: string;
35
39
  /**
36
40
  * serverless func load key
37
41
  */
38
- funcHandlerName: string;
42
+ funcHandlerName?: string;
39
43
  /**
40
44
  * controller provideId
41
45
  */
42
- controllerId: string;
46
+ controllerId?: string;
43
47
  /**
44
48
  * router middleware
45
49
  */
46
- middleware: any[];
50
+ middleware?: any[];
47
51
  /**
48
52
  * controller middleware in this router
49
53
  */
50
- controllerMiddleware: any[];
54
+ controllerMiddleware?: any[];
51
55
  /**
52
56
  * request args metadata
53
57
  */
54
- requestMetadata: any[];
58
+ requestMetadata?: any[];
55
59
  /**
56
60
  * response data metadata
57
61
  */
58
- responseMetadata: any[];
62
+ responseMetadata?: any[];
59
63
  /**
60
64
  * serverless function name
61
65
  */
@@ -72,7 +76,20 @@ export interface RouterInfo {
72
76
  * serverless function metadata
73
77
  */
74
78
  functionMetadata?: any;
79
+ /**
80
+ * url with prefix
81
+ */
82
+ fullUrl?: string;
83
+ /**
84
+ * pattern after path-regexp compile
85
+ */
86
+ fullUrlCompiledRegexp?: RegExp;
87
+ /**
88
+ * url after wildcard and can be path-to-regexp by path-to-regexp v6
89
+ */
90
+ fullUrlFlattenString?: string;
75
91
  }
92
+ export declare type DynamicRouterInfo = Omit<RouterInfo, 'id' | 'method' | 'controllerId' | 'controllerMiddleware' | 'responseMetadata'>;
76
93
  export interface RouterPriority {
77
94
  prefix: string;
78
95
  priority: number;
@@ -88,16 +105,31 @@ export interface RouterCollectorOptions {
88
105
  includeFunctionRouter?: boolean;
89
106
  globalPrefix?: string;
90
107
  }
91
- export declare class WebRouterCollector {
92
- protected readonly baseDir: string;
108
+ export declare class MidwayWebRouterService {
109
+ readonly options: RouterCollectorOptions;
93
110
  private isReady;
94
111
  protected routes: Map<string, RouterInfo[]>;
95
- private routesPriority;
96
- protected options: RouterCollectorOptions;
97
- constructor(baseDir?: string, options?: RouterCollectorOptions);
112
+ protected routesPriority: RouterPriority[];
113
+ private cachedFlattenRouteList;
114
+ private includeCompileUrlPattern;
115
+ constructor(options?: RouterCollectorOptions);
98
116
  protected analyze(): Promise<void>;
99
- protected collectRoute(module: any, functionMeta?: boolean): void;
100
- protected collectFunctionRoute(module: any, functionMeta?: boolean): void;
117
+ protected analyzeController(): void;
118
+ protected sortPrefixAndRouter(): void;
119
+ /**
120
+ * dynamically add a controller
121
+ * @param controllerClz
122
+ * @param controllerOption
123
+ * @param functionMeta
124
+ */
125
+ addController(controllerClz: any, controllerOption: ControllerOption, functionMeta?: boolean): void;
126
+ /**
127
+ * dynamically add a route to root prefix
128
+ * @param routerPath
129
+ * @param routerFunction
130
+ * @param routerInfoOption
131
+ */
132
+ addRouter(routerFunction: (...args: any[]) => void, routerInfoOption: DynamicRouterInfo): void;
101
133
  sortRouter(urlMatchList: RouterInfo[]): {
102
134
  _pureRouter: string;
103
135
  _level: number;
@@ -107,15 +139,15 @@ export declare class WebRouterCollector {
107
139
  /**
108
140
  * uuid
109
141
  */
110
- id: string;
142
+ id?: string;
111
143
  /**
112
144
  * router prefix from controller
113
145
  */
114
- prefix: string;
146
+ prefix?: string;
115
147
  /**
116
148
  * router alias name
117
149
  */
118
- routerName: string;
150
+ routerName?: string;
119
151
  /**
120
152
  * router path, without prefix
121
153
  */
@@ -127,40 +159,43 @@ export declare class WebRouterCollector {
127
159
  /**
128
160
  * invoke function method
129
161
  */
130
- method: string;
162
+ method: string | ((...args: any[]) => void);
131
163
  /**
132
164
  * router description
133
165
  */
134
- description: string;
135
- summary: string;
166
+ description?: string;
167
+ /**
168
+ * @deprecated
169
+ */
170
+ summary?: string;
136
171
  /**
137
172
  * router handler function key,for IoC container load
138
173
  */
139
- handlerName: string;
174
+ handlerName?: string;
140
175
  /**
141
176
  * serverless func load key
142
177
  */
143
- funcHandlerName: string;
178
+ funcHandlerName?: string;
144
179
  /**
145
180
  * controller provideId
146
181
  */
147
- controllerId: string;
182
+ controllerId?: string;
148
183
  /**
149
184
  * router middleware
150
185
  */
151
- middleware: any[];
186
+ middleware?: any[];
152
187
  /**
153
188
  * controller middleware in this router
154
189
  */
155
- controllerMiddleware: any[];
190
+ controllerMiddleware?: any[];
156
191
  /**
157
192
  * request args metadata
158
193
  */
159
- requestMetadata: any[];
194
+ requestMetadata?: any[];
160
195
  /**
161
196
  * response data metadata
162
197
  */
163
- responseMetadata: any[];
198
+ responseMetadata?: any[];
164
199
  /**
165
200
  * serverless function name
166
201
  */
@@ -177,10 +212,26 @@ export declare class WebRouterCollector {
177
212
  * serverless function metadata
178
213
  */
179
214
  functionMetadata?: any;
215
+ /**
216
+ * url with prefix
217
+ */
218
+ fullUrl?: string;
219
+ /**
220
+ * pattern after path-regexp compile
221
+ */
222
+ fullUrlCompiledRegexp?: RegExp;
223
+ /**
224
+ * url after wildcard and can be path-to-regexp by path-to-regexp v6
225
+ */
226
+ fullUrlFlattenString?: string;
180
227
  }[];
181
228
  getRoutePriorityList(): Promise<RouterPriority[]>;
182
229
  getRouterTable(): Promise<Map<string, RouterInfo[]>>;
183
- getFlattenRouterTable(): Promise<RouterInfo[]>;
184
- private checkDuplicateAndPush;
230
+ getFlattenRouterTable(options?: {
231
+ compileUrlPattern?: boolean;
232
+ noCache?: boolean;
233
+ }): Promise<RouterInfo[]>;
234
+ getMatchedRouterInfo(routerUrl: string, method: string): Promise<RouterInfo | undefined>;
235
+ protected checkDuplicateAndPush(prefix: any, routerInfo: RouterInfo): void;
185
236
  }
186
- //# sourceMappingURL=webRouterCollector.d.ts.map
237
+ //# sourceMappingURL=webRouterService.d.ts.map