|
1 |
| -var httpProxy = require('http-proxy'); |
2 |
| -var configFactory = require('./lib/config-factory'); |
3 |
| -var handlers = require('./lib/handlers'); |
4 |
| -var contextMatcher = require('./lib/context-matcher'); |
5 |
| -var PathRewriter = require('./lib/path-rewriter'); |
6 |
| -var ProxyTable = require('./lib/proxy-table'); |
7 |
| -var logger = require('./lib/logger').getInstance(); |
8 |
| -var getArrow = require('./lib/logger').getArrow; |
9 |
| - |
10 |
| -var httpProxyMiddleware = function(context, opts) { |
11 |
| - var isWsUpgradeListened = false; |
12 |
| - var config = configFactory.createConfig(context, opts); |
13 |
| - var proxyOptions = config.options; |
14 |
| - |
15 |
| - // create proxy |
16 |
| - var proxy = httpProxy.createProxyServer(proxyOptions); |
17 |
| - logger.info('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); |
18 |
| - |
19 |
| - var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided |
20 |
| - |
21 |
| - // attach handler to http-proxy events |
22 |
| - handlers.init(proxy, proxyOptions); |
23 |
| - |
24 |
| - // log errors for debug purpose |
25 |
| - proxy.on('error', logError); |
26 |
| - |
27 |
| - // https://github.com/chimurai/http-proxy-middleware/issues/19 |
28 |
| - // expose function to upgrade externally |
29 |
| - middleware.upgrade = function(req, socket, head) { |
30 |
| - handleUpgrade(req, socket, head); |
31 |
| - isWsUpgradeListened = true; |
32 |
| - }; |
33 |
| - |
34 |
| - return middleware; |
35 |
| - |
36 |
| - function middleware(req, res, next) { |
37 |
| - // https://github.com/chimurai/http-proxy-middleware/issues/17 |
38 |
| - req.url = req.originalUrl; |
39 |
| - |
40 |
| - if (contextMatcher.match(config.context, req.url, req)) { |
41 |
| - var activeProxyOptions = prepareProxyRequest(req); |
42 |
| - proxy.web(req, res, activeProxyOptions); |
43 |
| - } else { |
44 |
| - next(); |
45 |
| - } |
46 |
| - |
47 |
| - if (proxyOptions.ws === true) { |
48 |
| - catchUpgradeRequest(req.connection.server); |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - function catchUpgradeRequest(server) { |
53 |
| - // make sure only 1 handle listens to server's upgrade request. |
54 |
| - if (isWsUpgradeListened === true) { |
55 |
| - return; |
56 |
| - } |
57 |
| - |
58 |
| - server.on('upgrade', handleUpgrade); |
59 |
| - isWsUpgradeListened = true; |
60 |
| - } |
61 |
| - |
62 |
| - function handleUpgrade(req, socket, head) { |
63 |
| - if (contextMatcher.match(config.context, req.url, req)) { |
64 |
| - var activeProxyOptions = prepareProxyRequest(req); |
65 |
| - proxy.ws(req, socket, head, activeProxyOptions); |
66 |
| - logger.info('[HPM] Upgrading to WebSocket'); |
67 |
| - } |
68 |
| - } |
69 |
| - |
70 |
| - /** |
71 |
| - * Apply option.proxyTable and option.pathRewrite |
72 |
| - * Order matters: |
73 |
| - ProxyTable uses original path for routing; |
74 |
| - NOT the modified path, after it has been rewritten by pathRewrite |
75 |
| - */ |
76 |
| - function prepareProxyRequest(req) { |
77 |
| - // store uri before it gets rewritten for logging |
78 |
| - var originalPath = req.url; |
79 |
| - |
80 |
| - // Apply in order: |
81 |
| - // 1. option.proxyTable |
82 |
| - // 2. option.pathRewrite |
83 |
| - var alteredProxyOptions = __applyProxyTableOption(req, proxyOptions); |
84 |
| - __applyPathRewrite(pathRewriter, req); |
85 |
| - |
86 |
| - // debug logging for both http(s) and websockets |
87 |
| - if (proxyOptions.logLevel === 'debug') { |
88 |
| - var arrow = getArrow(originalPath, req.url, proxyOptions.target, alteredProxyOptions.target); |
89 |
| - logger.debug('[HPM] %s %s %s %s', req.method, originalPath, arrow, alteredProxyOptions.target); |
90 |
| - } |
91 |
| - |
92 |
| - return alteredProxyOptions; |
93 |
| - } |
94 |
| - |
95 |
| - // Modify option.target when proxyTable present. |
96 |
| - // return altered options |
97 |
| - function __applyProxyTableOption(req) { |
98 |
| - var result = proxyOptions; |
99 |
| - |
100 |
| - if (proxyOptions.proxyTable) { |
101 |
| - result = ProxyTable.createProxyOptions(req, proxyOptions); |
102 |
| - } |
103 |
| - |
104 |
| - return result; |
105 |
| - } |
106 |
| - |
107 |
| - // rewrite path |
108 |
| - function __applyPathRewrite(pathRewriter, req) { |
109 |
| - if (pathRewriter) { |
110 |
| - var path = pathRewriter(req.url, req); |
111 |
| - |
112 |
| - if (path) { |
113 |
| - req.url = path; |
114 |
| - } else { |
115 |
| - logger.info('[HPM] pathRewrite: No rewritten path found. (%s)', req.url); |
116 |
| - } |
117 |
| - } |
118 |
| - } |
119 |
| - |
120 |
| - function logError(err, req, res) { |
121 |
| - var hostname = (req.hostname || req.host) || (req.headers && req.headers.host); // (node0.10 || node 4/5) || (websocket) |
122 |
| - var targetUri = (proxyOptions.target.host || proxyOptions.target) + req.url; |
123 |
| - |
124 |
| - logger.error('[HPM] PROXY ERROR: %s. %s -> %s', err.code, hostname, targetUri); |
125 |
| - } |
| 1 | +var HPM = require('./lib'); |
126 | 2 |
|
| 3 | +module.exports = function(context, opts) { |
| 4 | + return new HPM(context, opts); |
127 | 5 | };
|
128 |
| - |
129 |
| -module.exports = httpProxyMiddleware; |
0 commit comments