Skip to content

Commit 3cdc13a

Browse files
committed
feat: remove bypass option from proxy configuration
1 parent 18704b6 commit 3cdc13a

6 files changed

Lines changed: 16 additions & 210 deletions

File tree

examples/general/proxy-advanced/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ npx webpack serve --open
1515
2. You should see the text on the page itself change to read `Success!`.
1616
3. Navigate to `http://localhost:8080/api/users`.
1717
4. The page should display several JSON objects.
18-
5. Navigate to `http://localhost:8080/api/nope`.
19-
6. The page should display `Bypassed proxy!``.

examples/general/proxy-advanced/bypass.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/general/proxy-advanced/webpack.config.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@ module.exports = setup({
88
context: __dirname,
99
entry: "./app.js",
1010
devServer: {
11-
proxy: {
12-
"/api": {
13-
target: "http://jsonplaceholder.typicode.com/",
14-
changeOrigin: true,
15-
pathRewrite: {
16-
"^/api": "",
17-
},
18-
bypass(req) {
19-
if (req.url === "/api/nope") {
20-
return "/bypass.html";
21-
}
11+
proxy: [
12+
{
13+
"/api": {
14+
target: "http://jsonplaceholder.typicode.com/",
15+
changeOrigin: true,
16+
pathRewrite: {
17+
"^/api": "",
18+
},
2219
},
2320
},
24-
},
21+
],
2522
},
2623
});

lib/Server.js

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const os = require("node:os");
44
const path = require("node:path");
55
const url = require("node:url");
6-
const util = require("node:util");
76
const fs = require("graceful-fs");
87
const ipaddr = require("ipaddr.js");
98
const { validate } = require("schema-utils");
@@ -138,14 +137,7 @@ const schema = require("./options.json");
138137
*/
139138

140139
/**
141-
* @callback ByPass
142-
* @param {Request} req
143-
* @param {Response} res
144-
* @param {ProxyConfigArrayItem} proxyConfig
145-
*/
146-
147-
/**
148-
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
140+
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
149141
*/
150142

151143
/**
@@ -2131,8 +2123,6 @@ class Server {
21312123
* @returns {RequestHandler | undefined} request handler
21322124
*/
21332125
const getProxyMiddleware = (proxyConfig) => {
2134-
// It is possible to use the `bypass` method without a `target` or `router`.
2135-
// However, the proxy middleware has no use in this case, and will fail to instantiate.
21362126
if (proxyConfig.target) {
21372127
const context = proxyConfig.context || proxyConfig.path;
21382128

@@ -2145,15 +2135,6 @@ class Server {
21452135
if (proxyConfig.router) {
21462136
return createProxyMiddleware(proxyConfig);
21472137
}
2148-
2149-
// TODO improve me after drop `bypass` to always generate error when configuration is bad
2150-
if (!proxyConfig.bypass) {
2151-
util.deprecate(
2152-
() => {},
2153-
`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`,
2154-
"DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT",
2155-
)();
2156-
}
21572138
};
21582139

21592140
/**
@@ -2205,7 +2186,7 @@ class Server {
22052186
if (newProxyConfig !== proxyConfig) {
22062187
proxyConfig = newProxyConfig;
22072188

2208-
const socket = req.socket || req.connection;
2189+
const socket = req.socket;
22092190
// @ts-expect-error
22102191
const server = socket ? socket.server : null;
22112192

@@ -2219,40 +2200,11 @@ class Server {
22192200
}
22202201
}
22212202

2222-
// - Check if we have a bypass function defined
2223-
// - In case the bypass function is defined we'll retrieve the
2224-
// bypassUrl from it otherwise bypassUrl would be null
2225-
// TODO remove in the next major in favor `context` and `router` options
2226-
const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
2227-
if (isByPassFuncDefined) {
2228-
util.deprecate(
2229-
() => {},
2230-
"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",
2231-
"DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT",
2232-
)();
2233-
}
2234-
const bypassUrl = isByPassFuncDefined
2235-
? await /** @type {ByPass} */ (proxyConfig.bypass)(
2236-
req,
2237-
res,
2238-
proxyConfig,
2239-
)
2240-
: null;
2241-
2242-
if (typeof bypassUrl === "boolean") {
2243-
// skip the proxy
2244-
res.statusCode = 404;
2245-
req.url = "";
2246-
next();
2247-
} else if (typeof bypassUrl === "string") {
2248-
// byPass to that url
2249-
req.url = bypassUrl;
2250-
next();
2251-
} else if (proxyMiddleware) {
2203+
if (proxyMiddleware) {
22522204
return proxyMiddleware(req, res, next);
2253-
} else {
2254-
next();
22552205
}
2206+
2207+
next();
22562208
};
22572209

22582210
middlewares.push({

test/server/proxy-option.test.js

Lines changed: 2 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
const path = require("node:path");
4-
const util = require("node:util");
54
const express = require("express");
65
const request = require("supertest");
76
const webpack = require("webpack");
@@ -23,48 +22,6 @@ const proxyOptionPathsAsProperties = [
2322
target: `http://localhost:${port2}`,
2423
pathRewrite: { "^/api": "" },
2524
},
26-
{
27-
context: "/foo",
28-
bypass(req) {
29-
if (/\.html$/.test(req.path || req.url)) {
30-
return "/index.html";
31-
}
32-
33-
return null;
34-
},
35-
},
36-
{
37-
context: "proxyfalse",
38-
bypass(req) {
39-
if (/\/proxyfalse$/.test(req.path || req.url)) {
40-
return false;
41-
}
42-
},
43-
},
44-
{
45-
context: "/proxy/async",
46-
bypass(req, res) {
47-
if (/\/proxy\/async$/.test(req.path || req.url)) {
48-
return new Promise((resolve) => {
49-
setTimeout(() => {
50-
res.end("proxy async response");
51-
resolve(true);
52-
}, 10);
53-
});
54-
}
55-
},
56-
},
57-
{
58-
context: "/bypass-with-target",
59-
target: `http://localhost:${port1}`,
60-
changeOrigin: true,
61-
secure: false,
62-
bypass(req) {
63-
if (/\.(html)$/i.test(req.path || req.url)) {
64-
return req.url;
65-
}
66-
},
67-
},
6825
];
6926

