http-proxy-middleware 2.0.4 → 3.0.0-beta.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 +254 -198
- package/dist/configuration.d.ts +2 -0
- package/dist/configuration.js +10 -0
- package/dist/debug.d.ts +5 -0
- package/dist/debug.js +8 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/get-plugins.d.ts +2 -0
- package/dist/get-plugins.js +15 -0
- package/dist/handlers/fix-request-body.d.ts +2 -1
- package/dist/handlers/index.js +5 -1
- package/dist/handlers/response-interceptor.js +10 -1
- package/dist/http-proxy-middleware.d.ts +3 -10
- package/dist/http-proxy-middleware.js +39 -50
- package/dist/index.d.ts +11 -3
- package/dist/index.js +15 -3
- package/dist/legacy/create-proxy-middleware.d.ts +11 -0
- package/dist/legacy/create-proxy-middleware.js +17 -0
- package/dist/legacy/index.d.ts +1 -0
- package/dist/legacy/index.js +17 -0
- package/dist/legacy/options-adapter.d.ts +6 -0
- package/dist/legacy/options-adapter.js +92 -0
- package/dist/legacy/public.d.ts +2 -0
- package/dist/legacy/public.js +5 -0
- package/dist/legacy/types.d.ts +110 -0
- package/dist/legacy/types.js +2 -0
- package/dist/logger.d.ts +2 -14
- package/dist/logger.js +20 -129
- package/dist/path-filter.d.ts +2 -0
- package/dist/{context-matcher.js → path-filter.js} +24 -24
- package/dist/path-rewriter.js +6 -6
- package/dist/plugins/default/debug-proxy-errors-plugin.d.ts +6 -0
- package/dist/plugins/default/debug-proxy-errors-plugin.js +61 -0
- package/dist/plugins/default/error-response-plugin.d.ts +2 -0
- package/dist/plugins/default/error-response-plugin.js +19 -0
- package/dist/plugins/default/index.d.ts +4 -0
- package/dist/plugins/default/index.js +20 -0
- package/dist/plugins/default/logger-plugin.d.ts +2 -0
- package/dist/plugins/default/logger-plugin.js +43 -0
- package/dist/plugins/default/proxy-events.d.ts +22 -0
- package/dist/plugins/default/proxy-events.js +33 -0
- package/dist/router.js +7 -7
- package/dist/status-code.d.ts +1 -0
- package/dist/status-code.js +24 -0
- package/dist/types.d.ts +71 -35
- package/dist/utils/function.d.ts +1 -0
- package/dist/utils/function.js +8 -0
- package/package.json +25 -19
- package/dist/_handlers.d.ts +0 -4
- package/dist/_handlers.js +0 -74
- package/dist/config-factory.d.ts +0 -6
- package/dist/config-factory.js +0 -80
- package/dist/context-matcher.d.ts +0 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifyConfig = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
function verifyConfig(options) {
|
|
6
|
+
if (!options.target && !options.router) {
|
|
7
|
+
throw new Error(errors_1.ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.verifyConfig = verifyConfig;
|
package/dist/debug.d.ts
ADDED
package/dist/debug.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Debug = void 0;
|
|
4
|
+
const createDebug = require("debug");
|
|
5
|
+
/**
|
|
6
|
+
* Debug instance with the given namespace: http-proxy-middleware
|
|
7
|
+
*/
|
|
8
|
+
exports.Debug = createDebug('http-proxy-middleware');
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare enum ERRORS {
|
|
2
2
|
ERR_CONFIG_FACTORY_TARGET_MISSING = "[HPM] Missing \"target\" option. Example: {target: \"http://www.example.org\"}",
|
|
3
3
|
ERR_CONTEXT_MATCHER_GENERIC = "[HPM] Invalid context. Expecting something like: \"/api\" or [\"/api\", \"/ajax\"]",
|
|
4
|
-
ERR_CONTEXT_MATCHER_INVALID_ARRAY = "[HPM] Invalid
|
|
4
|
+
ERR_CONTEXT_MATCHER_INVALID_ARRAY = "[HPM] Invalid pathFilter. Expecting something like: [\"/api\", \"/ajax\"] or [\"/api/**\", \"!**.html\"]",
|
|
5
5
|
ERR_PATH_REWRITER_CONFIG = "[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function"
|
|
6
6
|
}
|
package/dist/errors.js
CHANGED
|
@@ -5,6 +5,6 @@ var ERRORS;
|
|
|
5
5
|
(function (ERRORS) {
|
|
6
6
|
ERRORS["ERR_CONFIG_FACTORY_TARGET_MISSING"] = "[HPM] Missing \"target\" option. Example: {target: \"http://www.example.org\"}";
|
|
7
7
|
ERRORS["ERR_CONTEXT_MATCHER_GENERIC"] = "[HPM] Invalid context. Expecting something like: \"/api\" or [\"/api\", \"/ajax\"]";
|
|
8
|
-
ERRORS["ERR_CONTEXT_MATCHER_INVALID_ARRAY"] = "[HPM] Invalid
|
|
8
|
+
ERRORS["ERR_CONTEXT_MATCHER_INVALID_ARRAY"] = "[HPM] Invalid pathFilter. Expecting something like: [\"/api\", \"/ajax\"] or [\"/api/**\", \"!**.html\"]";
|
|
9
9
|
ERRORS["ERR_PATH_REWRITER_CONFIG"] = "[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function";
|
|
10
10
|
})(ERRORS = exports.ERRORS || (exports.ERRORS = {}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPlugins = void 0;
|
|
4
|
+
const default_1 = require("./plugins/default");
|
|
5
|
+
function getPlugins(options) {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
// don't load default errorResponsePlugin if user has specified their own
|
|
8
|
+
const maybeErrorResponsePlugin = !!((_a = options.on) === null || _a === void 0 ? void 0 : _a.error) ? [] : [default_1.errorResponsePlugin];
|
|
9
|
+
const defaultPlugins = !!options.ejectPlugins
|
|
10
|
+
? [] // no default plugins when ejecting
|
|
11
|
+
: [default_1.debugProxyErrorsPlugin, default_1.proxyEventsPlugin, default_1.loggerPlugin, ...maybeErrorResponsePlugin];
|
|
12
|
+
const userPlugins = (_b = options.plugins) !== null && _b !== void 0 ? _b : [];
|
|
13
|
+
return [...defaultPlugins, ...userPlugins];
|
|
14
|
+
}
|
|
15
|
+
exports.getPlugins = getPlugins;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type * as http from 'http';
|
|
3
|
+
import type { Request } from '../types';
|
|
3
4
|
/**
|
|
4
5
|
* Fix proxied body if bodyParser is involved.
|
|
5
6
|
*/
|
|
6
|
-
export declare function fixRequestBody(proxyReq: http.ClientRequest, req:
|
|
7
|
+
export declare function fixRequestBody(proxyReq: http.ClientRequest, req: Request): void;
|
package/dist/handlers/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
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);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.responseInterceptor = void 0;
|
|
4
4
|
const zlib = require("zlib");
|
|
5
|
+
const debug_1 = require("../debug");
|
|
6
|
+
const function_1 = require("../utils/function");
|
|
7
|
+
const debug = debug_1.Debug.extend('response-interceptor');
|
|
5
8
|
/**
|
|
6
9
|
* Intercept responses from upstream.
|
|
7
10
|
* Automatically decompress (deflate, gzip, brotli).
|
|
@@ -10,7 +13,8 @@ const zlib = require("zlib");
|
|
|
10
13
|
* NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
|
|
11
14
|
*/
|
|
12
15
|
function responseInterceptor(interceptor) {
|
|
13
|
-
return async function
|
|
16
|
+
return async function proxyResResponseInterceptor(proxyRes, req, res) {
|
|
17
|
+
debug('intercept proxy response');
|
|
14
18
|
const originalProxyRes = proxyRes;
|
|
15
19
|
let buffer = Buffer.from('', 'utf8');
|
|
16
20
|
// decompress proxy response
|
|
@@ -21,9 +25,12 @@ function responseInterceptor(interceptor) {
|
|
|
21
25
|
// copy original headers
|
|
22
26
|
copyHeaders(proxyRes, res);
|
|
23
27
|
// call interceptor with intercepted response (buffer)
|
|
28
|
+
debug('call interceptor function: %s', (0, function_1.getFunctionName)(interceptor));
|
|
24
29
|
const interceptedBuffer = Buffer.from(await interceptor(buffer, originalProxyRes, req, res));
|
|
25
30
|
// set correct content-length (with double byte character support)
|
|
31
|
+
debug('set content-length: %s', Buffer.byteLength(interceptedBuffer, 'utf8'));
|
|
26
32
|
res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
|
|
33
|
+
debug('write intercepted response');
|
|
27
34
|
res.write(interceptedBuffer);
|
|
28
35
|
res.end();
|
|
29
36
|
});
|
|
@@ -54,6 +61,7 @@ function decompress(proxyRes, contentEncoding) {
|
|
|
54
61
|
break;
|
|
55
62
|
}
|
|
56
63
|
if (decompress) {
|
|
64
|
+
debug(`decompress proxy response with 'content-encoding': %s`, contentEncoding);
|
|
57
65
|
_proxyRes.pipe(decompress);
|
|
58
66
|
_proxyRes = decompress;
|
|
59
67
|
}
|
|
@@ -64,6 +72,7 @@ function decompress(proxyRes, contentEncoding) {
|
|
|
64
72
|
* https://github.com/apache/superset/blob/9773aba522e957ed9423045ca153219638a85d2f/superset-frontend/webpack.proxy-config.js#L78
|
|
65
73
|
*/
|
|
66
74
|
function copyHeaders(originalResponse, response) {
|
|
75
|
+
debug('copy original response headers');
|
|
67
76
|
response.statusCode = originalResponse.statusCode;
|
|
68
77
|
response.statusMessage = originalResponse.statusMessage;
|
|
69
78
|
if (response.setHeader) {
|
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RequestHandler, Options } from './types';
|
|
2
2
|
export declare class HttpProxyMiddleware {
|
|
3
|
-
private logger;
|
|
4
|
-
private config;
|
|
5
3
|
private wsInternalSubscribed;
|
|
6
4
|
private serverOnCloseSubscribed;
|
|
7
5
|
private proxyOptions;
|
|
8
6
|
private proxy;
|
|
9
7
|
private pathRewriter;
|
|
10
|
-
constructor(
|
|
8
|
+
constructor(options: Options);
|
|
11
9
|
middleware: RequestHandler;
|
|
10
|
+
private registerPlugins;
|
|
12
11
|
private catchUpgradeRequest;
|
|
13
12
|
private handleUpgrade;
|
|
14
13
|
/**
|
|
15
14
|
* Determine whether request should be proxied.
|
|
16
|
-
*
|
|
17
|
-
* @private
|
|
18
|
-
* @param {String} context [description]
|
|
19
|
-
* @param {Object} req [description]
|
|
20
|
-
* @return {Boolean}
|
|
21
15
|
*/
|
|
22
16
|
private shouldProxy;
|
|
23
17
|
/**
|
|
@@ -31,5 +25,4 @@ export declare class HttpProxyMiddleware {
|
|
|
31
25
|
private prepareProxyRequest;
|
|
32
26
|
private applyRouter;
|
|
33
27
|
private applyPathRewrite;
|
|
34
|
-
private logError;
|
|
35
28
|
}
|
|
@@ -2,31 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HttpProxyMiddleware = void 0;
|
|
4
4
|
const httpProxy = require("http-proxy");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const logger_1 = require("./logger");
|
|
5
|
+
const configuration_1 = require("./configuration");
|
|
6
|
+
const get_plugins_1 = require("./get-plugins");
|
|
7
|
+
const path_filter_1 = require("./path-filter");
|
|
9
8
|
const PathRewriter = require("./path-rewriter");
|
|
10
9
|
const Router = require("./router");
|
|
10
|
+
const debug_1 = require("./debug");
|
|
11
|
+
const function_1 = require("./utils/function");
|
|
11
12
|
class HttpProxyMiddleware {
|
|
12
|
-
constructor(
|
|
13
|
-
this.logger = (0, logger_1.getInstance)();
|
|
13
|
+
constructor(options) {
|
|
14
14
|
this.wsInternalSubscribed = false;
|
|
15
15
|
this.serverOnCloseSubscribed = false;
|
|
16
16
|
// https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this
|
|
17
17
|
this.middleware = async (req, res, next) => {
|
|
18
18
|
var _a, _b;
|
|
19
|
-
if (this.shouldProxy(this.
|
|
19
|
+
if (this.shouldProxy(this.proxyOptions.pathFilter, req)) {
|
|
20
20
|
try {
|
|
21
21
|
const activeProxyOptions = await this.prepareProxyRequest(req);
|
|
22
|
+
(0, debug_1.Debug)(`proxy request to target: %O`, activeProxyOptions.target);
|
|
22
23
|
this.proxy.web(req, res, activeProxyOptions);
|
|
23
24
|
}
|
|
24
25
|
catch (err) {
|
|
25
|
-
next(err);
|
|
26
|
+
next && next(err);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
else {
|
|
29
|
-
next();
|
|
30
|
+
next && next();
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Get the server object to subscribe to server events;
|
|
@@ -36,10 +37,10 @@ class HttpProxyMiddleware {
|
|
|
36
37
|
* req.socket: node >= 13
|
|
37
38
|
* req.connection: node < 13 (Remove this when node 12/13 support is dropped)
|
|
38
39
|
*/
|
|
39
|
-
const server = (_b = ((_a = req.socket) !== null && _a !== void 0 ? _a : req.connection)) === null || _b === void 0 ? void 0 : _b.server;
|
|
40
|
+
const server = (_b = (((_a = req.socket) !== null && _a !== void 0 ? _a : req.connection))) === null || _b === void 0 ? void 0 : _b.server;
|
|
40
41
|
if (server && !this.serverOnCloseSubscribed) {
|
|
41
42
|
server.on('close', () => {
|
|
42
|
-
|
|
43
|
+
(0, debug_1.Debug)('server close signal received: closing proxy server');
|
|
43
44
|
this.proxy.close();
|
|
44
45
|
});
|
|
45
46
|
this.serverOnCloseSubscribed = true;
|
|
@@ -51,6 +52,7 @@ class HttpProxyMiddleware {
|
|
|
51
52
|
};
|
|
52
53
|
this.catchUpgradeRequest = (server) => {
|
|
53
54
|
if (!this.wsInternalSubscribed) {
|
|
55
|
+
(0, debug_1.Debug)('subscribing to server upgrade event');
|
|
54
56
|
server.on('upgrade', this.handleUpgrade);
|
|
55
57
|
// prevent duplicate upgrade handling;
|
|
56
58
|
// in case external upgrade is also configured
|
|
@@ -58,23 +60,17 @@ class HttpProxyMiddleware {
|
|
|
58
60
|
}
|
|
59
61
|
};
|
|
60
62
|
this.handleUpgrade = async (req, socket, head) => {
|
|
61
|
-
if (this.shouldProxy(this.
|
|
63
|
+
if (this.shouldProxy(this.proxyOptions.pathFilter, req)) {
|
|
62
64
|
const activeProxyOptions = await this.prepareProxyRequest(req);
|
|
63
65
|
this.proxy.ws(req, socket, head, activeProxyOptions);
|
|
64
|
-
|
|
66
|
+
(0, debug_1.Debug)('server upgrade event received. Proxying WebSocket');
|
|
65
67
|
}
|
|
66
68
|
};
|
|
67
69
|
/**
|
|
68
70
|
* Determine whether request should be proxied.
|
|
69
|
-
*
|
|
70
|
-
* @private
|
|
71
|
-
* @param {String} context [description]
|
|
72
|
-
* @param {Object} req [description]
|
|
73
|
-
* @return {Boolean}
|
|
74
71
|
*/
|
|
75
|
-
this.shouldProxy = (
|
|
76
|
-
|
|
77
|
-
return contextMatcher.match(context, path, req);
|
|
72
|
+
this.shouldProxy = (pathFilter, req) => {
|
|
73
|
+
return (0, path_filter_1.matchPathFilter)(pathFilter, req.url, req);
|
|
78
74
|
};
|
|
79
75
|
/**
|
|
80
76
|
* Apply option.router and option.pathRewrite
|
|
@@ -85,22 +81,20 @@ class HttpProxyMiddleware {
|
|
|
85
81
|
* @return {Object} proxy options
|
|
86
82
|
*/
|
|
87
83
|
this.prepareProxyRequest = async (req) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Incorrect usage confirmed: https://github.com/expressjs/express/issues/4854#issuecomment-1066171160
|
|
86
|
+
* Temporary restore req.url patch for {@link src/legacy/create-proxy-middleware.ts legacyCreateProxyMiddleware()}
|
|
87
|
+
* FIXME: remove this patch in future release
|
|
88
|
+
*/
|
|
89
|
+
if (this.middleware.__LEGACY_HTTP_PROXY_MIDDLEWARE__) {
|
|
90
|
+
req.url = req.originalUrl || req.url;
|
|
91
|
+
}
|
|
93
92
|
const newProxyOptions = Object.assign({}, this.proxyOptions);
|
|
94
93
|
// Apply in order:
|
|
95
94
|
// 1. option.router
|
|
96
95
|
// 2. option.pathRewrite
|
|
97
96
|
await this.applyRouter(req, newProxyOptions);
|
|
98
97
|
await this.applyPathRewrite(req, this.pathRewriter);
|
|
99
|
-
// debug logging for both http(s) and websockets
|
|
100
|
-
if (this.proxyOptions.logLevel === 'debug') {
|
|
101
|
-
const arrow = (0, logger_1.getArrow)(originalPath, req.url, this.proxyOptions.target, newProxyOptions.target);
|
|
102
|
-
this.logger.debug('[HPM] %s %s %s %s', req.method, originalPath, arrow, newProxyOptions.target);
|
|
103
|
-
}
|
|
104
98
|
return newProxyOptions;
|
|
105
99
|
};
|
|
106
100
|
// Modify option.target when router present.
|
|
@@ -109,7 +103,7 @@ class HttpProxyMiddleware {
|
|
|
109
103
|
if (options.router) {
|
|
110
104
|
newTarget = await Router.getTarget(req, options);
|
|
111
105
|
if (newTarget) {
|
|
112
|
-
|
|
106
|
+
(0, debug_1.Debug)('router new target: "%s"', newTarget);
|
|
113
107
|
options.target = newTarget;
|
|
114
108
|
}
|
|
115
109
|
}
|
|
@@ -119,32 +113,20 @@ class HttpProxyMiddleware {
|
|
|
119
113
|
if (pathRewriter) {
|
|
120
114
|
const path = await pathRewriter(req.url, req);
|
|
121
115
|
if (typeof path === 'string') {
|
|
116
|
+
(0, debug_1.Debug)('pathRewrite new path: %s', req.url);
|
|
122
117
|
req.url = path;
|
|
123
118
|
}
|
|
124
119
|
else {
|
|
125
|
-
|
|
120
|
+
(0, debug_1.Debug)('pathRewrite: no rewritten path found: %s', req.url);
|
|
126
121
|
}
|
|
127
122
|
}
|
|
128
123
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const requestHref = `${hostname}${req.url}`;
|
|
133
|
-
const targetHref = `${target === null || target === void 0 ? void 0 : target.href}`; // target is undefined when websocket errors
|
|
134
|
-
const errorMessage = '[HPM] Error occurred while proxying request %s to %s [%s] (%s)';
|
|
135
|
-
const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
|
|
136
|
-
this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference);
|
|
137
|
-
};
|
|
138
|
-
this.config = (0, config_factory_1.createConfig)(context, opts);
|
|
139
|
-
this.proxyOptions = this.config.options;
|
|
140
|
-
// create proxy
|
|
124
|
+
(0, configuration_1.verifyConfig)(options);
|
|
125
|
+
this.proxyOptions = options;
|
|
126
|
+
(0, debug_1.Debug)(`create proxy server`);
|
|
141
127
|
this.proxy = httpProxy.createProxyServer({});
|
|
142
|
-
this.
|
|
128
|
+
this.registerPlugins(this.proxy, this.proxyOptions);
|
|
143
129
|
this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
|
|
144
|
-
// attach handler to http-proxy events
|
|
145
|
-
handlers.init(this.proxy, this.proxyOptions);
|
|
146
|
-
// log errors for debug purpose
|
|
147
|
-
this.proxy.on('error', this.logError);
|
|
148
130
|
// https://github.com/chimurai/http-proxy-middleware/issues/19
|
|
149
131
|
// expose function to upgrade externally
|
|
150
132
|
this.middleware.upgrade = (req, socket, head) => {
|
|
@@ -153,5 +135,12 @@ class HttpProxyMiddleware {
|
|
|
153
135
|
}
|
|
154
136
|
};
|
|
155
137
|
}
|
|
138
|
+
registerPlugins(proxy, options) {
|
|
139
|
+
const plugins = (0, get_plugins_1.getPlugins)(options);
|
|
140
|
+
plugins.forEach((plugin) => {
|
|
141
|
+
(0, debug_1.Debug)(`register plugin: "${(0, function_1.getFunctionName)(plugin)}"`);
|
|
142
|
+
plugin(proxy, options);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
156
145
|
}
|
|
157
146
|
exports.HttpProxyMiddleware = HttpProxyMiddleware;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function createProxyMiddleware(
|
|
1
|
+
import type { Options, RequestHandler } from './types';
|
|
2
|
+
export declare function createProxyMiddleware(options: Options): RequestHandler;
|
|
3
3
|
export * from './handlers';
|
|
4
|
-
export { Filter, Options, RequestHandler } from './types';
|
|
4
|
+
export type { Filter, Options, RequestHandler } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Default plugins
|
|
7
|
+
*/
|
|
8
|
+
export * from './plugins/default';
|
|
9
|
+
/**
|
|
10
|
+
* Legacy exports
|
|
11
|
+
*/
|
|
12
|
+
export * from './legacy';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
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);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -12,9 +16,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
exports.createProxyMiddleware = void 0;
|
|
14
18
|
const http_proxy_middleware_1 = require("./http-proxy-middleware");
|
|
15
|
-
function createProxyMiddleware(
|
|
16
|
-
const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(
|
|
19
|
+
function createProxyMiddleware(options) {
|
|
20
|
+
const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(options);
|
|
17
21
|
return middleware;
|
|
18
22
|
}
|
|
19
23
|
exports.createProxyMiddleware = createProxyMiddleware;
|
|
20
24
|
__exportStar(require("./handlers"), exports);
|
|
25
|
+
/**
|
|
26
|
+
* Default plugins
|
|
27
|
+
*/
|
|
28
|
+
__exportStar(require("./plugins/default"), exports);
|
|
29
|
+
/**
|
|
30
|
+
* Legacy exports
|
|
31
|
+
*/
|
|
32
|
+
__exportStar(require("./legacy"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Filter, RequestHandler } from '../types';
|
|
2
|
+
import { LegacyOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated
|
|
5
|
+
* This function is deprecated and will be removed in a future version.
|
|
6
|
+
*
|
|
7
|
+
* Use {@link createProxyMiddleware} instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare function legacyCreateProxyMiddleware(shortHand: string): RequestHandler;
|
|
10
|
+
export declare function legacyCreateProxyMiddleware(legacyOptions: LegacyOptions): RequestHandler;
|
|
11
|
+
export declare function legacyCreateProxyMiddleware(legacyContext: Filter, legacyOptions: LegacyOptions): RequestHandler;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacyCreateProxyMiddleware = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const debug_1 = require("../debug");
|
|
6
|
+
const options_adapter_1 = require("./options-adapter");
|
|
7
|
+
const debug = debug_1.Debug.extend('legacy-create-proxy-middleware');
|
|
8
|
+
function legacyCreateProxyMiddleware(legacyContext, legacyOptions) {
|
|
9
|
+
debug('init');
|
|
10
|
+
const options = (0, options_adapter_1.legacyOptionsAdapter)(legacyContext, legacyOptions);
|
|
11
|
+
const proxyMiddleware = (0, __1.createProxyMiddleware)(options);
|
|
12
|
+
// https://github.com/chimurai/http-proxy-middleware/pull/731/files#diff-07e6ad10bda0df091b737caed42767657cd0bd74a01246a1a0b7ab59c0f6e977L118
|
|
13
|
+
debug('add marker for patching req.url (old behavior)');
|
|
14
|
+
proxyMiddleware.__LEGACY_HTTP_PROXY_MIDDLEWARE__ = true;
|
|
15
|
+
return proxyMiddleware;
|
|
16
|
+
}
|
|
17
|
+
exports.legacyCreateProxyMiddleware = legacyCreateProxyMiddleware;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './public';
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
__exportStar(require("./public"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Filter, Options } from '..';
|
|
2
|
+
import { LegacyOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Convert {@link LegacyOptions legacy Options} to new {@link Options}
|
|
5
|
+
*/
|
|
6
|
+
export declare function legacyOptionsAdapter(legacyContext: Filter | LegacyOptions, legacyOptions: LegacyOptions): Options;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacyOptionsAdapter = void 0;
|
|
4
|
+
const url = require("url");
|
|
5
|
+
const debug_1 = require("../debug");
|
|
6
|
+
const logger_1 = require("../logger");
|
|
7
|
+
const debug = debug_1.Debug.extend('legacy-options-adapter');
|
|
8
|
+
// https://github.com/chimurai/http-proxy-middleware/blob/7341704d0aa9d1606dfd37ebfdffddd34c894784/src/_handlers.ts#L20-L27
|
|
9
|
+
const proxyEventMap = {
|
|
10
|
+
onError: 'error',
|
|
11
|
+
onProxyReq: 'proxyReq',
|
|
12
|
+
onProxyRes: 'proxyRes',
|
|
13
|
+
onProxyReqWs: 'proxyReqWs',
|
|
14
|
+
onOpen: 'open',
|
|
15
|
+
onClose: 'close',
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Convert {@link LegacyOptions legacy Options} to new {@link Options}
|
|
19
|
+
*/
|
|
20
|
+
function legacyOptionsAdapter(legacyContext, legacyOptions) {
|
|
21
|
+
let options;
|
|
22
|
+
let logger;
|
|
23
|
+
// https://github.com/chimurai/http-proxy-middleware/pull/716
|
|
24
|
+
if (typeof legacyContext === 'string' && !!url.parse(legacyContext).host) {
|
|
25
|
+
throw new Error(`Shorthand syntax is removed from legacyCreateProxyMiddleware().
|
|
26
|
+
Please use "legacyCreateProxyMiddleware({ target: 'http://www.example.org' })" instead.`);
|
|
27
|
+
}
|
|
28
|
+
// detect old "context" argument and convert to "options.pathFilter"
|
|
29
|
+
// https://github.com/chimurai/http-proxy-middleware/pull/722/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80L4
|
|
30
|
+
if (legacyContext && legacyOptions) {
|
|
31
|
+
debug('map legacy context/filter to options.pathFilter');
|
|
32
|
+
options = { ...legacyOptions, pathFilter: legacyContext };
|
|
33
|
+
logger = getLegacyLogger(options);
|
|
34
|
+
logger.warn(`[http-proxy-middleware] Legacy "context" argument is deprecated. Migrate your "context" to "options.pathFilter":
|
|
35
|
+
|
|
36
|
+
const options = {
|
|
37
|
+
pathFilter: '${legacyContext}',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
else if (legacyContext && !legacyOptions) {
|
|
44
|
+
options = { ...legacyContext };
|
|
45
|
+
logger = getLegacyLogger(options);
|
|
46
|
+
}
|
|
47
|
+
// map old event names to new event names
|
|
48
|
+
// https://github.com/chimurai/http-proxy-middleware/pull/745/files#diff-c54113cf61ec99691748a3890bfbeb00e10efb3f0a76f03a0fd9ec49072e410aL48-L53
|
|
49
|
+
Object.entries(proxyEventMap).forEach(([legacyEventName, proxyEventName]) => {
|
|
50
|
+
if (options[legacyEventName]) {
|
|
51
|
+
options.on = { ...options.on };
|
|
52
|
+
options.on[proxyEventName] = options[legacyEventName];
|
|
53
|
+
debug('map legacy event "%s" to "on.%s"', legacyEventName, proxyEventName);
|
|
54
|
+
logger.warn(`[http-proxy-middleware] Legacy "${legacyEventName}" is deprecated. Migrate to "options.on.${proxyEventName}":
|
|
55
|
+
|
|
56
|
+
const options = {
|
|
57
|
+
on: {
|
|
58
|
+
${proxyEventName}: () => {},
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
|
|
63
|
+
`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
// map old logProvider to new logger
|
|
67
|
+
// https://github.com/chimurai/http-proxy-middleware/pull/749
|
|
68
|
+
const logProvider = options.logProvider && options.logProvider();
|
|
69
|
+
const logLevel = options.logLevel;
|
|
70
|
+
debug('legacy logLevel', logLevel);
|
|
71
|
+
debug('legacy logProvider: %O', logProvider);
|
|
72
|
+
if (typeof logLevel === 'string' && logLevel !== 'silent') {
|
|
73
|
+
debug('map "logProvider" to "logger"');
|
|
74
|
+
logger.warn(`[http-proxy-middleware] Legacy "logLevel" and "logProvider" are deprecated. Migrate to "options.logger":
|
|
75
|
+
|
|
76
|
+
const options = {
|
|
77
|
+
logger: console,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
More details: https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md
|
|
81
|
+
`);
|
|
82
|
+
}
|
|
83
|
+
return options;
|
|
84
|
+
}
|
|
85
|
+
exports.legacyOptionsAdapter = legacyOptionsAdapter;
|
|
86
|
+
function getLegacyLogger(options) {
|
|
87
|
+
const legacyLogger = options.logProvider && options.logProvider();
|
|
88
|
+
if (legacyLogger) {
|
|
89
|
+
options.logger = legacyLogger;
|
|
90
|
+
}
|
|
91
|
+
return (0, logger_1.getLogger)(options);
|
|
92
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacyCreateProxyMiddleware = void 0;
|
|
4
|
+
var create_proxy_middleware_1 = require("./create-proxy-middleware");
|
|
5
|
+
Object.defineProperty(exports, "legacyCreateProxyMiddleware", { enumerable: true, get: function () { return create_proxy_middleware_1.legacyCreateProxyMiddleware; } });
|