@midwayjs/core 3.19.0 → 4.0.0-alpha.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.
Files changed (134) hide show
  1. package/dist/baseFramework.d.ts +7 -35
  2. package/dist/baseFramework.js +10 -52
  3. package/dist/common/applicationManager.d.ts +6 -6
  4. package/dist/common/applicationManager.js +18 -35
  5. package/dist/common/asyncContextManager.d.ts +15 -0
  6. package/dist/common/asyncContextManager.js +51 -2
  7. package/dist/common/dataListener.js +1 -1
  8. package/dist/common/dataSourceManager.js +3 -4
  9. package/dist/common/fileDetector.d.ts +9 -9
  10. package/dist/common/fileDetector.js +30 -28
  11. package/dist/common/filterManager.js +5 -4
  12. package/dist/common/guardManager.js +3 -2
  13. package/dist/common/middlewareManager.js +4 -3
  14. package/dist/common/performanceManager.js +1 -1
  15. package/dist/common/priorityManager.js +2 -2
  16. package/dist/common/serviceFactory.js +1 -1
  17. package/dist/common/webGenerator.js +4 -6
  18. package/dist/config/config.default.js +1 -1
  19. package/dist/constants.d.ts +2 -32
  20. package/dist/constants.js +3 -33
  21. package/dist/context/componentLoader.d.ts +20 -0
  22. package/dist/context/componentLoader.js +193 -0
  23. package/dist/context/container.d.ts +14 -29
  24. package/dist/context/container.js +68 -306
  25. package/dist/context/definitionRegistry.d.ts +3 -0
  26. package/dist/context/definitionRegistry.js +8 -15
  27. package/dist/context/managedResolverFactory.d.ts +15 -40
  28. package/dist/context/managedResolverFactory.js +263 -348
  29. package/dist/context/requestContainer.d.ts +22 -12
  30. package/dist/context/requestContainer.js +43 -51
  31. package/dist/decorator/common/aspect.js +4 -4
  32. package/dist/decorator/common/autoload.js +1 -1
  33. package/dist/decorator/common/configuration.d.ts +1 -24
  34. package/dist/decorator/common/configuration.js +6 -1
  35. package/dist/decorator/common/filter.js +6 -6
  36. package/dist/decorator/common/framework.d.ts +46 -3
  37. package/dist/decorator/common/framework.js +43 -9
  38. package/dist/decorator/common/guard.js +4 -9
  39. package/dist/decorator/common/inject.d.ts +4 -2
  40. package/dist/decorator/common/inject.js +87 -4
  41. package/dist/decorator/common/mock.js +1 -1
  42. package/dist/decorator/common/objectDef.d.ts +0 -5
  43. package/dist/decorator/common/objectDef.js +8 -20
  44. package/dist/decorator/common/provide.d.ts +1 -1
  45. package/dist/decorator/common/provide.js +1 -1
  46. package/dist/decorator/common/scope.d.ts +6 -0
  47. package/dist/decorator/common/scope.js +21 -0
  48. package/dist/decorator/constant.d.ts +14 -17
  49. package/dist/decorator/constant.js +23 -35
  50. package/dist/decorator/decoratorManager.d.ts +21 -294
  51. package/dist/decorator/decoratorManager.js +127 -694
  52. package/dist/decorator/faas/serverlessTrigger.js +5 -5
  53. package/dist/decorator/index.d.ts +2 -3
  54. package/dist/decorator/index.js +6 -4
  55. package/dist/decorator/metadataManager.d.ts +127 -0
  56. package/dist/decorator/metadataManager.js +465 -0
  57. package/dist/decorator/microservice/consumer.js +3 -2
  58. package/dist/decorator/microservice/kafkaListener.js +2 -1
  59. package/dist/decorator/microservice/provider.js +6 -5
  60. package/dist/decorator/microservice/rabbitmqListener.js +2 -1
  61. package/dist/decorator/task/queue.js +3 -2
  62. package/dist/decorator/task/schedule.js +3 -2
  63. package/dist/decorator/task/task.js +4 -3
  64. package/dist/decorator/task/taskLocal.js +4 -3
  65. package/dist/decorator/web/controller.js +3 -2
  66. package/dist/decorator/web/paramMapping.js +2 -2
  67. package/dist/decorator/web/requestMapping.js +5 -5
  68. package/dist/decorator/web/response.js +6 -5
  69. package/dist/decorator/ws/webSocketController.js +3 -2
  70. package/dist/decorator/ws/webSocketEvent.js +7 -6
  71. package/dist/definitions/functionDefinition.d.ts +3 -3
  72. package/dist/definitions/functionDefinition.js +12 -11
  73. package/dist/definitions/objectCreator.d.ts +5 -11
  74. package/dist/definitions/objectCreator.js +3 -27
  75. package/dist/definitions/objectDefinition.d.ts +2 -3
  76. package/dist/definitions/objectDefinition.js +1 -3
  77. package/dist/error/base.js +2 -2
  78. package/dist/error/framework.d.ts +1 -8
  79. package/dist/error/framework.js +9 -25
  80. package/dist/error/http.js +1 -1
  81. package/dist/functional/configuration.d.ts +14 -15
  82. package/dist/functional/configuration.js +37 -47
  83. package/dist/functional/hooks.d.ts +10 -0
  84. package/dist/functional/hooks.js +68 -0
  85. package/dist/functional/index.d.ts +3 -0
  86. package/dist/functional/index.js +22 -0
  87. package/dist/index.d.ts +3 -3
  88. package/dist/index.js +3 -6
  89. package/dist/interface.d.ts +80 -114
  90. package/dist/interface.js +9 -33
  91. package/dist/legacy/constants.d.ts +29 -0
  92. package/dist/legacy/constants.js +33 -0
  93. package/dist/legacy/decorator.d.ts +255 -0
  94. package/dist/legacy/decorator.js +468 -0
  95. package/dist/legacy/index.d.ts +3 -0
  96. package/dist/legacy/index.js +19 -0
  97. package/dist/legacy/types.d.ts +2 -0
  98. package/dist/legacy/types.js +3 -0
  99. package/dist/response/base.d.ts +1 -0
  100. package/dist/response/http.d.ts +1 -0
  101. package/dist/service/aspectService.js +11 -12
  102. package/dist/service/configService.js +2 -2
  103. package/dist/service/decoratorService.d.ts +3 -3
  104. package/dist/service/decoratorService.js +14 -10
  105. package/dist/service/environmentService.js +2 -2
  106. package/dist/service/frameworkService.d.ts +5 -4
  107. package/dist/service/frameworkService.js +30 -28
  108. package/dist/service/healthService.js +4 -4
  109. package/dist/service/informationService.js +3 -4
  110. package/dist/service/lifeCycleService.js +6 -17
  111. package/dist/service/loggerService.js +3 -4
  112. package/dist/service/middlewareService.js +7 -8
  113. package/dist/service/mockService.js +9 -15
  114. package/dist/service/slsFunctionService.d.ts +1 -14
  115. package/dist/service/slsFunctionService.js +33 -81
  116. package/dist/service/webRouterService.js +11 -11
  117. package/dist/setup.d.ts +5 -5
  118. package/dist/setup.js +75 -93
  119. package/dist/util/contextUtil.d.ts +2 -2
  120. package/dist/util/httpclient.d.ts +3 -2
  121. package/dist/util/index.d.ts +14 -0
  122. package/dist/util/index.js +159 -25
  123. package/dist/util/pathFileUtil.d.ts +14 -1
  124. package/dist/util/pathFileUtil.js +27 -6
  125. package/dist/util/webRouterParam.js +2 -2
  126. package/package.json +24 -6
  127. package/dist/decorator/common/pipeline.d.ts +0 -3
  128. package/dist/decorator/common/pipeline.js +0 -12
  129. package/dist/decorator/rpc/hsf.d.ts +0 -13
  130. package/dist/decorator/rpc/hsf.js +0 -20
  131. package/dist/definitions/properties.d.ts +0 -7
  132. package/dist/definitions/properties.js +0 -19
  133. package/dist/service/pipelineService.d.ts +0 -168
  134. package/dist/service/pipelineService.js +0 -254
