@midwayjs/core 3.10.15 → 3.11.1

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.
@@ -7,6 +7,7 @@ const constant_1 = require("./constant");
7
7
  const types_1 = require("../util/types");
8
8
  const camelCase_1 = require("../util/camelCase");
9
9
  const util_1 = require("../util");
10
+ const pathFileUtil_1 = require("../util/pathFileUtil");
10
11
  const debug = require('util').debuglog('midway:core');
11
12
  exports.PRELOAD_MODULE_KEY = 'INJECTION_PRELOAD_MODULE_KEY';
12
13
  exports.INJECT_CLASS_KEY_PREFIX = 'INJECTION_CLASS_META_DATA';
@@ -249,6 +250,13 @@ let manager = new DecoratorManager();
249
250
  if (typeof global === 'object') {
250
251
  if (global['MIDWAY_GLOBAL_DECORATOR_MANAGER']) {
251
252
  console.warn('DecoratorManager not singleton and please check @midwayjs/core version by "npm ls @midwayjs/core"');
253
+ const coreModulePathList = (0, pathFileUtil_1.getModuleRequirePathList)('@midwayjs/core');
254
+ if (coreModulePathList.length) {
255
+ console.info('The module may be located in:');
256
+ coreModulePathList.forEach((path, index) => {
257
+ console.info(`${index + 1}. ${path}`);
258
+ });
259
+ }
252
260
  manager = global['MIDWAY_GLOBAL_DECORATOR_MANAGER'];
253
261
  }
254
262
  else {
@@ -243,6 +243,7 @@ export interface JoinPoint {
243
243
  target: any;
244
244
  args: any[];
245
245
  proceed?(...args: any[]): any;
246
+ proceedIsAsyncFunction?: boolean;
246
247
  }
247
248
  export interface AspectMetadata {
248
249
  aspectTarget: any;
@@ -619,13 +620,14 @@ export interface Context {
619
620
  }
620
621
  export type IMidwayContext<FrameworkContext = unknown> = Context & FrameworkContext;
621
622
  export type NextFunction = () => Promise<any>;
623
+ export type IgnoreMatcher<CTX> = string | RegExp | ((ctx: CTX) => boolean);
622
624
  /**
623
625
  * Common middleware definition
624
626
  */
625
627
  export interface IMiddleware<CTX, R, N = unknown> {
626
628
  resolve: (app: IMidwayApplication) => FunctionMiddleware<CTX, R, N> | Promise<FunctionMiddleware<CTX, R, N>>;
627
- match?: (ctx: CTX) => boolean;
628
- ignore?: (ctx: CTX) => boolean;
629
+ match?: IgnoreMatcher<CTX> | IgnoreMatcher<CTX>[];
630
+ ignore?: IgnoreMatcher<CTX> | IgnoreMatcher<CTX>[];
629
631
  }
630
632
  export type FunctionMiddleware<CTX, R, N = unknown> = N extends true ? (req: CTX, res: R, next: N) => any : (context: CTX, next: R, options?: any) => any;
631
633
  export type ClassMiddleware<CTX, R, N> = new (...args: any[]) => IMiddleware<CTX, R, N>;
@@ -78,6 +78,7 @@ let MidwayAspectService = class MidwayAspectService {
78
78
  target: this,
79
79
  args: args,
80
80
  proceed: newProceed,
81
+ proceedIsAsyncFunction: true,
81
82
  };
82
83
  if (typeof aspectObject === 'function') {
83
84
  aspectObject = aspectObject();
@@ -122,6 +123,7 @@ let MidwayAspectService = class MidwayAspectService {
122
123
  target: this,
123
124
  args: args,
124
125
  proceed: newProceed,
126
+ proceedIsAsyncFunction: false,
125
127
  };
126
128
  if (typeof aspectObject === 'function') {
127
129
  aspectObject = aspectObject();
@@ -15,12 +15,13 @@ const interface_1 = require("../interface");
15
15
  const error_1 = require("../error");
16
16
  const util_1 = require("../util");
17
17
  const types_1 = require("../util/types");
18
+ const util_2 = require("util");
19
+ const debug = (0, util_2.debuglog)('midway:debug');
18
20
  let MidwayMiddlewareService = class MidwayMiddlewareService {
19
21
  constructor(applicationContext) {
20
22
  this.applicationContext = applicationContext;
21
23
  }
22
24
  async compose(middleware, app, name) {
23
- var _a, _b;
24
25
  if (!Array.isArray(middleware)) {
25
26
  throw new error_1.MidwayParameterError('Middleware stack must be an array');
26
27
  }
@@ -49,8 +50,9 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
49
50
  // wrap ignore and match
50
51
  const mw = fn;
51
52
  const match = (0, util_1.pathMatching)({
52
- match: (_a = classMiddleware.match) === null || _a === void 0 ? void 0 : _a.bind(classMiddleware),
53
- ignore: (_b = classMiddleware.ignore) === null || _b === void 0 ? void 0 : _b.bind(classMiddleware),
53
+ match: classMiddleware.match,
54
+ ignore: classMiddleware.ignore,
55
+ thisResolver: classMiddleware,
54
56
  });
55
57
  fn = (ctx, next, options) => {
56
58
  if (!match(ctx))
@@ -89,6 +91,9 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
89
91
  fn = next;
90
92
  if (!fn)
91
93
  return Promise.resolve();
94
+ const middlewareName = `${name ? `${name}.` : ''}${index} ${fn._name || fn.name || 'anonymous'}`;
95
+ const startTime = Date.now();
96
+ debug(`[middleware]: in ${middlewareName} +0`);
92
97
  try {
93
98
  if (supportBody) {
94
99
  return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
@@ -105,16 +110,21 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
105
110
  else if (context['body'] !== undefined) {
106
111
  result = context['body'];
107
112
  }
113
+ debug(`[middleware]: out ${middlewareName} +${Date.now() - startTime} with body`);
108
114
  return result;
109
115
  });
110
116
  }
111
117
  else {
112
118
  return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
113
119
  index,
114
- }));
120
+ })).then(result => {
121
+ debug(`[middleware]: out ${middlewareName} +${Date.now() - startTime}`);
122
+ return result;
123
+ });
115
124
  }
116
125
  }
