Skip to content

Commit c04cadc

Browse files
authored
feat!: update http-proxy-middleware v3, remove bypass and path options (#101)
1 parent 7aa5eec commit c04cadc

6 files changed

Lines changed: 130 additions & 171 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"compression": "^1.8.1",
107107
"connect-history-api-fallback": "^2.0.0",
108108
"express": "^4.22.1",
109-
"http-proxy-middleware": "^2.0.9",
109+
"http-proxy-middleware": "^3.0.5",
110110
"ipaddr.js": "^2.3.0",
111111
"launch-editor": "^2.13.1",
112112
"open": "^10.2.0",

pnpm-lock.yaml

Lines changed: 22 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/server.ts

Lines changed: 19 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import * as ipaddr from 'ipaddr.js';
1717
import type {
1818
AddressInfo,
1919
BasicApplication,
20-
ByPass,
2120
ClientConfiguration,
2221
ClientConnection,
2322
Compiler,
@@ -1171,43 +1170,8 @@ class Server<
11711170
return item;
11721171
}
11731172

1174-
const getLogLevelForProxy = (
1175-
level:
1176-
| 'info'
1177-
| 'warn'
1178-
| 'error'
1179-
| 'debug'
1180-
| 'silent'
1181-
| undefined
1182-
| 'none'
1183-
| 'log'
1184-
| 'verbose',
1185-
): 'info' | 'warn' | 'error' | 'debug' | 'silent' | undefined => {
1186-
if (level === 'none') {
1187-
return 'silent';
1188-
}
1189-
1190-
if (level === 'log') {
1191-
return 'info';
1192-
}
1193-
1194-
if (level === 'verbose') {
1195-
return 'debug';
1196-
}
1197-
1198-
return level;
1199-
};
1200-
1201-
if (typeof item.logLevel === 'undefined') {
1202-
item.logLevel = getLogLevelForProxy(
1203-
compilerOptions.infrastructureLogging
1204-
? compilerOptions.infrastructureLogging.level
1205-
: 'info',
1206-
);
1207-
}
1208-
1209-
if (typeof item.logProvider === 'undefined') {
1210-
item.logProvider = () => this.logger;
1173+
if (typeof item.logger === 'undefined') {
1174+
item.logger = this.logger as EXPECTED_ANY;
12111175
}
12121176

12131177
return item;
@@ -1926,26 +1890,26 @@ class Server<
19261890
const getProxyMiddleware = (
19271891
proxyConfig: ProxyConfigArrayItem,
19281892
): RequestHandler | undefined => {
1929-
// It is possible to use the `bypass` method without a `target` or `router`.
1930-
// However, the proxy middleware has no use in this case, and will fail to instantiate.
1931-
if (proxyConfig.target) {
1932-
const context = proxyConfig.context || proxyConfig.path;
1893+
const { context, ...proxyOptions } = proxyConfig;
1894+
const pathFilter = proxyOptions.pathFilter ?? context;
19331895

1934-
return createProxyMiddleware(context as string, proxyConfig);
1896+
if (typeof pathFilter !== 'undefined') {
1897+
proxyOptions.pathFilter = pathFilter;
19351898
}
19361899

1937-
if (proxyConfig.router) {
1938-
return createProxyMiddleware(proxyConfig);
1900+
if (typeof proxyOptions.logger === 'undefined') {
1901+
proxyOptions.logger = this.logger as EXPECTED_ANY;
19391902
}
19401903

1941-
// TODO improve me after drop `bypass` to always generate error when configuration is bad
1942-
if (!proxyConfig.bypass) {
1943-
util.deprecate(
1944-
() => {},
1945-
`Invalid proxy configuration:\n\n${JSON.stringify(proxyConfig, null, 2)}\n\nThe use of proxy object notation as proxy routes has been removed.\nPlease use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options`,
1946-
'DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT',
1947-
)();
1904+
if (proxyOptions.target || proxyOptions.router) {
1905+
return createProxyMiddleware(proxyOptions);
19481906
}
1907+
1908+
util.deprecate(
1909+
() => {},
1910+
`Invalid proxy configuration:\n\n${JSON.stringify(proxyConfig, null, 2)}\n\nThe use of proxy object notation as proxy routes has been removed.\nPlease use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware`,
1911+
'DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT',
1912+
)();
19491913
};
19501914

19511915
/**
@@ -2001,36 +1965,11 @@ class Server<
20011965
}
20021966
}
20031967

2004-
// - Check if we have a bypass function defined
2005-
// - In case the bypass function is defined we'll retrieve the
2006-
// bypassUrl from it otherwise bypassUrl would be null
2007-
// TODO remove in the next major in favor `context` and `router` options
2008-
const isByPassFuncDefined = typeof proxyConfig.bypass === 'function';
2009-
if (isByPassFuncDefined) {
2010-
util.deprecate(
2011-
() => {},
2012-
"Using the 'bypass' option is deprecated. Please use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options",
2013-
'DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT',
2014-
)();
2015-
}
2016-
const bypassUrl = isByPassFuncDefined
2017-
? await (proxyConfig.bypass as ByPass)(req, res, proxyConfig)
2018-
: null;
2019-
2020-
if (typeof bypassUrl === 'boolean') {
2021-
// skip the proxy
2022-
res.statusCode = 404;
2023-
req.url = '';
2024-
next();
2025-
} else if (typeof bypassUrl === 'string') {
2026-
// byPass to that url
2027-
req.url = bypassUrl;
2028-
next();
2029-
} else if (proxyMiddleware) {
1968+
if (proxyMiddleware) {
20301969
return proxyMiddleware(req, res, next);
2031-
} else {
2032-
next();
20331970
}
1971+
1972+
next();
20341973
};
20351974

20361975
middlewares.push({

src/types.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,12 @@ export interface WebSocketServerImplementation {
167167
clients: ClientConnection[];
168168
}
169169

170-
export type ByPass<
171-
Req = Request,
172-
Res = Response,
173-
ProxyConfig = ProxyConfigArrayItem,
174-
> = (req: Req, res: Res, proxyConfig: ProxyConfig) => void;
175-
176170
export type ProxyConfigArrayItem = {
177-
path?: HttpProxyMiddlewareOptionsFilter;
171+
/**
172+
* Alias for `pathFilter` in `http-proxy-middleware` options.
173+
* When both `context` and `pathFilter` are provided, `pathFilter` takes precedence.
174+
*/
178175
context?: HttpProxyMiddlewareOptionsFilter;
179-
bypass?: ByPass;
180176
} & HttpProxyMiddlewareOptions;
181177

182178
export type ProxyConfigArray = Array<

0 commit comments

Comments
 (0)