@midwayjs/core 3.4.0-beta.5 → 3.4.0-beta.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.
@@ -133,7 +133,9 @@ let MidwayServerlessFunctionService = class MidwayServerlessFunctionService exte
133
133
  }
134
134
  }
135
135
  async getFunctionList() {
136
- return this.getFlattenRouterTable();
136
+ return this.getFlattenRouterTable({
137
+ compileUrlPattern: true,
138
+ });
137
139
  }
138
140
  addServerlessFunction(func, triggerOptions, functionOptions = {}) {
139
141
  var _a, _b;
@@ -73,6 +73,10 @@ export interface RouterInfo {
73
73
  * serverless function metadata
74
74
  */
75
75
  functionMetadata?: any;
76
+ /**
77
+ * pattern after path-regexp compile
78
+ */
79
+ urlCompiledPattern?: RegExp;
76
80
  }
77
81
  export declare type DynamicRouterInfo = Omit<RouterInfo, 'id' | 'method' | 'controllerId' | 'controllerMiddleware' | 'responseMetadata'>;
78
82
  export interface RouterPriority {
@@ -95,6 +99,8 @@ export declare class MidwayWebRouterService {
95
99
  private isReady;
96
100
  protected routes: Map<string, RouterInfo[]>;
97
101
  protected routesPriority: RouterPriority[];
102
+ private cachedFlattenRouteList;
103
+ private includeCompileUrlPattern;
98
104
  constructor(options?: RouterCollectorOptions);
99
105
  protected analyze(): Promise<void>;
100
106
  protected analyzeController(): void;
@@ -192,10 +198,18 @@ export declare class MidwayWebRouterService {
192
198
  * serverless function metadata
193
199
  */
194
200
  functionMetadata?: any;
201
+ /**
202
+ * pattern after path-regexp compile
203
+ */
204
+ urlCompiledPattern?: RegExp;
195
205
  }[];
196
206
  getRoutePriorityList(): Promise<RouterPriority[]>;
197
207
  getRouterTable(): Promise<Map<string, RouterInfo[]>>;
198
- getFlattenRouterTable(): Promise<RouterInfo[]>;
208
+ getFlattenRouterTable(options?: {
209
+ compileUrlPattern?: boolean;
210
+ noCache?: boolean;
211
+ }): Promise<RouterInfo[]>;
212
+ getMatchedRouterInfo(routerUrl: string, method: string): Promise<RouterInfo | undefined>;
199
213
  protected checkDuplicateAndPush(prefix: any, routerInfo: RouterInfo): void;
200
214
  }
201
215
  //# sourceMappingURL=webRouterService.d.ts.map
@@ -14,6 +14,7 @@ const decorator_1 = require("@midwayjs/decorator");
14
14
  const util_1 = require("../util");
15
15
  const error_1 = require("../error");
16
16
  const util = require("util");
17
+ const pathToRegexp_1 = require("../util/pathToRegexp");
17
18
  const debug = util.debuglog('midway:debug');
18
19
  let MidwayWebRouterService = class MidwayWebRouterService {
19
20
  constructor(options = {}) {
@@ -21,6 +22,7 @@ let MidwayWebRouterService = class MidwayWebRouterService {
21
22
  this.isReady = false;
22
23
  this.routes = new Map();
23
24
  this.routesPriority = [];
25
+ this.includeCompileUrlPattern = false;
24
26
  }
25
27
  async analyze() {
26
28
  this.analyzeController();
@@ -249,7 +251,21 @@ let MidwayWebRouterService = class MidwayWebRouterService {
249
251
  }
250
252
  return this.routes;
251
253
  }
252
- async getFlattenRouterTable() {
254
+ async getFlattenRouterTable(options = {}) {
255
+ if (this.cachedFlattenRouteList && !options.noCache) {
256
+ if (options.compileUrlPattern && !this.includeCompileUrlPattern) {
257
+ this.includeCompileUrlPattern = true;
258
+ // attach match pattern function
259
+ for (const item of this.cachedFlattenRouteList) {
260
+ if (item.url) {
261
+ item.urlCompiledPattern = (0, pathToRegexp_1.pathToRegexp)(item.url, [], {
262
+ end: false,
263
+ });
264
+ }
265
+ }
266
+ }
267
+ return this.cachedFlattenRouteList;
268
+ }
253
269
  if (!this.isReady) {
254
270
  await this.analyze();
255
271
  this.isReady = true;
@@ -258,8 +274,34 @@ let MidwayWebRouterService = class MidwayWebRouterService {
258
274
  for (const routerPriority of this.routesPriority) {
259
275
  routeArr = routeArr.concat(this.routes.get(routerPriority.prefix));
260
276
  }
277
+ if (options.compileUrlPattern) {
278
+ this.includeCompileUrlPattern = true;
279
+ // attach match pattern function
280
+ for (const item of routeArr) {
281
+ item.urlCompiledPattern = (0, pathToRegexp_1.pathToRegexp)(item.url, [], {
282
+ end: false,
283
+ });
284
+ }
285
+ }
286
+ this.cachedFlattenRouteList = routeArr;
261
287
  return routeArr;
262
288
  }
289
+ async getMatchedRouterInfo(routerUrl, method) {
290
+ const routes = await this.getFlattenRouterTable({
291
+ compileUrlPattern: true,
292
+ });
293
+ let matchedRouterInfo;
294
+ for (const item of routes) {
295
+ if (item.urlCompiledPattern) {
296
+ if (method.toUpperCase() === item['requestMethod'].toUpperCase() &&
297
+ item.urlCompiledPattern.test(routerUrl)) {
298
+ matchedRouterInfo = item;
299
+ break;
300
+ }
301
+ }
302
+ }
303
+ return matchedRouterInfo;
304
+ }
263
305
  checkDuplicateAndPush(prefix, routerInfo) {
264
306
  const prefixList = this.routes.get(prefix);
265
307
  const matched = prefixList.filter(item => {
@@ -1,6 +1,12 @@
1
1
  /**
2
2
  * this file fork from path-to-regexp package v1.8.0
3
3
  */
4
+ export interface PathToRegexpOptions {
5
+ end?: boolean;
6
+ strict?: boolean;
7
+ delimiter?: string;
8
+ sensitive?: boolean;
9
+ }
4
10
  /**
5
11
  * Normalize the given path string, returning a regular expression.
6
12
  *
@@ -13,5 +19,6 @@
13
19
  * @param {Object=} options
14
20
  * @return {!RegExp}
15
21
  */
16
- export declare function pathToRegexp(path: any, keys: any, options: any): any;
22
+ export declare function pathToRegexp(path: string | RegExp | Array<string>, options?: PathToRegexpOptions): RegExp;
23
+ export declare function pathToRegexp(path: string | RegExp | Array<string>, keys: Array<string>, options?: PathToRegexpOptions): RegExp;
17
24
  //# sourceMappingURL=pathToRegexp.d.ts.map
@@ -246,18 +246,6 @@ function tokensToRegExp(tokens, keys, options) {
246
246
  }
247
247
  return attachKeys(new RegExp('^' + route, flags(options)), keys);
248
248
  }
249
- /**
250
- * Normalize the given path string, returning a regular expression.
251
- *
252
- * An empty array can be passed in for the keys, which will hold the
253
- * placeholder key descriptions. For example, using `/user/:id`, `keys` will
254
- * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
255
- *
256
- * @param {(string|RegExp|Array)} path
257
- * @param {(Array|Object)=} keys
258
- * @param {Object=} options
259
- * @return {!RegExp}
260
- */
261
249
  function pathToRegexp(path, keys, options) {
262
250
  if (!Array.isArray(keys)) {
263
251
  options = /** @type {!Object} */ keys || options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.4.0-beta.5",
3
+ "version": "3.4.0-beta.6",
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.4.0-beta.5",
24
+ "@midwayjs/decorator": "^3.4.0-beta.6",
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=12"
47
47
  },
48
- "gitHead": "d84d1c77aed1fd973d002ab65cd0adeadb7924a6"
48
+ "gitHead": "8e187c4593d4832de32b6745af3e195f6b90816c"
49
49
  }