@midwayjs/core 4.0.0-alpha.1 → 4.0.0-beta.10
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/README.md +1 -1
- package/dist/baseFramework.d.ts +13 -10
- package/dist/baseFramework.js +30 -15
- package/dist/common/applicationManager.js +6 -4
- package/dist/common/asyncContextManager.js +23 -0
- package/dist/common/dataListener.d.ts +5 -3
- package/dist/common/dataListener.js +11 -3
- package/dist/common/dataSourceManager.d.ts +18 -9
- package/dist/common/dataSourceManager.js +129 -77
- package/dist/common/fileDetector.js +2 -4
- package/dist/common/filterManager.js +6 -8
- package/dist/common/loggerFactory.js +1 -3
- package/dist/common/middlewareManager.js +2 -2
- package/dist/common/performanceManager.d.ts +0 -1
- package/dist/common/performanceManager.js +21 -20
- package/dist/common/priorityManager.js +2 -4
- package/dist/common/serviceDiscovery/healthCheck.d.ts +66 -0
- package/dist/common/serviceDiscovery/healthCheck.js +215 -0
- package/dist/common/serviceDiscovery/loadBalancer.d.ts +21 -0
- package/dist/common/serviceDiscovery/loadBalancer.js +49 -0
- package/dist/common/serviceDiscovery/serviceDiscovery.d.ts +59 -0
- package/dist/common/serviceDiscovery/serviceDiscovery.js +107 -0
- package/dist/common/serviceFactory.d.ts +5 -2
- package/dist/common/serviceFactory.js +47 -12
- package/dist/common/typedResourceManager.js +3 -2
- package/dist/common/webGenerator.js +2 -0
- package/dist/config/config.default.js +4 -1
- package/dist/context/componentLoader.js +3 -2
- package/dist/context/container.d.ts +4 -4
- package/dist/context/container.js +15 -10
- package/dist/context/definitionRegistry.d.ts +2 -0
- package/dist/context/definitionRegistry.js +11 -16
- package/dist/context/dynamicContainer.d.ts +17 -0
- package/dist/context/dynamicContainer.js +203 -0
- package/dist/context/managedResolverFactory.d.ts +1 -2
- package/dist/context/managedResolverFactory.js +16 -8
- package/dist/context/providerWrapper.js +1 -2
- package/dist/context/requestContainer.d.ts +1 -0
- package/dist/context/requestContainer.js +8 -2
- package/dist/decorator/common/aspect.js +1 -2
- package/dist/decorator/common/autoload.js +1 -2
- package/dist/decorator/common/configuration.js +1 -2
- package/dist/decorator/common/filter.js +2 -3
- package/dist/decorator/common/framework.js +8 -9
- package/dist/decorator/common/guard.js +2 -3
- package/dist/decorator/common/inject.js +4 -5
- package/dist/decorator/common/middleware.js +1 -2
- package/dist/decorator/common/mock.js +1 -2
- package/dist/decorator/common/objectDef.js +2 -3
- package/dist/decorator/common/pipe.js +1 -2
- package/dist/decorator/common/provide.js +1 -2
- package/dist/decorator/common/scope.js +2 -3
- package/dist/decorator/decoratorManager.js +4 -4
- package/dist/decorator/faas/serverlessTrigger.js +2 -3
- package/dist/decorator/metadataManager.d.ts +14 -6
- package/dist/decorator/metadataManager.js +49 -63
- package/dist/decorator/microservice/consumer.js +1 -2
- package/dist/decorator/microservice/kafkaListener.js +1 -2
- package/dist/decorator/microservice/provider.js +4 -4
- package/dist/decorator/microservice/rabbitmqListener.js +1 -2
- package/dist/decorator/task/queue.js +1 -2
- package/dist/decorator/task/schedule.js +1 -2
- package/dist/decorator/task/task.js +1 -2
- package/dist/decorator/task/taskLocal.js +1 -2
- package/dist/decorator/web/controller.d.ts +6 -0
- package/dist/decorator/web/controller.js +1 -2
- package/dist/decorator/web/response.js +5 -6
- package/dist/decorator/ws/webSocketController.js +1 -2
- package/dist/decorator/ws/webSocketEvent.js +6 -6
- package/dist/definitions/functionDefinition.js +21 -12
- package/dist/definitions/objectCreator.js +5 -2
- package/dist/definitions/objectDefinition.js +19 -17
- package/dist/error/base.js +5 -2
- package/dist/error/framework.d.ts +1 -1
- package/dist/error/framework.js +4 -2
- package/dist/error/http.d.ts +7 -0
- package/dist/error/http.js +11 -1
- package/dist/functional/configuration.d.ts +6 -6
- package/dist/functional/configuration.js +11 -10
- package/dist/functional/hooks.d.ts +3 -1
- package/dist/functional/hooks.js +18 -9
- package/dist/index.d.ts +5 -1
- package/dist/index.js +8 -2
- package/dist/interface.d.ts +179 -21
- package/dist/interface.js +15 -1
- package/dist/legacy/decorator.js +31 -32
- package/dist/response/base.d.ts +3 -5
- package/dist/response/base.js +22 -21
- package/dist/response/http.d.ts +4 -7
- package/dist/response/http.js +12 -12
- package/dist/response/sse.d.ts +0 -1
- package/dist/response/sse.js +4 -1
- package/dist/response/stream.d.ts +0 -1
- package/dist/response/stream.js +3 -1
- package/dist/service/aspectService.js +1 -0
- package/dist/service/configService.d.ts +1 -1
- package/dist/service/configService.js +16 -14
- package/dist/service/decoratorService.js +6 -4
- package/dist/service/environmentService.js +2 -3
- package/dist/service/frameworkService.js +9 -1
- package/dist/service/healthService.d.ts +2 -0
- package/dist/service/healthService.js +20 -8
- package/dist/service/informationService.d.ts +3 -0
- package/dist/service/informationService.js +13 -0
- package/dist/service/lifeCycleService.d.ts +13 -7
- package/dist/service/lifeCycleService.js +55 -33
- package/dist/service/loggerService.js +6 -2
- package/dist/service/middlewareService.js +1 -0
- package/dist/service/mockService.js +17 -15
- package/dist/service/slsFunctionService.js +1 -0
- package/dist/service/webRouterService.d.ts +25 -1
- package/dist/service/webRouterService.js +20 -3
- package/dist/setup.js +12 -6
- package/dist/util/camelCase.js +2 -3
- package/dist/util/contextUtil.d.ts +3 -3
- package/dist/util/extend.js +1 -2
- package/dist/util/flatted.js +2 -3
- package/dist/util/fs.js +2 -2
- package/dist/util/httpclient.d.ts +0 -4
- package/dist/util/httpclient.js +3 -2
- package/dist/util/index.d.ts +3 -18
- package/dist/util/index.js +49 -103
- package/dist/util/network.d.ts +10 -0
- package/dist/util/network.js +40 -0
- package/dist/util/pathFileUtil.d.ts +1 -2
- package/dist/util/pathFileUtil.js +7 -7
- package/dist/util/retry.js +2 -3
- package/dist/util/timeout.d.ts +56 -0
- package/dist/util/timeout.js +143 -0
- package/dist/util/types.d.ts +7 -7
- package/dist/util/types.js +17 -17
- package/dist/util/uuid.js +1 -2
- package/package.json +5 -5
package/README.md
CHANGED
package/dist/baseFramework.d.ts
CHANGED
|
@@ -14,10 +14,8 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
14
14
|
app: APP;
|
|
15
15
|
configurationOptions: OPT;
|
|
16
16
|
protected logger: ILogger;
|
|
17
|
-
protected
|
|
17
|
+
protected frameworkLoggerName: string;
|
|
18
18
|
protected defaultContext: {};
|
|
19
|
-
protected contextLoggerApplyLogger: string;
|
|
20
|
-
protected contextLoggerFormat: any;
|
|
21
19
|
protected middlewareManager: ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
22
20
|
protected filterManager: FilterManager<CTX, ResOrNext, Next>;
|
|
23
21
|
protected guardManager: GuardManager<CTX>;
|
|
@@ -25,14 +23,14 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
25
23
|
protected bootstrapOptions: IMidwayBootstrapOptions;
|
|
26
24
|
protected asyncContextManager: AsyncContextManager;
|
|
27
25
|
private namespace;
|
|
28
|
-
loggerService: MidwayLoggerService;
|
|
29
|
-
environmentService: MidwayEnvironmentService;
|
|
30
|
-
configService: MidwayConfigService;
|
|
31
|
-
informationService: MidwayInformationService;
|
|
32
|
-
middlewareService: MidwayMiddlewareService<CTX, ResOrNext, Next>;
|
|
33
|
-
mockService: MidwayMockService;
|
|
26
|
+
protected loggerService: MidwayLoggerService;
|
|
27
|
+
protected environmentService: MidwayEnvironmentService;
|
|
28
|
+
protected configService: MidwayConfigService;
|
|
29
|
+
protected informationService: MidwayInformationService;
|
|
30
|
+
protected middlewareService: MidwayMiddlewareService<CTX, ResOrNext, Next>;
|
|
31
|
+
protected mockService: MidwayMockService;
|
|
34
32
|
constructor(applicationContext: IMidwayGlobalContainer);
|
|
35
|
-
init(): Promise<this>;
|
|
33
|
+
protected init(): Promise<this>;
|
|
36
34
|
abstract configure(options?: OPT): OPT;
|
|
37
35
|
abstract applicationInitialize(options: IMidwayBootstrapOptions): void | Promise<void>;
|
|
38
36
|
abstract run(): Promise<void>;
|
|
@@ -64,5 +62,10 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
64
62
|
protected createGuardManager(): GuardManager<CTX>;
|
|
65
63
|
setNamespace(namespace: string): void;
|
|
66
64
|
getNamespace(): string;
|
|
65
|
+
/**
|
|
66
|
+
* Set the default framework logger name
|
|
67
|
+
* @since 4.0.0
|
|
68
|
+
*/
|
|
69
|
+
setFrameworkLoggerName(loggerName: string): void;
|
|
67
70
|
}
|
|
68
71
|
//# sourceMappingURL=baseFramework.d.ts.map
|
package/dist/baseFramework.js
CHANGED
|
@@ -27,21 +27,31 @@ const asyncContextManager_1 = require("./common/asyncContextManager");
|
|
|
27
27
|
const guardManager_1 = require("./common/guardManager");
|
|
28
28
|
const debug = util.debuglog('midway:debug');
|
|
29
29
|
class BaseFramework {
|
|
30
|
+
applicationContext;
|
|
31
|
+
app;
|
|
32
|
+
configurationOptions;
|
|
33
|
+
logger;
|
|
34
|
+
frameworkLoggerName = 'appLogger';
|
|
35
|
+
defaultContext = {};
|
|
36
|
+
middlewareManager = this.createMiddlewareManager();
|
|
37
|
+
filterManager = this.createFilterManager();
|
|
38
|
+
guardManager = this.createGuardManager();
|
|
39
|
+
composeMiddleware = null;
|
|
40
|
+
bootstrapOptions;
|
|
41
|
+
asyncContextManager;
|
|
42
|
+
namespace;
|
|
43
|
+
loggerService;
|
|
44
|
+
environmentService;
|
|
45
|
+
configService;
|
|
46
|
+
informationService;
|
|
47
|
+
middlewareService;
|
|
48
|
+
mockService;
|
|
30
49
|
constructor(applicationContext) {
|
|
31
50
|
this.applicationContext = applicationContext;
|
|
32
|
-
this.defaultContext = {};
|
|
33
|
-
this.middlewareManager = this.createMiddlewareManager();
|
|
34
|
-
this.filterManager = this.createFilterManager();
|
|
35
|
-
this.guardManager = this.createGuardManager();
|
|
36
|
-
this.composeMiddleware = null;
|
|
37
51
|
}
|
|
38
52
|
async init() {
|
|
39
53
|
this.configurationOptions = this.configure() ?? {};
|
|
40
|
-
this.contextLoggerApplyLogger =
|
|
41
|
-
this.configurationOptions.contextLoggerApplyLogger ?? 'appLogger';
|
|
42
|
-
this.contextLoggerFormat = this.configurationOptions.contextLoggerFormat;
|
|
43
54
|
this.logger = this.loggerService.getLogger('coreLogger');
|
|
44
|
-
this.appLogger = this.loggerService.getLogger('appLogger');
|
|
45
55
|
return this;
|
|
46
56
|
}
|
|
47
57
|
isEnable() {
|
|
@@ -74,7 +84,7 @@ class BaseFramework {
|
|
|
74
84
|
return this.app;
|
|
75
85
|
}
|
|
76
86
|
createContextLogger(ctx, name) {
|
|
77
|
-
if (name && name !==
|
|
87
|
+
if (name && name !== this.frameworkLoggerName) {
|
|
78
88
|
const appLogger = this.getLogger(name);
|
|
79
89
|
let ctxLoggerCache = ctx.getAttr(constants_1.REQUEST_CTX_LOGGER_CACHE_KEY);
|
|
80
90
|
if (!ctxLoggerCache) {
|
|
@@ -91,14 +101,12 @@ class BaseFramework {
|
|
|
91
101
|
return ctxLogger;
|
|
92
102
|
}
|
|
93
103
|
else {
|
|
94
|
-
const appLogger = this.getLogger(name ?? this.contextLoggerApplyLogger);
|
|
95
104
|
// avoid maximum call stack size exceeded
|
|
96
105
|
if (ctx['_logger']) {
|
|
97
106
|
return ctx['_logger'];
|
|
98
107
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
});
|
|
108
|
+
const appLogger = this.getLogger(name);
|
|
109
|
+
ctx['_logger'] = this.loggerService.createContextLogger(ctx, appLogger);
|
|
102
110
|
return ctx['_logger'];
|
|
103
111
|
}
|
|
104
112
|
}
|
|
@@ -258,7 +266,7 @@ class BaseFramework {
|
|
|
258
266
|
return this.composeMiddleware;
|
|
259
267
|
}
|
|
260
268
|
getLogger(name) {
|
|
261
|
-
return this.loggerService.getLogger(name
|
|
269
|
+
return this.loggerService.getLogger(name ?? this.frameworkLoggerName);
|
|
262
270
|
}
|
|
263
271
|
getCoreLogger() {
|
|
264
272
|
return this.logger;
|
|
@@ -302,6 +310,13 @@ class BaseFramework {
|
|
|
302
310
|
getNamespace() {
|
|
303
311
|
return this.namespace;
|
|
304
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* Set the default framework logger name
|
|
315
|
+
* @since 4.0.0
|
|
316
|
+
*/
|
|
317
|
+
setFrameworkLoggerName(loggerName) {
|
|
318
|
+
this.frameworkLoggerName = loggerName;
|
|
319
|
+
}
|
|
305
320
|
}
|
|
306
321
|
exports.BaseFramework = BaseFramework;
|
|
307
322
|
__decorate([
|
|
@@ -10,9 +10,7 @@ exports.MidwayApplicationManager = void 0;
|
|
|
10
10
|
const interface_1 = require("../interface");
|
|
11
11
|
const decorator_1 = require("../decorator");
|
|
12
12
|
let MidwayApplicationManager = class MidwayApplicationManager {
|
|
13
|
-
|
|
14
|
-
this.globalFrameworkMap = new Map();
|
|
15
|
-
}
|
|
13
|
+
globalFrameworkMap = new Map();
|
|
16
14
|
addFramework(frameworkNameOrNamespace, framework) {
|
|
17
15
|
this.globalFrameworkMap.set(frameworkNameOrNamespace, framework);
|
|
18
16
|
}
|
|
@@ -33,8 +31,12 @@ let MidwayApplicationManager = class MidwayApplicationManager {
|
|
|
33
31
|
}
|
|
34
32
|
getApplications(frameworkNameOrNamespace) {
|
|
35
33
|
if (!frameworkNameOrNamespace) {
|
|
36
|
-
return Array.from(this.globalFrameworkMap.values())
|
|
34
|
+
return Array.from(this.globalFrameworkMap.values())
|
|
35
|
+
.map(framework => {
|
|
37
36
|
return framework.getApplication();
|
|
37
|
+
})
|
|
38
|
+
.filter(app => {
|
|
39
|
+
return !!app;
|
|
38
40
|
});
|
|
39
41
|
}
|
|
40
42
|
else {
|
|
@@ -18,6 +18,7 @@ exports.AsyncLocalStorageContextManager = exports.NoopContextManager = exports.A
|
|
|
18
18
|
*/
|
|
19
19
|
const async_hooks_1 = require("async_hooks");
|
|
20
20
|
class AsyncBaseContext {
|
|
21
|
+
_currentContext;
|
|
21
22
|
/**
|
|
22
23
|
* Construct a new context which inherits values from an optional parent context.
|
|
23
24
|
*
|
|
@@ -40,6 +41,27 @@ class AsyncBaseContext {
|
|
|
40
41
|
return context;
|
|
41
42
|
};
|
|
42
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Get a value from the context.
|
|
46
|
+
*
|
|
47
|
+
* @param key key which identifies a context value
|
|
48
|
+
*/
|
|
49
|
+
getValue;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new context which inherits from this context and has
|
|
52
|
+
* the given key set to the given value.
|
|
53
|
+
*
|
|
54
|
+
* @param key context key for which to set the value
|
|
55
|
+
* @param value value to set for the given key
|
|
56
|
+
*/
|
|
57
|
+
setValue;
|
|
58
|
+
/**
|
|
59
|
+
* Return a new context which inherits from this context but does
|
|
60
|
+
* not contain a value for the given key.
|
|
61
|
+
*
|
|
62
|
+
* @param key context key for which to clear a value
|
|
63
|
+
*/
|
|
64
|
+
deleteValue;
|
|
43
65
|
}
|
|
44
66
|
/** The root context is used as the default parent context when there is no active context */
|
|
45
67
|
exports.ASYNC_ROOT_CONTEXT = new AsyncBaseContext();
|
|
@@ -62,6 +84,7 @@ class NoopContextManager {
|
|
|
62
84
|
}
|
|
63
85
|
exports.NoopContextManager = NoopContextManager;
|
|
64
86
|
class AsyncLocalStorageContextManager {
|
|
87
|
+
_asyncLocalStorage;
|
|
65
88
|
constructor() {
|
|
66
89
|
this._asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
|
|
67
90
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
export declare abstract class DataListener<T> {
|
|
2
|
-
|
|
1
|
+
export declare abstract class DataListener<T, U = T> {
|
|
2
|
+
protected innerData: T;
|
|
3
|
+
private isReady;
|
|
3
4
|
protected init(): Promise<void>;
|
|
4
5
|
abstract initData(): T | Promise<T>;
|
|
5
6
|
abstract onData(callback: (data: T) => void): any;
|
|
6
7
|
protected setData(data: T): void;
|
|
7
|
-
getData():
|
|
8
|
+
getData(): U;
|
|
9
|
+
protected transformData(data: T): U;
|
|
8
10
|
stop(): Promise<void>;
|
|
9
11
|
protected destroyListener(): Promise<void>;
|
|
10
12
|
}
|
|
@@ -12,15 +12,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.DataListener = void 0;
|
|
13
13
|
const decorator_1 = require("../decorator");
|
|
14
14
|
class DataListener {
|
|
15
|
+
innerData;
|
|
16
|
+
isReady = false;
|
|
15
17
|
async init() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
if (!this.isReady) {
|
|
19
|
+
this.innerData = await this.initData();
|
|
20
|
+
await this.onData(this.setData.bind(this));
|
|
21
|
+
this.isReady = true;
|
|
22
|
+
}
|
|
18
23
|
}
|
|
19
24
|
setData(data) {
|
|
20
25
|
this.innerData = data;
|
|
21
26
|
}
|
|
22
27
|
getData() {
|
|
23
|
-
return this.innerData;
|
|
28
|
+
return this.transformData(this.innerData);
|
|
29
|
+
}
|
|
30
|
+
transformData(data) {
|
|
31
|
+
return data;
|
|
24
32
|
}
|
|
25
33
|
async stop() {
|
|
26
34
|
await this.destroyListener();
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { ModuleLoadType, DataSourceManagerConfigOption } from '../interface';
|
|
1
|
+
import { ModuleLoadType, DataSourceManagerConfigOption, IDataSourceManager, BaseDataSourceManagerConfigOption } from '../interface';
|
|
2
2
|
import { MidwayEnvironmentService } from '../service/environmentService';
|
|
3
3
|
import { MidwayPriorityManager } from './priorityManager';
|
|
4
|
-
export declare abstract class DataSourceManager<T, ConnectionOpts extends Record<string, any> = Record<string, any
|
|
4
|
+
export declare abstract class DataSourceManager<T, ConnectionOpts extends BaseDataSourceManagerConfigOption<Record<string, any>, ENTITY_CONFIG_KEY> = BaseDataSourceManagerConfigOption<Record<string, any>, 'entities'>, ENTITY_CONFIG_KEY extends string = 'entities'> implements IDataSourceManager<T, ConnectionOpts> {
|
|
5
5
|
protected dataSource: Map<string, T>;
|
|
6
|
-
protected options: DataSourceManagerConfigOption<ConnectionOpts>;
|
|
6
|
+
protected options: DataSourceManagerConfigOption<ConnectionOpts, ENTITY_CONFIG_KEY>;
|
|
7
7
|
protected modelMapping: WeakMap<object, any>;
|
|
8
8
|
private innerDefaultDataSourceName;
|
|
9
9
|
protected dataSourcePriority: Record<string, string>;
|
|
10
|
-
|
|
10
|
+
private creatingDataSources;
|
|
11
|
+
protected baseDir: string;
|
|
11
12
|
protected environmentService: MidwayEnvironmentService;
|
|
12
13
|
protected priorityManager: MidwayPriorityManager;
|
|
13
|
-
protected initDataSource(dataSourceConfig: DataSourceManagerConfigOption<ConnectionOpts>, baseDirOrOptions: {
|
|
14
|
-
baseDir
|
|
14
|
+
protected initDataSource(dataSourceConfig: DataSourceManagerConfigOption<ConnectionOpts, ENTITY_CONFIG_KEY>, baseDirOrOptions: {
|
|
15
|
+
baseDir?: string;
|
|
15
16
|
entitiesConfigKey?: string;
|
|
17
|
+
concurrent?: boolean;
|
|
16
18
|
} | string): Promise<void>;
|
|
17
19
|
/**
|
|
18
20
|
* get a data source instance
|
|
@@ -31,8 +33,15 @@ export declare abstract class DataSourceManager<T, ConnectionOpts extends Record
|
|
|
31
33
|
* @param dataSourceName
|
|
32
34
|
*/
|
|
33
35
|
isConnected(dataSourceName: string): Promise<boolean>;
|
|
34
|
-
createInstance(config:
|
|
36
|
+
createInstance(config: ConnectionOpts): Promise<T | void>;
|
|
37
|
+
createInstance(config: ConnectionOpts, clientName: string, options?: {
|
|
38
|
+
/**
|
|
39
|
+
* @deprecated
|
|
40
|
+
*/
|
|
35
41
|
validateConnection?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated
|
|
44
|
+
*/
|
|
36
45
|
cacheInstance?: boolean | undefined;
|
|
37
46
|
}): Promise<T | void>;
|
|
38
47
|
/**
|
|
@@ -53,7 +62,7 @@ export declare abstract class DataSourceManager<T, ConnectionOpts extends Record
|
|
|
53
62
|
isHighPriority(name: string): boolean;
|
|
54
63
|
isMediumPriority(name: string): boolean;
|
|
55
64
|
isLowPriority(name: string): boolean;
|
|
65
|
+
static formatGlobString(globString: string): string[];
|
|
66
|
+
static globModels(globString: string, baseDir: string, loadMode?: ModuleLoadType): Promise<any[]>;
|
|
56
67
|
}
|
|
57
|
-
export declare function formatGlobString(globString: string): string[];
|
|
58
|
-
export declare function globModels(globString: string, appDir: string, loadMode?: ModuleLoadType): Promise<any[]>;
|
|
59
68
|
//# sourceMappingURL=dataSourceManager.d.ts.map
|
|
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.DataSourceManager = void 0;
|
|
13
13
|
/**
|
|
14
14
|
* 数据源管理器实现
|
|
15
15
|
*/
|
|
@@ -26,12 +26,16 @@ const environmentService_1 = require("../service/environmentService");
|
|
|
26
26
|
const priorityManager_1 = require("./priorityManager");
|
|
27
27
|
const debug = (0, util_1.debuglog)('midway:debug');
|
|
28
28
|
class DataSourceManager {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
dataSource = new Map();
|
|
30
|
+
options = {};
|
|
31
|
+
modelMapping = new WeakMap();
|
|
32
|
+
innerDefaultDataSourceName;
|
|
33
|
+
dataSourcePriority = {};
|
|
34
|
+
// for multi client with initialization
|
|
35
|
+
creatingDataSources = new Map();
|
|
36
|
+
baseDir;
|
|
37
|
+
environmentService;
|
|
38
|
+
priorityManager;
|
|
35
39
|
async initDataSource(dataSourceConfig, baseDirOrOptions) {
|
|
36
40
|
this.options = dataSourceConfig;
|
|
37
41
|
if (!this.options.dataSource) {
|
|
@@ -41,18 +45,18 @@ class DataSourceManager {
|
|
|
41
45
|
baseDirOrOptions = {
|
|
42
46
|
baseDir: baseDirOrOptions,
|
|
43
47
|
entitiesConfigKey: 'entities',
|
|
48
|
+
concurrent: false,
|
|
44
49
|
};
|
|
45
50
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const userEntities = dataSourceOptions[
|
|
51
|
+
const { baseDir = this.baseDir, entitiesConfigKey = 'entities', concurrent, } = baseDirOrOptions;
|
|
52
|
+
const processDataSource = async (dataSourceName, dataSourceOptions) => {
|
|
53
|
+
const userEntities = dataSourceOptions[entitiesConfigKey];
|
|
49
54
|
if (userEntities) {
|
|
50
55
|
const entities = new Set();
|
|
51
|
-
|
|
52
|
-
for (const entity of userEntities) {
|
|
56
|
+
const processEntity = async (entity) => {
|
|
53
57
|
if (typeof entity === 'string') {
|
|
54
58
|
// string will be glob file
|
|
55
|
-
const models = await globModels(entity,
|
|
59
|
+
const models = await DataSourceManager.globModels(entity, baseDir, this.environmentService?.getModuleLoadType());
|
|
56
60
|
for (const model of models) {
|
|
57
61
|
entities.add(model);
|
|
58
62
|
this.modelMapping.set(model, dataSourceName);
|
|
@@ -63,17 +67,33 @@ class DataSourceManager {
|
|
|
63
67
|
entities.add(entity);
|
|
64
68
|
this.modelMapping.set(entity, dataSourceName);
|
|
65
69
|
}
|
|
70
|
+
};
|
|
71
|
+
if (concurrent) {
|
|
72
|
+
await Promise.all(userEntities.map(processEntity));
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
for (const entity of userEntities) {
|
|
76
|
+
await processEntity(entity);
|
|
77
|
+
}
|
|
66
78
|
}
|
|
67
|
-
dataSourceOptions[
|
|
68
|
-
|
|
69
|
-
debug(`[core]: DataManager load ${dataSourceOptions[baseDirOrOptions.entitiesConfigKey].length} models from ${dataSourceName}.`);
|
|
79
|
+
dataSourceOptions[entitiesConfigKey] = Array.from(entities);
|
|
80
|
+
debug(`[core]: DataManager load ${dataSourceOptions[entitiesConfigKey].length} models from ${dataSourceName}.`);
|
|
70
81
|
}
|
|
71
82
|
// create data source
|
|
72
83
|
const opts = {
|
|
73
|
-
cacheInstance: dataSourceConfig.cacheInstance,
|
|
84
|
+
cacheInstance: dataSourceConfig.cacheInstance,
|
|
74
85
|
validateConnection: dataSourceConfig.validateConnection,
|
|
75
86
|
};
|
|
76
|
-
|
|
87
|
+
return this.createInstance(dataSourceOptions, dataSourceName, opts);
|
|
88
|
+
};
|
|
89
|
+
const entries = Object.entries(dataSourceConfig.dataSource);
|
|
90
|
+
if (concurrent) {
|
|
91
|
+
await Promise.all(entries.map(([name, options]) => processDataSource(name, options)));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
for (const [name, options] of entries) {
|
|
95
|
+
await processDataSource(name, options);
|
|
96
|
+
}
|
|
77
97
|
}
|
|
78
98
|
}
|
|
79
99
|
/**
|
|
@@ -105,26 +125,59 @@ class DataSourceManager {
|
|
|
105
125
|
return inst ? this.checkConnected(inst) : false;
|
|
106
126
|
}
|
|
107
127
|
async createInstance(config, clientName, options) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (cache && clientName && client) {
|
|
116
|
-
this.dataSource.set(clientName, client);
|
|
128
|
+
if (clientName && typeof clientName !== 'string') {
|
|
129
|
+
options = clientName;
|
|
130
|
+
clientName = undefined;
|
|
131
|
+
}
|
|
132
|
+
if (clientName && options && options.cacheInstance === false) {
|
|
133
|
+
// 后面就用传不传 clientName 来判断是否缓存
|
|
134
|
+
clientName = undefined;
|
|
117
135
|
}
|
|
118
|
-
if (
|
|
119
|
-
if (
|
|
120
|
-
|
|
136
|
+
if (clientName) {
|
|
137
|
+
if (this.dataSource.has(clientName)) {
|
|
138
|
+
return this.dataSource.get(clientName);
|
|
121
139
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
140
|
+
if (this.creatingDataSources.has(clientName)) {
|
|
141
|
+
return this.creatingDataSources.get(clientName);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const validateConnection = config.validateConnection ??
|
|
145
|
+
(options && options.validateConnection) ??
|
|
146
|
+
false;
|
|
147
|
+
// options.clients[id] will be merged with options.default
|
|
148
|
+
const configNow = (0, extend_1.extend)(true, {}, this.options['default'], config);
|
|
149
|
+
const clientCreatingPromise = this.createDataSource(configNow, clientName);
|
|
150
|
+
if (clientCreatingPromise && types_1.Types.isPromise(clientCreatingPromise)) {
|
|
151
|
+
if (clientName) {
|
|
152
|
+
this.creatingDataSources.set(clientName, clientCreatingPromise);
|
|
125
153
|
}
|
|
154
|
+
return clientCreatingPromise
|
|
155
|
+
.then(async (client) => {
|
|
156
|
+
if (clientName) {
|
|
157
|
+
this.dataSource.set(clientName, client);
|
|
158
|
+
}
|
|
159
|
+
if (validateConnection) {
|
|
160
|
+
if (!client) {
|
|
161
|
+
throw new error_1.MidwayCommonError(`[DataSourceManager] ${clientName} initialization failed.`);
|
|
162
|
+
}
|
|
163
|
+
const connected = await this.checkConnected(client);
|
|
164
|
+
if (!connected) {
|
|
165
|
+
throw new error_1.MidwayCommonError(`[DataSourceManager] ${clientName} is not connected.`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return client;
|
|
169
|
+
})
|
|
170
|
+
.finally(() => {
|
|
171
|
+
if (clientName) {
|
|
172
|
+
this.creatingDataSources.delete(clientName);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// 处理同步返回的情况
|
|
177
|
+
if (clientName) {
|
|
178
|
+
this.dataSource.set(clientName, clientCreatingPromise);
|
|
126
179
|
}
|
|
127
|
-
return
|
|
180
|
+
return clientCreatingPromise;
|
|
128
181
|
}
|
|
129
182
|
/**
|
|
130
183
|
* get data source name by model or repository
|
|
@@ -171,12 +224,53 @@ class DataSourceManager {
|
|
|
171
224
|
isLowPriority(name) {
|
|
172
225
|
return this.priorityManager.isLowPriority(this.dataSourcePriority[name]);
|
|
173
226
|
}
|
|
227
|
+
static formatGlobString(globString) {
|
|
228
|
+
let pattern;
|
|
229
|
+
if (!/^\*/.test(globString)) {
|
|
230
|
+
globString = '/' + globString;
|
|
231
|
+
}
|
|
232
|
+
const parsePattern = (0, path_1.parse)(globString);
|
|
233
|
+
if (parsePattern.base &&
|
|
234
|
+
(/\*/.test(parsePattern.base) || parsePattern.ext)) {
|
|
235
|
+
pattern = [globString];
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
pattern = [...constants_1.DEFAULT_PATTERN.map(p => (0, path_1.join)(globString, p))];
|
|
239
|
+
}
|
|
240
|
+
return pattern;
|
|
241
|
+
}
|
|
242
|
+
static async globModels(globString, baseDir, loadMode) {
|
|
243
|
+
const pattern = this.formatGlobString(globString);
|
|
244
|
+
const models = [];
|
|
245
|
+
// string will be glob file
|
|
246
|
+
const files = (0, glob_1.run)(pattern, {
|
|
247
|
+
cwd: baseDir,
|
|
248
|
+
ignore: constants_1.IGNORE_PATTERN,
|
|
249
|
+
});
|
|
250
|
+
for (const file of files) {
|
|
251
|
+
const exports = await (0, util_2.loadModule)(file, {
|
|
252
|
+
loadMode,
|
|
253
|
+
});
|
|
254
|
+
if (types_1.Types.isClass(exports)) {
|
|
255
|
+
models.push(exports);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
for (const m in exports) {
|
|
259
|
+
const module = exports[m];
|
|
260
|
+
if (types_1.Types.isClass(module)) {
|
|
261
|
+
models.push(module);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return models;
|
|
267
|
+
}
|
|
174
268
|
}
|
|
175
269
|
exports.DataSourceManager = DataSourceManager;
|
|
176
270
|
__decorate([
|
|
177
271
|
(0, decorator_1.Inject)(),
|
|
178
272
|
__metadata("design:type", String)
|
|
179
|
-
], DataSourceManager.prototype, "
|
|
273
|
+
], DataSourceManager.prototype, "baseDir", void 0);
|
|
180
274
|
__decorate([
|
|
181
275
|
(0, decorator_1.Inject)(),
|
|
182
276
|
__metadata("design:type", environmentService_1.MidwayEnvironmentService)
|
|
@@ -185,46 +279,4 @@ __decorate([
|
|
|
185
279
|
(0, decorator_1.Inject)(),
|
|
186
280
|
__metadata("design:type", priorityManager_1.MidwayPriorityManager)
|
|
187
281
|
], DataSourceManager.prototype, "priorityManager", void 0);
|
|
188
|
-
function formatGlobString(globString) {
|
|
189
|
-
let pattern;
|
|
190
|
-
if (!/^\*/.test(globString)) {
|
|
191
|
-
globString = '/' + globString;
|
|
192
|
-
}
|
|
193
|
-
const parsePattern = (0, path_1.parse)(globString);
|
|
194
|
-
if (parsePattern.base && (/\*/.test(parsePattern.base) || parsePattern.ext)) {
|
|
195
|
-
pattern = [globString];
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
pattern = [...constants_1.DEFAULT_PATTERN.map(p => (0, path_1.join)(globString, p))];
|
|
199
|
-
}
|
|
200
|
-
return pattern;
|
|
201
|
-
}
|
|
202
|
-
exports.formatGlobString = formatGlobString;
|
|
203
|
-
async function globModels(globString, appDir, loadMode) {
|
|
204
|
-
const pattern = formatGlobString(globString);
|
|
205
|
-
const models = [];
|
|
206
|
-
// string will be glob file
|
|
207
|
-
const files = (0, glob_1.run)(pattern, {
|
|
208
|
-
cwd: appDir,
|
|
209
|
-
ignore: constants_1.IGNORE_PATTERN,
|
|
210
|
-
});
|
|
211
|
-
for (const file of files) {
|
|
212
|
-
const exports = await (0, util_2.loadModule)(file, {
|
|
213
|
-
loadMode,
|
|
214
|
-
});
|
|
215
|
-
if (types_1.Types.isClass(exports)) {
|
|
216
|
-
models.push(exports);
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
for (const m in exports) {
|
|
220
|
-
const module = exports[m];
|
|
221
|
-
if (types_1.Types.isClass(module)) {
|
|
222
|
-
models.push(module);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return models;
|
|
228
|
-
}
|
|
229
|
-
exports.globModels = globModels;
|
|
230
282
|
//# sourceMappingURL=dataSourceManager.js.map
|
|
@@ -10,6 +10,7 @@ const decorator_1 = require("../decorator");
|
|
|
10
10
|
const util_2 = require("util");
|
|
11
11
|
const debug = (0, util_2.debuglog)('midway:debug');
|
|
12
12
|
class AbstractFileDetector {
|
|
13
|
+
options;
|
|
13
14
|
constructor(options) {
|
|
14
15
|
this.options = options;
|
|
15
16
|
}
|
|
@@ -32,10 +33,7 @@ const DEFAULT_IGNORE_PATTERN = [
|
|
|
32
33
|
* CommonJS module loader
|
|
33
34
|
*/
|
|
34
35
|
class CommonJSFileDetector extends AbstractFileDetector {
|
|
35
|
-
|
|
36
|
-
super(...arguments);
|
|
37
|
-
this.duplicateModuleCheckSet = new Map();
|
|
38
|
-
}
|
|
36
|
+
duplicateModuleCheckSet = new Map();
|
|
39
37
|
async run(container, namespace) {
|
|
40
38
|
if (this.getType() === 'commonjs') {
|
|
41
39
|
return this.loadSync(container, namespace);
|
|
@@ -5,14 +5,12 @@ const decorator_1 = require("../decorator");
|
|
|
5
5
|
const util_1 = require("../util");
|
|
6
6
|
const metadataManager_1 = require("../decorator/metadataManager");
|
|
7
7
|
class FilterManager {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.protoMatchList = [];
|
|
15
|
-
}
|
|
8
|
+
errFilterList = [];
|
|
9
|
+
successFilterList = [];
|
|
10
|
+
exceptionMap = new WeakMap();
|
|
11
|
+
defaultErrFilter = undefined;
|
|
12
|
+
matchFnList = [];
|
|
13
|
+
protoMatchList = [];
|
|
16
14
|
useFilter(Filters) {
|
|
17
15
|
if (!Array.isArray(Filters)) {
|
|
18
16
|
Filters = [Filters];
|
|
@@ -5,9 +5,7 @@ class LoggerFactory {
|
|
|
5
5
|
}
|
|
6
6
|
exports.LoggerFactory = LoggerFactory;
|
|
7
7
|
class DefaultConsoleLoggerFactory {
|
|
8
|
-
|
|
9
|
-
this.instance = new Map();
|
|
10
|
-
}
|
|
8
|
+
instance = new Map();
|
|
11
9
|
createLogger(name, options) {
|
|
12
10
|
this.instance.set(name, console);
|
|
13
11
|
return console;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ContextMiddlewareManager = void 0;
|
|
4
|
+
exports.createMiddleware = createMiddleware;
|
|
4
5
|
class ContextMiddlewareManager extends Array {
|
|
5
6
|
/**
|
|
6
7
|
* insert a middleware or middleware array to first
|
|
@@ -210,5 +211,4 @@ function createMiddleware(middleware, options, name) {
|
|
|
210
211
|
name,
|
|
211
212
|
};
|
|
212
213
|
}
|
|
213
|
-
exports.createMiddleware = createMiddleware;
|
|
214
214
|
//# sourceMappingURL=middlewareManager.js.map
|