7027
const proxyOption = [
@@ -77,7 +34,7 @@ const proxyOption = [
7734
let maxServerListeners = 0;
7835
const proxyOptionOfArray = [
7936
{ context: "/proxy1", target: `http://localhost:${port1}` },
80-
function proxy(req, res, next) {
37+
function proxy(req) {
8138
if (req) {
8239
const socket = req.socket || req.connection;
8340
const server = socket ? socket.server : null;
@@ -92,19 +49,6 @@ const proxyOptionOfArray = [
9249
context: "/api/proxy2",
9350
target: `http://localhost:${port2}`,
9451
pathRewrite: { "^/api": "" },
95-
bypass: () => {
96-
if (req) {
97-
const resolveUrl = new URL(req.url, `http://${req.headers.host}`);
98-
const params = new URLSearchParams(resolveUrl.search);
99-
const foo = params.get("foo");
100-
101-
if (foo) {
102-
res.end(`foo+${next.name}+${typeof next}`);
103-
104-
return false;
105-
}
106-
}
107-
},
10852
};
10953
},
11054
];
@@ -246,75 +190,6 @@ describe("proxy option", () => {
246190
expect(response.text).toContain("from proxy2");
247191
});
248192
});
249-
250-
describe("bypass", () => {
251-
it("should log deprecation warning when bypass is used", async () => {
252-
const utilSpy = jest.spyOn(util, "deprecate");
253-
254-
const response = await req.get("/foo/bar.html");
255-
256-
expect(response.status).toBe(200);
257-
expect(response.text).toContain("Hello");
258-
259-
const lastCall = utilSpy.mock.calls[utilSpy.mock.calls.length - 1];
260-
261-
expect(lastCall[1]).toBe(
262-
"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",
263-
);
264-
expect(lastCall[2]).toBe(
265-
"DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT",
266-
);
267-
268-
utilSpy.mockRestore();
269-
});
270-
271-
it("can rewrite a request path", async () => {
272-
const response = await req.get("/foo/bar.html");
273-
274-
expect(response.status).toBe(200);
275-
expect(response.text).toContain("Hello");
276-
});
277-
278-
it("can rewrite a request path regardless of the target defined a bypass option", async () => {
279-
const response = await req.get("/baz/hoge.html");
280-
281-
expect(response.status).toBe(200);
282-
expect(response.text).toContain("Hello");
283-
});
284-
285-
it("should pass through a proxy when a bypass function returns null", async () => {
286-
const response = await req.get("/foo.js");
287-
288-
expect(response.status).toBe(200);
289-
expect(response.text).toContain("Hey");
290-
});
291-
292-
it("should not pass through a proxy when a bypass function returns false", async () => {
293-
const response = await req.get("/proxyfalse");
294-
295-
expect(response.status).toBe(404);
296-
});
297-
298-
it("should wait if bypass returns promise", async () => {
299-
const response = await req.get("/proxy/async");
300-
301-
expect(response.status).toBe(200);
302-
expect(response.text).toContain("proxy async response");
303-
});
304-
305-
it("should work with the 'target' option", async () => {
306-
const response = await req.get("/bypass-with-target/foo.js");
307-
308-
expect(response.status).toBe(404);
309-
});
310-
311-
it("should work with the 'target' option #2", async () => {
312-
const response = await req.get("/bypass-with-target/index.html");
313-
314-
expect(response.status).toBe(200);
315-
expect(response.text).toContain("Hello");
316-
});
317-
});
318193
});
319194

320195
describe("as an option is an object with the `context` option", () => {
@@ -502,7 +377,7 @@ describe("proxy option", () => {
502377
const response = await req.get("/api/proxy2?foo=true");
503378

504379
expect(response.statusCode).toBe(200);
505-
expect(response.text).toBe("foo+next+function");
380+
expect(response.text).toBe("from proxy2");
506381
});
507382

508383
it("should not exist multiple close events registered", async () => {

types/lib/Server.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,6 @@ declare namespace Server {
14651465
ClientConnection,
14661466
WebSocketServer,
14671467
WebSocketServerImplementation,
1468-
ByPass,
14691468
ProxyConfigArrayItem,
14701469
ProxyConfigArray,
14711470
OpenApp,
@@ -1655,16 +1654,9 @@ type WebSocketServerImplementation = {
16551654
implementation: WebSocketServer;
16561655
clients: ClientConnection[];
16571656
};
1658-
type ByPass = (
1659-
req: Request,
1660-
res: Response,
1661-
proxyConfig: ProxyConfigArrayItem,
1662-
) => any;
16631657
type ProxyConfigArrayItem = {
16641658
path?: HttpProxyMiddlewareOptionsFilter | undefined;
16651659
context?: HttpProxyMiddlewareOptionsFilter | undefined;
1666-
} & {
1667-
bypass?: ByPass;
16681660
} & HttpProxyMiddlewareOptions;
16691661
type ProxyConfigArray = (
16701662
| ProxyConfigArrayItem

0 commit comments

Comments
 (0)