@@ -1,4 +1,4 @@
1
- import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework, CommonFilterUnion, MiddlewareRespond, CommonGuardUnion, ILogger, MidwayLoggerOptions } from './interface';
1
+ import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContext, IMidwayFramework, CommonFilterUnion, MiddlewareRespond, CommonGuardUnion, ILogger, MidwayLoggerOptions, IMidwayGlobalContainer } from './interface';
2
2
  import { MidwayEnvironmentService } from './service/environmentService';
3
3
  import { MidwayConfigService } from './service/configService';
4
4
  import { MidwayInformationService } from './service/informationService';
@@ -10,7 +10,7 @@ import { MidwayMockService } from './service/mockService';
10
10
  import { AsyncContextManager } from './common/asyncContextManager';
11
11
  import { GuardManager } from './common/guardManager';
12
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> {
13
- readonly applicationContext: IMidwayContainer;
13
+ readonly applicationContext: IMidwayGlobalContainer;
14
14
  app: APP;
15
15
  configurationOptions: OPT;
16
16
  protected logger: ILogger;
@@ -31,51 +31,23 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
31
31
  informationService: MidwayInformationService;
32
32
  middlewareService: MidwayMiddlewareService<CTX, ResOrNext, Next>;
33
33
  mockService: MidwayMockService;
34
- constructor(applicationContext: IMidwayContainer);
34
+ constructor(applicationContext: IMidwayGlobalContainer);
35
35
  init(): Promise<this>;
36
- abstract configure(options?: OPT): any;
36
+ abstract configure(options?: OPT): OPT;
37
+ abstract applicationInitialize(options: IMidwayBootstrapOptions): void | Promise<void>;
38
+ abstract run(): Promise<void>;
37
39
  isEnable(): boolean;
38
40
  initialize(options?: IMidwayBootstrapOptions): Promise<void>;
39
- /**
40
- * @deprecated
41
- */
42
- protected containerInitialize(options: IMidwayBootstrapOptions): Promise<void>;
43
- /**
44
- * @deprecated
45
- */
46
- protected containerDirectoryLoad(options: IMidwayBootstrapOptions): Promise<void>;
47
- /**
48
- * @deprecated
49
- */
50
- protected containerReady(options: IMidwayBootstrapOptions): Promise<void>;
51
- getApplicationContext(): IMidwayContainer;
41
+ getApplicationContext(): IMidwayGlobalContainer;
52
42
  getConfiguration(key?: string): any;
53
43
  getCurrentEnvironment(): string;
54
44
  getApplication(): APP;
55
- abstract applicationInitialize(options: IMidwayBootstrapOptions): any;
56
- abstract run(): Promise<void>;
57
45
  protected createContextLogger(ctx: CTX, name?: string): ILogger;
58
46
  stop(): Promise<void>;
59
47
  getAppDir(): string;
60
48
  getBaseDir(): string;
61
49
  protected defineApplicationProperties(applicationProperties?: {}, whiteList?: string[]): void;
62
50
  protected beforeStop(): Promise<void>;
63
- /**
64
- * @deprecated
65
- */
66
- protected beforeContainerInitialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
67
- /**
68
- * @deprecated
69
- */
70
- protected afterContainerInitialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
71
- /**
72
- * @deprecated
73
- */
74
- protected afterContainerDirectoryLoad(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
75
- /**
76
- * @deprecated
77
- */
78
- protected afterContainerReady(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
79
51
  applyMiddleware<R, N>(lastMiddleware?: CommonMiddlewareUnion<CTX, R, N>): Promise<MiddlewareRespond<CTX, R, N>>;
80
52
  getLogger(name?: string): any;
81
53
  getCoreLogger(): ILogger;
@@ -36,10 +36,9 @@ class BaseFramework {
36
36
  this.composeMiddleware = null;
37
37
  }
38
38
  async init() {
39
- var _a, _b;
40
- this.configurationOptions = (_a = this.configure()) !== null && _a !== void 0 ? _a : {};
39
+ this.configurationOptions = this.configure() ?? {};
41
40
  this.contextLoggerApplyLogger =
42
- (_b = this.configurationOptions.contextLoggerApplyLogger) !== null && _b !== void 0 ? _b : 'appLogger';
41
+ this.configurationOptions.contextLoggerApplyLogger ?? 'appLogger';
43
42
  this.contextLoggerFormat = this.configurationOptions.contextLoggerFormat;
44
43
  this.logger = this.loggerService.getLogger('coreLogger');
45
44
  this.appLogger = this.loggerService.getLogger('appLogger');
@@ -50,34 +49,17 @@ class BaseFramework {
50
49
  }
51
50
  async initialize(options) {
52
51
  this.bootstrapOptions = options;
53
- await this.beforeContainerInitialize(options);
54
- await this.containerInitialize(options);
55
- await this.afterContainerInitialize(options);
56
- await this.containerDirectoryLoad(options);
57
- await this.afterContainerDirectoryLoad(options);
58
52
  /**
59
53
  * Third party application initialization
60
54
  */
61
55
  await this.applicationInitialize(options);
62
- await this.containerReady(options);
63
- await this.afterContainerReady(options);
64
- await this.mockService.runSimulatorAppSetup(this.app);
65
- }
66
- /**
67
- * @deprecated
68
- */
69
- async containerInitialize(options) { }
70
- /**
71
- * @deprecated
72
- */
73
- async containerDirectoryLoad(options) { }
74
- /**
75
- * @deprecated
76
- */
77
- async containerReady(options) {
56
+ /**
57
+ * define application properties if not exists
58
+ */
78
59
  if (!this.app.getApplicationContext) {
79
60
  this.defineApplicationProperties();
80
61
  }
62
+ await this.mockService.runSimulatorAppSetup(this.app);
81
63
  }
82
64
  getApplicationContext() {
83
65
  return this.applicationContext;
@@ -109,7 +91,7 @@ class BaseFramework {
109
91
  return ctxLogger;
110
92
  }
111
93
  else {
112
- const appLogger = this.getLogger(name !== null && name !== void 0 ? name : this.contextLoggerApplyLogger);
94
+ const appLogger = this.getLogger(name ?? this.contextLoggerApplyLogger);
113
95
  // avoid maximum call stack size exceeded
114
96
  if (ctx['_logger']) {
115
97
  return ctx['_logger'];
@@ -147,11 +129,6 @@ class BaseFramework {
147
129
  getConfig: (key) => {
148
130
  return this.getConfiguration(key);
149
131
  },
150
- getFrameworkType: () => {
151
- if (this['getFrameworkType']) {
152
- return this['getFrameworkType']();
153
- }
154
- },
155
132
  getProcessType: () => {
156
133
  return interface_1.MidwayProcessTypeEnum.APPLICATION;
157
134
  },
@@ -180,7 +157,6 @@ class BaseFramework {
180
157
  }
181
158
  if (!ctx.requestContext) {
182
159
  ctx.requestContext = new requestContainer_1.MidwayRequestContainer(ctx, this.getApplicationContext());
183
- ctx.requestContext.ready();
184
160
  }
185
161
  if (!ctx.getLogger) {
186
162
  ctx.getLogger = name => {
@@ -229,30 +205,13 @@ class BaseFramework {
229
205
  Object.assign(this.app, defaultApplicationProperties, applicationProperties);
230
206
  }
231
207
  async beforeStop() { }
232
- /**
233
- * @deprecated
234
- */
235
- async beforeContainerInitialize(options) { }
236
- /**
237
- * @deprecated
238
- */
239
- async afterContainerInitialize(options) { }
240
- /**
241
- * @deprecated
242
- */
243
- async afterContainerDirectoryLoad(options) { }
244
- /**
245
- * @deprecated
246
- */
247
- async afterContainerReady(options) { }
248
208
  async applyMiddleware(lastMiddleware) {
249
- var _a;
250
209
  if (!this.composeMiddleware) {
251
210
  if (!this.applicationContext.hasObject(constants_1.ASYNC_CONTEXT_MANAGER_KEY)) {
252
211
  const asyncContextManagerEnabled = this.configService.getConfiguration('asyncContextManager.enable') ||
253
212
  false;
254
213
  const contextManager = asyncContextManagerEnabled
255
- ? ((_a = this.bootstrapOptions) === null || _a === void 0 ? void 0 : _a.asyncContextManager) ||
214
+ ? this.bootstrapOptions?.asyncContextManager ||
256
215
  new asyncContextManager_1.NoopContextManager()
257
216
  : new asyncContextManager_1.NoopContextManager();
258
217
  if (asyncContextManagerEnabled) {
@@ -299,8 +258,7 @@ class BaseFramework {
299
258
  return this.composeMiddleware;
300
259
  }
301
260
  getLogger(name) {
302
- var _a;
303
- return (_a = this.loggerService.getLogger(name)) !== null && _a !== void 0 ? _a : this.appLogger;
261
+ return this.loggerService.getLogger(name) ?? this.appLogger;
304
262
  }
305
263
  getCoreLogger() {
306
264
  return this.logger;
@@ -345,6 +303,7 @@ class BaseFramework {
345
303
  return this.namespace;
346
304
  }
347
305
  }
306
+ exports.BaseFramework = BaseFramework;
348
307
  __decorate([
349
308
  (0, decorator_1.Inject)(),
350
309
  __metadata("design:type", loggerService_1.MidwayLoggerService)
@@ -375,5 +334,4 @@ __decorate([
375
334
  __metadata("design:paramtypes", []),
376
335
  __metadata("design:returntype", Promise)
377
336
  ], BaseFramework.prototype, "init", null);
378
- exports.BaseFramework = BaseFramework;
379
337
  //# sourceMappingURL=baseFramework.js.map
@@ -1,10 +1,10 @@
1
- import { IMidwayApplication, IMidwayFramework, FrameworkType } from '../interface';
1
+ import { IMidwayApplication, IMidwayFramework } from '../interface';
2
2
  export declare class MidwayApplicationManager {
3
3
  private globalFrameworkMap;
4
- private globalFrameworkTypeMap;
5
- addFramework(namespace: any, framework: IMidwayFramework<any, any, any>): void;
6
- getFramework(namespaceOrFrameworkType: string | FrameworkType): IMidwayFramework<any, any, any, unknown, unknown>;
7
- getApplication(namespaceOrFrameworkType: string | FrameworkType): IMidwayApplication;
8
- getApplications(namespaces?: Array<string | FrameworkType>): IMidwayApplication[];
4
+ addFramework(frameworkNameOrNamespace: string, framework: IMidwayFramework<any, any, any>): void;
5
+ hasFramework(frameworkNameOrNamespace: string): boolean;
6
+ getFramework(frameworkNameOrNamespace: string): IMidwayFramework<any, any, any, unknown, unknown>;
7
+ getApplication(frameworkNameOrNamespace: string): IMidwayApplication;
8
+ getApplications(frameworkNameOrNamespace?: Array<string>): IMidwayApplication[];
9
9
  }
10
10
  //# sourceMappingURL=applicationManager.d.ts.map
@@ -12,50 +12,33 @@ const decorator_1 = require("../decorator");
12
12
  let MidwayApplicationManager = class MidwayApplicationManager {
13
13
  constructor() {
14
14
  this.globalFrameworkMap = new Map();
15
- this.globalFrameworkTypeMap = new WeakMap();
16
15
  }
17
- addFramework(namespace, framework) {
18
- this.globalFrameworkMap.set(namespace, framework);
19
- if (framework['getFrameworkType']) {
20
- this.globalFrameworkTypeMap.set(framework['getFrameworkType'](), framework);
21
- }
16
+ addFramework(frameworkNameOrNamespace, framework) {
17
+ this.globalFrameworkMap.set(frameworkNameOrNamespace, framework);
22
18
  }
23
- getFramework(namespaceOrFrameworkType) {
24
- if (typeof namespaceOrFrameworkType === 'string') {
25
- if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
26
- return this.globalFrameworkMap.get(namespaceOrFrameworkType);
27
- }
28
- }
29
- else {
30
- if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
31
- return this.globalFrameworkTypeMap.get(namespaceOrFrameworkType);
32
- }
33
- }
19
+ hasFramework(frameworkNameOrNamespace) {
20
+ return this.globalFrameworkMap.has(frameworkNameOrNamespace);
34
21
  }
35
- getApplication(namespaceOrFrameworkType) {
36
- if (typeof namespaceOrFrameworkType === 'string') {
37
- if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
38
- return this.globalFrameworkMap
39
- .get(namespaceOrFrameworkType)
40
- .getApplication();
41
- }
22
+ getFramework(frameworkNameOrNamespace) {
23
+ if (this.globalFrameworkMap.has(frameworkNameOrNamespace)) {
24
+ return this.globalFrameworkMap.get(frameworkNameOrNamespace);
42
25
  }
43
- else {
44
- if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
45
- return this.globalFrameworkTypeMap
46
- .get(namespaceOrFrameworkType)
47
- .getApplication();
48
- }
26
+ }
27
+ getApplication(frameworkNameOrNamespace) {
28
+ if (this.globalFrameworkMap.has(frameworkNameOrNamespace)) {
29
+ return this.globalFrameworkMap
30
+ .get(frameworkNameOrNamespace)
31
+ .getApplication();
49
32
  }
50
33
  }
51
- getApplications(namespaces) {
52
- if (!namespaces) {
34
+ getApplications(frameworkNameOrNamespace) {
35
+ if (!frameworkNameOrNamespace) {
53
36
  return Array.from(this.globalFrameworkMap.values()).map(framework => {
54
37
  return framework.getApplication();
55
38
  });
56
39
  }
57
40
  else {
58
- return namespaces
41
+ return frameworkNameOrNamespace
59
42
  .map(namespace => {
60
43
  return this.getApplication(namespace);
61
44
  })
@@ -65,9 +48,9 @@ let MidwayApplicationManager = class MidwayApplicationManager {
65
48
  }
66
49
  }
67
50
  };
68
- MidwayApplicationManager = __decorate([
51
+ exports.MidwayApplicationManager = MidwayApplicationManager;
52
+ exports.MidwayApplicationManager = MidwayApplicationManager = __decorate([
69
53
  (0, decorator_1.Provide)(),
70
54
  (0, decorator_1.Scope)(interface_1.ScopeEnum.Singleton)
71
55
  ], MidwayApplicationManager);
72
- exports.MidwayApplicationManager = MidwayApplicationManager;
73
56
  //# sourceMappingURL=applicationManager.js.map
@@ -58,4 +58,19 @@ export declare class NoopContextManager implements AsyncContextManager {
58
58
  enable(): this;
59
59
  disable(): this;
60
60
  }
61
+ export declare class AsyncLocalStorageContextManager implements AsyncContextManager {
62
+ private _asyncLocalStorage;
63
+ constructor();
64
+ active(): AsyncContext;
65
+ with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(context: AsyncContext, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
66
+ enable(): this;
67
+ disable(): this;
68
+ /**
69
+ * Binds a the certain context or the active one to the target function and then returns the target
70
+ * @param context A context (span) to be bind to target
71
+ * @param target a function. When target or one of its callbacks is called,
72
+ * the provided context will be used as the active context for the duration of the call.
73
+ */
74
+ bind<T>(context: AsyncContext, target: T): T;
75
+ }
61
76
  //# sourceMappingURL=asyncContextManager.d.ts.map
@@ -1,4 +1,6 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncLocalStorageContextManager = exports.NoopContextManager = exports.ASYNC_ROOT_CONTEXT = void 0;
2
4
  /*
3
5
  * Copyright The OpenTelemetry Authors
4
6
  *
@@ -14,8 +16,7 @@
14
16
  * See the License for the specific language governing permissions and
15
17
  * limitations under the License.
16
18
  */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.NoopContextManager = exports.ASYNC_ROOT_CONTEXT = void 0;
19
+ const async_hooks_1 = require("async_hooks");
19
20
  class AsyncBaseContext {
20
21
  /**
21
22
  * Construct a new context which inherits values from an optional parent context.
@@ -60,4 +61,52 @@ class NoopContextManager {
60
61
  }
61
62
  }
62
63
  exports.NoopContextManager = NoopContextManager;
64
+ class AsyncLocalStorageContextManager {
65
+ constructor() {
66
+ this._asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
67
+ }
68
+ active() {
69
+ return this._asyncLocalStorage.getStore() ?? exports.ASYNC_ROOT_CONTEXT;
70
+ }
71
+ with(context, fn, thisArg, ...args) {
72
+ const cb = thisArg == null ? fn : fn.bind(thisArg);
73
+ return this._asyncLocalStorage.run(context, cb, ...args);
74
+ }
75
+ enable() {
76
+ return this;
77
+ }
78
+ disable() {
79
+ this._asyncLocalStorage.disable();
80
+ return this;
81
+ }
82
+ /**
83
+ * Binds a the certain context or the active one to the target function and then returns the target
84
+ * @param context A context (span) to be bind to target
85
+ * @param target a function. When target or one of its callbacks is called,
86
+ * the provided context will be used as the active context for the duration of the call.
87
+ */
88
+ bind(context, target) {
89
+ if (typeof target === 'function') {
90
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
91
+ const manager = this;
92
+ const contextWrapper = function (...args) {
93
+ return manager.with(context, () => target.apply(this, args));
94
+ };
95
+ Object.defineProperty(contextWrapper, 'length', {
96
+ enumerable: false,
97
+ configurable: true,
98
+ writable: false,
99
+ value: target.length,
100
+ });
101
+ /**
102
+ * It isn't possible to tell Typescript that contextWrapper is the same as T
103
+ * so we forced to cast as any here.
104
+ */
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ return contextWrapper;
107
+ }
108
+ return target;
109
+ }
110
+ }
111
+ exports.AsyncLocalStorageContextManager = AsyncLocalStorageContextManager;
63
112
  //# sourceMappingURL=asyncContextManager.js.map
@@ -27,6 +27,7 @@ class DataListener {
27
27
  }
28
28
  async destroyListener() { }
29
29
  }
30
+ exports.DataListener = DataListener;
30
31
  __decorate([
31
32
  (0, decorator_1.Init)(),
32
33
  __metadata("design:type", Function),
@@ -39,5 +40,4 @@ __decorate([
39
40
  __metadata("design:paramtypes", []),
40
41
  __metadata("design:returntype", Promise)
41
42
  ], DataListener.prototype, "stop", null);
42
- exports.DataListener = DataListener;
43
43
  //# sourceMappingURL=dataListener.js.map
@@ -33,7 +33,6 @@ class DataSourceManager {
33
33
  this.dataSourcePriority = {};
34
34
  }
35
35
  async initDataSource(dataSourceConfig, baseDirOrOptions) {
36
- var _a;
37
36
  this.options = dataSourceConfig;
38
37
  if (!this.options.dataSource) {
39
38
  throw new error_1.MidwayParameterError('[DataSourceManager] must set options.dataSource.');
@@ -53,7 +52,7 @@ class DataSourceManager {
53
52
  for (const entity of userEntities) {
54
53
  if (typeof entity === 'string') {
55
54
  // string will be glob file
56
- const models = await globModels(entity, baseDirOrOptions.baseDir, (_a = this.environmentService) === null || _a === void 0 ? void 0 : _a.getModuleLoadType());
55
+ const models = await globModels(entity, baseDirOrOptions.baseDir, this.environmentService?.getModuleLoadType());
57
56
  for (const model of models) {
58
57
  entities.add(model);
59
58
  this.modelMapping.set(model, dataSourceName);
@@ -71,7 +70,7 @@ class DataSourceManager {
71
70
  }
72
71
  // create data source
73
72
  const opts = {
74
- cacheInstance: dataSourceConfig.cacheInstance,
73
+ cacheInstance: dataSourceConfig.cacheInstance, // will default true
75
74
  validateConnection: dataSourceConfig.validateConnection,
76
75
  };
77
76
  await this.createInstance(dataSourceOptions, dataSourceName, opts);
@@ -173,6 +172,7 @@ class DataSourceManager {
173
172
  return this.priorityManager.isLowPriority(this.dataSourcePriority[name]);
174
173
  }
175
174
  }
175
+ exports.DataSourceManager = DataSourceManager;
176
176
  __decorate([
177
177
  (0, decorator_1.Inject)(),
178
178
  __metadata("design:type", String)
@@ -185,7 +185,6 @@ __decorate([
185
185
  (0, decorator_1.Inject)(),
186
186
  __metadata("design:type", priorityManager_1.MidwayPriorityManager)
187
187
  ], DataSourceManager.prototype, "priorityManager", void 0);
188
- exports.DataSourceManager = DataSourceManager;
189
188
  function formatGlobString(globString) {
190
189
  let pattern;
191
190
  if (!/^\*/.test(globString)) {
@@ -1,10 +1,9 @@
1
- import { IFileDetector, IMidwayContainer } from '../interface';
1
+ import { IFileDetector, IMidwayGlobalContainer } from '../interface';
2
2
  export declare abstract class AbstractFileDetector<T> implements IFileDetector {
3
3
  options: T;
4
- extraDetectorOptions: T;
5
4
  constructor(options?: T);
6
- abstract run(container: IMidwayContainer): void | Promise<void>;
7
- setExtraDetectorOptions(detectorOptions: T): void;
5
+ abstract run(container: IMidwayGlobalContainer, namespace: string): Promise<void>;
6
+ abstract runSync(container: IMidwayGlobalContainer, namespace: string): void;
8
7
  }
9
8
  /**
10
9
  * CommonJS module loader
@@ -13,14 +12,14 @@ export declare class CommonJSFileDetector extends AbstractFileDetector<{
13
12
  loadDir?: string | string[];
14
13
  pattern?: string | string[];
15
14
  ignore?: string | string[];
16
- namespace?: string;
17
15
  conflictCheck?: boolean;
18
16
  }> {
19
17
  private duplicateModuleCheckSet;
20
- run(container: any): void | Promise<void>;
21
- loadSync(container: any): void;
22
- loadAsync(container: any): Promise<void>;
18
+ run(container: IMidwayGlobalContainer, namespace: string): Promise<void>;
19
+ loadSync(container: IMidwayGlobalContainer, namespace: string): void;
20
+ loadAsync(container: IMidwayGlobalContainer, namespace: string): Promise<void>;
23
21
  getType(): 'commonjs' | 'module';
22
+ runSync(container: IMidwayGlobalContainer, namespace: string): void;
24
23
  }
25
24
  /**
26
25
  * ES module loader
@@ -32,6 +31,7 @@ export declare class CustomModuleDetector extends AbstractFileDetector<{
32
31
  modules?: any[];
33
32
  namespace?: string;
34
33
  }> {
35
- run(container: any): Promise<void>;
34
+ run(container: IMidwayGlobalContainer): Promise<void>;
35
+ runSync(container: IMidwayGlobalContainer, namespace: string): void;
36
36
  }
37
37
  //# sourceMappingURL=fileDetector.d.ts.map
@@ -5,15 +5,13 @@ const types_1 = require("../util/types");
5
5
  const glob_1 = require("@midwayjs/glob");
6
6
  const error_1 = require("../error");
7
7
  const constants_1 = require("../constants");
8
- const decorator_1 = require("../decorator");
9
8
  const util_1 = require("../util");
9
+ const decorator_1 = require("../decorator");
10
+ const util_2 = require("util");
11
+ const debug = (0, util_2.debuglog)('midway:debug');
10
12
  class AbstractFileDetector {
11
13
  constructor(options) {
12
14
  this.options = options;
13
- this.extraDetectorOptions = {};
14
- }
15
- setExtraDetectorOptions(detectorOptions) {
16
- this.extraDetectorOptions = detectorOptions;
17
15
  }
18
16
  }
19
17
  exports.AbstractFileDetector = AbstractFileDetector;
@@ -38,29 +36,27 @@ class CommonJSFileDetector extends AbstractFileDetector {
38
36
  super(...arguments);
39
37
  this.duplicateModuleCheckSet = new Map();
40
38
  }
41
- run(container) {
39
+ async run(container, namespace) {
42
40
  if (this.getType() === 'commonjs') {
43
- return this.loadSync(container);
41
+ return this.loadSync(container, namespace);
44
42
  }
45
43
  else {
46
- return this.loadAsync(container);
44
+ return this.loadAsync(container, namespace);
47
45
  }
48
46
  }
49
- loadSync(container) {
50
- var _a;
47
+ loadSync(container, namespace) {
51
48
  this.options = this.options || {};
52
- const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
49
+ const loadDirs = [].concat(this.options.loadDir ?? container.get('baseDir'));
53
50
  for (const dir of loadDirs) {
54
- const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
51
+ const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []), {
55
52
  cwd: dir,
56
- ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []).concat(this.extraDetectorOptions.ignore || []),
53
+ ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []),
57
54
  });
55
+ debug(`[core]: load file from ${dir}, files: ${fileResults}`);
58
56
  // 检查重复模块
59
57
  const checkDuplicatedHandler = (module, options) => {
60
- if ((this.options.conflictCheck ||
61
- this.extraDetectorOptions.conflictCheck) &&
62
- types_1.Types.isClass(module)) {
63
- const name = (0, decorator_1.getProviderName)(module);
58
+ if (this.options.conflictCheck && types_1.Types.isClass(module)) {
59
+ const name = decorator_1.DecoratorManager.getProviderName(module);
64
60
  if (name) {
65
61
  if (this.duplicateModuleCheckSet.has(name)) {
66
62
  throw new error_1.MidwayDuplicateClassNameError(name, options.srcPath, this.duplicateModuleCheckSet.get(name));
@@ -75,7 +71,7 @@ class CommonJSFileDetector extends AbstractFileDetector {
75
71
  const exports = require(file);
76
72
  // add module to set
77
73
  container.bindClass(exports, {
78
- namespace: this.options.namespace,
74
+ namespace,
79
75
  srcPath: file,
80
76
  createFrom: 'file',
81
77
  bindHook: checkDuplicatedHandler,
@@ -85,21 +81,19 @@ class CommonJSFileDetector extends AbstractFileDetector {
85
81
  // check end
86
82
  this.duplicateModuleCheckSet.clear();
87
83
  }
88
- async loadAsync(container) {
89
- var _a;
84
+ async loadAsync(container, namespace) {
90
85
  this.options = this.options || {};
91
- const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
86
+ const loadDirs = [].concat(this.options.loadDir ?? container.get('baseDir'));
92
87
  for (const dir of loadDirs) {
93
- const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
88
+ const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []), {
94
89
  cwd: dir,
95
- ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []).concat(this.extraDetectorOptions.ignore || []),
90
+ ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []),
96
91
  });
92
+ debug(`[core]: load file from ${dir}, files: ${fileResults}`);
97
93
  // 检查重复模块
98
94
  const checkDuplicatedHandler = (module, options) => {
99
- if ((this.options.conflictCheck ||
100
- this.extraDetectorOptions.conflictCheck) &&
101
- types_1.Types.isClass(module)) {
102
- const name = (0, decorator_1.getProviderName)(module);
95
+ if (this.options.conflictCheck && types_1.Types.isClass(module)) {
96
+ const name = decorator_1.DecoratorManager.getProviderName(module);
103
97
  if (name) {
104
98
  if (this.duplicateModuleCheckSet.has(name)) {
105
99
  throw new error_1.MidwayDuplicateClassNameError(name, options.srcPath, this.duplicateModuleCheckSet.get(name));
@@ -116,7 +110,7 @@ class CommonJSFileDetector extends AbstractFileDetector {
116
110
  });
117
111
  // add module to set
118
112
  container.bindClass(exports, {
119
- namespace: this.options.namespace,
113
+ namespace,
120
114
  srcPath: file,
121
115
  createFrom: 'file',
122
116
  bindHook: checkDuplicatedHandler,
@@ -129,6 +123,11 @@ class CommonJSFileDetector extends AbstractFileDetector {
129
123
  getType() {
130
124
  return 'commonjs';
131
125
  }
126
+ runSync(container, namespace) {
127
+ if (this.getType() === 'commonjs') {
128
+ return this.loadSync(container, namespace);
129
+ }
130
+ }
132
131
  }
133
132
  exports.CommonJSFileDetector = CommonJSFileDetector;
134
133
  /**
@@ -142,6 +141,9 @@ class ESModuleFileDetector extends CommonJSFileDetector {
142
141
  exports.ESModuleFileDetector = ESModuleFileDetector;
143
142
  class CustomModuleDetector extends AbstractFileDetector {
144
143
  async run(container) {
144
+ this.runSync(container, this.options.namespace);
145
+ }
146
+ runSync(container, namespace) {
145
147
  for (const module of this.options.modules) {
146
148
  container.bindClass(module, {
147
149
  namespace: this.options.namespace,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FilterManager = void 0;
4
4
  const decorator_1 = require("../decorator");
5
5
  const util_1 = require("../util");
6
+ const metadataManager_1 = require("../decorator/metadataManager");
6
7
  class FilterManager {
7
8
  constructor() {
8
9
  this.errFilterList = [];
@@ -17,10 +18,10 @@ class FilterManager {
17
18
  Filters = [Filters];
18
19
  }
19
20
  for (const Filter of Filters) {
20
- if ((0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, Filter)) {
21
+ if (metadataManager_1.MetadataManager.getOwnMetadata(decorator_1.CATCH_KEY, Filter)) {
21
22
  this.errFilterList.push(Filter);
22
23
  }
23
- if ((0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, Filter)) {
24
+ if (metadataManager_1.MetadataManager.getOwnMetadata(decorator_1.MATCH_KEY, Filter)) {
24
25
  this.successFilterList.push(Filter);
25
26
  }
26
27
  }
@@ -29,7 +30,7 @@ class FilterManager {
29
30
  // for catch exception
30
31
  for (const FilterClass of this.errFilterList) {
31
32
  const filter = await applicationContext.getAsync(FilterClass);
32
- const exceptionMetadata = (0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, FilterClass);
33
+ const exceptionMetadata = metadataManager_1.MetadataManager.getOwnMetadata(decorator_1.CATCH_KEY, FilterClass);
33
34
  if (exceptionMetadata && exceptionMetadata.catchTargets) {
34
35
  exceptionMetadata.catchOptions = exceptionMetadata.catchOptions || {};
35
36
  for (const Exception of exceptionMetadata.catchTargets) {
@@ -57,7 +58,7 @@ class FilterManager {
57
58
  // for success return
58
59
  for (const FilterClass of this.successFilterList) {
59
60
  const filter = await applicationContext.getAsync(FilterClass);
60
- const matchMetadata = (0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, FilterClass);
61
+ const matchMetadata = metadataManager_1.MetadataManager.getOwnMetadata(decorator_1.MATCH_KEY, FilterClass);
61
62
  if (matchMetadata && matchMetadata.matchPattern) {
62
63
  this.matchFnList.push({
63
64
  matchFn: (0, util_1.toPathMatch)(matchMetadata.matchPattern),