@fastcar/koa 0.1.18 → 0.1.20

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/annotation.d.ts CHANGED
@@ -6,6 +6,8 @@ type Ret = (target: any) => void;
6
6
 
7
7
  type MRet = (target: any, name: string, descriptor: PropertyDescriptor) => void;
8
8
 
9
+ type PMRet = (target: any, propertyKey: string) => void;
10
+
9
11
  type MiddleWareType = (...args: any) => Koa.Middleware | Koa.Middleware[];
10
12
 
11
13
  //关于请求方式注解
@@ -44,3 +46,5 @@ export function EnableKoa(target: any): void;
44
46
 
45
47
  //中间件添加拓展
46
48
  export function KoaMiddleware(...args: MiddleWareType[]): Ret;
49
+
50
+ export function KoaApp(): PMRet;
package/index.d.ts CHANGED
@@ -7,7 +7,7 @@ type MiddleWareType = (...args: any) => Koa.Middleware | Koa.Middleware[];
7
7
  export class KoaApplication {
8
8
  protected app: FastCarApplication;
9
9
  protected sysLogger: Logger;
10
- protected koaApp: Koa;
10
+ public koaApp: Koa;
11
11
 
12
12
  /***
13
13
  * @version 1.0 加载中间件
@@ -38,6 +38,17 @@ export type KoaConfig = {
38
38
  koaBodyOptions?: { [key: string]: any }; //文件上传的解析
39
39
  koaBodyParser?: { [key: string]: any }; //解析请求
40
40
  extra?: { [key: string]: any }; //拓展设置
41
+ koaProxy?: {
42
+ //基于http-proxy-middleware来进行拓展
43
+ [key: string]: {
44
+ target: string;
45
+ changeOrigin?: boolean;
46
+ pathRewrite?: {
47
+ [key: string]: string;
48
+ };
49
+ ws: boolean;
50
+ } & any;
51
+ };
41
52
  };
42
53
 
43
54
  //全局异常捕捉 可以用自定义的替换这个函数
@@ -55,10 +66,16 @@ export function KoaCors(app: FastCarApplication): MiddleWareType;
55
66
  //解析静态文件
56
67
  export function KoaStatic(app: FastCarApplication): MiddleWareType;
57
68
 
69
+ //反向代理文件
70
+ export function KoaProxy(app: FastCarApplication): MiddleWareType;
71
+
58
72
  //支持api说明
59
73
  export function Swagger(app: FastCarApplication): MiddleWareType;
60
74
 
61
75
  //增强multi文件解析
62
76
  export function KoaMulter(app: FastCarApplication): MiddleWareType;
63
77
 
78
+ //校验header 头编码是否错误
79
+ export function HeaderCoding(): MiddleWareType;
80
+
64
81
  export * from "./src/type/DesignMeta";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastcar/koa",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "homepage": "https://github.com/williamDazhangyu/fast-car",
5
5
  "description": "fastcar框架下对koa的包装",
6
6
  "main": "target/index.js",
@@ -30,7 +30,7 @@
30
30
  "node": ">=18"
31
31
  },
32
32
  "devDependencies": {
33
- "@fastcar/core": "0.3.1",
33
+ "@fastcar/core": "^0.3.10",
34
34
  "@fastcar/server": "^0.0.3",
35
35
  "@types/co-body": "^6.1.3",
36
36
  "@types/koa": "^2.13.5",
@@ -51,12 +51,15 @@
51
51
  "@types/koa-range": "^0.3.2",
52
52
  "@types/koa-static": "^4.0.2",
53
53
  "@types/koa2-cors": "^2.0.2",
54
+ "http-proxy-middleware": "^3.0.5",
54
55
  "koa-body": "^4.2.0",
55
56
  "koa-mount": "^4.0.0",
56
57
  "koa-range": "^0.3.0",
57
58
  "koa-static": "^5.0.0",
59
+ "koa2-connect": "^1.0.2",
58
60
  "koa2-cors": "^2.0.6",
59
- "multer": "*"
61
+ "multer": "*",
62
+ "path-to-regexp": "^8.3.0"
60
63
  },
61
64
  "peerDependenciesMeta": {
62
65
  "@types/koa-mount": {
@@ -8,7 +8,6 @@ import { DesignMeta } from "./type/DesignMeta";
8
8
  import { TypeUtil, ValidationUtil } from "@fastcar/core/utils";
9
9
  import { KoaConfig } from "./type/KoaConfig";
10
10
  import { ServerApplication } from "@fastcar/server";
11
-
12
11
  /***
13
12
  * @version 1.0 koa基础组件启动
14
13
  *
@@ -22,7 +21,7 @@ export default class KoaApplication {
22
21
  @Log("koa")
23
22
  protected koaLogger!: Logger;
24
23
 
25
- protected koaApp: Koa;
24
+ public koaApp: Koa;
26
25
 
27
26
  @Autowired
28
27
  private serverApplication!: ServerApplication;
@@ -0,0 +1,21 @@
1
+ import { FastCarMetaData } from "@fastcar/core";
2
+ import KoaApplication from "../KoaApplication";
3
+
4
+ const koaApp = "koaApp";
5
+
6
+ /**
7
+ * @version 1.0.0 获取koa-app方法
8
+ */
9
+ export default function KoaApp(target: any, propertyKey: string) {
10
+ Reflect.defineMetadata(
11
+ FastCarMetaData.InjectionValue,
12
+ [
13
+ {
14
+ key: koaApp,
15
+ propertyKey: propertyKey,
16
+ relayTarget: KoaApplication,
17
+ },
18
+ ],
19
+ target
20
+ );
21
+ }
@@ -3,8 +3,7 @@ import { MethodType } from "../../type/MethodType";
3
3
  import { DesignMeta } from "../../type/DesignMeta";
4
4
 
5
5
  export default function AddMapping(target: any, info: MethodType) {
6
-
7
- if(!info.url) {
6
+ if (!info.url) {
8
7
  info.url = info.method;
9
8
  }
10
9
 
@@ -19,13 +18,18 @@ export default function AddMapping(target: any, info: MethodType) {
19
18
  Reflect.defineMetadata(DesignMeta.ROUTER_MAP, routerMap, target);
20
19
  }
21
20
 
22
- let curr = routerMap.get(info.url);
23
- if (!curr) {
24
- routerMap.set(info.url, info);
25
- } else {
26
- if (info.url != curr.url) {
27
- console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
21
+ //修改主键 根据url-method作为唯一主键
22
+ info.request.forEach((m) => {
23
+ let urlKey = `${info.url}:${m}`;
24
+
25
+ let curr = routerMap.get(urlKey);
26
+ if (!curr) {
27
+ routerMap.set(urlKey, info);
28
+ } else {
29
+ if (info.url != curr.url) {
30
+ console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
31
+ }
32
+ curr.request = [...info.request, ...curr.request];
28
33
  }
29
- curr.request = [...info.request, ...curr.request];
30
- }
34
+ });
31
35
  }
package/src/annotation.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import EnableKoa from "./annotation/EnableKoa";
2
+ import KoaApp from "./annotation/KoaApp";
2
3
  import KoaMiddleware from "./annotation/KoaMiddleware";
3
4
  import AddMapping from "./annotation/router/AddMapping";
4
5
  import AllMapping from "./annotation/router/AllMapping";
@@ -40,4 +41,5 @@ export {
40
41
  PATCH,
41
42
  ALL,
42
43
  REQUEST,
44
+ KoaApp,
43
45
  };
package/src/index.ts CHANGED
@@ -7,5 +7,6 @@ import KoaCors from "./middleware/KoaCors";
7
7
  import KoaStatic from "./middleware/KoaStatic";
8
8
  import { DesignMeta } from "./type/DesignMeta";
9
9
  import KoaMulter from "./middleware/KoaMulter";
10
+ import HeaderCoding from "./middleware/HeaderCoding";
10
11
 
11
- export { KoaApplication, KoaConfig, ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors, KoaStatic, KoaMulter, DesignMeta };
12
+ export { KoaApplication, KoaConfig, ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors, KoaStatic, KoaMulter, HeaderCoding, DesignMeta };
@@ -0,0 +1,14 @@
1
+ //处理由于第三方头不规范导致的问题
2
+ import * as koa from "koa";
3
+
4
+ const ValidEncoding = ["gzip", "deflate", "br", "identity"];
5
+
6
+ export default function HeaderCoding() {
7
+ return async function (ctx: koa.Context, next: Function) {
8
+ let encoding = ctx.headers["content-encoding"];
9
+ if (encoding && !ValidEncoding.includes(encoding)) {
10
+ delete ctx.headers["content-encoding"];
11
+ }
12
+ await next();
13
+ };
14
+ }
@@ -1,7 +1,5 @@
1
- import { FastCarApplication } from "@fastcar/core";
2
-
3
1
  //对文件内容做解析
4
- export default function KoaMulter(app: FastCarApplication) {
2
+ export default function KoaMulter() {
5
3
  const multer = require("@koa/multer");
6
4
 
7
5
  return multer().single();
@@ -0,0 +1,29 @@
1
+ import { FastCarApplication } from "@fastcar/core";
2
+ import * as Koa from "koa";
3
+ import { KoaConfig } from "../type/KoaConfig";
4
+
5
+ //反向代理扩展
6
+ export default function KoaProxy(app: FastCarApplication): Koa.Middleware {
7
+ const httpProxy = require("http-proxy-middleware");
8
+ const k2c = require("koa2-connect");
9
+ const { match } = require("path-to-regexp");
10
+
11
+ return async function (ctx, next) {
12
+ let koaConfig: KoaConfig = app.getSetting("koa");
13
+
14
+ if (koaConfig && koaConfig.koaProxy) {
15
+ const { path } = ctx;
16
+ for (const route of Object.keys(koaConfig.koaProxy)) {
17
+ if (
18
+ match(route, {
19
+ decode: decodeURIComponent,
20
+ })(path)
21
+ ) {
22
+ return await k2c(httpProxy.createProxyMiddleware(koaConfig.koaProxy[route]))(ctx, next);
23
+ }
24
+ }
25
+ }
26
+
27
+ await next();
28
+ };
29
+ }
@@ -7,4 +7,15 @@ export type KoaConfig = {
7
7
  koaBodyOptions?: { [key: string]: any }; //文件上传的解析
8
8
  koaBodyParser?: { [key: string]: any }; //bodyParser.Options; //解析请求
9
9
  extra?: { [key: string]: any }; //拓展设置
10
+ koaProxy?: {
11
+ //基于http-proxy-middleware来进行拓展
12
+ [key: string]: {
13
+ target: string;
14
+ changeOrigin?: boolean;
15
+ pathRewrite?: {
16
+ [key: string]: string;
17
+ };
18
+ ws: boolean;
19
+ } & any;
20
+ };
10
21
  };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = KoaApp;
4
+ const core_1 = require("@fastcar/core");
5
+ const KoaApplication_1 = require("../KoaApplication");
6
+ const koaApp = "koaApp";
7
+ /**
8
+ * @version 1.0.0 获取koa-app方法
9
+ */
10
+ function KoaApp(target, propertyKey) {
11
+ Reflect.defineMetadata(core_1.FastCarMetaData.InjectionValue, [
12
+ {
13
+ key: koaApp,
14
+ propertyKey: propertyKey,
15
+ relayTarget: KoaApplication_1.default,
16
+ },
17
+ ], target);
18
+ }
@@ -16,14 +16,18 @@ function AddMapping(target, info) {
16
16
  routerMap = new Map();
17
17
  Reflect.defineMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, routerMap, target);
18
18
  }
19
- let curr = routerMap.get(info.url);
20
- if (!curr) {
21
- routerMap.set(info.url, info);
22
- }
23
- else {
24
- if (info.url != curr.url) {
25
- console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
19
+ //修改主键 根据url-method作为唯一主键
20
+ info.request.forEach((m) => {
21
+ let urlKey = `${info.url}:${m}`;
22
+ let curr = routerMap.get(urlKey);
23
+ if (!curr) {
24
+ routerMap.set(urlKey, info);
26
25
  }
27
- curr.request = [...info.request, ...curr.request];
28
- }
26
+ else {
27
+ if (info.url != curr.url) {
28
+ console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
29
+ }
30
+ curr.request = [...info.request, ...curr.request];
31
+ }
32
+ });
29
33
  }
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REQUEST = exports.ALL = exports.PATCH = exports.PUT = exports.DELETE = exports.POST = exports.GET = exports.KoaMiddleware = exports.EnableKoa = exports.RequestMapping = exports.PutMapping = exports.PostMapping = exports.PatchMapping = exports.GetMapping = exports.DeleteMapping = exports.AllMapping = exports.AddMapping = void 0;
3
+ exports.KoaApp = exports.REQUEST = exports.ALL = exports.PATCH = exports.PUT = exports.DELETE = exports.POST = exports.GET = exports.KoaMiddleware = exports.EnableKoa = exports.RequestMapping = exports.PutMapping = exports.PostMapping = exports.PatchMapping = exports.GetMapping = exports.DeleteMapping = exports.AllMapping = exports.AddMapping = void 0;
4
4
  const EnableKoa_1 = require("./annotation/EnableKoa");
5
5
  exports.EnableKoa = EnableKoa_1.default;
6
+ const KoaApp_1 = require("./annotation/KoaApp");
7
+ exports.KoaApp = KoaApp_1.default;
6
8
  const KoaMiddleware_1 = require("./annotation/KoaMiddleware");
7
9
  exports.KoaMiddleware = KoaMiddleware_1.default;
8
10
  const AddMapping_1 = require("./annotation/router/AddMapping");
package/target/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DesignMeta = exports.KoaMulter = exports.KoaStatic = exports.KoaCors = exports.KoaBodyParser = exports.KoaBody = exports.ExceptionGlobalHandler = exports.KoaApplication = void 0;
3
+ exports.DesignMeta = exports.HeaderCoding = exports.KoaMulter = exports.KoaStatic = exports.KoaCors = exports.KoaBodyParser = exports.KoaBody = exports.ExceptionGlobalHandler = exports.KoaApplication = void 0;
4
4
  const KoaApplication_1 = require("./KoaApplication");
5
5
  exports.KoaApplication = KoaApplication_1.default;
6
6
  const ExceptionGlobalHandler_1 = require("./middleware/ExceptionGlobalHandler");
@@ -17,3 +17,5 @@ const DesignMeta_1 = require("./type/DesignMeta");
17
17
  Object.defineProperty(exports, "DesignMeta", { enumerable: true, get: function () { return DesignMeta_1.DesignMeta; } });
18
18
  const KoaMulter_1 = require("./middleware/KoaMulter");
19
19
  exports.KoaMulter = KoaMulter_1.default;
20
+ const HeaderCoding_1 = require("./middleware/HeaderCoding");
21
+ exports.HeaderCoding = HeaderCoding_1.default;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = HeaderCoding;
4
+ const ValidEncoding = ["gzip", "deflate", "br", "identity"];
5
+ function HeaderCoding() {
6
+ return async function (ctx, next) {
7
+ let encoding = ctx.headers["content-encoding"];
8
+ if (encoding && !ValidEncoding.includes(encoding)) {
9
+ delete ctx.headers["content-encoding"];
10
+ }
11
+ await next();
12
+ };
13
+ }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = KoaMulter;
4
4
  //对文件内容做解析
5
- function KoaMulter(app) {
5
+ function KoaMulter() {
6
6
  const multer = require("@koa/multer");
7
7
  return multer().single();
8
8
  }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = KoaProxy;
4
+ //反向代理扩展
5
+ function KoaProxy(app) {
6
+ const httpProxy = require("http-proxy-middleware");
7
+ const k2c = require("koa2-connect");
8
+ const { match } = require("path-to-regexp");
9
+ return async function (ctx, next) {
10
+ let koaConfig = app.getSetting("koa");
11
+ if (koaConfig && koaConfig.koaProxy) {
12
+ const { path } = ctx;
13
+ for (const route of Object.keys(koaConfig.koaProxy)) {
14
+ if (match(route, {
15
+ decode: decodeURIComponent,
16
+ })(path)) {
17
+ return await k2c(httpProxy.createProxyMiddleware(koaConfig.koaProxy[route]))(ctx, next);
18
+ }
19
+ }
20
+ }
21
+ await next();
22
+ };
23
+ }
package/test/logs/sys.log CHANGED
@@ -141,3 +141,45 @@
141
141
  {"timestamp":"2025-07-10 13:33:04.671","level":"INFO","label":"sys","message":"http server is running in 1234"}
142
142
  {"timestamp":"2025-07-10 13:33:04.672","level":"INFO","label":"sys","message":"start server koaSimple is run"}
143
143
  {"timestamp":"2025-07-10 13:33:04.673","level":"INFO","label":"sys","message":"version 1.0.0"}
144
+ {"timestamp":"2025-07-29 16:31:09.811","level":"INFO","label":"sys","message":"Start scanning component"}
145
+ {"timestamp":"2025-07-29 16:31:11.641","level":"INFO","label":"sys","message":"Complete component scan"}
146
+ {"timestamp":"2025-07-29 16:31:11.641","level":"INFO","label":"sys","message":"Call application initialization method"}
147
+ {"timestamp":"2025-07-29 16:31:11.648","level":"INFO","label":"sys","message":"http server is running in 1234"}
148
+ {"timestamp":"2025-07-29 16:31:11.648","level":"INFO","label":"sys","message":"start server koaSimple is run"}
149
+ {"timestamp":"2025-07-29 16:31:11.649","level":"INFO","label":"sys","message":"version 1.0.0"}
150
+ {"timestamp":"2025-12-22 17:28:39.115","level":"INFO","label":"sys","message":"Start scanning component"}
151
+ {"timestamp":"2025-12-22 17:28:41.189","level":"INFO","label":"sys","message":"Complete component scan"}
152
+ {"timestamp":"2025-12-22 17:28:41.190","level":"INFO","label":"sys","message":"Call application initialization method"}
153
+ {"timestamp":"2025-12-22 17:28:43.337","level":"INFO","label":"sys","message":"http server is running in 1234"}
154
+ {"timestamp":"2025-12-22 17:28:43.338","level":"INFO","label":"sys","message":"start server koaSimple is run"}
155
+ {"timestamp":"2025-12-22 17:28:43.339","level":"INFO","label":"sys","message":"version 1.0.0"}
156
+ {"timestamp":"2025-12-22 17:31:22.709","level":"INFO","label":"sys","message":"Start scanning component"}
157
+ {"timestamp":"2025-12-22 17:31:23.51","level":"INFO","label":"sys","message":"Complete component scan"}
158
+ {"timestamp":"2025-12-22 17:31:23.52","level":"INFO","label":"sys","message":"Call application initialization method"}
159
+ {"timestamp":"2025-12-22 17:31:23.100","level":"INFO","label":"sys","message":"http server is running in 1234"}
160
+ {"timestamp":"2025-12-22 17:31:23.101","level":"INFO","label":"sys","message":"start server koaSimple is run"}
161
+ {"timestamp":"2025-12-22 17:31:23.101","level":"INFO","label":"sys","message":"version 1.0.0"}
162
+ {"timestamp":"2025-12-22 17:32:19.934","level":"INFO","label":"sys","message":"Start scanning component"}
163
+ {"timestamp":"2025-12-22 17:32:20.252","level":"INFO","label":"sys","message":"Complete component scan"}
164
+ {"timestamp":"2025-12-22 17:32:20.253","level":"INFO","label":"sys","message":"Call application initialization method"}
165
+ {"timestamp":"2025-12-22 17:32:20.303","level":"INFO","label":"sys","message":"http server is running in 1234"}
166
+ {"timestamp":"2025-12-22 17:32:20.304","level":"INFO","label":"sys","message":"start server koaSimple is run"}
167
+ {"timestamp":"2025-12-22 17:32:20.305","level":"INFO","label":"sys","message":"version 1.0.0"}
168
+ {"timestamp":"2025-12-23 10:47:56.901","level":"INFO","label":"sys","message":"Start scanning component"}
169
+ {"timestamp":"2025-12-23 10:47:59.717","level":"INFO","label":"sys","message":"Complete component scan"}
170
+ {"timestamp":"2025-12-23 10:47:59.718","level":"INFO","label":"sys","message":"Call application initialization method"}
171
+ {"timestamp":"2025-12-23 10:47:59.730","level":"INFO","label":"sys","message":"http server is running in 1234"}
172
+ {"timestamp":"2025-12-23 10:47:59.731","level":"INFO","label":"sys","message":"start server koaSimple is run"}
173
+ {"timestamp":"2025-12-23 10:47:59.731","level":"INFO","label":"sys","message":"version 1.0.0"}
174
+ {"timestamp":"2025-12-23 10:49:47.715","level":"INFO","label":"sys","message":"Start scanning component"}
175
+ {"timestamp":"2025-12-23 10:49:48.98","level":"INFO","label":"sys","message":"Complete component scan"}
176
+ {"timestamp":"2025-12-23 10:49:48.99","level":"INFO","label":"sys","message":"Call application initialization method"}
177
+ {"timestamp":"2025-12-23 10:49:48.106","level":"INFO","label":"sys","message":"http server is running in 1234"}
178
+ {"timestamp":"2025-12-23 10:49:48.106","level":"INFO","label":"sys","message":"start server koaSimple is run"}
179
+ {"timestamp":"2025-12-23 10:49:48.107","level":"INFO","label":"sys","message":"version 1.0.0"}
180
+ {"timestamp":"2025-12-23 10:59:33.290","level":"INFO","label":"sys","message":"Start scanning component"}
181
+ {"timestamp":"2025-12-23 10:59:33.820","level":"INFO","label":"sys","message":"Complete component scan"}
182
+ {"timestamp":"2025-12-23 10:59:33.822","level":"INFO","label":"sys","message":"Call application initialization method"}
183
+ {"timestamp":"2025-12-23 10:59:33.833","level":"INFO","label":"sys","message":"http server is running in 1234"}
184
+ {"timestamp":"2025-12-23 10:59:33.834","level":"INFO","label":"sys","message":"start server koaSimple is run"}
185
+ {"timestamp":"2025-12-23 10:59:33.835","level":"INFO","label":"sys","message":"version 1.0.0"}
@@ -8,6 +8,7 @@ settings:
8
8
  swagger:
9
9
  enable: true
10
10
  api: { "index": "/public/api/index.yaml" }
11
+ koaProxy: { "/:path(.*).php": { target: "http://localhost.better.lg", changeOrigin: true }, "/api/:path*": { target: "https://localhost.better.lg", "changeOrigin": true } }
11
12
  extra: #额外配置支持
12
13
  cors: #跨域支持示范
13
14
  origin: "https://localhost:1234"