@midwayjs/web 3.0.14-beta.1 → 3.0.14-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/base.js CHANGED
@@ -8,6 +8,8 @@ const fs_1 = require("fs");
8
8
  const logger_1 = require("./logger");
9
9
  const router_1 = require("@eggjs/router");
10
10
  const util_1 = require("util");
11
+ const lifecycle_1 = require("./framework/lifecycle");
12
+ const web_1 = require("./framework/web");
11
13
  const ROUTER = Symbol('EggCore#router');
12
14
  const EGG_LOADER = Symbol.for('egg#loader');
13
15
  const EGG_PATH = Symbol.for('egg#eggPath');
@@ -111,6 +113,7 @@ const createAppWorkerLoader = () => {
111
113
  this.app.beforeStart(async () => {
112
114
  debug('[egg]: start "initialize framework service with lazy in app.load"');
113
115
  const applicationContext = (0, core_1.getCurrentApplicationContext)();
116
+ applicationContext.bind(lifecycle_1.MidwayWebLifeCycleService);
114
117
  /**
115
118
  * 这里 logger service 已经被 get loggers() 初始化过了,就不需要在这里初始化了
116
119
  */
@@ -121,10 +124,17 @@ const createAppWorkerLoader = () => {
121
124
  application: this.app,
122
125
  },
123
126
  ]);
124
- // lifecycle support
125
- await applicationContext.getAsync(core_1.MidwayLifeCycleService, [
126
- applicationContext,
127
- ]);
127
+ this.app.once('server', async (server) => {
128
+ this.framework.setServer(server);
129
+ // register httpServer to applicationContext
130
+ applicationContext.registerObject(core_1.HTTP_SERVER_KEY, server);
131
+ await this.lifecycleService.afterInit();
132
+ });
133
+ // 这里生命周期走到 onReady
134
+ this.lifecycleService = await applicationContext.getAsync(lifecycle_1.MidwayWebLifeCycleService, [applicationContext]);
135
+ // 执行加载路由
136
+ this.framework = await applicationContext.getAsync(web_1.MidwayWebFramework);
137
+ await this.framework.loadMidwayController();
128
138
  });
129
139
  }
130
140
  }
@@ -1,15 +1,13 @@
1
1
  import { IMidwayWebApplication } from './interface';
2
2
  import { MidwayDecoratorService } from '@midwayjs/core';
3
- import { MidwayWebFramework } from './framework/web';
4
3
  export declare class EggConfiguration {
5
4
  baseDir: any;
6
5
  appDir: any;
7
6
  app: IMidwayWebApplication;
8
7
  decoratorService: MidwayDecoratorService;
9
- framework: MidwayWebFramework;
10
8
  init(): void;
11
9
  onReady(): Promise<void>;
12
- onServerReady(container: any): Promise<void>;
10
+ onServerReady(): Promise<void>;
13
11
  onStop(): Promise<void>;
14
12
  }
15
13
  //# sourceMappingURL=configuration.d.ts.map
@@ -13,7 +13,6 @@ exports.EggConfiguration = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const core_1 = require("@midwayjs/core");
15
15
  const path_1 = require("path");
