Skip to content

Commit 3128888

Browse files
author
vhess
committed
wip: fix typescript errors
1 parent a24e06b commit 3128888

File tree

6 files changed

+45
-43
lines changed

6 files changed

+45
-43
lines changed

lib/http-proxy/index.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { EventEmitter } from "node:events";
77
import type { Stream } from "node:stream";
88
import debug from "debug";
99
import { toURL } from "./common";
10-
import type { Agent, Dispatcher } from "undici";
11-
import { Agent as UndiciAgent, interceptors } from "undici";
1210

1311
const log = debug("http-proxy-3");
1412

@@ -95,28 +93,30 @@ export interface ServerOptions {
9593
*/
9694
ca?: string;
9795
/** Enable undici for HTTP/2 support. Set to true for defaults, or provide custom configuration. */
98-
fetch?: boolean | FetchOptions;
96+
fetch?: boolean | FetchOptions;
9997
}
10098

99+
export type Dispatcher = RequestInit["dispatcher"];
100+
101101
export interface FetchOptions {
102-
/** Undici Agent configuration */
103-
dispatcher?: Dispatcher;
104-
/** Undici request options */
105-
requestOptions?: RequestInit;
106-
/** Called before making the undici request */
107-
onBeforeRequest?: (
108-
requestOptions: RequestInit,
109-
req: http.IncomingMessage,
110-
res: http.ServerResponse,
111-
options: NormalizedServerOptions,
112-
) => void | Promise<void>;
113-
/** Called after receiving the undici response */
114-
onAfterResponse?: (
115-
response: Response,
116-
req: http.IncomingMessage,
117-
res: http.ServerResponse,
118-
options: NormalizedServerOptions,
119-
) => void | Promise<void>;
102+
/** Undici Agent configuration */
103+
dispatcher?: Dispatcher;
104+
/** Undici request options */
105+
requestOptions?: RequestInit;
106+
/** Called before making the undici request */
107+
onBeforeRequest?: (
108+
requestOptions: RequestInit,
109+
req: http.IncomingMessage,
110+
res: http.ServerResponse,
111+
options: NormalizedServerOptions,
112+
) => void | Promise<void>;
113+
/** Called after receiving the undici response */
114+
onAfterResponse?: (
115+
response: Response,
116+
req: http.IncomingMessage,
117+
res: http.ServerResponse,
118+
options: NormalizedServerOptions,
119+
) => void | Promise<void>;
120120
}
121121

122122

@@ -265,7 +265,7 @@ export class ProxyServer<TIncomingMessage extends typeof http.IncomingMessage =
265265
this.webPasses = Object.values(WEB_PASSES) as Array<PassFunctions<TIncomingMessage, TServerResponse, TError>['web']>;
266266
this.wsPasses = Object.values(WS_PASSES) as Array<PassFunctions<TIncomingMessage, TServerResponse, TError>['ws']>;
267267
this.on("error", this.onError);
268-
}
268+
}
269269

