@midwayjs/core 3.0.0-alpha.6 → 3.0.0-beta.11

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 (113) hide show
  1. package/CHANGELOG.md +137 -0
  2. package/README.md +1 -1
  3. package/dist/baseFramework.d.ts +56 -22
  4. package/dist/baseFramework.js +157 -232
  5. package/dist/common/applicationManager.d.ts +12 -0
  6. package/dist/common/applicationManager.js +66 -0
  7. package/dist/{util → common}/fileDetector.d.ts +0 -0
  8. package/dist/{util → common}/fileDetector.js +16 -10
  9. package/dist/common/filterManager.d.ts +19 -0
  10. package/dist/common/filterManager.js +85 -0
  11. package/dist/common/middlewareManager.d.ts +68 -0
  12. package/dist/common/middlewareManager.js +183 -0
  13. package/dist/common/serviceFactory.d.ts +15 -0
  14. package/dist/{util → common}/serviceFactory.js +16 -6
  15. package/dist/{util → common}/triggerCollector.d.ts +0 -0
  16. package/dist/{util → common}/triggerCollector.js +0 -0
  17. package/dist/common/webGenerator.d.ts +16 -0
  18. package/dist/common/webGenerator.js +114 -0
  19. package/dist/{util → common}/webRouterCollector.d.ts +21 -2
  20. package/dist/common/webRouterCollector.js +331 -0
  21. package/dist/config/config.default.d.ts +7 -0
  22. package/dist/config/config.default.js +28 -0
  23. package/dist/context/container.d.ts +73 -43
  24. package/dist/context/container.js +289 -271
  25. package/dist/context/definitionRegistry.d.ts +26 -0
  26. package/dist/context/definitionRegistry.js +124 -0
  27. package/dist/context/managedResolverFactory.d.ts +15 -32
  28. package/dist/context/managedResolverFactory.js +88 -317
  29. package/dist/context/providerWrapper.d.ts +2 -3
  30. package/dist/context/requestContainer.d.ts +0 -2
  31. package/dist/context/requestContainer.js +9 -26
  32. package/dist/definitions/functionDefinition.d.ts +3 -4
  33. package/dist/definitions/functionDefinition.js +0 -6
  34. package/dist/definitions/objectCreator.js +6 -6
  35. package/dist/definitions/objectDefinition.d.ts +4 -6
  36. package/dist/definitions/objectDefinition.js +0 -8
  37. package/dist/definitions/properties.d.ts +2 -15
  38. package/dist/definitions/properties.js +3 -72
  39. package/dist/error/base.d.ts +13 -0
  40. package/dist/error/base.js +19 -0
  41. package/dist/error/code.d.ts +60 -0
  42. package/dist/error/code.js +65 -0
  43. package/dist/error/framework.d.ts +24 -0
  44. package/dist/error/framework.js +58 -0
  45. package/dist/error/http.d.ts +58 -0
  46. package/dist/error/http.js +82 -0
  47. package/dist/error/index.d.ts +4 -0
  48. package/dist/error/index.js +16 -0
  49. package/dist/functional/configuration.d.ts +4 -2
  50. package/dist/functional/configuration.js +12 -2
  51. package/dist/index.d.ts +24 -11
  52. package/dist/index.js +36 -36
  53. package/dist/interface.d.ts +270 -184
  54. package/dist/interface.js +9 -1
  55. package/dist/service/aspectService.d.ts +11 -14
  56. package/dist/service/aspectService.js +116 -172
  57. package/dist/service/configService.d.ts +10 -8
  58. package/dist/service/configService.js +61 -38
  59. package/dist/service/decoratorService.d.ts +23 -0
  60. package/dist/service/decoratorService.js +149 -0
  61. package/dist/service/environmentService.js +15 -4
  62. package/dist/service/frameworkService.d.ts +26 -0
  63. package/dist/service/frameworkService.js +176 -0
  64. package/dist/service/informationService.d.ts +4 -7
  65. package/dist/service/informationService.js +41 -10
  66. package/dist/service/lifeCycleService.d.ts +14 -0
  67. package/dist/service/lifeCycleService.js +130 -0
  68. package/dist/service/loggerService.d.ts +27 -0
  69. package/dist/service/loggerService.js +132 -0
  70. package/dist/service/middlewareService.d.ts +11 -0
  71. package/dist/service/middlewareService.js +127 -0
  72. package/dist/{features/pipeline.d.ts → service/pipelineService.d.ts} +56 -46
  73. package/dist/{features/pipeline.js → service/pipelineService.js} +54 -19
  74. package/dist/setup.d.ts +4 -0
  75. package/dist/setup.js +119 -0
  76. package/dist/util/contextUtil.d.ts +1 -1
  77. package/dist/util/contextUtil.js +2 -2
  78. package/dist/util/index.d.ts +64 -0
  79. package/dist/util/index.js +195 -7
  80. package/dist/util/pathFileUtil.js +2 -2
  81. package/dist/util/pathToRegexp.d.ts +17 -0
  82. package/dist/util/pathToRegexp.js +280 -0
  83. package/dist/util/webRouterParam.d.ts +2 -2
  84. package/dist/util/webRouterParam.js +19 -20
  85. package/package.json +10 -15
  86. package/dist/common/lodashWrap.d.ts +0 -9
  87. package/dist/common/lodashWrap.js +0 -18
  88. package/dist/common/notFoundError.d.ts +0 -8
  89. package/dist/common/notFoundError.js +0 -20
  90. package/dist/common/reflectTool.d.ts +0 -15
  91. package/dist/common/reflectTool.js +0 -83
  92. package/dist/context/applicationContext.d.ts +0 -81
  93. package/dist/context/applicationContext.js +0 -263
  94. package/dist/context/managed.d.ts +0 -45
  95. package/dist/context/managed.js +0 -69
  96. package/dist/context/resolverHandler.d.ts +0 -34
  97. package/dist/context/resolverHandler.js +0 -88
  98. package/dist/definitions/messageSource.d.ts +0 -13
  99. package/dist/definitions/messageSource.js +0 -74
  100. package/dist/definitions/resource.d.ts +0 -27
  101. package/dist/definitions/resource.js +0 -116
  102. package/dist/features/index.d.ts +0 -2
  103. package/dist/features/index.js +0 -3
  104. package/dist/logger.d.ts +0 -4
  105. package/dist/logger.js +0 -20
  106. package/dist/util/containerUtil.d.ts +0 -9
  107. package/dist/util/containerUtil.js +0 -26
  108. package/dist/util/emptyFramework.d.ts +0 -62
  109. package/dist/util/emptyFramework.js +0 -72
  110. package/dist/util/serviceFactory.d.ts +0 -13
  111. package/dist/util/staticConfig.d.ts +0 -10
  112. package/dist/util/staticConfig.js +0 -67
  113. package/dist/util/webRouterCollector.js +0 -348
