@midwayjs/core 3.4.0-beta.9 → 3.4.3

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.
@@ -8,6 +8,7 @@ import { ContextMiddlewareManager } from './common/middlewareManager';
8
8
  import { MidwayMiddlewareService } from './service/middlewareService';
9
9
  import { FilterManager } from './common/filterManager';
10
10
  import { MidwayMockService } from './service/mockService';
11
+ import { AsyncContextManager } from './common/asyncContextManager';
11
12
  export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, OPT extends IConfigurationOptions, ResOrNext = unknown, Next = unknown> implements IMidwayFramework<APP, CTX, OPT, ResOrNext, Next> {
12
13
  readonly applicationContext: IMidwayContainer;
13
14
  app: APP;
@@ -21,6 +22,7 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
21
22
  protected filterManager: FilterManager<CTX, ResOrNext, Next>;
22
23
  protected composeMiddleware: any;
23
24
  protected bootstrapOptions: IMidwayBootstrapOptions;
25
+ protected asyncContextManager: AsyncContextManager;
24
26
  loggerService: MidwayLoggerService;
25
27
  environmentService: MidwayEnvironmentService;
26
28
  configService: MidwayConfigService;
@@ -234,18 +234,22 @@ class BaseFramework {
234
234
  async afterContainerReady(options) { }
235
235
  async applyMiddleware(lastMiddleware) {
236
236
  var _a;
237
- const asyncContextManagerEnabled = this.configService.getConfiguration('asyncContextManager.enable') ||
238
- false;
239
- const contextManager = asyncContextManagerEnabled
240
- ? ((_a = this.bootstrapOptions) === null || _a === void 0 ? void 0 : _a.contextManager) || new asyncContextManager_1.NoopContextManager()
241
- : new asyncContextManager_1.NoopContextManager();
242
- if (asyncContextManagerEnabled) {
243
- contextManager.enable();
237
+ if (!this.applicationContext.hasObject(interface_1.ASYNC_CONTEXT_MANAGER_KEY)) {
238
+ const asyncContextManagerEnabled = this.configService.getConfiguration('asyncContextManager.enable') ||
239
+ false;
240
+ const contextManager = asyncContextManagerEnabled
241
+ ? ((_a = this.bootstrapOptions) === null || _a === void 0 ? void 0 : _a.asyncContextManager) || new asyncContextManager_1.NoopContextManager()
242
+ : new asyncContextManager_1.NoopContextManager();
243
+ if (asyncContextManagerEnabled) {
244
+ contextManager.enable();
245
+ }
246
+ this.applicationContext.registerObject(interface_1.ASYNC_CONTEXT_MANAGER_KEY, contextManager);
244
247
  }
245
248
  if (!this.composeMiddleware) {
246
249
  this.middlewareManager.insertFirst((async (ctx, next) => {
247
250
  // warp with context manager
248
251
  const rootContext = asyncContextManager_1.ASYNC_ROOT_CONTEXT.setValue(interface_1.ASYNC_CONTEXT_KEY, ctx);
252
+ const contextManager = this.applicationContext.get(interface_1.ASYNC_CONTEXT_MANAGER_KEY);
249
253
  return await contextManager.with(rootContext, async () => {
250
254
  this.mockService.applyContextMocks(this.app, ctx);
251
255
  let returnResult = undefined;
@@ -11,6 +11,7 @@ export declare class DirectoryFileDetector extends AbstractFileDetector<{
11
11
  pattern: string | string[];
12
12
  ignore: string | string[];
13
13
  namespace: string;
14
+ conflictCheck: boolean;
14
15
  }> {
15
16
  private directoryFilterArray;
16
17
  private duplicateModuleCheckSet;
@@ -45,7 +45,7 @@ class DirectoryFileDetector extends AbstractFileDetector {
45
45
  });
46
46
  // 检查重复模块
47
47
  const checkDuplicatedHandler = (module, options) => {
48
- if (decorator_1.Types.isClass(module)) {
48
+ if (this.extraDetectorOptions.conflictCheck && decorator_1.Types.isClass(module)) {
49
49
  const name = (0, decorator_1.getProviderName)(module);
50
50
  if (name) {
51
51
  if (this.duplicateModuleCheckSet.has(name)) {
@@ -31,10 +31,11 @@ class FilterManager {
31
31
  const filter = await applicationContext.getAsync(FilterClass);
32
32
  const exceptionMetadata = (0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, FilterClass);
33
33
  if (exceptionMetadata && exceptionMetadata.catchTargets) {
34
+ exceptionMetadata.catchOptions = exceptionMetadata.catchOptions || {};
34
35
  for (const Exception of exceptionMetadata.catchTargets) {
35
36
  this.exceptionMap.set(Exception, {
36
37
  filter,
37
- catchOptions: exceptionMetadata.catchOptions || {},
38
+ catchOptions: exceptionMetadata.catchOptions,
38
39
  });
39
40
  if (exceptionMetadata.catchOptions['matchPrototype']) {
40
41
  this.protoMatchList.push(err => {
@@ -53,9 +53,10 @@ class ContainerConfiguration {
53
53
  namespace = configurationOptions.namespace;
54
54
  this.namespaceList.push(namespace);
55
55
  }
56
- if (configurationOptions.detectorOptions) {
57
- this.detectorOptionsList.push(configurationOptions.detectorOptions);
58
- }
56
+ this.detectorOptionsList.push({
57
+ conflictCheck: configurationOptions.conflictCheck,
58
+ ...configurationOptions.detectorOptions,
59
+ });
59
60
  debug(`[core]: load configuration in namespace="${namespace}"`);
60
61
  this.addImports(configurationOptions.imports);
61
62
  this.addImportObjects(configurationOptions.importObjects);
@@ -22,7 +22,7 @@ export declare class MidwayError extends Error {
22
22
  }
23
23
  export declare type ResOrMessage = string | {
24
24
  message: string;
25
- };
25
+ } | undefined;
26
26
  export declare class MidwayHttpError extends MidwayError {
27
27
  status: number;
28
28
  constructor(resOrMessage: ResOrMessage, status: number);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MidwayHttpError = exports.MidwayError = exports.registerErrorCode = void 0;
4
+ const http_1 = require("http");
4
5
  const codeGroup = new Set();
5
6
  /**
6
7
  * Register error group and code, return the standard ErrorCode
@@ -40,7 +41,11 @@ class MidwayError extends Error {
40
41
  exports.MidwayError = MidwayError;
41
42
  class MidwayHttpError extends MidwayError {
42
43
  constructor(resOrMessage, status, code, options) {
43
- super(typeof resOrMessage === 'string' ? resOrMessage : resOrMessage.message, code !== null && code !== void 0 ? code : String(status), options);
44
+ super(resOrMessage
45
+ ? typeof resOrMessage === 'string'
46
+ ? resOrMessage
47
+ : resOrMessage.message
48
+ : http_1.STATUS_CODES[status], code !== null && code !== void 0 ? code : String(status), options);
44
49
  this.status = status;
45
50
  }
46
51
  }
@@ -57,7 +57,7 @@ var HttpStatus;
57
57
  * 400 http error, Means that the request can be fulfilled because of the bad syntax.
58
58
  */
59
59
  class BadRequestError extends base_1.MidwayHttpError {
60
- constructor(resOrMessage = 'Bad Request') {
60
+ constructor(resOrMessage) {
61
61
  super(resOrMessage, HttpStatus.BAD_REQUEST);
62
62
  }
63
63
  }
@@ -66,7 +66,7 @@ exports.BadRequestError = BadRequestError;
66
66
  * 401 http error, Means that the request was legal, but the server is rejecting to answer it. For the use when authentication is required and has failed or has not yet been provided.
67
67
  */
68
68
  class UnauthorizedError extends base_1.MidwayHttpError {
69
- constructor(resOrMessage = 'Unauthorized') {
69
+ constructor(resOrMessage) {
70
70
  super(resOrMessage, HttpStatus.UNAUTHORIZED);
71
71
  }
72
72
  }
@@ -75,7 +75,7 @@ exports.UnauthorizedError = UnauthorizedError;
75
75
  * 4o4 http error, Means that the requested page cannot be found at the moment, but it may be available again in the future.
76
76
  */
77
77
  class NotFoundError extends base_1.MidwayHttpError {
78
- constructor(resOrMessage = 'Not Found') {
78
+ constructor(resOrMessage) {
79
79
  super(resOrMessage, HttpStatus.NOT_FOUND);
80
80
  }
81
81
  }
@@ -84,7 +84,7 @@ exports.NotFoundError = NotFoundError;
84
84
  * 403 http error, Means that the request is legal, but the server is rejecting to answer it.
85
85
  */
86
86
  class ForbiddenError extends base_1.MidwayHttpError {
87
- constructor(resOrMessage = 'Forbidden') {
87
+ constructor(resOrMessage) {
88
88
  super(resOrMessage, HttpStatus.FORBIDDEN);
89
89
  }
90
90
  }
@@ -93,7 +93,7 @@ exports.ForbiddenError = ForbiddenError;
93
93
  * 406 http error, Means that the server can only generate an answer which the client doesn't accept.
94
94
  */
95
95
  class NotAcceptableError extends base_1.MidwayHttpError {
96
- constructor(resOrMessage = 'Not Acceptable') {
96
+ constructor(resOrMessage) {
97
97
  super(resOrMessage, HttpStatus.NOT_ACCEPTABLE);
98
98
  }
99
99
  }
@@ -102,7 +102,7 @@ exports.NotAcceptableError = NotAcceptableError;
102
102
  * 408 http error, Means that the server timed out waiting for the request.
103
103
  */
104
104
  class RequestTimeoutError extends base_1.MidwayHttpError {
105
- constructor(resOrMessage = 'Request Timeout') {
105
+ constructor(resOrMessage) {
106
106
  super(resOrMessage, HttpStatus.REQUEST_TIMEOUT);
107
107
  }
108
108
  }
@@ -111,7 +111,7 @@ exports.RequestTimeoutError = RequestTimeoutError;
111
111
  * 409 http error, Means that the request cannot be completed, because of a conflict in the request.
112
112
  */
113
113
  class ConflictError extends base_1.MidwayHttpError {
114
- constructor(resOrMessage = 'Conflict') {
114
+ constructor(resOrMessage) {
115
115
  super(resOrMessage, HttpStatus.CONFLICT);
116
116
  }
117
117
  }
@@ -120,7 +120,7 @@ exports.ConflictError = ConflictError;
120
120
  * 410 http error, Means that the requested page is not available anymore.
121
121
  */
122
122
  class GoneError extends base_1.MidwayHttpError {
123
- constructor(resOrMessage = 'Gone') {
123
+ constructor(resOrMessage) {
124
124
  super(resOrMessage, HttpStatus.GONE);
125
125
  }
126
126
  }
@@ -129,7 +129,7 @@ exports.GoneError = GoneError;
129
129
  * 413 http error, Means that the request entity is too large and that's why the server won't accept the request.
130
130
  */
131
131
  class PayloadTooLargeError extends base_1.MidwayHttpError {
132
- constructor(resOrMessage = 'Request Entity Too Large') {
132
+ constructor(resOrMessage) {
133
133
  super(resOrMessage, HttpStatus.PAYLOAD_TOO_LARGE);
134
134
  }
135
135
  }
@@ -138,13 +138,13 @@ exports.PayloadTooLargeError = PayloadTooLargeError;
138
138
  * 415 http error, Means that the media type is not supported and that's why the server won't accept the request.
139
139
  */
140
140
  class UnsupportedMediaTypeError extends base_1.MidwayHttpError {
141
- constructor(resOrMessage = 'Unsupported Media Type') {
141
+ constructor(resOrMessage) {
142
142
  super(resOrMessage, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
143
143
  }
144
144
  }
145
145
  exports.UnsupportedMediaTypeError = UnsupportedMediaTypeError;
146
146
  class UnprocessableEntityError extends base_1.MidwayHttpError {
147
- constructor(resOrMessage = 'Unprocessable Entity') {
147
+ constructor(resOrMessage) {
148
148
  super(resOrMessage, HttpStatus.UNPROCESSABLE_ENTITY);
149
149
  }
150
150
  }
@@ -153,7 +153,7 @@ exports.UnprocessableEntityError = UnprocessableEntityError;
153
153
  * 500 http error, Is a generic error and users receive this error message when there is no more suitable specific message.
154
154
  */
155
155
  class InternalServerErrorError extends base_1.MidwayHttpError {
156
- constructor(resOrMessage = 'Internal Server Error') {
156
+ constructor(resOrMessage) {
157
157
  super(resOrMessage, HttpStatus.INTERNAL_SERVER_ERROR);
158
158
  }
159
159
  }
@@ -162,7 +162,7 @@ exports.InternalServerErrorError = InternalServerErrorError;
162
162
  * 501 http error, Means that the server doesn't recognize the request method or it lacks the ability to fulfill the request.
163
163
  */
164
164
  class NotImplementedError extends base_1.MidwayHttpError {
165
- constructor(resOrMessage = 'Not Implemented') {
165
+ constructor(resOrMessage) {
166
166
  super(resOrMessage, HttpStatus.NOT_IMPLEMENTED);
167
167
  }
168
168
  }
@@ -171,7 +171,7 @@ exports.NotImplementedError = NotImplementedError;
171
171
  * 502 http error, Means that the server was acting as a gateway or proxy and it received an invalid answer from the upstream server.
172
172
  */
173
173
  class BadGatewayError extends base_1.MidwayHttpError {
174
- constructor(resOrMessage = 'Bad Gateway') {
174
+ constructor(resOrMessage) {
175
175
  super(resOrMessage, HttpStatus.BAD_GATEWAY);
176
176
  }
177
177
  }
@@ -180,7 +180,7 @@ exports.BadGatewayError = BadGatewayError;
180
180
  * 503 http error, Means that the server is not available now (It may be overloaded or down).
181
181
  */
182
182
  class ServiceUnavailableError extends base_1.MidwayHttpError {
183
- constructor(resOrMessage = 'Service Unavailable') {
183
+ constructor(resOrMessage) {
184
184
  super(resOrMessage, HttpStatus.SERVICE_UNAVAILABLE);
185
185
  }
186
186
  }
@@ -189,7 +189,7 @@ exports.ServiceUnavailableError = ServiceUnavailableError;
189
189
  * 504 http error, Means that the server was acting as a gateway or proxy and it didn't get answer on time from the upstream server.
190
190
  */
191
191
  class GatewayTimeoutError extends base_1.MidwayHttpError {
192
- constructor(resOrMessage = 'Gateway Timeout') {
192
+ constructor(resOrMessage) {
193
193
  super(resOrMessage, HttpStatus.GATEWAY_TIMEOUT);
194
194
  }
195
195
  }
@@ -482,5 +482,6 @@ export interface MidwayConfig extends FileConfigOption<typeof _default> {
482
482
  [customConfigKey: string]: unknown;
483
483
  }
484
484
  export declare const ASYNC_CONTEXT_KEY: unique symbol;
485
+ export declare const ASYNC_CONTEXT_MANAGER_KEY = "MIDWAY_ASYNC_CONTEXT_MANAGER_KEY";
485
486
  export {};
486
487
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ASYNC_CONTEXT_KEY = exports.MIDWAY_LOGGER_WRITEABLE_DIR = exports.MidwayProcessTypeEnum = exports.REQUEST_CTX_LOGGER_CACHE_KEY = exports.HTTP_SERVER_KEY = exports.REQUEST_OBJ_CTX_KEY = exports.REQUEST_CTX_KEY = exports.ObjectLifeCycleEvent = void 0;
3
+ exports.ASYNC_CONTEXT_MANAGER_KEY = exports.ASYNC_CONTEXT_KEY = exports.MIDWAY_LOGGER_WRITEABLE_DIR = exports.MidwayProcessTypeEnum = exports.REQUEST_CTX_LOGGER_CACHE_KEY = exports.HTTP_SERVER_KEY = exports.REQUEST_OBJ_CTX_KEY = exports.REQUEST_CTX_KEY = exports.ObjectLifeCycleEvent = void 0;
4
4
  var ObjectLifeCycleEvent;
5
5
  (function (ObjectLifeCycleEvent) {
6
6
  ObjectLifeCycleEvent["BEFORE_BIND"] = "beforeBind";
@@ -20,4 +20,5 @@ var MidwayProcessTypeEnum;
20
20
  })(MidwayProcessTypeEnum = exports.MidwayProcessTypeEnum || (exports.MidwayProcessTypeEnum = {}));
21
21
  exports.MIDWAY_LOGGER_WRITEABLE_DIR = 'MIDWAY_LOGGER_WRITEABLE_DIR';
22
22
  exports.ASYNC_CONTEXT_KEY = Symbol('ASYNC_CONTEXT_KEY');
23
+ exports.ASYNC_CONTEXT_MANAGER_KEY = 'MIDWAY_ASYNC_CONTEXT_MANAGER_KEY';
23
24
  //# sourceMappingURL=interface.js.map
@@ -95,7 +95,7 @@ let MidwayWebRouterService = class MidwayWebRouterService {
95
95
  }
96
96
  else {
97
97
  // 不同的 controller,可能会有相同的 prefix,一旦 options 不同,就要报错
98
- if (middleware) {
98
+ if (middleware && middleware.length > 0) {
99
99
  const originRoute = this.routesPriority.filter(el => {
100
100
  return el.prefix === prefix;
101
101
  })[0];
@@ -1,5 +1,7 @@
1
1
  import { IConfigurationOptions, IMidwayContainer, IMidwayFramework } from '../interface';
2
+ import { AsyncContextManager } from '../common/asyncContextManager';
2
3
  export declare const getCurrentApplicationContext: () => IMidwayContainer;
3
4
  export declare const getCurrentMainFramework: <APP extends import("../interface").IMidwayBaseApplication<CTX>, CTX extends import("../interface").Context, CONFIG extends IConfigurationOptions>() => IMidwayFramework<APP, CTX, CONFIG, unknown, unknown>;
4
5
  export declare const getCurrentMainApp: <APP extends import("../interface").IMidwayBaseApplication<import("../interface").Context>>() => APP;
6
+ export declare const getCurrentAsyncContextManager: () => AsyncContextManager;
5
7
  //# sourceMappingURL=contextUtil.d.ts.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCurrentMainApp = exports.getCurrentMainFramework = exports.getCurrentApplicationContext = void 0;
3
+ exports.getCurrentAsyncContextManager = exports.getCurrentMainApp = exports.getCurrentMainFramework = exports.getCurrentApplicationContext = void 0;
4
+ const interface_1 = require("../interface");
4
5
  const getCurrentApplicationContext = () => {
5
6
  return global['MIDWAY_APPLICATION_CONTEXT'];
6
7
  };
@@ -17,4 +18,8 @@ const getCurrentMainApp = () => {
17
18
  return undefined;
18
19
  };
19
20
  exports.getCurrentMainApp = getCurrentMainApp;
21
+ const getCurrentAsyncContextManager = () => {
22
+ return (0, exports.getCurrentApplicationContext)().get(interface_1.ASYNC_CONTEXT_MANAGER_KEY);
23
+ };
24
+ exports.getCurrentAsyncContextManager = getCurrentAsyncContextManager;
20
25
  //# sourceMappingURL=contextUtil.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.4.0-beta.9",
3
+ "version": "3.4.3",
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.9",
24
+ "@midwayjs/decorator": "^3.4.1",
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": "41e82a0fba386c6ec42c2eefd1dff4795a81b389"
48
+ "gitHead": "0d79293f56d01a3ef3589e6a6ef53c582ac0c46f"
49
49
  }