@midwayjs/express 3.0.0-beta.6 → 3.0.0

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.
@@ -9,27 +9,43 @@ 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.pathMatching = exports.MidwayExpressMiddlewareService = void 0;
12
+ exports.wrapMiddleware = exports.MidwayExpressMiddlewareService = exports.wrapAsyncHandler = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const core_1 = require("@midwayjs/core");
15
+ const util_1 = require("./util");
16
+ function wrapAsyncHandler(fn) {
17
+ if (decorator_1.Types.isAsyncFunction(fn)) {
18
+ return (req, res, next) => {
19
+ return fn(req, res, next).catch(err => {
20
+ next(err);
21
+ });
22
+ };
23
+ }
24
+ else {
25
+ return fn;
26
+ }
27
+ }
28
+ exports.wrapAsyncHandler = wrapAsyncHandler;
15
29
  let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
16
30
  constructor(applicationContext) {
17
31
  this.applicationContext = applicationContext;
18
32
  }
19
- async compose(middleware, name) {
33
+ async compose(middleware, app, name) {
20
34
  if (!Array.isArray(middleware)) {
21
35
  throw new core_1.MidwayCommonError('Middleware stack must be an array');
22
36
  }
23
37
  const newMiddlewareArr = [];
24
38
  for (let fn of middleware) {
25
- if ((0, decorator_1.isClass)(fn) || typeof fn === 'string') {
39
+ if (decorator_1.Types.isClass(fn) || typeof fn === 'string') {
26
40
  if (typeof fn === 'string' &&
27
41
  !this.applicationContext.hasDefinition(fn)) {
28
42
  throw new core_1.MidwayCommonError('Middleware definition not found in midway container');
29
43
  }
30
44
  const classMiddleware = await this.applicationContext.getAsync(fn);
31
45
  if (classMiddleware) {
32
- fn = classMiddleware.resolve();
46
+ fn = await classMiddleware.resolve(app);
47
+ // wrap async middleware
48
+ fn = wrapAsyncHandler(fn);
33
49
  if (!classMiddleware.match && !classMiddleware.ignore) {
34
50
  fn._name = classMiddleware.constructor.name;
35
51
  // just got fn
@@ -38,7 +54,7 @@ let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
38
54
  else {
39
55
  // wrap ignore and match
40
56
  const mw = fn;
41
- const match = pathMatching({
57
+ const match = (0, core_1.pathMatching)({
42
58
  match: classMiddleware.match,
43
59
  ignore: classMiddleware.ignore,
44
60
  });
@@ -56,16 +72,40 @@ let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
56
72
  }
57
73
  }
58
74
  else {
75
+ // wrap async middleware
76
+ fn = wrapAsyncHandler(fn);
59
77
  newMiddlewareArr.push(fn);
60
78
  }
61
79
  }
62
- const composeFn = (req, res, next) => {
63
- (function iter(i, max) {
64
- if (i === max) {
65
- return next();
80
+ const composeFn = (req, res, nextFunction) => {
81
+ let index = -1;
82
+ function dispatch(pos, err) {
83
+ const handler = newMiddlewareArr[pos];
84
+ index = pos;
85
+ if (err || index === newMiddlewareArr.length) {
86
+ return nextFunction(err);
87
+ }
88
+ function next(err) {
89
+ if (pos < index) {
90
+ throw new TypeError('`next()` called multiple times');
91
+ }
92
+ return dispatch(pos + 1, err);
93
+ }
94
+ try {
95
+ Promise.resolve(handler(req, res, next)).then(result => {
96
+ if (result) {
97
+ (0, util_1.sendData)(res, result);
98
+ }
99
+ });
66
100
  }
67
- newMiddlewareArr[i](req, res, iter.bind(this, i + 1, max));
68
- })(0, newMiddlewareArr.length);
101
+ catch (err) {
102
+ // Avoid future errors that could diverge stack execution.
103
+ if (index > pos)
104
+ throw err;
105
+ return next(err);
106
+ }
107
+ }
108
+ return dispatch(0);
69
109
  };
70
110
  if (name) {
71
111
  composeFn._name = name;
@@ -79,41 +119,21 @@ MidwayExpressMiddlewareService = __decorate([
79
119
  __metadata("design:paramtypes", [Object])
80
120
  ], MidwayExpressMiddlewareService);
81
121
  exports.MidwayExpressMiddlewareService = MidwayExpressMiddlewareService;
82
- function pathMatching(options) {
83
- options = options || {};
84
- if (options.match && options.ignore)
85
- throw new core_1.MidwayCommonError('options.match and options.ignore can not both present');
122
+ function wrapMiddleware(mw, options) {
123
+ // support options.enable
124
+ if (options.enable === false)
125
+ return null;
126
+ // support options.match and options.ignore
86
127
  if (!options.match && !options.ignore)
87
- return () => true;
88
- const matchFn = options.match
89
- ? toPathMatch(options.match)
90
- : toPathMatch(options.ignore);
91
- return function pathMatch(ctx) {
92
- const matched = matchFn(ctx);
93
- return options.match ? matched : !matched;
128
+ return mw;
129
+ const match = (0, core_1.pathMatching)(options);
130
+ const fn = (req, res, next) => {
131
+ if (!match(req))
132
+ return next();
133
+ return mw(req, res, next);
94
134
  };
135
+ fn._name = mw._name + 'middlewareWrapper';
136
+ return fn;
95
137
  }
96
- exports.pathMatching = pathMatching;
97
- function toPathMatch(pattern) {
98
- if (typeof pattern === 'string') {
99
- const reg = (0, core_1.pathToRegexp)(pattern, [], { end: false });
100
- if (reg.global)
101
- reg.lastIndex = 0;
102
- return ctx => reg.test(ctx.path);
103
- }
104
- if (pattern instanceof RegExp) {
105
- return ctx => {
106
- if (pattern.global)
107
- pattern.lastIndex = 0;
108
- return pattern.test(ctx.path);
109
- };
110
- }
111
- if (typeof pattern === 'function')
112
- return pattern;
113
- if (Array.isArray(pattern)) {
114
- const matchs = pattern.map(item => toPathMatch(item));
115
- return ctx => matchs.some(match => match(ctx));
116
- }
117
- throw new core_1.MidwayCommonError('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
118
- }
138
+ exports.wrapMiddleware = wrapMiddleware;
119
139
  //# sourceMappingURL=middlewareService.js.map
package/dist/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function sendData(res: any, data: any): void;
2
+ //# sourceMappingURL=util.d.ts.map
package/dist/util.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sendData = void 0;
4
+ function sendData(res, data) {
5
+ if (typeof data === 'number') {
6
+ res.status(res.statusCode).send('' + data);
7
+ }
8
+ else {
9
+ res.status(res.statusCode).send(data);
10
+ }
11
+ }
12
+ exports.sendData = sendData;
13
+ //# sourceMappingURL=util.js.map
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@midwayjs/express",
3
- "version": "3.0.0-beta.6",
3
+ "version": "3.0.0",
4
4
  "description": "Midway Web Framework for Express",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest",
10
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
9
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
10
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
11
11
  "ci": "npm run test"
12
12
  },
13
13
  "keywords": [
@@ -23,15 +23,19 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/decorator": "^3.0.0-beta.6",
27
- "@midwayjs/mock": "^3.0.0-beta.6",
28
- "@types/express": "^4.17.8",
29
- "fs-extra": "^8.0.1"
26
+ "@midwayjs/decorator": "^3.0.0",
27
+ "@midwayjs/logger": "2.14.0",
28
+ "@midwayjs/mock": "^3.0.0",
29
+ "@types/body-parser": "1.19.2",
30
+ "@types/express": "4.17.13",
31
+ "fs-extra": "10.0.0"
30
32
  },
31
33
  "dependencies": {
32
- "@midwayjs/core": "^3.0.0-beta.6",
33
- "@midwayjs/logger": "^3.0.0-beta.6",
34
- "express": "^4.17.1"
34
+ "@midwayjs/core": "^3.0.0",
35
+ "@midwayjs/express-session": "^3.0.0",
36
+ "body-parser": "1.19.1",
37
+ "cookie-parser": "^1.4.6",
38
+ "express": "4.17.2"
35
39
  },
36
40
  "author": "Harry Chen <czy88840616@gmail.com>",
37
41
  "repository": {
@@ -41,5 +45,5 @@
41
45
  "engines": {
42
46
  "node": ">=12"
43
47
  },
44
- "gitHead": "e4595d30b369e36bef21b36f2b3737d8bc2f802d"
48
+ "gitHead": "55c26029bccf7bbb739fa1597e8f418dafa2caa0"
45
49
  }