Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"compression": "^1.8.1",
"connect-history-api-fallback": "^2.0.0",
"express": "^4.22.1",
"http-proxy-middleware": "^2.0.9",
"http-proxy-middleware": "^3.0.5",
"ipaddr.js": "^2.3.0",
"launch-editor": "^2.13.1",
"open": "^10.2.0",
Expand Down
48 changes: 22 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 19 additions & 80 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import * as ipaddr from 'ipaddr.js';
import type {
AddressInfo,
BasicApplication,
ByPass,
ClientConfiguration,
ClientConnection,
Compiler,
Expand Down Expand Up @@ -1171,43 +1170,8 @@ class Server<
return item;
}

const getLogLevelForProxy = (
level:
| 'info'
| 'warn'
| 'error'
| 'debug'
| 'silent'
| undefined
| 'none'
| 'log'
| 'verbose',
): 'info' | 'warn' | 'error' | 'debug' | 'silent' | undefined => {
if (level === 'none') {
return 'silent';
}

if (level === 'log') {
return 'info';
}

if (level === 'verbose') {
return 'debug';
}

return level;
};

if (typeof item.logLevel === 'undefined') {
item.logLevel = getLogLevelForProxy(
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: 'info',
);
}

if (typeof item.logProvider === 'undefined') {
item.logProvider = () => this.logger;
if (typeof item.logger === 'undefined') {
item.logger = this.logger as EXPECTED_ANY;
}
Comment on lines +1173 to 1175

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic to set a default logger is redundant. A similar check exists in getProxyMiddleware (lines 1900-1902), which handles all proxy configurations (both objects and functions). To centralize the logic and avoid duplication, this block can be removed. In fact, the entire if (typeof options.proxy !== 'undefined') block from line 1167 to 1179 can be removed as this is its only effective operation.


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

return createProxyMiddleware(context as string, proxyConfig);
if (typeof pathFilter !== 'undefined') {
proxyOptions.pathFilter = pathFilter;
}

if (proxyConfig.router) {
return createProxyMiddleware(proxyConfig);
if (typeof proxyOptions.logger === 'undefined') {
proxyOptions.logger = this.logger as EXPECTED_ANY;
}

// TODO improve me after drop `bypass` to always generate error when configuration is bad
if (!proxyConfig.bypass) {
util.deprecate(
() => {},
`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`,
'DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT',
)();
if (proxyOptions.target || proxyOptions.router) {
return createProxyMiddleware(proxyOptions);
}
Comment thread
chenjiahan marked this conversation as resolved.

util.deprecate(
() => {},
`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`,
Comment thread
chenjiahan marked this conversation as resolved.
'DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT',
)();
};

/**
Expand Down Expand Up @@ -2001,36 +1965,11 @@ class Server<
}
}

// - Check if we have a bypass function defined
// - In case the bypass function is defined we'll retrieve the
// bypassUrl from it otherwise bypassUrl would be null
// TODO remove in the next major in favor `context` and `router` options
const isByPassFuncDefined = typeof proxyConfig.bypass === 'function';
if (isByPassFuncDefined) {
util.deprecate(
() => {},
"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",
'DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT',
)();
}
const bypassUrl = isByPassFuncDefined
? await (proxyConfig.bypass as ByPass)(req, res, proxyConfig)
: null;

if (typeof bypassUrl === 'boolean') {
// skip the proxy
res.statusCode = 404;
req.url = '';
next();
} else if (typeof bypassUrl === 'string') {
// byPass to that url
req.url = bypassUrl;
next();
} else if (proxyMiddleware) {
if (proxyMiddleware) {
return proxyMiddleware(req, res, next);
} else {
next();
}

next();
};

middlewares.push({
Expand Down
12 changes: 4 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,12 @@ export interface WebSocketServerImplementation {
clients: ClientConnection[];
}

export type ByPass<
Req = Request,
Res = Response,
ProxyConfig = ProxyConfigArrayItem,
> = (req: Req, res: Res, proxyConfig: ProxyConfig) => void;

export type ProxyConfigArrayItem = {
path?: HttpProxyMiddlewareOptionsFilter;
/**
* Alias for `pathFilter` in `http-proxy-middleware` options.
* When both `context` and `pathFilter` are provided, `pathFilter` takes precedence.
*/
context?: HttpProxyMiddlewareOptionsFilter;
bypass?: ByPass;
} & HttpProxyMiddlewareOptions;

export type ProxyConfigArray = Array<
Expand Down
Loading