@midwayjs/express 3.0.0-beta.8 → 3.0.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/README.md +2 -2
- package/dist/config/config.default.d.ts +22 -0
- package/dist/config/config.default.js +27 -0
- package/dist/config/config.default.js.map +1 -0
- package/dist/configuration.d.ts +4 -1
- package/dist/configuration.js +46 -1
- package/dist/configuration.js.map +1 -0
- package/dist/framework.d.ts +8 -5
- package/dist/framework.js +87 -26
- package/dist/framework.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/interface.d.ts +37 -11
- package/dist/interface.js.map +1 -0
- package/dist/logger.d.ts +2 -2
- package/dist/logger.js.map +1 -0
- package/dist/middlewareService.d.ts +4 -3
- package/dist/middlewareService.js +50 -10
- package/dist/middlewareService.js.map +1 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +13 -0
- package/dist/util.js.map +1 -0
- package/package.json +17 -12
- package/CHANGELOG.md +0 -985
|
@@ -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.wrapMiddleware = 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 (
|
|
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
|
|
@@ -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,
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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);
|
|
66
87
|
}
|
|
67
|
-
|
|
68
|
-
|
|
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
|
+
});
|
|
100
|
+
}
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewareService.js","sourceRoot":"","sources":["../src/middlewareService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAuE;AACvE,yCAMwB;AAGxB,iCAAkC;AAElC,SAAgB,gBAAgB,CAAC,EAAE;IACjC,IAAI,iBAAK,CAAC,eAAe,CAAC,EAAE,CAAC,EAAE;QAC7B,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;YACxB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;gBACpC,IAAI,CAAC,GAAG,CAAC,CAAC;YACZ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;KACH;SAAM;QACL,OAAO,EAAE,CAAC;KACX;AACH,CAAC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F,EAAE,CAAC,KAAK,GAAI,EAAU,CAAC,KAAK,GAAG,mBAAmB,CAAC;IACnD,OAAO,EAAE,CAAC;AACZ,CAAC;AAdD,wCAcC","sourcesContent":["import { Types, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';\nimport {\n IMidwayContainer,\n MidwayCommonError,\n CommonMiddleware,\n FunctionMiddleware,\n pathMatching,\n} from '@midwayjs/core';\nimport { Context, IMidwayExpressMiddleware, Application } from './interface';\nimport { NextFunction, Response } from 'express';\nimport { sendData } from './util';\n\nexport function wrapAsyncHandler(fn): any {\n if (Types.isAsyncFunction(fn)) {\n return (req, res, next) => {\n return fn(req, res, next).catch(err => {\n next(err);\n });\n };\n } else {\n return fn;\n }\n}\n\n@Provide()\n@Scope(ScopeEnum.Singleton)\nexport class MidwayExpressMiddlewareService {\n constructor(readonly applicationContext: IMidwayContainer) {}\n\n async compose(\n middleware: Array<\n CommonMiddleware<Context, Response, NextFunction> | string\n >,\n app: Application,\n name?: string\n ) {\n if (!Array.isArray(middleware)) {\n throw new MidwayCommonError('Middleware stack must be an array');\n }\n\n const newMiddlewareArr = [];\n\n for (let fn of middleware) {\n if (Types.isClass(fn) || typeof fn === 'string') {\n if (\n typeof fn === 'string' &&\n !this.applicationContext.hasDefinition(fn)\n ) {\n throw new MidwayCommonError(\n 'Middleware definition not found in midway container'\n );\n }\n const classMiddleware =\n await this.applicationContext.getAsync<IMidwayExpressMiddleware>(\n fn as any\n );\n if (classMiddleware) {\n fn = await classMiddleware.resolve(app);\n // wrap async middleware\n fn = wrapAsyncHandler(fn);\n if (!classMiddleware.match && !classMiddleware.ignore) {\n (fn as any)._name = classMiddleware.constructor.name;\n // just got fn\n newMiddlewareArr.push(fn);\n } else {\n // wrap ignore and match\n const mw = fn as any;\n const match = pathMatching({\n match: classMiddleware.match,\n ignore: classMiddleware.ignore,\n });\n fn = (req, res, next) => {\n if (!match(req)) return next();\n return mw(req, res, next);\n };\n (fn as any)._name = classMiddleware.constructor.name;\n newMiddlewareArr.push(fn);\n }\n } else {\n throw new MidwayCommonError('Middleware must have resolve method!');\n }\n } else {\n // wrap async middleware\n fn = wrapAsyncHandler(fn);\n newMiddlewareArr.push(fn);\n }\n }\n\n const composeFn = (\n req: Context,\n res: Response,\n nextFunction: NextFunction\n ) => {\n let index = -1;\n function dispatch(pos: number, err?: Error | null) {\n const handler = newMiddlewareArr[pos];\n index = pos;\n if (err || index === newMiddlewareArr.length) {\n return nextFunction(err);\n }\n\n function next(err?: Error | null) {\n if (pos < index) {\n throw new TypeError('`next()` called multiple times');\n }\n return dispatch(pos + 1, err);\n }\n\n try {\n Promise.resolve(handler(req, res, next)).then(result => {\n if (result) {\n sendData(res, result);\n }\n });\n } catch (err) {\n // Avoid future errors that could diverge stack execution.\n if (index > pos) throw err;\n return next(err);\n }\n }\n return dispatch(0);\n };\n if (name) {\n composeFn._name = name;\n }\n return composeFn;\n }\n}\n\nexport function wrapMiddleware(mw: FunctionMiddleware<any, any, any>, options) {\n // support options.enable\n if (options.enable === false) return null;\n\n // support options.match and options.ignore\n if (!options.match && !options.ignore) return mw;\n const match = pathMatching(options);\n\n const fn = (req, res, next) => {\n if (!match(req)) return next();\n return mw(req, res, next);\n };\n fn._name = (mw as any)._name + 'middlewareWrapper';\n return fn;\n}\n"]}
|
package/dist/util.d.ts
ADDED
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/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;AAAA,SAAgB,QAAQ,CAAC,GAAG,EAAE,IAAI;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;KAC5C;SAAM;QACL,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KACvC;AACH,CAAC;AAND,4BAMC","sourcesContent":["export function sendData(res, data) {\n if (typeof data === 'number') {\n res.status(res.statusCode).send('' + data);\n } else {\n res.status(res.statusCode).send(data);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/express",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
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": [
|
|
@@ -19,19 +19,24 @@
|
|
|
19
19
|
],
|
|
20
20
|
"files": [
|
|
21
21
|
"dist/**/*.js",
|
|
22
|
-
"dist/**/*.d.ts"
|
|
22
|
+
"dist/**/*.d.ts",
|
|
23
|
+
"dist/**/*.js.map"
|
|
23
24
|
],
|
|
24
25
|
"license": "MIT",
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/decorator": "^3.0.
|
|
27
|
-
"@midwayjs/
|
|
28
|
-
"@
|
|
29
|
-
"
|
|
27
|
+
"@midwayjs/decorator": "^3.0.2",
|
|
28
|
+
"@midwayjs/logger": "^2.14.0",
|
|
29
|
+
"@midwayjs/mock": "^3.0.2",
|
|
30
|
+
"@types/body-parser": "1.19.2",
|
|
31
|
+
"@types/express": "4.17.13",
|
|
32
|
+
"fs-extra": "10.0.0"
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@midwayjs/core": "^3.0.
|
|
33
|
-
"@midwayjs/
|
|
34
|
-
"
|
|
35
|
+
"@midwayjs/core": "^3.0.2",
|
|
36
|
+
"@midwayjs/express-session": "^3.0.2",
|
|
37
|
+
"body-parser": "1.19.1",
|
|
38
|
+
"cookie-parser": "^1.4.6",
|
|
39
|
+
"express": "4.17.2"
|
|
35
40
|
},
|
|
36
41
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
37
42
|
"repository": {
|
|
@@ -41,5 +46,5 @@
|
|
|
41
46
|
"engines": {
|
|
42
47
|
"node": ">=12"
|
|
43
48
|
},
|
|
44
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "ca77247d229978a736e79bb208579c014ed226fc"
|
|
45
50
|
}
|