Skip to content

Commit b2e6c92

Browse files
committed
refactor!: remove legacy node Url support
1 parent aebb5c6 commit b2e6c92

6 files changed

Lines changed: 27 additions & 37 deletions

File tree

‎AGENTS.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ pnpm test # Lint + typecheck + tests with coverage
149149
## Key Types
150150

151151
```ts
152-
type ProxyTarget = string | URL | Partial<LegacyURL> | ProxyTargetDetailed;
152+
type ProxyTarget = string | URL | ProxyTargetDetailed;
153153

154154
interface ProxyTargetDetailed {
155-
host: string;
156-
port: number;
155+
host?: string;
156+
port?: number | string;
157157
protocol?: string;
158158
hostname?: string;
159159
socketPath?: string;
@@ -162,7 +162,7 @@ interface ProxyTargetDetailed {
162162

163163
interface ProxyServerOptions {
164164
target?: ProxyTarget; // Proxy destination
165-
forward?: ProxyTarget; // Forward destination (fires & forget)
165+
forward?: ProxyTarget; // Forward destination
166166
ws?: boolean; // Enable WebSocket proxying
167167
xfwd?: boolean; // Add x-forwarded-* headers
168168
changeOrigin?: boolean; // Rewrite Host header to target

‎src/_utils.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import httpNative from "node:http";
22
import httpsNative from "node:https";
33
import net from "node:net";
44
import type tls from "node:tls";
5-
import type { Url as LegacyURL } from "node:url";
6-
75
import type { ProxyServerOptions, ProxyTarget, ProxyTargetDetailed } from "./types.ts";
86

97
const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;
@@ -101,10 +99,7 @@ export function setupOutgoing(
10199

102100
// the final path is target path + relative path requested by user:
103101
const target = options[forward || "target"];
104-
const targetPath =
105-
target && options.prependPath !== false
106-
? (target as URL).pathname || (target as LegacyURL).path || ""
107-
: "";
102+
const targetPath = target && options.prependPath !== false ? (target as URL).pathname || "" : "";
108103

109104
const reqUrl = req.url || "";
110105
const qIdx = reqUrl.indexOf("?");

‎src/middleware/web-outgoing.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const setRedirectHostRewrite = defineProxyOutgoingMiddleware(
3232
redirectRegex.test(String(proxyRes.statusCode))
3333
) {
3434
const target =
35-
options.target instanceof URL ? options.target : new URL(options.target as string | URL); // TODO: handle legacy url?
35+
options.target instanceof URL ? options.target : new URL(options.target as string | URL);
3636
const u = new URL(proxyRes.headers.location);
3737

3838
// Make sure the redirected host matches the target host before rewriting

‎src/types.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type * as stream from "node:stream";
2-
import type { Url as LegacyURL } from "node:url";
32

43
export interface ProxyTargetDetailed {
5-
host: string;
6-
port: number;
4+
host?: string;
5+
port?: number | string;
76
protocol?: string;
87
hostname?: string;
98
socketPath?: string;
@@ -16,13 +15,13 @@ export interface ProxyTargetDetailed {
1615
secureProtocol?: string;
1716
}
1817

19-
export type ProxyTarget = string | URL | Partial<LegacyURL> | ProxyTargetDetailed;
18+
export type ProxyTarget = string | URL | ProxyTargetDetailed;
2019

2120
export interface ProxyServerOptions {
2221
/** URL string to be parsed. */
2322
target?: ProxyTarget;
2423
/** URL string to be parsed. */
25-
forward?: Exclude<ProxyTarget, ProxyTargetDetailed>;
24+
forward?: ProxyTarget;
2625
/** Object to be passed to http(s).request. */
2726
agent?: any;
2827
/** Object to be passed to https.createServer(). */

‎test/_utils.test.ts‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,28 @@ describe("lib/http-proxy/common.js", () => {
182182

183183
it("should keep the original target path in the outgoing path", () => {
184184
const outgoing = {} as any;
185-
common.setupOutgoing(outgoing, { target: { path: "some-path" } }, {
185+
common.setupOutgoing(outgoing, { target: URL.parse("http://localhost/some-path")! }, {
186186
url: "am",
187187
} as any);
188188

189-
expect(outgoing.path).to.eql("some-path/am");
189+
expect(outgoing.path).to.eql("/some-path/am");
190190
});
191191

192192
it("should keep the original forward path in the outgoing path", () => {
193193
const outgoing = {} as any;
194194
common.setupOutgoing(
195195
outgoing,
196196
{
197-
target: {},
198-
forward: {
199-
path: "some-path",
200-
},
197+
target: "http://localhost",
198+
forward: URL.parse("http://localhost/some-path")!,
201199
},
202200
{
203201
url: "am",
204202
} as any,
205203
"forward",
206204
);
207205

208-
expect(outgoing.path).to.eql("some-path/am");
206+
expect(outgoing.path).to.eql("/some-path/am");
209207
});
210208

211209
it("should properly detect https/wss protocol without the colon", () => {
@@ -229,7 +227,7 @@ describe("lib/http-proxy/common.js", () => {
229227
common.setupOutgoing(
230228
outgoing,
231229
{
232-
target: { path: "hellothere" },
230+
target: URL.parse("http://localhost/hellothere")!,
233231
prependPath: false,
234232
},
235233
{ url: "hi" } as any,
@@ -243,7 +241,7 @@ describe("lib/http-proxy/common.js", () => {
243241
common.setupOutgoing(
244242
outgoing,
245243
{
246-
target: { path: "/forward" },
244+
target: URL.parse("http://localhost/forward")!,
247245
},
248246
{ url: "/static/path" } as any,
249247
);
@@ -270,7 +268,7 @@ describe("lib/http-proxy/common.js", () => {
270268
common.setupOutgoing(
271269
outgoing,
272270
{
273-
target: { path: "/forward" },
271+
target: URL.parse("http://localhost/forward")!,
274272
},
275273
{
276274
url: "/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2",
@@ -364,7 +362,7 @@ describe("lib/http-proxy/common.js", () => {
364362
});
365363

366364
describe("when using changeOrigin", () => {
367-
it("should correctly set the port to the host when it is a non-standard port using url.parse", () => {
365+
it("should correctly set the port to the host when it is a non-standard port using URL.parse", () => {
368366
const outgoing = {} as any;
369367
const myEndpoint = "https://myCouch.com:6984";
370368
common.setupOutgoing(
@@ -459,14 +457,13 @@ describe("lib/http-proxy/common.js", () => {
459457
expect(outgoing.method).eql("POST");
460458
});
461459

462-
// url.parse('').path => null
463-
it("should not pass null as last arg to #urlJoin", () => {
460+
it("should handle empty pathname target", () => {
464461
const outgoing = {} as any;
465-
common.setupOutgoing(outgoing, { target: { path: "" } }, {
462+
common.setupOutgoing(outgoing, { target: URL.parse("http://localhost")! }, {
466463
url: "",
467464
} as any);
468465

469-
expect(outgoing.path).toBe("/"); // leading slash is new in httpxy
466+
expect(outgoing.path).toBe("/");
470467
});
471468
});
472469

‎test/middleware/web-incoming.test.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, it, expect } from "vitest";
33
import * as webPasses from "../../src/middleware/web-incoming.ts";
44
import * as httpProxy from "../../src/index.ts";
55
import concat from "concat-stream";
6-
import url from "node:url";
76
import http from "node:http";
87
import type { AddressInfo } from "node:net";
98

@@ -614,7 +613,7 @@ describe("#followRedirects", () => {
614613
const { resolve, promise } = Promise.withResolvers<void>();
615614

616615
const source = http.createServer(function (req: any, res: any) {
617-
if (url.parse(req.url).pathname === "/redirect") {
616+
if (new URL(req.url, "http://localhost").pathname === "/redirect") {
618617
res.writeHead(200, { "Content-Type": "text/plain" });
619618
res.end("ok");
620619
return;
@@ -647,7 +646,7 @@ describe("#followRedirects", () => {
647646
const { resolve, promise } = Promise.withResolvers<void>();
648647

649648
const source = http.createServer(function (req: any, res: any) {
650-
if (url.parse(req.url).pathname === "/dest") {
649+
if (new URL(req.url, "http://localhost").pathname === "/dest") {
651650
res.writeHead(200, { "Content-Type": "text/plain" });
652651
res.end(req.method);
653652
return;
@@ -686,7 +685,7 @@ describe("#followRedirects", () => {
686685
const postBody = "test-body-content";
687686

688687
const source = http.createServer(function (req: any, res: any) {
689-
if (url.parse(req.url).pathname === "/dest") {
688+
if (new URL(req.url, "http://localhost").pathname === "/dest") {
690689
let body = "";
691690
req.on("data", (chunk: Buffer) => (body += chunk));
692691
req.on("end", () => {
@@ -763,7 +762,7 @@ describe("#followRedirects", () => {
763762
const { resolve, promise } = Promise.withResolvers<void>();
764763

765764
const source = http.createServer(function (req: any, res: any) {
766-
if (url.parse(req.url).pathname === "/sub/dest") {
765+
if (new URL(req.url, "http://localhost").pathname === "/sub/dest") {
767766
res.writeHead(200, { "Content-Type": "text/plain" });
768767
res.end("ok");
769768
return;
@@ -797,7 +796,7 @@ describe("#followRedirects", () => {
797796
let proxyResCount = 0;
798797

799798
const source = http.createServer(function (req: any, res: any) {
800-
if (url.parse(req.url).pathname === "/final") {
799+
if (new URL(req.url, "http://localhost").pathname === "/final") {
801800
res.writeHead(200, { "Content-Type": "text/plain" });
802801
res.end("done");
803802
return;

0 commit comments

Comments
 (0)