117
126
  catch (err) {
127
+ debug(`[middleware]: out ${middlewareName} +${Date.now() - startTime} with err ${err.message}`);
118
128
  return Promise.reject(err);
119
129
  }
120
130
  }
@@ -1,4 +1,4 @@
1
- import { FunctionMiddleware } from '../interface';
1
+ import { FunctionMiddleware, IgnoreMatcher } from '../interface';
2
2
  import { camelCase, pascalCase } from './camelCase';
3
3
  import { randomUUID } from './uuid';
4
4
  import { safeParse, safeStringify } from './flatted';
@@ -85,7 +85,11 @@ export declare const deprecatedOutput: (message: string) => void;
85
85
  */
86
86
  export declare const transformRequestObjectByType: (originValue: any, targetType?: any) => any;
87
87
  export declare function toPathMatch(pattern: any): any;
88
- export declare function pathMatching(options: any): (ctx?: any) => any;
88
+ export declare function pathMatching(options: {
89
+ match?: IgnoreMatcher<any> | IgnoreMatcher<any>[];
90
+ ignore?: IgnoreMatcher<any> | IgnoreMatcher<any>[];
91
+ thisResolver?: any;
92
+ }): (ctx?: any) => boolean;
89
93
  /**
90
94
  * wrap function middleware with match and ignore
91
95
  * @param mw
@@ -270,9 +270,32 @@ function pathMatching(options) {
270
270
  throw new error_1.MidwayCommonError('options.match and options.ignore can not both present');
271
271
  if (!options.match && !options.ignore)
272
272
  return () => true;
273
+ if (options.match && !Array.isArray(options.match)) {
274
+ options.match = [options.match];
275
+ }
276
+ if (options.ignore && !Array.isArray(options.ignore)) {
277
+ options.ignore = [options.ignore];
278
+ }
279
+ const createMatch = (ignoreMatcherArr) => {
280
+ const matchedArr = ignoreMatcherArr.map(item => {
281
+ if (options.thisResolver) {
282
+ return toPathMatch(item).bind(options.thisResolver);
283
+ }
284
+ return toPathMatch(item);
285
+ });
286
+ return ctx => {
287
+ for (let i = 0; i < matchedArr.length; i++) {
288
+ const matched = matchedArr[i](ctx);
289
+ if (matched) {
290
+ return true;
291
+ }
292
+ }
293
+ return false;
294
+ };
295
+ };
273
296
  const matchFn = options.match
274
- ? toPathMatch(options.match)
275
- : toPathMatch(options.ignore);
297
+ ? createMatch(options.match)
298
+ : createMatch(options.ignore);
276
299
  return function pathMatch(ctx) {
277
300
  const matched = matchFn(ctx);
278
301
  return options.match ? matched : !matched;
@@ -7,4 +7,5 @@ export declare const PathFileUtil: {
7
7
  isPathEqual: typeof isPathEqual;
8
8
  getFileContentSync: typeof getFileContentSync;
9
9
  };
10
+ export declare function getModuleRequirePathList(moduleName: string): string[];
10
11
  //# sourceMappingURL=pathFileUtil.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PathFileUtil = exports.getFileContentSync = exports.isPathEqual = exports.isPath = void 0;
3
+ exports.getModuleRequirePathList = exports.PathFileUtil = exports.getFileContentSync = exports.isPathEqual = exports.isPath = void 0;
4
4
  const path_1 = require("path");
5
5
  const fs_1 = require("fs");
6
6
  function isPath(p) {
@@ -32,4 +32,31 @@ exports.PathFileUtil = {
32
32
  isPathEqual,
33
33
  getFileContentSync,
34
34
  };
35
+ function getModuleRequirePathList(moduleName) {
36
+ const moduleNameList = [moduleName, moduleName.replace(/\//g, '_')];
37
+ let moduleNameMap = {};
38
+ const modulePathList = [];
39
+ Object.keys(require.cache || {}).forEach(moduleName => {
40
+ let moduleIndex = -1;
41
+ for (const moduleName of moduleNameList) {
42
+ const index = moduleName.indexOf(moduleName);
43
+ if (index !== -1) {
44
+ moduleIndex = index;
45
+ break;
46
+ }
47
+ }
48
+ if (moduleIndex === -1) {
49
+ return;
50
+ }
51
+ const modulePath = moduleName.slice(0, moduleIndex);
52
+ if (moduleNameMap[modulePath]) {
53
+ return;
54
+ }
55
+ moduleNameMap[modulePath] = true;
56
+ modulePathList.push(modulePath);
57
+ });
58
+ moduleNameMap = undefined;
59
+ return modulePathList;
60
+ }
61
+ exports.getModuleRequirePathList = getModuleRequirePathList;
35
62
  //# sourceMappingURL=pathFileUtil.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.10.15",
3
+ "version": "3.11.1",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "koa": "2.14.1",
26
26
  "mm": "3.2.1",
27
27
  "raw-body": "2.5.2",
28
- "sinon": "15.0.1"
28
+ "sinon": "15.0.3"
29
29
  },
30
30
  "dependencies": {
31
31
  "@midwayjs/glob": "^1.0.2",
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=12"
44
44
  },
45
- "gitHead": "8d5fd0e58156612c675821134377208e4da710ad"
45
+ "gitHead": "bd9375874eb8cfaa49fbcfaa0497021cea06a394"
46
46
  }