16
- const web_1 = require("./framework/web");
17
16
  let EggConfiguration = class EggConfiguration {
18
17
  init() {
19
18
  this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
@@ -28,16 +27,9 @@ let EggConfiguration = class EggConfiguration {
28
27
  ? this.app.middleware.slice()
29
28
  : [];
30
29
  }
31
- async onServerReady(container) {
30
+ async onServerReady() {
32
31
  // trigger server didReady
33
32
  this.app.messenger.emit('egg-ready');
34
- if (process.env['EGG_CLUSTER_MODE'] === 'true') {
35
- this.app.once('server', server => {
36
- this.framework.setServer(server);
37
- // register httpServer to applicationContext
38
- container.registerObject(core_1.HTTP_SERVER_KEY, server);
39
- });
40
- }
41
33
  }
42
34
  async onStop() {
43
35
  // TODO flush egg logger and close it
@@ -59,10 +51,6 @@ __decorate([
59
51
  (0, decorator_1.Inject)(),
60
52
  __metadata("design:type", core_1.MidwayDecoratorService)
61
53
  ], EggConfiguration.prototype, "decoratorService", void 0);
62
- __decorate([
63
- (0, decorator_1.Inject)(),
64
- __metadata("design:type", web_1.MidwayWebFramework)
65
- ], EggConfiguration.prototype, "framework", void 0);
66
54
  __decorate([
67
55
  (0, decorator_1.Init)(),
68
56
  __metadata("design:type", Function),
@@ -0,0 +1,14 @@
1
+ import { IMidwayContainer, MidwayConfigService, MidwayFrameworkService } from '@midwayjs/core';
2
+ export declare class MidwayWebLifeCycleService {
3
+ readonly applicationContext: IMidwayContainer;
4
+ protected frameworkService: MidwayFrameworkService;
5
+ protected configService: MidwayConfigService;
6
+ private lifecycleInstanceList;
7
+ constructor(applicationContext: IMidwayContainer);
8
+ protected init(): Promise<void>;
9
+ afterInit(): Promise<void>;
10
+ stop(): Promise<void>;
11
+ private runContainerLifeCycle;
12
+ private runObjectLifeCycle;
13
+ }
14
+ //# sourceMappingURL=lifecycle.d.ts.map
@@ -0,0 +1,137 @@
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
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MidwayWebLifeCycleService = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const decorator_1 = require("@midwayjs/decorator");
15
+ const configuration_1 = require("@midwayjs/core/dist/functional/configuration");
16
+ const util_1 = require("util");
17
+ const debug = (0, util_1.debuglog)('midway:debug');
18
+ let MidwayWebLifeCycleService = class MidwayWebLifeCycleService {
19
+ constructor(applicationContext) {
20
+ this.applicationContext = applicationContext;
21
+ this.lifecycleInstanceList = [];
22
+ }
23
+ async init() {
24
+ // run lifecycle
25
+ const cycles = (0, decorator_1.listModule)(decorator_1.CONFIGURATION_KEY);
26
+ debug(`[core]: Found Configuration length = ${cycles.length}`);
27
+ for (const cycle of cycles) {
28
+ if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
29
+ // 函数式写法
30
+ cycle.instance = cycle.target;
31
+ }
32
+ else {
33
+ // 普通类写法
34
+ debug(`[core]: Lifecycle run ${cycle.target.name} init`);
35
+ cycle.instance = await this.applicationContext.getAsync(cycle.target);
36
+ }
37
+ if (cycle.instance) {
38
+ this.lifecycleInstanceList.push(cycle);
39
+ }
40
+ }
41
+ // bind object lifecycle
42
+ await Promise.all([
43
+ this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectCreated'),
44
+ this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectCreated'),
45
+ this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectInit'),
46
+ this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectDestroy'),
47
+ ]);
48
+ // bind framework lifecycle
49
+ // onAppError
50
+ // exec onConfigLoad()
51
+ await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onConfigLoad', configData => {
52
+ if (configData) {
53
+ this.configService.addObject(configData);
54
+ }
55
+ });
56
+ // exec onReady()
57
+ await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onReady');
58
+ }
59
+ async afterInit() {
60
+ // exec framework.run()
61
+ await this.frameworkService.runFramework();
62
+ // exec onServerReady()
63
+ await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onServerReady');
64
+ // clear config merge cache
65
+ if (!this.configService.getConfiguration('debug.recordConfigMergeOrder')) {
66
+ this.configService.clearConfigMergeOrder();
67
+ }
68
+ }
69
+ async stop() {
70
+ // stop lifecycle
71
+ const cycles = (0, decorator_1.listModule)(decorator_1.CONFIGURATION_KEY);
72
+ for (const cycle of cycles) {
73
+ let inst;
74
+ if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
75
+ // 函数式写法
76
+ inst = cycle.target;
77
+ }
78
+ else {
79
+ inst = await this.applicationContext.getAsync(cycle.target);
80
+ }
81
+ await this.runContainerLifeCycle(inst, 'onStop');
82
+ }
83
+ // stop framework
84
+ await this.frameworkService.stopFramework();
85
+ }
86
+ async runContainerLifeCycle(lifecycleInstanceOrList, lifecycle, resultHandler) {
87
+ if (Array.isArray(lifecycleInstanceOrList)) {
88
+ for (const cycle of lifecycleInstanceOrList) {
89
+ if (typeof cycle.instance[lifecycle] === 'function') {
90
+ debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
91
+ const result = await cycle.instance[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
92
+ if (resultHandler) {
93
+ resultHandler(result);
94
+ }
95
+ }
96
+ }
97
+ }
98
+ else {
99
+ if (typeof lifecycleInstanceOrList[lifecycle] === 'function') {
100
+ debug(`[core]: Lifecycle run ${lifecycleInstanceOrList.constructor.name} ${lifecycle}`);
101
+ const result = await lifecycleInstanceOrList[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
102
+ if (resultHandler) {
103
+ resultHandler(result);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ async runObjectLifeCycle(lifecycleInstanceList, lifecycle) {
109
+ for (const cycle of lifecycleInstanceList) {
110
+ if (typeof cycle.instance[lifecycle] === 'function') {
111
+ debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
112
+ return this.applicationContext[lifecycle](cycle.instance[lifecycle].bind(cycle.instance));
113
+ }
114
+ }
115
+ }
116
+ };
117
+ __decorate([
118
+ (0, decorator_1.Inject)(),
119
+ __metadata("design:type", core_1.MidwayFrameworkService)
120
+ ], MidwayWebLifeCycleService.prototype, "frameworkService", void 0);
121
+ __decorate([
122
+ (0, decorator_1.Inject)(),
123
+ __metadata("design:type", core_1.MidwayConfigService)
124
+ ], MidwayWebLifeCycleService.prototype, "configService", void 0);
125
+ __decorate([
126
+ (0, decorator_1.Init)(),
127
+ __metadata("design:type", Function),
128
+ __metadata("design:paramtypes", []),
129
+ __metadata("design:returntype", Promise)
130
+ ], MidwayWebLifeCycleService.prototype, "init", null);
131
+ MidwayWebLifeCycleService = __decorate([
132
+ (0, decorator_1.Provide)(),
133
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
134
+ __metadata("design:paramtypes", [Object])
135
+ ], MidwayWebLifeCycleService);
136
+ exports.MidwayWebLifeCycleService = MidwayWebLifeCycleService;
137
+ //# sourceMappingURL=lifecycle.js.map
@@ -147,26 +147,27 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
147
147
  this.setContextLoggerClass();
148
148
  }
149
149
  async loadMidwayController() {
150
+ // move egg router to last
151
+ this.app.getMiddleware().findAndInsertLast('eggRouterMiddleware');
150
152
  await this.generator.loadMidwayController(this.configurationOptions.globalPrefix, newRouter => {
151
153
  var _a;
152
154
  const dispatchFn = newRouter.middleware();
153
155
  dispatchFn._name = `midwayController(${((_a = newRouter === null || newRouter === void 0 ? void 0 : newRouter.opts) === null || _a === void 0 ? void 0 : _a.prefix) || '/'})`;
154
156
  this.app.useMiddleware(dispatchFn);
155
157
  });
158
+ // restore use method
159
+ this.app.use = this.app.originUse;
160
+ debug(`[egg]: current middleware = ${this.middlewareManager.getNames()}`);
156
161
  }
157
162
  getFrameworkType() {
158
163
  return decorator_1.MidwayFrameworkType.WEB;
159
164
  }
160
165
  async run() {
161
166
  var _a;
162
- // move egg router to last
163
- this.app.getMiddleware().findAndInsertLast('eggRouterMiddleware');
164
- // load controller
165
- await this.loadMidwayController();
166
- // restore use method
167
- this.app.use = this.app.originUse;
168
- debug(`[egg]: current middleware = ${this.middlewareManager.getNames()}`);
167
+ // cluster 模式加载路由需在 run 之前,因为 run 需要在拿到 server 之后执行
169
168
  if (!this.isClusterMode) {
169
+ // load controller
170
+ await this.loadMidwayController();
170
171
  // https config
171
172
  if (this.configurationOptions.key && this.configurationOptions.cert) {
172
173
  this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
@@ -86,7 +86,7 @@ export interface IWebMiddleware {
86
86
  resolve(): MidwayWebMiddleware;
87
87
  }
88
88
  declare module '@midwayjs/core/dist/interface' {
89
- interface MidwayConfig extends EggAppConfig {
89
+ interface MidwayConfig extends Partial<EggAppConfig> {
90
90
  egg?: IMidwayWebConfigurationOptions;
91
91
  }
92
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/web",
3
- "version": "3.0.14-beta.1",
3
+ "version": "3.0.14-beta.2",
4
4
  "description": "Midway Web Framework for Egg.js",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",