@@ -1,166 +1,87 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.BaseFramework = void 0;
4
13
  const interface_1 = require("./interface");
5
14
  const decorator_1 = require("@midwayjs/decorator");
6
15
  const logger_1 = require("@midwayjs/logger");
7
- const logger_2 = require("./logger");
8
16
  const requestContainer_1 = require("./context/requestContainer");
9
- const configuration_1 = require("./functional/configuration");
17
+ const environmentService_1 = require("./service/environmentService");
18
+ const configService_1 = require("./service/configService");
10
19
  const informationService_1 = require("./service/informationService");
11
- const containerUtil_1 = require("./util/containerUtil");
20
+ const loggerService_1 = require("./service/loggerService");
21
+ const middlewareManager_1 = require("./common/middlewareManager");
22
+ const middlewareService_1 = require("./service/middlewareService");
23
+ const filterManager_1 = require("./common/filterManager");
24
+ const util = require("util");
25
+ const debug = util.debuglog('midway:debug');
12
26
  class BaseFramework {
13
- constructor() {
27
+ constructor(applicationContext) {
28
+ this.applicationContext = applicationContext;
14
29
  this.defaultContext = {};
30
+ this.middlewareManager = this.createMiddlewareManager();
31
+ this.filterManager = this.createFilterManager();
32
+ this.composeMiddleware = null;
15
33
  }
16
- configure(options) {
17
- this.configurationOptions = options || {};
34
+ async init() {
35
+ var _a, _b, _c;
36
+ this.configurationOptions = (_a = this.configure()) !== null && _a !== void 0 ? _a : {};
18
37
  this.BaseContextLoggerClass =
19
- this.configurationOptions.ContextLoggerClass ||
20
- this.getDefaultContextLoggerClass();
21
- this.logger = this.configurationOptions.logger;
22
- this.appLogger = this.configurationOptions.appLogger;
38
+ (_b = this.configurationOptions.ContextLoggerClass) !== null && _b !== void 0 ? _b : this.getDefaultContextLoggerClass();
39
+ this.ContextLoggerApplyLogger =
40
+ (_c = this.configurationOptions.ContextLoggerApplyLogger) !== null && _c !== void 0 ? _c : 'appLogger';
41
+ this.logger = this.loggerService.getLogger('coreLogger');
42
+ this.appLogger = this.loggerService.getLogger('appLogger');
23
43
  return this;
24
44
  }
45
+ isEnable() {
46
+ return true;
47
+ }
25
48
  async initialize(options) {
26
- this.isMainFramework = options.isMainFramework;
27
- this.configurationOptions = this.configurationOptions || {};
28
- /**
29
- * before create MidwayContainer instance,can change init parameters
30
- */
31
49
  await this.beforeContainerInitialize(options);
32
- /**
33
- * initialize MidwayContainer instance
34
- */
35
50
  await this.containerInitialize(options);
36
- /**
37
- * before container load directory and bind
38
- */
39
51
  await this.afterContainerInitialize(options);
40
- /**
41
- * run container loadDirectoryLoad method to create object definition
42
- */
43
52
  await this.containerDirectoryLoad(options);
44
- /**
45
- * after container load directory and bind
46
- */
47
53
  await this.afterContainerDirectoryLoad(options);
48
54
  /**
49
55
  * Third party application initialization
50
56
  */
51
57
  await this.applicationInitialize(options);
52
- /**
53
- * start container ready
54
- */
55
58
  await this.containerReady(options);
56
- if (this.isMainFramework !== undefined) {
57
- // 多框架场景,由 bootstrap 执行后续流程
58
- return;
59
- }
60
- /**
61
- * load extensions and lifeCycle
62
- */
63
- await this.loadExtension();
64
- /**
65
- * after container refresh
66
- */
67
59
  await this.afterContainerReady(options);
68
60
  }
69
- async initializeInfo(options) {
70
- if (!this.applicationContext.getInformationService()) {
71
- const informationService = new informationService_1.MidwayInformationService(options);
72
- this.applicationContext.setInformationService(informationService);
73
- }
74
- }
75
- async initializeLogger(options) {
76
- if (!this.logger) {
77
- this.logger = new Proxy(logger_2.createMidwayLogger(this, 'coreLogger'), {});
78
- this.logger.updateDefaultLabel(this.getFrameworkName());
79
- }
80
- if (!this.appLogger) {
81
- this.appLogger = logger_2.createMidwayLogger(this, 'logger', {
82
- fileLogName: 'midway-app.log',
83
- });
84
- }
85
- }
86
- async containerInitialize(options) {
87
- /**
88
- * initialize container
89
- */
90
- if (options.applicationContext) {
91
- this.applicationContext = options.applicationContext;
92
- }
93
- else {
94
- this.applicationContext = containerUtil_1.createDirectoryGlobContainer({
95
- baseDir: options.baseDir,
96
- });
97
- }
98
- this.applicationContext.registerObject('baseDir', options.baseDir);
99
- this.applicationContext.registerObject('appDir', options.appDir);
100
- /**
101
- * initialize base information
102
- */
103
- await this.initializeInfo(options);
104
- /**
105
- * initialize framework logger
106
- */
107
- await this.initializeLogger(options);
108
- }
109
- async containerDirectoryLoad(options) {
110
- if (options.applicationContext) {
111
- // 如果有传入全局容器,就不需要再次扫描了
112
- return;
113
- }
114
- // 废弃 deprecated
115
- if (options.preloadModules && options.preloadModules.length) {
116
- for (const preloadModule of options.preloadModules) {
117
- this.applicationContext.bindClass(preloadModule);
118
- }
119
- }
120
- // register app
121
- this.applicationContext.registerDataHandler(decorator_1.APPLICATION_KEY, (key, meta) => {
122
- var _a;
123
- if (options.globalApplicationHandler) {
124
- return ((_a = options.globalApplicationHandler(meta === null || meta === void 0 ? void 0 : meta.type)) !== null && _a !== void 0 ? _a : this.getApplication());
125
- }
126
- else {
127
- return this.getApplication();
128
- }
129
- });
130
- // register logger
131
- this.getApplicationContext().registerDataHandler(decorator_1.LOGGER_KEY, key => {
132
- return this.getLogger(key);
133
- });
134
- }
61
+ /**
62
+ * @deprecated
63
+ */
64
+ async containerInitialize(options) { }
65
+ /**
66
+ * @deprecated
67
+ */
68
+ async containerDirectoryLoad(options) { }
69
+ /**
70
+ * @deprecated
71
+ */
135
72
  async containerReady(options) {
136
73
  if (!this.app.getApplicationContext) {
137
74
  this.defineApplicationProperties();
138
75
  }
139
- await this.applicationContext.ready();
140
- }
141
- async loadExtension() {
142
- // 切面支持
143
- await this.applicationContext.getAspectService().loadAspect();
144
- // 预加载模块支持
145
- await this.loadPreloadModule();
146
- // lifecycle 支持
147
- await this.loadLifeCycles();
148
- }
149
- async containerStop() {
150
- await this.applicationContext.stop();
151
76
  }
152
77
  getApplicationContext() {
153
78
  return this.applicationContext;
154
79
  }
155
80
  getConfiguration(key) {
156
- return this.getApplicationContext()
157
- .getConfigService()
158
- .getConfiguration(key);
81
+ return this.configService.getConfiguration(key);
159
82
  }
160
83
  getCurrentEnvironment() {
161
- return this.getApplicationContext()
162
- .getEnvironmentService()
163
- .getCurrentEnvironment();
84
+ return this.environmentService.getCurrentEnvironment();
164
85
  }
165
86
  getApplication() {
166
87
  return this.app;
@@ -169,44 +90,34 @@ class BaseFramework {
169
90
  this.BaseContextLoggerClass = BaseContextLogger;
170
91
  }
171
92
  createContextLogger(ctx, name) {
172
- const appLogger = this.getLogger(name);
93
+ const appLogger = this.getLogger(name !== null && name !== void 0 ? name : this.ContextLoggerApplyLogger);
173
94
  return new this.BaseContextLoggerClass(ctx, appLogger);
174
95
  }
175
96
  async stop() {
176
97
  await this.beforeStop();
177
- if (this.isMainFramework === true || this.isMainFramework === undefined) {
178
- await this.stopLifeCycles();
179
- await this.containerStop();
180
- }
181
98
  }
182
99
  getAppDir() {
183
- return this.applicationContext.getInformationService().getAppDir();
100
+ return this.informationService.getAppDir();
184
101
  }
185
102
  getBaseDir() {
186
- return this.applicationContext.getInformationService().getBaseDir();
103
+ return this.informationService.getBaseDir();
187
104
  }
188
105
  defineApplicationProperties(applicationProperties = {}, whiteList = []) {
189
106
  const defaultApplicationProperties = {
190
107
  getBaseDir: () => {
191
- return this.getApplicationContext()
192
- .getInformationService()
193
- .getBaseDir();
108
+ return this.getBaseDir();
194
109
  },
195
110
  getAppDir: () => {
196
- return this.getApplicationContext().getInformationService().getAppDir();
111
+ return this.getAppDir();
197
112
  },
198
113
  getEnv: () => {
199
- return this.getApplicationContext()
200
- .getEnvironmentService()
201
- .getCurrentEnvironment();
114
+ return this.getCurrentEnvironment();
202
115
  },
203
116
  getApplicationContext: () => {
204
117
  return this.getApplicationContext();
205
118
  },
206
119
  getConfig: (key) => {
207
- return this.getApplicationContext()
208
- .getConfigService()
209
- .getConfiguration(key);
120
+ return this.getConfiguration(key);
210
121
  },
211
122
  getFrameworkType: () => {
212
123
  return this.getFrameworkType();
@@ -243,20 +154,35 @@ class BaseFramework {
243
154
  return this.createContextLogger(ctx, name);
244
155
  };
245
156
  }
157
+ ctx.setAttr = (key, value) => {
158
+ ctx.requestContext.setAttr(key, value);
159
+ };
160
+ ctx.getAttr = (key) => {
161
+ return ctx.requestContext.getAttr(key);
162
+ };
246
163
  return ctx;
247
164
  },
248
165
  setContextLoggerClass: (BaseContextLogger) => {
249
166
  return this.setContextLoggerClass(BaseContextLogger);
250
167
  },
251
- addConfigObject(obj) {
252
- this.getApplicationContext().getConfigService().addObject(obj);
168
+ addConfigObject: (obj) => {
169
+ this.configService.addObject(obj);
253
170
  },
254
- setAttr(key, value) {
171
+ setAttr: (key, value) => {
255
172
  this.getApplicationContext().setAttr(key, value);
256
173
  },
257
- getAttr(key) {
174
+ getAttr: (key) => {
258
175
  return this.getApplicationContext().getAttr(key);
259
176
  },
177
+ useMiddleware: (middleware) => {
178
+ return this.useMiddleware(middleware);
179
+ },
180
+ getMiddleware: () => {
181
+ return this.getMiddleware();
182
+ },
183
+ useFilter: (Filter) => {
184
+ return this.useFilter(Filter);
185
+ },
260
186
  };
261
187
  for (const method of whiteList) {
262
188
  delete defaultApplicationProperties[method];
@@ -264,116 +190,115 @@ class BaseFramework {
264
190
  Object.assign(this.app, defaultApplicationProperties, applicationProperties);
265
191
  }
266
192
  async beforeStop() { }
193
+ /**
194
+ * @deprecated
195
+ */
267
196
  async beforeContainerInitialize(options) { }
197
+ /**
198
+ * @deprecated
199
+ */
268
200
  async afterContainerInitialize(options) { }
201
+ /**
202
+ * @deprecated
203
+ */
269
204
  async afterContainerDirectoryLoad(options) { }
205
+ /**
206
+ * @deprecated
207
+ */
270
208
  async afterContainerReady(options) { }
271
- async loadLifeCycles(isForce = false) {
272
- // agent 不加载生命周期
273
- if (this.app.getProcessType() === interface_1.MidwayProcessTypeEnum.AGENT)
274
- return;
275
- const cycles = decorator_1.listModule(decorator_1.CONFIGURATION_KEY);
276
- const lifecycleInstanceList = [];
277
- for (const cycle of cycles) {
278
- if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
279
- // 函数式写法
280
- cycle.instance = cycle.target;
281
- }
282
- else {
283
- // 普通类写法
284
- const providerId = decorator_1.getProviderId(cycle.target);
285
- cycle.instance =
286
- await this.getApplicationContext().getAsync(providerId);
287
- }
288
- lifecycleInstanceList.push(cycle);
289
- }
290
- // exec onConfigLoad()
291
- for (const cycle of lifecycleInstanceList) {
292
- if (typeof cycle.instance.onConfigLoad === 'function') {
293
- const configData = await cycle.instance.onConfigLoad(this.getApplicationContext());
294
- if (configData) {
295
- this.getApplicationContext().getConfigService().addObject(configData);
209
+ async applyMiddleware(lastMiddleware) {
210
+ if (!this.composeMiddleware) {
211
+ this.middlewareManager.insertFirst((async (ctx, next) => {
212
+ let returnResult = undefined;
213
+ try {
214
+ const result = await next();
215
+ returnResult = await this.filterManager.runResultFilter(result, ctx);
296
216
  }
297
- }
298
- }
299
- for (const cycle of lifecycleInstanceList) {
300
- if (typeof cycle.instance.onReady === 'function') {
301
- /**
302
- * 让组件能正确获取到 bind 之后 registerObject 的对象有三个方法
303
- * 1、在 load 之后修改 bind,不太可行
304
- * 2、每次 getAsync 的时候,去掉 namespace,同时还要查找当前全局的变量,性能差
305
- * 3、一般只会在 onReady 的地方执行 registerObject(否则没有全局的意义),这个取巧的办法就是 onReady 传入一个代理,其中绑定当前的 namespace
306
- */
307
- await cycle.instance.onReady(new Proxy(this.getApplicationContext(), {
308
- get: function (target, prop, receiver) {
309
- if (prop === 'getCurrentNamespace' && cycle.namespace) {
310
- return () => {
311
- return cycle.namespace;
312
- };
313
- }
314
- return Reflect.get(target, prop, receiver);
315
- },
316
- }), this.app);
317
- }
217
+ catch (err) {
218
+ returnResult = await this.filterManager.runErrorFilter(err, ctx);
219
+ }
220
+ if (returnResult.error) {
221
+ throw returnResult.error;
222
+ }
223
+ return returnResult.result;
224
+ }));
225
+ debug(`[core]: Compose middleware = [${this.middlewareManager.getNames()}]`);
226
+ this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager, this.app);
227
+ await this.filterManager.init(this.applicationContext);
318
228
  }
319
- }
320
- async stopLifeCycles() {
321
- const cycles = decorator_1.listModule(decorator_1.CONFIGURATION_KEY);
322
- for (const cycle of cycles) {
323
- let inst;
324
- if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
325
- // 函数式写法
326
- inst = cycle.target;
327
- }
328
- else {
329
- const providerId = decorator_1.getProviderId(cycle.target);
330
- inst = await this.applicationContext.getAsync(providerId);
331
- }
332
- if (inst.onStop && typeof inst.onStop === 'function') {
333
- await inst.onStop(new Proxy(this.getApplicationContext(), {
334
- get: function (target, prop, receiver) {
335
- if (prop === 'getCurrentNamespace' && cycle.namespace) {
336
- return () => {
337
- return cycle.namespace;
338
- };
339
- }
340
- return Reflect.get(target, prop, receiver);
341
- },
342
- }), this.app);
343
- }
229
+ if (lastMiddleware) {
230
+ return await this.middlewareService.compose([this.composeMiddleware, lastMiddleware], this.app);
344
231
  }
345
- }
346
- /**
347
- * load preload module for container
348
- * @private
349
- */
350
- async loadPreloadModule() {
351
- // some common decorator implementation
352
- const modules = decorator_1.listPreloadModule();
353
- for (const module of modules) {
354
- // preload init context
355
- await this.applicationContext.getAsync(module);
232
+ else {
233
+ return this.composeMiddleware;
356
234
  }
357
235
  }
358
236
  getLogger(name) {
359
237
  var _a;
360
- return (_a = logger_1.loggers.getLogger(name)) !== null && _a !== void 0 ? _a : this.appLogger;
238
+ return (_a = this.loggerService.getLogger(name)) !== null && _a !== void 0 ? _a : this.appLogger;
361
239
  }
362
240
  getCoreLogger() {
363
241
  return this.logger;
364
242
  }
365
243
  createLogger(name, option = {}) {
366
- return logger_2.createMidwayLogger(this, name, option);
244
+ return this.loggerService.createLogger(name, option);
367
245
  }
368
246
  getProjectName() {
369
- return this.applicationContext.getInformationService().getProjectName();
247
+ return this.informationService.getProjectName();
370
248
  }
371
249
  getFrameworkName() {
372
- return this.getFrameworkType().toString();
250
+ return this.getFrameworkType().name;
373
251
  }
374
252
  getDefaultContextLoggerClass() {
375
253
  return logger_1.MidwayContextLogger;
376
254
  }
255
+ useMiddleware(Middleware) {
256
+ this.middlewareManager.insertLast(Middleware);
257
+ }
258
+ getMiddleware() {
259
+ return this.middlewareManager;
260
+ }
261
+ useFilter(Filter) {
262
+ return this.filterManager.useFilter(Filter);
263
+ }
264
+ createMiddlewareManager() {
265
+ return new middlewareManager_1.ContextMiddlewareManager();
266
+ }
267
+ createFilterManager() {
268
+ return new filterManager_1.FilterManager();
269
+ }
377
270
  }
271
+ __decorate([
272
+ (0, decorator_1.Inject)(),
273
+ __metadata("design:type", loggerService_1.MidwayLoggerService)
274
+ ], BaseFramework.prototype, "loggerService", void 0);
275
+ __decorate([
276
+ (0, decorator_1.Inject)(),
277
+ __metadata("design:type", environmentService_1.MidwayEnvironmentService)
278
+ ], BaseFramework.prototype, "environmentService", void 0);
279
+ __decorate([
280
+ (0, decorator_1.Inject)(),
281
+ __metadata("design:type", configService_1.MidwayConfigService)
282
+ ], BaseFramework.prototype, "configService", void 0);
283
+ __decorate([
284
+ (0, decorator_1.Inject)(),
285
+ __metadata("design:type", informationService_1.MidwayInformationService)
286
+ ], BaseFramework.prototype, "informationService", void 0);
287
+ __decorate([
288
+ (0, decorator_1.Inject)(),
289
+ __metadata("design:type", middlewareService_1.MidwayMiddlewareService)
290
+ ], BaseFramework.prototype, "middlewareService", void 0);
291
+ __decorate([
292
+ (0, decorator_1.Init)(),
293
+ __metadata("design:type", Function),
294
+ __metadata("design:paramtypes", []),
295
+ __metadata("design:returntype", Promise)
296
+ ], BaseFramework.prototype, "init", null);
297
+ __decorate([
298
+ (0, decorator_1.Destroy)(),
299
+ __metadata("design:type", Function),
300
+ __metadata("design:paramtypes", []),
301
+ __metadata("design:returntype", Promise)
302
+ ], BaseFramework.prototype, "stop", null);
378
303
  exports.BaseFramework = BaseFramework;
379
304
  //# sourceMappingURL=baseFramework.js.map
@@ -0,0 +1,12 @@
1
+ import { IMidwayApplication, IMidwayFramework } from '../interface';
2
+ import { FrameworkType } from '@midwayjs/decorator';
3
+ export declare class MidwayApplicationManager {
4
+ private globalFrameworkMap;
5
+ private globalFrameworkTypeMap;
6
+ addFramework(namespace: any, framework: IMidwayFramework<any, any, any>): void;
7
+ getFramework(namespaceOrFrameworkType: string | FrameworkType): IMidwayFramework<any, any, any, unknown, unknown>;
8
+ getApplication(namespaceOrFrameworkType: string | FrameworkType): IMidwayApplication;
9
+ getApplications(namespaces: Array<string | FrameworkType>): IMidwayApplication[];
10
+ getWebLikeApplication(): IMidwayApplication[];
11
+ }
12
+ //# sourceMappingURL=applicationManager.d.ts.map
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.MidwayApplicationManager = void 0;
10
+ const decorator_1 = require("@midwayjs/decorator");
11
+ let MidwayApplicationManager = class MidwayApplicationManager {
12
+ constructor() {
13
+ this.globalFrameworkMap = new Map();
14
+ this.globalFrameworkTypeMap = new WeakMap();
15
+ }
16
+ addFramework(namespace, framework) {
17
+ this.globalFrameworkMap.set(namespace, framework);
18
+ this.globalFrameworkTypeMap.set(framework.getFrameworkType(), framework);
19
+ }
20
+ getFramework(namespaceOrFrameworkType) {
21
+ if (typeof namespaceOrFrameworkType === 'string') {
22
+ if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
23
+ return this.globalFrameworkMap.get(namespaceOrFrameworkType);
24
+ }
25
+ }
26
+ else {
27
+ if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
28
+ return this.globalFrameworkTypeMap.get(namespaceOrFrameworkType);
29
+ }
30
+ }
31
+ }
32
+ getApplication(namespaceOrFrameworkType) {
33
+ if (typeof namespaceOrFrameworkType === 'string') {
34
+ if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
35
+ return this.globalFrameworkMap
36
+ .get(namespaceOrFrameworkType)
37
+ .getApplication();
38
+ }
39
+ }
40
+ else {
41
+ if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
42
+ return this.globalFrameworkTypeMap
43
+ .get(namespaceOrFrameworkType)
44
+ .getApplication();
45
+ }
46
+ }
47
+ }
48
+ getApplications(namespaces) {
49
+ return namespaces
50
+ .map(namespace => {
51
+ return this.getApplication(namespace);
52
+ })
53
+ .filter(app => {
54
+ return !!app;
55
+ });
56
+ }
57
+ getWebLikeApplication() {
58
+ return this.getApplications(['express', 'koa', 'egg', 'faas']);
59
+ }
60
+ };
61
+ MidwayApplicationManager = __decorate([
62
+ (0, decorator_1.Provide)(),
63
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
64
+ ], MidwayApplicationManager);
65
+ exports.MidwayApplicationManager = MidwayApplicationManager;
66
+ //# sourceMappingURL=applicationManager.js.map
File without changes
@@ -29,16 +29,13 @@ class DirectoryFileDetector extends AbstractFileDetector {
29
29
  this.directoryFilterArray = [];
30
30
  }
31
31
  run(container) {
32
- const debugLogger = container.getDebugLogger();
33
32
  const loadDirs = [].concat(this.options.loadDir || []);
34
33
  for (const dir of loadDirs) {
35
- const fileResults = glob_1.run(DEFAULT_PATTERN.concat(this.options.pattern || []), {
34
+ const fileResults = (0, glob_1.run)(DEFAULT_PATTERN.concat(this.options.pattern || []), {
36
35
  cwd: dir,
37
36
  ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []),
38
37
  });
39
38
  for (const file of fileResults) {
40
- debugLogger(`\nmain:*********** binding "${file}" ***********`);
41
- debugLogger(` namespace => "${this.options.namespace}"`);
42
39
  if (this.directoryFilterArray.length) {
43
40
  for (const resolveFilter of this.directoryFilterArray) {
44
41
  if (typeof resolveFilter.pattern === 'string') {
@@ -50,7 +47,7 @@ class DirectoryFileDetector extends AbstractFileDetector {
50
47
  continue;
51
48
  }
52
49
  }
53
- else if (decorator_1.isRegExp(resolveFilter.pattern)) {
50
+ else if ((0, decorator_1.isRegExp)(resolveFilter.pattern)) {
54
51
  if (resolveFilter.pattern.test(file)) {
55
52
  const exports = resolveFilter.ignoreRequire
56
53
  ? undefined
@@ -61,15 +58,21 @@ class DirectoryFileDetector extends AbstractFileDetector {
61
58
  }
62
59
  const exports = require(file);
63
60
  // add module to set
64
- container.bindClass(exports, this.options.namespace, file);
65
- debugLogger(` binding "${file}" end`);
61
+ container.bindClass(exports, {
62
+ namespace: this.options.namespace,
63
+ srcPath: file,
64
+ createFrom: 'file',
65
+ });
66
66
  }
67
67
  }
68
68
  else {
69
69
  const exports = require(file);
70
70
  // add module to set
71
- container.bindClass(exports, this.options.namespace, file);
72
- debugLogger(` binding "${file}" end`);
71
+ container.bindClass(exports, {
72
+ namespace: this.options.namespace,
73
+ srcPath: file,
74
+ createFrom: 'file',
75
+ });
73
76
  }
74
77
  }
75
78
  }
@@ -79,7 +82,10 @@ exports.DirectoryFileDetector = DirectoryFileDetector;
79
82
  class CustomModuleDetector extends AbstractFileDetector {
80
83
  run(container) {
81
84
  for (const module of this.options.modules) {
82
- container.bindClass(module, this.options.namespace);
85
+ container.bindClass(module, {
86
+ namespace: this.options.namespace,
87
+ createFrom: 'module',
88
+ });
83
89
  }
84
90
  }
85
91
  }
@@ -0,0 +1,19 @@
1
+ import { CommonFilterUnion, IMidwayContainer, IMidwayContext } from '../interface';
2
+ export declare class FilterManager<CTX extends IMidwayContext = IMidwayContext, R = any, N = any> {
3
+ private errFilterList;
4
+ private successFilterList;
5
+ private exceptionMap;
6
+ private defaultErrFilter;
7
+ private matchFnList;
8
+ useFilter(Filters: CommonFilterUnion<CTX, R, N>): void;
9
+ init(applicationContext: IMidwayContainer): Promise<void>;
10
+ runErrorFilter(err: Error, ctx: CTX, res?: R, next?: N): Promise<{
11
+ result: any;
12
+ error: any;
13
+ }>;
14
+ runResultFilter(result: any, ctx: CTX, res?: R, next?: N): Promise<{
15
+ result: any;
16
+ error: any;
17
+ }>;
18
+ }
19
+ //# sourceMappingURL=filterManager.d.ts.map