/
http-proxy-middleware.ts
187 lines (163 loc) · 5.48 KB
/
http-proxy-middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as httpProxy from 'http-proxy';
import * as _ from 'lodash';
import { createConfig } from './config-factory';
import * as contextMatcher from './context-matcher';
import * as handlers from './handlers';
import { getArrow, getInstance } from './logger';
import * as PathRewriter from './path-rewriter';
import * as Router from './router';
export class HttpProxyMiddleware {
private logger = getInstance();
private config;
private wsInternalSubscribed = false;
private proxyOptions;
private proxy;
private pathRewriter;
constructor(context, opts) {
this.config = createConfig(context, opts);
this.proxyOptions = this.config.options;
// create proxy
this.proxy = httpProxy.createProxyServer({});
this.logger.info(
`[HPM] Proxy created: ${this.config.context} -> ${this.proxyOptions.target}`
);
this.pathRewriter = PathRewriter.createPathRewriter(
this.proxyOptions.pathRewrite
); // returns undefined when "pathRewrite" is not provided
// attach handler to http-proxy events
handlers.init(this.proxy, this.proxyOptions);
// log errors for debug purpose
this.proxy.on('error', this.logError);
// https://github.com/chimurai/http-proxy-middleware/issues/19
// expose function to upgrade externally
(this.middleware as any).upgrade = (req, socket, head) => {
if (!this.wsInternalSubscribed) {
this.handleUpgrade(req, socket, head);
}
};
}
// https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this
public middleware = async (req, res, next) => {
if (this.shouldProxy(this.config.context, req)) {
const activeProxyOptions = await this.prepareProxyRequest(req);
this.proxy.web(req, res, activeProxyOptions);
} else {
next();
}
if (this.proxyOptions.ws === true) {
// use initial request to access the server object to subscribe to http upgrade event
this.catchUpgradeRequest(req.connection.server);
}
};
private catchUpgradeRequest = server => {
if (!this.wsInternalSubscribed) {
server.on('upgrade', this.handleUpgrade);
// prevent duplicate upgrade handling;
// in case external upgrade is also configured
this.wsInternalSubscribed = true;
}
};
private handleUpgrade = async (req, socket, head) => {
if (this.shouldProxy(this.config.context, req)) {
const activeProxyOptions = await this.prepareProxyRequest(req);
this.proxy.ws(req, socket, head, activeProxyOptions);
this.logger.info('[HPM] Upgrading to WebSocket');
}
};
/**
* Determine whether request should be proxied.
*
* @private
* @param {String} context [description]
* @param {Object} req [description]
* @return {Boolean}
*/
private shouldProxy = (context, req) => {
const path = req.originalUrl || req.url;
return contextMatcher.match(context, path, req);
};
/**
* Apply option.router and option.pathRewrite
* Order matters:
* Router uses original path for routing;
* NOT the modified path, after it has been rewritten by pathRewrite
* @param {Object} req
* @return {Object} proxy options
*/
private prepareProxyRequest = async req => {
// https://github.com/chimurai/http-proxy-middleware/issues/17
// https://github.com/chimurai/http-proxy-middleware/issues/94
req.url = req.originalUrl || req.url;
// store uri before it gets rewritten for logging
const originalPath = req.url;
const newProxyOptions = _.assign({}, this.proxyOptions);
// Apply in order:
// 1. option.router
// 2. option.pathRewrite
await this.applyRouter(req, newProxyOptions);
this.applyPathRewrite(req, this.pathRewriter);
// debug logging for both http(s) and websockets
if (this.proxyOptions.logLevel === 'debug') {
const arrow = getArrow(
originalPath,
req.url,
this.proxyOptions.target,
newProxyOptions.target
);
this.logger.debug(
'[HPM] %s %s %s %s',
req.method,
originalPath,
arrow,
newProxyOptions.target
);
}
return newProxyOptions;
};
// Modify option.target when router present.
private applyRouter = async (req, options) => {
let newTarget;
if (options.router) {
newTarget = await Router.getTarget(req, options);
if (newTarget) {
this.logger.debug(
'[HPM] Router new target: %s -> "%s"',
options.target,
newTarget
);
options.target = newTarget;
}
}
};
// rewrite path
private applyPathRewrite = (req, pathRewriter) => {
if (pathRewriter) {
const path = pathRewriter(req.url, req);
if (typeof path === 'string') {
req.url = path;
} else {
this.logger.info(
'[HPM] pathRewrite: No rewritten path found. (%s)',
req.url
);
}
}
};
private logError = (err, req, res) => {
const hostname =
(req.headers && req.headers.host) || (req.hostname || req.host); // (websocket) || (node0.10 || node 4/5)
const target = this.proxyOptions.target.host || this.proxyOptions.target;
const errorMessage =
'[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)';
const errReference =
'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
this.logger.error(
errorMessage,
req.url,
hostname,
target,
err.code || err,
errReference
);
};
}