Skip to content

Commit 697d838

Browse files
author
vhess
committed
small refactor, fix ts errors
1 parent bf7f015 commit 697d838

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ The names of passes are exported as WEB_PASSES from this module.
77
88
*/
99

10+
import type {
11+
IncomingMessage as Request,
12+
ServerResponse as Response,
13+
} from "node:http";
1014
import * as http from "node:http";
1115
import * as https from "node:https";
12-
import { OUTGOING_PASSES, EditableResponse } from "./web-outgoing";
13-
import * as common from "../common";
16+
import type { Socket } from "node:net";
17+
import type Stream from "node:stream";
1418
import * as followRedirects from "follow-redirects";
15-
import {
16-
type IncomingMessage as Request,
17-
type ServerResponse as Response,
18-
} from "node:http";
19-
import { type Socket } from "node:net";
19+
import { Agent, type Dispatcher, interceptors } from "undici";
2020
import type { ErrorCallback, NormalizedServerOptions, NormalizeProxyTarget, ProxyServer, ProxyTarget, ProxyTargetUrl, ServerOptions } from "..";
21-
import Stream from "node:stream";
22-
import { Agent, Dispatcher, interceptors } from "undici";
21+
import * as common from "../common";
22+
import { type EditableResponse, OUTGOING_PASSES } from "./web-outgoing";
2323

2424
export type ProxyResponse = Request & {
2525
headers: { [key: string]: string | string[] };
@@ -79,7 +79,8 @@ export function stream(req: Request, res: Response, options: NormalizedServerOpt
7979
// And we begin!
8080
server.emit("start", req, res, options.target || options.forward!);
8181

82-
if (options.agentOptions || options.requestOptions || true) {
82+
if (options.agentOptions || options.requestOptions
83+
) {
8384
return stream2(req, res, options, _, server, cb);
8485
}
8586

@@ -274,7 +275,7 @@ async function stream2(
274275
};
275276

276277
if (options.auth) {
277-
requestOptions.headers["authorization"] = `Basic ${Buffer.from(options.auth).toString("base64")}`
278+
requestOptions.headers = { ...requestOptions.headers, authorization: `Basic ${Buffer.from(options.auth).toString("base64")}` };
278279
}
279280

280281
if (options.buffer) {
@@ -283,46 +284,48 @@ async function stream2(
283284
requestOptions.body = req;
284285
}
285286

286-
// server?.emit("proxyReq", requestOptions, req, res, options, undefined);
287-
let proxyRes: ProxyResponse
288-
289-
287+
288+
289+
290290
try {
291291
const { statusCode, headers, body } = await agent.request(
292292
requestOptions
293293
);
294-
proxyRes = {} as ProxyResponse;
295-
proxyRes.statusCode = statusCode;
296-
proxyRes.headers = headers as { [key: string]: string | string[] };
297-
proxyRes.rawHeaders = Object.entries(headers).flatMap(([key, value]) => {
294+
295+
// ProxyRes is used in the outgoing passes
296+
// But since only certain properties are used, we can fake it here
297+
// to avoid having to refactor everything.
298+
const fakeProxyRes = {} as ProxyResponse;
299+
300+
fakeProxyRes.statusCode = statusCode;
301+
fakeProxyRes.headers = headers as { [key: string]: string | string[] };
302+
fakeProxyRes.rawHeaders = Object.entries(headers).flatMap(([key, value]) => {
298303
if (Array.isArray(value)) {
299-
return value.map(v => [key, v]).flat();
304+
return value.flatMap(v => (v != null ? [key, v] : []));
300305
}
301-
return [key, value];
302-
});
303-
proxyRes.pipe = body.pipe.bind(body);
306+
return value != null ? [key, value] : [];
307+
}) as string[];
308+
fakeProxyRes.pipe = body.pipe.bind(body);
304309

305310

306-
server?.emit("proxyRes", proxyRes, req, res);
307-
308311
if (!res.headersSent && !options.selfHandleResponse) {
309312
for (const pass of web_o) {
310313
// note: none of these return anything
311-
pass(req, res as EditableResponse, proxyRes, options as NormalizedServerOptions & { target: NormalizeProxyTarget<ProxyTarget> });
314+
pass(req, res as EditableResponse, fakeProxyRes, options as NormalizedServerOptions & { target: NormalizeProxyTarget<ProxyTarget> });
312315
}
313316
}
314317

315318
if (!res.writableEnded) {
316319
// Allow us to listen for when the proxy has completed
317320
body.on("end", () => {
318-
server?.emit("end", req, res, proxyRes);
321+
server?.emit("end", req, res, fakeProxyRes);
319322
});
320323
// We pipe to the response unless its expected to be handled by the user
321324
if (!options.selfHandleResponse) {
322325
body.pipe(res);
323326
}
324327
} else {
325-
server?.emit("end", req, res, proxyRes);
328+
server?.emit("end", req, res, fakeProxyRes);
326329
}
327330

328331

0 commit comments

Comments
 (0)