@midwayjs/http-proxy 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.
- package/README.md +35 -0
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +5 -0
- package/dist/configuration.d.ts +7 -0
- package/dist/configuration.js +47 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +22 -0
- package/dist/interface.d.ts +9 -0
- package/dist/interface.js +3 -0
- package/dist/middleware.d.ts +14 -0
- package/dist/middleware.js +133 -0
- package/index.d.ts +8 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
## HTTP 代理组件
|
|
2
|
+
|
|
3
|
+
HTTP 代理组件
|
|
4
|
+
|
|
5
|
+
适用于 `@midwayjs/faas` 、`@midwayjs/web` 、`@midwayjs/koa` 和 `@midwayjs/express` 多种框架的 HTTP 代理组件,支持GET、POST等多种请求方法。
|
|
6
|
+
|
|
7
|
+
### Usage
|
|
8
|
+
|
|
9
|
+
1. 安装依赖
|
|
10
|
+
```shell
|
|
11
|
+
tnpm i @midwayjs/http-proxy --save
|
|
12
|
+
```
|
|
13
|
+
2. 在 configuration 中引入组件,
|
|
14
|
+
```ts
|
|
15
|
+
import * as proxy from '@midwayjs/http-proxy';
|
|
16
|
+
@Configuration({
|
|
17
|
+
imports: [
|
|
18
|
+
// ...other components
|
|
19
|
+
proxy
|
|
20
|
+
],
|
|
21
|
+
})
|
|
22
|
+
export class AutoConfiguration {}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 配置
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
export const httpProxy = [
|
|
29
|
+
{
|
|
30
|
+
host: 'http://127.0.0.1',
|
|
31
|
+
match: /\/assets\/(.*)/,
|
|
32
|
+
target: 'http://127.0.0.1/$1',
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
```
|
|
@@ -0,0 +1,47 @@
|
|
|
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.HttpProxyConfiguration = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const DefaultConfig = require("./config/config.default");
|
|
15
|
+
const core_1 = require("@midwayjs/core");
|
|
16
|
+
const middleware_1 = require("./middleware");
|
|
17
|
+
let HttpProxyConfiguration = class HttpProxyConfiguration {
|
|
18
|
+
async onReady() {
|
|
19
|
+
if (this.httpProxy) {
|
|
20
|
+
this.applicationManager
|
|
21
|
+
.getApplications(['koa', 'faas', 'express', 'egg'])
|
|
22
|
+
.forEach(app => {
|
|
23
|
+
app.useMiddleware(middleware_1.HttpProxyMiddleware);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
__decorate([
|
|
29
|
+
(0, decorator_1.Inject)(),
|
|
30
|
+
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
31
|
+
], HttpProxyConfiguration.prototype, "applicationManager", void 0);
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, decorator_1.Config)('httpProxy'),
|
|
34
|
+
__metadata("design:type", Object)
|
|
35
|
+
], HttpProxyConfiguration.prototype, "httpProxy", void 0);
|
|
36
|
+
HttpProxyConfiguration = __decorate([
|
|
37
|
+
(0, decorator_1.Configuration)({
|
|
38
|
+
namespace: 'upload',
|
|
39
|
+
importConfigs: [
|
|
40
|
+
{
|
|
41
|
+
default: DefaultConfig,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
})
|
|
45
|
+
], HttpProxyConfiguration);
|
|
46
|
+
exports.HttpProxyConfiguration = HttpProxyConfiguration;
|
|
47
|
+
//# sourceMappingURL=configuration.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Configuration = void 0;
|
|
18
|
+
var configuration_1 = require("./configuration");
|
|
19
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.HttpProxyConfiguration; } });
|
|
20
|
+
__exportStar(require("./interface"), exports);
|
|
21
|
+
__exportStar(require("./middleware"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IMiddleware, IMidwayLogger } from '@midwayjs/core';
|
|
3
|
+
import { HttpProxyConfig } from './interface';
|
|
4
|
+
export declare class HttpProxyMiddleware implements IMiddleware<any, any> {
|
|
5
|
+
httpProxy: HttpProxyConfig | HttpProxyConfig[];
|
|
6
|
+
logger: IMidwayLogger;
|
|
7
|
+
resolve(app: any): (req: any, res: any, next: any) => Promise<any>;
|
|
8
|
+
execProxy(ctx: any, req: any, res: any, next: any, isExpress: any): Promise<any>;
|
|
9
|
+
getProxyList(url: any): undefined | {
|
|
10
|
+
proxy: HttpProxyConfig;
|
|
11
|
+
url: URL;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1,133 @@
|
|
|
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.HttpProxyMiddleware = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const axios_1 = require("axios");
|
|
15
|
+
let HttpProxyMiddleware = class HttpProxyMiddleware {
|
|
16
|
+
resolve(app) {
|
|
17
|
+
if (app.getFrameworkType() === decorator_1.MidwayFrameworkType.WEB_EXPRESS) {
|
|
18
|
+
return async (req, res, next) => {
|
|
19
|
+
return this.execProxy(req, req, res, next, true);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return async (ctx, next) => {
|
|
24
|
+
var _a;
|
|
25
|
+
const req = ((_a = ctx.request) === null || _a === void 0 ? void 0 : _a.req) || ctx.request;
|
|
26
|
+
return this.execProxy(ctx, req, ctx, next, false);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async execProxy(ctx, req, res, next, isExpress) {
|
|
31
|
+
var _a, _b, _c;
|
|
32
|
+
const proxyInfo = this.getProxyList(ctx.url);
|
|
33
|
+
if (!proxyInfo) {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
const { proxy, url } = proxyInfo;
|
|
37
|
+
const reqHeaders = {};
|
|
38
|
+
for (const key of Object.keys(req.headers)) {
|
|
39
|
+
if (((_a = proxy.ignoreHeaders) === null || _a === void 0 ? void 0 : _a[key]) || ctx.header[key] === undefined) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
reqHeaders[key.toLowerCase()] = ctx.header[key];
|
|
43
|
+
}
|
|
44
|
+
// X-Forwarded-For
|
|
45
|
+
const forwarded = req.headers['x-forwarded-for'];
|
|
46
|
+
reqHeaders['x-forwarded-for'] = forwarded
|
|
47
|
+
? `${forwarded}, ${ctx.ip}`
|
|
48
|
+
: ctx.ip;
|
|
49
|
+
reqHeaders['host'] = url.host;
|
|
50
|
+
const method = req.method.toUpperCase();
|
|
51
|
+
const targetRes = res.res || res;
|
|
52
|
+
const isStream = targetRes.on && targetRes.writable;
|
|
53
|
+
const reqOptions = {
|
|
54
|
+
method,
|
|
55
|
+
url: url.href,
|
|
56
|
+
headers: reqHeaders,
|
|
57
|
+
responseType: isStream ? 'stream' : 'arrayBuffer',
|
|
58
|
+
};
|
|
59
|
+
if (method === 'POST' || method === 'PUT') {
|
|
60
|
+
reqOptions.data = (_b = req.body) !== null && _b !== void 0 ? _b : (_c = ctx.request) === null || _c === void 0 ? void 0 : _c.body;
|
|
61
|
+
if (req.headers['content-type'] === 'application/x-www-form-urlencoded' &&
|
|
62
|
+
typeof reqOptions.data !== 'string') {
|
|
63
|
+
reqOptions.data = Object.keys(reqOptions.data)
|
|
64
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(reqOptions.data[key])}`)
|
|
65
|
+
.join('&');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const proxyResponse = await (0, axios_1.default)(reqOptions).catch(err => {
|
|
69
|
+
if (!err || !err.response) {
|
|
70
|
+
throw err || new Error('proxy unknown error');
|
|
71
|
+
}
|
|
72
|
+
return err.response;
|
|
73
|
+
});
|
|
74
|
+
res.type = proxyResponse.headers['content-type'];
|
|
75
|
+
Object.keys(proxyResponse.headers).forEach(key => {
|
|
76
|
+
res.set(key, proxyResponse.headers[key]);
|
|
77
|
+
});
|
|
78
|
+
res.status = proxyResponse.status;
|
|
79
|
+
if (isStream) {
|
|
80
|
+
await new Promise(resolve => {
|
|
81
|
+
proxyResponse.data.on('finish', resolve);
|
|
82
|
+
proxyResponse.data.pipe(targetRes);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
res.body = proxyResponse.data;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
getProxyList(url) {
|
|
90
|
+
if (!this.httpProxy) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const proxyList = [].concat(this.httpProxy);
|
|
94
|
+
for (const proxy of proxyList) {
|
|
95
|
+
if (!proxy.match) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (!proxy.match.test(url)) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (proxy.host) {
|
|
102
|
+
if (url[0] === '/') {
|
|
103
|
+
url = proxy.host + url;
|
|
104
|
+
}
|
|
105
|
+
const urlObj = new URL(url);
|
|
106
|
+
return {
|
|
107
|
+
proxy,
|
|
108
|
+
url: urlObj,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else if (proxy.target) {
|
|
112
|
+
const newURL = url.replace(proxy.match, proxy.target);
|
|
113
|
+
return {
|
|
114
|
+
proxy,
|
|
115
|
+
url: new URL(newURL),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
__decorate([
|
|
122
|
+
(0, decorator_1.Config)('httpProxy'),
|
|
123
|
+
__metadata("design:type", Object)
|
|
124
|
+
], HttpProxyMiddleware.prototype, "httpProxy", void 0);
|
|
125
|
+
__decorate([
|
|
126
|
+
(0, decorator_1.Logger)(),
|
|
127
|
+
__metadata("design:type", Object)
|
|
128
|
+
], HttpProxyMiddleware.prototype, "logger", void 0);
|
|
129
|
+
HttpProxyMiddleware = __decorate([
|
|
130
|
+
(0, decorator_1.Middleware)()
|
|
131
|
+
], HttpProxyMiddleware);
|
|
132
|
+
exports.HttpProxyMiddleware = HttpProxyMiddleware;
|
|
133
|
+
//# sourceMappingURL=middleware.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/http-proxy",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Midway Component for http proxy",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
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
|
+
"ci": "npm run test"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**/*.js",
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"index.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"axios": "^0.26.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@midwayjs/core": "^3.1.7-alpha.0",
|
|
29
|
+
"@midwayjs/decorator": "^3.0.10",
|
|
30
|
+
"@midwayjs/express": "^3.1.7-alpha.0",
|
|
31
|
+
"@midwayjs/faas": "^3.1.7-alpha.0",
|
|
32
|
+
"@midwayjs/koa": "^3.1.7-alpha.0",
|
|
33
|
+
"@midwayjs/mock": "^3.1.7-alpha.0",
|
|
34
|
+
"@midwayjs/serverless-app": "^3.1.7-alpha.0",
|
|
35
|
+
"@midwayjs/web": "^3.1.7-alpha.0"
|
|
36
|
+
}
|
|
37
|
+
}
|