@midwayjs/core 3.4.0-beta.8 → 3.4.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.
- package/dist/baseFramework.d.ts +3 -0
- package/dist/baseFramework.js +32 -13
- package/dist/common/asyncContextManager.d.ts +61 -0
- package/dist/common/asyncContextManager.js +63 -0
- package/dist/common/fileDetector.d.ts +1 -0
- package/dist/common/fileDetector.js +1 -1
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +3 -0
- package/dist/context/container.js +4 -3
- package/dist/error/base.d.ts +1 -1
- package/dist/error/base.js +6 -1
- package/dist/error/http.js +16 -16
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/interface.d.ts +4 -0
- package/dist/interface.js +3 -1
- package/dist/service/middlewareService.js +4 -0
- package/dist/service/webRouterService.js +1 -1
- package/dist/util/contextUtil.d.ts +2 -0
- package/dist/util/contextUtil.js +6 -1
- package/package.json +3 -3
package/dist/baseFramework.d.ts
CHANGED
|
@@ -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;
|
|
@@ -20,6 +21,8 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
20
21
|
protected middlewareManager: ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
21
22
|
protected filterManager: FilterManager<CTX, ResOrNext, Next>;
|
|
22
23
|
protected composeMiddleware: any;
|
|
24
|
+
protected bootstrapOptions: IMidwayBootstrapOptions;
|
|
25
|
+
protected asyncContextManager: AsyncContextManager;
|
|
23
26
|
loggerService: MidwayLoggerService;
|
|
24
27
|
environmentService: MidwayEnvironmentService;
|
|
25
28
|
configService: MidwayConfigService;
|
package/dist/baseFramework.js
CHANGED
|
@@ -22,6 +22,7 @@ const middlewareService_1 = require("./service/middlewareService");
|
|
|
22
22
|
const filterManager_1 = require("./common/filterManager");
|
|
23
23
|
const mockService_1 = require("./service/mockService");
|
|
24
24
|
const util = require("util");
|
|
25
|
+
const asyncContextManager_1 = require("./common/asyncContextManager");
|
|
25
26
|
const debug = util.debuglog('midway:debug');
|
|
26
27
|
class BaseFramework {
|
|
27
28
|
constructor(applicationContext) {
|
|
@@ -45,6 +46,7 @@ class BaseFramework {
|
|
|
45
46
|
return true;
|
|
46
47
|
}
|
|
47
48
|
async initialize(options) {
|
|
49
|
+
this.bootstrapOptions = options;
|
|
48
50
|
await this.beforeContainerInitialize(options);
|
|
49
51
|
await this.containerInitialize(options);
|
|
50
52
|
await this.afterContainerInitialize(options);
|
|
@@ -231,21 +233,38 @@ class BaseFramework {
|
|
|
231
233
|
*/
|
|
232
234
|
async afterContainerReady(options) { }
|
|
233
235
|
async applyMiddleware(lastMiddleware) {
|
|
236
|
+
var _a;
|
|
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);
|
|
247
|
+
}
|
|
234
248
|
if (!this.composeMiddleware) {
|
|
235
249
|
this.middlewareManager.insertFirst((async (ctx, next) => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
250
|
+
// warp with context manager
|
|
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);
|
|
253
|
+
return await contextManager.with(rootContext, async () => {
|
|
254
|
+
this.mockService.applyContextMocks(this.app, ctx);
|
|
255
|
+
let returnResult = undefined;
|
|
256
|
+
try {
|
|
257
|
+
const result = await next();
|
|
258
|
+
returnResult = await this.filterManager.runResultFilter(result, ctx);
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
returnResult = await this.filterManager.runErrorFilter(err, ctx);
|
|
262
|
+
}
|
|
263
|
+
if (returnResult.error) {
|
|
264
|
+
throw returnResult.error;
|
|
265
|
+
}
|
|
266
|
+
return returnResult.result;
|
|
267
|
+
});
|
|
249
268
|
}));
|
|
250
269
|
debug(`[core]: Compose middleware = [${this.middlewareManager.getNames()}]`);
|
|
251
270
|
this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager, this.app);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export interface AsyncContext {
|
|
2
|
+
/**
|
|
3
|
+
* Get a value from the context.
|
|
4
|
+
*
|
|
5
|
+
* @param key key which identifies a context value
|
|
6
|
+
*/
|
|
7
|
+
getValue(key: symbol): unknown;
|
|
8
|
+
/**
|
|
9
|
+
* Create a new context which inherits from this context and has
|
|
10
|
+
* the given key set to the given value.
|
|
11
|
+
*
|
|
12
|
+
* @param key context key for which to set the value
|
|
13
|
+
* @param value value to set for the given key
|
|
14
|
+
*/
|
|
15
|
+
setValue(key: symbol, value: unknown): AsyncContext;
|
|
16
|
+
/**
|
|
17
|
+
* Return a new context which inherits from this context but does
|
|
18
|
+
* not contain a value for the given key.
|
|
19
|
+
*
|
|
20
|
+
* @param key context key for which to clear a value
|
|
21
|
+
*/
|
|
22
|
+
deleteValue(key: symbol): AsyncContext;
|
|
23
|
+
}
|
|
24
|
+
export interface AsyncContextManager {
|
|
25
|
+
/**
|
|
26
|
+
* Get the current active context
|
|
27
|
+
*/
|
|
28
|
+
active(): AsyncContext;
|
|
29
|
+
/**
|
|
30
|
+
* Run the fn callback with object set as the current active context
|
|
31
|
+
* @param context Any object to set as the current active context
|
|
32
|
+
* @param fn A callback to be immediately run within a specific context
|
|
33
|
+
* @param thisArg optional receiver to be used for calling fn
|
|
34
|
+
* @param args optional arguments forwarded to fn
|
|
35
|
+
*/
|
|
36
|
+
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(context: AsyncContext, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
|
|
37
|
+
/**
|
|
38
|
+
* Bind an object as the current context (or a specific one)
|
|
39
|
+
* @param [context] Optionally specify the context which you want to assign
|
|
40
|
+
* @param target Any object to which a context need to be set
|
|
41
|
+
*/
|
|
42
|
+
bind<T>(context: AsyncContext, target: T): T;
|
|
43
|
+
/**
|
|
44
|
+
* Enable context management
|
|
45
|
+
*/
|
|
46
|
+
enable(): this;
|
|
47
|
+
/**
|
|
48
|
+
* Disable context management
|
|
49
|
+
*/
|
|
50
|
+
disable(): this;
|
|
51
|
+
}
|
|
52
|
+
/** The root context is used as the default parent context when there is no active context */
|
|
53
|
+
export declare const ASYNC_ROOT_CONTEXT: AsyncContext;
|
|
54
|
+
export declare class NoopContextManager implements AsyncContextManager {
|
|
55
|
+
active(): AsyncContext;
|
|
56
|
+
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(_context: AsyncContext, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
|
|
57
|
+
bind<T>(_context: AsyncContext, target: T): T;
|
|
58
|
+
enable(): this;
|
|
59
|
+
disable(): this;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=asyncContextManager.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright The OpenTelemetry Authors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.NoopContextManager = exports.ASYNC_ROOT_CONTEXT = void 0;
|
|
19
|
+
class AsyncBaseContext {
|
|
20
|
+
/**
|
|
21
|
+
* Construct a new context which inherits values from an optional parent context.
|
|
22
|
+
*
|
|
23
|
+
* @param parentContext a context from which to inherit values
|
|
24
|
+
*/
|
|
25
|
+
constructor(parentContext) {
|
|
26
|
+
// for minification
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
28
|
+
const self = this;
|
|
29
|
+
self._currentContext = parentContext ? new Map(parentContext) : new Map();
|
|
30
|
+
self.getValue = (key) => self._currentContext.get(key);
|
|
31
|
+
self.setValue = (key, value) => {
|
|
32
|
+
const context = new AsyncBaseContext(self._currentContext);
|
|
33
|
+
context._currentContext.set(key, value);
|
|
34
|
+
return context;
|
|
35
|
+
};
|
|
36
|
+
self.deleteValue = (key) => {
|
|
37
|
+
const context = new AsyncBaseContext(self._currentContext);
|
|
38
|
+
context._currentContext.delete(key);
|
|
39
|
+
return context;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The root context is used as the default parent context when there is no active context */
|
|
44
|
+
exports.ASYNC_ROOT_CONTEXT = new AsyncBaseContext();
|
|
45
|
+
class NoopContextManager {
|
|
46
|
+
active() {
|
|
47
|
+
return exports.ASYNC_ROOT_CONTEXT;
|
|
48
|
+
}
|
|
49
|
+
with(_context, fn, thisArg, ...args) {
|
|
50
|
+
return fn.call(thisArg, ...args);
|
|
51
|
+
}
|
|
52
|
+
bind(_context, target) {
|
|
53
|
+
return target;
|
|
54
|
+
}
|
|
55
|
+
enable() {
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
disable() {
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.NoopContextManager = NoopContextManager;
|
|
63
|
+
//# sourceMappingURL=asyncContextManager.js.map
|
|
@@ -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)) {
|
|
@@ -8,6 +8,9 @@ exports.default = (appInfo) => {
|
|
|
8
8
|
const isDevelopment = (0, util_1.isDevelopmentEnvironment)((0, util_1.getCurrentEnvironment)());
|
|
9
9
|
const logRoot = (_a = process.env[interface_1.MIDWAY_LOGGER_WRITEABLE_DIR]) !== null && _a !== void 0 ? _a : appInfo.root;
|
|
10
10
|
return {
|
|
11
|
+
asyncContextManager: {
|
|
12
|
+
enable: false,
|
|
13
|
+
},
|
|
11
14
|
midwayLogger: {
|
|
12
15
|
default: {
|
|
13
16
|
dir: (0, path_1.join)(logRoot, 'logs', appInfo.name),
|
|
@@ -53,9 +53,10 @@ class ContainerConfiguration {
|
|
|
53
53
|
namespace = configurationOptions.namespace;
|
|
54
54
|
this.namespaceList.push(namespace);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
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);
|
package/dist/error/base.d.ts
CHANGED
|
@@ -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);
|
package/dist/error/base.js
CHANGED
|
@@ -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(
|
|
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
|
}
|
package/dist/error/http.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
192
|
+
constructor(resOrMessage) {
|
|
193
193
|
super(resOrMessage, HttpStatus.GATEWAY_TIMEOUT);
|
|
194
194
|
}
|
|
195
195
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ export * from './common/filterManager';
|
|
|
35
35
|
export * from './common/applicationManager';
|
|
36
36
|
export * from './setup';
|
|
37
37
|
export * from './error';
|
|
38
|
+
export { AsyncContextManager, ASYNC_ROOT_CONTEXT, AsyncContext, } from './common/asyncContextManager';
|
|
38
39
|
/**
|
|
39
40
|
* proxy
|
|
40
41
|
*/
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.MidwayFrameworkType = exports.DataSourceManager = exports.WebRouterCollector = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
17
|
+
exports.MidwayFrameworkType = exports.ASYNC_ROOT_CONTEXT = exports.DataSourceManager = exports.WebRouterCollector = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
18
18
|
__exportStar(require("./interface"), exports);
|
|
19
19
|
__exportStar(require("./context/container"), exports);
|
|
20
20
|
var requestContainer_1 = require("./context/requestContainer");
|
|
@@ -81,6 +81,8 @@ __exportStar(require("./common/filterManager"), exports);
|
|
|
81
81
|
__exportStar(require("./common/applicationManager"), exports);
|
|
82
82
|
__exportStar(require("./setup"), exports);
|
|
83
83
|
__exportStar(require("./error"), exports);
|
|
84
|
+
var asyncContextManager_1 = require("./common/asyncContextManager");
|
|
85
|
+
Object.defineProperty(exports, "ASYNC_ROOT_CONTEXT", { enumerable: true, get: function () { return asyncContextManager_1.ASYNC_ROOT_CONTEXT; } });
|
|
84
86
|
/**
|
|
85
87
|
* proxy
|
|
86
88
|
*/
|
package/dist/interface.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { ILogger, LoggerOptions, LoggerContextFormat } from '@midwayjs/logger';
|
|
|
4
4
|
import * as EventEmitter from 'events';
|
|
5
5
|
import { ContextMiddlewareManager } from './common/middlewareManager';
|
|
6
6
|
import _default from './config/config.default';
|
|
7
|
+
import { AsyncContextManager } from './common/asyncContextManager';
|
|
7
8
|
export declare type PowerPartial<T> = {
|
|
8
9
|
[U in keyof T]?: T[U] extends {} ? PowerPartial<T[U]> : T[U];
|
|
9
10
|
};
|
|
@@ -432,6 +433,7 @@ export interface IMidwayBootstrapOptions {
|
|
|
432
433
|
globalConfig?: Array<{
|
|
433
434
|
[environmentName: string]: Record<string, any>;
|
|
434
435
|
}> | Record<string, any>;
|
|
436
|
+
asyncContextManager?: AsyncContextManager;
|
|
435
437
|
}
|
|
436
438
|
export interface IConfigurationOptions {
|
|
437
439
|
logger?: ILogger;
|
|
@@ -479,5 +481,7 @@ export interface MidwayAppInfo {
|
|
|
479
481
|
export interface MidwayConfig extends FileConfigOption<typeof _default> {
|
|
480
482
|
[customConfigKey: string]: unknown;
|
|
481
483
|
}
|
|
484
|
+
export declare const ASYNC_CONTEXT_KEY: unique symbol;
|
|
485
|
+
export declare const ASYNC_CONTEXT_MANAGER_KEY = "MIDWAY_ASYNC_CONTEXT_MANAGER_KEY";
|
|
482
486
|
export {};
|
|
483
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.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";
|
|
@@ -19,4 +19,6 @@ var MidwayProcessTypeEnum;
|
|
|
19
19
|
MidwayProcessTypeEnum["AGENT"] = "AGENT";
|
|
20
20
|
})(MidwayProcessTypeEnum = exports.MidwayProcessTypeEnum || (exports.MidwayProcessTypeEnum = {}));
|
|
21
21
|
exports.MIDWAY_LOGGER_WRITEABLE_DIR = 'MIDWAY_LOGGER_WRITEABLE_DIR';
|
|
22
|
+
exports.ASYNC_CONTEXT_KEY = Symbol('ASYNC_CONTEXT_KEY');
|
|
23
|
+
exports.ASYNC_CONTEXT_MANAGER_KEY = 'MIDWAY_ASYNC_CONTEXT_MANAGER_KEY';
|
|
22
24
|
//# sourceMappingURL=interface.js.map
|
|
@@ -32,6 +32,10 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
|
|
|
32
32
|
const classMiddleware = await this.applicationContext.getAsync(fn);
|
|
33
33
|
if (classMiddleware) {
|
|
34
34
|
fn = await classMiddleware.resolve(app);
|
|
35
|
+
if (!fn) {
|
|
36
|
+
// for middleware enabled
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
35
39
|
if (!classMiddleware.match && !classMiddleware.ignore) {
|
|
36
40
|
if (!fn.name) {
|
|
37
41
|
fn._name = classMiddleware.constructor.name;
|
|
@@ -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
|
package/dist/util/contextUtil.js
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "3.4.1",
|
|
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.
|
|
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": "
|
|
48
|
+
"gitHead": "c7b47267c80f93407537677d8366f68bc6dfc462"
|
|
49
49
|
}
|