-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: remove bypass option from proxy configuration #5641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
243e8d7
76c0862
98bdf5a
e3aba33
d725363
44712d3
e61d44b
051176c
bdb9c1a
b5fb00d
dce874a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| "use strict"; | ||
|
|
||
| const path = require("node:path"); | ||
| const util = require("node:util"); | ||
| const express = require("express"); | ||
| const request = require("supertest"); | ||
| const webpack = require("webpack"); | ||
|
|
@@ -19,51 +18,14 @@ const proxyOptionPathsAsProperties = [ | |
| target: `http://localhost:${port1}`, | ||
| }, | ||
| { | ||
| context: "/api/proxy2", | ||
| path: "/api/proxy2", | ||
| target: `http://localhost:${port2}`, | ||
| pathRewrite: { "^/api": "" }, | ||
| }, | ||
| { | ||
| context: "/foo", | ||
| bypass(req) { | ||
| if (/\.html$/.test(req.path || req.url)) { | ||
| return "/index.html"; | ||
| } | ||
|
|
||
| return null; | ||
| }, | ||
| }, | ||
| { | ||
| context: "proxyfalse", | ||
| bypass(req) { | ||
| if (/\/proxyfalse$/.test(req.path || req.url)) { | ||
| return false; | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| context: "/proxy/async", | ||
| bypass(req, res) { | ||
| if (/\/proxy\/async$/.test(req.path || req.url)) { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| res.end("proxy async response"); | ||
| resolve(true); | ||
| }, 10); | ||
| }); | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| context: "/bypass-with-target", | ||
| pathFilter: "**/*.html", | ||
| target: `http://localhost:${port1}`, | ||
| changeOrigin: true, | ||
| secure: false, | ||
| bypass(req) { | ||
| if (/\.(html)$/i.test(req.path || req.url)) { | ||
| return req.url; | ||
| } | ||
| }, | ||
| pathRewrite: () => "/index.html", | ||
| }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rewrite these test to using new options from Also let's add in migration guide how to achive the same behaviour with new http proxy middleware |
||
| ]; | ||
|
|
||
|
|
@@ -77,7 +39,7 @@ const proxyOption = [ | |
| let maxServerListeners = 0; | ||
| const proxyOptionOfArray = [ | ||
| { context: "/proxy1", target: `http://localhost:${port1}` }, | ||
| function proxy(req, res, next) { | ||
| function proxy(req) { | ||
| if (req) { | ||
| const socket = req.socket || req.connection; | ||
| const server = socket ? socket.server : null; | ||
|
|
@@ -92,19 +54,6 @@ const proxyOptionOfArray = [ | |
| context: "/api/proxy2", | ||
| target: `http://localhost:${port2}`, | ||
| pathRewrite: { "^/api": "" }, | ||
| bypass: () => { | ||
| if (req) { | ||
| const resolveUrl = new URL(req.url, `http://${req.headers.host}`); | ||
| const params = new URLSearchParams(resolveUrl.search); | ||
| const foo = params.get("foo"); | ||
|
|
||
| if (foo) { | ||
| res.end(`foo+${next.name}+${typeof next}`); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| ]; | ||
|
|
@@ -167,6 +116,9 @@ describe("proxy option", () => { | |
| proxyApp1.get("/api", (req, res) => { | ||
| res.send("api response from proxy1"); | ||
| }); | ||
| proxyApp1.get("/index.html", (req, res) => { | ||
| res.send("Hello"); | ||
| }); | ||
| proxyApp2.get("/proxy2", (req, res) => { | ||
| res.send("from proxy2"); | ||
| }); | ||
|
|
@@ -248,26 +200,6 @@ describe("proxy option", () => { | |
| }); | ||
|
|
||
| describe("bypass", () => { | ||
| it("should log deprecation warning when bypass is used", async () => { | ||
| const utilSpy = jest.spyOn(util, "deprecate"); | ||
|
|
||
| const response = await req.get("/foo/bar.html"); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.text).toContain("Hello"); | ||
|
|
||
| const lastCall = utilSpy.mock.calls[utilSpy.mock.calls.length - 1]; | ||
|
|
||
| expect(lastCall[1]).toBe( | ||
| "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", | ||
| ); | ||
| expect(lastCall[2]).toBe( | ||
| "DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT", | ||
| ); | ||
|
|
||
| utilSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("can rewrite a request path", async () => { | ||
| const response = await req.get("/foo/bar.html"); | ||
|
|
||
|
|
@@ -295,13 +227,6 @@ describe("proxy option", () => { | |
| expect(response.status).toBe(404); | ||
| }); | ||
|
|
||
| it("should wait if bypass returns promise", async () => { | ||
| const response = await req.get("/proxy/async"); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.text).toContain("proxy async response"); | ||
| }); | ||
|
|
||
|
Comment on lines
-298
to
-304
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neither |
||
| it("should work with the 'target' option", async () => { | ||
| const response = await req.get("/bypass-with-target/foo.js"); | ||
|
|
||
|
|
@@ -498,13 +423,6 @@ describe("proxy option", () => { | |
| expect(response.text).toContain("from proxy2"); | ||
| }); | ||
|
|
||
| it("should allow req, res, and next", async () => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const response = await req.get("/api/proxy2?foo=true"); | ||
|
|
||
| expect(response.statusCode).toBe(200); | ||
| expect(response.text).toBe("foo+next+function"); | ||
| }); | ||
|
|
||
| it("should not exist multiple close events registered", async () => { | ||
| expect(maxServerListeners).toBeLessThanOrEqual(1); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect that using router: "() =>
http://localhost:${port3}" should work and be somewhat similar to whatbypassdid, but it doesn’t fully work. I’m still thinking about a way to recreatebypass.With
bypass, the request never went through the proxy, which is why it could returnindex.htmlfrom the webpack-dev-server itself. Now we need to find a way for the request to go through the proxy and still reach the original server, without having to create a new route in the proxy.