@midwayjs/web 3.0.0-beta.7 → 3.0.0-beta.8

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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([b9272e0](https://github.com/midwayjs/midway/commit/b9272e0971003443304b0c53815be31a0061b4bd))
12
+
13
+
14
+
15
+
16
+
6
17
  # [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
7
18
 
8
19
 
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  this is a sub package for midway.
7
7
 
8
- Document: [https://midwayjs.org/midway](https://midwayjs.org/midway)
8
+ Document: [https://midwayjs.org](https://midwayjs.org)
9
9
 
10
10
  ## License
11
11
 
package/app.js CHANGED
@@ -24,10 +24,9 @@ class AppBootHook {
24
24
  async didLoad() {
25
25
  debug('[egg lifecycle]: app didLoad');
26
26
  if (this.app.loader['useEggSocketIO']) {
27
- // socketio 下会提前加入 session 中间件,这里删除,防止重复加载
28
- if (this.app.middleware.length && this.app.middleware[this.app.middleware.length - 1]._name === 'session') {
29
- this.app.middleware.pop();
30
- }
27
+ // egg socket.io 需要这个中间件
28
+ // const session = this.app.getMiddleware().findItem('session');
29
+ // this.app.middleware.push(session);
31
30
  }
32
31
  }
33
32
 
@@ -6,7 +6,7 @@ export declare class EggConfiguration {
6
6
  app: IMidwayWebApplication;
7
7
  decoratorService: MidwayDecoratorService;
8
8
  init(): void;
9
- onReady(container: any): Promise<void>;
9
+ onReady(): Promise<void>;
10
10
  onServerReady(): Promise<void>;
11
11
  onStop(): Promise<void>;
12
12
  }
@@ -18,7 +18,11 @@ let EggConfiguration = class EggConfiguration {
18
18
  return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1]);
19
19
  });
20
20
  }
21
- async onReady(container) { }
21
+ async onReady() {
22
+ // ps: 本想在这里清理掉egg添加的中间件,但是这里的数组已经只有一个 midway root middleware,其他的 getter 都是 egg 加进去的,但是不会被中间件执行,所以不需要清理
23
+ // 去掉为了 egg-socket.io 报错扫进去的 session
24
+ this.app.middleware = this.app.middleware.slice(0);
25
+ }
22
26
  async onServerReady() {
23
27
  // trigger server didReady
24
28
  this.app.messenger.emit('egg-ready');
@@ -72,10 +72,11 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
72
72
  await this.initSingleProcessEgg();
73
73
  }
74
74
  // insert error handler
75
- this.app.use(async (ctx, next) => {
75
+ const midwayRootMiddleware = async (ctx, next) => {
76
76
  // this.app.createAnonymousContext(ctx);
77
77
  await (await this.getMiddleware())(ctx, next);
78
- });
78
+ };
79
+ this.app.use(midwayRootMiddleware);
79
80
  this.generator = new EggControllerGenerator(this.app, this.applicationContext, this.appLogger);
80
81
  this.overwriteApplication('app');
81
82
  await new Promise(resolve => {
@@ -84,7 +85,7 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
84
85
  resolve();
85
86
  });
86
87
  this.app.loader.loadOrigin();
87
- // hack use method
88
+ // 这里拦截 app.use 方法,让他可以加到 midway 的 middlewareManager 中
88
89
  this.app.originUse = this.app.use;
89
90
  this.app.use = this.app.useMiddleware;
90
91
  this.app.ready();
@@ -109,6 +110,13 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
109
110
  });
110
111
  debug(`[egg]: overwrite properties to "${processType}"`);
