http-proxy-middleware 1.0.3 → 1.1.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/CHANGELOG.md +19 -0
- package/README.md +9 -9
- package/dist/config-factory.js +8 -7
- package/dist/context-matcher.js +3 -3
- package/dist/errors.js +1 -0
- package/dist/handlers.js +9 -7
- package/dist/http-proxy-middleware.d.ts +1 -0
- package/dist/http-proxy-middleware.js +22 -4
- package/dist/index.js +1 -0
- package/dist/logger.js +6 -6
- package/dist/path-rewriter.d.ts +1 -1
- package/dist/path-rewriter.js +14 -15
- package/dist/router.js +8 -7
- package/dist/types.js +1 -0
- package/package.json +35 -34
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [v1.1.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.0)
|
|
4
|
+
|
|
5
|
+
- fix(errorHandler): fix confusing error message ([#509](https://github.com/chimurai/http-proxy-middleware/pull/509))
|
|
6
|
+
- fix(proxy): close proxy when server closes ([#508](https://github.com/chimurai/http-proxy-middleware/pull/508))
|
|
7
|
+
- refactor(lodash): remove lodash ([#459](https://github.com/chimurai/http-proxy-middleware/pull/459)) ([#507](https://github.com/chimurai/http-proxy-middleware/pull/507)) ([TrySound](https://github.com/TrySound))
|
|
8
|
+
- fix(ETIMEDOUT): return 504 on ETIMEDOUT ([#480](https://github.com/chimurai/http-proxy-middleware/pull/480)) ([aremishevsky](https://github.com/aremishevsky))
|
|
9
|
+
|
|
10
|
+
## [v1.0.6](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.6)
|
|
11
|
+
|
|
12
|
+
- chore(deps): lodash 4.17.20 ([#475](https://github.com/chimurai/http-proxy-middleware/pull/475))
|
|
13
|
+
|
|
14
|
+
## [v1.0.5](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.6)
|
|
15
|
+
|
|
16
|
+
- chore(deps): lodash 4.17.19 ([#454](https://github.com/chimurai/http-proxy-middleware/pull/454))
|
|
17
|
+
|
|
18
|
+
## [v1.0.4](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.4)
|
|
19
|
+
|
|
20
|
+
- chore(deps): http-proxy 1.18.1 ([#442](https://github.com/chimurai/http-proxy-middleware/pull/442))
|
|
21
|
+
|
|
3
22
|
## [v1.0.3](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.3)
|
|
4
23
|
|
|
5
24
|
- build(package): exclude build artifact tsconfig.tsbuildinfo ([#415](https://github.com/chimurai/http-proxy-middleware/pull/415))
|
package/README.md
CHANGED
|
@@ -132,13 +132,13 @@ const options = {
|
|
|
132
132
|
ws: true, // proxy websockets
|
|
133
133
|
pathRewrite: {
|
|
134
134
|
'^/api/old-path': '/api/new-path', // rewrite path
|
|
135
|
-
'^/api/remove/path': '/path' // remove base path
|
|
135
|
+
'^/api/remove/path': '/path', // remove base path
|
|
136
136
|
},
|
|
137
137
|
router: {
|
|
138
138
|
// when request.headers.host == 'dev.localhost:3000',
|
|
139
139
|
// override target 'http://www.example.org' to 'http://localhost:8000'
|
|
140
|
-
'dev.localhost:3000': 'http://localhost:8000'
|
|
141
|
-
}
|
|
140
|
+
'dev.localhost:3000': 'http://localhost:8000',
|
|
141
|
+
},
|
|
142
142
|
};
|
|
143
143
|
|
|
144
144
|
// create the proxy (without context)
|
|
@@ -194,12 +194,12 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
194
194
|
/**
|
|
195
195
|
* @return {Boolean}
|
|
196
196
|
*/
|
|
197
|
-
const filter = function(pathname, req) {
|
|
197
|
+
const filter = function (pathname, req) {
|
|
198
198
|
return pathname.match('^/api') && req.method === 'GET';
|
|
199
199
|
};
|
|
200
200
|
|
|
201
201
|
const apiProxy = createProxyMiddleware(filter, {
|
|
202
|
-
target: 'http://www.example.org'
|
|
202
|
+
target: 'http://www.example.org',
|
|
203
203
|
});
|
|
204
204
|
```
|
|
205
205
|
|
|
@@ -285,7 +285,7 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
285
285
|
debug: logger.debug,
|
|
286
286
|
info: logger.info,
|
|
287
287
|
warn: logger.warn,
|
|
288
|
-
error: logger.error
|
|
288
|
+
error: logger.error,
|
|
289
289
|
};
|
|
290
290
|
return myCustomProvider;
|
|
291
291
|
}
|
|
@@ -300,7 +300,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
|
|
|
300
300
|
```javascript
|
|
301
301
|
function onError(err, req, res) {
|
|
302
302
|
res.writeHead(500, {
|
|
303
|
-
'Content-Type': 'text/plain'
|
|
303
|
+
'Content-Type': 'text/plain',
|
|
304
304
|
});
|
|
305
305
|
res.end('Something went wrong. And we are reporting a custom error message.');
|
|
306
306
|
}
|
|
@@ -417,7 +417,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
417
417
|
res,
|
|
418
418
|
{
|
|
419
419
|
target: 'http://localhost:4003/',
|
|
420
|
-
buffer: streamify(req.rawBody)
|
|
420
|
+
buffer: streamify(req.rawBody),
|
|
421
421
|
},
|
|
422
422
|
next
|
|
423
423
|
);
|
|
@@ -540,4 +540,4 @@ $ yarn cover
|
|
|
540
540
|
|
|
541
541
|
The MIT License (MIT)
|
|
542
542
|
|
|
543
|
-
Copyright (c) 2015-
|
|
543
|
+
Copyright (c) 2015-2021 Steven Chim
|
package/dist/config-factory.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.createConfig = void 0;
|
|
4
|
+
const isPlainObj = require("is-plain-obj");
|
|
4
5
|
const url = require("url");
|
|
5
6
|
const errors_1 = require("./errors");
|
|
6
7
|
const logger_1 = require("./logger");
|
|
@@ -9,12 +10,12 @@ function createConfig(context, opts) {
|
|
|
9
10
|
// structure of config object to be returned
|
|
10
11
|
const config = {
|
|
11
12
|
context: undefined,
|
|
12
|
-
options: {}
|
|
13
|
+
options: {},
|
|
13
14
|
};
|
|
14
15
|
// app.use('/api', proxy({target:'http://localhost:9000'}));
|
|
15
16
|
if (isContextless(context, opts)) {
|
|
16
17
|
config.context = '/';
|
|
17
|
-
config.options =
|
|
18
|
+
config.options = Object.assign(config.options, context);
|
|
18
19
|
// app.use('/api', proxy('http://localhost:9000'));
|
|
19
20
|
// app.use(proxy('http://localhost:9000/api'));
|
|
20
21
|
}
|
|
@@ -22,7 +23,7 @@ function createConfig(context, opts) {
|
|
|
22
23
|
const oUrl = url.parse(context);
|
|
23
24
|
const target = [oUrl.protocol, '//', oUrl.host].join('');
|
|
24
25
|
config.context = oUrl.pathname || '/';
|
|
25
|
-
config.options =
|
|
26
|
+
config.options = Object.assign(config.options, { target }, opts);
|
|
26
27
|
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
|
|
27
28
|
config.options.ws = true;
|
|
28
29
|
}
|
|
@@ -30,7 +31,7 @@ function createConfig(context, opts) {
|
|
|
30
31
|
}
|
|
31
32
|
else {
|
|
32
33
|
config.context = context;
|
|
33
|
-
config.options =
|
|
34
|
+
config.options = Object.assign(config.options, opts);
|
|
34
35
|
}
|
|
35
36
|
configureLogger(config.options);
|
|
36
37
|
if (!config.options.target) {
|
|
@@ -51,7 +52,7 @@ exports.createConfig = createConfig;
|
|
|
51
52
|
* @return {Boolean} [description]
|
|
52
53
|
*/
|
|
53
54
|
function isStringShortHand(context) {
|
|
54
|
-
if (
|
|
55
|
+
if (typeof context === 'string') {
|
|
55
56
|
return !!url.parse(context).host;
|
|
56
57
|
}
|
|
57
58
|
}
|
|
@@ -67,7 +68,7 @@ function isStringShortHand(context) {
|
|
|
67
68
|
* @return {Boolean} [description]
|
|
68
69
|
*/
|
|
69
70
|
function isContextless(context, opts) {
|
|
70
|
-
return
|
|
71
|
+
return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0);
|
|
71
72
|
}
|
|
72
73
|
function configureLogger(options) {
|
|
73
74
|
if (options.logLevel) {
|
package/dist/context-matcher.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.match = void 0;
|
|
3
4
|
const isGlob = require("is-glob");
|
|
4
|
-
const _ = require("lodash");
|
|
5
5
|
const micromatch = require("micromatch");
|
|
6
6
|
const url = require("url");
|
|
7
7
|
const errors_1 = require("./errors");
|
|
@@ -25,7 +25,7 @@ function match(context, uri, req) {
|
|
|
25
25
|
throw new Error(errors_1.ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY);
|
|
26
26
|
}
|
|
27
27
|
// custom matching
|
|
28
|
-
if (
|
|
28
|
+
if (typeof context === 'function') {
|
|
29
29
|
const pathname = getUrlPathName(uri);
|
|
30
30
|
return context(pathname, req);
|
|
31
31
|
}
|
|
@@ -74,7 +74,7 @@ function getUrlPathName(uri) {
|
|
|
74
74
|
return uri && url.parse(uri).pathname;
|
|
75
75
|
}
|
|
76
76
|
function isStringPath(context) {
|
|
77
|
-
return
|
|
77
|
+
return typeof context === 'string' && !isGlob(context);
|
|
78
78
|
}
|
|
79
79
|
function isGlobPath(context) {
|
|
80
80
|
return isGlob(context);
|
package/dist/errors.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ERRORS = void 0;
|
|
3
4
|
var ERRORS;
|
|
4
5
|
(function (ERRORS) {
|
|
5
6
|
ERRORS["ERR_CONFIG_FACTORY_TARGET_MISSING"] = "[HPM] Missing \"target\" option. Example: {target: \"http://www.example.org\"}";
|
package/dist/handlers.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.getHandlers = exports.init = void 0;
|
|
4
|
+
const camelcase = require("camelcase");
|
|
4
5
|
const logger_1 = require("./logger");
|
|
5
6
|
const logger = logger_1.getInstance();
|
|
6
7
|
function init(proxy, option) {
|
|
@@ -19,18 +20,18 @@ function getHandlers(options) {
|
|
|
19
20
|
// all handlers for the http-proxy events are prefixed with 'on'.
|
|
20
21
|
// loop through options and try to find these handlers
|
|
21
22
|
// and add them to the handlers object for subscription in init().
|
|
22
|
-
const eventName =
|
|
23
|
-
const fnHandler =
|
|
24
|
-
if (
|
|
23
|
+
const eventName = camelcase('on ' + event);
|
|
24
|
+
const fnHandler = options ? options[eventName] : null;
|
|
25
|
+
if (typeof fnHandler === 'function') {
|
|
25
26
|
handlers[event] = fnHandler;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
// add default error handler in absence of error handler
|
|
29
|
-
if (
|
|
30
|
+
if (typeof handlers.error !== 'function') {
|
|
30
31
|
handlers.error = defaultErrorHandler;
|
|
31
32
|
}
|
|
32
33
|
// add default close handler in absence of close handler
|
|
33
|
-
if (
|
|
34
|
+
if (typeof handlers.close !== 'function') {
|
|
34
35
|
handlers.close = logClose;
|
|
35
36
|
}
|
|
36
37
|
return handlers;
|
|
@@ -48,6 +49,7 @@ function defaultErrorHandler(err, req, res) {
|
|
|
48
49
|
case 'ECONNRESET':
|
|
49
50
|
case 'ENOTFOUND':
|
|
50
51
|
case 'ECONNREFUSED':
|
|
52
|
+
case 'ETIMEDOUT':
|
|
51
53
|
res.writeHead(504);
|
|
52
54
|
break;
|
|
53
55
|
default:
|
|
@@ -55,7 +57,7 @@ function defaultErrorHandler(err, req, res) {
|
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
|
-
res.end(
|
|
60
|
+
res.end(`Error occured while trying to proxy: ${host}${req.url}`);
|
|
59
61
|
}
|
|
60
62
|
function logClose(req, socket, head) {
|
|
61
63
|
// view disconnected websocket connections
|
|
@@ -9,8 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HttpProxyMiddleware = void 0;
|
|
12
13
|
const httpProxy = require("http-proxy");
|
|
13
|
-
const _ = require("lodash");
|
|
14
14
|
const config_factory_1 = require("./config-factory");
|
|
15
15
|
const contextMatcher = require("./context-matcher");
|
|
16
16
|
const handlers = require("./handlers");
|
|
@@ -21,8 +21,10 @@ class HttpProxyMiddleware {
|
|
|
21
21
|
constructor(context, opts) {
|
|
22
22
|
this.logger = logger_1.getInstance();
|
|
23
23
|
this.wsInternalSubscribed = false;
|
|
24
|
+
this.serverOnCloseSubscribed = false;
|
|
24
25
|
// https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this
|
|
25
26
|
this.middleware = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
var _a, _b;
|
|
26
28
|
if (this.shouldProxy(this.config.context, req)) {
|
|
27
29
|
try {
|
|
28
30
|
const activeProxyOptions = yield this.prepareProxyRequest(req);
|
|
@@ -35,12 +37,28 @@ class HttpProxyMiddleware {
|
|
|
35
37
|
else {
|
|
36
38
|
next();
|
|
37
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Get the server object to subscribe to server events;
|
|
42
|
+
* 'upgrade' for websocket and 'close' for graceful shutdown
|
|
43
|
+
*
|
|
44
|
+
* NOTE:
|
|
45
|
+
* req.socket: node >= 13
|
|
46
|
+
* req.connection: node < 13 (Remove this when node 12/13 support is dropped)
|
|
47
|
+
*/
|
|
48
|
+
const server = (_b = ((_a = req.socket) !== null && _a !== void 0 ? _a : req.connection)) === null || _b === void 0 ? void 0 : _b.server;
|
|
49
|
+
if (server && !this.serverOnCloseSubscribed) {
|
|
50
|
+
server.on('close', () => {
|
|
51
|
+
this.logger.info('[HPM] server close signal received: closing proxy server');
|
|
52
|
+
this.proxy.close();
|
|
53
|
+
});
|
|
54
|
+
this.serverOnCloseSubscribed = true;
|
|
55
|
+
}
|
|
38
56
|
if (this.proxyOptions.ws === true) {
|
|
39
57
|
// use initial request to access the server object to subscribe to http upgrade event
|
|
40
|
-
this.catchUpgradeRequest(
|
|
58
|
+
this.catchUpgradeRequest(server);
|
|
41
59
|
}
|
|
42
60
|
});
|
|
43
|
-
this.catchUpgradeRequest = server => {
|
|
61
|
+
this.catchUpgradeRequest = (server) => {
|
|
44
62
|
if (!this.wsInternalSubscribed) {
|
|
45
63
|
server.on('upgrade', this.handleUpgrade);
|
|
46
64
|
// prevent duplicate upgrade handling;
|
|
@@ -81,7 +99,7 @@ class HttpProxyMiddleware {
|
|
|
81
99
|
req.url = req.originalUrl || req.url;
|
|
82
100
|
// store uri before it gets rewritten for logging
|
|
83
101
|
const originalPath = req.url;
|
|
84
|
-
const newProxyOptions =
|
|
102
|
+
const newProxyOptions = Object.assign({}, this.proxyOptions);
|
|
85
103
|
// Apply in order:
|
|
86
104
|
// 1. option.router
|
|
87
105
|
// 2. option.pathRewrite
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createProxyMiddleware = void 0;
|
|
3
4
|
const http_proxy_middleware_1 = require("./http-proxy-middleware");
|
|
4
5
|
function createProxyMiddleware(context, options) {
|
|
5
6
|
const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(context, options);
|
package/dist/logger.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable prefer-rest-params */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
+
exports.getArrow = exports.getInstance = void 0;
|
|
4
5
|
const util = require("util");
|
|
5
6
|
let loggerInstance;
|
|
6
7
|
const defaultProvider = {
|
|
@@ -9,7 +10,7 @@ const defaultProvider = {
|
|
|
9
10
|
debug: console.log,
|
|
10
11
|
info: console.info,
|
|
11
12
|
warn: console.warn,
|
|
12
|
-
error: console.error
|
|
13
|
+
error: console.error,
|
|
13
14
|
};
|
|
14
15
|
// log level 'weight'
|
|
15
16
|
var LEVELS;
|
|
@@ -68,7 +69,7 @@ class Logger {
|
|
|
68
69
|
}
|
|
69
70
|
isValidProvider(fnProvider) {
|
|
70
71
|
const result = true;
|
|
71
|
-
if (fnProvider &&
|
|
72
|
+
if (fnProvider && typeof fnProvider !== 'function') {
|
|
72
73
|
throw new Error('[HPM] Log provider config error. Expecting a function.');
|
|
73
74
|
}
|
|
74
75
|
return result;
|
|
@@ -96,9 +97,8 @@ class Logger {
|
|
|
96
97
|
}
|
|
97
98
|
// make sure logged messages and its data are return interpolated
|
|
98
99
|
// make it possible for additional log data, such date/time or custom prefix.
|
|
99
|
-
_interpolate() {
|
|
100
|
-
const
|
|
101
|
-
const result = fn(_.slice(arguments));
|
|
100
|
+
_interpolate(format, ...args) {
|
|
101
|
+
const result = util.format(format, ...args);
|
|
102
102
|
return result;
|
|
103
103
|
}
|
|
104
104
|
}
|
package/dist/path-rewriter.d.ts
CHANGED
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
* @param {Object} rewriteConfig
|
|
5
5
|
* @return {Function} Function to rewrite paths; This function should accept `path` (request.url) as parameter
|
|
6
6
|
*/
|
|
7
|
-
export declare function createPathRewriter(rewriteConfig: any):
|
|
7
|
+
export declare function createPathRewriter(rewriteConfig: any): any;
|
package/dist/path-rewriter.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.createPathRewriter = void 0;
|
|
4
|
+
const isPlainObj = require("is-plain-obj");
|
|
4
5
|
const errors_1 = require("./errors");
|
|
5
6
|
const logger_1 = require("./logger");
|
|
6
7
|
const logger = logger_1.getInstance();
|
|
@@ -15,7 +16,7 @@ function createPathRewriter(rewriteConfig) {
|
|
|
15
16
|
if (!isValidRewriteConfig(rewriteConfig)) {
|
|
16
17
|
return;
|
|
17
18
|
}
|
|
18
|
-
if (
|
|
19
|
+
if (typeof rewriteConfig === 'function') {
|
|
19
20
|
const customRewriteFn = rewriteConfig;
|
|
20
21
|
return customRewriteFn;
|
|
21
22
|
}
|
|
@@ -25,27 +26,25 @@ function createPathRewriter(rewriteConfig) {
|
|
|
25
26
|
}
|
|
26
27
|
function rewritePath(path) {
|
|
27
28
|
let result = path;
|
|
28
|
-
|
|
29
|
+
for (const rule of rulesCache) {
|
|
29
30
|
if (rule.regex.test(path)) {
|
|
30
31
|
result = result.replace(rule.regex, rule.value);
|
|
31
32
|
logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result);
|
|
32
|
-
|
|
33
|
+
break;
|
|
33
34
|
}
|
|
34
|
-
}
|
|
35
|
+
}
|
|
35
36
|
return result;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
exports.createPathRewriter = createPathRewriter;
|
|
39
40
|
function isValidRewriteConfig(rewriteConfig) {
|
|
40
|
-
if (
|
|
41
|
+
if (typeof rewriteConfig === 'function') {
|
|
41
42
|
return true;
|
|
42
43
|
}
|
|
43
|
-
else if (
|
|
44
|
-
return
|
|
44
|
+
else if (isPlainObj(rewriteConfig)) {
|
|
45
|
+
return Object.keys(rewriteConfig).length !== 0;
|
|
45
46
|
}
|
|
46
|
-
else if (
|
|
47
|
-
_.isNull(rewriteConfig) ||
|
|
48
|
-
_.isEqual(rewriteConfig, {})) {
|
|
47
|
+
else if (rewriteConfig === undefined || rewriteConfig === null) {
|
|
49
48
|
return false;
|
|
50
49
|
}
|
|
51
50
|
else {
|
|
@@ -54,14 +53,14 @@ function isValidRewriteConfig(rewriteConfig) {
|
|
|
54
53
|
}
|
|
55
54
|
function parsePathRewriteRules(rewriteConfig) {
|
|
56
55
|
const rules = [];
|
|
57
|
-
if (
|
|
58
|
-
|
|
56
|
+
if (isPlainObj(rewriteConfig)) {
|
|
57
|
+
for (const [key] of Object.entries(rewriteConfig)) {
|
|
59
58
|
rules.push({
|
|
60
59
|
regex: new RegExp(key),
|
|
61
|
-
value: rewriteConfig[key]
|
|
60
|
+
value: rewriteConfig[key],
|
|
62
61
|
});
|
|
63
62
|
logger.info('[HPM] Proxy rewrite rule created: "%s" ~> "%s"', key, rewriteConfig[key]);
|
|
64
|
-
}
|
|
63
|
+
}
|
|
65
64
|
}
|
|
66
65
|
return rules;
|
|
67
66
|
}
|
package/dist/router.js
CHANGED
|
@@ -9,17 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
|
|
12
|
+
exports.getTarget = void 0;
|
|
13
|
+
const isPlainObj = require("is-plain-obj");
|
|
13
14
|
const logger_1 = require("./logger");
|
|
14
15
|
const logger = logger_1.getInstance();
|
|
15
16
|
function getTarget(req, config) {
|
|
16
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
17
18
|
let newTarget;
|
|
18
19
|
const router = config.router;
|
|
19
|
-
if (
|
|
20
|
+
if (isPlainObj(router)) {
|
|
20
21
|
newTarget = getTargetFromProxyTable(req, router);
|
|
21
22
|
}
|
|
22
|
-
else if (
|
|
23
|
+
else if (typeof router === 'function') {
|
|
23
24
|
newTarget = yield router(req);
|
|
24
25
|
}
|
|
25
26
|
return newTarget;
|
|
@@ -31,13 +32,13 @@ function getTargetFromProxyTable(req, table) {
|
|
|
31
32
|
const host = req.headers.host;
|
|
32
33
|
const path = req.url;
|
|
33
34
|
const hostAndPath = host + path;
|
|
34
|
-
|
|
35
|
+
for (const [key] of Object.entries(table)) {
|
|
35
36
|
if (containsPath(key)) {
|
|
36
37
|
if (hostAndPath.indexOf(key) > -1) {
|
|
37
38
|
// match 'localhost:3000/api'
|
|
38
39
|
result = table[key];
|
|
39
40
|
logger.debug('[HPM] Router table match: "%s"', key);
|
|
40
|
-
|
|
41
|
+
break;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
else {
|
|
@@ -45,10 +46,10 @@ function getTargetFromProxyTable(req, table) {
|
|
|
45
46
|
// match 'localhost:3000'
|
|
46
47
|
result = table[key];
|
|
47
48
|
logger.debug('[HPM] Router table match: "%s"', host);
|
|
48
|
-
|
|
49
|
+
break;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
}
|
|
52
|
+
}
|
|
52
53
|
return result;
|
|
53
54
|
}
|
|
54
55
|
function containsPath(v) {
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "http-proxy-middleware",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,18 +9,16 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean": "rm -rf dist && rm -rf coverage",
|
|
12
|
-
"lint": "yarn
|
|
13
|
-
"lint:
|
|
14
|
-
"
|
|
15
|
-
"
|
|
12
|
+
"lint": "yarn prettier && yarn eslint",
|
|
13
|
+
"lint:fix": "yarn prettier:fix && yarn eslint:fix",
|
|
14
|
+
"eslint": "eslint '{src,test}/**/*.ts'",
|
|
15
|
+
"eslint:fix": "yarn eslint --fix",
|
|
16
|
+
"prettier": "prettier --list-different \"**/*.{js,ts,md,yml,json,html}\"",
|
|
17
|
+
"prettier:fix": "prettier --write \"**/*.{js,ts,md,yml,json,html}\"",
|
|
16
18
|
"build": "tsc",
|
|
17
19
|
"pretest": "yarn build",
|
|
18
20
|
"test": "jest",
|
|
19
|
-
"
|
|
20
|
-
"cover": "jest --coverage",
|
|
21
|
-
"precoveralls": "yarn clean && yarn build",
|
|
22
|
-
"coveralls": "jest --coverage --coverageReporters=text-lcov | coveralls",
|
|
23
|
-
"postcoveralls": "yarn clean",
|
|
21
|
+
"coverage": "jest --coverage --coverageReporters=lcov",
|
|
24
22
|
"prepare": "yarn clean && yarn build && rm dist/tsconfig.tsbuildinfo"
|
|
25
23
|
},
|
|
26
24
|
"repository": {
|
|
@@ -50,37 +48,40 @@
|
|
|
50
48
|
},
|
|
51
49
|
"homepage": "https://github.com/chimurai/http-proxy-middleware#readme",
|
|
52
50
|
"devDependencies": {
|
|
53
|
-
"@commitlint/cli": "^
|
|
54
|
-
"@commitlint/config-conventional": "^
|
|
55
|
-
"@types/express": "^4.17.
|
|
51
|
+
"@commitlint/cli": "^12.0.1",
|
|
52
|
+
"@commitlint/config-conventional": "^12.0.1",
|
|
53
|
+
"@types/express": "^4.17.3",
|
|
56
54
|
"@types/is-glob": "^4.0.1",
|
|
57
|
-
"@types/jest": "^
|
|
58
|
-
"@types/lodash": "^4.14.149",
|
|
55
|
+
"@types/jest": "^26.0.22",
|
|
59
56
|
"@types/micromatch": "^4.0.1",
|
|
60
|
-
"@types/node": "^
|
|
61
|
-
"@types/supertest": "^2.0.
|
|
62
|
-
"
|
|
57
|
+
"@types/node": "^14.14.37",
|
|
58
|
+
"@types/supertest": "^2.0.10",
|
|
59
|
+
"@types/ws": "^7.4.0",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^4.19.0",
|
|
61
|
+
"@typescript-eslint/parser": "^4.19.0",
|
|
62
|
+
"browser-sync": "^2.26.14",
|
|
63
63
|
"connect": "^3.7.0",
|
|
64
|
-
"
|
|
64
|
+
"eslint": "^7.23.0",
|
|
65
|
+
"eslint-config-prettier": "^8.1.0",
|
|
66
|
+
"eslint-plugin-prettier": "^3.3.1",
|
|
65
67
|
"express": "^4.17.1",
|
|
66
|
-
"husky": "^4.
|
|
67
|
-
"jest": "^
|
|
68
|
-
"lint-staged": "^10.
|
|
69
|
-
"mockttp": "^
|
|
70
|
-
"open": "^7.
|
|
71
|
-
"prettier": "^
|
|
72
|
-
"supertest": "^
|
|
73
|
-
"ts-jest": "^
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"typescript": "^3.8.2",
|
|
77
|
-
"ws": "^7.2.1"
|
|
68
|
+
"husky": "^4.3.0",
|
|
69
|
+
"jest": "^26.6.3",
|
|
70
|
+
"lint-staged": "^10.5.4",
|
|
71
|
+
"mockttp": "^1.2.0",
|
|
72
|
+
"open": "^7.4.2",
|
|
73
|
+
"prettier": "^2.2.1",
|
|
74
|
+
"supertest": "^6.1.3",
|
|
75
|
+
"ts-jest": "^26.5.4",
|
|
76
|
+
"typescript": "^4.2.3",
|
|
77
|
+
"ws": "^7.4.4"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@types/http-proxy": "^1.17.
|
|
81
|
-
"
|
|
80
|
+
"@types/http-proxy": "^1.17.5",
|
|
81
|
+
"camelcase": "^6.2.0",
|
|
82
|
+
"http-proxy": "^1.18.1",
|
|
82
83
|
"is-glob": "^4.0.1",
|
|
83
|
-
"
|
|
84
|
+
"is-plain-obj": "^3.0.0",
|
|
84
85
|
"micromatch": "^4.0.2"
|
|
85
86
|
},
|
|
86
87
|
"engines": {
|