270270
/**
271271
* Creates the proxy server with specified options.

lib/http-proxy/passes/web-incoming.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { Socket } from "node:net";
1717
import type Stream from "node:stream";
1818
import * as followRedirects from "follow-redirects";
1919
import type { Dispatcher } from "undici";
20-
import type { ErrorCallback, FetchOptions, NormalizedServerOptions, NormalizeProxyTarget, ProxyServer, ProxyTarget, ProxyTargetUrl, ServerOptions, UndiciOptions } from "..";
20+
import type { ErrorCallback, FetchOptions, NormalizedServerOptions, NormalizeProxyTarget, ProxyServer, ProxyTarget, ProxyTargetUrl, ServerOptions } from "..";
2121
import * as common from "../common";
2222
import { type EditableResponse, OUTGOING_PASSES } from "./web-outgoing";
2323
import { Readable } from "node:stream";
@@ -236,11 +236,12 @@ async function stream2(
236236
);
237237

238238
const requestOptions: RequestInit = {
239-
origin: new URL(outgoingOptions.url).origin,
240-
method: outgoingOptions.method as Dispatcher.HttpMethod,
241-
headers: outgoingOptions.headers || {},
242-
path: outgoingOptions.path || "/",
243-
};
239+
method: outgoingOptions.method,
240+
}
241+
242+
if (fetchOptions.dispatcher) {
243+
requestOptions.dispatcher = fetchOptions.dispatcher;
244+
}
244245

245246
// Handle request body
246247
if (options.buffer) {
@@ -283,14 +284,15 @@ async function stream2(
283284
const outgoingOptions = common.setupOutgoing(options.ssl || {}, options, req);
284285

285286
const requestOptions: RequestInit = {
286-
origin: new URL(outgoingOptions.url).origin,
287287
method: outgoingOptions.method as Dispatcher.HttpMethod,
288-
headers: outgoingOptions.headers || {},
289-
path: outgoingOptions.path || "/",
290-
headersTimeout: options.proxyTimeout,
288+
headers: outgoingOptions.headers as Record<string, string>,
291289
...fetchOptions.requestOptions
292290
};
293291

292+
if (fetchOptions.dispatcher) {
293+
requestOptions.dispatcher = fetchOptions.dispatcher;
294+
}
295+
294296
if (options.auth) {
295297
requestOptions.headers = { ...requestOptions.headers, authorization: `Basic ${Buffer.from(options.auth).toString("base64")}` };
296298
}

lib/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Agent } from 'undici';
12
import {
23
type ErrorCallback,
34
ProxyServer,
@@ -33,8 +34,8 @@ import type * as http from 'node:http';
3334

3435
function createProxyServer<TIncomingMessage extends typeof http.IncomingMessage = typeof http.IncomingMessage, TServerResponse extends typeof http.ServerResponse = typeof http.ServerResponse, TError = Error>(options: ServerOptions = {}): ProxyServer<TIncomingMessage, TServerResponse, TError> {
3536
// Check if we're in forced undici mode
36-
if (process.env.FORCE_UNDICI_PATH === 'true' && options.undici === undefined) {
37-
options = { ...options, undici: { agentOptions: { allowH2: true } } };
37+
if (process.env.FORCE_FETCH_PATH === 'true' && options.fetch === undefined) {
38+
options = { ...options, fetch: { dispatcher: new Agent({ allowH2: true, connect: { rejectUnauthorized: true } }) as any } };
3839
}
3940

4041
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);

lib/test/http/proxy-callbacks.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import * as httpProxy from "../..";
99
import getPort from "../get-port";
1010
import fetch from "node-fetch";
1111
import { describe, it, expect, beforeAll, afterAll } from "vitest";
12-
import { Agent, setGlobalDispatcher } from "undici";
12+
import { Agent } from "undici";
1313

14-
setGlobalDispatcher(new Agent({
15-
allowH2: true
16-
}));
1714

1815
describe("Undici callback functions (onBeforeRequest and onAfterResponse)", () => {
1916
let ports: Record<'target' | 'proxy', number>;
@@ -48,8 +45,10 @@ describe("Undici callback functions (onBeforeRequest and onAfterResponse)", () =
4845

4946
const proxy = httpProxy.createServer({
5047
target: `http://localhost:${ports.target}`,
51-
undici: {
52-
agentOptions: { allowH2: true }, // Enable undici code path
48+
fetch: {
49+
dispatcher: new Agent({
50+
allowH2: true
51+
}) as any, // Enable undici code path
5352
onBeforeRequest: async (requestOptions, _req, _res, _options) => {
5453
onBeforeRequestCalled = true;
5554
// Modify the outgoing request
@@ -62,7 +61,7 @@ describe("Undici callback functions (onBeforeRequest and onAfterResponse)", () =
6261
onAfterResponse: async (response, _req, _res, _options) => {
6362
onAfterResponseCalled = true;
6463
capturedResponse = response;
65-
console.log(`Response received: ${response.statusCode}`);
64+
console.log(`Response received: ${response.status}`);
6665
}
6766
}
6867
}); servers.proxy = proxy.listen(ports.proxy);

lib/test/http/proxy-http2-to-http2.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("Basic example of proxying over HTTP2 to a target HTTP2 server", () =>
4646
.createServer({
4747
target: `https://localhost:${ports.http2}`,
4848
ssl,
49-
undici: { agentOptions: { allowH2: true } },
49+
fetch: { dispatcher: new Agent({ allowH2: true }) as any },
5050
// without secure false, clients will fail and this is broken:
5151
secure: false,
5252
})

lib/test/lib/http-proxy-passes-web-incoming.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe("#createProxyServer.web() using own http server", () => {
306306
target: address(8083),
307307
proxyTimeout: 100,
308308
timeout: 150, // so client exits and isn't left handing the test.
309-
undici: true
309+
fetch: true
310310
});
311311

312312
const server = require("net").createServer().listen(ports["8083"]);

0 commit comments

Comments
 (0)