111
112
  this.defineApplicationProperties({
113
+ generateController: (controllerMapping) => {
114
+ const [controllerId, methodName] = controllerMapping.split('.');
115
+ return this.generator.generateController({
116
+ id: controllerId,
117
+ method: methodName,
118
+ });
119
+ },
112
120
  generateMiddleware: async (middlewareId) => {
113
121
  return this.generateMiddleware(middlewareId);
114
122
  },
@@ -128,7 +136,10 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
128
136
  }
129
137
  async loadMidwayController() {
130
138
  await this.generator.loadMidwayController(this.configurationOptions.globalPrefix, newRouter => {
131
- this.app.use(newRouter.middleware());
139
+ var _a;
140
+ const dispatchFn = newRouter.middleware();
141
+ dispatchFn._name = `midwayController(${((_a = newRouter === null || newRouter === void 0 ? void 0 : newRouter.opts) === null || _a === void 0 ? void 0 : _a.prefix) || '/'})`;
142
+ this.app.useMiddleware(dispatchFn);
132
143
  });
133
144
  }
134
145
  getFrameworkType() {
@@ -136,10 +147,13 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
136
147
  }
137
148
  async run() {
138
149
  var _a;
150
+ // move egg router to last
151
+ this.app.getMiddleware().findAndInsertLast('eggRouterMiddleware');
139
152
  // load controller
140
153
  await this.loadMidwayController();
141
154
  // restore use method
142
155
  this.app.use = this.app.originUse;
156
+ debug(`[egg]: current middleware = ${this.middlewareManager.getNames()}`);
143
157
  if (!this.isClusterMode) {
144
158
  // https config
145
159
  if (this.configurationOptions.key && this.configurationOptions.cert) {
@@ -170,17 +184,19 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
170
184
  const eggConfig = this.configService.getConfiguration('egg');
171
185
  if (!this.isClusterMode && eggConfig) {
172
186
  const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : eggConfig.port;
173
- new Promise(resolve => {
174
- const args = [customPort];
175
- if (eggConfig.hostname) {
176
- args.push(eggConfig.hostname);
177
- }
178
- args.push(() => {
179
- resolve();
187
+ if (customPort) {
188
+ new Promise(resolve => {
189
+ const args = [customPort];
190
+ if (eggConfig.hostname) {
191
+ args.push(eggConfig.hostname);
192
+ }
193
+ args.push(() => {
194
+ resolve();
195
+ });
196
+ this.server.listen(...args);
197
+ process.env.MIDWAY_HTTP_PORT = String(customPort);
180
198
  });
181
- this.server.listen(...args);
182
- process.env.MIDWAY_HTTP_PORT = String(customPort);
183
- });
199
+ }
184
200
  }
185
201
  }
186
202
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/web",
3
- "version": "3.0.0-beta.7",
3
+ "version": "3.0.0-beta.8",
4
4
  "description": "Midway Web Framework for Egg.js",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -27,8 +27,8 @@
27
27
  ],
28
28
  "license": "MIT",
29
29
  "devDependencies": {
30
- "@midwayjs/decorator": "^3.0.0-beta.7",
31
- "@midwayjs/mock": "^3.0.0-beta.7",
30
+ "@midwayjs/decorator": "^3.0.0-beta.8",
31
+ "@midwayjs/mock": "^3.0.0-beta.8",
32
32
  "dayjs": "^1.10.7",
33
33
  "egg-logger": "^2.4.2",
34
34
  "egg-mock": "^3.26.0",
@@ -43,8 +43,8 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@eggjs/router": "^2.0.0",
46
- "@midwayjs/core": "^3.0.0-beta.7",
47
- "@midwayjs/logger": "^3.0.0-beta.7",
46
+ "@midwayjs/core": "^3.0.0-beta.8",
47
+ "@midwayjs/logger": "^3.0.0-beta.8",
48
48
  "egg": "^2.28.0",
49
49
  "extend2": "^1.0.0",
50
50
  "find-up": "^5.0.0",
@@ -58,5 +58,5 @@
58
58
  "engines": {
59
59
  "node": ">=12"
60
60
  },
61
- "gitHead": "24590729121d9110e2d960db5b5e587cf55a1efc"
61
+ "gitHead": "bfafbdf8798f48d4daac5dd88ad53c6b2f33c110"
62
62
  }